@foxpage/foxpage-node-sdk
Version:
foxpage node sdk
78 lines (77 loc) • 2.95 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.renderBlockHtml = exports.getBlockDSL = void 0;
const foxpage_manager_1 = require("@foxpage/foxpage-manager");
const common_1 = require("../common");
const errors_1 = require("../errors");
const task_1 = require("../task");
/**
* get block dsl
* @param contentId
* @param appId
* @param opt
* @returns
*/
const getBlockDSL = async (contentId, 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 });
if (!verified) {
throw new errors_1.AccessDeniedError((_a = opt.request.URL) === null || _a === void 0 ? void 0 : _a.pathname);
}
}
// get block
const block = await (0, task_1.blockTask)(contentId, app, ctx);
if (!block) {
throw new errors_1.NotFoundDSLError(contentId, 'the block content is not exist.');
}
// parse block
const { content } = await (0, task_1.parseTask)(block, ctx);
return content === null || content === void 0 ? void 0 : content.schemas;
};
exports.getBlockDSL = getBlockDSL;
/**
* render block html by id
* @param contentId block id
* @param appId application id
* @returns html string
*/
const renderBlockHtml = async (contentId, 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 });
if (!verified) {
throw new errors_1.AccessDeniedError((_a = opt.request.URL) === null || _a === void 0 ? void 0 : _a.pathname);
}
}
// get block
const block = await (0, task_1.blockTask)(contentId, app, ctx);
if (!block) {
throw new errors_1.NotFoundDSLError(contentId, 'the block content is not exist.');
}
// parse block
const { content, ctx: context } = await (0, task_1.parseTask)(block, 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.renderBlockHtml = renderBlockHtml;