UNPKG

@foxpage/foxpage-node-sdk

Version:

foxpage node sdk

76 lines (75 loc) 2.76 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.routerHandler = void 0; const common_1 = require("../common"); const errors_1 = require("../errors"); const task_1 = require("../task"); /** * request handler * @returns html */ const routerHandler = () => async (opt) => { var _a, _b, _c; if (!((_a = opt.request) === null || _a === void 0 ? void 0 : _a.URL)) { return; } const { URL, query } = opt.request; const { pathname = '' } = URL; // ignore assets if (/\.(js|css|json|svg|png|webp|jpe?g|woff)$/.test(pathname)) { return null; } // get app const appInfo = (0, task_1.appTask)(pathname); if (!appInfo) { throw new errors_1.NotFoundAppByPathError(pathname); } const { app, matchedRoute } = appInfo; // init renderContext task const ctx = await (0, task_1.contextTask)(app, opt); ctx.matchedRoute = matchedRoute; // get content const content = await (0, task_1.routerTask)(app, ctx); if (!content) { throw new errors_1.NotMatchRouterError(pathname, URL.href); } const pageId = content.id; // check page route if (!pageId) { // is system routes const verified = await (0, task_1.accessControlTask)(app, ctx); if (!verified) { throw new errors_1.AccessDeniedError((_b = ctx.URL) === null || _b === void 0 ? void 0 : _b.pathname); } // dispatch const result = await content.action(ctx); return result; } // is not prod access // access control verified if (!(0, common_1.isProd)(ctx)) { const verified = await (0, task_1.accessControlTask)(app, ctx, { contentId: pageId }); if (!verified) { throw new errors_1.AccessDeniedError((_c = ctx.URL) === null || _c === void 0 ? void 0 : _c.pathname); } } if (ctx.isModuleViewMode) { const { moduleIds: idStr } = query || {}; const moduleIds = idStr ? String(idStr).split(',').filter(Boolean) : []; // init module ids ctx.moduleIds = moduleIds; } // get page const page = await (0, task_1.pageTask)(pageId, app, ctx); if (!page) { throw new errors_1.NotFoundDSLError(pageId, 'the page content is not exist.'); } // parse page const { content: parsedContent, ctx: context } = await (0, task_1.parseTask)(page, ctx); if (!parsedContent.schemas) { throw new errors_1.ParseDSLError(new Error('parsed.schemas is empty'), ctx.origin); } const html = await (0, task_1.renderTask)(parsedContent, context); return { html, dsl: context.origin.page, vars: context.variables, contextValue: context }; }; exports.routerHandler = routerHandler;