@ddd-ts-kit/http 0.89.0 → 0.91.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 (202) hide show
  1. package/README.fr.md +222 -2
  2. package/README.md +217 -1
  3. package/dist/error/refusal-codes-by-status.d.ts +11 -0
  4. package/dist/error/refusal-codes-by-status.d.ts.map +1 -0
  5. package/dist/error/refusal-codes-by-status.js +48 -0
  6. package/dist/error/refusal-codes-by-status.js.map +1 -0
  7. package/dist/nest/controllers/arguments-of.d.ts +7 -0
  8. package/dist/nest/controllers/arguments-of.d.ts.map +1 -0
  9. package/dist/nest/controllers/arguments-of.js +21 -0
  10. package/dist/nest/controllers/arguments-of.js.map +1 -0
  11. package/dist/nest/controllers/assert-not-routed-by-nest.d.ts +8 -0
  12. package/dist/nest/controllers/assert-not-routed-by-nest.d.ts.map +1 -0
  13. package/dist/nest/controllers/assert-not-routed-by-nest.js +19 -0
  14. package/dist/nest/controllers/assert-not-routed-by-nest.js.map +1 -0
  15. package/dist/nest/controllers/controllers-binder.d.ts +6 -0
  16. package/dist/nest/controllers/controllers-binder.d.ts.map +1 -0
  17. package/dist/nest/controllers/controllers-binder.js +5 -0
  18. package/dist/nest/controllers/controllers-binder.js.map +1 -0
  19. package/dist/nest/controllers/controllers-module-options.d.ts +8 -0
  20. package/dist/nest/controllers/controllers-module-options.d.ts.map +1 -0
  21. package/dist/nest/controllers/controllers-module-options.js +2 -0
  22. package/dist/nest/controllers/controllers-module-options.js.map +1 -0
  23. package/dist/nest/controllers/create-controllers-module.d.ts +10 -0
  24. package/dist/nest/controllers/create-controllers-module.d.ts.map +1 -0
  25. package/dist/nest/controllers/create-controllers-module.js +28 -0
  26. package/dist/nest/controllers/create-controllers-module.js.map +1 -0
  27. package/dist/nest/controllers/current-actor.d.ts +6 -0
  28. package/dist/nest/controllers/current-actor.d.ts.map +1 -0
  29. package/dist/nest/controllers/current-actor.js +9 -0
  30. package/dist/nest/controllers/current-actor.js.map +1 -0
  31. package/dist/nest/controllers/dto-type.d.ts +3 -0
  32. package/dist/nest/controllers/dto-type.d.ts.map +1 -0
  33. package/dist/nest/controllers/dto-type.js +2 -0
  34. package/dist/nest/controllers/dto-type.js.map +1 -0
  35. package/dist/nest/controllers/injected-parameter.d.ts +4 -0
  36. package/dist/nest/controllers/injected-parameter.d.ts.map +1 -0
  37. package/dist/nest/controllers/injected-parameter.js +17 -0
  38. package/dist/nest/controllers/injected-parameter.js.map +1 -0
  39. package/dist/nest/controllers/is-nest-controller.d.ts +4 -0
  40. package/dist/nest/controllers/is-nest-controller.d.ts.map +1 -0
  41. package/dist/nest/controllers/is-nest-controller.js +7 -0
  42. package/dist/nest/controllers/is-nest-controller.js.map +1 -0
  43. package/dist/nest/controllers/keyed-schema.d.ts +8 -0
  44. package/dist/nest/controllers/keyed-schema.d.ts.map +1 -0
  45. package/dist/nest/controllers/keyed-schema.js +33 -0
  46. package/dist/nest/controllers/keyed-schema.js.map +1 -0
  47. package/dist/nest/controllers/kit-route-spec.d.ts +47 -0
  48. package/dist/nest/controllers/kit-route-spec.d.ts.map +1 -0
  49. package/dist/nest/controllers/kit-route-spec.js +2 -0
  50. package/dist/nest/controllers/kit-route-spec.js.map +1 -0
  51. package/dist/nest/controllers/kit-route.d.ts +18 -0
  52. package/dist/nest/controllers/kit-route.d.ts.map +1 -0
  53. package/dist/nest/controllers/kit-route.js +29 -0
  54. package/dist/nest/controllers/kit-route.js.map +1 -0
  55. package/dist/nest/controllers/kit-routes-options.d.ts +15 -0
  56. package/dist/nest/controllers/kit-routes-options.d.ts.map +1 -0
  57. package/dist/nest/controllers/kit-routes-options.js +2 -0
  58. package/dist/nest/controllers/kit-routes-options.js.map +1 -0
  59. package/dist/nest/controllers/kit-routes.d.ts +5 -0
  60. package/dist/nest/controllers/kit-routes.d.ts.map +1 -0
  61. package/dist/nest/controllers/kit-routes.js +8 -0
  62. package/dist/nest/controllers/kit-routes.js.map +1 -0
  63. package/dist/nest/controllers/nest-controller-class.d.ts +3 -0
  64. package/dist/nest/controllers/nest-controller-class.d.ts.map +1 -0
  65. package/dist/nest/controllers/nest-controller-class.js +2 -0
  66. package/dist/nest/controllers/nest-controller-class.js.map +1 -0
  67. package/dist/nest/controllers/nest-controller-route.d.ts +6 -0
  68. package/dist/nest/controllers/nest-controller-route.d.ts.map +1 -0
  69. package/dist/nest/controllers/nest-controller-route.js +12 -0
  70. package/dist/nest/controllers/nest-controller-route.js.map +1 -0
  71. package/dist/nest/controllers/nest-controller-routes-options.d.ts +14 -0
  72. package/dist/nest/controllers/nest-controller-routes-options.d.ts.map +1 -0
  73. package/dist/nest/controllers/nest-controller-routes-options.js +2 -0
  74. package/dist/nest/controllers/nest-controller-routes-options.js.map +1 -0
  75. package/dist/nest/controllers/nest-controller-routes.d.ts +12 -0
  76. package/dist/nest/controllers/nest-controller-routes.d.ts.map +1 -0
  77. package/dist/nest/controllers/nest-controller-routes.js +142 -0
  78. package/dist/nest/controllers/nest-controller-routes.js.map +1 -0
  79. package/dist/nest/controllers/nest-controllers-options.d.ts +7 -0
  80. package/dist/nest/controllers/nest-controllers-options.d.ts.map +1 -0
  81. package/dist/nest/controllers/nest-controllers-options.js +2 -0
  82. package/dist/nest/controllers/nest-controllers-options.js.map +1 -0
  83. package/dist/nest/controllers/nest-controllers.d.ts +21 -0
  84. package/dist/nest/controllers/nest-controllers.d.ts.map +1 -0
  85. package/dist/nest/controllers/nest-controllers.js +31 -0
  86. package/dist/nest/controllers/nest-controllers.js.map +1 -0
  87. package/dist/nest/controllers/nest-method.d.ts +17 -0
  88. package/dist/nest/controllers/nest-method.d.ts.map +1 -0
  89. package/dist/nest/controllers/nest-method.js +2 -0
  90. package/dist/nest/controllers/nest-method.js.map +1 -0
  91. package/dist/nest/controllers/nest-parameter.d.ts +10 -0
  92. package/dist/nest/controllers/nest-parameter.d.ts.map +1 -0
  93. package/dist/nest/controllers/nest-parameter.js +2 -0
  94. package/dist/nest/controllers/nest-parameter.js.map +1 -0
  95. package/dist/nest/controllers/nest-route-metadata.d.ts +22 -0
  96. package/dist/nest/controllers/nest-route-metadata.d.ts.map +1 -0
  97. package/dist/nest/controllers/nest-route-metadata.js +121 -0
  98. package/dist/nest/controllers/nest-route-metadata.js.map +1 -0
  99. package/dist/nest/controllers/nest-route-methods.d.ts +4 -0
  100. package/dist/nest/controllers/nest-route-methods.d.ts.map +1 -0
  101. package/dist/nest/controllers/nest-route-methods.js +12 -0
  102. package/dist/nest/controllers/nest-route-methods.js.map +1 -0
  103. package/dist/nest/controllers/nest-routes-registry.d.ts +18 -0
  104. package/dist/nest/controllers/nest-routes-registry.d.ts.map +1 -0
  105. package/dist/nest/controllers/nest-routes-registry.js +23 -0
  106. package/dist/nest/controllers/nest-routes-registry.js.map +1 -0
  107. package/dist/nest/controllers/prototype-of.d.ts +4 -0
  108. package/dist/nest/controllers/prototype-of.d.ts.map +1 -0
  109. package/dist/nest/controllers/prototype-of.js +10 -0
  110. package/dist/nest/controllers/prototype-of.js.map +1 -0
  111. package/dist/nest/controllers/provide-controllers.d.ts +12 -0
  112. package/dist/nest/controllers/provide-controllers.d.ts.map +1 -0
  113. package/dist/nest/controllers/provide-controllers.js +33 -0
  114. package/dist/nest/controllers/provide-controllers.js.map +1 -0
  115. package/dist/nest/controllers/reflected-metadata.d.ts +3 -0
  116. package/dist/nest/controllers/reflected-metadata.d.ts.map +1 -0
  117. package/dist/nest/controllers/reflected-metadata.js +11 -0
  118. package/dist/nest/controllers/reflected-metadata.js.map +1 -0
  119. package/dist/nest/controllers/request-schemas-of.d.ts +13 -0
  120. package/dist/nest/controllers/request-schemas-of.d.ts.map +1 -0
  121. package/dist/nest/controllers/request-schemas-of.js +49 -0
  122. package/dist/nest/controllers/request-schemas-of.js.map +1 -0
  123. package/dist/nest/controllers/route-context.d.ts +3 -0
  124. package/dist/nest/controllers/route-context.d.ts.map +1 -0
  125. package/dist/nest/controllers/route-context.js +6 -0
  126. package/dist/nest/controllers/route-context.js.map +1 -0
  127. package/dist/nest/controllers/schema-of.d.ts +6 -0
  128. package/dist/nest/controllers/schema-of.d.ts.map +1 -0
  129. package/dist/nest/controllers/schema-of.js +12 -0
  130. package/dist/nest/controllers/schema-of.js.map +1 -0
  131. package/dist/nest/controllers/summary-of.d.ts +3 -0
  132. package/dist/nest/controllers/summary-of.d.ts.map +1 -0
  133. package/dist/nest/controllers/summary-of.js +6 -0
  134. package/dist/nest/controllers/summary-of.js.map +1 -0
  135. package/dist/nest/controllers/with-answers.d.ts +10 -0
  136. package/dist/nest/controllers/with-answers.d.ts.map +1 -0
  137. package/dist/nest/controllers/with-answers.js +11 -0
  138. package/dist/nest/controllers/with-answers.js.map +1 -0
  139. package/dist/nest.d.ts +18 -0
  140. package/dist/nest.d.ts.map +1 -1
  141. package/dist/nest.js +11 -0
  142. package/dist/nest.js.map +1 -1
  143. package/dist/openapi/responses.d.ts.map +1 -1
  144. package/dist/openapi/responses.js +2 -41
  145. package/dist/openapi/responses.js.map +1 -1
  146. package/dist/orpc/orpc-protocol.d.ts +7 -0
  147. package/dist/orpc/orpc-protocol.d.ts.map +1 -0
  148. package/dist/orpc/orpc-protocol.js +13 -0
  149. package/dist/orpc/orpc-protocol.js.map +1 -0
  150. package/dist/orpc/orpc-router.d.ts +22 -0
  151. package/dist/orpc/orpc-router.d.ts.map +1 -0
  152. package/dist/orpc/orpc-router.js +43 -0
  153. package/dist/orpc/orpc-router.js.map +1 -0
  154. package/dist/orpc.d.ts +4 -0
  155. package/dist/orpc.d.ts.map +1 -0
  156. package/dist/orpc.js +2 -0
  157. package/dist/orpc.js.map +1 -0
  158. package/dist/pipeline/primed.d.ts +2 -1
  159. package/dist/pipeline/primed.d.ts.map +1 -1
  160. package/dist/pipeline/primed.js +14 -11
  161. package/dist/pipeline/primed.js.map +1 -1
  162. package/dist/rpc/rpc-answer.d.ts +18 -0
  163. package/dist/rpc/rpc-answer.d.ts.map +1 -0
  164. package/dist/rpc/rpc-answer.js +61 -0
  165. package/dist/rpc/rpc-answer.js.map +1 -0
  166. package/dist/rpc/rpc-context.d.ts +15 -0
  167. package/dist/rpc/rpc-context.d.ts.map +1 -0
  168. package/dist/rpc/rpc-context.js +2 -0
  169. package/dist/rpc/rpc-context.js.map +1 -0
  170. package/dist/rpc/rpc-input.d.ts +11 -0
  171. package/dist/rpc/rpc-input.d.ts.map +1 -0
  172. package/dist/rpc/rpc-input.js +2 -0
  173. package/dist/rpc/rpc-input.js.map +1 -0
  174. package/dist/rpc/rpc-refusal.d.ts +14 -0
  175. package/dist/rpc/rpc-refusal.d.ts.map +1 -0
  176. package/dist/rpc/rpc-refusal.js +2 -0
  177. package/dist/rpc/rpc-refusal.js.map +1 -0
  178. package/dist/rpc/rpc-request.d.ts +9 -0
  179. package/dist/rpc/rpc-request.d.ts.map +1 -0
  180. package/dist/rpc/rpc-request.js +28 -0
  181. package/dist/rpc/rpc-request.js.map +1 -0
  182. package/dist/trpc/route-refusal.d.ts +8 -0
  183. package/dist/trpc/route-refusal.d.ts.map +1 -0
  184. package/dist/trpc/route-refusal.js +10 -0
  185. package/dist/trpc/route-refusal.js.map +1 -0
  186. package/dist/trpc/trpc-error-shape.d.ts +17 -0
  187. package/dist/trpc/trpc-error-shape.d.ts.map +1 -0
  188. package/dist/trpc/trpc-error-shape.js +2 -0
  189. package/dist/trpc/trpc-error-shape.js.map +1 -0
  190. package/dist/trpc/trpc-protocol.d.ts +8 -0
  191. package/dist/trpc/trpc-protocol.d.ts.map +1 -0
  192. package/dist/trpc/trpc-protocol.js +43 -0
  193. package/dist/trpc/trpc-protocol.js.map +1 -0
  194. package/dist/trpc/trpc-router.d.ts +38 -0
  195. package/dist/trpc/trpc-router.d.ts.map +1 -0
  196. package/dist/trpc/trpc-router.js +44 -0
  197. package/dist/trpc/trpc-router.js.map +1 -0
  198. package/dist/trpc.d.ts +5 -0
  199. package/dist/trpc.d.ts.map +1 -0
  200. package/dist/trpc.js +2 -0
  201. package/dist/trpc.js.map +1 -0
  202. package/package.json +35 -12
