UNPKG

@crowdin/app-project-module

Version:

Module that generates for you all common endpoints for serving standalone Crowdin App

43 lines (42 loc) 2.42 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = handle; const util_1 = require("../util"); const static_files_1 = require("../util/static-files"); const views_1 = require("../views"); const jsx_renderer_1 = require("../util/jsx-renderer"); function handle(moduleConfig, config) { return (0, util_1.runAsyncWrapper)((req, res, next) => __awaiter(this, void 0, void 0, function* () { if (moduleConfig.formSchema) { // Skip non-HTML requests so custom form data endpoints (formGetDataUrl/formPostDataUrl) // are not intercepted when they share the same base path as the module UI route if (!/\.html?$/i.test(req.path)) { return next(); } const html = (0, jsx_renderer_1.renderJSXOnServer)(views_1.FormPage, { formGetDataUrl: moduleConfig.formGetDataUrl || `/api/${moduleConfig.key}/form-data`, formPostDataUrl: moduleConfig.formPostDataUrl || `/api/${moduleConfig.key}/form-data`, formSchema: JSON.stringify(moduleConfig.formSchema).replace(/</g, '\\u003c'), formUiSchema: moduleConfig.formUiSchema ? JSON.stringify(moduleConfig.formUiSchema).replace(/</g, '\\u003c') : '{}', formPatchDataUrl: moduleConfig.formPatchDataUrl, }); res.setHeader('Content-Type', 'text/html; charset=utf-8'); return res.send(html); } if (moduleConfig.uiPath) { return (0, static_files_1.serveStatic)(config, moduleConfig.uiPath)(req, res, next); } throw new Error('uiPath or formSchema should be provided for module'); })); }