ddd-ts-kit 0.3.0 → 0.4.0

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 (254) hide show
  1. package/README.md +44 -1
  2. package/dist/check/canonical/roles.d.ts.map +1 -1
  3. package/dist/check/canonical/roles.js +18 -1
  4. package/dist/check/canonical/roles.js.map +1 -1
  5. package/dist/check/catalogue/eslint-only-rules.d.ts.map +1 -1
  6. package/dist/check/catalogue/eslint-only-rules.js +12 -0
  7. package/dist/check/catalogue/eslint-only-rules.js.map +1 -1
  8. package/dist/check/declarations/classes-of.d.ts +2 -2
  9. package/dist/check/declarations/classes-of.js +4 -6
  10. package/dist/check/declarations/classes-of.js.map +1 -1
  11. package/dist/check/explain/tree-docs.d.ts.map +1 -1
  12. package/dist/check/explain/tree-docs.js +3 -2
  13. package/dist/check/explain/tree-docs.js.map +1 -1
  14. package/dist/check/fixtures/authz-declared-fixtures.d.ts.map +1 -1
  15. package/dist/check/fixtures/authz-declared-fixtures.js +19 -0
  16. package/dist/check/fixtures/authz-declared-fixtures.js.map +1 -1
  17. package/dist/check/fixtures/errors-fixtures.d.ts.map +1 -1
  18. package/dist/check/fixtures/errors-fixtures.js +10 -0
  19. package/dist/check/fixtures/errors-fixtures.js.map +1 -1
  20. package/dist/check/fixtures/tree-fixtures.d.ts.map +1 -1
  21. package/dist/check/fixtures/tree-fixtures.js +25 -3
  22. package/dist/check/fixtures/tree-fixtures.js.map +1 -1
  23. package/dist/check/rules/authz-declared.d.ts.map +1 -1
  24. package/dist/check/rules/authz-declared.js +24 -0
  25. package/dist/check/rules/authz-declared.js.map +1 -1
  26. package/dist/check/rules/declarations-role-declared.js +1 -1
  27. package/dist/check/rules/declarations-role-declared.js.map +1 -1
  28. package/dist/check/rules/errors-known-code.js +2 -2
  29. package/dist/check/rules/errors-known-code.js.map +1 -1
  30. package/dist/commands/controllers.d.ts +8 -0
  31. package/dist/commands/controllers.d.ts.map +1 -0
  32. package/dist/commands/controllers.js +57 -0
  33. package/dist/commands/controllers.js.map +1 -0
  34. package/dist/commands/generate.js +10 -0
  35. package/dist/commands/generate.js.map +1 -1
  36. package/dist/commands/init.d.ts.map +1 -1
  37. package/dist/commands/init.js +1 -0
  38. package/dist/commands/init.js.map +1 -1
  39. package/dist/config/index.d.ts +1 -1
  40. package/dist/config/index.d.ts.map +1 -1
  41. package/dist/config/index.js +1 -1
  42. package/dist/config/index.js.map +1 -1
  43. package/dist/config/parse-stack.d.ts.map +1 -1
  44. package/dist/config/parse-stack.js +2 -1
  45. package/dist/config/parse-stack.js.map +1 -1
  46. package/dist/config/stack.d.ts +9 -0
  47. package/dist/config/stack.d.ts.map +1 -1
  48. package/dist/config/stack.js +1 -0
  49. package/dist/config/stack.js.map +1 -1
  50. package/dist/controllers/entries-of.d.ts +3 -0
  51. package/dist/controllers/entries-of.d.ts.map +1 -0
  52. package/dist/controllers/entries-of.js +19 -0
  53. package/dist/controllers/entries-of.js.map +1 -0
  54. package/dist/controllers/finish-migration.d.ts +8 -0
  55. package/dist/controllers/finish-migration.d.ts.map +1 -0
  56. package/dist/controllers/finish-migration.js +71 -0
  57. package/dist/controllers/finish-migration.js.map +1 -0
  58. package/dist/controllers/function-route.d.ts +23 -0
  59. package/dist/controllers/function-route.d.ts.map +1 -0
  60. package/dist/controllers/function-route.js +2 -0
  61. package/dist/controllers/function-route.js.map +1 -0
  62. package/dist/controllers/handle-body.d.ts +8 -0
  63. package/dist/controllers/handle-body.d.ts.map +1 -0
  64. package/dist/controllers/handle-body.js +29 -0
  65. package/dist/controllers/handle-body.js.map +1 -0
  66. package/dist/controllers/handle-types-of.d.ts +15 -0
  67. package/dist/controllers/handle-types-of.d.ts.map +1 -0
  68. package/dist/controllers/handle-types-of.js +48 -0
  69. package/dist/controllers/handle-types-of.js.map +1 -0
  70. package/dist/controllers/migrate-context.d.ts +4 -0
  71. package/dist/controllers/migrate-context.d.ts.map +1 -0
  72. package/dist/controllers/migrate-context.js +74 -0
  73. package/dist/controllers/migrate-context.js.map +1 -0
  74. package/dist/controllers/migrated-group.d.ts +11 -0
  75. package/dist/controllers/migrated-group.d.ts.map +1 -0
  76. package/dist/controllers/migrated-group.js +2 -0
  77. package/dist/controllers/migrated-group.js.map +1 -0
  78. package/dist/controllers/migration.d.ts +16 -0
  79. package/dist/controllers/migration.d.ts.map +1 -0
  80. package/dist/controllers/migration.js +2 -0
  81. package/dist/controllers/migration.js.map +1 -0
  82. package/dist/controllers/plan-context.d.ts +22 -0
  83. package/dist/controllers/plan-context.d.ts.map +1 -0
  84. package/dist/controllers/plan-context.js +70 -0
  85. package/dist/controllers/plan-context.js.map +1 -0
  86. package/dist/controllers/read-function-route.d.ts +4 -0
  87. package/dist/controllers/read-function-route.d.ts.map +1 -0
  88. package/dist/controllers/read-function-route.js +37 -0
  89. package/dist/controllers/read-function-route.js.map +1 -0
  90. package/dist/controllers/render-controller.d.ts +15 -0
  91. package/dist/controllers/render-controller.d.ts.map +1 -0
  92. package/dist/controllers/render-controller.js +104 -0
  93. package/dist/controllers/render-controller.js.map +1 -0
  94. package/dist/declared/declared-role.d.ts +8 -2
  95. package/dist/declared/declared-role.d.ts.map +1 -1
  96. package/dist/declared/declared-role.js +17 -3
  97. package/dist/declared/declared-role.js.map +1 -1
  98. package/dist/declared/index.d.ts +1 -1
  99. package/dist/declared/index.d.ts.map +1 -1
  100. package/dist/declared/index.js +1 -1
  101. package/dist/declared/index.js.map +1 -1
  102. package/dist/depcruise/hygiene-rules.js +2 -1
  103. package/dist/depcruise/hygiene-rules.js.map +1 -1
  104. package/dist/describe/context-description.d.ts +5 -0
  105. package/dist/describe/context-description.d.ts.map +1 -1
  106. package/dist/describe/decorator-options.d.ts +3 -0
  107. package/dist/describe/decorator-options.d.ts.map +1 -0
  108. package/dist/describe/decorator-options.js +20 -0
  109. package/dist/describe/decorator-options.js.map +1 -0
  110. package/dist/describe/format-description.js +4 -1
  111. package/dist/describe/format-description.js.map +1 -1
  112. package/dist/describe/listed-in.d.ts +3 -0
  113. package/dist/describe/listed-in.d.ts.map +1 -0
  114. package/dist/describe/listed-in.js +9 -0
  115. package/dist/describe/listed-in.js.map +1 -0
  116. package/dist/describe/read-controllers.d.ts +5 -0
  117. package/dist/describe/read-controllers.d.ts.map +1 -0
  118. package/dist/describe/read-controllers.js +98 -0
  119. package/dist/describe/read-controllers.js.map +1 -0
  120. package/dist/describe/read-route-groups.d.ts +5 -0
  121. package/dist/describe/read-route-groups.d.ts.map +1 -0
  122. package/dist/describe/read-route-groups.js +37 -0
  123. package/dist/describe/read-route-groups.js.map +1 -0
  124. package/dist/describe/read-routes.d.ts +4 -1
  125. package/dist/describe/read-routes.d.ts.map +1 -1
  126. package/dist/describe/read-routes.js +10 -4
  127. package/dist/describe/read-routes.js.map +1 -1
  128. package/dist/describe/reference-lines.js +13 -6
  129. package/dist/describe/reference-lines.js.map +1 -1
  130. package/dist/describe/route-group-fact.d.ts +11 -0
  131. package/dist/describe/route-group-fact.d.ts.map +1 -0
  132. package/dist/describe/route-group-fact.js +2 -0
  133. package/dist/describe/route-group-fact.js.map +1 -0
  134. package/dist/generate/kind.d.ts +12 -0
  135. package/dist/generate/kind.d.ts.map +1 -1
  136. package/dist/generate/kinds/declare-context-entries.d.ts +1 -1
  137. package/dist/generate/kinds/declare-context-entries.js +4 -3
  138. package/dist/generate/kinds/declare-context-entries.js.map +1 -1
  139. package/dist/generate/kinds/e2e-kind.d.ts.map +1 -1
  140. package/dist/generate/kinds/e2e-kind.js +29 -6
  141. package/dist/generate/kinds/e2e-kind.js.map +1 -1
  142. package/dist/generate/kinds/extraction/service-selection.d.ts.map +1 -1
  143. package/dist/generate/kinds/extraction/service-selection.js +2 -1
  144. package/dist/generate/kinds/extraction/service-selection.js.map +1 -1
  145. package/dist/generate/kinds/guard-kind.d.ts +3 -0
  146. package/dist/generate/kinds/guard-kind.d.ts.map +1 -0
  147. package/dist/generate/kinds/guard-kind.js +27 -0
  148. package/dist/generate/kinds/guard-kind.js.map +1 -0
  149. package/dist/generate/kinds/route/controller-imports.d.ts +17 -0
  150. package/dist/generate/kinds/route/controller-imports.d.ts.map +1 -0
  151. package/dist/generate/kinds/route/controller-imports.js +22 -0
  152. package/dist/generate/kinds/route/controller-imports.js.map +1 -0
  153. package/dist/generate/kinds/route/controller-plan.d.ts +12 -0
  154. package/dist/generate/kinds/route/controller-plan.d.ts.map +1 -0
  155. package/dist/generate/kinds/route/controller-plan.js +2 -0
  156. package/dist/generate/kinds/route/controller-plan.js.map +1 -0
  157. package/dist/generate/kinds/route/controller-types.d.ts +18 -0
  158. package/dist/generate/kinds/route/controller-types.d.ts.map +1 -0
  159. package/dist/generate/kinds/route/controller-types.js +27 -0
  160. package/dist/generate/kinds/route/controller-types.js.map +1 -0
  161. package/dist/generate/kinds/route/controller-vars.d.ts +6 -0
  162. package/dist/generate/kinds/route/controller-vars.d.ts.map +1 -0
  163. package/dist/generate/kinds/route/controller-vars.js +117 -0
  164. package/dist/generate/kinds/route/controller-vars.js.map +1 -0
  165. package/dist/generate/kinds/route/group-file.d.ts +3 -0
  166. package/dist/generate/kinds/route/group-file.d.ts.map +1 -0
  167. package/dist/generate/kinds/route/group-file.js +5 -0
  168. package/dist/generate/kinds/route/group-file.js.map +1 -0
  169. package/dist/generate/kinds/route/group-prefix.d.ts +4 -0
  170. package/dist/generate/kinds/route/group-prefix.d.ts.map +1 -0
  171. package/dist/generate/kinds/route/group-prefix.js +17 -0
  172. package/dist/generate/kinds/route/group-prefix.js.map +1 -0
  173. package/dist/generate/kinds/route/group-vars.d.ts +17 -0
  174. package/dist/generate/kinds/route/group-vars.d.ts.map +1 -0
  175. package/dist/generate/kinds/route/group-vars.js +33 -0
  176. package/dist/generate/kinds/route/group-vars.js.map +1 -0
  177. package/dist/generate/kinds/route/plan-controller.d.ts +6 -0
  178. package/dist/generate/kinds/route/plan-controller.d.ts.map +1 -0
  179. package/dist/generate/kinds/route/plan-controller.js +16 -0
  180. package/dist/generate/kinds/route/plan-controller.js.map +1 -0
  181. package/dist/generate/kinds/route/resolve-group.d.ts +11 -0
  182. package/dist/generate/kinds/route/resolve-group.d.ts.map +1 -0
  183. package/dist/generate/kinds/route/resolve-group.js +31 -0
  184. package/dist/generate/kinds/route/resolve-group.js.map +1 -0
  185. package/dist/generate/kinds/route/resource-of.d.ts +5 -0
  186. package/dist/generate/kinds/route/resource-of.d.ts.map +1 -0
  187. package/dist/generate/kinds/route/resource-of.js +6 -0
  188. package/dist/generate/kinds/route/resource-of.js.map +1 -0
  189. package/dist/generate/kinds/route/route-group.d.ts +11 -0
  190. package/dist/generate/kinds/route/route-group.d.ts.map +1 -0
  191. package/dist/generate/kinds/route/route-group.js +2 -0
  192. package/dist/generate/kinds/route/route-group.js.map +1 -0
  193. package/dist/generate/kinds/route/write-controller.d.ts +5 -0
  194. package/dist/generate/kinds/route/write-controller.d.ts.map +1 -0
  195. package/dist/generate/kinds/route/write-controller.js +55 -0
  196. package/dist/generate/kinds/route/write-controller.js.map +1 -0
  197. package/dist/generate/kinds/route-group-kind.d.ts +3 -0
  198. package/dist/generate/kinds/route-group-kind.d.ts.map +1 -0
  199. package/dist/generate/kinds/route-group-kind.js +55 -0
  200. package/dist/generate/kinds/route-group-kind.js.map +1 -0
  201. package/dist/generate/kinds/route-kind.d.ts +2 -0
  202. package/dist/generate/kinds/route-kind.d.ts.map +1 -1
  203. package/dist/generate/kinds/route-kind.js +19 -4
  204. package/dist/generate/kinds/route-kind.js.map +1 -1
  205. package/dist/generate/kinds/write-context-files.d.ts.map +1 -1
  206. package/dist/generate/kinds/write-context-files.js +5 -4
  207. package/dist/generate/kinds/write-context-files.js.map +1 -1
  208. package/dist/generate/kinds.d.ts.map +1 -1
  209. package/dist/generate/kinds.js +6 -1
  210. package/dist/generate/kinds.js.map +1 -1
  211. package/dist/generate/lists-routes.d.ts +8 -0
  212. package/dist/generate/lists-routes.d.ts.map +1 -0
  213. package/dist/generate/lists-routes.js +10 -0
  214. package/dist/generate/lists-routes.js.map +1 -0
  215. package/dist/init/files/audit-files.d.ts.map +1 -1
  216. package/dist/init/files/audit-files.js +2 -7
  217. package/dist/init/files/audit-files.js.map +1 -1
  218. package/dist/init/manifest/versions.js +23 -23
  219. package/dist/init/stack/resolve-stack.d.ts.map +1 -1
  220. package/dist/init/stack/resolve-stack.js +3 -1
  221. package/dist/init/stack/resolve-stack.js.map +1 -1
  222. package/dist/init/stack/stack-options.d.ts +1 -0
  223. package/dist/init/stack/stack-options.d.ts.map +1 -1
  224. package/dist/run.d.ts.map +1 -1
  225. package/dist/run.js +7 -0
  226. package/dist/run.js.map +1 -1
  227. package/dist/stack/audit-vars.d.ts.map +1 -1
  228. package/dist/stack/audit-vars.js +3 -1
  229. package/dist/stack/audit-vars.js.map +1 -1
  230. package/dist/stack/stack-vars.d.ts.map +1 -1
  231. package/dist/stack/stack-vars.js +16 -0
  232. package/dist/stack/stack-vars.js.map +1 -1
  233. package/dist/version.d.ts +1 -1
  234. package/dist/version.js +1 -1
  235. package/package.json +2 -2
  236. package/templates/context/wiring.declared.tpl +2 -2
  237. package/templates/controller/controller.class-validator.ts.tpl +85 -0
  238. package/templates/controller/controller.none.ts.tpl +101 -0
  239. package/templates/controller/controller.search.ts.tpl +84 -0
  240. package/templates/controller/controller.spec.ts.tpl +49 -0
  241. package/templates/controller/controller.standard-schema.ts.tpl +113 -0
  242. package/templates/controller/controller.zod.ts.tpl +99 -0
  243. package/templates/controller/group.ts.tpl +4 -0
  244. package/templates/controller/guard.spec.ts.tpl +27 -0
  245. package/templates/controller/guard.ts.tpl +14 -0
  246. package/templates/controller/mapper.ts.tpl +10 -0
  247. package/templates/controller/routes.tsyringe.tpl +5 -0
  248. package/templates/controller/serializer.ts.tpl +10 -0
  249. package/templates/controller/wiring.manual.tpl +5 -0
  250. package/templates/init/platform/audit/audit.module.manual.ts.tpl +18 -0
  251. package/templates/init/platform/audit/search-audit-entries.controller.ts.tpl +193 -0
  252. package/templates/init/platform/compose-application.tsyringe.ts.tpl +38 -4
  253. package/templates/init/rules/presentation.md.tpl +14 -6
  254. package/templates/pipeline/package-scripts.json +1 -1
