UNPKG

@knapsack/app

Version:

Build Design Systems with Knapsack

117 lines 4.3 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.authCheck = exports.getContentStateFromId = exports.getDemoFromId = exports.localDevSaveDataCache = void 0; const core_1 = require("@knapsack/core"); const utils_1 = require("@knapsack/utils"); const lru_cache_1 = require("lru-cache"); const url_join_1 = __importDefault(require("url-join")); const log_1 = require("../cli/log"); const exec_curl_1 = require("./util/exec-curl"); const ks_urls_1 = require("./ks-urls"); const utils_2 = require("./utils"); let fetchMethod = 'fetch'; exports.localDevSaveDataCache = new lru_cache_1.LRUCache({ max: 1000, }); // this needs to be called after the cli command is set to serve only // or is not required anymore. async function canUseFetch() { await (0, utils_1.fetcher)({ url: ks_urls_1.ksUrls.apiRootEndpoint, }) .then((response) => { if (typeof response.message !== 'string') { log_1.log.warn(`Could not use fetch to get data from ${ks_urls_1.ksUrls.apiRootEndpoint} - falling back to curl`); fetchMethod = 'curl'; } log_1.log.verbose(`Using fetch to get data from ${ks_urls_1.ksUrls.apiRootEndpoint}`); }) .catch((e) => { log_1.log.warn(`Could not use fetch to get data from ${ks_urls_1.ksUrls.apiRootEndpoint} - falling back to curl`); fetchMethod = 'curl'; (0, exec_curl_1.execCurl)({ url: ks_urls_1.ksUrls.apiRootEndpoint, }) .then(() => { // we're good, curl works log_1.log.verbose(`Using curl to get data from ${ks_urls_1.ksUrls.apiRootEndpoint}`); }) .catch((err) => { log_1.log.error(`Could not use curl or fetch to get data from ${ks_urls_1.ksUrls.apiRootEndpoint} - this is critical for Knapsack to work and must be resolved. \nFetch error: ${e.message}\nCurl error: ${err.message}`); process.exit(1); }); }); } /** * Only `GET` */ async function fetchWithCurlFallback({ url, headers = {}, }) { if (fetchMethod === 'fetch') { return (0, utils_1.fetcher)({ url, headers, }); } return (0, exec_curl_1.execCurl)({ url, headers }); } async function getRenderData({ dataId, siteId, type, appClientVersion, }) { if (!dataId) { throw new Error(`A "dataId" is required when getting data for "${type}"`); } const isLocalDevelopment = (0, utils_2.getCliCommand)() === 'start'; if (isLocalDevelopment) { const data = exports.localDevSaveDataCache.get(dataId); if (data) { return data; } // fallback to app api } const url = new URL((0, url_join_1.default)(ks_urls_1.ksUrls.apiRootEndpoint, '/render-data')); url.searchParams.set('dataId', dataId); url.searchParams.set('type', type); return fetchWithCurlFallback({ url: url.toString(), headers: { [core_1.ksHttpHeaders.siteId]: siteId, [core_1.ksHttpHeaders.appClientVersion]: appClientVersion, }, }); } exports.getDemoFromId = (0, utils_1.memoize)(({ dataId, siteId, appClientVersion, }) => getRenderData({ dataId, siteId, type: 'demo', appClientVersion, }), { promise: true, max: 50, normalizer: (args) => JSON.stringify(args[0]), }); exports.getContentStateFromId = (0, utils_1.memoize)(({ stateId, siteId, appClientVersion, }) => getRenderData({ dataId: stateId, siteId, type: 'content-state-for-rendering', appClientVersion, }), { promise: true, max: 1, // 6 hours maxAge: 6 * 60 * 60_000, normalizer: (args) => JSON.stringify(args[0]), }); exports.authCheck = (0, utils_1.memoize)(async ({ siteId, authHeader }) => { const headers = { [core_1.ksHttpHeaders.siteId]: siteId, }; if (authHeader) { headers.Authorization = authHeader; } return fetchWithCurlFallback({ url: new URL((0, url_join_1.default)(ks_urls_1.ksUrls.apiRootEndpoint, `/domains/users/auth-check`)).toString(), headers, }); }); //# sourceMappingURL=api-client.js.map