diff --git a/dist/cjs/oembed-parser.js b/dist/cjs/oembed-parser.js index bc2298a..1144b33 100644 --- a/dist/cjs/oembed-parser.js +++ b/dist/cjs/oembed-parser.js @@ -1,24 +1,11 @@ -// oembed-parser@3.0.4, by @ndaidong - built with esbuild at 2022-05-23T15:11:10.458Z - published under MIT license +"use strict"; +// oembed-parser@3.0.5, by @ndaidong - built with esbuild at 2022-06-23T07:21:16.535Z - published under MIT license var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; -var __getOwnPropSymbols = Object.getOwnPropertySymbols; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; -var __propIsEnum = Object.prototype.propertyIsEnumerable; -var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; -var __spreadValues = (a, b) => { - for (var prop in b || (b = {})) - if (__hasOwnProp.call(b, prop)) - __defNormalProp(a, prop, b[prop]); - if (__getOwnPropSymbols) - for (var prop of __getOwnPropSymbols(b)) { - if (__propIsEnum.call(b, prop)) - __defNormalProp(a, prop, b[prop]); - } - return a; -}; var __commonJS = (cb, mod) => function __require() { return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports; }; @@ -12365,45 +12352,45 @@ var require_axios2 = __commonJS({ } }); -// node_modules/.pnpm/bellajs@11.0.2/node_modules/bellajs/dist/cjs/bella.js +// node_modules/.pnpm/bellajs@11.0.3/node_modules/bellajs/dist/cjs/bella.js var require_bella = __commonJS({ - "node_modules/.pnpm/bellajs@11.0.2/node_modules/bellajs/dist/cjs/bella.js"(exports, module2) { + "node_modules/.pnpm/bellajs@11.0.3/node_modules/bellajs/dist/cjs/bella.js"(exports, module2) { var S = Object.defineProperty; var U = Object.getOwnPropertyDescriptor; var q = Object.getOwnPropertyNames; var F = Object.prototype.hasOwnProperty; - var R = (t) => S(t, "__esModule", { value: true }); - var z = (t, e) => { + var R = (t, e) => { for (var r in e) S(t, r, { get: e[r], enumerable: true }); }; - var B = (t, e, r, n) => { + var z = (t, e, r, n) => { if (e && typeof e == "object" || typeof e == "function") for (let o of q(e)) - !F.call(t, o) && (r || o !== "default") && S(t, o, { get: () => e[o], enumerable: !(n = U(e, o)) || n.enumerable }); + !F.call(t, o) && o !== r && S(t, o, { get: () => e[o], enumerable: !(n = U(e, o)) || n.enumerable }); return t; }; - var H = ((t) => (e, r) => t && t.get(e) || (r = B(R({}), e, 1), t && t.set(e, r), r))(typeof WeakMap != "undefined" ? /* @__PURE__ */ new WeakMap() : 0); - var gt = {}; - z(gt, { clone: () => M, compose: () => it, copies: () => I, curry: () => ct, equals: () => A, escapeHTML: () => Z, formatDateString: () => ot, formatTimeAgo: () => st, genid: () => et, hasProperty: () => a, isArray: () => u, isBoolean: () => V, isDate: () => h, isElement: () => G, isEmail: () => Y, isEmpty: () => d, isFunction: () => X, isInteger: () => $, isLetter: () => W, isNil: () => _, isNull: () => E, isNumber: () => y, isObject: () => l, isString: () => p, isUndefined: () => N, maybe: () => b, pick: () => at, pipe: () => ut, randint: () => O, replaceAll: () => w, shuffle: () => P, slugify: () => rt, sort: () => L, sortBy: () => ft, stripAccent: () => D, stripTags: () => Q, truncate: () => K, ucfirst: () => T, ucwords: () => tt, unescapeHTML: () => v, unique: () => lt }); + var B = (t) => z(S({}, "__esModule", { value: true }), t); + var at = {}; + R(at, { clone: () => M, compose: () => ct, copies: () => I, curry: () => st, equals: () => A, escapeHTML: () => Q, formatDateString: () => nt, formatTimeAgo: () => ot, genid: () => tt, hasProperty: () => a, isArray: () => u, isBoolean: () => $, isDate: () => h, isElement: () => X, isEmail: () => W, isEmpty: () => d, isFunction: () => V, isInteger: () => H, isLetter: () => G, isNil: () => _, isNull: () => E, isNumber: () => y, isObject: () => l, isString: () => p, isUndefined: () => N, maybe: () => b, pick: () => ft, pipe: () => it, randint: () => O, replaceAll: () => w, shuffle: () => P, slugify: () => et, sort: () => L, sortBy: () => pt, stripAccent: () => D, stripTags: () => K, truncate: () => J, ucfirst: () => T, ucwords: () => v, unescapeHTML: () => Z, unique: () => ut }); + module2.exports = B(at); var m = (t) => ({}).toString.call(t); - var $ = (t) => Number.isInteger(t); + var H = (t) => Number.isInteger(t); var u = (t) => Array.isArray(t); var p = (t) => String(t) === t; var y = (t) => Number(t) === t; - var V = (t) => Boolean(t) === t; + var $ = (t) => Boolean(t) === t; var E = (t) => m(t) === "[object Null]"; var N = (t) => m(t) === "[object Undefined]"; var _ = (t) => N(t) || E(t); - var X = (t) => m(t) === "[object Function]"; + var V = (t) => m(t) === "[object Function]"; var l = (t) => m(t) === "[object Object]" && !u(t); var h = (t) => t instanceof Date && !isNaN(t.valueOf()); - var G = (t) => m(t).match(/^\[object HTML\w*Element]$/) !== null; - var W = (t) => { + var X = (t) => m(t).match(/^\[object HTML\w*Element]$/) !== null; + var G = (t) => { let e = /^[a-z]+$/i; return p(t) && e.test(t); }; - var Y = (t) => { + var W = (t) => { let e = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i; return p(t) && e.test(t); }; @@ -12438,9 +12425,9 @@ var require_bella = __commonJS({ } return t === e; }; - var J = Number.MAX_SAFE_INTEGER; + var Y = Number.MAX_SAFE_INTEGER; var O = (t, e) => { - if ((!t || t < 0) && (t = 0), e || (e = J), t === e) + if ((!t || t < 0) && (t = 0), e || (e = Y), t === e) return e; t > e && (t = Math.min(t, e), e = Math.max(t, e)); let r = t, n = e - t + 1; @@ -12452,21 +12439,21 @@ var require_bella = __commonJS({ throw new Error("InvalidInput: String required."); return e; }; - var K = (t, e) => { + var J = (t, e) => { let r = g(t), n = e || 140; if (r.length <= n) return r; let o = r.substring(0, n), s = o.split(" "), i = s.length, c = ""; return i > 1 ? (s.pop(), c += s.join(" "), c.length < r.length && (c += "...")) : (o = o.substring(0, n - 3), c = o + "..."), c; }; - var Q = (t) => g(t).replace(/<.*?>/gi, " ").replace(/\s\s+/g, " ").trim(); - var Z = (t) => g(t).replace(/&/g, "&").replace(//g, ">").replace(/"/g, """); - var v = (t) => g(t).replace(/"/g, '"').replace(/</g, "<").replace(/>/g, ">").replace(/&/g, "&"); + var K = (t) => g(t).replace(/<.*?>/gi, " ").replace(/\s\s+/g, " ").trim(); + var Q = (t) => g(t).replace(/&/g, "&").replace(//g, ">").replace(/"/g, """); + var Z = (t) => g(t).replace(/"/g, '"').replace(/</g, "<").replace(/>/g, ">").replace(/&/g, "&"); var T = (t) => { let e = g(t).toLowerCase(); return e.length > 1 ? e.charAt(0).toUpperCase() + e.slice(1) : e.toUpperCase(); }; - var tt = (t) => g(t).split(" ").map((e) => T(e)).join(" "); + var v = (t) => g(t).split(" ").map((e) => T(e)).join(" "); var w = (t, e, r) => { let n = g(t); if (y(e) && (e = String(e)), y(r) && (r = String(r)), p(e) && p(r)) @@ -12493,7 +12480,7 @@ var require_bella = __commonJS({ a(r, o) && r[o].split("|").forEach((i) => n(i, o)); return e; }; - var et = (t, e = "") => { + var tt = (t, e = "") => { let r = "abcdefghijklmnopqrstuvwxyz", n = r.toUpperCase(), s = [r, n, "0123456789"].join("").split("").sort(() => Math.random() > 0.5).join(""), i = s.length, c = Math.max(t || 32, e.length), f = e; for (; f.length < c; ) { let C = O(0, i); @@ -12501,21 +12488,21 @@ var require_bella = __commonJS({ } return f; }; - var rt = (t, e = "-") => D(t).trim().toLowerCase().replace(/\W+/g, " ").replace(/\s+/g, " ").replace(/\s/g, e); + var et = (t, e = "-") => D(t).trim().toLowerCase().replace(/\W+/g, " ").replace(/\s+/g, " ").replace(/\s/g, e); var k = { dateStyle: "medium", timeStyle: "long" }; var j = { second: 1e3, minute: 60, hour: 60, day: 24, week: 7, month: 4, year: 12 }; - var nt = (t) => { + var rt = (t) => { try { return new Intl.Locale(t).language !== ""; } catch { return false; } }; - var ot = (...t) => { - let e = t[0], r = nt(t[1]) ? t[1] : "en", n = t.length >= 3 ? t[2] : t.length === 1 ? k : l(t[1]) ? t[1] : k; + var nt = (...t) => { + let e = t[0], r = rt(t[1]) ? t[1] : "en", n = t.length >= 3 ? t[2] : t.length === 1 ? k : l(t[1]) ? t[1] : k; return new Intl.DateTimeFormat(r, n).format(new Date(e)); }; - var st = (t, e = "en", r = "just now") => { + var ot = (t, e = "en", r = "just now") => { let n = new Date(t), o = Date.now() - n; if (o <= j.second) return r; @@ -12527,12 +12514,12 @@ var require_bella = __commonJS({ } return o = Math.floor(o), new Intl.RelativeTimeFormat(e).format(-o, s); }; - var ct = (t) => { + var st = (t) => { let e = t.length, r = (n, o) => n > 0 ? (...s) => r(n - s.length, [...o, ...s]) : t(...o); return r(e, []); }; - var it = (...t) => t.reduce((e, r) => (n) => e(r(n))); - var ut = (...t) => t.reduce((e, r) => (n) => r(e(n))); + var ct = (...t) => t.reduce((e, r) => (n) => e(r(n))); + var it = (...t) => t.reduce((e, r) => (n) => r(e(n))); var x = (t, e, r, n = {}) => { let { writable: o = false, configurable: s = false, enumerable: i = false } = n; Object.defineProperty(t, e, { value: r, writable: o, configurable: s, enumerable: i }); @@ -12563,13 +12550,13 @@ var require_bella = __commonJS({ } return e; }; - var lt = (t = []) => [...new Set(t)]; - var pt = (t, e) => t > e ? 1 : t < e ? -1 : 0; + var ut = (t = []) => [...new Set(t)]; + var lt = (t, e) => t > e ? 1 : t < e ? -1 : 0; var L = (t = [], e = null) => { - let r = [...t], n = e || pt; + let r = [...t], n = e || lt; return r.sort(n), r; }; - var ft = (t = [], e = 1, r = "") => !p(r) || !a(t[0], r) ? t : L(t, (n, o) => n[r] > o[r] ? e : n[r] < o[r] ? -1 * e : 0); + var pt = (t = [], e = 1, r = "") => !p(r) || !a(t[0], r) ? t : L(t, (n, o) => n[r] > o[r] ? e : n[r] < o[r] ? -1 * e : 0); var P = (t = []) => { let e = [...t], r = [], n = e.length; for (; n > 0; ) { @@ -12578,11 +12565,10 @@ var require_bella = __commonJS({ } return r; }; - var at = (t = [], e = 1) => { + var ft = (t = [], e = 1) => { let r = P(t), n = Math.max(1, e), o = Math.min(n, r.length - 1); return r.splice(0, o); }; - module2.exports = H(gt); } }); @@ -12629,32 +12615,42 @@ var setRequestOptions = (opts) => { (0, import_bellajs.copies)(opts, requestOptions); }; +// src/utils/logger.js +var import_debug = __toESM(require_src(), 1); +var name = "oembed-parser"; +var info = (0, import_debug.default)(`${name}:info`); +var error = (0, import_debug.default)(`${name}:error`); +var warning = (0, import_debug.default)(`${name}:warning`); +var logger_default = { + info: (0, import_debug.default)(`${name}:info`), + error: (0, import_debug.default)(`${name}:error`), + warning: (0, import_debug.default)(`${name}:warning`) +}; + // src/utils/retrieve.js +var isValidContentType = (ctype) => { + return [ + "application/json", + "text/javascript" + ].some((item) => { + return ctype.includes(item); + }); +}; var retrieve_default = async (url) => { try { const res = await import_axios.default.get(url, getRequestOptions()); const contentType = res.headers["content-type"] || ""; - if (!contentType || !contentType.includes("application/json")) { + if (!isValidContentType(contentType)) { + error(`Invalid content type! (${contentType})`); return null; } return res.data; } catch (err) { + error(err.message); return null; } }; -// src/utils/logger.js -var import_debug = __toESM(require_src(), 1); -var name = "oembed-parser"; -var info = (0, import_debug.default)(`${name}:info`); -var error = (0, import_debug.default)(`${name}:error`); -var warning = (0, import_debug.default)(`${name}:warning`); -var logger_default = { - info: (0, import_debug.default)(`${name}:info`), - error: (0, import_debug.default)(`${name}:error`), - warning: (0, import_debug.default)(`${name}:warning`) -}; - // src/utils/fetchEmbed.js var isFacebookGraphDependent = (url) => { return url.includes("facebook.com") || url.includes("instagram.com"); @@ -12672,10 +12668,11 @@ var getRegularUrl = (query, basseUrl) => { return basseUrl.replace(/\{format\}/g, "json") + "?" + query; }; var fetchEmbed_default = async (url, provider, params = {}) => { - const query = __spreadValues({ + const query = { url, - format: "json" - }, params); + format: "json", + ...params + }; if (query.maxwidth <= 0) { delete query.maxwidth; } @@ -12687,6 +12684,7 @@ var fetchEmbed_default = async (url, provider, params = {}) => { } const queryParams = new URLSearchParams(query).toString(); const link = getRegularUrl(queryParams, provider.fetchEndpoint); + console.log(link); const body = retrieve_default(link); return body; }; diff --git a/dist/cjs/package.json b/dist/cjs/package.json index 5ac84b8..0b2fd3f 100644 --- a/dist/cjs/package.json +++ b/dist/cjs/package.json @@ -1,5 +1,5 @@ { "name": "oembed-parser-cjs", - "version": "3.0.4", + "version": "3.0.5", "main": "./oembed-parser.js" } \ No newline at end of file diff --git a/package.json b/package.json index 254ee13..9e5be02 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { - "version": "3.0.4", + "version": "3.0.5", "name": "oembed-parser", "description": "Get oEmbed data from given URL.", "homepage": "https://www.npmjs.com/package/oembed-parser", @@ -29,7 +29,7 @@ }, "dependencies": { "axios": "^0.27.2", - "bellajs": "^11.0.2", + "bellajs": "^11.0.3", "debug": "^4.3.4" }, "standard": { @@ -38,9 +38,9 @@ ] }, "devDependencies": { - "esbuild": "^0.14.39", - "jest": "^28.1.0", - "nock": "^13.2.4" + "esbuild": "^0.14.47", + "jest": "^28.1.1", + "nock": "^13.2.7" }, "keywords": [ "oembed", diff --git a/src/utils/fetchEmbed.js b/src/utils/fetchEmbed.js index ea1e747..b168211 100644 --- a/src/utils/fetchEmbed.js +++ b/src/utils/fetchEmbed.js @@ -42,6 +42,7 @@ export default async (url, provider, params = {}) => { const queryParams = new URLSearchParams(query).toString() const link = getRegularUrl(queryParams, provider.fetchEndpoint) + console.log(link) const body = retrieve(link) return body } diff --git a/src/utils/retrieve.js b/src/utils/retrieve.js index ae06440..2e78041 100644 --- a/src/utils/retrieve.js +++ b/src/utils/retrieve.js @@ -3,18 +3,28 @@ import axios from 'axios' import { getRequestOptions } from '../config.js' +import { error } from './logger.js' + +const isValidContentType = (ctype) => { + return [ + 'application/json', + 'text/javascript' + ].some((item) => { + return ctype.includes(item) + }) +} export default async (url) => { try { const res = await axios.get(url, getRequestOptions()) - const contentType = res.headers['content-type'] || '' - if (!contentType || !contentType.includes('application/json')) { + if (!isValidContentType(contentType)) { + error(`Invalid content type! (${contentType})`) return null } - return res.data } catch (err) { + error(err.message) return null } }