package/README.fr.md CHANGED
@@ -1,10 +1,11 @@
1
1
  [English](README.md) · **Français**
2
- <!-- translation-of: README.md sha256:f1ef3d45593ee08e -->
2
+ <!-- translation-of: README.md sha256:b73771513830b6fc -->
3
3
 
4
4
  # @ddd-ts-kit/http
5
5
 
6
6
  Déclarez une route une seule fois, sans framework. Montez-la sur Express 5, Fastify 5, Hono 4
7
- (sur Node) ou NestJS, et générez le contrat OpenAPI 3.1 à partir de la même déclaration.
7
+ (sur Node) ou NestJS, servez-la aussi comme procédure oRPC ou tRPC, et générez le contrat OpenAPI 3.1 à
8
+ partir de la même déclaration.
8
9
 
9
10
  Chaque adaptateur passe par un seul pipeline (`executeRoute`) : validation, gestionnaire
10
11
  (handler), correspondance des erreurs. Le comportement est donc le même sur les quatre
@@ -1300,6 +1301,220 @@ contournent les pipes, les guards, les intercepteurs, les filtres d'exceptions e
1300
1301
  global de Nest : la validation et la correspondance des erreurs sont celles décrites plus haut.
1301
1302
  Utilisez `prefixRoutes` pour un préfixe.
1302
1303
 
