@agnostack/next-zcli
Version:
Please contact agnoStack via info@agnostack.com for any questions
184 lines • 16.8 kB
JavaScript
"use strict";
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getZendeskData = exports.getAppData = exports.generateReplacements = exports.arrowsReplace = exports.ASSET_URL_PREFIX = void 0;
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const mustache_1 = __importDefault(require("mustache"));
const browser_monads_ts_1 = require("browser-monads-ts");
const deepmerge_1 = __importDefault(require("deepmerge"));
const display_1 = require("./display");
const locations_1 = require("./locations");
exports.ASSET_URL_PREFIX = 'assets/';
const getProductIcon = ({ product, location, isMultiProduct }) => {
var _a;
let productIcon;
// NOTE: if running in browser/old node version/old webpack version may not have method
if (fs_1.default === null || fs_1.default === void 0 ? void 0 : fs_1.default.existsSync) {
if (!((_a = locations_1.ICON_LOCATION_ALLOWLIST[product]) === null || _a === void 0 ? void 0 : _a.includes(location))) {
return productIcon;
}
const svgPath = isMultiProduct ? `${product}/icon_${location}.svg` : `icon_${location}.svg`;
const baseIconPath = path_1.default.resolve(__dirname);
const imagePaths = [svgPath, path_1.default.join('assets', svgPath), path_1.default.join('images', svgPath)];
// NOTE: intentionally using for loops instead of map so we can break to exit quickly soon as matched
// eslint-disable-next-line no-restricted-syntax
for (const imagePath of imagePaths) {
if (fs_1.default.existsSync(path_1.default.join(baseIconPath, 'public', imagePath))) {
productIcon = {
svg: imagePath,
// HMMM: active/inactive/hover??
};
break;
}
}
}
return productIcon;
};
const getIconsByProduct = ({ product, productLocations, isMultiProduct }) => (Object.entries(productLocations).reduce((productLocationIcons, [location]) => {
const productIcon = getProductIcon({ product, location, isMultiProduct });
return Object.assign(Object.assign({}, productLocationIcons), (productIcon && {
[location]: productIcon,
}));
}, {}));
// HMMMMM: optimize this method!
const normalizeLocations = (_locations) => {
var _a, _b;
const locations = (0, display_1.ensureObject)(_locations);
const isMultiProduct = Object.keys(locations) > 1;
const productIcons = Object.entries(locations).reduce((_productIcons, [product, productLocations]) => (Object.assign(Object.assign({}, _productIcons), { [product]: getIconsByProduct({ product, productLocations, isMultiProduct }) })), {});
for (const product in productIcons) {
for (const locationName in productIcons[product]) {
if (typeof locations[product][locationName] === 'string') {
locations[product][locationName] = {
url: (0, display_1.cleanURL)(locations[product][locationName]),
};
}
if (typeof ((_a = locations[product][locationName]) === null || _a === void 0 ? void 0 : _a.url) === 'string') {
locations[product][locationName] = Object.assign(Object.assign({}, locations[product][locationName]), { url: (0, display_1.cleanURL)(locations[product][locationName].url) });
}
if (productIcons[product][locationName].svg) {
locations[product][locationName].svg = productIcons[product][locationName].svg;
}
if (productIcons[product][locationName].active) {
locations[product][locationName].active = productIcons[product][locationName].active;
}
if (productIcons[product][locationName].inactive) {
locations[product][locationName].inactive = productIcons[product][locationName].inactive;
}
if (productIcons[product][locationName].hover) {
locations[product][locationName].hover = productIcons[product][locationName].hover;
}
}
}
// Handle the case where an app installation location is not an object but is a string,
for (const product in locations) {
for (const locationName in locations[product]) {
if (typeof locations[product][locationName] === 'string') {
locations[product][locationName] = {
url: (0, display_1.cleanURL)(locations[product][locationName]),
};
}
else if (typeof ((_b = locations[product][locationName]) === null || _b === void 0 ? void 0 : _b.url) === 'string') {
locations[product][locationName] = Object.assign(Object.assign({}, locations[product][locationName]), { url: (0, display_1.cleanURL)(locations[product][locationName].url) });
}
}
}
return locations;
};
const templateReplace = (template, replacements, partials, tokens) => {
// NOTE: fixes issues where by default standard characters are HTML encoded (https://stackoverflow.com/questions/22910428/mustache-globally-disable-html-escaping)
mustache_1.default.escape = (value) => (value);
return mustache_1.default.render((0, display_1.ensureString)(template), (0, display_1.ensureObject)(replacements), partials, tokens);
};
const arrowsReplace = (template, replacements) => (templateReplace(template, (0, display_1.cleanObject)(replacements, false, display_1.stringEmptyOnly), {}, ['<<', '>>']));
exports.arrowsReplace = arrowsReplace;
const arrowsItemReplace = (value, replacements) => ((0, display_1.isType)(value, 'object')
? Object.entries(value).reduce((_data, [_key, _value]) => (Object.assign(Object.assign({}, _data), { [_key]: (0, exports.arrowsReplace)(_value, replacements) })), {})
: (0, exports.arrowsReplace)(value, replacements));
const generateReplacements = (_replacements) => {
const replacements = (0, display_1.cleanObject)(_replacements, false, display_1.stringEmptyOnly);
return (0, display_1.cleanObject)(Object.entries(replacements).reduce((data, [key, _value]) => {
// eslint-disable-next-line no-nested-ternary
const value = Array.isArray(_value)
? _value.reduce((_data, __value) => ([
..._data,
arrowsItemReplace(__value, replacements)
]), [])
: arrowsItemReplace(_value, replacements);
return Object.assign(Object.assign({}, data), { [key]: value });
}, {}), false, display_1.stringEmptyOnly);
};
exports.generateReplacements = generateReplacements;
const getRouteReplacements = (_locations, defaultRoute = '/index.html') => {
const locations = (0, display_1.ensureObject)(_locations);
return {
location: normalizeLocations(locations),
// NOTE: this generates a default set of routes that can be override by any passed in..BUT not sure how location.product factors in here
routes: Object.values(locations).reduce((_routes, productLocations) => (Object.assign(Object.assign({}, _routes), Object.keys((0, display_1.ensureObject)(productLocations)).reduce((_locationRoutes, productLocation) => (Object.assign(Object.assign({}, _locationRoutes), { [productLocation]: defaultRoute })), {}))), {}),
};
};
const getManifestData = (manifestTemplate, _replacements, mergeData) => {
var _a;
const data = (0, display_1.cleanObject)((0, deepmerge_1.default)((0, display_1.ensureObject)(manifestTemplate), (0, exports.generateReplacements)(mergeData)), false, display_1.stringEmptyOnly);
const replacements = (0, exports.generateReplacements)(_replacements);
return JSON.parse((0, exports.arrowsReplace)(JSON.stringify(data, null, 2), (0, deepmerge_1.default)(getRouteReplacements(data === null || data === void 0 ? void 0 : data.location, (_a = replacements === null || replacements === void 0 ? void 0 : replacements.routes) === null || _a === void 0 ? void 0 : _a.default), replacements)));
};
const getAppData = (manifestTemplate, replacements, mergeData) => {
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _0, _1, _2, _3, _4, _5, _6, _7, _8, _9;
const manifestData = getManifestData(manifestTemplate, replacements, mergeData);
const _10 = (0, exports.generateReplacements)(mergeData), { settings } = _10, _installationData = __rest(_10, ["settings"]);
const defaultSettings = (0, display_1.ensureArray)(manifestData === null || manifestData === void 0 ? void 0 : manifestData.parameters).reduce((_parameters, { name: _name, default: defaultValue }) => {
var _a;
return (Object.assign(Object.assign({}, _parameters), { [_name]: (_a = _parameters[_name]) !== null && _a !== void 0 ? _a : defaultValue }));
}, (0, display_1.ensureObject)(replacements === null || replacements === void 0 ? void 0 : replacements.parameters));
const installationData = Object.assign(Object.assign({ id: (_c = (_b = (_a = replacements === null || replacements === void 0 ? void 0 : replacements.installation_id) !== null && _a !== void 0 ? _a : replacements === null || replacements === void 0 ? void 0 : replacements.installationId) !== null && _b !== void 0 ? _b : replacements === null || replacements === void 0 ? void 0 : replacements.id) !== null && _c !== void 0 ? _c : (0, display_1.getInteger)(), app_id: (_e = (_d = replacements === null || replacements === void 0 ? void 0 : replacements.app_id) !== null && _d !== void 0 ? _d : replacements === null || replacements === void 0 ? void 0 : replacements.appId) !== null && _e !== void 0 ? _e : (0, display_1.getInteger)(), instance_guid: (_h = (_g = (_f = replacements === null || replacements === void 0 ? void 0 : replacements.instance_guid) !== null && _f !== void 0 ? _f : replacements === null || replacements === void 0 ? void 0 : replacements.instanceGUID) !== null && _g !== void 0 ? _g : replacements === null || replacements === void 0 ? void 0 : replacements.instanceGuid) !== null && _h !== void 0 ? _h : (0, display_1.getGUID)(), host_name: (_l = (_k = (_j = replacements === null || replacements === void 0 ? void 0 : replacements.host_name) !== null && _j !== void 0 ? _j : replacements === null || replacements === void 0 ? void 0 : replacements.host) !== null && _k !== void 0 ? _k : replacements === null || replacements === void 0 ? void 0 : replacements.hostName) !== null && _l !== void 0 ? _l : browser_monads_ts_1.window.location.host, product: (_m = replacements === null || replacements === void 0 ? void 0 : replacements.product) !== null && _m !== void 0 ? _m : locations_1.PRODUCTS.SUPPORT, location: (_o = replacements === null || replacements === void 0 ? void 0 : replacements.location) !== null && _o !== void 0 ? _o : locations_1.LOCATIONS.SIDEBAR_TICKET, app_owner: (_p = replacements === null || replacements === void 0 ? void 0 : replacements.app_owner) !== null && _p !== void 0 ? _p : replacements === null || replacements === void 0 ? void 0 : replacements.owner, app_name: (_t = (_s = (_r = (_q = replacements === null || replacements === void 0 ? void 0 : replacements.app_name) !== null && _q !== void 0 ? _q : replacements === null || replacements === void 0 ? void 0 : replacements.appName) !== null && _r !== void 0 ? _r : manifestData === null || manifestData === void 0 ? void 0 : manifestData.appName) !== null && _s !== void 0 ? _s : replacements === null || replacements === void 0 ? void 0 : replacements.name) !== null && _t !== void 0 ? _t : manifestData === null || manifestData === void 0 ? void 0 : manifestData.name, app_name_short: (_x = (_w = (_v = (_u = replacements === null || replacements === void 0 ? void 0 : replacements.app_name_short) !== null && _u !== void 0 ? _u : replacements === null || replacements === void 0 ? void 0 : replacements.shortName) !== null && _v !== void 0 ? _v : manifestData === null || manifestData === void 0 ? void 0 : manifestData.shortName) !== null && _w !== void 0 ? _w : replacements === null || replacements === void 0 ? void 0 : replacements.name) !== null && _x !== void 0 ? _x : manifestData === null || manifestData === void 0 ? void 0 : manifestData.name, app_version: (_z = (_y = replacements === null || replacements === void 0 ? void 0 : replacements.app_version) !== null && _y !== void 0 ? _y : replacements === null || replacements === void 0 ? void 0 : replacements.version) !== null && _z !== void 0 ? _z : manifestData === null || manifestData === void 0 ? void 0 : manifestData.version, terms_conditions_url: (_1 = (_0 = replacements === null || replacements === void 0 ? void 0 : replacements.terms_conditions_url) !== null && _0 !== void 0 ? _0 : replacements === null || replacements === void 0 ? void 0 : replacements.termsConditionsURL) !== null && _1 !== void 0 ? _1 : manifestData === null || manifestData === void 0 ? void 0 : manifestData.termsConditionsURL, app_locations: normalizeLocations((_3 = (_2 = replacements === null || replacements === void 0 ? void 0 : replacements.app_locations) !== null && _2 !== void 0 ? _2 : replacements === null || replacements === void 0 ? void 0 : replacements.locations) !== null && _3 !== void 0 ? _3 : manifestData === null || manifestData === void 0 ? void 0 : manifestData.location) }, _installationData), { settings: (0, deepmerge_1.default)((0, exports.generateReplacements)(defaultSettings), (0, display_1.ensureObject)(settings)) });
const subscriptionId = (_5 = (_4 = replacements === null || replacements === void 0 ? void 0 : replacements.stripe_subscription_id) !== null && _4 !== void 0 ? _4 : replacements === null || replacements === void 0 ? void 0 : replacements.subscription_id) !== null && _5 !== void 0 ? _5 : replacements === null || replacements === void 0 ? void 0 : replacements.subscriptionId;
const subscriptionData = Object.assign(Object.assign({}, subscriptionId && {
stripe_subscription_id: subscriptionId,
}), { plan: {
name: (_9 = (_8 = (_7 = (_6 = replacements === null || replacements === void 0 ? void 0 : replacements.stripe_subscription_plan) !== null && _6 !== void 0 ? _6 : replacements === null || replacements === void 0 ? void 0 : replacements.subscription_plan) !== null && _7 !== void 0 ? _7 : replacements === null || replacements === void 0 ? void 0 : replacements.subscriptionPlan) !== null && _8 !== void 0 ? _8 : replacements === null || replacements === void 0 ? void 0 : replacements.plan) !== null && _9 !== void 0 ? _9 : null,
} });
const safData = {
context: {
host: 'zendesk', // HMMM?
product: installationData.product,
location: installationData.location,
instanceGuid: installationData.instance_guid,
account: {
subdomain: installationData.host_name,
},
},
metadata: Object.assign({ appId: installationData.app_id, name: installationData.app_name, installationId: installationData.id, version: installationData.app_version, settings: installationData.settings }, subscriptionData),
};
return {
safData,
manifestData,
installationData,
};
};
exports.getAppData = getAppData;
const getZendeskData = (_a) => {
var { pluginConfig } = _a, replacements = __rest(_a, ["pluginConfig"]);
const _b = pluginConfig !== null && pluginConfig !== void 0 ? pluginConfig : {}, { routes = {}, data = {}, manifestTemplate } = _b, zendeskData = __rest(_b, ["routes", "data", "manifestTemplate"]);
return Object.assign(Object.assign({}, (0, exports.getAppData)(manifestTemplate, Object.assign(Object.assign(Object.assign({}, (0, display_1.cleanObject)(replacements, false, display_1.stringEmptyOnly)), (0, display_1.cleanObject)(data, false, display_1.stringEmptyOnly)), (0, display_1.objectNotEmpty)((0, display_1.cleanObject)(routes, false, display_1.stringEmptyOnly)) && {
routes,
}))), (0, display_1.objectNotEmpty)((0, display_1.cleanObject)(zendeskData, false, display_1.stringEmptyOnly)) && {
zendeskData,
});
};
exports.getZendeskData = getZendeskData;
//# sourceMappingURL=data.js.map