@tsed/common 7.0.0-beta.12 → 7.0.0-beta.15

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 (228) hide show
  1. package/jest.config.js +14 -0
  2. package/lib/cjs/builder/PlatformBuilder.js +83 -63
  3. package/lib/cjs/builder/PlatformBuilder.js.map +1 -1
  4. package/lib/cjs/config/interfaces/PlatformLoggerSettings.js.map +1 -1
  5. package/lib/cjs/config/services/PlatformConfiguration.js +0 -70
  6. package/lib/cjs/config/services/PlatformConfiguration.js.map +1 -1
  7. package/lib/cjs/decorators/params/error.js +1 -1
  8. package/lib/cjs/decorators/params/error.js.map +1 -1
  9. package/lib/cjs/domain/AnyToPromiseWithCtx.js +4 -5
  10. package/lib/cjs/domain/AnyToPromiseWithCtx.js.map +1 -1
  11. package/lib/cjs/domain/PlatformContext.js +82 -32
  12. package/lib/cjs/domain/PlatformContext.js.map +1 -1
  13. package/lib/cjs/domain/PlatformRouteDetails.js +0 -21
  14. package/lib/cjs/domain/PlatformRouteDetails.js.map +1 -1
  15. package/lib/cjs/exports.js +1 -0
  16. package/lib/cjs/exports.js.map +1 -1
  17. package/lib/cjs/index.js +1 -7
  18. package/lib/cjs/index.js.map +1 -1
  19. package/lib/cjs/interfaces/Route.js.map +1 -1
  20. package/lib/cjs/middlewares/PlatformAcceptMimesMiddleware.js +2 -1
  21. package/lib/cjs/middlewares/PlatformAcceptMimesMiddleware.js.map +1 -1
  22. package/lib/cjs/middlewares/PlatformMulterMiddleware.js +2 -0
  23. package/lib/cjs/middlewares/PlatformMulterMiddleware.js.map +1 -1
  24. package/lib/cjs/services/FakeAdapter.js +6 -12
  25. package/lib/cjs/services/FakeAdapter.js.map +1 -1
  26. package/lib/cjs/services/Platform.js +43 -85
  27. package/lib/cjs/services/Platform.js.map +1 -1
  28. package/lib/cjs/services/PlatformAdapter.js +10 -0
  29. package/lib/cjs/services/PlatformAdapter.js.map +1 -1
  30. package/lib/cjs/services/PlatformApplication.js +23 -9
  31. package/lib/cjs/services/PlatformApplication.js.map +1 -1
  32. package/lib/cjs/services/PlatformHandler.js +88 -161
  33. package/lib/cjs/services/PlatformHandler.js.map +1 -1
  34. package/lib/cjs/services/PlatformMiddlewaresChain.js +5 -19
  35. package/lib/cjs/services/PlatformMiddlewaresChain.js.map +1 -1
  36. package/lib/cjs/services/PlatformRequest.js +17 -12
  37. package/lib/cjs/services/PlatformRequest.js.map +1 -1
  38. package/lib/cjs/services/PlatformResponse.js +20 -20
  39. package/lib/cjs/services/PlatformResponse.js.map +1 -1
  40. package/lib/cjs/services/PlatformTest.js +20 -8
  41. package/lib/cjs/services/PlatformTest.js.map +1 -1
  42. package/lib/cjs/utils/createContext.js +30 -21
  43. package/lib/cjs/utils/createContext.js.map +1 -1
  44. package/lib/cjs/utils/createHttpServer.js +3 -3
  45. package/lib/cjs/utils/createHttpServer.js.map +1 -1
  46. package/lib/cjs/utils/createHttpsServer.js +3 -3
  47. package/lib/cjs/utils/createHttpsServer.js.map +1 -1
  48. package/lib/cjs/utils/createInjector.js +6 -8
  49. package/lib/cjs/utils/createInjector.js.map +1 -1
  50. package/lib/cjs/utils/ensureContext.js +16 -0
  51. package/lib/cjs/utils/ensureContext.js.map +1 -0
  52. package/lib/cjs/utils/getConfiguration.js +2 -12
  53. package/lib/cjs/utils/getConfiguration.js.map +1 -1
  54. package/lib/cjs/utils/printRoutes.js +10 -8
  55. package/lib/cjs/utils/printRoutes.js.map +1 -1
  56. package/lib/cjs/utils/setResponseHeaders.js +7 -13
  57. package/lib/cjs/utils/setResponseHeaders.js.map +1 -1
  58. package/lib/esm/builder/PlatformBuilder.js +60 -63
  59. package/lib/esm/builder/PlatformBuilder.js.map +1 -1
  60. package/lib/esm/config/interfaces/PlatformLoggerSettings.js.map +1 -1
  61. package/lib/esm/config/services/PlatformConfiguration.js +0 -70
  62. package/lib/esm/config/services/PlatformConfiguration.js.map +1 -1
  63. package/lib/esm/decorators/params/error.js +1 -1
  64. package/lib/esm/decorators/params/error.js.map +1 -1
  65. package/lib/esm/domain/AnyToPromiseWithCtx.js +4 -5
  66. package/lib/esm/domain/AnyToPromiseWithCtx.js.map +1 -1
  67. package/lib/esm/domain/PlatformContext.js +82 -32
  68. package/lib/esm/domain/PlatformContext.js.map +1 -1
  69. package/lib/esm/domain/PlatformRouteDetails.js +1 -19
  70. package/lib/esm/domain/PlatformRouteDetails.js.map +1 -1
  71. package/lib/esm/exports.js +1 -0
  72. package/lib/esm/exports.js.map +1 -1
  73. package/lib/esm/index.js +1 -7
  74. package/lib/esm/index.js.map +1 -1
  75. package/lib/esm/interfaces/Route.js.map +1 -1
  76. package/lib/esm/middlewares/PlatformAcceptMimesMiddleware.js +2 -1
  77. package/lib/esm/middlewares/PlatformAcceptMimesMiddleware.js.map +1 -1
  78. package/lib/esm/middlewares/PlatformMulterMiddleware.js +2 -0
  79. package/lib/esm/middlewares/PlatformMulterMiddleware.js.map +1 -1
  80. package/lib/esm/services/FakeAdapter.js +6 -12
  81. package/lib/esm/services/FakeAdapter.js.map +1 -1
  82. package/lib/esm/services/Platform.js +44 -86
  83. package/lib/esm/services/Platform.js.map +1 -1
  84. package/lib/esm/services/PlatformAdapter.js +10 -0
  85. package/lib/esm/services/PlatformAdapter.js.map +1 -1
  86. package/lib/esm/services/PlatformApplication.js +23 -9
  87. package/lib/esm/services/PlatformApplication.js.map +1 -1
  88. package/lib/esm/services/PlatformHandler.js +90 -163
  89. package/lib/esm/services/PlatformHandler.js.map +1 -1
  90. package/lib/esm/services/PlatformMiddlewaresChain.js +5 -19
  91. package/lib/esm/services/PlatformMiddlewaresChain.js.map +1 -1
  92. package/lib/esm/services/PlatformRequest.js +17 -12
  93. package/lib/esm/services/PlatformRequest.js.map +1 -1
  94. package/lib/esm/services/PlatformResponse.js +20 -20
  95. package/lib/esm/services/PlatformResponse.js.map +1 -1
  96. package/lib/esm/services/PlatformTest.js +20 -8
  97. package/lib/esm/services/PlatformTest.js.map +1 -1
  98. package/lib/esm/utils/createContext.js +28 -20
  99. package/lib/esm/utils/createContext.js.map +1 -1
  100. package/lib/esm/utils/createHttpServer.js +3 -3
  101. package/lib/esm/utils/createHttpServer.js.map +1 -1
  102. package/lib/esm/utils/createHttpsServer.js +3 -3
  103. package/lib/esm/utils/createHttpsServer.js.map +1 -1
  104. package/lib/esm/utils/createInjector.js +7 -9
  105. package/lib/esm/utils/createInjector.js.map +1 -1
  106. package/lib/esm/utils/ensureContext.js +12 -0
  107. package/lib/esm/utils/ensureContext.js.map +1 -0
  108. package/lib/esm/utils/getConfiguration.js +4 -14
  109. package/lib/esm/utils/getConfiguration.js.map +1 -1
  110. package/lib/esm/utils/printRoutes.js +10 -8
  111. package/lib/esm/utils/printRoutes.js.map +1 -1
  112. package/lib/esm/utils/setResponseHeaders.js +7 -13
  113. package/lib/esm/utils/setResponseHeaders.js.map +1 -1
  114. package/lib/types/builder/PlatformBuilder.d.ts +17 -16
  115. package/lib/types/config/interfaces/PlatformLoggerSettings.d.ts +2 -43
  116. package/lib/types/config/services/PlatformConfiguration.d.ts +1 -22
  117. package/lib/types/domain/AnyToPromiseWithCtx.d.ts +1 -9
  118. package/lib/types/domain/PlatformContext.d.ts +36 -15
  119. package/lib/types/domain/PlatformRouteDetails.d.ts +4 -19
  120. package/lib/types/exports.d.ts +1 -0
  121. package/lib/types/index.d.ts +1 -7
  122. package/lib/types/interfaces/Route.d.ts +2 -3
  123. package/lib/types/middlewares/PlatformMulterMiddleware.d.ts +1 -1
  124. package/lib/types/services/FakeAdapter.d.ts +7 -6
  125. package/lib/types/services/Platform.d.ts +11 -28
  126. package/lib/types/services/PlatformAdapter.d.ts +19 -10
  127. package/lib/types/services/PlatformApplication.d.ts +14 -6
  128. package/lib/types/services/PlatformHandler.d.ts +19 -55
  129. package/lib/types/services/PlatformMiddlewaresChain.d.ts +1 -2
  130. package/lib/types/services/PlatformRequest.d.ts +8 -9
  131. package/lib/types/services/PlatformResponse.d.ts +10 -13
  132. package/lib/types/services/PlatformTest.d.ts +4 -3
  133. package/lib/types/utils/createContext.d.ts +4 -1
  134. package/lib/types/utils/createHttpsServer.d.ts +1 -0
  135. package/lib/types/utils/createInjector.d.ts +2 -2
  136. package/lib/types/utils/ensureContext.d.ts +2 -0
  137. package/lib/types/utils/listenServer.d.ts +2 -0
  138. package/package.json +23 -21
  139. package/lib/cjs/builder/PlatformControllerBuilder.js +0 -82
  140. package/lib/cjs/builder/PlatformControllerBuilder.js.map +0 -1
  141. package/lib/cjs/domain/ControllerProvider.js +0 -51
  142. package/lib/cjs/domain/ControllerProvider.js.map +0 -1
  143. package/lib/cjs/domain/HandlerMetadata.js +0 -57
  144. package/lib/cjs/domain/HandlerMetadata.js.map +0 -1
  145. package/lib/cjs/middlewares/__mock__/FakeCtrl.js +0 -12
  146. package/lib/cjs/middlewares/__mock__/FakeCtrl.js.map +0 -1
  147. package/lib/cjs/middlewares/bindEndpointMiddleware.js +0 -13
  148. package/lib/cjs/middlewares/bindEndpointMiddleware.js.map +0 -1
  149. package/lib/cjs/services/PlatformRouter.js +0 -108
  150. package/lib/cjs/services/PlatformRouter.js.map +0 -1
  151. package/lib/cjs/utils/__mock__/Test1.js +0 -8
  152. package/lib/cjs/utils/__mock__/Test1.js.map +0 -1
  153. package/lib/cjs/utils/__mock__/Test2.js +0 -8
  154. package/lib/cjs/utils/__mock__/Test2.js.map +0 -1
  155. package/lib/cjs/utils/__mock__/controllers/rest/TestCtrl.js +0 -12
  156. package/lib/cjs/utils/__mock__/controllers/rest/TestCtrl.js.map +0 -1
  157. package/lib/cjs/utils/__mock__/module1/Module1.js +0 -19
  158. package/lib/cjs/utils/__mock__/module1/Module1.js.map +0 -1
  159. package/lib/cjs/utils/__mock__/module1/controllers/M1Ctrl1.js +0 -17
  160. package/lib/cjs/utils/__mock__/module1/controllers/M1Ctrl1.js.map +0 -1
  161. package/lib/cjs/utils/__mock__/module1/services/M1Service.js +0 -17
  162. package/lib/cjs/utils/__mock__/module1/services/M1Service.js.map +0 -1
  163. package/lib/cjs/utils/__mock__/module1/submodule/SubModule.js +0 -15
  164. package/lib/cjs/utils/__mock__/module1/submodule/SubModule.js.map +0 -1
  165. package/lib/cjs/utils/__mock__/module1/submodule/SubService.js +0 -12
  166. package/lib/cjs/utils/__mock__/module1/submodule/SubService.js.map +0 -1
  167. package/lib/cjs/utils/__mock__/module2/Module2.js +0 -18
  168. package/lib/cjs/utils/__mock__/module2/Module2.js.map +0 -1
  169. package/lib/cjs/utils/__mock__/module2/controllers/M2Ctrl.js +0 -12
  170. package/lib/cjs/utils/__mock__/module2/controllers/M2Ctrl.js.map +0 -1
  171. package/lib/cjs/utils/createHandlerMetadata.js +0 -49
  172. package/lib/cjs/utils/createHandlerMetadata.js.map +0 -1
  173. package/lib/cjs/utils/useCtxHandler.js +0 -15
  174. package/lib/cjs/utils/useCtxHandler.js.map +0 -1
  175. package/lib/esm/builder/PlatformControllerBuilder.js +0 -76
  176. package/lib/esm/builder/PlatformControllerBuilder.js.map +0 -1
  177. package/lib/esm/domain/ControllerProvider.js +0 -47
  178. package/lib/esm/domain/ControllerProvider.js.map +0 -1
  179. package/lib/esm/domain/HandlerMetadata.js +0 -53
  180. package/lib/esm/domain/HandlerMetadata.js.map +0 -1
  181. package/lib/esm/middlewares/__mock__/FakeCtrl.js +0 -9
  182. package/lib/esm/middlewares/__mock__/FakeCtrl.js.map +0 -1
  183. package/lib/esm/middlewares/bindEndpointMiddleware.js +0 -9
  184. package/lib/esm/middlewares/bindEndpointMiddleware.js.map +0 -1
  185. package/lib/esm/services/PlatformRouter.js +0 -105
  186. package/lib/esm/services/PlatformRouter.js.map +0 -1
  187. package/lib/esm/utils/__mock__/Test1.js +0 -4
  188. package/lib/esm/utils/__mock__/Test1.js.map +0 -1
  189. package/lib/esm/utils/__mock__/Test2.js +0 -4
  190. package/lib/esm/utils/__mock__/Test2.js.map +0 -1
  191. package/lib/esm/utils/__mock__/controllers/rest/TestCtrl.js +0 -9
  192. package/lib/esm/utils/__mock__/controllers/rest/TestCtrl.js.map +0 -1
  193. package/lib/esm/utils/__mock__/module1/Module1.js +0 -16
  194. package/lib/esm/utils/__mock__/module1/Module1.js.map +0 -1
  195. package/lib/esm/utils/__mock__/module1/controllers/M1Ctrl1.js +0 -14
  196. package/lib/esm/utils/__mock__/module1/controllers/M1Ctrl1.js.map +0 -1
  197. package/lib/esm/utils/__mock__/module1/services/M1Service.js +0 -14
  198. package/lib/esm/utils/__mock__/module1/services/M1Service.js.map +0 -1
  199. package/lib/esm/utils/__mock__/module1/submodule/SubModule.js +0 -12
  200. package/lib/esm/utils/__mock__/module1/submodule/SubModule.js.map +0 -1
  201. package/lib/esm/utils/__mock__/module1/submodule/SubService.js +0 -9
  202. package/lib/esm/utils/__mock__/module1/submodule/SubService.js.map +0 -1
  203. package/lib/esm/utils/__mock__/module2/Module2.js +0 -15
  204. package/lib/esm/utils/__mock__/module2/Module2.js.map +0 -1
  205. package/lib/esm/utils/__mock__/module2/controllers/M2Ctrl.js +0 -9
  206. package/lib/esm/utils/__mock__/module2/controllers/M2Ctrl.js.map +0 -1
  207. package/lib/esm/utils/createHandlerMetadata.js +0 -45
  208. package/lib/esm/utils/createHandlerMetadata.js.map +0 -1
  209. package/lib/esm/utils/useCtxHandler.js +0 -11
  210. package/lib/esm/utils/useCtxHandler.js.map +0 -1
  211. package/lib/types/builder/PlatformControllerBuilder.d.ts +0 -18
  212. package/lib/types/domain/ControllerProvider.d.ts +0 -24
  213. package/lib/types/domain/HandlerMetadata.d.ts +0 -32
  214. package/lib/types/middlewares/__mock__/FakeCtrl.d.ts +0 -2
  215. package/lib/types/middlewares/bindEndpointMiddleware.d.ts +0 -6
  216. package/lib/types/services/PlatformRouter.d.ts +0 -51
  217. package/lib/types/utils/__mock__/Test1.d.ts +0 -3
  218. package/lib/types/utils/__mock__/Test2.d.ts +0 -3
  219. package/lib/types/utils/__mock__/controllers/rest/TestCtrl.d.ts +0 -2
  220. package/lib/types/utils/__mock__/module1/Module1.d.ts +0 -2
  221. package/lib/types/utils/__mock__/module1/controllers/M1Ctrl1.d.ts +0 -5
  222. package/lib/types/utils/__mock__/module1/services/M1Service.d.ts +0 -5
  223. package/lib/types/utils/__mock__/module1/submodule/SubModule.d.ts +0 -2
  224. package/lib/types/utils/__mock__/module1/submodule/SubService.d.ts +0 -2
  225. package/lib/types/utils/__mock__/module2/Module2.d.ts +0 -2
  226. package/lib/types/utils/__mock__/module2/controllers/M2Ctrl.d.ts +0 -2
  227. package/lib/types/utils/createHandlerMetadata.d.ts +0 -8
  228. package/lib/types/utils/useCtxHandler.d.ts +0 -13
