UNPKG

@agnostack/next-zcli

Version:

Please contact agnoStack via info@agnostack.com for any questions

109 lines 6.46 kB
"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.zendeskCLIHandler = void 0; const nextjs_cors_1 = __importDefault(require("nextjs-cors")); const utils_1 = require("./utils"); const transformApp = ({ locale, asset_url_prefix, appId: _appId, appName: _appName, appLocale: _appLocale, appVersion: _appVersion, appSignedURLs: _appSignedURLs, appSingleInstall: _appSingleInstall, appFrameworkVersionL: _appFrameworkVersion, appLocations: _appLocations, installationData: { app_id, app_name, app_locations, } = {}, manifestData: { version, defaultLocale, signedUrls, singleInstall, frameworkVersion, } = {}, }) => { var _a, _b, _c, _d, _e; const appId = _appId !== null && _appId !== void 0 ? _appId : app_id; const appName = _appName !== null && _appName !== void 0 ? _appName : app_name; const appVersion = _appVersion !== null && _appVersion !== void 0 ? _appVersion : version; const appSignedURLs = (_a = _appSignedURLs !== null && _appSignedURLs !== void 0 ? _appSignedURLs : signedUrls) !== null && _a !== void 0 ? _a : false; const appSingleInstall = (_b = _appSingleInstall !== null && _appSingleInstall !== void 0 ? _appSingleInstall : singleInstall) !== null && _b !== void 0 ? _b : false; const appFrameworkVersion = (_c = _appFrameworkVersion !== null && _appFrameworkVersion !== void 0 ? _appFrameworkVersion : frameworkVersion) !== null && _c !== void 0 ? _c : '2.0'; const appLocale = (_e = (_d = _appLocale !== null && _appLocale !== void 0 ? _appLocale : locale) !== null && _d !== void 0 ? _d : defaultLocale) !== null && _e !== void 0 ? _e : 'en'; // HMMM: not sure about locale here as second vs third fallback const appLocations = (0, utils_1.ensureObject)(_appLocations !== null && _appLocations !== void 0 ? _appLocations : app_locations); return { name: appName, version: appVersion, signed_urls: appSignedURLs, single_install: appSingleInstall, framework_version: appFrameworkVersion, id: appId, locations: appLocations, default_locale: appLocale, asset_url_prefix, }; }; const transformInstallation = ({ installationId: _installationId, appId: _appId, appName: _appName, appSettings: _appSettings, appRequirements: _appRequirements, installationData: { id, app_id, app_name, settings, } = {}, safData: { metadata: { plan: { name: subscriptionPlan, } = {}, } = {}, } = {}, }) => { const installationId = _installationId !== null && _installationId !== void 0 ? _installationId : id; const appId = _appId !== null && _appId !== void 0 ? _appId : app_id; const appName = _appName !== null && _appName !== void 0 ? _appName : app_name; const appSettings = (0, utils_1.ensureObject)(_appSettings !== null && _appSettings !== void 0 ? _appSettings : settings); const appRequirements = (0, utils_1.ensureArray)(_appRequirements); const appSettingsArray = Object.entries(appSettings).map(([key, value]) => ({ [key]: value, })); return Object.assign(Object.assign({ id: installationId, app_id: appId, name: appName, requirements: appRequirements, settings: [{ title: appName }, ...appSettingsArray] }, subscriptionPlan && { plan: subscriptionPlan, }), { updated_at: new Date().toISOString(), collapsible: true, enabled: true }); }; const buildAppJSON = (data) => { const { protocol, host, asset_url_prefix, deployment_prefix = '', base_url = '' } = data; let assetURLPrefix = asset_url_prefix || `${(0, utils_1.removeTrailingSlash)(deployment_prefix)}/${(0, utils_1.removeLeadingSlash)(base_url)}`; if (asset_url_prefix === '/') { assetURLPrefix = `${protocol}://${host}/${(0, utils_1.removeLeadingSlash)(base_url)}`; } const app = transformApp(Object.assign({ asset_url_prefix: assetURLPrefix }, data)); const installation = transformInstallation(data); return { apps: [app], installations: [installation], }; }; const getRequestData = ({ protocol: reqProtocol, query, headers, secure, encrypted } = {}) => { const { host: _host, Host, origin: _origin, Origin, 'X-Forwarded-Proto': forwardedProtocol } = headers !== null && headers !== void 0 ? headers : {}; const { locale, subdomain } = query !== null && query !== void 0 ? query : {}; const host = _host || Host; const origin = _origin || Origin; let protocol = reqProtocol || forwardedProtocol; if (!protocol && (secure != undefined)) { if (secure) { protocol = 'https'; } } if (!protocol && encrypted) { protocol = 'https'; } if (!protocol) { protocol = 'http'; } const { hostname } = origin ? new URL(origin) : {}; return { host, protocol, locale, origin, subdomain, hostname }; }; // NOTE: api routes do NOT work in `next export` const zendeskCLIHandler = (props) => async (req, res) => { const { protocol, query, headers, secure, socket } = req; const _a = getRequestData({ protocol, query, headers, secure, encrypted: (socket.encrypted != undefined), }), { hostname, subdomain, origin } = _a, requestData = __rest(_a, ["hostname", "subdomain", "origin"]); if (!subdomain || (hostname !== `${subdomain}.zendesk.com`)) { return res.status(403).json({ message: 'Missing/invalid hostname or subdomain, try running inside of zendesk w/ ?zcli_apps=true' }); } await (0, nextjs_cors_1.default)(req, res, { methods: ['GET'], optionsSuccessStatus: 200, // some legacy browsers (IE11, various SmartTVs) choke on 204 origin, }); return res.json(buildAppJSON(Object.assign(Object.assign({}, requestData), props))); }; exports.zendeskCLIHandler = zendeskCLIHandler; //# sourceMappingURL=zendeskCLIHandler.js.map