@tsed/formio 8.11.1 → 8.11.2-rc.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (154) hide show
  1. package/lib/esm/FormioModule.js +0 -1
  2. package/lib/esm/builder/FormioMapper.js +0 -1
  3. package/lib/esm/components/AlterActions.js +0 -1
  4. package/lib/esm/components/AlterAudit.js +0 -1
  5. package/lib/esm/components/AlterHost.js +0 -1
  6. package/lib/esm/components/AlterLog.js +0 -1
  7. package/lib/esm/components/AlterSkip.js +0 -1
  8. package/lib/esm/components/AlterTemplateExportSteps.js +0 -1
  9. package/lib/esm/components/AlterTemplateImportSteps.js +0 -1
  10. package/lib/esm/decorators/action.js +0 -1
  11. package/lib/esm/decorators/actionCtx.js +0 -1
  12. package/lib/esm/decorators/alter.js +0 -1
  13. package/lib/esm/decorators/on.js +0 -1
  14. package/lib/esm/decorators/useFormioAuth.js +0 -1
  15. package/lib/esm/domain/AlterHook.js +0 -1
  16. package/lib/esm/domain/Formio.js +0 -1
  17. package/lib/esm/domain/FormioAction.js +0 -1
  18. package/lib/esm/domain/FormioActionsIndex.js +0 -1
  19. package/lib/esm/domain/FormioAuth.js +0 -1
  20. package/lib/esm/domain/FormioBaseModel.js +0 -1
  21. package/lib/esm/domain/FormioConfig.js +0 -1
  22. package/lib/esm/domain/FormioCtxMapper.js +0 -1
  23. package/lib/esm/domain/FormioDecodedToken.js +0 -1
  24. package/lib/esm/domain/FormioErrors.js +0 -1
  25. package/lib/esm/domain/FormioHooks.js +0 -1
  26. package/lib/esm/domain/FormioJs.js +0 -1
  27. package/lib/esm/domain/FormioModels.js +0 -1
  28. package/lib/esm/domain/FormioRouter.js +0 -1
  29. package/lib/esm/domain/FormioSettings.js +0 -1
  30. package/lib/esm/domain/FormioTemplate.js +0 -1
  31. package/lib/esm/domain/FormioTemplateUtil.js +0 -1
  32. package/lib/esm/domain/FormioUpdate.js +0 -1
  33. package/lib/esm/domain/FormioUtils.js +0 -1
  34. package/lib/esm/domain/OnHook.js +0 -1
  35. package/lib/esm/domain/Resource.js +0 -1
  36. package/lib/esm/index.js +0 -1
  37. package/lib/esm/middlewares/FormioAuthMiddleware.js +0 -1
  38. package/lib/esm/services/FormioAuthService.js +0 -1
  39. package/lib/esm/services/FormioDatabase.js +0 -1
  40. package/lib/esm/services/FormioHooksService.js +0 -1
  41. package/lib/esm/services/FormioInstaller.js +0 -1
  42. package/lib/esm/services/FormioRepository.js +0 -1
  43. package/lib/esm/services/FormioService.js +0 -1
  44. package/lib/esm/utils/isMongoId.js +0 -1
  45. package/package.json +13 -10
  46. package/lib/esm/FormioModule.js.map +0 -1
  47. package/lib/esm/builder/FormioMapper.js.map +0 -1
  48. package/lib/esm/components/AlterActions.js.map +0 -1
  49. package/lib/esm/components/AlterAudit.js.map +0 -1
  50. package/lib/esm/components/AlterHost.js.map +0 -1
  51. package/lib/esm/components/AlterLog.js.map +0 -1
  52. package/lib/esm/components/AlterSkip.js.map +0 -1
  53. package/lib/esm/components/AlterTemplateExportSteps.js.map +0 -1
  54. package/lib/esm/components/AlterTemplateImportSteps.js.map +0 -1
  55. package/lib/esm/decorators/action.js.map +0 -1
  56. package/lib/esm/decorators/actionCtx.js.map +0 -1
  57. package/lib/esm/decorators/alter.js.map +0 -1
  58. package/lib/esm/decorators/on.js.map +0 -1
  59. package/lib/esm/decorators/useFormioAuth.js.map +0 -1
  60. package/lib/esm/domain/AlterHook.js.map +0 -1
  61. package/lib/esm/domain/Formio.js.map +0 -1
  62. package/lib/esm/domain/FormioAction.js.map +0 -1
  63. package/lib/esm/domain/FormioActionsIndex.js.map +0 -1
  64. package/lib/esm/domain/FormioAuth.js.map +0 -1
  65. package/lib/esm/domain/FormioBaseModel.js.map +0 -1
  66. package/lib/esm/domain/FormioConfig.js.map +0 -1
  67. package/lib/esm/domain/FormioCtxMapper.js.map +0 -1
  68. package/lib/esm/domain/FormioDecodedToken.js.map +0 -1
  69. package/lib/esm/domain/FormioErrors.js.map +0 -1
  70. package/lib/esm/domain/FormioHooks.js.map +0 -1
  71. package/lib/esm/domain/FormioJs.js.map +0 -1
  72. package/lib/esm/domain/FormioModels.js.map +0 -1
  73. package/lib/esm/domain/FormioRouter.js.map +0 -1
  74. package/lib/esm/domain/FormioSettings.js.map +0 -1
  75. package/lib/esm/domain/FormioTemplate.js.map +0 -1
  76. package/lib/esm/domain/FormioTemplateUtil.js.map +0 -1
  77. package/lib/esm/domain/FormioUpdate.js.map +0 -1
  78. package/lib/esm/domain/FormioUtils.js.map +0 -1
  79. package/lib/esm/domain/OnHook.js.map +0 -1
  80. package/lib/esm/domain/Resource.js.map +0 -1
  81. package/lib/esm/index.js.map +0 -1
  82. package/lib/esm/middlewares/FormioAuthMiddleware.js.map +0 -1
  83. package/lib/esm/services/FormioAuthService.js.map +0 -1
  84. package/lib/esm/services/FormioDatabase.js.map +0 -1
  85. package/lib/esm/services/FormioHooksService.js.map +0 -1
  86. package/lib/esm/services/FormioInstaller.js.map +0 -1
  87. package/lib/esm/services/FormioRepository.js.map +0 -1
  88. package/lib/esm/services/FormioService.js.map +0 -1
  89. package/lib/esm/utils/isMongoId.js.map +0 -1
  90. package/src/FormioModule.spec.ts +0 -113
  91. package/src/FormioModule.ts +0 -130
  92. package/src/builder/FormioMapper.spec.ts +0 -93
  93. package/src/builder/FormioMapper.ts +0 -71
  94. package/src/components/AlterActions.spec.ts +0 -376
  95. package/src/components/AlterActions.ts +0 -136
  96. package/src/components/AlterAudit.spec.ts +0 -19
  97. package/src/components/AlterAudit.ts +0 -12
  98. package/src/components/AlterHost.spec.ts +0 -20
  99. package/src/components/AlterHost.ts +0 -11
  100. package/src/components/AlterLog.spec.ts +0 -19
  101. package/src/components/AlterLog.ts +0 -12
  102. package/src/components/AlterSkip.spec.ts +0 -44
  103. package/src/components/AlterSkip.ts +0 -28
  104. package/src/components/AlterTemplateExportSteps.spec.ts +0 -99
  105. package/src/components/AlterTemplateExportSteps.ts +0 -58
  106. package/src/components/AlterTemplateImportSteps.spec.ts +0 -70
  107. package/src/components/AlterTemplateImportSteps.ts +0 -50
  108. package/src/decorators/action.ts +0 -20
  109. package/src/decorators/actionCtx.spec.ts +0 -25
  110. package/src/decorators/actionCtx.ts +0 -29
  111. package/src/decorators/alter.spec.ts +0 -16
  112. package/src/decorators/alter.ts +0 -19
  113. package/src/decorators/on.spec.ts +0 -16
  114. package/src/decorators/on.ts +0 -19
  115. package/src/decorators/useFormioAuth.spec.ts +0 -15
  116. package/src/decorators/useFormioAuth.ts +0 -12
  117. package/src/domain/AlterHook.ts +0 -3
  118. package/src/domain/Formio.ts +0 -122
  119. package/src/domain/FormioAction.ts +0 -30
  120. package/src/domain/FormioActionsIndex.ts +0 -19
  121. package/src/domain/FormioAuth.ts +0 -83
  122. package/src/domain/FormioBaseModel.ts +0 -14
  123. package/src/domain/FormioConfig.ts +0 -63
  124. package/src/domain/FormioCtxMapper.ts +0 -8
  125. package/src/domain/FormioDecodedToken.ts +0 -13
  126. package/src/domain/FormioErrors.ts +0 -53
  127. package/src/domain/FormioHooks.ts +0 -207
  128. package/src/domain/FormioJs.ts +0 -18
  129. package/src/domain/FormioModels.ts +0 -48
  130. package/src/domain/FormioRouter.ts +0 -10
  131. package/src/domain/FormioSettings.ts +0 -61
  132. package/src/domain/FormioTemplate.ts +0 -9
  133. package/src/domain/FormioTemplateUtil.ts +0 -15
  134. package/src/domain/FormioUpdate.ts +0 -23
  135. package/src/domain/FormioUtils.ts +0 -331
  136. package/src/domain/OnHook.ts +0 -3
  137. package/src/domain/Resource.ts +0 -21
  138. package/src/index.ts +0 -46
  139. package/src/middlewares/FormioAuthMiddleware.spec.ts +0 -61
  140. package/src/middlewares/FormioAuthMiddleware.ts +0 -34
  141. package/src/services/FormioAuthService.spec.ts +0 -396
  142. package/src/services/FormioAuthService.ts +0 -228
  143. package/src/services/FormioDatabase.spec.ts +0 -326
  144. package/src/services/FormioDatabase.ts +0 -165
  145. package/src/services/FormioHooksService.spec.ts +0 -156
  146. package/src/services/FormioHooksService.ts +0 -92
  147. package/src/services/FormioInstaller.spec.ts +0 -146
  148. package/src/services/FormioInstaller.ts +0 -46
  149. package/src/services/FormioRepository.spec.ts +0 -114
  150. package/src/services/FormioRepository.ts +0 -49
  151. package/src/services/FormioService.spec.ts +0 -368
  152. package/src/services/FormioService.ts +0 -134
  153. package/src/utils/isMongoId.ts +0 -3
  154. package/vitest.config.mts +0 -21
