UNPKG

@knapsack/app

Version:

Build Design Systems with Knapsack

98 lines 3.83 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.setupRoutes = setupRoutes; /* eslint-disable @typescript-eslint/consistent-type-imports */ const file_utils_1 = require("@knapsack/file-utils"); const path_1 = require("path"); const handlebars_helpers_1 = require("../lib/handlebars-helpers"); const routes_1 = require("../lib/routes"); function setupRoutes({ patterns, allAssetSetIds, }) { const allPatterns = patterns.getPatterns(); function getDemoUrls() { return allPatterns?.map((pattern) => ({ id: pattern.id, title: pattern.title, url: (0, routes_1.createPatternUrl)({ patternId: pattern.id, }), templates: pattern.templates?.map((template) => ({ id: template.id, title: template.title ?? template.id, url: (0, routes_1.createPatternUrl)({ patternId: pattern.id, templateId: template.id, }), demoUrls: template.demoIds?.flatMap((demoId) => { if (allAssetSetIds.length === 0) { return [ (0, routes_1.createDemoUrl)({ patternId: pattern.id, templateId: template.id, demoId, assetSetId: '', }), ]; } return allAssetSetIds.map((assetSetId) => { return (0, routes_1.createDemoUrl)({ patternId: pattern.id, templateId: template.id, demoId, assetSetId, }); }); }), })), })); } const demoUrlsDataEndpoint = { method: 'GET', path: '/demo-urls-data', handle: async () => getDemoUrls(), }; /** This page is mainly so IE can get a list of links to view the individual templates outside of the system */ const demoUrlsEndpoint = { method: 'GET', path: '/demo-urls', handle: async () => { const demoUrlsHbsPath = (0, path_1.join)(__dirname, './templates/demo-urls.html.hbs'); if (!(0, file_utils_1.existsSync)(demoUrlsHbsPath)) { throw new Error(`Demo URLs handlebars template does not exist: ${demoUrlsHbsPath}`); } const patternDemos = getDemoUrls(); const demoUrlsHbs = await (0, file_utils_1.readFile)(demoUrlsHbsPath); return (0, handlebars_helpers_1.hbsRenderString)({ hbsString: demoUrlsHbs, data: { patternDemos, }, }); }, }; const { KS_APP_UI_URL = 'https://app.knapsack.cloud' } = process.env; const redirectEndpoint = { method: 'GET', path: '/', handle: async () => { const redirectUrl = new URL('/site/custom/', KS_APP_UI_URL).toString(); const page = /* html */ ` <!DOCTYPE html> <html lang="en-US"> <head> <meta charset="UTF-8" /> <script type="text/javascript"> var urlEnd = encodeURIComponent(window.location.origin); var redirectUrl = '${redirectUrl}' + urlEnd; window.location.href = redirectUrl; </script> <title>Knapsack Redirection</title> </head> <body></body> </html> `; return page; }, }; return [demoUrlsDataEndpoint, demoUrlsEndpoint, redirectEndpoint]; } //# sourceMappingURL=routes.js.map