@tsed/common 7.0.0-beta.2 → 7.0.0-beta.20

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 (305) hide show
  1. package/coverage.json +6 -0
  2. package/jest.config.js +9 -0
  3. package/lib/cjs/builder/PlatformBuilder.js +94 -65
  4. package/lib/cjs/builder/PlatformBuilder.js.map +1 -1
  5. package/lib/cjs/config/interfaces/PlatformLoggerSettings.js.map +1 -1
  6. package/lib/cjs/config/interfaces/PlatformMulterSettings.js.map +1 -1
  7. package/lib/cjs/config/services/PlatformConfiguration.js +2 -66
  8. package/lib/cjs/config/services/PlatformConfiguration.js.map +1 -1
  9. package/lib/cjs/decorators/multer/multipartFile.js.map +1 -1
  10. package/lib/cjs/decorators/params/error.js +1 -1
  11. package/lib/cjs/decorators/params/error.js.map +1 -1
  12. package/lib/cjs/domain/AnyToPromiseWithCtx.js +4 -5
  13. package/lib/cjs/domain/AnyToPromiseWithCtx.js.map +1 -1
  14. package/lib/cjs/domain/EndpointMetadata.js +2 -66
  15. package/lib/cjs/domain/EndpointMetadata.js.map +1 -1
  16. package/lib/cjs/domain/PlatformContext.js +46 -38
  17. package/lib/cjs/domain/PlatformContext.js.map +1 -1
  18. package/lib/cjs/domain/PlatformRouteDetails.js +0 -21
  19. package/lib/cjs/domain/PlatformRouteDetails.js.map +1 -1
  20. package/lib/cjs/exports.js +32 -0
  21. package/lib/cjs/exports.js.map +1 -0
  22. package/lib/cjs/index.js +58 -30
  23. package/lib/cjs/index.js.map +1 -1
  24. package/lib/cjs/interfaces/PlatformRouteOptions.js.map +1 -1
  25. package/lib/cjs/interfaces/Route.js.map +1 -1
  26. package/lib/cjs/middlewares/PlatformAcceptMimesMiddleware.js +10 -9
  27. package/lib/cjs/middlewares/PlatformAcceptMimesMiddleware.js.map +1 -1
  28. package/lib/cjs/middlewares/PlatformMulterMiddleware.js +27 -13
  29. package/lib/cjs/middlewares/PlatformMulterMiddleware.js.map +1 -1
  30. package/lib/cjs/package.json +3 -0
  31. package/lib/cjs/services/FakeAdapter.js +49 -0
  32. package/lib/cjs/services/FakeAdapter.js.map +1 -0
  33. package/lib/cjs/services/Platform.js +33 -68
  34. package/lib/cjs/services/Platform.js.map +1 -1
  35. package/lib/cjs/services/PlatformAdapter.js +24 -0
  36. package/lib/cjs/services/PlatformAdapter.js.map +1 -0
  37. package/lib/cjs/services/PlatformApplication.js +26 -14
  38. package/lib/cjs/services/PlatformApplication.js.map +1 -1
  39. package/lib/cjs/services/PlatformHandler.js +115 -172
  40. package/lib/cjs/services/PlatformHandler.js.map +1 -1
  41. package/lib/cjs/services/PlatformMiddlewaresChain.js +46 -19
  42. package/lib/cjs/services/PlatformMiddlewaresChain.js.map +1 -1
  43. package/lib/cjs/services/PlatformRequest.js +17 -13
  44. package/lib/cjs/services/PlatformRequest.js.map +1 -1
  45. package/lib/cjs/services/PlatformResponse.js +20 -23
  46. package/lib/cjs/services/PlatformResponse.js.map +1 -1
  47. package/lib/cjs/services/PlatformTest.js +31 -10
  48. package/lib/cjs/services/PlatformTest.js.map +1 -1
  49. package/lib/cjs/utils/createContext.js +32 -21
  50. package/lib/cjs/utils/createContext.js.map +1 -1
  51. package/lib/cjs/utils/createHttpServer.js +6 -11
  52. package/lib/cjs/utils/createHttpServer.js.map +1 -1
  53. package/lib/cjs/utils/createHttpsServer.js +6 -11
  54. package/lib/cjs/utils/createHttpsServer.js.map +1 -1
  55. package/lib/cjs/utils/createInjector.js +16 -10
  56. package/lib/cjs/utils/createInjector.js.map +1 -1
  57. package/lib/cjs/utils/ensureContext.js +16 -0
  58. package/lib/cjs/utils/ensureContext.js.map +1 -0
  59. package/lib/cjs/utils/getConfiguration.js +6 -12
  60. package/lib/cjs/utils/getConfiguration.js.map +1 -1
  61. package/lib/cjs/utils/printRoutes.js +10 -8
  62. package/lib/cjs/utils/printRoutes.js.map +1 -1
  63. package/lib/cjs/utils/setResponseHeaders.js +8 -14
  64. package/lib/cjs/utils/setResponseHeaders.js.map +1 -1
  65. package/lib/esm/builder/PlatformBuilder.js +74 -68
  66. package/lib/esm/builder/PlatformBuilder.js.map +1 -1
  67. package/lib/esm/config/interfaces/PlatformLoggerSettings.js.map +1 -1
  68. package/lib/esm/config/interfaces/PlatformMulterSettings.js.map +1 -1
  69. package/lib/esm/config/services/PlatformConfiguration.js +0 -64
  70. package/lib/esm/config/services/PlatformConfiguration.js.map +1 -1
  71. package/lib/esm/decorators/multer/multerFileSize.js +1 -1
  72. package/lib/esm/decorators/multer/multerOptions.js +1 -1
  73. package/lib/esm/decorators/multer/multipartFile.js +1 -1
  74. package/lib/esm/decorators/multer/multipartFile.js.map +1 -1
  75. package/lib/esm/decorators/params/error.js +1 -1
  76. package/lib/esm/decorators/params/error.js.map +1 -1
  77. package/lib/esm/domain/AnyToPromiseWithCtx.js +4 -5
  78. package/lib/esm/domain/AnyToPromiseWithCtx.js.map +1 -1
  79. package/lib/esm/domain/EndpointMetadata.js +1 -66
  80. package/lib/esm/domain/EndpointMetadata.js.map +1 -1
  81. package/lib/esm/domain/PlatformContext.js +49 -41
  82. package/lib/esm/domain/PlatformContext.js.map +1 -1
  83. package/lib/esm/domain/PlatformRouteDetails.js +1 -19
  84. package/lib/esm/domain/PlatformRouteDetails.js.map +1 -1
  85. package/lib/esm/exports.js +14 -0
  86. package/lib/esm/exports.js.map +1 -0
  87. package/lib/esm/index.js +58 -27
  88. package/lib/esm/index.js.map +1 -1
  89. package/lib/esm/interfaces/PlatformRouteOptions.js.map +1 -1
  90. package/lib/esm/interfaces/Route.js.map +1 -1
  91. package/lib/esm/middlewares/PlatformAcceptMimesMiddleware.js +2 -1
  92. package/lib/esm/middlewares/PlatformAcceptMimesMiddleware.js.map +1 -1
  93. package/lib/esm/middlewares/PlatformMulterMiddleware.js +18 -5
  94. package/lib/esm/middlewares/PlatformMulterMiddleware.js.map +1 -1
  95. package/lib/esm/package.json +3 -0
  96. package/lib/esm/services/FakeAdapter.js +45 -0
  97. package/lib/esm/services/FakeAdapter.js.map +1 -0
  98. package/lib/esm/services/Platform.js +34 -69
  99. package/lib/esm/services/Platform.js.map +1 -1
  100. package/lib/esm/services/PlatformAdapter.js +20 -0
  101. package/lib/esm/services/PlatformAdapter.js.map +1 -0
  102. package/lib/esm/services/PlatformApplication.js +27 -15
  103. package/lib/esm/services/PlatformApplication.js.map +1 -1
  104. package/lib/esm/services/PlatformHandler.js +116 -173
  105. package/lib/esm/services/PlatformHandler.js.map +1 -1
  106. package/lib/esm/services/PlatformMiddlewaresChain.js +49 -22
  107. package/lib/esm/services/PlatformMiddlewaresChain.js.map +1 -1
  108. package/lib/esm/services/PlatformRequest.js +16 -12
  109. package/lib/esm/services/PlatformRequest.js.map +1 -1
  110. package/lib/esm/services/PlatformResponse.js +19 -22
  111. package/lib/esm/services/PlatformResponse.js.map +1 -1
  112. package/lib/esm/services/PlatformTest.js +34 -14
  113. package/lib/esm/services/PlatformTest.js.map +1 -1
  114. package/lib/esm/utils/createContext.js +33 -23
  115. package/lib/esm/utils/createContext.js.map +1 -1
  116. package/lib/esm/utils/createHttpServer.js +6 -11
  117. package/lib/esm/utils/createHttpServer.js.map +1 -1
  118. package/lib/esm/utils/createHttpsServer.js +6 -11
  119. package/lib/esm/utils/createHttpsServer.js.map +1 -1
  120. package/lib/esm/utils/createInjector.js +20 -14
  121. package/lib/esm/utils/createInjector.js.map +1 -1
  122. package/lib/esm/utils/ensureContext.js +12 -0
  123. package/lib/esm/utils/ensureContext.js.map +1 -0
  124. package/lib/esm/utils/getConfiguration.js +8 -14
  125. package/lib/esm/utils/getConfiguration.js.map +1 -1
  126. package/lib/esm/utils/printRoutes.js +10 -8
  127. package/lib/esm/utils/printRoutes.js.map +1 -1
  128. package/lib/esm/utils/setResponseHeaders.js +8 -14
  129. package/lib/esm/utils/setResponseHeaders.js.map +1 -1
  130. package/lib/types/builder/PlatformBuilder.d.ts +19 -17
  131. package/lib/types/config/interfaces/PlatformLoggerSettings.d.ts +2 -43
  132. package/lib/types/config/interfaces/PlatformMulterSettings.d.ts +6 -130
  133. package/lib/types/config/services/PlatformConfiguration.d.ts +6 -21
  134. package/lib/types/domain/AnyToPromiseWithCtx.d.ts +1 -9
  135. package/lib/types/domain/EndpointMetadata.d.ts +1 -50
  136. package/lib/types/domain/PlatformContext.d.ts +32 -18
  137. package/lib/types/domain/PlatformRouteDetails.d.ts +4 -20
  138. package/lib/types/exports.d.ts +13 -0
  139. package/lib/types/index.d.ts +56 -18
  140. package/lib/types/interfaces/PlatformRouteOptions.d.ts +1 -2
  141. package/lib/types/interfaces/Route.d.ts +2 -3
  142. package/lib/types/middlewares/PlatformMulterMiddleware.d.ts +8 -3
  143. package/lib/types/services/FakeAdapter.d.ts +31 -0
  144. package/lib/types/services/Platform.d.ts +9 -21
  145. package/lib/types/services/PlatformAdapter.d.ts +58 -0
  146. package/lib/types/services/PlatformApplication.d.ts +15 -7
  147. package/lib/types/services/PlatformHandler.d.ts +21 -59
  148. package/lib/types/services/PlatformMiddlewaresChain.d.ts +6 -3
  149. package/lib/types/services/PlatformRequest.d.ts +8 -10
  150. package/lib/types/services/PlatformResponse.d.ts +15 -19
  151. package/lib/types/services/PlatformTest.d.ts +5 -4
  152. package/lib/types/utils/createContext.d.ts +4 -1
  153. package/lib/types/utils/createHttpServer.d.ts +1 -1
  154. package/lib/types/utils/createHttpsServer.d.ts +3 -1
  155. package/lib/types/utils/createInjector.d.ts +5 -3
  156. package/lib/types/utils/ensureContext.d.ts +2 -0
  157. package/lib/types/utils/listenServer.d.ts +2 -0
  158. package/package.json +43 -26
  159. package/readme.md +52 -58
  160. package/tsconfig.compile.esm.json +2 -12
  161. package/lib/cjs/builder/PlatformControllerBuilder.js +0 -85
  162. package/lib/cjs/builder/PlatformControllerBuilder.js.map +0 -1
  163. package/lib/cjs/config/index.js +0 -7
  164. package/lib/cjs/config/index.js.map +0 -1
  165. package/lib/cjs/config/interfaces/EndpointDirectoriesSettings.js +0 -3
  166. package/lib/cjs/config/interfaces/EndpointDirectoriesSettings.js.map +0 -1
  167. package/lib/cjs/config/interfaces/index.js +0 -8
  168. package/lib/cjs/config/interfaces/index.js.map +0 -1
  169. package/lib/cjs/decorators/httpServer.js +0 -32
  170. package/lib/cjs/decorators/httpServer.js.map +0 -1
  171. package/lib/cjs/decorators/httpsServer.js +0 -32
  172. package/lib/cjs/decorators/httpsServer.js.map +0 -1
  173. package/lib/cjs/decorators/index.js +0 -21
  174. package/lib/cjs/decorators/index.js.map +0 -1
  175. package/lib/cjs/decorators/method/acceptMime.js +0 -28
  176. package/lib/cjs/decorators/method/acceptMime.js.map +0 -1
  177. package/lib/cjs/decorators/method/endpointFn.js +0 -15
  178. package/lib/cjs/decorators/method/endpointFn.js.map +0 -1
  179. package/lib/cjs/decorators/method/location.js +0 -6
  180. package/lib/cjs/decorators/method/location.js.map +0 -1
  181. package/lib/cjs/decorators/method/redirect.js +0 -6
  182. package/lib/cjs/decorators/method/redirect.js.map +0 -1
  183. package/lib/cjs/decorators/method/route.js +0 -13
  184. package/lib/cjs/decorators/method/route.js.map +0 -1
  185. package/lib/cjs/decorators/method/view.js +0 -34
  186. package/lib/cjs/decorators/method/view.js.map +0 -1
  187. package/lib/cjs/decorators/multer/index.js +0 -7
  188. package/lib/cjs/decorators/multer/index.js.map +0 -1
  189. package/lib/cjs/domain/ControllerProvider.js +0 -51
  190. package/lib/cjs/domain/ControllerProvider.js.map +0 -1
  191. package/lib/cjs/domain/HandlerMetadata.js +0 -57
  192. package/lib/cjs/domain/HandlerMetadata.js.map +0 -1
  193. package/lib/cjs/domain/index.js +0 -10
  194. package/lib/cjs/domain/index.js.map +0 -1
  195. package/lib/cjs/interfaces/PathType.js +0 -3
  196. package/lib/cjs/interfaces/PathType.js.map +0 -1
  197. package/lib/cjs/interfaces/PlatformAdapter.js +0 -3
  198. package/lib/cjs/interfaces/PlatformAdapter.js.map +0 -1
  199. package/lib/cjs/interfaces/index.js +0 -21
  200. package/lib/cjs/interfaces/index.js.map +0 -1
  201. package/lib/cjs/middlewares/__mock__/FakeCtrl.js +0 -12
  202. package/lib/cjs/middlewares/__mock__/FakeCtrl.js.map +0 -1
  203. package/lib/cjs/middlewares/bindEndpointMiddleware.js +0 -13
  204. package/lib/cjs/middlewares/bindEndpointMiddleware.js.map +0 -1
  205. package/lib/cjs/middlewares/index.js +0 -7
  206. package/lib/cjs/middlewares/index.js.map +0 -1
  207. package/lib/cjs/services/FakeRawDriver.js +0 -26
  208. package/lib/cjs/services/FakeRawDriver.js.map +0 -1
  209. package/lib/cjs/services/PlatformRouter.js +0 -126
  210. package/lib/cjs/services/PlatformRouter.js.map +0 -1
  211. package/lib/cjs/services/index.js +0 -12
  212. package/lib/cjs/services/index.js.map +0 -1
  213. package/lib/cjs/utils/createHandlerMetadata.js +0 -49
  214. package/lib/cjs/utils/createHandlerMetadata.js.map +0 -1
  215. package/lib/cjs/utils/index.js +0 -11
  216. package/lib/cjs/utils/index.js.map +0 -1
  217. package/lib/cjs/utils/useCtxHandler.js +0 -15
  218. package/lib/cjs/utils/useCtxHandler.js.map +0 -1
  219. package/lib/esm/builder/PlatformControllerBuilder.js +0 -79
  220. package/lib/esm/builder/PlatformControllerBuilder.js.map +0 -1
  221. package/lib/esm/config/index.js +0 -4
  222. package/lib/esm/config/index.js.map +0 -1
  223. package/lib/esm/config/interfaces/EndpointDirectoriesSettings.js +0 -2
  224. package/lib/esm/config/interfaces/EndpointDirectoriesSettings.js.map +0 -1
  225. package/lib/esm/config/interfaces/index.js +0 -5
  226. package/lib/esm/config/interfaces/index.js.map +0 -1
  227. package/lib/esm/decorators/httpServer.js +0 -28
  228. package/lib/esm/decorators/httpServer.js.map +0 -1
  229. package/lib/esm/decorators/httpsServer.js +0 -28
  230. package/lib/esm/decorators/httpsServer.js.map +0 -1
  231. package/lib/esm/decorators/index.js +0 -18
  232. package/lib/esm/decorators/index.js.map +0 -1
  233. package/lib/esm/decorators/method/acceptMime.js +0 -24
  234. package/lib/esm/decorators/method/acceptMime.js.map +0 -1
  235. package/lib/esm/decorators/method/endpointFn.js +0 -11
  236. package/lib/esm/decorators/method/endpointFn.js.map +0 -1
  237. package/lib/esm/decorators/method/location.js +0 -2
  238. package/lib/esm/decorators/method/location.js.map +0 -1
  239. package/lib/esm/decorators/method/redirect.js +0 -2
  240. package/lib/esm/decorators/method/redirect.js.map +0 -1
  241. package/lib/esm/decorators/method/route.js +0 -2
  242. package/lib/esm/decorators/method/route.js.map +0 -1
  243. package/lib/esm/decorators/method/view.js +0 -30
  244. package/lib/esm/decorators/method/view.js.map +0 -1
  245. package/lib/esm/decorators/multer/index.js +0 -4
  246. package/lib/esm/decorators/multer/index.js.map +0 -1
  247. package/lib/esm/domain/ControllerProvider.js +0 -47
  248. package/lib/esm/domain/ControllerProvider.js.map +0 -1
  249. package/lib/esm/domain/HandlerMetadata.js +0 -53
  250. package/lib/esm/domain/HandlerMetadata.js.map +0 -1
  251. package/lib/esm/domain/index.js +0 -7
  252. package/lib/esm/domain/index.js.map +0 -1
  253. package/lib/esm/interfaces/PathType.js +0 -2
  254. package/lib/esm/interfaces/PathType.js.map +0 -1
  255. package/lib/esm/interfaces/PlatformAdapter.js +0 -2
  256. package/lib/esm/interfaces/PlatformAdapter.js.map +0 -1
  257. package/lib/esm/interfaces/index.js +0 -18
  258. package/lib/esm/interfaces/index.js.map +0 -1
  259. package/lib/esm/middlewares/__mock__/FakeCtrl.js +0 -9
  260. package/lib/esm/middlewares/__mock__/FakeCtrl.js.map +0 -1
  261. package/lib/esm/middlewares/bindEndpointMiddleware.js +0 -9
  262. package/lib/esm/middlewares/bindEndpointMiddleware.js.map +0 -1
  263. package/lib/esm/middlewares/index.js +0 -4
  264. package/lib/esm/middlewares/index.js.map +0 -1
  265. package/lib/esm/services/FakeRawDriver.js +0 -22
  266. package/lib/esm/services/FakeRawDriver.js.map +0 -1
  267. package/lib/esm/services/PlatformRouter.js +0 -123
  268. package/lib/esm/services/PlatformRouter.js.map +0 -1
  269. package/lib/esm/services/index.js +0 -9
  270. package/lib/esm/services/index.js.map +0 -1
  271. package/lib/esm/utils/createHandlerMetadata.js +0 -45
  272. package/lib/esm/utils/createHandlerMetadata.js.map +0 -1
  273. package/lib/esm/utils/index.js +0 -8
  274. package/lib/esm/utils/index.js.map +0 -1
  275. package/lib/esm/utils/useCtxHandler.js +0 -11
  276. package/lib/esm/utils/useCtxHandler.js.map +0 -1
  277. package/lib/types/builder/PlatformControllerBuilder.d.ts +0 -17
  278. package/lib/types/config/index.d.ts +0 -3
  279. package/lib/types/config/interfaces/EndpointDirectoriesSettings.d.ts +0 -3
  280. package/lib/types/config/interfaces/index.d.ts +0 -97
  281. package/lib/types/decorators/httpServer.d.ts +0 -28
  282. package/lib/types/decorators/httpsServer.d.ts +0 -28
  283. package/lib/types/decorators/index.d.ts +0 -14
  284. package/lib/types/decorators/method/acceptMime.d.ts +0 -19
  285. package/lib/types/decorators/method/endpointFn.d.ts +0 -9
  286. package/lib/types/decorators/method/location.d.ts +0 -1
  287. package/lib/types/decorators/method/redirect.d.ts +0 -1
  288. package/lib/types/decorators/method/route.d.ts +0 -1
  289. package/lib/types/decorators/method/view.d.ts +0 -25
  290. package/lib/types/decorators/multer/index.d.ts +0 -3
  291. package/lib/types/domain/ControllerProvider.d.ts +0 -24
  292. package/lib/types/domain/HandlerMetadata.d.ts +0 -32
  293. package/lib/types/domain/index.d.ts +0 -6
  294. package/lib/types/interfaces/PathType.d.ts +0 -1
  295. package/lib/types/interfaces/PlatformAdapter.d.ts +0 -19
  296. package/lib/types/interfaces/index.d.ts +0 -17
  297. package/lib/types/middlewares/__mock__/FakeCtrl.d.ts +0 -2
  298. package/lib/types/middlewares/bindEndpointMiddleware.d.ts +0 -5
  299. package/lib/types/middlewares/index.d.ts +0 -3
  300. package/lib/types/services/FakeRawDriver.d.ts +0 -15
  301. package/lib/types/services/PlatformRouter.d.ts +0 -48
  302. package/lib/types/services/index.d.ts +0 -8
  303. package/lib/types/utils/createHandlerMetadata.d.ts +0 -8
  304. package/lib/types/utils/index.d.ts +0 -7
  305. package/lib/types/utils/useCtxHandler.d.ts +0 -13