1304
+ ### Contrôleurs Nest servis par le kit
1305
+
1306
+ Un projet qui écrit des contrôleurs Nest les garde : `@Controller`, `@Get`, `@Post` et les autres
1307
+ méthodes, `@Param`, `@Query`, `@Headers`, `@Body` avec une classe DTO, `@HttpCode`, l'injection par
1308
+ constructeur et les modules. Chaque méthode qui porte un décorateur de route de Nest est une route
1309
+ du kit, nommée d'après la méthode (`issueInvoice`, résumé `Issue invoice` ; renommer la méthode
1310
+ renomme la route, sa procédure et la fonction de son client), derrière les *guards* de `@KitRoutes`
1311
+ sur la classe. `@KitRoute` sur une méthode, facultatif, ajoute ce que Nest ne dit pas :
1312
+ un autre nom, ses propres *guards*, les refus qu'elle répond, sa réponse. C'est le kit, et non le
1313
+ routeur de Nest, qui sert la méthode : c'est une route du kit, avec ses *guards*, sa validation, ses
1314
+ documents *problem*, son traçage et son contrat, et une procédure sur `/rpc` et `/trpc` comme toute
1315
+ route.
1316
+
1317
+ <!-- checked sample: legacy-decorators -->
1318
+ ```ts
1319
+ import { Body, Controller, Inject, Module, Param, Post } from '@nestjs/common';
1320
+ import { IsString, MinLength } from 'class-validator';
1321
+ import type { Actor } from '@ddd-ts-kit/kernel';
1322
+ import { Authenticated, type ExecuteOptions } from '@ddd-ts-kit/http';
1323
+ import { createControllersModule, CurrentActor, KitRoute, KitRoutes } from '@ddd-ts-kit/http/nest';
1324
+ import { classSchema } from '@ddd-ts-kit/validation/class-validator';
1325
+
1326
+ declare const options: ExecuteOptions;
1327
+
1328
+ /** What the controller sends its use case through: here a port of the application. */
1329
+ abstract class Invoicing {
1330
+ abstract issue(invoiceId: string, customerId: string, actor: Actor): Promise<void>;
1331
+ }
1332
+
1333
+ declare const invoicing: Invoicing;
1334
+
1335
+ class IssueInvoiceBody {
1336
+ @IsString()
1337
+ @MinLength(1)
1338
+ customerId = '';
1339
+ }
1340
+
1341
+ @Controller('invoices')
1342
+ @KitRoutes({ guards: [Authenticated], tags: ['Invoices'] })
1343
+ export class InvoicesController {
1344
+ constructor(@Inject(Invoicing) private readonly invoicing: Invoicing) {}
1345
+
1346
+ @Post(':invoiceId/issue')
1347
+ @KitRoute({ errors: ['INVOICE_ALREADY_ISSUED'] }) // named issueInvoice, after the method
1348
+ async issue(
1349
+ @Param('invoiceId') invoiceId: string,
1350
+ @Body() body: IssueInvoiceBody,
1351
+ @CurrentActor() actor: Actor,
1352
+ ): Promise<void> {
1353
+ await this.invoicing.issue(invoiceId, body.customerId, actor);
1354
+ }
1355
+ }
1356
+
1357
+ @Module({
1358
+ imports: [
1359
+ createControllersModule([InvoicesController], {
1360
+ ...options,
1361
+ providers: [{ provide: Invoicing, useValue: invoicing }],
1362
+ schemaOfClass: classSchema,
1363
+ }),
1364
+ ],
1365
+ })
1366
+ export class AppModule {}
1367
+ ```
1368
+
1369
+ - `createControllersModule(controllers, options)` enregistre les contrôleurs comme *providers*,
1370
+ pour que Nest les construise avec ce qu'ils injectent, et monte chaque méthode portant
1371
+ `@KitRoute` sur le serveur HTTP sous Nest. Un contrôleur listé aussi dans les `controllers` d'un
1372
+ module Nest est refusé au démarrage : Nest le servirait une seconde fois, par son propre pipeline.
1373
+ - `@KitRoutes({ guards, tags, errors })` sur la classe est ce que toutes ses routes partagent, comme
1374
+ un groupe de routes ; les `guards` de `@KitRoute` passent après. Une route sans *guard* ni d'un
1375
+ côté ni de l'autre est refusée : `[Public]` dit qu'elle est ouverte.
1376
+ - `@CurrentActor()` donne à la méthode l'appelant que l'authentification a résolu
1377
+ (`resolvePrincipal`), jamais un appelant construit par le contrôleur ; `@RouteContext()` le
1378
+ contexte de la requête (`expectedVersion` sous `ifMatch`).
1379
+ - Un paramètre qui prend une partie entière est validé par sa classe, lue par `schemaOfClass`
1380
+ (`classSchema` de class-validator ; le projet compile avec `emitDecoratorMetadata`, comme Nest le
1381
+ demande) ; une partie nommée une à une (`@Param('invoiceId')`) est un texte ; `request` de
1382
+ `@KitRoute` donne à la place un schéma de n'importe quelle bibliothèque. `response.body` est un
1383
+ schéma ou une classe DTO. Une méthode ne répond un corps que si `response.body` le déclare, comme
1384
+ le dit le contrat : une méthode qui rend une valeur sans lui est refusée, à la construction des
1385
+ routes si TypeScript a enregistré son type de retour, sinon à l'appel (une 500, signalée à
1386
+ `onError`), jamais répondue vide.
1387
+ - `nestControllerRoutes(controllers, { schemaOfClass })` donne les mêmes routes sans le conteneur
1388
+ de Nest, pour `generateOpenApi`, `orpcRouter` et `trpcRouter` ; `nestControllerRoute` l'unique
1389
+ route d'un contrôleur, pour sa spec.
1390
+ - Une application qui compose ses routes avant le démarrage de Nest prend
1391
+ `nestControllers(controllers, { providers, schemaOfClass })` : ses `routes` rejoignent les autres
1392
+ routes de l'application, et `provideControllers(served)` dans le module Nest construit les
1393
+ contrôleurs avec ce qu'ils injectent (les bus, comme `{ provide: CommandBus, useValue: commands }`)
1394
+ et donne aux routes leurs instances au démarrage. `ddd-ts-kit init --routes nest` écrit cette
1395
+ composition.
1396
+ - `@KitRoutes({ group })` nomme un groupe de routes du kit (`@RouteGroup`) : son préfixe précède les
1397
+ chemins de Nest, ses tags, *guards* et codes précèdent ceux de la classe.
1398
+ - Ce qu'une méthode répond, c'est sa signature : TypeScript efface `Promise<IssuedInvoice>`, donc
1399
+ `withAnswers(controller, { issueInvoice: IssuedInvoice })` le dit au pont, une classe DTO, un schéma
1400
+ ou `null` pour aucun corps ; `response` de `@KitRoute` l'emporte toujours. Sous `--routes nest`,
1401
+ `ddd-ts-kit` lit les signatures et écrit ces appels dans le fichier généré `<context>.controllers.ts`,
1402
+ que le module du contexte liste.
1403
+ - Refusés quand les routes sont construites, parce que le pipeline du kit ne les exécuterait pas :
1404
+ `@UseGuards`, `@UsePipes`, `@UseInterceptors`, `@UseFilters`, `@Header`, `@Redirect`, `@Render`,
1405
+ `@Sse`, `@Version`, un *pipe* sur un paramètre, `@Req`, `@Res` et les autres paramètres de Nest. Le
1406
+ kit lit ce qu'enregistrent les décorateurs de Nest (`@nestjs/common/constants`) ; la forme en est
1407
+ vérifiée à chaque lecture, et un test du kit la tient sur la version de Nest avec laquelle il est
1408
+ publié : une version de Nest qui la changerait est refusée au démarrage plutôt que lue de travers.
1409
+
1410
+ ## Procédures oRPC
1411
+
1412
+ Une seconde porte vers les mêmes routes, pour un client TypeScript qui appelle des procédures
1413
+ plutôt que des chemins. `orpcRouter` fait de chaque route une procédure oRPC nommée par son
1414
+ `operationId`, que `RPCHandler` de `@orpc/server` (1.15 ou plus récent, pair optionnelle) sert
1415
+ sous un préfixe, à côté des routes elles-mêmes. Chaque appel passe par `executeRoute` avec les
1416
+ options données à l'adaptateur HTTP : les gardes, le principal, le tenant, la validation, les
1417
+ permissions du cas d'usage, `onError` et `around` voient un appel sur `/rpc` comme ils voient une
1418
+ requête.
1419
+
1420
+ <!-- checked sample -->
1421
+ ```ts
1422
+ import express from 'express';
1423
+ import type { ExecuteOptions, Route } from '@ddd-ts-kit/http';
1424
+ import { mountExpress } from '@ddd-ts-kit/http/express';
1425
+ import { orpcRouter } from '@ddd-ts-kit/http/orpc';
1426
+ import { RPCHandler } from '@orpc/server/node';
1427
+
1428
+ declare const routes: readonly Route[];
1429
+ declare const options: ExecuteOptions;
1430
+
1431
+ const app = express();
1432
+ const rpc = new RPCHandler(orpcRouter(routes, options));
1433
+
1434
+ app.use(express.json()); // oRPC lit le corps qu'Express a déjà analysé
1435
+ app.use('/rpc', (request, response, next) => {
1436
+ const context = { headers: request.headers, raw: request };
1437
+
1438
+ void rpc.handle(request, response, { prefix: '/rpc', context }).then(({ matched }) => {
1439
+ if (!matched) {
1440
+ next();
1441
+ }
1442
+ }, next);
1443
+ });
1444
+ mountExpress(app, routes, options);
1445
+ ```
1446
+
1447
+ Le `context` d'un appel est un `OrpcContext` : les en-têtes de la requête RPC, qui portent les
1448
+ identifiants, les identifiants de requête et de corrélation et le tenant, et `raw`, ce que lit
1449
+ `resolvePrincipal` (`{ headers }` quand il est omis, ce que lit `principalResolver` de
1450
+ `@ddd-ts-kit/auth`). Fastify (`@orpc/server/fastify`), Hono (`@orpc/server/fetch`) et NestJS sur
1451
+ Express (`@orpc/server/node`) le montent de la même façon ; `ddd-ts-kit init --rpc orpc` écrit le
1452
+ montage pour le framework du projet, et `ddd-ts-kit sdk` donne à chaque client le contrat oRPC de
1453
+ son document, sous `<client>/orpc`.
1454
+
1455
+ | | Sur `/rpc` |
1456
+ |---|---|
1457
+ | Entrée | `{ path, query, header, body }`, l'entrée de l'opération dans le client généré ; `header` passe par-dessus les en-têtes de la requête RPC |
1458
+ | Réponse | le corps du succès, tel que JSON l'écrit (une date est son texte, comme le dit le contrat) ; un corps binaire est un `Blob` |
1459
+ | Refus | une `ORPCError` : le `code` et le `status` du *problem*, son `detail` (ou son `title`) comme `message`, le document *problem* comme `data`. C'est une erreur définie quand la route déclare son code (ses codes et ceux de ses gardes, `VALIDATION_FAILED`, les préconditions), avec le statut que lui donne le correspondant d'erreurs de l'application. Un refus que le gestionnaire répond lui-même, sans document *problem*, est `HTTP_<statut>` avec son corps comme `data` |
1460
+ | `If-Match` | envoyé dans `header`, vérifié comme sur la route |
1461
+ | En-têtes de la réponse | non rendus : ni `ETag`, ni politique de cache, ni `Location`. Un cache partagé ne garde rien de `/rpc` et n'a rien à purger |
1462
+ | Représentations | aucune : JSON seulement, donc ni 406 ni 415 pour un type de média |
1463
+ | Téléversement, corps brut | pas une procédure : seule la route HTTP reçoit un fichier |
1464
+ | Fichier, liste lue au fil de l'eau | refusés en 406 (`NOT_ACCEPTABLE`, son *problem* comme `data`) et fermés, sans rien lire de plus : ils sont envoyés en flux parce qu'ils peuvent ne pas tenir en mémoire |
1465
+
1466
+ Deux routes d'un même `operationId` sont refusées, comme `mountExpress` les refuse : une
1467
+ procédure cacherait l'autre.
1468
+
1469
+ ## Procédures tRPC
1470
+
1471
+ La même porte pour un client tRPC. `trpcRouter` fait de chaque route une procédure tRPC nommée
1472
+ par son `operationId`, une *query* pour une route `GET` et une *mutation* pour les autres
1473
+ méthodes, que les adaptateurs de `@trpc/server` (11.10 ou plus récent, pair optionnelle) servent
1474
+ sous un point d'entrée comme `/trpc`. Chaque appel passe par `executeRoute` avec les options de
1475
+ l'adaptateur HTTP, comme un appel d'`orpcRouter`.
1476
+
1477
+ <!-- checked sample -->
1478
+ ```ts
1479
+ import express from 'express';
1480
+ import type { ExecuteOptions, Route } from '@ddd-ts-kit/http';
1481
+ import { mountExpress } from '@ddd-ts-kit/http/express';
1482
+ import { trpcRouter } from '@ddd-ts-kit/http/trpc';
1483
+ import { createExpressMiddleware } from '@trpc/server/adapters/express';
1484
+
1485
+ declare const routes: readonly Route[];
1486
+ declare const options: ExecuteOptions;
1487
+
1488
+ const app = express();
1489
+
1490
+ app.use(express.json()); // tRPC lit le corps qu'Express a déjà analysé
1491
+ app.use(
1492
+ '/trpc',
1493
+ createExpressMiddleware({
1494
+ router: trpcRouter(routes, options),
1495
+ createContext: ({ req }) => ({ headers: req.headers, raw: req }),
1496
+ }),
1497
+ );
1498
+ mountExpress(app, routes, options);
1499
+ ```
1500
+
1501
+ Le contexte d'un appel est un `TrpcContext`, le même qu'`OrpcContext`. Fastify
1502
+ (`@trpc/server/adapters/fastify`), Hono (`@trpc/server/adapters/fetch`) et NestJS sur Express
1503
+ (`@trpc/server/adapters/standalone`) le montent de la même façon ; `ddd-ts-kit init --rpc trpc`
1504
+ écrit le montage pour le framework du projet, et `ddd-ts-kit sdk` donne à chaque client le type du
1505
+ routeur, sous `<client>/trpc`. Montez-le comme un routeur à part, sans le fusionner dans un autre :
1506
+ tRPC met en forme une erreur avec le formateur du routeur qu'il sert, et c'est celui de
1507
+ `trpcRouter` qui place le document *problem* dans `data.problem`.
1508
+
1509
+ | | Sur `/trpc` |
1510
+ |---|---|
1511
+ | Procédure | une *query* pour une route `GET` (`client.getOrder.query(input)`), une *mutation* pour les autres méthodes (`client.placeOrder.mutate(input)`) |
1512
+ | Entrée | `{ path, query, header, body }`, comme sur `/rpc` |
1513
+ | Réponse | le corps du succès, tel que JSON l'écrit |
1514
+ | Refus | une `TRPCError` du code que tRPC a pour le statut (`UNAUTHORIZED` pour 401, `NOT_FOUND` pour 404, `CONFLICT` pour 409, `UNPROCESSABLE_CONTENT` pour 422 ; `BAD_REQUEST` ou `INTERNAL_SERVER_ERROR` pour un statut sans code), le `detail` (ou le `title`) du *problem* comme `message`, et le document *problem* comme `data.problem`. tRPC a une liste fixe de codes : c'est le `code` du *problem*, celui du kit, qui permet à un client de distinguer les refus. `data.problem` vaut `null` pour un refus que le gestionnaire répond lui-même, sans document *problem* (son message est `HTTP_<statut>`), et pour une erreur de tRPC lui-même, comme une procédure inconnue. La pile d'appels que tRPC ajoute en développement est retirée |
1515
+ | `If-Match`, en-têtes de la réponse, représentations, téléversements et corps bruts | comme sur `/rpc` |
1516
+ | Fichier, liste lue au fil de l'eau, corps binaire | refusés en 406 (`NOT_ACCEPTABLE` dans `data.problem`) et fermés : tRPC ne répond qu'en JSON |
1517
+
1303
1518
  ## OpenAPI 3.1