@@ -0,0 +1,99 @@
1
+ {{#if route.declaresSchema}}
2
+ import { z } from 'zod';
3
+ {{/if}}
4
+ import { {{route.httpImports}} } from '@ddd-ts-kit/http';
5
+ import type { {{route.busType}} } from '@ddd-ts-kit/kernel';
6
+ {{#if route.usesInfer}}
7
+ import type { Infer } from '@ddd-ts-kit/validation';
8
+ {{/if}}
9
+ {{#if route.declaresSchema}}
10
+ import { zodSchema } from '@ddd-ts-kit/validation/zod';
11
+ {{/if}}
12
+ {{route.imports}}
13
+
14
+ {{#if route.hasParams}}
15
+ const params = zodSchema(z.object({ {{route.zodParams}} }));
16
+
17
+ {{/if}}
18
+ {{#if route.hasBody}}
19
+ const body = zodSchema(
20
+ z.object({
21
+ // TODO(domain): declare the fields the client sends for "{{name.words}}", with shape constraints only (types, formats, lengths); business rules stay in the domain.
22
+ }),
23
+ );
24
+
25
+ {{/if}}
26
+ {{#if route.returnsId}}
27
+ const accepted = zodSchema(z.object({ id: z.string() }), { io: 'output' });
28
+
29
+ {{/if}}
30
+ {{#if route.isQuery}}
31
+ const answer = zodSchema(
32
+ z.object({
33
+ // TODO(domain): declare the fields of {{name.pascal}}Result that are part of the public contract.
34
+ }),
35
+ { io: 'output' },
36
+ );
37
+
38
+ {{/if}}
39
+ @Controller({
40
+ {{route.access}}
41
+ method: '{{route.method}}',
42
+ path: '{{route.groupPath}}',
43
+ summary: '{{name.title}}',
44
+ {{#if route.hasTypedParams}}
45
+ params,
46
+ {{/if}}
47
+ {{#if route.hasBody}}
48
+ body,
49
+ {{/if}}
50
+ {{#if route.returnsId}}
51
+ response: { status: {{route.status}}, description: 'Done; the body carries the identifier.', body: accepted },
52
+ {{/if}}
53
+ {{#if route.returnsNothing}}
54
+ response: { status: {{route.status}}, description: 'Done.' },
55
+ {{/if}}
56
+ {{#if route.isQuery}}
57
+ response: { status: {{route.status}}, description: 'The answer.', body: answer },
58
+ {{/if}}
59
+ // TODO(domain): list the codes of the refusals "{{name.words}}" can raise, as their factories set them, so that they appear in the contract with their status (`errors/known-code`).
60
+ errors: [],
61
+ })
62
+ export class {{name.pascal}}Controller {
63
+ constructor({{route.constructorParams}}) {}
64
+
65
+ {{#if route.isQuery}}
66
+ async handle({{route.requestParam}}): Promise<{{route.answerType}}> {
67
+ {{#unless route.mapper}}
68
+ // TODO(domain): build the query from `request` (params, query string); the actor is already there.
69
+ {{/unless}}
70
+ {{#if route.serializer}}
71
+ return this.serializer.serialize(await this.{{route.bus}}.execute({{route.message}}));
72
+ {{/if}}
73
+ {{#unless route.serializer}}
74
+ // TODO(domain): map the answer of the query to the public fields.
75
+ await this.{{route.bus}}.execute({{route.message}});
76
+
77
+ return {};
78
+ {{/unless}}
79
+ }
80
+ {{/if}}
81
+ {{#if route.returnsId}}
82
+ async handle({{route.requestParam}}): Promise<{{route.acceptedType}}> {
83
+ {{#unless route.mapper}}
84
+ // TODO(domain): build the command from `request` (body, params); the actor is already there.
85
+ {{/unless}}
86
+ const id = await this.{{route.bus}}.execute({{route.message}});
87
+
88
+ return { id };
89
+ }
90
+ {{/if}}
91
+ {{#if route.returnsNothing}}
92
+ async handle({{route.requestParam}}): Promise<void> {
93
+ {{#unless route.mapper}}
94
+ // TODO(domain): build the command from `request` (body, params); the actor is already there.
95
+ {{/unless}}
96
+ await this.{{route.bus}}.execute({{route.message}});
97
+ }
98
+ {{/if}}
99
+ }
@@ -0,0 +1,4 @@
1
+ {{group.imports}}
2
+
3
+ @RouteGroup({ {{group.options}} })
4
+ export class {{group.pascal}}Routes {}
@@ -0,0 +1,27 @@
1
+ import { describe, expect, it } from '{{tests.module}}';
2
+ import { ForbiddenError, type GuardRequest } from '@ddd-ts-kit/http';
3
+ import { anonymousActor, executionContext } from '@ddd-ts-kit/kernel';
4
+ import { {{name.pascal}}Guard } from '{{alias.context}}/presentation/http/guards/{{name.kebab}}.guard{{alias.ext}}';
5
+
6
+ const actor = { id: 'user-1', type: 'user', roles: [], attributes: {} } as const;
7
+ const execution = executionContext({ correlationId: 'correlation-1', actor, origin: 'http' });
8
+
9
+ const request: GuardRequest = {
10
+ method: 'GET',
11
+ path: '/',
12
+ operationId: 'anOperation',
13
+ params: {},
14
+ headers: {},
15
+ actor,
16
+ context: { requestId: 'request-1', execution, raw: undefined },
17
+ };
18
+
19
+ describe('{{name.pascal}}Guard', () => {
20
+ it('refuses a caller with 403', () => {
21
+ expect(() => new {{name.pascal}}Guard().check(request)).toThrow(ForbiddenError);
22
+ expect(anonymousActor().type).toBe('anonymous');
23
+ });
24
+
25
+ // TODO(domain): replace the test above by one per decision of the guard.
26
+ it.todo('lets through the caller it is written for');
27
+ });
@@ -0,0 +1,14 @@
1
+ import { ForbiddenError, Guard, type GuardRequest } from '@ddd-ts-kit/http';
2
+
3
+ // TODO(domain): say in one sentence what this guard controls at the edge. It reads the caller and the request, never an aggregate: a right that depends on the element is a permission of the use case (`@Requires`).
4
+ @Guard()
5
+ export class {{name.pascal}}Guard implements Guard {
6
+ readonly documents = { errors: ['FORBIDDEN'] };
7
+
8
+ check({ actor }: GuardRequest): void {
9
+ // TODO(domain): replace this refusal of everybody by the control: a role, a membership read from the path, a key of the request.
10
+ if (actor.id !== '') {
11
+ throw new ForbiddenError('The caller may not use these routes');
12
+ }
13
+ }
14
+ }
@@ -0,0 +1,10 @@
1
+ import { RequestMapper, type HttpRequest } from '@ddd-ts-kit/http';
2
+ {{route.mapperImports}}
3
+
4
+ @RequestMapper()
5
+ export class {{name.pascal}}Mapper implements RequestMapper<{{route.mapperRequest}}, {{route.input}}> {
6
+ map(request: {{route.mapperRequest}}): {{route.input}} {
7
+ // TODO(domain): hand over what the {{route.messageKind}} needs from `request` (params, query, body){{route.actorNote}}.
8
+ return {{route.mapperMessage}};
9
+ }
10
+ }
@@ -0,0 +1,5 @@
1
+ @@imports
2
+ import { controllerRoutes } from '@ddd-ts-kit/http';
3
+ {{route.wiringImports}}
4
+ @@routes
5
+ ...controllerRoutes([new {{name.pascal}}Controller({{route.wiringArguments}}),
@@ -0,0 +1,10 @@
1
+ import { Serializer } from '@ddd-ts-kit/http';
2
+ {{route.serializerImports}}
3
+
4
+ @Serializer()
5
+ export class {{name.pascal}}Serializer implements Serializer<{{route.resultType}}, {{route.serializedType}}> {
6
+ serialize(_result: {{route.resultType}}): {{route.serializedType}} {
7
+ // TODO(domain): copy the public fields of the result, dates as ISO text; what the contract does not declare stays inside.
8
+ return {};
9
+ }
10
+ }
@@ -0,0 +1,5 @@
1
+ @@imports
2
+ import { controllerRoutes } from '@ddd-ts-kit/http';
3
+ {{route.wiringImports}}
4
+ @@routes
5
+ ...controllerRoutes([new {{name.pascal}}Controller({{route.platformArguments}}),
@@ -1,10 +1,21 @@
1
1
  import { requires, type UseCaseDeclaration } from '@ddd-ts-kit/auth';
2
+ {{#if audit.controller}}
3
+ import { controllerRoutes, type Route } from '@ddd-ts-kit/http';
4
+ {{/if}}
5
+ {{#if audit.functionRoute}}
2
6
  import type { Route } from '@ddd-ts-kit/http';
7
+ {{/if}}
3
8
  import type { Platform } from '{{alias.platform}}/platform{{alias.ext}}';
4
9
  import { auditPermissions } from '{{alias.platform}}/audit/audit.permissions{{alias.ext}}';
5
10
  import { SearchAuditEntriesHandler } from '{{alias.platform}}/audit/search-audit-entries.handler{{alias.ext}}';
6
11
  import { SearchAuditEntriesQuery } from '{{alias.platform}}/audit/search-audit-entries.query{{alias.ext}}';
12
+ {{#if audit.functionRoute}}
7
13
  import { searchAuditEntriesRoute } from '{{alias.platform}}/audit/search-audit-entries.route{{alias.ext}}';
14
+ {{/if}}
15
+ {{#if audit.controller}}
16
+ import { SearchAuditEntriesController } from '{{alias.platform}}/audit/search-audit-entries.controller{{alias.ext}}';
17
+ import { securitySchemes } from '{{alias.platform}}/auth/security-schemes{{alias.ext}}';
18
+ {{/if}}
8
19
 
9
20
  interface AuditWiring {
10
21
  readonly name: string;
@@ -34,6 +45,13 @@ export function auditModule(platform: Platform): AuditWiring {
34
45
  {{#unless authz.rebac}}
35
46
  useCases: [requires(SearchAuditEntriesQuery, auditPermissions['trail.read'])],
36
47
  {{/unless}}
48
+ {{#if audit.functionRoute}}
37
49
  routes: [searchAuditEntriesRoute(platform.queries)],
50
+ {{/if}}
51
+ {{#if audit.controller}}
52
+ routes: controllerRoutes([new SearchAuditEntriesController(platform.queries)], {
53
+ security: Object.keys(securitySchemes),
54
+ }),
55
+ {{/if}}
38
56
  };
39
57
  }
@@ -0,0 +1,193 @@
1
+ import type { AuditEntry } from '@ddd-ts-kit/audit';
2
+ import {
3
+ Authenticated,
4
+ Controller,
5
+ pageRequestFrom,
6
+ pageRequestParameters,
7
+ pageResponse,
8
+ pageSchemaOf,
9
+ type HttpRequest,
10
+ type PageBody,
11
+ } from '@ddd-ts-kit/http';
12
+ import { err, ok, type QueryBus } from '@ddd-ts-kit/kernel';
13
+ import { customSchema, type JsonSchema } from '@ddd-ts-kit/validation';
14
+ import { SearchAuditEntriesQuery, type AuditEntriesFilter } from '{{alias.platform}}/audit/search-audit-entries.query{{alias.ext}}';
15
+
16
+ /** Newest first unless asked otherwise, by cursor: the trail only grows, an offset would shift under the reader. */
17
+ const pageLimits = { sortable: ['occurredAt', 'sequence'], maxSize: 100, kind: 'cursor' } as const;
18
+ const outcomes = ['allowed', 'denied', 'succeeded', 'failed'] as const;
19
+
20
+ type Parameters = Readonly<Record<string, unknown>>;
21
+
22
+ const query = customSchema<Parameters>(
23
+ (input) =>
24
+ isRecord(input) ? ok(input) : err([{ path: '', code: 'invalid_type', message: 'Expected query parameters.' }]),
25
+ {
26
+ type: 'object',
27
+ properties: {
28
+ ...propertiesOf(pageRequestParameters(pageLimits)),
29
+ actorId: { type: 'string', description: 'Entries about what this actor did.' },
30
+ action: { type: 'string', description: 'A permission id, a command name or an event type.' },
31
+ outcome: { type: 'string', enum: [...outcomes] },
32
+ correlationId: { type: 'string', description: 'Everything one request or event caused.' },
33
+ },
34
+ },
35
+ );
36
+
37
+ /** What leaves the process: identifiers and codes, as the trail holds them. `changes` and `extensions` stay inside. */
38
+ interface AuditEntryView {
39
+ readonly id: string;
40
+ readonly chain: string;
41
+ readonly sequence: number;
42
+ readonly occurredAt: string;
43
+ readonly category: string;
44
+ readonly action: string;
45
+ readonly outcome: string;
46
+ readonly reason?: string;
47
+ readonly actorId: string;
48
+ readonly actorType: string;
49
+ readonly tenantId?: string;
50
+ readonly resourceType?: string;
51
+ readonly resourceId?: string;
52
+ readonly context?: string;
53
+ readonly correlationId: string;
54
+ readonly causationId?: string;
55
+ readonly origin: string;
56
+ readonly previousHash: string;
57
+ readonly hash: string;
58
+ }
59
+
60
+ const text = { type: 'string' } as const;
61
+
62
+ const entry = customSchema<AuditEntryView>(
63
+ (input) =>
64
+ isEntryView(input) ? ok(input) : err([{ path: '', code: 'invalid_type', message: 'Expected an audit entry.' }]),
65
+ {
66
+ type: 'object',
67
+ required: [
68
+ 'id',
69
+ 'chain',
70
+ 'sequence',
71
+ 'occurredAt',
72
+ 'category',
73
+ 'action',
74
+ 'outcome',
75
+ 'actorId',
76
+ 'actorType',
77
+ 'correlationId',
78
+ 'origin',
79
+ 'previousHash',
80
+ 'hash',
81
+ ],
82
+ properties: {
83
+ id: text,
84
+ chain: text,
85
+ sequence: { type: 'integer', minimum: 1 },
86
+ occurredAt: { type: 'string', format: 'date-time' },
87
+ category: text,
88
+ action: text,
89
+ outcome: { type: 'string', enum: [...outcomes] },
90
+ reason: text,
91
+ actorId: text,
92
+ actorType: text,
93
+ tenantId: text,
94
+ resourceType: text,
95
+ resourceId: text,
96
+ context: text,
97
+ correlationId: text,
98
+ causationId: text,
99
+ origin: text,
100
+ previousHash: text,
101
+ hash: text,
102
+ },
103
+ },
104
+ );
105
+
106
+ /** The audit trail as a protected read: a use case like any other, so reading it is authorized and recorded. */
107
+ @Controller({
108
+ method: 'GET',
109
+ path: '/audit/entries',
110
+ summary: 'Search the entries of the audit trail',
111
+ tags: ['audit'],
112
+ guards: [Authenticated],
113
+ query,
114
+ response: { status: 200, description: 'A page of entries.', body: pageSchemaOf(entry) },
115
+ errors: ['FORBIDDEN'],
116
+ })
117
+ export class SearchAuditEntriesController {
118
+ constructor(private readonly queries: QueryBus) {}
119
+
120
+ async handle({ query: parameters, actor }: HttpRequest<{ query: Parameters }>): Promise<PageBody<AuditEntryView>> {
121
+ const page = await this.queries.execute(
122
+ new SearchAuditEntriesQuery(actor, pageRequestFrom(parameters, pageLimits), filterOf(parameters)),
123
+ );
124
+
125
+ return pageResponse(page, toView);
126
+ }
127
+ }
128
+
129
+ function filterOf(parameters: Parameters): AuditEntriesFilter {
130
+ const outcome = outcomes.find((candidate) => candidate === parameters['outcome']);
131
+
132
+ return {
133
+ ...textOf(parameters, 'actorId'),
134
+ ...textOf(parameters, 'action'),
135
+ ...textOf(parameters, 'correlationId'),
136
+ ...(outcome === undefined ? {} : { outcome }),
137
+ };
138
+ }
139
+
140
+ function textOf<Key extends string>(parameters: Parameters, key: Key): Partial<Record<Key, string>> {
141
+ const value = parameters[key];
142
+ const found: Partial<Record<Key, string>> = {};
143
+
144
+ if (typeof value === 'string' && value !== '') {
145
+ found[key] = value;
146
+ }
147
+
148
+ return found;
149
+ }
150
+
151
+ function toView(source: AuditEntry): AuditEntryView {
152
+ const { actor, resource } = source;
153
+
154
+ return {
155
+ id: source.id,
156
+ chain: source.chain,
157
+ sequence: source.sequence,
158
+ occurredAt: source.occurredAt.toISOString(),
159
+ category: source.category,
160
+ action: source.action,
161
+ outcome: source.outcome,
162
+ ...(source.reason === undefined ? {} : { reason: source.reason }),
163
+ actorId: actor.id,
164
+ actorType: actor.type,
165
+ ...(source.tenantId === undefined ? {} : { tenantId: source.tenantId }),
166
+ ...(resource === undefined ? {} : { resourceType: resource.type, resourceId: resource.id }),
167
+ ...(source.context === undefined ? {} : { context: source.context }),
168
+ correlationId: source.correlationId,
169
+ ...(source.causationId === undefined ? {} : { causationId: source.causationId }),
170
+ origin: source.origin,
171
+ previousHash: source.previousHash,
172
+ hash: source.hash,
173
+ };
174
+ }
175
+
176
+ function propertiesOf(schema: JsonSchema): JsonSchema {
177
+ const { properties } = schema;
178
+
179
+ return typeof properties === 'object' && properties !== null ? { ...properties } : {};
180
+ }
181
+
182
+ function isRecord(input: unknown): input is Parameters {
183
+ return typeof input === 'object' && input !== null && !Array.isArray(input);
184
+ }
185
+
186
+ function isEntryView(input: unknown): input is AuditEntryView {
187
+ return (
188
+ isRecord(input) &&
189
+ typeof input['id'] === 'string' &&
190
+ typeof input['hash'] === 'string' &&
191
+ typeof input['sequence'] === 'number'
192
+ );
193
+ }
@@ -3,6 +3,9 @@ import { installAspectRuntime } from '@ddd-ts-kit/aspects';
3
3
  import { Authenticator } from '@ddd-ts-kit/auth';
4
4
  {{/if}}
5
5
  import {
6
+ {{#if routes.declaredControllers}}
7
+ DeclaredEdges,
8
+ {{/if}}
6
9
  createApplication,
7
10
  kernelModule,
8
11
  type Application as Container,
@@ -13,7 +16,12 @@ import {
13
16
  import { OutboxPublisher } from '@ddd-ts-kit/events';
14
17
  {{/if}}
15
18
  {{#if http.any}}
19
+ {{#if routes.mountsControllers}}
20
+ import { ErrorMapper, controllerRoutes, defineRoutes, type Route } from '@ddd-ts-kit/http';
21
+ {{/if}}
22
+ {{#unless routes.mountsControllers}}
16
23
  import { ErrorMapper, defineRoutes, type Route } from '@ddd-ts-kit/http';
24
+ {{/unless}}
17
25
  {{/if}}
18
26
  import {
19
27
  Cache,
@@ -51,10 +59,16 @@ import { tracedPublisher } from '@ddd-ts-kit/tracing';
51
59
  {{#if audit.on}}
52
60
  import { auditModule } from '{{alias.platform}}/audit/audit.module{{alias.ext}}';
53
61
  {{/if}}
54
- {{#if audit.route}}
62
+ {{#if audit.functionRoute}}
55
63
  import { searchAuditEntriesRoute } from '{{alias.platform}}/audit/search-audit-entries.route{{alias.ext}}';
56
64
  {{/if}}
65
+ {{#if audit.controller}}
66
+ import { SearchAuditEntriesController } from '{{alias.platform}}/audit/search-audit-entries.controller{{alias.ext}}';
67
+ {{/if}}
57
68
  import { authorization } from '{{alias.platform}}/auth/authorization{{alias.ext}}';
69
+ {{#if routes.mountsControllers}}
70
+ import { securitySchemes } from '{{alias.platform}}/auth/security-schemes{{alias.ext}}';
71
+ {{/if}}
58
72
  {{#if http.any}}
59
73
  import { httpBoundary, type HttpBoundary } from '{{alias.platform}}/auth/http-boundary{{alias.ext}}';
60
74
  {{/if}}
@@ -109,8 +123,8 @@ export interface Application {
109
123
  export interface ContextSelection {
110
124
  readonly modules: readonly ContextModule[];
111
125
  {{#if http.any}}
112
- /** The routes of each context, given the two buses they call. */
113
- readonly routes: readonly ((commands: CommandBus, queries: QueryBus) => readonly Route[])[];
126
+ /** The function routes of each context, given the two buses they call; controllers need no list. */
127
+ readonly routes?: readonly ((commands: CommandBus, queries: QueryBus) => readonly Route[])[];
114
128
  {{/if}}
115
129
  }
116
130
 
@@ -262,6 +276,26 @@ function routesOf(container: Container, contexts: ContextSelection): readonly Ro
262
276
  const commands = container.resolve(CommandBus);
263
277
  const queries = container.resolve(QueryBus);
264
278
 
265
- return defineRoutes(...contexts.routes.map((routes) => routes(commands, queries)){{#if audit.route}}, [searchAuditEntriesRoute(queries)]{{/if}});
279
+ {{#unless routes.mountsControllers}}
280
+ return defineRoutes(...(contexts.routes ?? []).map((routes) => routes(commands, queries)){{#if audit.route}}, [searchAuditEntriesRoute(queries)]{{/if}});
281
+ {{/unless}}
282
+ {{#if routes.mountsControllers}}
283
+ // Every route that is not public names these schemes in the contract.
284
+ const security = Object.keys(securitySchemes);
285
+
286
+ return defineRoutes(
287
+ ...(contexts.routes ?? []).map((routes) => routes(commands, queries)),
288
+ {{#if routes.declaredControllers}}
289
+ // The controllers the hosted contexts declare, each built from its constructor.
290
+ controllerRoutes(container.resolve(DeclaredEdges).of('controller'), { resolve: container.resolve, security }),
291
+ {{/if}}
292
+ {{#if audit.functionRoute}}
293
+ [searchAuditEntriesRoute(queries)],
294
+ {{/if}}
295
+ {{#if audit.controller}}
296
+ controllerRoutes([new SearchAuditEntriesController(queries)], { security }),
297
+ {{/if}}
298
+ );
299
+ {{/if}}
266
300
  }
267
301
  {{/if}}
@@ -7,18 +7,26 @@ paths:
7
7
 
8
8
  # Presentation layer
9
9
 
10
+ {{#if routes.controllers}}
11
+ - A route is one class with one method: `<use-case>.controller.ts` in `presentation/http/<resource>/<use-case>/`, carrying `@Controller` of `@ddd-ts-kit/http`. The decorator is the single statement of the route (method, path after the prefix of its group, schemas, response, error codes); `handle` receives what the schemas produce and returns the declared body, and the compiler refuses anything else (`presentation/controller-shape`).
12
+ - A controller decides nothing: it is given the bus of its method (`CommandBus` for POST, PUT, PATCH, DELETE; `QueryBus` for GET), a request mapper and a serializer, nothing else (`presentation/controller-delegates`). `<use-case>.mapper.ts` turns the request and its actor into the command or the query; `<use-case>.serializer.ts` turns the result into the body. Both are optional.
13
+ - A route group (`<resource>.routes.ts`, `@RouteGroup`) gives its controllers a prefix, a tag and guards. A controller names its group, or says `guards: [Public]`: nothing is open by omission (`authz/declared`).
14
+ - A guard is a control at the edge (authenticated, a role, the tenant of the path); a permission stays on the use case (`@Requires`), where a job and an event handler meet it too. A guard of the project is a declared class in `presentation/http/guards/*.guard.ts` and never imports `domain/`, `infrastructure/`, a repository or the command bus (`presentation/guard-at-the-edge`).
15
+ {{/if}}
16
+ {{#if routes.functions}}
10
17
  - A route validates the shape of the input, builds a command or a query with the actor, executes it through `CommandBus` or `QueryBus` (never a handler class) and maps the answer. Nothing else.
18
+ {{/if}}
11
19
  - Imports `application/` (and domain types for mapping), never `infrastructure/` (`layer/presentation-no-infrastructure`).
12
20
  - Input shape is validated here with the schema library of the project, behind the `Schema` port. Business rules are not validated here.
13
21
  {{#if refusals.factories}}
14
- - Every route declares its `security`, or `security: false`, and the codes of the refusals it can answer in `errors`, as their factories set them (`errors/known-code`); the mapping from code to status lives in one place, the error mapper. A client reads the `code`, and under `facts` only what a factory exposed.
22
+ - Every route says who may call it (its group or its guards for a controller, `security` for a function) and the codes of the refusals it can answer in `errors`, as their factories set them (`errors/known-code`); the mapping from code to status lives in one place, the error mapper. A client reads the `code`, under `facts` only what a factory exposed, and the message of the refusal as `detail` unless it quotes a fact that is not exposed.
15
23
  {{/if}}
16
24
  {{#if refusals.classes}}
17
- - Every route declares its `security`, or `security: false`, and the `DomainError` codes it can answer in `errors`; the mapping from code to status lives in one place, the error mapper.
25
+ - Every route says who may call it (its group or its guards for a controller, `security` for a function) and the `DomainError` codes it can answer in `errors`; the mapping from code to status lives in one place, the error mapper.
18
26
  {{/if}}
19
- - No role or ownership test here (`authz/in-application`): the bus asks for the permission, a policy decides on loaded state.
20
- - Request and response types are presentation types (`*.dto.ts`): never an aggregate, never a row.
27
+ - No role or ownership test in a controller or a route (`authz/in-application`): a guard of the group asks for a role at the edge, the bus asks for the permission, a policy decides on loaded state.
28
+ - Request and response types are presentation types, one class or schema per file next to the controller (`*.body.ts`, `*.params.ts`, `*.answer.ts`...; `*.dto.ts` for function routes and under `shared/`): never an aggregate, never a row.
21
29
  - Never put the message of an unexpected failure in a response or a log.
22
- - Files: `*.route.ts` (or `*.controller.ts`) and `*.dto.ts` in `presentation/http/` (`naming/suffix`).
30
+ - Files: the canonical tree of `presentation/http/` (`naming/suffix`, `tree/closed`): a folder per controller, `*.routes.ts` for the groups, `guards/`, `shared/`; function routes stay flat (`*.route.ts`, `*.dto.ts`).
23
31
  - After a change, `npm run docs:generate` regenerates `openapi/openapi.json`, committed with the route; CI fails on a difference. Never edit it by hand.
24
- - Contract with `/ddd-ts-kit:document`. Create with `npx ddd-ts-kit generate route <context> <UseCase> --method <M> --path <p> --json`, then `generate e2e <context> <UseCase>`.
32
+ - Contract with `/ddd-ts-kit:document`. Create with `npx ddd-ts-kit generate route <context> <UseCase> --method <M> --path <p> [--group <Name>] [--mapper] [--serializer] --json`, then `generate e2e <context> <UseCase>`; `generate route-group` and `generate guard` write a group and a guard.
@@ -23,7 +23,7 @@
23
23
  "@biomejs/biome": "2.5.15",
24
24
  "@commitlint/cli": "^21.2.3",
25
25
  "@commitlint/config-conventional": "^21.2.3",
26
- "@ddd-ts-kit/eslint-plugin": "^0.3.0",
26
+ "@ddd-ts-kit/eslint-plugin": "^0.4.0",
27
27
  "@eslint/js": "^10.0.1",
28
28
  "@typescript/native": "npm:typescript@~7.0.2",
29
29
  "@vitest/coverage-v8": "5.0.3",