@@ -5,229 +5,172 @@ const tslib_1 = require("tslib");
5
5
  const core_1 = require("@tsed/core");
6
6
  const di_1 = require("@tsed/di");
7
7
  const logger_1 = require("@tsed/logger");
8
+ const platform_exceptions_1 = require("@tsed/platform-exceptions");
8
9
  const platform_params_1 = require("@tsed/platform-params");
9
10
  const platform_response_filter_1 = require("@tsed/platform-response-filter");
11
+ const platform_router_1 = require("@tsed/platform-router");
12
+ const util_1 = require("util");
10
13
  const AnyToPromiseWithCtx_1 = require("../domain/AnyToPromiseWithCtx");
11
- const HandlerMetadata_1 = require("../domain/HandlerMetadata");
12
- const HandlerType_1 = require("../interfaces/HandlerType");
13
- const createHandlerMetadata_1 = require("../utils/createHandlerMetadata");
14
14
  const setResponseHeaders_1 = require("../utils/setResponseHeaders");
15
+ const PlatformApplication_1 = require("./PlatformApplication");
16
+ const PlatformMiddlewaresChain_1 = require("./PlatformMiddlewaresChain");
15
17
  /**
16
18
  * Platform Handler abstraction layer. Wrap original class method to a pure platform handler (Express, Koa, etc...).
17
19
  * @platform
18
20
  */