1304
1519
 
1305
1520
  <!-- checked sample -->
@@ -1565,6 +1780,11 @@ schéma de corps n'est jamais refusée par un 406.
1565
1780
  est coupée, le client voit un corps tronqué, et `onError` est prévenu. Les éléments d'un flux
1566
1781
  ne sont pas vérifiés un par un par `validateResponses`.
1567
1782
  - CSV est écrit, jamais lu, et ne contient que des lignes plates.
1783
+ - Une procédure oRPC ou tRPC ne répond que son corps : ce qu'une route dit dans ses en-têtes (un
1784
+ `ETag`, une politique de cache, un `Location`) reste à la route HTTP, comme les téléversements,
1785
+ les corps bruts, les fichiers, les listes lues au fil de l'eau et les représentations autres que
1786
+ JSON (voir « Procédures oRPC ») ; sur tRPC, les corps binaires aussi, et un refus porte le code
1787
+ que tRPC a pour son statut, celui du kit étant dans `data.problem`.
1568
1788
  - Un formulaire décrit des scalaires, des objets imbriqués et des listes d'un vocabulaire fermé ;
1569
1789
  une liste d'objets est un champ `list` sans détail. La mise en page au-delà des groupes et de
1570
1790
  l'ordre revient au front-end. Une consultation nomme la route des options ; la façon dont le