@@ -1,9 +1,8 @@
1
1
  import { __decorate, __metadata } from "tslib";
2
- import { Injectable, InjectorService, ProviderScope, ProviderType } from "@tsed/di";
3
- import { concatPath, getJsonEntityStore, getOperationsRoutes, JsonEntityStore } from "@tsed/schema";
4
- import { buildRouter, createRouter, getRouter } from "../builder/PlatformControllerBuilder.js";
5
- import { PlatformRouteDetails } from "../domain/PlatformRouteDetails.js";
2
+ import { Injectable, InjectorService, ProviderScope } from "@tsed/di";
3
+ import { PlatformRouters } from "@tsed/platform-router";
6
4
  import { PlatformApplication } from "./PlatformApplication.js";
5
+ import { PlatformHandler } from "./PlatformHandler.js";
7
6
  import { PlatformMiddlewaresChain } from "./PlatformMiddlewaresChain.js";
8
7
  /**
9
8
  * `Platform` is used to provide all routes collected by annotation `@Controller`.
@@ -11,115 +10,74 @@ import { PlatformMiddlewaresChain } from "./PlatformMiddlewaresChain.js";
11
10
  * @platform
12
11
  */
13
12
  let Platform = class Platform {
14
- constructor(injector, platformApplication) {
13
+ injector;
14
+ platformApplication;
15
+ platformRouters;
16
+ platformHandler;
17
+ platformMiddlewaresChain;
18
+ #controllers = new Map();
19
+ constructor(injector, platformApplication, platformRouters, platformHandler, platformMiddlewaresChain) {
15
20
  this.injector = injector;
16
21
  this.platformApplication = platformApplication;
17
- this._routes = [];
18
- this._controllers = [];
19
- this.createRouters();
22
+ this.platformRouters = platformRouters;
23
+ this.platformHandler = platformHandler;
24
+ this.platformMiddlewaresChain = platformMiddlewaresChain;
25
+ // configure the router module
26
+ platformRouters.hooks
27
+ .on("alterEndpointHandlers", platformMiddlewaresChain.get.bind(platformMiddlewaresChain))
28
+ .on("alterHandler", (handler, handlerMetadata) => {
29
+ handler = handlerMetadata.isRawMiddleware() ? handler : this.platformHandler.createHandler(handler, handlerMetadata);
30
+ return platformApplication.adapter.mapHandler(handler, handlerMetadata);
31
+ });
32
+ platformRouters.prebuild();
20
33
  }
21
34
  get app() {
22
35
  return this.platformApplication;
23
36
  }
24
- get routes() {
25
- return this._routes;
26
- }
27
37
  addRoutes(routes) {
28
38
  routes.forEach((routeSettings) => {
29
39
  this.addRoute(routeSettings.route, routeSettings.token);
30
40
  });
31
41
  }
32
- addRoute(basePath, token) {
33
- const { injector } = this;
34
- const provider = injector.getProvider(token);
42
+ addRoute(route, token) {
43
+ const provider = this.injector.getProvider(token);
35
44
  if (!provider || provider.hasParent()) {
36
- return;
45
+ return this;
37
46
  }
38
- this._controllers.push(...this.getAllControllers(basePath, token));
39
- const ctrlPath = concatPath(basePath, JsonEntityStore.from(provider.token).path);
40
- this.app.use(ctrlPath, ...[].concat(getRouter(injector, provider).callback()));
41
- this._routes = getOperationsRoutes(provider.token, {
42
- withChildren: true,
43
- basePath
44
- }).reduce((routes, operationRoute) => {
45
- if (injector.hasProvider(token)) {
46
- const provider = injector.getProvider(operationRoute.token);
47
- const route = new PlatformRouteDetails({
48
- ...operationRoute,
49
- provider
50
- });
51
- routes = routes.concat(route);
52
- }
53
- return routes;
54
- }, this._routes);
47
+ const router = this.platformRouters.from(provider.token);
48
+ this.app.use(route, router);
55
49
  return this;
56
50
  }
57
- /**
58
- * Get all routes built by TsExpressDecorators and mounted on Express application.
59
- * @returns {PlatformRouteDetails[]}
60
- */
61
- getRoutes() {
62
- return this._routes;
51
+ getLayers() {
52
+ this.#controllers = new Map();
53
+ return this.platformRouters.getLayers(this.app).map((layer) => {
54
+ if (layer.isProvider()) {
55
+ this.#controllers.set(layer.provider.token, {
56
+ route: String(layer.path).split(layer.provider.path)[0],
57
+ provider: layer.provider
58
+ });
59
+ }
60
+ return layer;
61
+ });
63
62
  }
64
63
  /**
65
64
  * Get all controllers mounted on the application.
66
65
  * @returns {RouteController[]}
67
66
  */
68
67
  getMountedControllers() {
69
- return this._controllers;
70
- }
71
- $onInit() {
72
- this.buildControllers();
73
- }
74
- /**
75
- * Create routers from the collected controllers.
76
- * @private
77
- */
78
- createRouters() {
79
- const { injector } = this;
80
- injector.getProviders(ProviderType.CONTROLLER).map((provider) => {
81
- createRouter(injector, provider);
82
- });
83
- }
84
- /**
85
- * Get all router controllers from the controller token.
86
- * @private
87
- */
88
- getAllControllers(basePath, token) {
89
- const store = token.isStore ? token : getJsonEntityStore(token);
90
- const ctrlPath = concatPath(basePath, JsonEntityStore.from(token).path);
91
- const children = store.get("childrenControllers", []);
92
- return children
93
- .reduce((controllers, token) => {
94
- const childBasePath = concatPath(basePath, store.path);
95
- return controllers.concat(this.getAllControllers(childBasePath, token));
96
- }, [])
97
- .concat([
98
- {
99
- route: ctrlPath,
100
- provider: this.injector.getProvider(token)
101
- }
102
- ]);
103
- }
104
- /**
105
- * Create controllers from DI
106
- * @private
107
- */
108
- buildControllers() {
109
- const { injector } = this;
110
- injector.getProviders(ProviderType.CONTROLLER).map((provider) => {
111
- if (!provider.hasParent()) {
112
- return buildRouter(injector, provider);
113
- }
114
- });
68
+ return [...this.#controllers.values()];
115
69
  }
116
70
  };
117
71
  Platform = __decorate([
118
72
  Injectable({
119
73
  scope: ProviderScope.SINGLETON,
120
- imports: [PlatformMiddlewaresChain]
74
+ imports: []
121
75
  }),
122
- __metadata("design:paramtypes", [InjectorService, PlatformApplication])
76
+ __metadata("design:paramtypes", [InjectorService,
77
+ PlatformApplication,
78
+ PlatformRouters,
79
+ PlatformHandler,
80
+ PlatformMiddlewaresChain])
123
81
  ], Platform);
124
82
  export { Platform };
125
83
  //# sourceMappingURL=Platform.js.map
@@ -1 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AAY,CAAC,UAAU,CAAC,CAAC,GAAG,CAAC,CAAC,QAA4B,EAAE,EAAE;YAClF,IAAI,CAAC,QAAQ,CAAC,SAAS,EAAE,EAAE;gBACzB,OAAO,WAAW,CAAC,QAAQ,EAAE,QAAQ,CAAC,CAAC;aACxC;QACH,CAAC,CAAC,CAAC;IACL,CAAC;CACF,CAAA;AA3HY,QAAQ;IAJpB,UAAU,CAAC;QACV,KAAK,EAAE,aAAa,CAAC,SAAS;QAC9B,OAAO,EAAE,CAAC,wBAAwB,CAAC;KACpC,CAAC;qCAK+B,eAAe,EAAgC,mBAAmB;GAJtF,QAAQ,CA2HpB;SA3HY,QAAQ","sourcesContent":["import {Type} from \"@tsed/core\";\nimport {Injectable, InjectorService, ProviderScope, ProviderType, TokenProvider} from \"@tsed/di\";\nimport {concatPath, EndpointMetadata, getJsonEntityStore, getOperationsRoutes, JsonEntityStore} from \"@tsed/schema\";\nimport {buildRouter, createRouter, getRouter} from \"../builder/PlatformControllerBuilder\";\nimport {ControllerProvider} from \"../domain/ControllerProvider\";\nimport {PlatformRouteDetails} from \"../domain/PlatformRouteDetails\";\nimport {Route, RouteController} from \"../interfaces/Route\";\nimport {PlatformApplication} from \"./PlatformApplication\";\nimport {PlatformMiddlewaresChain} from \"./PlatformMiddlewaresChain\";\n\n/**\n * `Platform` is used to provide all routes collected by annotation `@Controller`.\n *\n * @platform\n */\n@Injectable({\n scope: ProviderScope.SINGLETON,\n imports: [PlatformMiddlewaresChain]\n})\nexport class Platform {\n private _routes: PlatformRouteDetails[] = [];\n private _controllers: RouteController[] = [];\n\n constructor(readonly injector: InjectorService, readonly platformApplication: PlatformApplication) {\n this.createRouters();\n }\n\n get app() {\n return this.platformApplication;\n }\n\n get routes(): PlatformRouteDetails[] {\n return this._routes;\n }\n\n public addRoutes(routes: Route[]) {\n routes.forEach((routeSettings) => {\n this.addRoute(routeSettings.route, routeSettings.token);\n });\n }\n\n public addRoute(basePath: string, token: TokenProvider) {\n const {injector} = this;\n const provider = injector.getProvider(token) as ControllerProvider;\n\n if (!provider || provider.hasParent()) {\n return;\n }\n\n this._controllers.push(...this.getAllControllers(basePath, token));\n\n const ctrlPath = concatPath(basePath, JsonEntityStore.from(provider.token).path);\n\n this.app.use(ctrlPath, ...[].concat(getRouter(injector, provider).callback()));\n\n this._routes = getOperationsRoutes<EndpointMetadata>(provider.token, {\n withChildren: true,\n basePath\n }).reduce((routes, operationRoute) => {\n if (injector.hasProvider(token)) {\n const provider = injector.getProvider(operationRoute.token) as ControllerProvider;\n const route = new PlatformRouteDetails({\n ...operationRoute,\n provider\n });\n\n routes = routes.concat(route);\n }\n\n return routes;\n }, this._routes);\n\n return this;\n }\n\n /**\n * Get all routes built by TsExpressDecorators and mounted on Express application.\n * @returns {PlatformRouteDetails[]}\n */\n public getRoutes(): PlatformRouteDetails[] {\n return this._routes;\n }\n\n /**\n * Get all controllers mounted on the application.\n * @returns {RouteController[]}\n */\n public getMountedControllers(): RouteController[] {\n return this._controllers;\n }\n\n protected $onInit() {\n this.buildControllers();\n }\n\n /**\n * Create routers from the collected controllers.\n * @private\n */\n private createRouters() {\n const {injector} = this;\n\n injector.getProviders(ProviderType.CONTROLLER).map((provider: ControllerProvider) => {\n createRouter(injector, provider);\n });\n }\n\n /**\n * Get all router controllers from the controller token.\n * @private\n */\n private getAllControllers(basePath: string, token: Type<any> | any): RouteController[] {\n const store: JsonEntityStore = token.isStore ? token : getJsonEntityStore(token);\n const ctrlPath = concatPath(basePath, JsonEntityStore.from(token).path);\n const children = store.get<Type[]>(\"childrenControllers\", []);\n\n return children\n .reduce<RouteController[]>((controllers, token) => {\n const childBasePath = concatPath(basePath, store.path);\n return controllers.concat(this.getAllControllers(childBasePath, token));\n }, [])\n .concat([\n {\n route: ctrlPath,\n provider: this.injector.getProvider(token) as ControllerProvider\n }\n ]);\n }\n\n /**\n * Create controllers from DI\n * @private\n */\n private buildControllers() {\n const {injector} = this;\n\n injector.getProviders(ProviderType.CONTROLLER).map((provider: ControllerProvider) => {\n if (!provider.hasParent()) {\n return buildRouter(injector, provider);\n }\n });\n }\n}\n"]}
1
+ {"version":3,"file":"Platform.js","sourceRoot":"","sources":["../../../src/services/Platform.ts"],"names":[],"mappings":";AAAA,OAAO,EAAC,UAAU,EAAE,eAAe,EAAE,aAAa,EAAgB,MAAM,UAAU,CAAC;AACnF,OAAO,EAA0B,eAAe,EAAC,MAAM,uBAAuB,CAAC;AAG/E,OAAO,EAAC,mBAAmB,EAAC,MAAM,uBAAuB,CAAC;AAC1D,OAAO,EAAC,eAAe,EAAC,MAAM,mBAAmB,CAAC;AAClD,OAAO,EAAC,wBAAwB,EAAC,MAAM,4BAA4B,CAAC;AAEpE;;;;GAIG;AAKH,IAAa,QAAQ,GAArB,MAAa,QAAQ;IAIR;IACA;IACA;IACA;IACA;IAPX,YAAY,GAAiC,IAAI,GAAG,EAAE,CAAC;IAEvD,YACW,QAAyB,EACzB,mBAAwC,EACxC,eAAgC,EAChC,eAAgC,EAChC,wBAAkD;QAJlD,aAAQ,GAAR,QAAQ,CAAiB;QACzB,wBAAmB,GAAnB,mBAAmB,CAAqB;QACxC,oBAAe,GAAf,eAAe,CAAiB;QAChC,oBAAe,GAAf,eAAe,CAAiB;QAChC,6BAAwB,GAAxB,wBAAwB,CAA0B;QAE3D,8BAA8B;QAC9B,eAAe,CAAC,KAAK;aAClB,EAAE,CAAC,uBAAuB,EAAE,wBAAwB,CAAC,GAAG,CAAC,IAAI,CAAC,wBAAwB,CAAC,CAAC;aACxF,EAAE,CAAC,cAAc,EAAE,CAAC,OAAiB,EAAE,eAAwC,EAAE,EAAE;YAClF,OAAO,GAAG,eAAe,CAAC,eAAe,EAAE,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,IAAI,CAAC,eAAe,CAAC,aAAa,CAAC,OAAc,EAAE,eAAe,CAAC,CAAC;YAE5H,OAAO,mBAAmB,CAAC,OAAO,CAAC,UAAU,CAAC,OAAO,EAAE,eAAe,CAAC,CAAC;QAC1E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AAC,CAAC;aACJ;YAED,OAAO,KAAK,CAAC;QACf,CAAC,CAAC,CAAC;IACL,CAAC;IAED;;;OAGG;IACI,qBAAqB;QAC1B,OAAO,CAAC,GAAG,IAAI,CAAC,YAAY,CAAC,MAAM,EAAE,CAAC,CAAC;IACzC,CAAC;CACF,CAAA;AApEY,QAAQ;IAJpB,UAAU,CAAC;QACV,KAAK,EAAE,aAAa,CAAC,SAAS;QAC9B,OAAO,EAAE,EAAE;KACZ,CAAC;qCAKqB,eAAe;QACJ,mBAAmB;QACvB,eAAe;QACf,eAAe;QACN,wBAAwB;GARlD,QAAQ,CAoEpB;SApEY,QAAQ","sourcesContent":["import {Injectable, InjectorService, ProviderScope, TokenProvider} from \"@tsed/di\";\nimport {PlatformHandlerMetadata, PlatformRouters} from \"@tsed/platform-router\";\nimport {ControllerProvider} from \"@tsed/di\";\nimport {Route, RouteController} from \"../interfaces/Route\";\nimport {PlatformApplication} from \"./PlatformApplication\";\nimport {PlatformHandler} from \"./PlatformHandler\";\nimport {PlatformMiddlewaresChain} from \"./PlatformMiddlewaresChain\";\n\n/**\n * `Platform` is used to provide all routes collected by annotation `@Controller`.\n *\n * @platform\n */\n@Injectable({\n scope: ProviderScope.SINGLETON,\n imports: []\n})\nexport class Platform {\n #controllers: Map<string, RouteController> = new Map();\n\n constructor(\n readonly injector: InjectorService,\n readonly platformApplication: PlatformApplication,\n readonly platformRouters: PlatformRouters,\n readonly platformHandler: PlatformHandler,\n readonly platformMiddlewaresChain: PlatformMiddlewaresChain\n ) {\n // configure the router module\n platformRouters.hooks\n .on(\"alterEndpointHandlers\", platformMiddlewaresChain.get.bind(platformMiddlewaresChain))\n .on(\"alterHandler\", (handler: Function, handlerMetadata: PlatformHandlerMetadata) => {\n handler = handlerMetadata.isRawMiddleware() ? handler : this.platformHandler.createHandler(handler as any, handlerMetadata);\n\n return platformApplication.adapter.mapHandler(handler, handlerMetadata);\n });\n\n platformRouters.prebuild();\n }\n\n get app() {\n return this.platformApplication;\n }\n\n public addRoutes(routes: Route[]) {\n routes.forEach((routeSettings) => {\n this.addRoute(routeSettings.route, routeSettings.token);\n });\n }\n\n public addRoute(route: string, token: TokenProvider) {\n const provider = this.injector.getProvider(token) as ControllerProvider;\n\n if (!provider || provider.hasParent()) {\n return this;\n }\n\n const router = this.platformRouters.from(provider.token);\n\n this.app.use(route, router);\n\n return this;\n }\n\n public getLayers() {\n this.#controllers = new Map();\n\n return this.platformRouters.getLayers(this.app).map((layer) => {\n if (layer.isProvider()) {\n this.#controllers.set(layer.provider.token, {\n route: String(layer.path).split(layer.provider.path)[0],\n provider: layer.provider\n });\n }\n\n return layer;\n });\n }\n\n /**\n * Get all controllers mounted on the application.\n * @returns {RouteController[]}\n */\n public getMountedControllers(): RouteController[] {\n return [...this.#controllers.values()];\n }\n}\n"]}
@@ -1,6 +1,16 @@
1
1
  import { InjectorService, registerProvider } from "@tsed/di";
