@foxpage/foxpage-node-sdk
Version:
foxpage node sdk
61 lines (60 loc) • 2.32 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.renderModuleToHtml = void 0;
const foxpage_manager_1 = require("@foxpage/foxpage-manager");
const foxpage_shared_1 = require("@foxpage/foxpage-shared");
const common_1 = require("../common");
const errors_1 = require("../errors");
const task_1 = require("../task");
/**
* render module to html
* @param fileId
* @param locale
* @param appId
* @param opt
* @returns
*/
const renderModuleToHtml = async (fileId, locale = '', appId, opt) => {
var _a;
const app = (0, foxpage_manager_1.getApplication)(appId);
if (!app) {
throw new errors_1.NotFoundAppError(appId);
}
// init renderContext task
const ctx = opt.ctx ? opt.ctx : await (0, task_1.contextTask)(app, opt);
// is not prod access
// access control verified
if (opt.accessControl && !(0, common_1.isProd)(ctx)) {
const verified = await (0, task_1.accessControlTask)(app, opt.request, { fileId });
if (!verified) {
throw new errors_1.AccessDeniedError((_a = opt.request.URL) === null || _a === void 0 ? void 0 : _a.pathname);
}
}
// get file
const tags = locale ? foxpage_shared_1.tag.generateTagByQuerystring(`locale=${locale}`) : [];
const content = await app.tagManager.matchTag(tags, {
fileId,
withContentInfo: !ctx.isPreviewMode,
});
if (!content) {
throw new errors_1.NotFoundDSLError(fileId, 'the page content or locale is not exist');
}
// get page
const page = await (0, task_1.pageTask)(content.id, app, ctx);
if (!page) {
throw new errors_1.NotFoundDSLError(content.id, 'the page content or locale is not exist');
}
if (ctx.isModuleViewMode) {
// init module ids
ctx.moduleIds = opt.moduleIds;
}
// 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);
}
// render task
const html = (await (0, task_1.renderTask)(parsedContent, context)) || null;
return { html, dsl: context.origin.page, vars: context.variables, contextValue: context };
};
exports.renderModuleToHtml = renderModuleToHtml;