19
21
  let PlatformHandler = class PlatformHandler {
20
- constructor(injector, params) {
21
- this.injector = injector;
22
- this.params = params;
22
+ platformRouters;
23
+ responseFilter;
24
+ platformParams;
25
+ platformExceptions;
26
+ platformApplication;
27
+ platformMiddlewaresChain;
28
+ constructor(platformRouters) {
29
+ this.platformRouters = platformRouters;
30
+ // configure the router module
31
+ platformRouters.hooks
32
+ .on("alterEndpointHandlers", (allMiddlewares, operationRoute) => {
33
+ allMiddlewares = this.platformMiddlewaresChain.get(allMiddlewares, operationRoute);
34
+ return [...allMiddlewares, (0, platform_router_1.useResponseHandler)(this.onFinish.bind(this))];
35
+ })
36
+ .on("alterHandler", (handlerMetadata) => {
37
+ return this.platformApplication.adapter.mapHandler(this.createHandler(handlerMetadata), handlerMetadata);
38
+ });
23
39
  }
24
- /**
25
- * Create a native middleware based on the given metadata and return an instance of AnyToPromiseWithCtx
26
- * @param input
27
- * @param options
28
- */
29
- createHandler(input, options = {}) {
30
- return this.createRawHandler((0, createHandlerMetadata_1.createHandlerMetadata)(this.injector, input, options));
40
+ createHandler(handlerMetadata) {
41
+ if (handlerMetadata.isInjectable()) {
42
+ const handler = this.platformParams.compileHandler(handlerMetadata);
43
+ return async ($ctx) => {
44
+ $ctx.handlerMetadata = handlerMetadata;
45
+ await (0, core_1.catchAsyncError)(() => this.onRequest(handler, $ctx));
46
+ return this.next($ctx);
47
+ };
48
+ }
49
+ return handlerMetadata.handler;
31
50
  }
32
51
  /**
33
- * Create injectable handler from the given provider
34
52
  * @param provider
35
53
  * @param propertyKey
36
54
  */
37
55
  createCustomHandler(provider, propertyKey) {
38
- const handler = this.compileHandler(new HandlerMetadata_1.HandlerMetadata({
39
- token: provider.provide,
40
- target: provider.useClass,
41
- type: HandlerType_1.HandlerType.CUSTOM,
42
- scope: provider.scope,
56
+ const metadata = new platform_router_1.PlatformHandlerMetadata({
57
+ provider,
58
+ type: platform_router_1.PlatformHandlerType.CUSTOM,
43
59
  propertyKey
44
- }));
45
- return async ($ctx, next) => handler({
46
- $ctx,
47
- next
48
60
  });
61
+ const handler = this.platformParams.compileHandler(metadata.store);
62
+ return async ($ctx) => {
63
+ $ctx.handlerMetadata = metadata;
64
+ // @ts-ignore
65
+ return this.onRequest(handler, $ctx);
66
+ };
49
67
  }
50
68
  /**
51
69
  * Send the response to the consumer.
52
- * @param data
53
- * @param ctx
54
70
  * @protected
71
+ * @param $ctx
55
72
  */
56
- async flush(data, ctx) {
57
- const { response } = ctx;
58
- if (!response.isDone()) {
59
- data = await this.responseFilter.serialize(data, ctx);
60
- data = await this.responseFilter.transform(data, ctx);
73
+ async flush($ctx) {
74
+ const { response } = $ctx;
75
+ if (!$ctx.isDone()) {
76
+ let data = await this.responseFilter.serialize($ctx.data, $ctx);
77
+ data = await this.responseFilter.transform(data, $ctx);
61
78
  response.body(data);
62
79
  }
63
80
  }
64
- compileHandler(metadata) {
65
- if (metadata.type === HandlerType_1.HandlerType.CTX_FN) {
66
- return async (scope) => {
67
- // @ts-ignore
68
- return this.onCtxRequest({
69
- ...scope,
70
- handler: () => metadata.handler(scope.$ctx),
71
- metadata
72
- });
73
- };
81
+ /**
82
+ * @param $ctx
83
+ */
84
+ next($ctx) {
85
+ if ((0, core_1.isStream)($ctx.data) || $ctx.isDone()) {
86
+ return;
74
87
  }
75
- const promise = this.params.compileHandler({
76
- token: metadata.token,
77
- propertyKey: metadata.propertyKey,
78
- getCustomArgs: metadata.injectable ? undefined : this.getDefaultArgs(metadata)
79
- });
80
- return async (scope) => {
81
- const handler = await promise;
82
- // @ts-ignore
83
- return this.onRequest({
84
- ...scope,
85
- handler,
86
- metadata
87
- });
88
- };
89
- }
90
- async onCtxRequest(requestOptions) {
91
- await requestOptions.handler(requestOptions);
92
- return this.next(requestOptions);
88
+ return $ctx.error ? $ctx.next($ctx.error) : $ctx.next();
93
89
  }
94
90
  /**
95
91
  * Call handler when a request his handle
96
- * @param requestOptions
97
92
  */
98
- async onRequest(requestOptions) {
99
- const { $ctx, metadata, err, handler } = requestOptions;
100
- // istanbul ignore next
101
- if (!$ctx || $ctx.isDone()) {
93
+ async onRequest(handler, $ctx) {
94
+ if ($ctx.isDone()) {
102
95
  logger_1.$log.error({
103
96
  name: "HEADERS_SENT",
104
- message: `An endpoint is called but the response is already send to the client. The call comes from the handler: ${metadata.toString()}`
97
+ message: `An endpoint is called but the response is already send to the client. The call comes from the handler: ${$ctx.handlerMetadata.toString()}`
105
98
  });
106
99
  return;
107
100
  }
108
- const resolver = new AnyToPromiseWithCtx_1.AnyToPromiseWithCtx({ $ctx, err });
109
- return this.injector.runInContext($ctx, async () => {
110
- try {
111
- const { state, data, status, headers } = await resolver.call(handler);
112
- if (state === core_1.AnyToPromiseStatus.RESOLVED) {
113
- if (status) {
114
- $ctx.response.status(status);
101
+ if (($ctx.error instanceof Error && !$ctx.handlerMetadata.hasErrorParam) || ($ctx.handlerMetadata.hasErrorParam && !$ctx.error)) {
102
+ return;
103
+ }
104
+ try {
105
+ const { handlerMetadata } = $ctx;
106
+ if (handlerMetadata.type === platform_router_1.PlatformHandlerType.CTX_FN) {
107
+ return await handler({ $ctx });
108
+ }
109
+ const resolver = new AnyToPromiseWithCtx_1.AnyToPromiseWithCtx($ctx);
110
+ const { state, type, data, status, headers } = await resolver.call(handler);
111
+ // Note: restore previous handler metadata (for OIDC)
112
+ $ctx.handlerMetadata = handlerMetadata;
113
+ if (state === core_1.AnyToPromiseStatus.RESOLVED) {
114
+ if (status) {
115
+ $ctx.response.status(status);
116
+ }
117
+ if (headers) {
118
+ $ctx.response.setHeaders(headers);
119
+ }
120
+ if (data !== undefined) {
121
+ $ctx.data = data;
122
+ }
123
+ if (!$ctx.isDone()) {
124
+ $ctx.error = null;
125
+ // set headers each times that an endpoint is called
126
+ if (handlerMetadata.isEndpoint()) {
127
+ (0, setResponseHeaders_1.setResponseHeaders)($ctx);
115
128
  }
116
- if (headers) {
117
- $ctx.response.setHeaders(headers);
129
+ // call returned middleware
130
+ if ((0, core_1.isFunction)($ctx.data) && !(0, core_1.isStream)($ctx.data)) {
131
+ return (0, util_1.promisify)($ctx.data)($ctx.getRequest(), $ctx.getResponse());
118
132
  }
119
- if (data !== undefined) {
120
- $ctx.data = data;
133
+ if (type === core_1.AnyToPromiseResponseTypes.STREAM) {
134
+ return this.flush($ctx);
121
135
  }
122
- // Can be canceled by the handler itself
123
- return await this.onSuccess($ctx.data, requestOptions);
124
136
  }
125
137
  }
126
- catch (er) {
127
- return this.onError(er, requestOptions);
128
- }
129
- });
130
- }
131
- async onError(er, requestOptions) {
132
- const { next, $ctx, metadata } = requestOptions;
133
- $ctx.data = er;
134
- if (!next) {
135
- throw er;
136
- }
137
- // istanbul ignore next
138
- if (!$ctx || ($ctx === null || $ctx === void 0 ? void 0 : $ctx.isDone())) {
139
- logger_1.$log.warn({
140
- name: "HEADERS_SENT",
141
- message: `An error was caught after the headers were sent to the client. The error comes from the handler: ${metadata.toString()}`,
142
- stack: er.stack,
143
- origin: er,
144
- request_id: $ctx.id
145
- });
146
- return;
147
- }
148
- return next && next(er);
149
- }
150
- /**
151
- * Manage success scenario
152
- * @param data
153
- * @param requestOptions
154
- * @protected
155
- */
156
- async onSuccess(data, requestOptions) {
157
- const { metadata, $ctx, next } = requestOptions;
158
- // istanbul ignore next
159
- if ($ctx === null || $ctx === void 0 ? void 0 : $ctx.isDone()) {
160
- return;
161
138
  }
162
- // set headers each times that an endpoint is called
163
- if (metadata.type === HandlerType_1.HandlerType.ENDPOINT) {
164
- this.setHeaders($ctx);
165
- }
166
- // call returned middleware
167
- if ((0, core_1.isFunction)(data) && !(0, core_1.isStream)(data)) {
168
- return this.callReturnedMiddleware(data, $ctx, next);
169
- }
170
- if (metadata.isFinal()) {
171
- return this.flush(data, $ctx);
172
- }
173
- return this.next(requestOptions);
174
- }
175
- /**
176
- * Call the returned middleware by the handler.
177
- * @param middleware
178
- * @param ctx
179
- * @param next
180
- * @protected
181
- */
182
- callReturnedMiddleware(middleware, ctx, next) {
183
- return middleware(ctx.getRequest(), ctx.getResponse(), next);
184
- }
185
- /**
186
- * create Raw handler
187
- * @param metadata
188
- */
189
- createRawHandler(metadata) {
190
- if ([HandlerType_1.HandlerType.RAW_ERR_FN, HandlerType_1.HandlerType.RAW_FN].includes(metadata.type)) {
191
- return metadata.handler;
139
+ catch (error) {
140
+ $ctx.error = error;
141
+ throw error;
192
142
  }
193
- const handler = this.compileHandler(metadata);
194
- return async (request, response, next) => {
195
- return handler({
196
- next,
197
- $ctx: request.$ctx
198
- });
199
- };
200
- }
201
- /**
202
- * Set response headers
203
- * @param ctx
204
- * @protected
205
- */
206
- setHeaders(ctx) {
207
- return (0, setResponseHeaders_1.setResponseHeaders)(ctx);
208
143
  }
209
- next(requestOptions) {
210
- const { $ctx, next } = requestOptions;
211
- return !$ctx.response.isDone() && next && next();
212
- }
213
- getDefaultArgs(metadata) {
214
- return async (scope) => [
215
- metadata.hasErrorParam && scope.err,
216
- scope.$ctx.request.request,
217
- scope.$ctx.response.response,
218
- metadata.hasNextFunction && scope.next
219
- ].filter(Boolean);
144
+ async onFinish($ctx) {
145
+ $ctx.error = await (0, core_1.catchAsyncError)(() => this.flush($ctx));
146
+ return $ctx.error && this.platformExceptions.catch($ctx.error, $ctx);
220
147
  }
221
148
  };
222
- (0, tslib_1.__decorate)([
149
+ tslib_1.__decorate([
223
150
  (0, di_1.Inject)(),
224
- (0, tslib_1.__metadata)("design:type", platform_response_filter_1.PlatformResponseFilter)
151
+ tslib_1.__metadata("design:type", platform_response_filter_1.PlatformResponseFilter)
225
152
  ], PlatformHandler.prototype, "responseFilter", void 0);
226
- PlatformHandler = (0, tslib_1.__decorate)([
153
+ tslib_1.__decorate([
154
+ (0, di_1.Inject)(),
155
+ tslib_1.__metadata("design:type", platform_params_1.PlatformParams)
156
+ ], PlatformHandler.prototype, "platformParams", void 0);
157
+ tslib_1.__decorate([
158
+ (0, di_1.Inject)(),
159
+ tslib_1.__metadata("design:type", platform_exceptions_1.PlatformExceptions)
160
+ ], PlatformHandler.prototype, "platformExceptions", void 0);
161
+ tslib_1.__decorate([
162
+ (0, di_1.Inject)(),
163
+ tslib_1.__metadata("design:type", PlatformApplication_1.PlatformApplication)
164
+ ], PlatformHandler.prototype, "platformApplication", void 0);
165
+ tslib_1.__decorate([
166
+ (0, di_1.Inject)(),
167
+ tslib_1.__metadata("design:type", PlatformMiddlewaresChain_1.PlatformMiddlewaresChain)
168
+ ], PlatformHandler.prototype, "platformMiddlewaresChain", void 0);
169
+ PlatformHandler = tslib_1.__decorate([
227
170
  (0, di_1.Injectable)({
228
171
  scope: di_1.ProviderScope.SINGLETON
229
172
  }),
230
- (0, tslib_1.__metadata)("design:paramtypes", [di_1.InjectorService, platform_params_1.PlatformParams])
173
+ tslib_1.__metadata("design:paramtypes", [platform_router_1.PlatformRouters])
231
174
  ], PlatformHandler);
232
175
  exports.PlatformHandler = PlatformHandler;
233
176
  //# sourceMappingURL=PlatformHandler.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"PlatformHandler.js","sourceRoot":"","sources":["../../../src/services/PlatformHandler.ts"],"names":[],"mappings":";;;;AAAA,qCAAoE;AACpE,iCAAsF;AACtF,yCAAkC;AAClC,2DAAiF;AACjF,6EAAsE;AACtE,uEAAkE;AAElE,+DAA0D;AAE1D,2DAAsD;AAEtD,0EAAqE;AACrE,oEAA+D;AAY/D;;;GAGG;AAIH,IAAa,eAAe,GAA5B,MAAa,eAAe;IAI1B,YAAsB,QAAyB,EAAY,MAAsB;QAA3D,aAAQ,GAAR,QAAQ,CAAiB;QAAY,WAAM,GAAN,MAAM,CAAgB;IAAG,CAAC;IAErF;;;;OAIG;IACH,aAAa,CAAC,KAA+C,EAAE,UAAwC,EAAE;QACvG,OAAO,IAAI,CAAC,gBAAgB,CAAC,IAAA,6CAAqB,EAAC,IAAI,CAAC,QAAQ,EAAE,KAAK,EAAE,OAAO,CAAC,CAAC,CAAC;IACrF,CAAC;IAED;;;;OAIG;IACH,mBAAmB,CAAC,QAAkB,EAAE,WAAmB;QACzD,MAAM,OAAO,GAAG,IAAI,CAAC,cAAc,CACjC,IAAI,iCAAe,CAAC;YAClB,KAAK,EAAE,QAAQ,CAAC,OAAO;YACvB,MAAM,EAAE,QAAQ,CAAC,QAAQ;YACzB,IAAI,EAAE,yBAAW,CAAC,MAAM;YACxB,KAAK,EAAE,QAAQ,CAAC,KAAK;YACrB,WAAW;SACZ,CAAC,CACH,CAAC;QAEF,OAAO,KAAK,EAAE,IAAqB,EAAE,IAAU,EAAE,EAAE,CACjD,OAAO,CAAC;YACN,IAAI;YACJ,IAAI;SACL,CAAC,CAAC;IACP,CAAC;IAED;;;;;OAKG;IACH,KAAK,CAAC,KAAK,CAAC,IAAS,EAAE,GAAoB;QACzC,MAAM,EAAC,QAAQ,EAAC,GAAG,GAAG,CAAC;QAEvB,IAAI,CAAC,QAAQ,CAAC,MAAM,EAAE,EAAE;YACtB,IAAI,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AAC,OAAO,CAAC,OAAO;YAC1B,KAAK,CAAC,IAAI,CAAC,QAAQ,CAAC,QAAQ;YAC5B,QAAQ,CAAC,eAAe,IAAI,KAAK,CAAC,IAAI;SACvC,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;IACtB,CAAC;CACF,CAAA;AA/OC;IADC,IAAA,WAAM,GAAE;2CACiB,iDAAsB;uDAAC;AAFtC,eAAe;IAH3B,IAAA,eAAU,EAAC;QACV,KAAK,EAAE,kBAAa,CAAC,SAAS;KAC/B,CAAC;kDAKgC,oBAAe,EAAoB,gCAAc;GAJtE,eAAe,CAiP3B;AAjPY,0CAAe","sourcesContent":["import {AnyToPromiseStatus, isFunction, isStream} from \"@tsed/core\";\nimport {Inject, Injectable, InjectorService, Provider, ProviderScope} from \"@tsed/di\";\nimport {$log} from \"@tsed/logger\";\nimport {ArgScope, HandlerWithScope, PlatformParams} from \"@tsed/platform-params\";\nimport {PlatformResponseFilter} from \"@tsed/platform-response-filter\";\nimport {AnyToPromiseWithCtx} from \"../domain/AnyToPromiseWithCtx\";\nimport {EndpointMetadata} from \"../domain/EndpointMetadata\";\nimport {HandlerMetadata} from \"../domain/HandlerMetadata\";\nimport {PlatformContext} from \"../domain/PlatformContext\";\nimport {HandlerType} from \"../interfaces/HandlerType\";\nimport {PlatformRouteWithoutHandlers} from \"../interfaces/PlatformRouteOptions\";\nimport {createHandlerMetadata} from \"../utils/createHandlerMetadata\";\nimport {setResponseHeaders} from \"../utils/setResponseHeaders\";\n\nexport interface OnRequestOptions {\n $ctx: PlatformContext;\n metadata: HandlerMetadata;\n handler: HandlerWithScope;\n next?: any;\n err?: any;\n\n [key: string]: any;\n}\n\n/**\n * Platform Handler abstraction layer. Wrap original class method to a pure platform handler (Express, Koa, etc...).\n * @platform\n */\n@Injectable({\n scope: ProviderScope.SINGLETON\n})\nexport class PlatformHandler {\n @Inject()\n protected responseFilter: PlatformResponseFilter;\n\n constructor(protected injector: InjectorService, protected params: PlatformParams) {}\n\n /**\n * Create a native middleware based on the given metadata and return an instance of AnyToPromiseWithCtx\n * @param input\n * @param options\n */\n createHandler(input: EndpointMetadata | HandlerMetadata | any, options: PlatformRouteWithoutHandlers = {}) {\n return this.createRawHandler(createHandlerMetadata(this.injector, input, options));\n }\n\n /**\n * Create injectable handler from the given provider\n * @param provider\n * @param propertyKey\n */\n createCustomHandler(provider: Provider, propertyKey: string) {\n const handler = this.compileHandler(\n new HandlerMetadata({\n token: provider.provide,\n target: provider.useClass,\n type: HandlerType.CUSTOM,\n scope: provider.scope,\n propertyKey\n })\n );\n\n return async ($ctx: PlatformContext, next?: any) =>\n handler({\n $ctx,\n next\n });\n }\n\n /**\n * Send the response to the consumer.\n * @param data\n * @param ctx\n * @protected\n */\n async flush(data: any, ctx: PlatformContext) {\n const {response} = ctx;\n\n if (!response.isDone()) {\n data = await this.responseFilter.serialize(data, ctx);\n data = await this.responseFilter.transform(data, ctx);\n response.body(data);\n }\n }\n\n compileHandler(metadata: HandlerMetadata) {\n if (metadata.type === HandlerType.CTX_FN) {\n return async (scope: Record<string, any>) => {\n // @ts-ignore\n return this.onCtxRequest({\n ...scope,\n handler: () => metadata.handler(scope.$ctx),\n metadata\n });\n };\n }\n\n const promise = this.params.compileHandler<PlatformContext>({\n token: metadata.token,\n propertyKey: metadata.propertyKey,\n getCustomArgs: metadata.injectable ? undefined : this.getDefaultArgs(metadata)\n });\n\n return async (scope: Record<string, any>) => {\n const handler = await promise;\n // @ts-ignore\n return this.onRequest({\n ...scope,\n handler,\n metadata\n });\n };\n }\n\n protected async onCtxRequest(requestOptions: OnRequestOptions): Promise<any> {\n await requestOptions.handler(requestOptions);\n\n return this.next(requestOptions);\n }\n\n /**\n * Call handler when a request his handle\n * @param requestOptions\n */\n protected async onRequest(requestOptions: OnRequestOptions): Promise<any> {\n const {$ctx, metadata, err, handler} = requestOptions;\n // istanbul ignore next\n if (!$ctx || $ctx.isDone()) {\n $log.error({\n name: \"HEADERS_SENT\",\n message: `An endpoint is called but the response is already send to the client. The call comes from the handler: ${metadata.toString()}`\n });\n return;\n }\n\n const resolver = new AnyToPromiseWithCtx({$ctx, err});\n\n return this.injector.runInContext($ctx, async () => {\n try {\n const {state, data, status, headers} = await resolver.call(handler);\n\n if (state === AnyToPromiseStatus.RESOLVED) {\n if (status) {\n $ctx.response.status(status);\n }\n\n if (headers) {\n $ctx.response.setHeaders(headers);\n }\n\n if (data !== undefined) {\n $ctx.data = data;\n }\n\n // Can be canceled by the handler itself\n return await this.onSuccess($ctx.data, requestOptions);\n }\n } catch (er) {\n return this.onError(er, requestOptions);\n }\n });\n }\n\n protected async onError(er: Error, requestOptions: OnRequestOptions) {\n const {next, $ctx, metadata} = requestOptions;\n $ctx.data = er;\n\n if (!next) {\n throw er;\n }\n\n // istanbul ignore next\n if (!$ctx || $ctx?.isDone()) {\n $log.warn({\n name: \"HEADERS_SENT\",\n message: `An error was caught after the headers were sent to the client. The error comes from the handler: ${metadata.toString()}`,\n stack: er.stack,\n origin: er,\n request_id: $ctx.id\n });\n\n return;\n }\n\n return next && next(er);\n }\n\n /**\n * Manage success scenario\n * @param data\n * @param requestOptions\n * @protected\n */\n protected async onSuccess(data: any, requestOptions: OnRequestOptions) {\n const {metadata, $ctx, next} = requestOptions;\n\n // istanbul ignore next\n if ($ctx?.isDone()) {\n return;\n }\n\n // set headers each times that an endpoint is called\n if (metadata.type === HandlerType.ENDPOINT) {\n this.setHeaders($ctx);\n }\n\n // call returned middleware\n if (isFunction(data) && !isStream(data)) {\n return this.callReturnedMiddleware(data, $ctx, next);\n }\n\n if (metadata.isFinal()) {\n return this.flush(data, $ctx);\n }\n\n return this.next(requestOptions);\n }\n\n /**\n * Call the returned middleware by the handler.\n * @param middleware\n * @param ctx\n * @param next\n * @protected\n */\n protected callReturnedMiddleware(middleware: any, ctx: PlatformContext, next: any) {\n return middleware(ctx.getRequest(), ctx.getResponse(), next);\n }\n\n /**\n * create Raw handler\n * @param metadata\n */\n protected createRawHandler(metadata: HandlerMetadata) {\n if ([HandlerType.RAW_ERR_FN, HandlerType.RAW_FN].includes(metadata.type)) {\n return metadata.handler;\n }\n\n const handler = this.compileHandler(metadata);\n\n return async (request: any, response: any, next: any) => {\n return handler({\n next,\n $ctx: request.$ctx\n });\n };\n }\n\n /**\n * Set response headers\n * @param ctx\n * @protected\n */\n protected setHeaders(ctx: PlatformContext) {\n return setResponseHeaders(ctx);\n }\n\n protected next(requestOptions: OnRequestOptions) {\n const {$ctx, next} = requestOptions;\n\n return !$ctx.response.isDone() && next && next();\n }\n\n protected getDefaultArgs(metadata: HandlerMetadata) {\n return async (scope: ArgScope<PlatformContext>) =>\n [\n metadata.hasErrorParam && scope.err,\n scope.$ctx.request.request,\n scope.$ctx.response.response,\n metadata.hasNextFunction && scope.next\n ].filter(Boolean);\n }\n}\n"]}
1
+ {"version":3,"file":"PlatformHandler.js","sourceRoot":"","sources":["../../../src/services/PlatformHandler.ts"],"names":[],"mappings":";;;;AAAA,qCAAgH;AAChH,iCAAqE;AACrE,yCAAkC;AAClC,mEAA6D;AAC7D,2DAA6E;AAC7E,6EAAsE;AACtE,2DAAwH;AAExH,+BAA+B;AAC/B,uEAAkE;AAElE,oEAA+D;AAC/D,+DAA0D;AAC1D,yEAAoE;AAEpE;;;GAGG;AAIH,IAAa,eAAe,GAA5B,MAAa,eAAe;IAgBJ;IAdZ,cAAc,CAAyB;IAGvC,cAAc,CAAiB;IAG/B,kBAAkB,CAAqB;IAGvC,mBAAmB,CAAsB;IAGzC,wBAAwB,CAA2B;IAE7D,YAAsB,eAAgC;QAAhC,oBAAe,GAAf,eAAe,CAAiB;QACpD,8BAA8B;QAC9B,eAAe,CAAC,KAAK;aAClB,EAAE,CAAC,uBAAuB,EAAE,CAAC,cAAqB,EAAE,cAAkC,EAAE,EAAE;YACzF,cAAc,GAAG,IAAI,CAAC,wBAAwB,CAAC,GAAG,CAAC,cAAc,EAAE,cAAc,CAAC,CAAC;YAEnF,OAAO,CAAC,GAAG,cAAc,EAAE,IAAA,o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AK,gCAAyB,CAAC,MAAM,EAAE;wBAC7C,OAAO,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;qBACzB;iBACF;aACF;SACF;QAAC,OAAO,KAAK,EAAE;YACd,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;YAEnB,MAAM,KAAK,CAAC;SACb;IACH,CAAC;IAED,KAAK,CAAC,QAAQ,CAAC,IAAqB;QAClC,IAAI,CAAC,KAAK,GAAG,MAAM,IAAA,sBAAe,EAAC,GAAG,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC;QAE3D,OAAO,IAAI,CAAC,KAAK,IAAI,IAAI,CAAC,kBAAkB,CAAC,KAAK,CAAC,IAAI,CAAC,KAAK,EAAE,IAAI,CAAC,CAAC;IACvE,CAAC;CACF,CAAA;AAnKC;IADC,IAAA,WAAM,GAAE;sCACiB,iDAAsB;uDAAC;AAGjD;IADC,IAAA,WAAM,GAAE;sCACiB,gCAAc;uDAAC;AAGzC;IADC,IAAA,WAAM,GAAE;sCACqB,wCAAkB;2DAAC;AAGjD;IADC,IAAA,WAAM,GAAE;sCACsB,yCAAmB;4DAAC;AAGnD;IADC,IAAA,WAAM,GAAE;sCAC2B,mDAAwB;iEAAC;AAdlD,eAAe;IAH3B,IAAA,eAAU,EAAC;QACV,KAAK,EAAE,kBAAa,CAAC,SAAS;KAC/B,CAAC;6CAiBuC,iCAAe;GAhB3C,eAAe,CAqK3B;AArKY,0CAAe","sourcesContent":["import {AnyToPromiseResponseTypes, AnyToPromiseStatus, catchAsyncError, isFunction, isStream} from \"@tsed/core\";\nimport {Inject, Injectable, Provider, ProviderScope} from \"@tsed/di\";\nimport {$log} from \"@tsed/logger\";\nimport {PlatformExceptions} from \"@tsed/platform-exceptions\";\nimport {PlatformParams, PlatformParamsCallback} from \"@tsed/platform-params\";\nimport {PlatformResponseFilter} from \"@tsed/platform-response-filter\";\nimport {PlatformHandlerMetadata, PlatformHandlerType, PlatformRouters, useResponseHandler} from \"@tsed/platform-router\";\nimport {JsonOperationRoute} from \"@tsed/schema\";\nimport {promisify} from \"util\";\nimport {AnyToPromiseWithCtx} from \"../domain/AnyToPromiseWithCtx\";\nimport {PlatformContext} from \"../domain/PlatformContext\";\nimport {setResponseHeaders} from \"../utils/setResponseHeaders\";\nimport {PlatformApplication} from \"./PlatformApplication\";\nimport {PlatformMiddlewaresChain} from \"./PlatformMiddlewaresChain\";\n\n/**\n * Platform Handler abstraction layer. Wrap original class method to a pure platform handler (Express, Koa, etc...).\n * @platform\n */\n@Injectable({\n scope: ProviderScope.SINGLETON\n})\nexport class PlatformHandler {\n @Inject()\n protected responseFilter: PlatformResponseFilter;\n\n @Inject()\n protected platformParams: PlatformParams;\n\n @Inject()\n protected platformExceptions: PlatformExceptions;\n\n @Inject()\n protected platformApplication: PlatformApplication;\n\n @Inject()\n protected platformMiddlewaresChain: PlatformMiddlewaresChain;\n\n constructor(protected platformRouters: PlatformRouters) {\n // configure the router module\n platformRouters.hooks\n .on(\"alterEndpointHandlers\", (allMiddlewares: any[], operationRoute: JsonOperationRoute) => {\n allMiddlewares = this.platformMiddlewaresChain.get(allMiddlewares, operationRoute);\n\n return [...allMiddlewares, useResponseHandler(this.onFinish.bind(this))];\n })\n .on(\"alterHandler\", (handlerMetadata: PlatformHandlerMetadata) => {\n return this.platformApplication.adapter.mapHandler(this.createHandler(handlerMetadata), handlerMetadata);\n });\n }\n\n createHandler(handlerMetadata: PlatformHandlerMetadata): any {\n if (handlerMetadata.isInjectable()) {\n const handler = this.platformParams.compileHandler(handlerMetadata);\n\n return async ($ctx: PlatformContext) => {\n $ctx.handlerMetadata = handlerMetadata;\n\n await catchAsyncError(() => this.onRequest(handler, $ctx));\n\n return this.next($ctx);\n };\n }\n\n return handlerMetadata.handler;\n }\n\n /**\n * @param provider\n * @param propertyKey\n */\n createCustomHandler(provider: Provider, propertyKey: string) {\n const metadata = new PlatformHandlerMetadata({\n provider,\n type: PlatformHandlerType.CUSTOM,\n propertyKey\n });\n\n const handler = this.platformParams.compileHandler(metadata.store);\n\n return async ($ctx: PlatformContext) => {\n $ctx.handlerMetadata = metadata;\n\n // @ts-ignore\n return this.onRequest(handler, $ctx);\n };\n }\n\n /**\n * Send the response to the consumer.\n * @protected\n * @param $ctx\n */\n async flush($ctx: PlatformContext) {\n const {response} = $ctx;\n\n if (!$ctx.isDone()) {\n let data = await this.responseFilter.serialize($ctx.data, $ctx as any);\n data = await this.responseFilter.transform(data, $ctx as any);\n response.body(data);\n }\n }\n\n /**\n * @param $ctx\n */\n next($ctx: PlatformContext) {\n if (isStream($ctx.data) || $ctx.isDone()) {\n return;\n }\n\n return $ctx.error ? $ctx.next($ctx.error) : $ctx.next();\n }\n\n /**\n * Call handler when a request his handle\n */\n async onRequest(handler: PlatformParamsCallback, $ctx: PlatformContext): Promise<any> {\n if ($ctx.isDone()) {\n $log.error({\n name: \"HEADERS_SENT\",\n message: `An endpoint is called but the response is already send to the client. The call comes from the handler: ${$ctx.handlerMetadata.toString()}`\n });\n return;\n }\n\n if (($ctx.error instanceof Error && !$ctx.handlerMetadata.hasErrorParam) || ($ctx.handlerMetadata.hasErrorParam && !$ctx.error)) {\n return;\n }\n\n try {\n const {handlerMetadata} = $ctx;\n\n if (handlerMetadata.type === PlatformHandlerType.CTX_FN) {\n return await handler({$ctx});\n }\n\n const resolver = new AnyToPromiseWithCtx($ctx);\n\n const {state, type, data, status, headers} = await resolver.call(handler);\n\n // Note: restore previous handler metadata (for OIDC)\n $ctx.handlerMetadata = handlerMetadata;\n\n if (state === AnyToPromiseStatus.RESOLVED) {\n if (status) {\n $ctx.response.status(status);\n }\n\n if (headers) {\n $ctx.response.setHeaders(headers);\n }\n\n if (data !== undefined) {\n $ctx.data = data;\n }\n\n if (!$ctx.isDone()) {\n $ctx.error = null;\n\n // set headers each times that an endpoint is called\n if (handlerMetadata.isEndpoint()) {\n setResponseHeaders($ctx);\n }\n\n // call returned middleware\n if (isFunction($ctx.data) && !isStream($ctx.data)) {\n return promisify($ctx.data)($ctx.getRequest(), $ctx.getResponse());\n }\n\n if (type === AnyToPromiseResponseTypes.STREAM) {\n return this.flush($ctx);\n }\n }\n }\n } catch (error) {\n $ctx.error = error;\n\n throw error;\n }\n }\n\n async onFinish($ctx: PlatformContext) {\n $ctx.error = await catchAsyncError(() => this.flush($ctx));\n\n return $ctx.error && this.platformExceptions.catch($ctx.error, $ctx);\n }\n}\n"]}
@@ -2,32 +2,59 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.PlatformMiddlewaresChain = void 0;
4
4
  const tslib_1 = require("tslib");
5
+ const core_1 = require("@tsed/core");
5
6
  const di_1 = require("@tsed/di");
6
7
  const platform_params_1 = require("@tsed/platform-params");
7
- const bindEndpointMiddleware_1 = require("../middlewares/bindEndpointMiddleware");
8
+ const schema_1 = require("@tsed/schema");
8
9
  const PlatformAcceptMimesMiddleware_1 = require("../middlewares/PlatformAcceptMimesMiddleware");
9
10
  const PlatformMulterMiddleware_1 = require("../middlewares/PlatformMulterMiddleware");
10
- const useCtxHandler_1 = require("../utils/useCtxHandler");
11
+ const PlatformAdapter_1 = require("../services/PlatformAdapter");
11
12
  let PlatformMiddlewaresChain = class PlatformMiddlewaresChain {
12
- get(provider, operationRoute) {
13
- const { endpoint } = operationRoute;
14
- const { beforeMiddlewares, middlewares: mldwrs, afterMiddlewares } = endpoint;
15
- const { middlewares: { use, useAfter } } = provider;
16
- const hasFiles = [...operationRoute.endpoint.children.values()].find((item) => item.paramType === platform_params_1.ParamTypes.FILES);
17
- return []
18
- .concat((0, useCtxHandler_1.useCtxHandler)((0, bindEndpointMiddleware_1.bindEndpointMiddleware)(endpoint)))
19
- .concat(PlatformAcceptMimesMiddleware_1.PlatformAcceptMimesMiddleware)
20
- .concat(hasFiles && PlatformMulterMiddleware_1.PlatformMulterMiddleware)
21
- .concat(beforeMiddlewares) // Endpoint before-middlewares
22
- .concat(use) // Controller use-middlewares
23
- .concat(mldwrs) // Endpoint middlewares
24
- .concat(endpoint) // Endpoint metadata
25
- .concat(afterMiddlewares) // Endpoint after-middlewares
26
- .concat(useAfter) // Controller after middlewares (equivalent to afterEach)
27
- .filter((item) => !!item);
13
+ acceptMimes;
14
+ adapter;
15
+ get(allMiddlewares, operationRoute) {
16
+ const { ACCEPT_MIMES, FILE, RAW_BODY, BODY } = this.getParamTypes(allMiddlewares, operationRoute);
17
+ return [
18
+ ACCEPT_MIMES && PlatformAcceptMimesMiddleware_1.PlatformAcceptMimesMiddleware,
19
+ FILE && PlatformMulterMiddleware_1.PlatformMulterMiddleware,
20
+ !FILE && RAW_BODY && this.adapter.bodyParser("raw"),
21
+ !FILE && !RAW_BODY && BODY && this.adapter.bodyParser("json"),
22
+ !FILE && !RAW_BODY && BODY && this.adapter.bodyParser("urlencoded"),
23
+ ...allMiddlewares
24
+ ].filter(Boolean);
25
+ }
26
+ hasAcceptMimes(operationRoute) {
27
+ return operationRoute.endpoint.acceptMimes.length || this.acceptMimes.length;
28
+ }
29
+ getParamTypes(middlewares, operationRoute) {
30
+ return middlewares.filter(core_1.isClass).reduce((paramTypes, token) => {
31
+ if (token !== operationRoute.endpoint) {
32
+ const entity = schema_1.JsonEntityStore.fromMethod(token, "use");
33
+ if (entity.decoratorType === "method") {
34
+ const { FILE, RAW_BODY, BODY } = entity.getParamTypes();
35
+ paramTypes.FILE = paramTypes.FILE || FILE;
36
+ paramTypes.RAW_BODY = paramTypes.RAW_BODY || RAW_BODY;
37
+ paramTypes.BODY = paramTypes.BODY || BODY;
38
+ }
39
+ }
40
+ return paramTypes;
41
+ }, {
42
+ ACCEPT_MIMES: this.hasAcceptMimes(operationRoute),
43
+ FILE: operationRoute.has(platform_params_1.ParamTypes.FILES),
44
+ RAW_BODY: operationRoute.has(platform_params_1.ParamTypes.RAW_BODY),
45
+ BODY: operationRoute.has(platform_params_1.ParamTypes.BODY) || operationRoute.method === "ALL"
46
+ });
28
47
  }
29
48
  };
30
- PlatformMiddlewaresChain = (0, tslib_1.__decorate)([
49
+ tslib_1.__decorate([
50
+ (0, di_1.Constant)("acceptMimes", []),
51
+ tslib_1.__metadata("design:type", Array)
52
+ ], PlatformMiddlewaresChain.prototype, "acceptMimes", void 0);
53
+ tslib_1.__decorate([
54
+ (0, di_1.Inject)(),
55
+ tslib_1.__metadata("design:type", PlatformAdapter_1.PlatformAdapter)
56
+ ], PlatformMiddlewaresChain.prototype, "adapter", void 0);
57
+ PlatformMiddlewaresChain = tslib_1.__decorate([
31
58
  (0, di_1.Injectable)()
32
59
  ], PlatformMiddlewaresChain);
33
60
  exports.PlatformMiddlewaresChain = PlatformMiddlewaresChain;
@@ -1 +1 @@
1
- {"version":3,"file":"PlatformMiddlewaresChain.js","sourceRoot":"","sources":["../../../src/services/PlatformMiddlewaresChain.ts"],"names":[],"mappings":";;;;AAAA,iCAAoC;AACpC,2DAAgE;AAIhE,kFAA6E;AAC7E,gGAA2F;AAC3F,sFAAiF;AACjF,0DAAqD;AAGrD,IAAa,wBAAwB,GAArC,MAAa,wBAAwB;IACnC,GAAG,CAAC,QAA4B,EAAE,cAAoD;QACpF,MAAM,EAAC,QAAQ,EAAC,GAAG,cAAc,CAAC;QAClC,MAAM,EAAC,iBAAiB,EAAE,WAAW,EAAE,MAAM,EAAE,gBAAgB,EAAC,GAAG,QAAQ,CAAC;QAE5E,MAAM,EACJ,WAAW,EAAE,EAAC,GAAG,EAAE,QAAQ,EAAC,EAC7B,GAAG,QAAQ,CAAC;QAEb,MAAM,QAAQ,GAAG,CAAC,GAAG,cAAc,CAAC,QAAQ,CAAC,QAAQ,CAAC,MAAM,EAAE,CAAC,CAAC,IAAI,CAAC,CAAC,IAAmB,EAAE,EAAE,CAAC,IAAI,CAAC,SAAS,KAAK,4BAAU,CAAC,KAAK,CAAC,CAAC;QAEnI,OAAQ,EAAY;aACjB,MAAM,CAAC,IAAA,6BAAa,EAAC,IAAA,+CAAsB,EAAC,QAAQ,CAAC,CAAC,CAAC;aACvD,MAAM,CAAC,6DAA6B,CAAC;aACrC,MAAM,CAAC,QAAQ,IAAI,mDAAwB,CAAC;aAC5C,MAAM,CAAC,iBAAiB,CAAC,CAAC,8BAA8B;aACxD,MAAM,CAAC,GAAG,CAAC,CAAC,6BAA6B;aACzC,MAAM,CAAC,MAAM,CAAC,CAAC,uBAAuB;aACtC,MAAM,CAAC,QAAQ,CAAC,CAAC,oBAAoB;aACrC,MAAM,CAAC,gBAAgB,CAAC,CAAC,6BAA6B;aACtD,MAAM,CAAC,QAAQ,CAAC,CAAC,yDAAyD;aAC1E,MAAM,CAAC,CAAC,IAAS,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC;IACnC,CAAC;CACF,CAAA;AAvBY,wBAAwB;IADpC,IAAA,eAAU,GAAE;GACA,wBAAwB,CAuBpC;AAvBY,4DAAwB","sourcesContent":["import {Injectable} from \"@tsed/di\";\nimport {ParamMetadata, ParamTypes} from \"@tsed/platform-params\";\nimport {JsonOperationRoute} from \"@tsed/schema\";\nimport {ControllerProvider} from \"../domain/ControllerProvider\";\nimport {EndpointMetadata} from \"../domain/EndpointMetadata\";\nimport {bindEndpointMiddleware} from \"../middlewares/bindEndpointMiddleware\";\nimport {PlatformAcceptMimesMiddleware} from \"../middlewares/PlatformAcceptMimesMiddleware\";\nimport {PlatformMulterMiddleware} from \"../middlewares/PlatformMulterMiddleware\";\nimport {useCtxHandler} from \"../utils/useCtxHandler\";\n\n@Injectable()\nexport class PlatformMiddlewaresChain {\n get(provider: ControllerProvider, operationRoute: JsonOperationRoute<EndpointMetadata>) {\n const {endpoint} = operationRoute;\n const {beforeMiddlewares, middlewares: mldwrs, afterMiddlewares} = endpoint;\n\n const {\n middlewares: {use, useAfter}\n } = provider;\n\n const hasFiles = [...operationRoute.endpoint.children.values()].find((item: ParamMetadata) => item.paramType === ParamTypes.FILES);\n\n return ([] as any[])\n .concat(useCtxHandler(bindEndpointMiddleware(endpoint)))\n .concat(PlatformAcceptMimesMiddleware)\n .concat(hasFiles && PlatformMulterMiddleware)\n .concat(beforeMiddlewares) // Endpoint before-middlewares\n .concat(use) // Controller use-middlewares\n .concat(mldwrs) // Endpoint middlewares\n .concat(endpoint) // Endpoint metadata\n .concat(afterMiddlewares) // Endpoint after-middlewares\n .concat(useAfter) // Controller after middlewares (equivalent to afterEach)\n .filter((item: any) => !!item);\n }\n}\n"]}
1
+ {"version":3,"file":"PlatformMiddlewaresChain.js","sourceRoot":"","sources":["../../../src/services/PlatformMiddlewaresChain.ts"],"names":[],"mappings":";;;;AAAA,qCAAmC;AACnC,iCAAsD;AACtD,2DAAiD;AACjD,yCAAiE;AACjE,gGAA2F;AAC3F,sFAAiF;AACjF,iEAA4D;AAG5D,IAAa,wBAAwB,GAArC,MAAa,wBAAwB;IAEzB,WAAW,CAAW;IAGtB,OAAO,CAAkB;IAEnC,GAAG,CAAC,cAAqB,EAAE,cAAkC;QAC3D,MAAM,EAAC,YAAY,EAAE,IAAI,EAAE,QAAQ,EAAE,IAAI,EAAC,GAAG,IAAI,CAAC,aAAa,CAAC,cAAc,EAAE,cAAc,CAAC,CAAC;QAEhG,OAAO;YACL,YAAY,IAAI,6DAA6B;YAC7C,IAAI,IAAI,mDAAwB;YAChC,CAAC,IAAI,IAAI,QAAQ,IAAI,IAAI,CAAC,OAAO,CAAC,UAAU,CAAC,KAAK,CAAC;YACnD,CAAC,IAAI,IAAI,CAAC,QAAQ,IAAI,IAAI,IAAI,IAAI,CAAC,OAAO,CAAC,UAAU,CAAC,MAAM,CAAC;YAC7D,CAAC,IAAI,IAAI,CAAC,QAAQ,IAAI,IAAI,IAAI,IAAI,CAAC,OAAO,CAAC,UAAU,CAAC,YAAY,CAAC;YACnE,GAAG,cAAc;SAClB,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;IACpB,CAAC;IAES,cAAc,CAAC,cAAkC;QACzD,OAAO,cAAc,CAAC,QAAQ,CAAC,WAAW,CAAC,MAAM,IAAI,IAAI,CAAC,WAAW,CAAC,MAAM,CAAC;IAC/E,CAA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nimport {Constant, Inject, Injectable} from \"@tsed/di\";\nimport {ParamTypes} from \"@tsed/platform-params\";\nimport {JsonEntityStore, JsonOperationRoute} from \"@tsed/schema\";\nimport {PlatformAcceptMimesMiddleware} from \"../middlewares/PlatformAcceptMimesMiddleware\";\nimport {PlatformMulterMiddleware} from \"../middlewares/PlatformMulterMiddleware\";\nimport {PlatformAdapter} from \"../services/PlatformAdapter\";\n\n@Injectable()\nexport class PlatformMiddlewaresChain {\n @Constant(\"acceptMimes\", [])\n protected acceptMimes: string[];\n\n @Inject()\n protected adapter: PlatformAdapter;\n\n get(allMiddlewares: any[], operationRoute: JsonOperationRoute) {\n const {ACCEPT_MIMES, FILE, RAW_BODY, BODY} = this.getParamTypes(allMiddlewares, operationRoute);\n\n return [\n ACCEPT_MIMES && PlatformAcceptMimesMiddleware,\n FILE && PlatformMulterMiddleware,\n !FILE && RAW_BODY && this.adapter.bodyParser(\"raw\"),\n !FILE && !RAW_BODY && BODY && this.adapter.bodyParser(\"json\"),\n !FILE && !RAW_BODY && BODY && this.adapter.bodyParser(\"urlencoded\"),\n ...allMiddlewares\n ].filter(Boolean);\n }\n\n protected hasAcceptMimes(operationRoute: JsonOperationRoute) {\n return operationRoute.endpoint.acceptMimes.length || this.acceptMimes.length;\n }\n\n protected getParamTypes(middlewares: any[], operationRoute: JsonOperationRoute) {\n return middlewares.filter(isClass).reduce(\n (paramTypes, token) => {\n if (token !== operationRoute.endpoint) {\n const entity = JsonEntityStore.fromMethod(token, \"use\");\n\n if (entity.decoratorType === \"method\") {\n const {FILE, RAW_BODY, BODY} = entity.getParamTypes();\n paramTypes.FILE = paramTypes.FILE || FILE;\n paramTypes.RAW_BODY = paramTypes.RAW_BODY || RAW_BODY;\n paramTypes.BODY = paramTypes.BODY || BODY;\n }\n }\n\n return paramTypes;\n },\n {\n ACCEPT_MIMES: this.hasAcceptMimes(operationRoute),\n FILE: operationRoute.has(ParamTypes.FILES),\n RAW_BODY: operationRoute.has(ParamTypes.RAW_BODY),\n BODY: operationRoute.has(ParamTypes.BODY) || operationRoute.method === \"ALL\"\n }\n );\n }\n}\n"]}
@@ -8,9 +8,18 @@ const di_1 = require("@tsed/di");
8
8
  * @platform
9
9
  */
10
10
  let PlatformRequest = class PlatformRequest {
11
- constructor({ request }, $ctx) {
11
+ $ctx;
12
+ constructor($ctx) {
12
13
  this.$ctx = $ctx;
13
- this.raw = request;
14
+ }
15
+ /**
16
+ * The current @@PlatformResponse@@.
17
+ */
18
+ get response() {
19
+ return this.$ctx.response;
20
+ }
21
+ get raw() {
22
+ return this.$ctx.event.request;
14
23
  }
15
24
  get secure() {
16
25
  return this.raw.secure;
@@ -77,6 +86,9 @@ let PlatformRequest = class PlatformRequest {
77
86
  get files() {
78
87
  return this.raw.files;
79
88
  }
89
+ get route() {
90
+ return this.$ctx.handlerMetadata?.path;
91
+ }
80
92
  /**
81
93
  * Return the original request framework instance
82
94
  */
@@ -108,14 +120,6 @@ let PlatformRequest = class PlatformRequest {
108
120
  isAborted() {
109
121
  return this.raw.aborted;
110
122
  }
111
- destroy() {
112
- // @ts-ignore
113
- delete this.raw;
114
- // @ts-ignore
115
- delete this.response;
116
- // @ts-ignore
117
- delete this.$ctx;
118
- }
119
123
  /**
120
124
  * Return the Framework response object (express, koa, etc...)
121
125
  */
@@ -126,13 +130,13 @@ let PlatformRequest = class PlatformRequest {
126
130
  * Return the Node.js response object
127
131
  */
128
132
  getReq() {
129
- return this.raw;
133
+ return this.$ctx.event.request;
130
134
  }
131
135
  };
132
- PlatformRequest = (0, tslib_1.__decorate)([
136
+ PlatformRequest = tslib_1.__decorate([
133
137
  (0, di_1.Injectable)(),
134
138
  (0, di_1.Scope)(di_1.ProviderScope.INSTANCE),
135
- (0, tslib_1.__metadata)("design:paramtypes", [Object, Function])
139
+ tslib_1.__metadata("design:paramtypes", [Function])
136
140
  ], PlatformRequest);
137
141
  exports.PlatformRequest = PlatformRequest;
138
142
  //# sourceMappingURL=PlatformRequest.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"PlatformRequest.js","sourceRoot":"","sources":["../../../src/services/PlatformRequest.ts"],"names":[],"mappings":";;;;AAAA,iCAA0D;AAgB1D;;;GAGG;AAGH,IAAa,eAAe,GAA5B,MAAa,eAAe;IAQ1B,YAAY,EAAC,OAAO,EAAgB,EAAY,IAAqB;QAArB,SAAI,GAAJ,IAAI,CAAiB;QACnE,IAAI,CAAC,GAAG,GAAG,OAAc,CAAC;IAC5B,CAAC;IAED,IAAI,MAAM;QACR,OAAO,IAAI,CAAC,GAAG,CAAC,MAAM,CAAC;IACzB,CAAC;IAED,IAAI,IAAI;QACN,OAAO,IAAI,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;IAC1B,CAAC;IAED,IAAI,QAAQ;QACV,OAAO,IAAI,CAAC,GAAG,CAAC,QAAQ,CAAC;IAC3B,CAAC;IAED;;;;OAIG;IACH,IAAI,GAAG;QACL,OAAO,IAAI,CAAC,GAAG,CAAC,WAAW,IAAI,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC;IAC9C,CAAC;IAED,IAAI,OAAO;QACT,OAAO,IAAI,CAAC,GAAG,CAAC,OAAO,CAAC;IAC1B,CAAC;IAED,IAAI,MAAM;QACR,OAAO,IAAI,CAAC,GAAG,CAAC,MAAM,CAAC;IACzB,CAAC;IAED;;;OAGG;IACH,IAAI,IAAI;QACN,OAAO,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC;IACvB,CAAC;IAED,IAAI,OAAO;QACT,OAAO,IAAI,CAAC,GAAG,CAAC,OAAO,IAAI,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC;IAC3C,CAAC;IAED;;;OAGG;IACH,IAAI,OAAO;QACT,OAAO,IAAI,CAAC,GAAG,CAAC,OAAO,CAAC;IAC1B,CAAC;IAED;;;;OAIG;IACH,IAAI,MAAM;QACR,OAAO,IAAI,CAAC,GAAG,CAAC,MAAM,CAAC;IACzB,CAAC;IAED;;;OAGG;IACH,IAAI,KAAK;QACP,OAAO,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC;IACxB,CAAC;IAED;;;OAGG;IACH,IAAI,OAAO;QACT,OAAO,IAAI,CAAC,GAAG,CAAC,OAAc,CAAC;IACjC,CAAC;IAED,IAAI,KAAK;QACP,OAAO,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC;IACxB,CAAC;IAED;;OAEG;IACH,IAAI,OAAO;QACT,OAAO,IAAI,CAAC,UAAU,EAAE,CAAC;IAC3B,CAAC;IAED;;OAEG;IACH,IAAI,GAAG;QACL,OAAO,IAAI,CAAC,MAAM,EAAE,CAAC;IACvB,CAAC;IAED;;;;;;;;OAQG;IACH,GAAG,CAAC,IAAY;QACd,OAAO,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;IAC5B,CAAC;IAaD,OAAO,CAAC,IAAwB;QAC9B,aAAa;QACb,OAAO,IAAI,CAAC,GAAG,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;IAChC,CAAC;IAED,SAAS;QACP,OAAO,IAAI,CAAC,GAAG,CAAC,OAAO,CAAC;IAC1B,CAAC;IAED,OAAO;QACL,aAAa;QACb,OAAO,IAAI,CAAC,GAAG,CAAC;QAChB,aAAa;QACb,OAAO,IAAI,CAAC,QAAQ,CAAC;QACrB,aAAa;QACb,OAAO,IAAI,CAAC,IAAI,CAAC;IACnB,CAAC;IAED;;OAEG;IACH,UAAU;QACR,OAAO,IAAI,CAAC,GAAU,CAAC;IACzB,CAAC;IAED;;OAEG;IACH,MAAM;QACJ,OAAO,IAAI,CAAC,GAAU,CAAC;IACzB,CAAC;CACF,CAAA;AA/JY,eAAe;IAF3B,IAAA,eAAU,GAAE;IACZ,IAAA,UAAK,EAAC,kBAAa,CAAC,QAAQ,CAAC;;GACjB,eAAe,CA+J3B;AA/JY,0CAAe","sourcesContent":["import {Injectable, ProviderScope, Scope} from \"@tsed/di\";\nimport {IncomingHttpHeaders, IncomingMessage} from \"http\";\nimport type {PlatformContext} from \"../domain/PlatformContext\";\nimport {IncomingEvent} from \"../interfaces/IncomingEvent\";\nimport type {PlatformResponse} from \"./PlatformResponse\";\n\ndeclare global {\n namespace TsED {\n // @ts-ignore\n export interface Request {\n id: string;\n $ctx: PlatformContext;\n }\n }\n}\n\n/**\n * Platform Request abstraction layer.\n * @platform\n */\n@Injectable()\n@Scope(ProviderScope.INSTANCE)\nexport class PlatformRequest<T extends {[key: string]: any} = any> {\n public raw: T;\n\n /**\n * The current @@PlatformResponse@@.\n */\n public response: PlatformResponse;\n\n constructor({request}: IncomingEvent, protected $ctx: PlatformContext) {\n this.raw = request as any;\n }\n\n get secure(): boolean {\n return this.raw.secure;\n }\n\n get host(): string {\n return this.get(\"host\");\n }\n\n get protocol(): string {\n return this.raw.protocol;\n }\n\n /**\n * Get the url of the request.\n *\n * Is equivalent of `express.response.originalUrl || express.response.url`.\n */\n get url(): string {\n return this.raw.originalUrl || this.raw.url;\n }\n\n get headers(): IncomingHttpHeaders {\n return this.raw.headers;\n }\n\n get method(): string {\n return this.raw.method;\n }\n\n /**\n * Contains key-value pairs of data submitted in the request body. By default, it is `undefined`, and is populated when you use\n * `body-parsing` middleware such as `express.json()` or `express.urlencoded()`.\n */\n get body(): any {\n return this.raw.body;\n }\n\n get rawBody(): any {\n return this.raw.rawBody || this.raw.body;\n }\n\n /**\n * When using `cookie-parser` middleware, this property is an object that contains cookies sent by the request.\n * If the request contains no cookies, it defaults to `{}`.\n */\n get cookies(): {[key: string]: any} {\n return this.raw.cookies;\n }\n\n /**\n * This property is an object containing properties mapped to the named route `parameters`.\n * For example, if you have the route `/user/:name`, then the `name` property is available as `req.params.name`.\n * This object defaults to `{}`.\n */\n get params(): {[key: string]: any} {\n return this.raw.params;\n }\n\n /**\n * This property is an object containing a property for each query string parameter in the route.\n * When query parser is set to disabled, it is an empty object `{}`, otherwise it is the result of the configured query parser.\n */\n get query(): {[key: string]: any} {\n return this.raw.query;\n }\n\n /**\n * This property is an object containing a property for each session attributes set by any code.\n * It require to install a middleware like express-session to work.\n */\n get session(): {[key: string]: any} {\n return this.raw.session as any;\n }\n\n get files() {\n return this.raw.files;\n }\n\n /**\n * Return the original request framework instance\n */\n get request() {\n return this.getRequest();\n }\n\n /**\n * Return the original request node.js instance\n */\n get req() {\n return this.getReq();\n }\n\n /**\n * Returns the HTTP request header specified by field. The match is case-insensitive.\n *\n * ```typescript\n * request.get('Content-Type') // => \"text/plain\"\n * ```\n *\n * @param name\n */\n get(name: string) {\n return this.raw.get(name);\n }\n\n /**\n * Checks if the specified content types are acceptable, based on the request’s Accept HTTP header field. The method returns the best match, or if none of the specified content types is acceptable, returns false (in which case, the application should respond with 406 \"Not Acceptable\").\n *\n * The type value may be a single MIME type string (such as “application/json”), an extension name such as “json”, a comma-delimited list, or an array. For a list or array, the method returns the best match (if any).\n *\n * @param mime\n */\n accepts(mime: string): string | false;\n\n accepts(mime: string[]): string[] | false;\n\n accepts(mime?: string | string[]): string | string[] | false {\n // @ts-ignore\n return this.raw.accepts(mime);\n }\n\n isAborted() {\n return this.raw.aborted;\n }\n\n destroy() {\n // @ts-ignore\n delete this.raw;\n // @ts-ignore\n delete this.response;\n // @ts-ignore\n delete this.$ctx;\n }\n\n /**\n * Return the Framework response object (express, koa, etc...)\n */\n getRequest<Req = T>(): Req {\n return this.raw as any;\n }\n\n /**\n * Return the Node.js response object\n */\n getReq(): IncomingMessage {\n return this.raw as any;\n }\n}\n"]}
1
+ {"version":3,"file":"PlatformRequest.js","sourceRoot":"","sources":["../../../src/services/PlatformRequest.ts"],"names":[],"mappings":";;;;AAAA,iCAA0D;AAc1D;;;GAGG;AAGH,IAAa,eAAe,GAA5B,MAAa,eAAe;IACL;IAArB,YAAqB,IAAqB;QAArB,SAAI,GAAJ,IAAI,CAAiB;IAAG,CAAC;IAE9C;;OAEG;IACH,IAAI,QAAQ;QACV,OAAO,IAAI,CAAC,IAAI,CAAC,QAAQ,CAAC;IAC5B,CAAC;IAED,IAAI,GAAG;QACL,OAAO,IAAI,CAAC,IAAI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CAAC,IAAI,CAAC,eAAe,EAAE,IAAI,CAAC;IACzC,CAAC;IAED;;OAEG;IACH,IAAI,OAAO;QACT,OAAO,IAAI,CAAC,UAAU,EAAE,CAAC;IAC3B,CAAC;IAED;;OAEG;IACH,IAAI,GAAG;QACL,OAAO,IAAI,CAAC,MAAM,EAAE,CAAC;IACvB,CAAC;IAED;;;;;;;;OAQG;IACH,GAAG,CAAC,IAAY;QACd,OAAO,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;IAC5B,CAAC;IAaD,OAAO,CAAC,IAAwB;QAC9B,aAAa;QACb,OAAO,IAAI,CAAC,GAAG,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;IAChC,CAAC;IAED,SAAS;QACP,OAAO,IAAI,CAAC,GAAG,CAAC,OAAO,CAAC;IAC1B,CAAC;IAED;;OAEG;IACH,UAAU;QACR,OAAO,IAAI,CAAC,GAAU,CAAC;IACzB,CAAC;IAED;;OAEG;IACH,MAAM;QACJ,OAAO,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC;IACjC,CAAC;CACF,CAAA;AA5JY,eAAe;IAF3B,IAAA,eAAU,GAAE;IACZ,IAAA,UAAK,EAAC,kBAAa,CAAC,QAAQ,CAAC;;GACjB,eAAe,CA4J3B;AA5JY,0CAAe","sourcesContent":["import {Injectable, ProviderScope, Scope} from \"@tsed/di\";\nimport {IncomingHttpHeaders, IncomingMessage} from \"http\";\nimport type {PlatformContext} from \"../domain/PlatformContext\";\nimport type {PlatformResponse} from \"./PlatformResponse\";\n\ndeclare global {\n namespace TsED {\n // @ts-ignore\n export interface Request {\n id: string;\n }\n }\n}\n\n/**\n * Platform Request abstraction layer.\n * @platform\n */\n@Injectable()\n@Scope(ProviderScope.INSTANCE)\nexport class PlatformRequest<Req extends {[key: string]: any} = any> {\n constructor(readonly $ctx: PlatformContext) {}\n\n /**\n * The current @@PlatformResponse@@.\n */\n get response(): PlatformResponse {\n return this.$ctx.response;\n }\n\n get raw(): Req {\n return this.$ctx.event.request as any;\n }\n\n get secure(): boolean {\n return this.raw.secure;\n }\n\n get host(): string {\n return this.get(\"host\");\n }\n\n get protocol(): string {\n return this.raw.protocol;\n }\n\n /**\n * Get the url of the request.\n *\n * Is equivalent of `express.response.originalUrl || express.response.url`.\n */\n get url(): string {\n return this.raw.originalUrl || this.raw.url;\n }\n\n get headers(): IncomingHttpHeaders {\n return this.raw.headers;\n }\n\n get method(): string {\n return this.raw.method;\n }\n\n /**\n * Contains key-value pairs of data submitted in the request body. By default, it is `undefined`, and is populated when you use\n * `body-parsing` middleware such as `express.json()` or `express.urlencoded()`.\n */\n get body(): any {\n return this.raw.body;\n }\n\n get rawBody(): any {\n return this.raw.rawBody || this.raw.body;\n }\n\n /**\n * When using `cookie-parser` middleware, this property is an object that contains cookies sent by the request.\n * If the request contains no cookies, it defaults to `{}`.\n */\n get cookies(): {[key: string]: any} {\n return this.raw.cookies;\n }\n\n /**\n * This property is an object containing properties mapped to the named route `parameters`.\n * For example, if you have the route `/user/:name`, then the `name` property is available as `req.params.name`.\n * This object defaults to `{}`.\n */\n get params(): {[key: string]: any} {\n return this.raw.params;\n }\n\n /**\n * This property is an object containing a property for each query string parameter in the route.\n * When query parser is set to disabled, it is an empty object `{}`, otherwise it is the result of the configured query parser.\n */\n get query(): {[key: string]: any} {\n return this.raw.query;\n }\n\n /**\n * This property is an object containing a property for each session attributes set by any code.\n * It require to install a middleware like express-session to work.\n */\n get session(): {[key: string]: any} {\n return this.raw.session as any;\n }\n\n get files() {\n return this.raw.files;\n }\n\n get route() {\n return this.$ctx.handlerMetadata?.path;\n }\n\n /**\n * Return the original request framework instance\n */\n get request() {\n return this.getRequest();\n }\n\n /**\n * Return the original request node.js instance\n */\n get req() {\n return this.getReq();\n }\n\n /**\n * Returns the HTTP request header specified by field. The match is case-insensitive.\n *\n * ```typescript\n * request.get('Content-Type') // => \"text/plain\"\n * ```\n *\n * @param name\n */\n get(name: string) {\n return this.raw.get(name);\n }\n\n /**\n * Checks if the specified content types are acceptable, based on the request’s Accept HTTP header field. The method returns the best match, or if none of the specified content types is acceptable, returns false (in which case, the application should respond with 406 \"Not Acceptable\").\n *\n * The type value may be a single MIME type string (such as “application/json”), an extension name such as “json”, a comma-delimited list, or an array. For a list or array, the method returns the best match (if any).\n *\n * @param mime\n */\n accepts(mime: string): string | false;\n\n accepts(mime: string[]): string[] | false;\n\n accepts(mime?: string | string[]): string | string[] | false {\n // @ts-ignore\n return this.raw.accepts(mime);\n }\n\n isAborted() {\n return this.raw.aborted;\n }\n\n /**\n * Return the Framework response object (express, koa, etc...)\n */\n getRequest<R = Req>(): R {\n return this.raw as any;\n }\n\n /**\n * Return the Node.js response object\n */\n getReq(): IncomingMessage {\n return this.$ctx.event.request;\n }\n}\n"]}