UNPKG

@tsed/formio

Version:
129 lines (128 loc) 4.68 kB
import { __decorate, __metadata } from "tslib"; import { deepClone } from "@tsed/core"; import { Constant, Inject, InjectorService, Module } from "@tsed/di"; import { normalizePath } from "@tsed/normalize-path"; import { PlatformApplication } from "@tsed/platform-http"; import { AlterActions } from "./components/AlterActions.js"; import { AlterAudit } from "./components/AlterAudit.js"; import { AlterHost } from "./components/AlterHost.js"; import { AlterLog } from "./components/AlterLog.js"; import { AlterSkip } from "./components/AlterSkip.js"; import { AlterTemplateExportSteps } from "./components/AlterTemplateExportSteps.js"; import { AlterTemplateImportSteps } from "./components/AlterTemplateImportSteps.js"; import { FormioAuthService } from "./services/FormioAuthService.js"; import { FormioHooksService } from "./services/FormioHooksService.js"; import { FormioInstaller } from "./services/FormioInstaller.js"; import { FormioService } from "./services/FormioService.js"; let FormioModule = class FormioModule { $onInit() { return this.init(deepClone(this.settings)); } init(options) { return this.formio.init(options, this.hooks.getHooks()); } async $onRoutesInit() { if (this.formio.isInit()) { this.app.use(this.baseUrl, this.formio.middleware.restrictRequestTypes, this.formio.router); if (await this.shouldInstall()) { await this.installer.install(this.template, this.root); } } } async $logRoutes(routes) { if (this.formio.isInit()) { const spec = (await this.formio.swagger({ $ctx: { request: { protocol: "http", host: "localhost" } } }, this.formio.router)); const { baseUrl } = this; Object.entries(spec.paths).forEach(([path, methods]) => { Object.entries(methods).forEach(([method, operation]) => { routes.push({ method, name: operation.operationId, url: normalizePath(baseUrl, path.replace(/\/{(.*)}/gi, "/:$1")), className: "formio", methodClassName: operation.operationId }); }); }); } return routes; } // istanbul ignore next $onReady() { if (this.formio.isInit() && "getBestHost" in this.injector.settings) { const { injector } = this; // @ts-ignore const host = injector.settings.getBestHost(); const url = host.toString(); injector.logger.info(`Form.io API is available on ${url}${this.baseUrl || "/"}`); } } async shouldInstall() { const hasForms = await this.installer.hasForms(); return this.template && !(hasForms || this.skipInstall); } }; __decorate([ Inject(), __metadata("design:type", FormioService) ], FormioModule.prototype, "formio", void 0); __decorate([ Inject(), __metadata("design:type", FormioHooksService) ], FormioModule.prototype, "hooks", void 0); __decorate([ Inject(), __metadata("design:type", FormioInstaller) ], FormioModule.prototype, "installer", void 0); __decorate([ Inject(), __metadata("design:type", PlatformApplication) ], FormioModule.prototype, "app", void 0); __decorate([ Inject(), __metadata("design:type", InjectorService) ], FormioModule.prototype, "injector", void 0); __decorate([ Constant("formio", {}), __metadata("design:type", Object) ], FormioModule.prototype, "settings", void 0); __decorate([ Constant("formio.baseUrl", "/"), __metadata("design:type", String) ], FormioModule.prototype, "baseUrl", void 0); __decorate([ Constant("formio.skipInstall", false), __metadata("design:type", Boolean) ], FormioModule.prototype, "skipInstall", void 0); __decorate([ Constant("formio.template"), __metadata("design:type", Object) ], FormioModule.prototype, "template", void 0); __decorate([ Constant("formio.root"), __metadata("design:type", Object) ], FormioModule.prototype, "root", void 0); FormioModule = __decorate([ Module({ imports: [ FormioService, FormioHooksService, FormioAuthService, AlterActions, AlterHost, AlterAudit, AlterLog, AlterSkip, AlterTemplateImportSteps, AlterTemplateExportSteps ] }) ], FormioModule); export { FormioModule };