@@ -1,92 +0,0 @@
1
- import {promisify} from "node:util";
2
-
3
- import {Inject, Injectable, InjectorService} from "@tsed/di";
4
- import {Request} from "express";
5
-
6
- import {FormioHooks} from "../domain/FormioHooks.js";
7
- import {FormioService} from "./FormioService.js";
8
-
9
- @Injectable()
10
- export class FormioHooksService {
11
- @Inject()
12
- protected injector: InjectorService;
13
-
14
- @Inject(FormioService)
15
- protected formio: FormioService;
16
-
17
- get settings(): (req: Request, cb: Function) => void {
18
- return this.formio.hook.settings;
19
- }
20
-
21
- get invoke() {
22
- return this.formio.hook.invoke;
23
- }
24
-
25
- get alter(): (event: string, ...args: any[]) => any {
26
- return this.formio.hook.alter;
27
- }
28
-
29
- get alterAsync(): (event: string, ...args: any[]) => Promise<any> {
30
- return promisify(this.alter);
31
- }
32
-
33
- getHooks(): FormioHooks {
34
- return {
35
- alter: this.getHooksProvider("alter"),
36
- on: this.getHooksProvider("on")
37
- };
38
- }
39
-
40
- protected getProviders(type: "alter" | "on") {
41
- return this.injector.getProviders(`formio:${type}`);
42
- }
43
-
44
- protected getHooksProvider(type: "alter" | "on") {
45
- return this.bindHooks(type, this.createHooks(type));
46
- }
47
-
48
- protected bindHooks(type: "alter" | "on", hooks: Record<string, Function[]>) {
49
- return Object.entries(hooks).reduce((newHooks, [key, pool]) => {
50
- const wrap = (input: any, ...args: any[]): any => {
51
- let last: any;
52
-
53
- for (const fn of pool) {
54
- const result = fn(input, ...args);
55
-
56
- if (type === "alter") {
57
- input = result;
58
- } else {
59
- last = result;
60
- }
61
- }
62
-
63
- return type === "alter" ? input : last;
64
- };
65
-
66
- return {
67
- ...newHooks,
68
- [key]: wrap
69
- };
70
- }, {});
71
- }
72
-
73
- private createHooks(type: "alter" | "on") {
74
- return this.getProviders(type).reduce<Record<string, Function[]>>((hooks, provider) => {
75
- const instance = this.injector.invoke<any>(provider.token);
76
- const name = provider.store.get(`formio:${type}:name`);
77
- const pool: Function[] = hooks[name] || [];
78
-
79
- const hook = (...args: any[]) =>
80
- instance[type === "alter" ? "transform" : "on"](
81
- ...args.map((input: any) => {
82
- return input && input.$ctx ? input.$ctx : input;
83
- })
84
- );
85
-
86
- return {
87
- ...hooks,
88
- [name]: ([] as Function[]).concat(pool, hook)
89
- };
90
- }, {});
91
- }
92
- }
@@ -1,146 +0,0 @@
1
- import {faker} from "@faker-js/faker";
2
- import {PlatformTest} from "@tsed/platform-http/testing";
3
-
4
- import {FormioInstaller} from "./FormioInstaller.js";
5
- import {FormioService} from "./FormioService.js";
6
-
7
- async function createFormioInstallerFixture(options: any = {}) {
8
- const {
9
- count = 1,
10
- errorCount = null,
11
- submission = {
12
- _id: "id",
13
- data: {}
14
- },
15
- errorSubmission = null
16
- } = options;
17
-
18
- const collections = {
19
- estimatedDocumentCount: vi.fn().mockImplementation((cb) => {
20
- cb(errorCount, count);
21
- })
22
- };
23
- const formioService = {
24
- db: {
25
- collection: vi.fn().mockReturnValue(collections)
26
- },
27
- formio: {},
28
- encrypt: vi.fn().mockResolvedValue("hash"),
29
- resources: {
30
- submission: {
31
- model: {
32
- create: vi.fn().mockImplementation((_, cb) => {
33
- cb(errorSubmission, submission);
34
- })
35
- }
36
- }
37
- },
38
- importTemplate: vi.fn().mockImplementation((o) => o)
39
- };
40
- const service = await PlatformTest.invoke<FormioInstaller>(FormioInstaller, [
41
- {
42
- token: FormioService,
43
- use: formioService
44
- }
45
- ]);
46
-
47
- return {service, formioService, collections};
48
- }
49
-
50
- describe("FormioImporter", () => {
51
- beforeEach(PlatformTest.create);
52
- afterEach(PlatformTest.reset);
53
-
54
- describe("createRootUser()", () => {
55
- it("should create the user root", async () => {
56
- const {service, formioService} = await createFormioInstallerFixture({count: 1});
57
- const template = {
58
- resources: {
59
- admin: {
60
- _id: faker.string.uuid()
61
- }
62
- },
63
- roles: {
64
- administrator: {
65
- _id: faker.string.uuid()
66
- }
67
- }
68
- };
69
-
70
- const user = {
71
- email: faker.internet.email(),
72
- password: faker.internet.password({length: 12})
73
- };
74
-
75
- expect(await service.createRootUser(user, template as any)).toEqual({
76
- _id: "id",
77
- data: {}
78
- });
79
- expect(formioService.resources.submission.model.create).toHaveBeenCalledWith(
80
- {
81
- data: {email: user.email, password: "hash"},
82
- form: template.resources.admin._id,
83
- roles: [template.roles.administrator._id]
84
- },
85
- expect.any(Function)
86
- );
87
- });
88
- it("should throw error", async () => {
89
- const {service} = await createFormioInstallerFixture({errorSubmission: new Error("message")});
90
- const template = {
91
- resources: {
92
- admin: {
93
- _id: faker.string.uuid()
94
- }
95
- },
96
- roles: {
97
- administrator: {
98
- _id: faker.string.uuid()
99
- }
100
- }
101
- };
102
-
103
- const user = {
104
- email: faker.internet.email(),
105
- password: faker.internet.password({length: 12})
106
- };
107
-
108
- let actualError: any;
109
- try {
110
- await service.createRootUser(user, template as any);
111
- } catch (er) {
112
- actualError = er;
113
- }
114
- expect(actualError.message).toEqual("message");
115
- });
116
- });
117
- describe("install()", () => {
118
- it("should install database", async () => {
119
- const {service, formioService} = await createFormioInstallerFixture({count: 1});
120
- const template = {
121
- resources: {
122
- admin: {
123
- _id: faker.string.uuid()
124
- }
125
- },
126
- roles: {
127
- administrator: {
128
- _id: faker.string.uuid()
129
- }
130
- }
131
- };
132
-
133
- const user = {
134
- email: faker.internet.email(),
135
- password: faker.internet.password({length: 12})
136
- };
137
-
138
- vi.spyOn(service, "createRootUser");
139
-
140
- await service.install(template as any, user);
141
-
142
- expect(formioService.importTemplate).toHaveBeenCalledWith(template);
143
- expect(service.createRootUser).toHaveBeenCalledWith(user, template);
144
- });
145
- });
146
- });
@@ -1,46 +0,0 @@
1
- import {Inject, Injectable} from "@tsed/di";
2
- import {Logger} from "@tsed/logger";
3
-
4
- import {FormioSubmission} from "../domain/FormioModels.js";
5
- import {FormioTemplate} from "../domain/FormioTemplate.js";
6
- import {FormioDatabase} from "./FormioDatabase.js";
7
-
8
- @Injectable()
9
- export class FormioInstaller extends FormioDatabase {
10
- @Inject()
11
- protected logger: Logger;
12
-
13
- async install(template: FormioTemplate, root: any) {
14
- this.logger.info("Install formio template...");
15
- template = await this.formio.importTemplate(template);
16
-
17
- if (root) {
18
- this.logger.info("Create root user...");
19
- await this.createRootUser(root, template);
20
- }
21
- }
22
-
23
- async createRootUser<User = unknown>(user: {email: string; password: string}, template: FormioTemplate): Promise<FormioSubmission<User>> {
24
- const hash = await this.formio.encrypt(user.password);
25
-
26
- return new Promise((resolve, reject) => {
27
- this.formio.resources.submission.model.create(
28
- {
29
- form: template.resources.admin._id,
30
- data: {
31
- email: user.email,
32
- password: hash
33
- },
34
- roles: [template.roles?.administrator._id].filter(Boolean)
35
- },
36
- (err: unknown, item: any) => {
37
- if (err) {
38
- return reject(err);
39
- }
40
-
41
- resolve(item);
42
- }
43
- );
44
- });
45
- }
46
- }
@@ -1,114 +0,0 @@
1
- import {Injectable} from "@tsed/di";
2
- import {PlatformTest} from "@tsed/platform-http/testing";
3
-
4
- import {FormioDatabase} from "./FormioDatabase.js";
5
- import {FormioRepository} from "./FormioRepository.js";
6
-
7
- @Injectable()
8
- class PackagesRepository extends FormioRepository {
9
- formName = "package";
10
- }
11
-
12
- describe("FormioRepository", () => {
13
- beforeEach(() => PlatformTest.create());
14
- afterEach(() => PlatformTest.reset());
15
-
16
- describe("saveSubmission()", () => {
17
- it("should create submission", async () => {
18
- const database = {
19
- formModel: {
20
- findOne: vi.fn().mockResolvedValue({
21
- _id: "id"
22
- })
23
- },
24
- saveSubmission: vi.fn().mockImplementation((o) => o)
25
- };
26
-
27
- const service = await PlatformTest.invoke<PackagesRepository>(PackagesRepository, [
28
- {
29
- token: FormioDatabase,
30
- use: database
31
- }
32
- ]);
33
-
34
- const result = await service.saveSubmission({
35
- data: {
36
- label: "label"
37
- }
38
- });
39
-
40
- expect(result).toEqual({
41
- data: {
42
- label: "label"
43
- },
44
- form: "id"
45
- });
46
- expect(database.saveSubmission).toHaveBeenCalledWith({
47
- data: {
48
- label: "label"
49
- },
50
- form: "id"
51
- });
52
- });
53
- });
54
- describe("getSubmissions()", () => {
55
- it("should get all saved submissions", async () => {
56
- const database = {
57
- formModel: {
58
- findOne: vi.fn().mockResolvedValue({
59
- _id: "id"
60
- })
61
- },
62
- getSubmissions: vi.fn().mockResolvedValue([])
63
- };
64
-
65
- const service = await PlatformTest.invoke<PackagesRepository>(PackagesRepository, [
66
- {
67
- token: FormioDatabase,
68
- use: database
69
- }
70
- ]);
71
-
72
- const submissions = await service.getSubmissions();
73
-
74
- expect(submissions).toEqual([]);
75
- expect(database.formModel.findOne).toHaveBeenCalledWith({
76
- name: {
77
- $eq: "package"
78
- }
79
- });
80
- expect(database.getSubmissions).toHaveBeenCalledWith({form: "id"});
81
- });
82
- });
83
- describe("findOneSubmission()", () => {
84
- it("should find on submission", async () => {
85
- const database = {
86
- formModel: {
87
- findOne: vi.fn().mockResolvedValue({
88
- _id: "id"
89
- })
90
- },
91
- submissionModel: {
92
- findOne: vi.fn().mockResolvedValue({_id: "id"})
93
- }
94
- };
95
-
96
- const service = await PlatformTest.invoke<PackagesRepository>(PackagesRepository, [
97
- {
98
- token: FormioDatabase,
99
- use: database
100
- }
101
- ]);
102
-
103
- const submission = await service.findOneSubmission({});
104
-
105
- expect(submission).toEqual({_id: "id"});
106
- expect(database.formModel.findOne).toHaveBeenCalledWith({
107
- name: {
108
- $eq: "package"
109
- }
110
- });
111
- expect(database.submissionModel.findOne).toHaveBeenCalledWith({deleted: null, form: "id"});
112
- });
113
- });
114
- });
@@ -1,49 +0,0 @@
1
- import {Inject} from "@tsed/di";
2
- import {MongooseDocument, MongooseModel} from "@tsed/mongoose";
3
- import type {FilterQuery} from "mongoose";
4
-
5
- import {FormioSubmission} from "../domain/FormioModels.js";
6
- import {FormioDatabase} from "./FormioDatabase.js";
7
-
8
- export abstract class FormioRepository<SubmissionData = any> {
9
- @Inject()
10
- protected formioDatabase: FormioDatabase;
11
-
12
- protected abstract formName: string;
13
-
14
- private formId: string;
15
-
16
- async getFormId() {
17
- if (!this.formId) {
18
- const form = await this.formioDatabase.formModel.findOne({name: {$eq: this.formName}});
19
-
20
- if (form) {
21
- this.formId = form._id;
22
- }
23
- }
24
-
25
- return this.formId;
26
- }
27
-
28
- async saveSubmission(submission: Omit<Partial<FormioSubmission<SubmissionData>>, "form"> & {form?: any}) {
29
- return this.formioDatabase.saveSubmission<SubmissionData>({
30
- ...submission,
31
- form: submission.form || (await this.getFormId())
32
- });
33
- }
34
-
35
- async getSubmissions(query: FilterQuery<MongooseModel<FormioSubmission<SubmissionData>>> = {}) {
36
- return this.formioDatabase.getSubmissions<SubmissionData>({
37
- ...query,
38
- form: await this.getFormId()
39
- });
40
- }
41
-
42
- async findOneSubmission(query: any): Promise<MongooseDocument<FormioSubmission<SubmissionData>> | undefined> {
43
- return this.formioDatabase.submissionModel.findOne({
44
- form: await this.getFormId(),
45
- deleted: null,
46
- ...query
47
- }) as any;
48
- }
49
- }