UNPKG

@flyyer/flyyer-lite

Version:

Flyyer.io helper classes and methods to generate smart URL to render images.

268 lines (232 loc) 7.7 kB
import stringify from 'qs/lib/stringify'; var CDN = "https://cdn.flyyer.io"; function isUndefined(value) { return typeof value === "undefined"; } /** * Set `__v` variable for cache invalidation */ function __V(v) { // return isUndefined(v) ? (new Date().getTime() / 1000).toFixed(0) : v; if (isUndefined(v)) { return (new Date().getTime() / 1000).toFixed(0); } if (v === null) { return undefined; // gets removed from querystring } return v; // keep wanted constant value } // @ts-expect-error Type /** * Internally used to convert an object to querystring. */ function toQuery(variables, options) { return stringify(variables, Object.assign({ addQueryPrefix: false, format: "RFC1738" }, options)); } var isProduction = process.env.NODE_ENV === "production"; var base = "Flyyer invariant failed"; function invariant(condition, message) { if (condition) return; if (isProduction) { throw new Error(base); } throw new Error(base + ": " + (message || "")); } /** * Convert path or array of path parts to a string. */ function normalizePath(path) { return [].concat(path) // force array .filter(function (part) { return part || part === 0; }) // filter falsy values .map(function (part) { return String(part).replace(/^\/+/, "").replace(/\/+$/, ""); }) // remove leading and trailing slashes .join("/"); // compose URL } var Flyyer = /*#__PURE__*/function () { function Flyyer(args) { !args ? process.env.NODE_ENV !== "production" ? invariant(false, "Flyyer constructor must not be empty. Expected object with 'project' property.") : invariant(false) : void 0; this.project = args.project; this.path = args.path; this["default"] = args["default"]; this.extension = args.extension; this.variables = args.variables || {}; this.meta = args.meta || {}; this.secret = args.secret; this.strategy = args.strategy; } /** * Override this method to implement signatures. Must be synchronous (no `Promise` allowed). */ var _proto = Flyyer.prototype; _proto.sign = function sign(project, path, // normalized params, strategy, secret) {}; _proto.params = function params(extra, options) { var meta = this.meta; var defaults = { __v: __V(meta.v), __id: meta.id, _w: meta.width, _h: meta.height, _res: meta.resolution, _ua: meta.agent, _def: this["default"], _ext: this.extension }; return toQuery(Object.assign(defaults, this.variables, extra), options); } /** * Generate final URL you can use in your og:images. * @example * <meta property="og:image" content={flyyer.href()} /> * @example * const flyyer = new Flyyer({ meta: { v: null } }); * <img src={flyyer.href()} /> */ ; _proto.href = function href() { var project = this.project; !!isUndefined(project) ? process.env.NODE_ENV !== "production" ? invariant(false, "Missing 'project' property") : invariant(false) : void 0; var path = normalizePath(this.path); var strategy = this.strategy; var secret = this.secret; var signature = this.sign(project, path, this.params({ __v: undefined }), strategy, secret); var params = this.params() || "_"; if (strategy && strategy.toUpperCase() === "JWT") { var __v = __V(this.meta.v); var query = toQuery({ __v: __v }, { addQueryPrefix: true }); return CDN + "/v2/" + project + "/jwt-" + signature + query; } else { return CDN + "/v2/" + project + "/" + (signature || "_") + "/" + params + "/" + path; } } /** * Alias of `.href()` */ ; _proto.toString = function toString() { return this.href(); }; return Flyyer; }(); /** * This class helps you creating URLs that will render Flyyer images. * * Required is: `tenant`, `deck`, `template`. * * Set and override the variables of the template by using the `variables` object. * * Example: https://cdn.flyyer.io/r/v2/flyyer/default/main.jpeg?title=Thanks+for+reading+this * @example * const flyyer = new FlyyerRender({ * tenant: "flyyer", * deck: "default", * template: "main", * variables: { title: "Thanks for reading this" }, * }); * console.log(flyyer.href()) * // https://cdn.flyyer.io/r/v2/flyyer/default/main.jpeg?title=Thanks+for+reading+this */ var FlyyerRender = /*#__PURE__*/function () { function FlyyerRender(args) { !args ? process.env.NODE_ENV !== "production" ? invariant(false, "FlyyerRender constructor must not be empty") : invariant(false) : void 0; this.tenant = args.tenant; this.deck = args.deck; this.template = args.template; this.version = args.version; this.extension = args.extension; this.variables = args.variables || {}; this.meta = args.meta || {}; this.secret = args.secret; this.strategy = args.strategy; } /** * Override this method to implement signatures. Must be synchronous (no `Promise` allowed). */ var _proto = FlyyerRender.prototype; _proto.sign = function sign(deck, template, version, extension, variables, meta, strategy, secret) {}; _proto.querystring = function querystring(extra, options) { var meta = this.meta; var defaults = { __v: __V(meta.v), __id: meta.id, _w: meta.width, _h: meta.height, _res: meta.resolution, _ua: meta.agent, _loc: meta.locale }; return toQuery(Object.assign(defaults, this.variables, extra), options); } /** * Generate final URL you can use in your og:images. * @example * <meta property="og:image" content={flyyer.href()} /> * @example * const flyyer = new FlyyerRender({ meta: { v: null } }); * <img src={flyyer.href()} /> */ ; _proto.href = function href() { var tenant = this.tenant, deck = this.deck, template = this.template, strategy = this.strategy, secret = this.secret, version = this.version, extension = this.extension, variables = this.variables, meta = this.meta; !!isUndefined(tenant) ? process.env.NODE_ENV !== "production" ? invariant(false, "Missing 'tenant' property") : invariant(false) : void 0; !!isUndefined(deck) ? process.env.NODE_ENV !== "production" ? invariant(false, "Missing 'deck' property") : invariant(false) : void 0; !!isUndefined(template) ? process.env.NODE_ENV !== "production" ? invariant(false, "Missing 'template' property") : invariant(false) : void 0; var signature = this.sign(deck, template, version, extension, variables, meta, strategy, secret); if (strategy && strategy.toUpperCase() === "JWT") { var __v = __V(meta.v); var query = toQuery({ __jwt: signature, __v: __v }, { addQueryPrefix: true }); return CDN + "/r/v2/" + tenant + query; } else { var _query = this.querystring({ __hmac: signature }, { addQueryPrefix: true }); var base = CDN + "/r/v2/" + tenant + "/" + deck + "/" + template; if (version && extension) { return base + "." + version + "." + extension + _query; } else if (version) { return base + "." + version + _query; } else if (extension) { return base + "." + extension + _query; } else { return "" + base + _query; } } } /** * Alias of `.href()` */ ; _proto.toString = function toString() { return this.href(); }; return FlyyerRender; }(); export { Flyyer, FlyyerRender, __V, invariant, normalizePath, toQuery }; //# sourceMappingURL=flyyer-lite.esm.js.map