@foxpage/foxpage-node-sdk
Version:
foxpage node sdk
191 lines (190 loc) • 7.25 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.renderToHtmlByFileIdAndLocale = exports.renderToHtmlByDSL = exports.renderToHtmlByPage = exports.renderByPageId = exports.renderToHtmlByPageId = void 0;
const foxpage_manager_1 = require("@foxpage/foxpage-manager");
const foxpage_shared_1 = require("@foxpage/foxpage-shared");
const common_1 = require("../common");
const context_1 = require("../context");
const errors_1 = require("../errors");
const task_1 = require("../task");
/**
* render to html by pageId
* @param pageId page id
* @param appId application id
* @returns html string
*/
const renderToHtmlByPageId = async (pageId, appId, opt) => {
const result = await (0, exports.renderByPageId)(pageId, appId, opt);
return result.html;
};
exports.renderToHtmlByPageId = renderToHtmlByPageId;
/**
* render by pageId
* @param pageId page id
* @param appId application id
* @returns html string
*/
const renderByPageId = async (pageId, 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, { contentId: pageId });
if (!verified) {
throw new errors_1.AccessDeniedError((_a = opt.request.URL) === null || _a === void 0 ? void 0 : _a.pathname);
}
}
// 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, ctx: context } = await (0, task_1.parseTask)(page, ctx);
if (!content.schemas) {
throw new errors_1.ParseDSLError(new Error('parsed.schemas is empty'), ctx.origin);
}
// render task
const html = (await (0, task_1.renderTask)(content, context)) || null;
return { html, dsl: context.origin.page, vars: context.variables, contextValue: context };
};
exports.renderByPageId = renderByPageId;
/**
* render to html by page info
* @param page page info
* @param app app
* @returns html
*/
async function renderToHtmlByPage(page, app, opt) {
var _a;
let appInstance;
if (typeof app === 'string') {
appInstance = (0, foxpage_manager_1.getApplication)(app);
if (!appInstance) {
throw new errors_1.NotFoundAppError(app);
}
}
else {
appInstance = app;
}
// init renderContext task
const ctx = opt.ctx ? opt.ctx : await (0, task_1.contextTask)(appInstance, opt);
// is not prod access
// access control verified
if (opt.accessControl && !(0, common_1.isProd)(ctx)) {
const verified = await (0, task_1.accessControlTask)(appInstance, opt.request, { contentId: page.id });
if (!verified) {
throw new errors_1.AccessDeniedError((_a = opt.request.URL) === null || _a === void 0 ? void 0 : _a.pathname);
}
}
const pageInstance = new foxpage_manager_1.PageInstance(page);
await (0, context_1.updateContext)(ctx, { app: appInstance, content: pageInstance });
// parse page
const { content, ctx: context } = await (0, task_1.parseTask)(pageInstance, ctx);
if (!content.schemas) {
throw new errors_1.ParseDSLError(new Error('parsed.schemas is empty'), ctx.origin);
}
// render task
const html = (await (0, task_1.renderTask)(content, context)) || null;
return { html, dsl: context.origin.page, vars: context.variables, contextValue: context };
}
exports.renderToHtmlByPage = renderToHtmlByPage;
/**
* render to html by dsl
* @param dsl
* @param app
* @param opt
* @returns
*/
async function renderToHtmlByDSL(dsl, app, opt) {
var _a;
let appInstance;
if (typeof app === 'string') {
appInstance = (0, foxpage_manager_1.getApplication)(app);
if (!appInstance) {
throw new errors_1.NotFoundAppError(app);
}
}
else {
appInstance = app;
}
// init renderContext task
const ctx = opt.ctx ? opt.ctx : await (0, task_1.contextTask)(appInstance, opt);
if (!ctx.renderConfig) {
ctx.renderConfig = {};
}
ctx.renderConfig.errorThrow = opt.errorThrow;
// is not prod access
// access control verified
if (opt.accessControl && !(0, common_1.isProd)(ctx)) {
const verified = await (0, task_1.accessControlTask)(appInstance, opt.request, { contentId: dsl.id });
if (!verified) {
throw new errors_1.AccessDeniedError((_a = opt.request.URL) === null || _a === void 0 ? void 0 : _a.pathname);
}
}
// create instance by dsl content
// update context
const page = new foxpage_manager_1.PageInstance(dsl);
if (page) {
await (0, context_1.updateContext)(ctx, { app: appInstance, content: page });
ctx.updatePage(page);
}
// render task
const html = (await (0, task_1.renderTask)(dsl, ctx)) || null;
return { html };
}
exports.renderToHtmlByDSL = renderToHtmlByDSL;
/**
* render html by file id and locale
* @param fileId file id
* @param locale locale
* @param appId application id
* @param opt options
* @returns
*/
const renderToHtmlByFileIdAndLocale = 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');
}
// 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.renderToHtmlByFileIdAndLocale = renderToHtmlByFileIdAndLocale;