2
2
  import { FakeAdapter } from "./FakeAdapter.js";
3
3
  export class PlatformAdapter {
4
+ /**
5
+ * Load providers in top priority
6
+ */
7
+ providers;
8
+ /**
9
+ * Called after the injector instantiation
10
+ */
11
+ onInit;
12
+ beforeLoadRoutes;
13
+ afterLoadRoutes;
4
14
  }
5
15
  registerProvider({
6
16
  provide: PlatformAdapter,
@@ -1 +1 @@
1
- {"version":3,"file":"PlatformAdapter.js","sourceRoot":"","sources":["../../../src/services/PlatformAdapter.ts"],"names":[],"mappings":"AACA,OAAO,EAAC,eAAe,EAAgB,gBAAgB,EAAC,MAAM,UAAU,CAAC;AAIzE,OAAO,EAAC,WAAW,EAAC,MAAM,eAAe,CAAC;AAE1C,MAAM,OAAgB,eAAe;CAoCpC;AAMD,gBAAgB,CAAC;IACf,OAAO,EAAE,eAAe;IACxB,IAAI,EAAE,CAAC,eAAe,CAAC;IACvB,QAAQ,EAAE,WAAW;CACtB,CAAC,CAAC","sourcesContent":["import {Type} from \"@tsed/core\";\nimport {InjectorService, ProviderOpts, registerProvider} from \"@tsed/di\";\nimport {PlatformMulter, PlatformMulterSettings} from \"../config/interfaces/PlatformMulterSettings\";\nimport {PlatformStaticsOptions} from \"../config/interfaces/PlatformStaticsSettings\";\nimport {RouterOptions} from \"express\";\nimport {FakeAdapter} from \"./FakeAdapter\";\n\nexport abstract class PlatformAdapter<App = TsED.Application, Router = TsED.Router> {\n /**\n * Load providers in top priority\n */\n providers: ProviderOpts[];\n /**\n * Called after the injector instantiation\n */\n onInit?: () => any;\n beforeLoadRoutes?: () => Promise<any>;\n afterLoadRoutes?: () => Promise<any>;\n useRouter?: () => any;\n useContext?: () => any;\n\n abstract app(): {app: App; callback(): any};\n\n abstract router(routerOptions?: Partial<RouterOptions>): {router: Router; callback(): any};\n /**\n * Return the statics middlewares\n * @param endpoint\n * @param options\n */\n abstract statics(endpoint: string, options: PlatformStaticsOptions): any;\n\n /**\n * Return the multipart middleware\n * @param options\n */\n abstract multipart(options: PlatformMulterSettings): PlatformMulter;\n\n /**\n * Return the body parser for the given\n * @param type\n * @param opts\n */\n abstract bodyParser(type: string, opts?: Record<string, any>): any;\n}\n\nexport interface PlatformBuilderSettings<App = TsED.Application, Router = TsED.Router> extends Partial<TsED.Configuration> {\n adapter?: Type<PlatformAdapter<App, Router>>;\n}\n\nregisterProvider({\n provide: PlatformAdapter,\n deps: [InjectorService],\n useClass: FakeAdapter\n});\n"]}
1
+ {"version":3,"file":"PlatformAdapter.js","sourceRoot":"","sources":["../../../src/services/PlatformAdapter.ts"],"names":[],"mappings":"AACA,OAAO,EAAC,eAAe,EAAgB,gBAAgB,EAAC,MAAM,UAAU,CAAC;AAKzE,OAAO,EAAC,WAAW,EAAC,MAAM,eAAe,CAAC;AAE1C,MAAM,OAAgB,eAAe;IACnC;;OAEG;IACH,SAAS,CAAiB;IAC1B;;OAEG;IACH,MAAM,CAAa;IACnB,gBAAgB,CAAsB;IACtC,eAAe,CAAsB;CAsCtC;AAMD,gBAAgB,CAAC;IACf,OAAO,EAAE,eAAe;IACxB,IAAI,EAAE,CAAC,eAAe,CAAC;IACvB,QAAQ,EAAE,WAAW;CACtB,CAAC,CAAC","sourcesContent":["import {Type} from \"@tsed/core\";\nimport {InjectorService, ProviderOpts, registerProvider} from \"@tsed/di\";\nimport {PlatformContextHandler, PlatformHandlerMetadata, PlatformLayer} from \"@tsed/platform-router\";\nimport {PlatformMulter, PlatformMulterSettings} from \"../config/interfaces/PlatformMulterSettings\";\nimport {PlatformStaticsOptions} from \"../config/interfaces/PlatformStaticsSettings\";\nimport {PlatformContext} from \"../domain/PlatformContext\";\nimport {FakeAdapter} from \"./FakeAdapter\";\n\nexport abstract class PlatformAdapter<App = TsED.Application> {\n /**\n * Load providers in top priority\n */\n providers: ProviderOpts[];\n /**\n * Called after the injector instantiation\n */\n onInit?: () => any;\n beforeLoadRoutes?: () => Promise<any>;\n afterLoadRoutes?: () => Promise<any>;\n /**\n * create initial context\n */\n abstract useContext: () => any;\n /**\n * Map router layer to the targeted framework\n */\n abstract mapLayers: (layer: PlatformLayer[]) => void;\n /**\n * Map handler to the targeted framework\n */\n abstract mapHandler: (handler: Function, layer: PlatformHandlerMetadata) => Function;\n\n /**\n * Return the app instance\n */\n abstract app(): {app: App; callback(): any};\n\n /**\n * Return the statics middlewares\n * @param endpoint\n * @param options\n */\n abstract statics(endpoint: string, options: PlatformStaticsOptions): any;\n\n /**\n * Return the multipart middleware\n * @param options\n */\n abstract multipart(options: PlatformMulterSettings): PlatformMulter;\n\n /**\n * Return the body parser for the given\n * @param type\n * @param opts\n */\n abstract bodyParser(type: string, opts?: Record<string, any>): any;\n}\n\nexport interface PlatformBuilderSettings<App = TsED.Application> extends Partial<TsED.Configuration> {\n adapter?: Type<PlatformAdapter<App>>;\n}\n\nregisterProvider({\n provide: PlatformAdapter,\n deps: [InjectorService],\n useClass: FakeAdapter\n});\n"]}
@@ -1,7 +1,6 @@
1
1
  import { __decorate, __metadata } from "tslib";
2
- import { Injectable, ProviderScope } from "@tsed/di";
3
- import { PlatformHandler } from "./PlatformHandler.js";
4
- import { PlatformRouter } from "./PlatformRouter.js";
2
+ import { Injectable, InjectorService, ProviderScope } from "@tsed/di";
3
+ import { PlatformRouter } from "@tsed/platform-router";
5
4
  import { PlatformAdapter } from "./PlatformAdapter.js";
6
5
  /**
7
6
  * `PlatformApplication` is used to provide all routes collected by annotation `@Controller`.
@@ -9,21 +8,36 @@ import { PlatformAdapter } from "./PlatformAdapter.js";
9
8
  * @platform
10
9
  */
11
10
  let PlatformApplication = class PlatformApplication extends PlatformRouter {
12
- constructor(platformHandler, adapter) {
13
- super(platformHandler, adapter);
11
+ adapter;
12
+ injector;
13
+ rawApp;
14
+ rawCallback;
15
+ constructor(adapter, injector) {
16
+ super(injector);
17
+ this.adapter = adapter;
18
+ this.injector = injector;
14
19
  const { app, callback } = adapter.app();
15
- this.rawApp = this.raw = app;
16
- this.callback = callback;
20
+ this.rawApp = app;
21
+ this.rawCallback = callback;
17
22
  }
18
23
  getApp() {
19
- return this.raw;
24
+ return this.rawApp;
25
+ }
26
+ multer(options) {
27
+ return this.adapter.multipart(options);
28
+ }
29
+ callback(req, res) {
30
+ if (req && res) {
31
+ return this.callback()(req, res);
32
+ }
33
+ return this.rawCallback();
20
34
  }
21
35
  };
22
36
  PlatformApplication = __decorate([
23
37
  Injectable({
24
38
  scope: ProviderScope.SINGLETON
25
39
  }),
26
- __metadata("design:paramtypes", [PlatformHandler, PlatformAdapter])
40
+ __metadata("design:paramtypes", [PlatformAdapter, InjectorService])
27
41
  ], PlatformApplication);
28
42
  export { PlatformApplication };
29
43
  //# sourceMappingURL=PlatformApplication.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"PlatformApplication.js","sourceRoot":"","sources":["../../../src/services/PlatformApplication.ts"],"names":[],"mappings":";AAAA,OAAO,EAAC,UAAU,EAAE,aAAa,EAAC,MAAM,UAAU,CAAC;AACnD,OAAO,EAAC,eAAe,EAAC,MAAM,mBAAmB,CAAC;AAClD,OAAO,EAAC,cAAc,EAAC,MAAM,kBAAkB,CAAC;AAChD,OAAO,EAAC,eAAe,EAAC,MAAM,mBAAmB,CAAC;AASlD;;;;GAIG;AAIH,IAAa,mBAAmB,GAAhC,MAAa,mBAAkE,SAAQ,cAA2B;IAKhH,YAAY,eAAgC,EAAE,OAAqC;QACjF,KAAK,CAAC,eAAe,EAAE,OAAO,CAAC,CAAC;QAChC,MAAM,EAAC,GAAG,EAAE,QAAQ,EAAC,GAAG,OAAO,CAAC,GAAG,EAAE,CAAC;QAEtC,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC,GAAG,GAAG,GAAG,CAAC;QAC7B,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;IAC3B,CAAC;IAED,MAAM;QACJ,OAAO,IAAI,CAAC,GAAG,CAAC;IAClB,CAAC;CACF,CAAA;AAhBY,mBAAmB;IAH/B,UAAU,CAAC;QACV,KAAK,EAAE,aAAa,CAAC,SAAS;KAC/B,CAAC;qCAM6B,eAAe,EAAW,eAAe;GAL3D,mBAAmB,CAgB/B;SAhBY,mBAAmB","sourcesContent":["import {Injectable, ProviderScope} from \"@tsed/di\";\nimport {PlatformHandler} from \"./PlatformHandler\";\nimport {PlatformRouter} from \"./PlatformRouter\";\nimport {PlatformAdapter} from \"./PlatformAdapter\";\n\ndeclare global {\n namespace TsED {\n // @ts-ignore\n export interface Application {}\n }\n}\n\n/**\n * `PlatformApplication` is used to provide all routes collected by annotation `@Controller`.\n *\n * @platform\n */\n@Injectable({\n scope: ProviderScope.SINGLETON\n})\nexport class PlatformApplication<App = TsED.Application, Router = TsED.Router> extends PlatformRouter<App, Router> {\n raw: App;\n rawApp: App;\n declare rawRouter: Router;\n\n constructor(platformHandler: PlatformHandler, adapter: PlatformAdapter<App, Router>) {\n super(platformHandler, adapter);\n const {app, callback} = adapter.app();\n\n this.rawApp = this.raw = app;\n this.callback = callback;\n }\n\n getApp(): App {\n return this.raw;\n }\n}\n"]}
1
+ {"version":3,"file":"PlatformApplication.js","sourceRoot":"","sources":["../../../src/services/PlatformApplication.ts"],"names":[],"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port {IncomingMessage, ServerResponse} from \"http\";\nimport {PlatformMulterSettings} from \"../config/interfaces/PlatformMulterSettings\";\nimport {PlatformAdapter} from \"./PlatformAdapter\";\n\ndeclare global {\n namespace TsED {\n // @ts-ignore\n export interface Application {}\n }\n}\n\n/**\n * `PlatformApplication` is used to provide all routes collected by annotation `@Controller`.\n *\n * @platform\n */\n@Injectable({\n scope: ProviderScope.SINGLETON\n})\nexport class PlatformApplication<App = TsED.Application> extends PlatformRouter {\n rawApp: App;\n rawCallback: () => any;\n\n constructor(public adapter: PlatformAdapter<App>, public injector: InjectorService) {\n super(injector);\n const {app, callback} = adapter.app();\n\n this.rawApp = app;\n this.rawCallback = callback;\n }\n\n getApp(): App {\n return this.rawApp;\n }\n\n multer(options: PlatformMulterSettings) {\n return this.adapter.multipart(options);\n }\n\n callback(): (req: IncomingMessage, res: ServerResponse) => any;\n callback(req: IncomingMessage, res: ServerResponse): any;\n callback(req?: IncomingMessage, res?: ServerResponse) {\n if (req && res) {\n return this.callback()(req, res);\n }\n\n return this.rawCallback();\n }\n}\n"]}
@@ -1,232 +1,159 @@
1
1
  import { __decorate, __metadata } from "tslib";
2
- import { AnyToPromiseStatus, isFunction, isStream } from "@tsed/core";
3
- import { Inject, Injectable, InjectorService, ProviderScope, runInContext } from "@tsed/di";
2
+ import { AnyToPromiseStatus, catchAsyncError, isFunction, isStream } from "@tsed/core";
3
+ import { Inject, Injectable, ProviderScope } from "@tsed/di";
4
4
  import { $log } from "@tsed/logger";
5
5
  import { PlatformParams } from "@tsed/platform-params";
6
6
  import { PlatformResponseFilter } from "@tsed/platform-response-filter";
7
+ import { PlatformHandlerMetadata, PlatformHandlerType, PlatformRouters } from "@tsed/platform-router";
8
+ import { promisify } from "util";
7
9
  import { AnyToPromiseWithCtx } from "../domain/AnyToPromiseWithCtx.js";
8
- import { HandlerMetadata } from "../domain/HandlerMetadata.js";
9
- import { HandlerType } from "../interfaces/HandlerType.js";
10
- import { createHandlerMetadata } from "../utils/createHandlerMetadata.js";
11
10
  import { setResponseHeaders } from "../utils/setResponseHeaders.js";
12
11
  /**
13
12
  * Platform Handler abstraction layer. Wrap original class method to a pure platform handler (Express, Koa, etc...).
14
13
  * @platform
15
14
  */
16
15
  let PlatformHandler = class PlatformHandler {
17
- constructor(injector, params) {
18
- this.injector = injector;
19
- this.params = params;
20
- }
21
- /**
22
- * Create a native middleware based on the given metadata and return an instance of AnyToPromiseWithCtx
23
- * @param input
24
- * @param options
25
- */
26
- createHandler(input, options = {}) {
27
- return this.createRawHandler(createHandlerMetadata(this.injector, input, options));
16
+ responseFilter;
17
+ platformRouters;
18
+ platformParams;
19
+ createHandler(handler, handlerMetadata) {
20
+ return async ($ctx) => {
21
+ $ctx.handlerMetadata = handlerMetadata;
22
+ const error = await catchAsyncError(() => this.onRequest(handler, $ctx));
23
+ return this.next(error, $ctx.next, $ctx);
24
+ };
28
25
  }
29
26
  /**
30
- * Create injectable handler from the given provider
31
27
  * @param provider
32
28
  * @param propertyKey
33
29
  */
34
30
  createCustomHandler(provider, propertyKey) {
35
- const handler = this.compileHandler(new HandlerMetadata({
36
- token: provider.provide,
37
- target: provider.useClass,
38
- type: HandlerType.CUSTOM,
39
- scope: provider.scope,
31
+ const metadata = new PlatformHandlerMetadata({
32
+ provider,
33
+ type: PlatformHandlerType.CUSTOM,
40
34
  propertyKey
41
- }));
42
- return async ($ctx, next) => handler({
43
- $ctx,
44
- next
45
35
  });
36
+ const handler = this.platformParams.compileHandler(metadata.store);
37
+ return async ($ctx) => {
38
+ $ctx.set(PlatformHandlerMetadata, metadata);
39
+ // @ts-ignore
40
+ return this.onRequest(handler, $ctx);
41
+ };
46
42
  }
47
43
  /**
48
44
  * Send the response to the consumer.
49
- * @param data
50
- * @param ctx
51
45
  * @protected
46
+ * @param $ctx
52
47
  */
53
- async flush(data, ctx) {
54
- const { response } = ctx;
48
+ async flush($ctx) {
49
+ const { response } = $ctx;
55
50
  if (!response.isDone()) {
56
- data = await this.responseFilter.serialize(data, ctx);
57
- data = await this.responseFilter.transform(data, ctx);
51
+ let data = await this.responseFilter.serialize($ctx.data, $ctx);
52
+ data = await this.responseFilter.transform(data, $ctx);
58
53
  response.body(data);
59
54
  }
55
+ return response;
60
56
  }
61
- compileHandler(metadata) {
62
- if (metadata.type === HandlerType.CTX_FN) {
63
- return async (scope) => {
64
- // @ts-ignore
65
- return this.onCtxRequest({
66
- ...scope,
67
- handler: () => metadata.handler(scope.$ctx),
68
- metadata
69
- });
70
- };
57
+ setResponseHeaders($ctx) {
58
+ if (!$ctx.response.isDone()) {
59
+ return setResponseHeaders($ctx);
71
60
  }
72
- const promise = this.params.compileHandler({
73
- token: metadata.token,
74
- propertyKey: metadata.propertyKey,
75
- getCustomArgs: metadata.injectable ? undefined : this.getDefaultArgs(metadata)
76
- });
77
- return async (scope) => {
78
- const handler = await promise;
79
- // @ts-ignore
80
- return this.onRequest({
81
- ...scope,
82
- handler,
83
- metadata
84
- });
85
- };
86
61
  }
87
- async onCtxRequest(requestOptions) {
88
- await requestOptions.handler(requestOptions);
89
- return this.next(requestOptions);
62
+ /**
63
+ * @param error
64
+ * @param next
65
+ * @param $ctx
66
+ */
67
+ next(error, next, $ctx) {
68
+ if (isStream($ctx.data) || $ctx.isDone()) {
69
+ return;
70
+ }
71
+ return error ? next(error) : next();
90
72
  }
91
73
  /**
92
74
  * Call handler when a request his handle
93
- * @param requestOptions
94
75
  */
95
- async onRequest(requestOptions) {
96
- const { $ctx, metadata, err, handler } = requestOptions;
97
- // istanbul ignore next
98
- if (!$ctx || $ctx.isDone()) {
76
+ async onRequest(handler, $ctx) {
77
+ // istanbul ignore next$
78
+ if ($ctx.isDone()) {
99
79
  $log.error({
100
80
  name: "HEADERS_SENT",
101
- message: `An endpoint is called but the response is already send to the client. The call comes from the handler: ${metadata.toString()}`
81
+ message: `An endpoint is called but the response is already send to the client. The call comes from the handler: ${$ctx.handlerMetadata.toString()}`
102
82
  });
103
83
  return;
104
84
  }
105
- const resolver = new AnyToPromiseWithCtx({ $ctx, err });
106
- return runInContext($ctx, async () => {
107
- try {
108
- const { state, data, status, headers } = await resolver.call(handler);
109
- if (state === AnyToPromiseStatus.RESOLVED) {
110
- if (status) {
111
- $ctx.response.status(status);
112
- }
113
- if (headers) {
114
- $ctx.response.setHeaders(headers);
115
- }
116
- if (data !== undefined) {
117
- $ctx.data = data;
118
- }
119
- // Can be canceled by the handler itself
120
- return await this.onSuccess($ctx.data, requestOptions);
121
- }
85
+ try {
86
+ const { handlerMetadata } = $ctx;
87
+ if (handlerMetadata.type === PlatformHandlerType.CTX_FN) {
88
+ return await handler({ $ctx });
122
89
  }
123
- catch (er) {
124
- return this.onError(er, requestOptions);
90
+ const resolver = new AnyToPromiseWithCtx($ctx);
91
+ const { state, data, status, headers } = await resolver.call(handler);
92
+ // Note: restore previous handler metadata (for OIDC)
93
+ $ctx.handlerMetadata = handlerMetadata;
94
+ if (state === AnyToPromiseStatus.RESOLVED) {
95
+ if (status) {
96
+ $ctx.response.status(status);
97
+ }
98
+ if (headers) {
99
+ $ctx.response.setHeaders(headers);
100
+ }
101
+ if (data !== undefined) {
102
+ $ctx.data = data;
103
+ }
104
+ // Can be canceled by the handler itself
105
+ return await this.onSuccess($ctx);
125
106
  }
126
- });
127
- }
128
- async onError(er, requestOptions) {
129
- const { next, $ctx, metadata } = requestOptions;
130
- if ($ctx) {
131
- $ctx.data = er;
132
107
  }
133
- if (!next) {
134
- throw er;
108
+ catch (error) {
109
+ return this.onError(error, $ctx);
135
110
  }
136
- // istanbul ignore next
137
- if (!$ctx || $ctx.isDone()) {
138
- $log.warn({
139
- name: "HEADERS_SENT",
140
- message: `An error was caught after the headers were sent to the client. The error comes from the handler: ${metadata.toString()}`,
141
- stack: er.stack,
142
- origin: er,
143
- request_id: $ctx === null || $ctx === void 0 ? void 0 : $ctx.id
144
- });
145
- return;
146
- }
147
- return next(er);
111
+ }
112
+ async onError(error, $ctx) {
113
+ $ctx.error = error;
114
+ throw error;
148
115
  }
149
116
  /**
150
117
  * Manage success scenario
151
- * @param data
152
- * @param requestOptions
118
+ * @param $ctx
153
119
  * @protected
154
120
  */
155
- async onSuccess(data, requestOptions) {
156
- const { metadata, $ctx, next } = requestOptions;
121
+ async onSuccess($ctx) {
157
122
  // istanbul ignore next
158
- if ($ctx === null || $ctx === void 0 ? void 0 : $ctx.isDone()) {
123
+ if ($ctx.isDone()) {
159
124
  return;
160
125
  }
126
+ $ctx.error = null;
127
+ const metadata = $ctx.handlerMetadata;
161
128
  // set headers each times that an endpoint is called
162
- if (metadata.type === HandlerType.ENDPOINT) {
163
- this.setHeaders($ctx);
129
+ if (metadata.isEndpoint()) {
130
+ this.setResponseHeaders($ctx);
164
131
  }
165
132
  // call returned middleware
166
- if (isFunction(data) && !isStream(data)) {
167
- return this.callReturnedMiddleware(data, $ctx, next);
133
+ if (isFunction($ctx.data) && !isStream($ctx.data)) {
134
+ return promisify($ctx.data)($ctx.getRequest(), $ctx.getResponse());
168
135
  }
169
136
  if (metadata.isFinal()) {
170
- return this.flush(data, $ctx);
171
- }
172
- return this.next(requestOptions);
173
- }
174
- /**
175
- * Call the returned middleware by the handler.
176
- * @param middleware
177
- * @param ctx
178
- * @param next
179
- * @protected
180
- */
181
- callReturnedMiddleware(middleware, ctx, next) {
182
- return middleware(ctx.getRequest(), ctx.getResponse(), next);
183
- }
184
- /**
185
- * create Raw handler
186
- * @param metadata
187
- */
188
- createRawHandler(metadata) {
189
- if ([HandlerType.RAW_ERR_FN, HandlerType.RAW_FN].includes(metadata.type)) {
190
- return metadata.handler;
137
+ return this.flush($ctx);
191
138
  }
192
- const handler = this.compileHandler(metadata);
193
- return async (request, response, next) => {
194
- return handler({
195
- next,
196
- $ctx: request.$ctx
197
- });
198
- };
199
- }
200
- /**
201
- * Set response headers
202
- * @param ctx
203
- * @protected
204
- */
205
- setHeaders(ctx) {
206
- return setResponseHeaders(ctx);
207
- }
208
- next(requestOptions) {
209
- const { $ctx, next } = requestOptions;
210
- return !$ctx.response.isDone() && next && next();
211
- }
212
- getDefaultArgs(metadata) {
213
- return async (scope) => [
214
- metadata.hasErrorParam && scope.err,
215
- scope.$ctx.request.request,
216
- scope.$ctx.response.response,
217
- metadata.hasNextFunction && scope.next
218
- ].filter(Boolean);
219
139
  }
220
140
  };
221
141
  __decorate([
222
142
  Inject(),
223
143
  __metadata("design:type", PlatformResponseFilter)
224
144
  ], PlatformHandler.prototype, "responseFilter", void 0);
145
+ __decorate([
146
+ Inject(),
147
+ __metadata("design:type", PlatformRouters)
148
+ ], PlatformHandler.prototype, "platformRouters", void 0);
149
+ __decorate([
150
+ Inject(),
151
+ __metadata("design:type", PlatformParams)
152
+ ], PlatformHandler.prototype, "platformParams", void 0);
225
153
  PlatformHandler = __decorate([
226
154
  Injectable({
227
155
  scope: ProviderScope.SINGLETON
228
- }),
229
- __metadata("design:paramtypes", [InjectorService, PlatformParams])
156
+ })
230
157
  ], PlatformHandler);
231
158
  export { PlatformHandler };
232
159
  //# sourceMappingURL=PlatformHandler.js.map