package/README.md CHANGED
@@ -3,7 +3,8 @@
3
3
  # @ddd-ts-kit/http
4
4
 
5
5
  Declare a route once, without a framework. Mount it on Express 5, Fastify 5, Hono 4 (on Node)
6
- or NestJS, and generate the OpenAPI 3.1 contract from the same declaration.
6
+ or NestJS, serve it as an oRPC or tRPC procedure too, and generate the OpenAPI 3.1 contract from the
7
+ same declaration.
7
8
 
8
9
  Every adapter goes through one pipeline (`executeRoute`): validation, handler, error mapping.
9
10
  The behaviour is therefore the same on the four frameworks, and one test suite proves it.
@@ -1247,6 +1248,216 @@ A plain array works as well: `createRoutesModule(routes, options)`. These routes
1247
1248
  pipes, guards, interceptors, exception filters and global prefix: validation and error mapping
1248
1249
  are those described above. Use `prefixRoutes` for a prefix.
1249
1250
 
1251
+ ### Nest controllers served by the kit
1252
+
1253
+ A project that writes Nest controllers keeps them: `@Controller`, `@Get`, `@Post` and the other
1254
+ methods, `@Param`, `@Query`, `@Headers`, `@Body` with a DTO class, `@HttpCode`, injection by
1255
+ constructor and modules. Each method that carries a route decorator of Nest is a route of the
1256
+ kit, named after the method (`issueInvoice`, summary `Issue invoice`; renaming the method renames
1257
+ the route, its procedure and the function of its client), behind the guards of `@KitRoutes` on
1258
+ the class. `@KitRoute` on a method, optional, adds what Nest does not say: another
1259
+ name, its own guards, the refusals it answers, its answer. The kit, not Nest's router,
1260
+ serves the method: it is a route of the kit, with its guards, validation, problem documents,
1261
+ tracing and contract, and a procedure over `/rpc` and `/trpc` as every route.
1262
+
1263
+ <!-- checked sample: legacy-decorators -->
1264
+ ```ts
1265
+ import { Body, Controller, Inject, Module, Param, Post } from '@nestjs/common';
1266
+ import { IsString, MinLength } from 'class-validator';
1267
+ import type { Actor } from '@ddd-ts-kit/kernel';
1268
+ import { Authenticated, type ExecuteOptions } from '@ddd-ts-kit/http';
1269
+ import { createControllersModule, CurrentActor, KitRoute, KitRoutes } from '@ddd-ts-kit/http/nest';
1270
+ import { classSchema } from '@ddd-ts-kit/validation/class-validator';
1271
+
1272
+ declare const options: ExecuteOptions;
1273
+
1274
+ /** What the controller sends its use case through: here a port of the application. */
1275
+ abstract class Invoicing {
1276
+ abstract issue(invoiceId: string, customerId: string, actor: Actor): Promise<void>;
1277
+ }
1278
+
1279
+ declare const invoicing: Invoicing;
1280
+
1281
+ class IssueInvoiceBody {
1282
+ @IsString()
1283
+ @MinLength(1)
1284
+ customerId = '';
1285
+ }
1286
+
1287
+ @Controller('invoices')
1288
+ @KitRoutes({ guards: [Authenticated], tags: ['Invoices'] })
1289
+ export class InvoicesController {
1290
+ constructor(@Inject(Invoicing) private readonly invoicing: Invoicing) {}
1291
+
1292
+ @Post(':invoiceId/issue')
1293
+ @KitRoute({ errors: ['INVOICE_ALREADY_ISSUED'] }) // named issueInvoice, after the method
1294
+ async issue(
1295
+ @Param('invoiceId') invoiceId: string,
1296
+ @Body() body: IssueInvoiceBody,
1297
+ @CurrentActor() actor: Actor,
1298
+ ): Promise<void> {
1299
+ await this.invoicing.issue(invoiceId, body.customerId, actor);
1300
+ }
1301
+ }
1302
+
1303
+ @Module({
1304
+ imports: [
1305
+ createControllersModule([InvoicesController], {
1306
+ ...options,
1307
+ providers: [{ provide: Invoicing, useValue: invoicing }],
1308
+ schemaOfClass: classSchema,
1309
+ }),
1310
+ ],
1311
+ })
1312
+ export class AppModule {}
1313
+ ```
1314
+
1315
+ - `createControllersModule(controllers, options)` registers the controllers as providers, so that
1316
+ Nest builds them with what they inject, and mounts each method carrying `@KitRoute` on the
1317
+ HTTP server underneath Nest. A controller also listed in the `controllers` of a Nest module is
1318
+ refused at the start: Nest would serve it a second time, through its own pipeline.
1319
+ - `@KitRoutes({ guards, tags, errors })` on the class is what every route shares, as a route group;
1320
+ `guards` of `@KitRoute` run after. A route with no guard on either is refused: `[Public]` says
1321
+ it is open.
1322
+ - `@CurrentActor()` hands the method the caller the authentication resolved (`resolvePrincipal`),
1323
+ never one the controller builds; `@RouteContext()` the context of the request (`expectedVersion`
1324
+ under `ifMatch`).
1325
+ - A parameter that takes a part whole is validated by its class, read through `schemaOfClass`
1326
+ (`classSchema` of class-validator; the project compiles with `emitDecoratorMetadata`, as Nest
1327
+ needs); a part named one by one (`@Param('invoiceId')`) is a text; `request` of `@KitRoute`
1328
+ gives a schema of any library instead. `response.body` is a schema or a DTO class. A method
1329
+ answers a body only when `response.body` declares it, as the contract says: one that returns a
1330
+ value without it is refused, when the routes are built if TypeScript recorded its return type,
1331
+ else at the call (a 500, told to `onError`), never answered empty.
1332
+ - `nestControllerRoutes(controllers, { schemaOfClass })` gives the same routes without Nest's
1333
+ container, for `generateOpenApi`, `orpcRouter` and `trpcRouter`; `nestControllerRoute` the one
1334
+ route of a controller, for its spec.
1335
+ - An application that composes its routes before Nest starts takes `nestControllers(controllers,
1336
+ { providers, schemaOfClass })`: its `routes` join the other routes of the application, and
1337
+ `provideControllers(served)` in the Nest module builds the controllers with what they inject
1338
+ (the buses, as `{ provide: CommandBus, useValue: commands }`) and hands the routes their
1339
+ instances at the start. `ddd-ts-kit init --routes nest` writes that composition.
1340
+ - `@KitRoutes({ group })` names a route group of the kit (`@RouteGroup`): its prefix comes before
1341
+ the paths of Nest, its tags, guards and codes before those of the class.
1342
+ - What a method answers is its signature: TypeScript erases `Promise<IssuedInvoice>`, so
1343
+ `withAnswers(controller, { issueInvoice: IssuedInvoice })` says it to the bridge, a DTO class, a
1344
+ schema or `null` for no body; `response` of `@KitRoute` still wins. Under `--routes nest`,
1345
+ `ddd-ts-kit` reads the signatures and writes these calls in the generated
1346
+ `<context>.controllers.ts`, which the module of the context lists.
1347
+ - Refused when the routes are built, because the kit's pipeline would not run them: `@UseGuards`,
1348
+ `@UsePipes`, `@UseInterceptors`, `@UseFilters`, `@Header`, `@Redirect`, `@Render`, `@Sse`,
1349
+ `@Version`, a pipe on a parameter, `@Req`, `@Res` and the other parameters of Nest. The kit
1350
+ reads what Nest's decorators record (`@nestjs/common/constants`); its shape is checked on every
1351
+ read, and a test of the kit holds it on the Nest it is released with, so a release of Nest that
1352
+ changes it is refused at the start rather than read wrong.
1353
+
1354
+ ## oRPC procedures
1355
+
1356
+ A second door to the same routes, for a TypeScript client that calls procedures rather than
1357
+ paths. `orpcRouter` turns each route into an oRPC procedure named by its `operationId`, which
1358
+ `RPCHandler` of `@orpc/server` (1.15 or later, an optional peer) serves under a prefix, next to
1359
+ the routes themselves. Each call runs through `executeRoute` with the options the HTTP adapter
1360
+ is given: the guards, the principal, the tenant, the validation, the permissions of the use case,
1361
+ `onError` and `around` see a call over `/rpc` as they see a request.
1362
+
1363
+ <!-- checked sample -->
1364
+ ```ts
1365
+ import express from 'express';
1366
+ import type { ExecuteOptions, Route } from '@ddd-ts-kit/http';
1367
+ import { mountExpress } from '@ddd-ts-kit/http/express';
1368
+ import { orpcRouter } from '@ddd-ts-kit/http/orpc';
1369
+ import { RPCHandler } from '@orpc/server/node';
1370
+
1371
+ declare const routes: readonly Route[];
1372
+ declare const options: ExecuteOptions;
1373
+
1374
+ const app = express();
1375
+ const rpc = new RPCHandler(orpcRouter(routes, options));
1376
+
1377
+ app.use(express.json()); // oRPC reads the body Express already parsed
1378
+ app.use('/rpc', (request, response, next) => {
1379
+ const context = { headers: request.headers, raw: request };
1380
+
1381
+ void rpc.handle(request, response, { prefix: '/rpc', context }).then(({ matched }) => {
1382
+ if (!matched) {
1383
+ next();
1384
+ }
1385
+ }, next);
1386
+ });
1387
+ mountExpress(app, routes, options);
1388
+ ```
1389
+
1390
+ The `context` of a call is `OrpcContext`: the headers of the RPC request, which carry the
1391
+ credentials, the request and correlation ids and the tenant, and `raw`, what `resolvePrincipal`
1392
+ reads (`{ headers }` when it is left out, which `principalResolver` of `@ddd-ts-kit/auth` reads).
1393
+ Fastify (`@orpc/server/fastify`), Hono (`@orpc/server/fetch`) and NestJS on Express
1394
+ (`@orpc/server/node`) mount it the same way; `ddd-ts-kit init --rpc orpc` writes the mount for
1395
+ the framework of the project, and `ddd-ts-kit sdk` gives each client the oRPC contract of its
1396
+ document, at `<client>/orpc`.
1397
+
1398
+ | | Over `/rpc` |
1399
+ |---|---|
1400
+ | Input | `{ path, query, header, body }`, the input of the operation in the generated client; `header` goes over the headers of the RPC request |
1401
+ | Answer | the body of the success, as JSON writes it (a date is its text, as the contract says); a binary body is a `Blob` |
1402
+ | Refusal | an `ORPCError`: the `code` and `status` of the problem, its `detail` (or `title`) as `message`, the problem document as `data`. It is a defined error when the route declares its code (its own codes and those of its guards, `VALIDATION_FAILED`, the preconditions), with the status the error mapper of the application gives it. A refusal the handler answers itself, without a problem document, is `HTTP_<status>` with its body as `data` |
1403
+ | `If-Match` | sent in `header`, checked as on the route |
1404
+ | Headers of the answer | not returned: no `ETag`, no cache policy, no `Location`. A shared cache keeps nothing of `/rpc` and needs no purge |
1405
+ | Representations | none: JSON only, so no 406 nor 415 for a media type |
1406
+ | Upload, raw body | not a procedure: only the HTTP route receives a file |
1407
+ | File, list streamed as it is read | refused with 406 (`NOT_ACCEPTABLE`, its problem as `data`) and closed, without reading further: they are streamed because they may not fit in memory |
1408
+
1409
+ Two routes with one `operationId` are refused, as `mountExpress` refuses them: one procedure
1410
+ would hide the other.
1411
+
1412
+ ## tRPC procedures
1413
+
1414
+ The same door for a client of tRPC. `trpcRouter` turns each route into a tRPC procedure named by
1415
+ its `operationId`, a query for a `GET` route and a mutation for the other methods, which the
1416
+ adapters of `@trpc/server` (11.10 or later, an optional peer) serve under an endpoint such as
1417
+ `/trpc`. Each call runs through `executeRoute` with the options of the HTTP adapter, as a call of
1418
+ `orpcRouter` does.
1419
+
1420
+ <!-- checked sample -->
1421
+ ```ts
1422
+ import express from 'express';
1423
+ import type { ExecuteOptions, Route } from '@ddd-ts-kit/http';
1424
+ import { mountExpress } from '@ddd-ts-kit/http/express';
1425
+ import { trpcRouter } from '@ddd-ts-kit/http/trpc';
1426
+ import { createExpressMiddleware } from '@trpc/server/adapters/express';
1427
+
1428
+ declare const routes: readonly Route[];
1429
+ declare const options: ExecuteOptions;
1430
+
1431
+ const app = express();
1432
+
1433
+ app.use(express.json()); // tRPC reads the body Express already parsed
1434
+ app.use(
1435
+ '/trpc',
1436
+ createExpressMiddleware({
1437
+ router: trpcRouter(routes, options),
1438
+ createContext: ({ req }) => ({ headers: req.headers, raw: req }),
1439
+ }),
1440
+ );
1441
+ mountExpress(app, routes, options);
1442
+ ```
1443
+
1444
+ The context of a call is `TrpcContext`, the same as `OrpcContext`. Fastify
1445
+ (`@trpc/server/adapters/fastify`), Hono (`@trpc/server/adapters/fetch`) and NestJS on Express
1446
+ (`@trpc/server/adapters/standalone`) mount it the same way; `ddd-ts-kit init --rpc trpc` writes
1447
+ the mount for the framework of the project, and `ddd-ts-kit sdk` gives each client the type of
1448
+ the router, at `<client>/trpc`. Mount it as a router of its own, not merged into another: tRPC formats an
1449
+ error with the formatter of the router it serves, and the one of `trpcRouter` is what puts the
1450
+ problem document in `data.problem`.
1451
+
1452
+ | | Over `/trpc` |
1453
+ |---|---|
1454
+ | Procedure | a query for a `GET` route (`client.getOrder.query(input)`), a mutation for the other methods (`client.placeOrder.mutate(input)`) |
1455
+ | Input | `{ path, query, header, body }`, as over `/rpc` |
1456
+ | Answer | the body of the success, as JSON writes it |
1457
+ | Refusal | a `TRPCError` of the code tRPC has for the status (`UNAUTHORIZED` for 401, `NOT_FOUND` for 404, `CONFLICT` for 409, `UNPROCESSABLE_CONTENT` for 422; `BAD_REQUEST` or `INTERNAL_SERVER_ERROR` for a status it has no code for), the `detail` (or `title`) of the problem as `message`, and the problem document as `data.problem`. tRPC has a fixed set of codes: the `code` of the problem, the one of the kit, is what a client tells refusals apart by. `data.problem` is `null` for a refusal the handler answers without a problem document (its message is `HTTP_<status>`) and for an error of tRPC itself, such as an unknown procedure. The stack tRPC adds in development is left out |
1458
+ | `If-Match`, headers of the answer, representations, uploads and raw bodies | as over `/rpc` |
1459
+ | File, list streamed as it is read, binary body | refused with 406 (`NOT_ACCEPTABLE` in `data.problem`) and closed: tRPC answers JSON only |
1460
+
1250
1461
  ## OpenAPI 3.1
