UNPKG

@tsed/formio

Version:
109 lines (108 loc) 4.1 kB
import { __decorate, __metadata } from "tslib"; import { AnyToPromise, AnyToPromiseStatus } from "@tsed/core"; import { Inject, InjectorService } from "@tsed/di"; import { setResponseHeaders } from "@tsed/platform-http"; import { PlatformParams } from "@tsed/platform-params"; import { PlatformResponseFilter } from "@tsed/platform-response-filter"; import { EndpointMetadata } from "@tsed/schema"; import { Alter } from "../decorators/alter.js"; import { FormioService } from "../services/FormioService.js"; let AlterActions = class AlterActions { transform(actions) { const { Action } = this.formio; return this.getActions().reduce((actions, provider) => { const instance = this.injector.invoke(provider.token); const options = provider.store.get("formio:action"); const resolveHandler = this.createHandler(provider, "resolve"); return { ...actions, [options.name]: class extends Action { static { this.access = options.access; } static async info(req, res, next) { let opts = { ...options }; if (instance.info) { opts = await instance.info(opts, req, res); } next(null, opts); } static async settingsForm(req, res, next) { next(null, await instance.settingsForm(req, res)); } resolve(handler, method, req, res, next, setActionItemMessage) { resolveHandler(this, handler, method, req, res, next, setActionItemMessage); } } }; }, actions); } getActions() { return this.injector.getProviders("formio:action"); } createHandler(provider, propertyKey) { const compiledHandler = this.params.compileHandler({ token: provider.token, propertyKey }); return async (action, handler, method, req, res, next, setActionItemMessage) => { const { $ctx } = req; if ($ctx) { $ctx.set("ACTION_CTX", { handler, method, setActionItemMessage, action }); $ctx.endpoint = EndpointMetadata.get(provider.useClass, "resolve"); try { if (await this.onRequest(compiledHandler, $ctx)) { next(); } } catch (er) { next(er); } } }; } async onRequest(handler, $ctx) { const resolver = new AnyToPromise(); const { state, data, status, headers } = await resolver.call(() => handler({ $ctx })); if (state === AnyToPromiseStatus.RESOLVED) { if (status) { $ctx.response.status(status); } if (headers) { $ctx.response.setHeaders(headers); } if (data !== undefined) { $ctx.data = data; return this.flush($ctx.data, $ctx); } return true; } } async flush(data, $ctx) { const { response } = $ctx; if (!$ctx.isDone()) { setResponseHeaders($ctx); data = await this.responseFilter.transform(data, $ctx); response.body(data); } return response; } }; __decorate([ Inject(), __metadata("design:type", InjectorService) ], AlterActions.prototype, "injector", void 0); __decorate([ Inject(), __metadata("design:type", FormioService) ], AlterActions.prototype, "formio", void 0); __decorate([ Inject(), __metadata("design:type", PlatformParams) ], AlterActions.prototype, "params", void 0); __decorate([ Inject(), __metadata("design:type", PlatformResponseFilter) ], AlterActions.prototype, "responseFilter", void 0); AlterActions = __decorate([ Alter("actions") ], AlterActions); export { AlterActions };