@tsed/formio
Version:
Formio package for Ts.ED framework
78 lines (77 loc) • 2.49 kB
JavaScript
import { __decorate, __metadata } from "tslib";
import { promisify } from "node:util";
import { Inject, Injectable, InjectorService } from "@tsed/di";
import { FormioService } from "./FormioService.js";
let FormioHooksService = class FormioHooksService {
get settings() {
return this.formio.hook.settings;
}
get invoke() {
return this.formio.hook.invoke;
}
get alter() {
return this.formio.hook.alter;
}
get alterAsync() {
return promisify(this.alter);
}
getHooks() {
return {
alter: this.getHooksProvider("alter"),
on: this.getHooksProvider("on")
};
}
getProviders(type) {
return this.injector.getProviders(`formio:${type}`);
}
getHooksProvider(type) {
return this.bindHooks(type, this.createHooks(type));
}
bindHooks(type, hooks) {
return Object.entries(hooks).reduce((newHooks, [key, pool]) => {
const wrap = (input, ...args) => {
let last;
for (const fn of pool) {
const result = fn(input, ...args);
if (type === "alter") {
input = result;
}
else {
last = result;
}
}
return type === "alter" ? input : last;
};
return {
...newHooks,
[key]: wrap
};
}, {});
}
createHooks(type) {
return this.getProviders(type).reduce((hooks, provider) => {
const instance = this.injector.invoke(provider.token);
const name = provider.store.get(`formio:${type}:name`);
const pool = hooks[name] || [];
const hook = (...args) => instance[type === "alter" ? "transform" : "on"](...args.map((input) => {
return input && input.$ctx ? input.$ctx : input;
}));
return {
...hooks,
[name]: [].concat(pool, hook)
};
}, {});
}
};
__decorate([
Inject(),
__metadata("design:type", InjectorService)
], FormioHooksService.prototype, "injector", void 0);
__decorate([
Inject(FormioService),
__metadata("design:type", FormioService)
], FormioHooksService.prototype, "formio", void 0);
FormioHooksService = __decorate([
Injectable()
], FormioHooksService);
export { FormioHooksService };