1251
1462
 
1252
1463
  <!-- checked sample -->
@@ -1499,6 +1710,11 @@ writes a file, and a route that declares no body schema is never refused 406.
1499
1710
  the client sees a truncated body, and `onError` is told. The items of a stream are not checked
1500
1711
  one by one by `validateResponses`.
1501
1712
  - CSV is written, never read, and holds flat rows only.
1713
+ - An oRPC or tRPC procedure answers its body only: what a route says in its headers (an `ETag`, a
1714
+ cache policy, a `Location`) stays with the HTTP route, and so do uploads, raw bodies, files,
1715
+ lists streamed as they are read and representations other than JSON (see "oRPC procedures");
1716
+ over tRPC, binary bodies too, and a refusal has the code tRPC has for its status, the code of
1717
+ the kit being in `data.problem`.
1502
1718
  - A form describes scalars, nested objects and lists of a closed vocabulary; a list of objects
1503
1719
  is a `list` field with no detail. Layout beyond groups and order is the front end's. A lookup
1504
1720
  names the route of the options; how the front end calls it and labels its items is its own.
@@ -0,0 +1,11 @@
1
+ import type { Representations } from '../representation/representations.js';
2
+ import type { Route } from '../route/route.js';
3
+ import type { ErrorMapper } from './error-mapper.js';
4
+ /**
5
+ * The error codes a route may answer, by the status the mapper gives each: those it declares,
6
+ * plus those the pipeline answers for it (a refused input, an upload, a precondition, a media
7
+ * type under negotiation). What the contract documents in `x-error-codes`, and what an oRPC
8
+ * procedure declares.
9
+ */
10
+ export declare function refusalCodesByStatus(route: Route, mapper: ErrorMapper, representations: Representations | undefined): Map<number, string[]>;
11
+ //# sourceMappingURL=refusal-codes-by-status.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"refusal-codes-by-status.d.ts","sourceRoot":"","sources":["../../src/error/refusal-codes-by-status.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,mCAAmC,CAAC;AAGzE,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,gBAAgB,CAAC;AAC5C,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAQlD;;;;;GAKG;AACH,wBAAgB,oBAAoB,CAClC,KAAK,EAAE,KAAK,EACZ,MAAM,EAAE,WAAW,EACnB,eAAe,EAAE,eAAe,GAAG,SAAS,GAC3C,GAAG,CAAC,MAAM,EAAE,MAAM,EAAE,CAAC,CAqBvB"}
@@ -0,0 +1,48 @@
1
+ import { IfMatch } from '../cache/if-match.js';
2
+ import { NOT_ACCEPTABLE } from '../representation/not-acceptable-error.js';
3
+ import { UNSUPPORTED_MEDIA_TYPE } from '../representation/unsupported-media-type-error.js';
4
+ import { answersWithSchema } from '../route/answers-with-schema.js';
5
+ const VALIDATION_FAILED = 'VALIDATION_FAILED';
6
+ const PRECONDITION_FAILED = 'PRECONDITION_FAILED';
7
+ const PRECONDITION_REQUIRED = 'PRECONDITION_REQUIRED';
8
+ /** What a route that declares an upload may answer besides its own codes: a file too large, of another type, or missing. */
9
+ const UPLOAD_REFUSALS = ['FILE_TOO_LARGE', 'UNSUPPORTED_MEDIA_TYPE', VALIDATION_FAILED];
10
+ /**
11
+ * The error codes a route may answer, by the status the mapper gives each: those it declares,
12
+ * plus those the pipeline answers for it (a refused input, an upload, a precondition, a media
13
+ * type under negotiation). What the contract documents in `x-error-codes`, and what an oRPC
14
+ * procedure declares.
15
+ */
16
+ export function refusalCodesByStatus(route, mapper, representations) {
17
+ const validatesInput = Object.values(route.request ?? {}).some((schema) => schema !== undefined);
18
+ const uploads = route.upload === undefined ? [] : UPLOAD_REFUSALS;
19
+ const codes = new Set([
20
+ ...(route.errors ?? []),
21
+ ...(validatesInput ? [VALIDATION_FAILED] : []),
22
+ ...uploads,
23
+ ...(representations === undefined ? [] : negotiationCodes(route)),
24
+ ...preconditionCodes(route),
25
+ ]);
26
+ const codesByStatus = new Map();
27
+ for (const code of codes) {
28
+ const status = mapper.statusOf(code);
29
+ codesByStatus.set(status, [...(codesByStatus.get(status) ?? []), code]);
30
+ }
31
+ return codesByStatus;
32
+ }
33
+ /**
34
+ * Under negotiation, a route whose answer a representation writes refuses an `Accept` header it
35
+ * cannot meet with 406, and a route that reads a body refuses a media type it does not read with
36
+ * 415, whatever it declares in `produces` and `consumes`.
37
+ */
38
+ function negotiationCodes(route) {
39
+ const readsBody = route.request?.body !== undefined && route.upload === undefined && route.rawBody !== true;
40
+ return [...(answersWithSchema(route) ? [NOT_ACCEPTABLE] : []), ...(readsBody ? [UNSUPPORTED_MEDIA_TYPE] : [])];
41
+ }
42
+ function preconditionCodes({ ifMatch }) {
43
+ if (ifMatch === undefined) {
44
+ return [];
45
+ }
46
+ return ifMatch === IfMatch.required ? [PRECONDITION_FAILED, PRECONDITION_REQUIRED] : [PRECONDITION_FAILED];
47
+ }
48
+ //# sourceMappingURL=refusal-codes-by-status.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"refusal-codes-by-status.js","sourceRoot":"","sources":["../../src/error/refusal-codes-by-status.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,mBAAmB,CAAC;AAC5C,OAAO,EAAE,cAAc,EAAE,MAAM,wCAAwC,CAAC;AAExE,OAAO,EAAE,sBAAsB,EAAE,MAAM,gDAAgD,CAAC;AACxF,OAAO,EAAE,iBAAiB,EAAE,MAAM,8BAA8B,CAAC;AAIjE,MAAM,iBAAiB,GAAG,mBAAmB,CAAC;AAC9C,MAAM,mBAAmB,GAAG,qBAAqB,CAAC;AAClD,MAAM,qBAAqB,GAAG,uBAAuB,CAAC;AACtD,4HAA4H;AAC5H,MAAM,eAAe,GAAG,CAAC,gBAAgB,EAAE,wBAAwB,EAAE,iBAAiB,CAAC,CAAC;AAExF;;;;;GAKG;AACH,MAAM,UAAU,oBAAoB,CAClC,KAAY,EACZ,MAAmB,EACnB,eAA4C;IAE5C,MAAM,cAAc,GAAG,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,OAAO,IAAI,EAAE,CAAC,CAAC,IAAI,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,MAAM,KAAK,SAAS,CAAC,CAAC;IACjG,MAAM,OAAO,GAAG,KAAK,CAAC,MAAM,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,eAAe,CAAC;IAElE,MAAM,KAAK,GAAG,IAAI,GAAG,CAAC;QACpB,GAAG,CAAC,KAAK,CAAC,MAAM,IAAI,EAAE,CAAC;QACvB,GAAG,CAAC,cAAc,CAAC,CAAC,CAAC,CAAC,iBAAiB,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;QAC9C,GAAG,OAAO;QACV,GAAG,CAAC,eAAe,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,gBAAgB,CAAC,KAAK,CAAC,CAAC;QACjE,GAAG,iBAAiB,CAAC,KAAK,CAAC;KAC5B,CAAC,CAAC;IAEH,MAAM,aAAa,GAAG,IAAI,GAAG,EAAoB,CAAC;IAElD,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE,CAAC;QACzB,MAAM,MAAM,GAAG,MAAM,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC;QAErC,aAAa,CAAC,GAAG,CAAC,MAAM,EAAE,CAAC,GAAG,CAAC,aAAa,CAAC,GAAG,CAAC,MAAM,CAAC,IAAI,EAAE,CAAC,EAAE,IAAI,CAAC,CAAC,CAAC;IAC1E,CAAC;IAED,OAAO,aAAa,CAAC;AACvB,CAAC;AAED;;;;GAIG;AACH,SAAS,gBAAgB,CAAC,KAAY;IACpC,MAAM,SAAS,GAAG,KAAK,CAAC,OAAO,EAAE,IAAI,KAAK,SAAS,IAAI,KAAK,CAAC,MAAM,KAAK,SAAS,IAAI,KAAK,CAAC,OAAO,KAAK,IAAI,CAAC;IAE5G,OAAO,CAAC,GAAG,CAAC,iBAAiB,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,cAAc,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,GAAG,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,sBAAsB,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;AACjH,CAAC;AAED,SAAS,iBAAiB,CAAC,EAAE,OAAO,EAAS;IAC3C,IAAI,OAAO,KAAK,SAAS,EAAE,CAAC;QAC1B,OAAO,EAAE,CAAC;IACZ,CAAC;IAED,OAAO,OAAO,KAAK,OAAO,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,mBAAmB,EAAE,qBAAqB,CAAC,CAAC,CAAC,CAAC,CAAC,mBAAmB,CAAC,CAAC;AAC7G,CAAC"}
@@ -0,0 +1,7 @@
1
+ import type { ErasedController } from '../../controller/erased-controller.js';
2
+ import type { NestMethod } from './nest-method.js';
3
+ type Request = Parameters<ErasedController['handle']>[0];
4
+ /** The arguments of the method, in the order of its parameters, from the validated request. */
5
+ export declare function argumentsOf({ read, injected }: NestMethod, request: Request): unknown[];
6
+ export {};
7
+ //# sourceMappingURL=arguments-of.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"arguments-of.d.ts","sourceRoot":"","sources":["../../../src/nest/controllers/arguments-of.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,oCAAoC,CAAC;AAC3E,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAEhD,KAAK,OAAO,GAAG,UAAU,CAAC,gBAAgB,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;AAEzD,+FAA+F;AAC/F,wBAAgB,WAAW,CAAC,EAAE,IAAI,EAAE,QAAQ,EAAE,EAAE,UAAU,EAAE,OAAO,EAAE,OAAO,GAAG,OAAO,EAAE,CAevF"}
@@ -0,0 +1,21 @@
1
+ /** The arguments of the method, in the order of its parameters, from the validated request. */
2
+ export function argumentsOf({ read, injected }, request) {
3
+ const arity = Math.max(-1, ...read.parameters.map(({ index }) => index), ...injected.keys()) + 1;
4
+ const args = Array.from({ length: arity }, () => undefined);
5
+ for (const { index, source, key } of read.parameters) {
6
+ const part = request[source];
7
+ args[index] = key === undefined ? part : fieldOf(part, source === 'headers' ? key.toLowerCase() : key);
8
+ }
9
+ for (const [index, what] of injected) {
10
+ args[index] = what === 'actor' ? request.actor : request.context;
11
+ }
12
+ return args;
13
+ }
14
+ function fieldOf(part, key) {
15
+ if (typeof part !== 'object' || part === null) {
16
+ return undefined;
17
+ }
18
+ const value = Reflect.get(part, key);
19
+ return value;
20
+ }
21
+ //# sourceMappingURL=arguments-of.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"arguments-of.js","sourceRoot":"","sources":["../../../src/nest/controllers/arguments-of.ts"],"names":[],"mappings":"AAKA,+FAA+F;AAC/F,MAAM,UAAU,WAAW,CAAC,EAAE,IAAI,EAAE,QAAQ,EAAc,EAAE,OAAgB;IAC1E,MAAM,KAAK,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,GAAG,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC,EAAE,KAAK,EAAE,EAAE,EAAE,CAAC,KAAK,CAAC,EAAE,GAAG,QAAQ,CAAC,IAAI,EAAE,CAAC,GAAG,CAAC,CAAC;IACjG,MAAM,IAAI,GAAc,KAAK,CAAC,IAAI,CAAC,EAAE,MAAM,EAAE,KAAK,EAAE,EAAE,GAAG,EAAE,CAAC,SAAS,CAAC,CAAC;IAEvE,KAAK,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,GAAG,EAAE,IAAI,IAAI,CAAC,UAAU,EAAE,CAAC;QACrD,MAAM,IAAI,GAAY,OAAO,CAAC,MAAM,CAAC,CAAC;QAEtC,IAAI,CAAC,KAAK,CAAC,GAAG,GAAG,KAAK,SAAS,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,OAAO,CAAC,IAAI,EAAE,MAAM,KAAK,SAAS,CAAC,CAAC,CAAC,GAAG,CAAC,WAAW,EAAE,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC;IACzG,CAAC;IAED,KAAK,MAAM,CAAC,KAAK,EAAE,IAAI,CAAC,IAAI,QAAQ,EAAE,CAAC;QACrC,IAAI,CAAC,KAAK,CAAC,GAAG,IAAI,KAAK,OAAO,CAAC,CAAC,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,OAAO,CAAC,OAAO,CAAC;IACnE,CAAC;IAED,OAAO,IAAI,CAAC;AACd,CAAC;AAED,SAAS,OAAO,CAAC,IAAa,EAAE,GAAW;IACzC,IAAI,OAAO,IAAI,KAAK,QAAQ,IAAI,IAAI,KAAK,IAAI,EAAE,CAAC;QAC9C,OAAO,SAAS,CAAC;IACnB,CAAC;IAED,MAAM,KAAK,GAAY,OAAO,CAAC,GAAG,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC;IAE9C,OAAO,KAAK,CAAC;AACf,CAAC"}
@@ -0,0 +1,8 @@
1
+ import type { ModulesContainer } from '@nestjs/core';
2
+ /**
3
+ * Refuses the start when a controller the kit serves is also among the `controllers` of a Nest
4
+ * module: Nest's router would serve it a second time, through its own pipeline, with none of the
5
+ * guards, validation and problem documents of the kit.
6
+ */
7
+ export declare function assertNotRoutedByNest(modules: ModulesContainer): void;
8
+ //# sourceMappingURL=assert-not-routed-by-nest.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"assert-not-routed-by-nest.d.ts","sourceRoot":"","sources":["../../../src/nest/controllers/assert-not-routed-by-nest.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AAGrD;;;;GAIG;AACH,wBAAgB,qBAAqB,CAAC,OAAO,EAAE,gBAAgB,GAAG,IAAI,CAcrE"}
@@ -0,0 +1,19 @@
1
+ import { DeclarationError } from '@ddd-ts-kit/kernel/declarations';
2
+ import { nestRoutesRegistry } from './nest-routes-registry.js';
3
+ /**
4
+ * Refuses the start when a controller the kit serves is also among the `controllers` of a Nest
5
+ * module: Nest's router would serve it a second time, through its own pipeline, with none of the
6
+ * guards, validation and problem documents of the kit.
7
+ */
8
+ export function assertNotRoutedByNest(modules) {
9
+ const { routes } = nestRoutesRegistry();
10
+ for (const module of modules.values()) {
11
+ for (const token of module.controllers.keys()) {
12
+ const prototype = typeof token === 'function' ? Reflect.get(token, 'prototype') : undefined;
13
+ if (typeof token === 'function' && typeof prototype === 'object' && prototype !== null && routes.has(prototype)) {
14
+ throw new DeclarationError(`${token.name} carries @KitRoute and is in the controllers of the Nest module ${module.metatype.name}: Nest would serve it through its own pipeline too. Give it to createControllersModule only.`);
15
+ }
16
+ }
17
+ }
18
+ }
19
+ //# sourceMappingURL=assert-not-routed-by-nest.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"assert-not-routed-by-nest.js","sourceRoot":"","sources":["../../../src/nest/controllers/assert-not-routed-by-nest.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,iCAAiC,CAAC;AAEnE,OAAO,EAAE,kBAAkB,EAAE,MAAM,wBAAwB,CAAC;AAE5D;;;;GAIG;AACH,MAAM,UAAU,qBAAqB,CAAC,OAAyB;IAC7D,MAAM,EAAE,MAAM,EAAE,GAAG,kBAAkB,EAAE,CAAC;IAExC,KAAK,MAAM,MAAM,IAAI,OAAO,CAAC,MAAM,EAAE,EAAE,CAAC;QACtC,KAAK,MAAM,KAAK,IAAI,MAAM,CAAC,WAAW,CAAC,IAAI,EAAE,EAAE,CAAC;YAC9C,MAAM,SAAS,GAAY,OAAO,KAAK,KAAK,UAAU,CAAC,CAAC,CAAC,OAAO,CAAC,GAAG,CAAC,KAAK,EAAE,WAAW,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC;YAErG,IAAI,OAAO,KAAK,KAAK,UAAU,IAAI,OAAO,SAAS,KAAK,QAAQ,IAAI,SAAS,KAAK,IAAI,IAAI,MAAM,CAAC,GAAG,CAAC,SAAS,CAAC,EAAE,CAAC;gBAChH,MAAM,IAAI,gBAAgB,CACxB,GAAG,KAAK,CAAC,IAAI,mEAAmE,MAAM,CAAC,QAAQ,CAAC,IAAI,8FAA8F,CACnM,CAAC;YACJ,CAAC;QACH,CAAC;IACH,CAAC;AACH,CAAC"}
@@ -0,0 +1,6 @@
1
+ /** The Nest provider `provideControllers` registers: it hands the routes their instances when Nest initialises it. */
2
+ export declare abstract class ControllersBinder {
3
+ static readonly tokenKey = "http.ControllersBinder";
4
+ abstract onModuleInit(): void;
5
+ }
6
+ //# sourceMappingURL=controllers-binder.d.ts.map