@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.
- package/jest.config.js +14 -0
- package/lib/cjs/builder/PlatformBuilder.js +83 -63
- package/lib/cjs/builder/PlatformBuilder.js.map +1 -1
- package/lib/cjs/config/interfaces/PlatformLoggerSettings.js.map +1 -1
- package/lib/cjs/config/services/PlatformConfiguration.js +0 -70
- package/lib/cjs/config/services/PlatformConfiguration.js.map +1 -1
- package/lib/cjs/decorators/params/error.js +1 -1
- package/lib/cjs/decorators/params/error.js.map +1 -1
- package/lib/cjs/domain/AnyToPromiseWithCtx.js +4 -5
- package/lib/cjs/domain/AnyToPromiseWithCtx.js.map +1 -1
- package/lib/cjs/domain/PlatformContext.js +82 -32
- package/lib/cjs/domain/PlatformContext.js.map +1 -1
- package/lib/cjs/domain/PlatformRouteDetails.js +0 -21
- package/lib/cjs/domain/PlatformRouteDetails.js.map +1 -1
- package/lib/cjs/exports.js +1 -0
- package/lib/cjs/exports.js.map +1 -1
- package/lib/cjs/index.js +1 -7
- package/lib/cjs/index.js.map +1 -1
- package/lib/cjs/interfaces/Route.js.map +1 -1
- package/lib/cjs/middlewares/PlatformAcceptMimesMiddleware.js +2 -1
- package/lib/cjs/middlewares/PlatformAcceptMimesMiddleware.js.map +1 -1
- package/lib/cjs/middlewares/PlatformMulterMiddleware.js +2 -0
- package/lib/cjs/middlewares/PlatformMulterMiddleware.js.map +1 -1
- package/lib/cjs/services/FakeAdapter.js +6 -12
- package/lib/cjs/services/FakeAdapter.js.map +1 -1
- package/lib/cjs/services/Platform.js +43 -85
- package/lib/cjs/services/Platform.js.map +1 -1
- package/lib/cjs/services/PlatformAdapter.js +10 -0
- package/lib/cjs/services/PlatformAdapter.js.map +1 -1
- package/lib/cjs/services/PlatformApplication.js +23 -9
- package/lib/cjs/services/PlatformApplication.js.map +1 -1
- package/lib/cjs/services/PlatformHandler.js +88 -161
- package/lib/cjs/services/PlatformHandler.js.map +1 -1
- package/lib/cjs/services/PlatformMiddlewaresChain.js +5 -19
- package/lib/cjs/services/PlatformMiddlewaresChain.js.map +1 -1
- package/lib/cjs/services/PlatformRequest.js +17 -12
- package/lib/cjs/services/PlatformRequest.js.map +1 -1
- package/lib/cjs/services/PlatformResponse.js +20 -20
- package/lib/cjs/services/PlatformResponse.js.map +1 -1
- package/lib/cjs/services/PlatformTest.js +20 -8
- package/lib/cjs/services/PlatformTest.js.map +1 -1
- package/lib/cjs/utils/createContext.js +30 -21
- package/lib/cjs/utils/createContext.js.map +1 -1
- package/lib/cjs/utils/createHttpServer.js +3 -3
- package/lib/cjs/utils/createHttpServer.js.map +1 -1
- package/lib/cjs/utils/createHttpsServer.js +3 -3
- package/lib/cjs/utils/createHttpsServer.js.map +1 -1
- package/lib/cjs/utils/createInjector.js +6 -8
- package/lib/cjs/utils/createInjector.js.map +1 -1
- package/lib/cjs/utils/ensureContext.js +16 -0
- package/lib/cjs/utils/ensureContext.js.map +1 -0
- package/lib/cjs/utils/getConfiguration.js +2 -12
- package/lib/cjs/utils/getConfiguration.js.map +1 -1
- package/lib/cjs/utils/printRoutes.js +10 -8
- package/lib/cjs/utils/printRoutes.js.map +1 -1
- package/lib/cjs/utils/setResponseHeaders.js +7 -13
- package/lib/cjs/utils/setResponseHeaders.js.map +1 -1
- package/lib/esm/builder/PlatformBuilder.js +60 -63
- package/lib/esm/builder/PlatformBuilder.js.map +1 -1
- package/lib/esm/config/interfaces/PlatformLoggerSettings.js.map +1 -1
- package/lib/esm/config/services/PlatformConfiguration.js +0 -70
- package/lib/esm/config/services/PlatformConfiguration.js.map +1 -1
- package/lib/esm/decorators/params/error.js +1 -1
- package/lib/esm/decorators/params/error.js.map +1 -1
- package/lib/esm/domain/AnyToPromiseWithCtx.js +4 -5
- package/lib/esm/domain/AnyToPromiseWithCtx.js.map +1 -1
- package/lib/esm/domain/PlatformContext.js +82 -32
- package/lib/esm/domain/PlatformContext.js.map +1 -1
- package/lib/esm/domain/PlatformRouteDetails.js +1 -19
- package/lib/esm/domain/PlatformRouteDetails.js.map +1 -1
- package/lib/esm/exports.js +1 -0
- package/lib/esm/exports.js.map +1 -1
- package/lib/esm/index.js +1 -7
- package/lib/esm/index.js.map +1 -1
- package/lib/esm/interfaces/Route.js.map +1 -1
- package/lib/esm/middlewares/PlatformAcceptMimesMiddleware.js +2 -1
- package/lib/esm/middlewares/PlatformAcceptMimesMiddleware.js.map +1 -1
- package/lib/esm/middlewares/PlatformMulterMiddleware.js +2 -0
- package/lib/esm/middlewares/PlatformMulterMiddleware.js.map +1 -1
- package/lib/esm/services/FakeAdapter.js +6 -12
- package/lib/esm/services/FakeAdapter.js.map +1 -1
- package/lib/esm/services/Platform.js +44 -86
- package/lib/esm/services/Platform.js.map +1 -1
- package/lib/esm/services/PlatformAdapter.js +10 -0
- package/lib/esm/services/PlatformAdapter.js.map +1 -1
- package/lib/esm/services/PlatformApplication.js +23 -9
- package/lib/esm/services/PlatformApplication.js.map +1 -1
- package/lib/esm/services/PlatformHandler.js +90 -163
- package/lib/esm/services/PlatformHandler.js.map +1 -1
- package/lib/esm/services/PlatformMiddlewaresChain.js +5 -19
- package/lib/esm/services/PlatformMiddlewaresChain.js.map +1 -1
- package/lib/esm/services/PlatformRequest.js +17 -12
- package/lib/esm/services/PlatformRequest.js.map +1 -1
- package/lib/esm/services/PlatformResponse.js +20 -20
- package/lib/esm/services/PlatformResponse.js.map +1 -1
- package/lib/esm/services/PlatformTest.js +20 -8
- package/lib/esm/services/PlatformTest.js.map +1 -1
- package/lib/esm/utils/createContext.js +28 -20
- package/lib/esm/utils/createContext.js.map +1 -1
- package/lib/esm/utils/createHttpServer.js +3 -3
- package/lib/esm/utils/createHttpServer.js.map +1 -1
- package/lib/esm/utils/createHttpsServer.js +3 -3
- package/lib/esm/utils/createHttpsServer.js.map +1 -1
- package/lib/esm/utils/createInjector.js +7 -9
- package/lib/esm/utils/createInjector.js.map +1 -1
- package/lib/esm/utils/ensureContext.js +12 -0
- package/lib/esm/utils/ensureContext.js.map +1 -0
- package/lib/esm/utils/getConfiguration.js +4 -14
- package/lib/esm/utils/getConfiguration.js.map +1 -1
- package/lib/esm/utils/printRoutes.js +10 -8
- package/lib/esm/utils/printRoutes.js.map +1 -1
- package/lib/esm/utils/setResponseHeaders.js +7 -13
- package/lib/esm/utils/setResponseHeaders.js.map +1 -1
- package/lib/types/builder/PlatformBuilder.d.ts +17 -16
- package/lib/types/config/interfaces/PlatformLoggerSettings.d.ts +2 -43
- package/lib/types/config/services/PlatformConfiguration.d.ts +1 -22
- package/lib/types/domain/AnyToPromiseWithCtx.d.ts +1 -9
- package/lib/types/domain/PlatformContext.d.ts +36 -15
- package/lib/types/domain/PlatformRouteDetails.d.ts +4 -19
- package/lib/types/exports.d.ts +1 -0
- package/lib/types/index.d.ts +1 -7
- package/lib/types/interfaces/Route.d.ts +2 -3
- package/lib/types/middlewares/PlatformMulterMiddleware.d.ts +1 -1
- package/lib/types/services/FakeAdapter.d.ts +7 -6
- package/lib/types/services/Platform.d.ts +11 -28
- package/lib/types/services/PlatformAdapter.d.ts +19 -10
- package/lib/types/services/PlatformApplication.d.ts +14 -6
- package/lib/types/services/PlatformHandler.d.ts +19 -55
- package/lib/types/services/PlatformMiddlewaresChain.d.ts +1 -2
- package/lib/types/services/PlatformRequest.d.ts +8 -9
- package/lib/types/services/PlatformResponse.d.ts +10 -13
- package/lib/types/services/PlatformTest.d.ts +4 -3
- package/lib/types/utils/createContext.d.ts +4 -1
- package/lib/types/utils/createHttpsServer.d.ts +1 -0
- package/lib/types/utils/createInjector.d.ts +2 -2
- package/lib/types/utils/ensureContext.d.ts +2 -0
- package/lib/types/utils/listenServer.d.ts +2 -0
- package/package.json +23 -21
- package/lib/cjs/builder/PlatformControllerBuilder.js +0 -82
- package/lib/cjs/builder/PlatformControllerBuilder.js.map +0 -1
- package/lib/cjs/domain/ControllerProvider.js +0 -51
- package/lib/cjs/domain/ControllerProvider.js.map +0 -1
- package/lib/cjs/domain/HandlerMetadata.js +0 -57
- package/lib/cjs/domain/HandlerMetadata.js.map +0 -1
- package/lib/cjs/middlewares/__mock__/FakeCtrl.js +0 -12
- package/lib/cjs/middlewares/__mock__/FakeCtrl.js.map +0 -1
- package/lib/cjs/middlewares/bindEndpointMiddleware.js +0 -13
- package/lib/cjs/middlewares/bindEndpointMiddleware.js.map +0 -1
- package/lib/cjs/services/PlatformRouter.js +0 -108
- package/lib/cjs/services/PlatformRouter.js.map +0 -1
- package/lib/cjs/utils/__mock__/Test1.js +0 -8
- package/lib/cjs/utils/__mock__/Test1.js.map +0 -1
- package/lib/cjs/utils/__mock__/Test2.js +0 -8
- package/lib/cjs/utils/__mock__/Test2.js.map +0 -1
- package/lib/cjs/utils/__mock__/controllers/rest/TestCtrl.js +0 -12
- package/lib/cjs/utils/__mock__/controllers/rest/TestCtrl.js.map +0 -1
- package/lib/cjs/utils/__mock__/module1/Module1.js +0 -19
- package/lib/cjs/utils/__mock__/module1/Module1.js.map +0 -1
- package/lib/cjs/utils/__mock__/module1/controllers/M1Ctrl1.js +0 -17
- package/lib/cjs/utils/__mock__/module1/controllers/M1Ctrl1.js.map +0 -1
- package/lib/cjs/utils/__mock__/module1/services/M1Service.js +0 -17
- package/lib/cjs/utils/__mock__/module1/services/M1Service.js.map +0 -1
- package/lib/cjs/utils/__mock__/module1/submodule/SubModule.js +0 -15
- package/lib/cjs/utils/__mock__/module1/submodule/SubModule.js.map +0 -1
- package/lib/cjs/utils/__mock__/module1/submodule/SubService.js +0 -12
- package/lib/cjs/utils/__mock__/module1/submodule/SubService.js.map +0 -1
- package/lib/cjs/utils/__mock__/module2/Module2.js +0 -18
- package/lib/cjs/utils/__mock__/module2/Module2.js.map +0 -1
- package/lib/cjs/utils/__mock__/module2/controllers/M2Ctrl.js +0 -12
- package/lib/cjs/utils/__mock__/module2/controllers/M2Ctrl.js.map +0 -1
- package/lib/cjs/utils/createHandlerMetadata.js +0 -49
- package/lib/cjs/utils/createHandlerMetadata.js.map +0 -1
- package/lib/cjs/utils/useCtxHandler.js +0 -15
- package/lib/cjs/utils/useCtxHandler.js.map +0 -1
- package/lib/esm/builder/PlatformControllerBuilder.js +0 -76
- package/lib/esm/builder/PlatformControllerBuilder.js.map +0 -1
- package/lib/esm/domain/ControllerProvider.js +0 -47
- package/lib/esm/domain/ControllerProvider.js.map +0 -1
- package/lib/esm/domain/HandlerMetadata.js +0 -53
- package/lib/esm/domain/HandlerMetadata.js.map +0 -1
- package/lib/esm/middlewares/__mock__/FakeCtrl.js +0 -9
- package/lib/esm/middlewares/__mock__/FakeCtrl.js.map +0 -1
- package/lib/esm/middlewares/bindEndpointMiddleware.js +0 -9
- package/lib/esm/middlewares/bindEndpointMiddleware.js.map +0 -1
- package/lib/esm/services/PlatformRouter.js +0 -105
- package/lib/esm/services/PlatformRouter.js.map +0 -1
- package/lib/esm/utils/__mock__/Test1.js +0 -4
- package/lib/esm/utils/__mock__/Test1.js.map +0 -1
- package/lib/esm/utils/__mock__/Test2.js +0 -4
- package/lib/esm/utils/__mock__/Test2.js.map +0 -1
- package/lib/esm/utils/__mock__/controllers/rest/TestCtrl.js +0 -9
- package/lib/esm/utils/__mock__/controllers/rest/TestCtrl.js.map +0 -1
- package/lib/esm/utils/__mock__/module1/Module1.js +0 -16
- package/lib/esm/utils/__mock__/module1/Module1.js.map +0 -1
- package/lib/esm/utils/__mock__/module1/controllers/M1Ctrl1.js +0 -14
- package/lib/esm/utils/__mock__/module1/controllers/M1Ctrl1.js.map +0 -1
- package/lib/esm/utils/__mock__/module1/services/M1Service.js +0 -14
- package/lib/esm/utils/__mock__/module1/services/M1Service.js.map +0 -1
- package/lib/esm/utils/__mock__/module1/submodule/SubModule.js +0 -12
- package/lib/esm/utils/__mock__/module1/submodule/SubModule.js.map +0 -1
- package/lib/esm/utils/__mock__/module1/submodule/SubService.js +0 -9
- package/lib/esm/utils/__mock__/module1/submodule/SubService.js.map +0 -1
- package/lib/esm/utils/__mock__/module2/Module2.js +0 -15
- package/lib/esm/utils/__mock__/module2/Module2.js.map +0 -1
- package/lib/esm/utils/__mock__/module2/controllers/M2Ctrl.js +0 -9
- package/lib/esm/utils/__mock__/module2/controllers/M2Ctrl.js.map +0 -1
- package/lib/esm/utils/createHandlerMetadata.js +0 -45
- package/lib/esm/utils/createHandlerMetadata.js.map +0 -1
- package/lib/esm/utils/useCtxHandler.js +0 -11
- package/lib/esm/utils/useCtxHandler.js.map +0 -1
- package/lib/types/builder/PlatformControllerBuilder.d.ts +0 -18
- package/lib/types/domain/ControllerProvider.d.ts +0 -24
- package/lib/types/domain/HandlerMetadata.d.ts +0 -32
- package/lib/types/middlewares/__mock__/FakeCtrl.d.ts +0 -2
- package/lib/types/middlewares/bindEndpointMiddleware.d.ts +0 -6
- package/lib/types/services/PlatformRouter.d.ts +0 -51
- package/lib/types/utils/__mock__/Test1.d.ts +0 -3
- package/lib/types/utils/__mock__/Test2.d.ts +0 -3
- package/lib/types/utils/__mock__/controllers/rest/TestCtrl.d.ts +0 -2
- package/lib/types/utils/__mock__/module1/Module1.d.ts +0 -2
- package/lib/types/utils/__mock__/module1/controllers/M1Ctrl1.d.ts +0 -5
- package/lib/types/utils/__mock__/module1/services/M1Service.d.ts +0 -5
- package/lib/types/utils/__mock__/module1/submodule/SubModule.d.ts +0 -2
- package/lib/types/utils/__mock__/module1/submodule/SubService.d.ts +0 -2
- package/lib/types/utils/__mock__/module2/Module2.d.ts +0 -2
- package/lib/types/utils/__mock__/module2/controllers/M2Ctrl.d.ts +0 -2
- package/lib/types/utils/createHandlerMetadata.d.ts +0 -8
- 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
|
|
3
|
-
import {
|
|
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
|
-
|
|
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.
|
|
18
|
-
this.
|
|
19
|
-
this.
|
|
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(
|
|
33
|
-
const
|
|
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.
|
|
39
|
-
|
|
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
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
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.
|
|
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: [
|
|
74
|
+
imports: []
|
|
121
75
|
}),
|
|
122
|
-
__metadata("design:paramtypes", [InjectorService,
|
|
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 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Platform.js","sourceRoot":"","sources":["../../../src/services/Platform.ts"],"names":[],"mappings":";
|
|
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,CAAC,CAAC,CAAC;QAEL,eAAe,CAAC,QAAQ,EAAE,CAAC;IAC7B,CAAC;IAED,IAAI,GAAG;QACL,OAAO,IAAI,CAAC,mBAAmB,CAAC;IAClC,CAAC;IAEM,SAAS,CAAC,MAAe;QAC9B,MAAM,CAAC,OAAO,CAAC,CAAC,aAAa,EAAE,EAAE;YAC/B,IAAI,CAAC,QAAQ,CAAC,aAAa,CAAC,KAAK,EAAE,aAAa,CAAC,KAAK,CAAC,CAAC;QAC1D,CAAC,CAAC,CAAC;IACL,CAAC;IAEM,QAAQ,CAAC,KAAa,EAAE,KAAoB;QACjD,MAAM,QAAQ,GAAG,IAAI,CAAC,QAAQ,CAAC,WAAW,CAAC,KAAK,CAAuB,CAAC;QAExE,IAAI,CAAC,QAAQ,IAAI,QAAQ,CAAC,SAAS,EAAE,EAAE;YACrC,OAAO,IAAI,CAAC;SACb;QAED,MAAM,MAAM,GAAG,IAAI,CAAC,eAAe,CAAC,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC;QAEzD,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC;QAE5B,OAAO,IAAI,CAAC;IACd,CAAC;IAEM,SAAS;QACd,IAAI,CAAC,YAAY,GAAG,IAAI,GAAG,EAAE,CAAC;QAE9B,OAAO,IAAI,CAAC,eAAe,CAAC,SAAS,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE;YAC5D,IAAI,KAAK,CAAC,UAAU,EAAE,EAAE;gBACtB,IAAI,CAAC,YAAY,CAAC,GAAG,CAAC,KAAK,CAAC,QAAQ,CAAC,KAAK,EAAE;oBAC1C,KAAK,EAAE,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,KAAK,CAAC,KAAK,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;oBACvD,QAAQ,EAAE,KAAK,CAAC,QAAQ;iBACzB,CAAC,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;
|
|
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 {
|
|
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
|
-
|
|
13
|
-
|
|
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 =
|
|
16
|
-
this.
|
|
20
|
+
this.rawApp = app;
|
|
21
|
+
this.rawCallback = callback;
|
|
17
22
|
}
|
|
18
23
|
getApp() {
|
|
19
|
-
return this.
|
|
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", [
|
|
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;
|
|
1
|
+
{"version":3,"file":"PlatformApplication.js","sourceRoot":"","sources":["../../../src/services/PlatformApplication.ts"],"names":[],"mappings":";AAAA,OAAO,EAAC,UAAU,EAAE,eAAe,EAAE,aAAa,EAAC,MAAM,UAAU,CAAC;AACpE,OAAO,EAAC,cAAc,EAAC,MAAM,uBAAuB,CAAC;AAGrD,OAAO,EAAC,eAAe,EAAC,MAAM,mBAAmB,CAAC;AASlD;;;;GAIG;AAIH,IAAa,mBAAmB,GAAhC,MAAa,mBAA4C,SAAQ,cAAc;IAI1D;IAAsC;IAHzD,MAAM,CAAM;IACZ,WAAW,CAAY;IAEvB,YAAmB,OAA6B,EAAS,QAAyB;QAChF,KAAK,CAAC,QAAQ,CAAC,CAAC;QADC,YAAO,GAAP,OAAO,CAAsB;QAAS,aAAQ,GAAR,QAAQ,CAAiB;QAEhF,MAAM,EAAC,GAAG,EAAE,QAAQ,EAAC,GAAG,OAAO,CAAC,GAAG,EAAE,CAAC;QAEtC,IAAI,CAAC,MAAM,GAAG,GAAG,CAAC;QAClB,IAAI,CAAC,WAAW,GAAG,QAAQ,CAAC;IAC9B,CAAC;IAED,MAAM;QACJ,OAAO,IAAI,CAAC,MAAM,CAAC;IACrB,CAAC;IAED,MAAM,CAAC,OAA+B;QACpC,OAAO,IAAI,CAAC,OAAO,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC;IACzC,CAAC;IAID,QAAQ,CAAC,GAAqB,EAAE,GAAoB;QAClD,IAAI,GAAG,IAAI,GAAG,EAAE;YACd,OAAO,IAAI,CAAC,QAAQ,EAAE,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC;SAClC;QAED,OAAO,IAAI,CAAC,WAAW,EAAE,CAAC;IAC5B,CAAC;CACF,CAAA;AA7BY,mBAAmB;IAH/B,UAAU,CAAC;QACV,KAAK,EAAE,aAAa,CAAC,SAAS;KAC/B,CAAC;qCAK4B,eAAe,EAAwB,eAAe;GAJvE,mBAAmB,CA6B/B;SA7BY,mBAAmB","sourcesContent":["import {Injectable, InjectorService, ProviderScope} from \"@tsed/di\";\nimport {PlatformRouter} from \"@tsed/platform-router\";\nimport {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,
|
|
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
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
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
|
|
36
|
-
|
|
37
|
-
|
|
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(
|
|
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
|
-
|
|
62
|
-
if (
|
|
63
|
-
return
|
|
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
|
-
|
|
88
|
-
|
|
89
|
-
|
|
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(
|
|
96
|
-
|
|
97
|
-
|
|
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: ${
|
|
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
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
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
|
-
|
|
124
|
-
|
|
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
|
-
|
|
134
|
-
|
|
108
|
+
catch (error) {
|
|
109
|
+
return this.onError(error, $ctx);
|
|
135
110
|
}
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
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
|
|
152
|
-
* @param requestOptions
|
|
118
|
+
* @param $ctx
|
|
153
119
|
* @protected
|
|
154
120
|
*/
|
|
155
|
-
async onSuccess(
|
|
156
|
-
const { metadata, $ctx, next } = requestOptions;
|
|
121
|
+
async onSuccess($ctx) {
|
|
157
122
|
// istanbul ignore next
|
|
158
|
-
if ($ctx
|
|
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.
|
|
163
|
-
this.
|
|
129
|
+
if (metadata.isEndpoint()) {
|
|
130
|
+
this.setResponseHeaders($ctx);
|
|
164
131
|
}
|
|
165
132
|
// call returned middleware
|
|
166
|
-
if (isFunction(data) && !isStream(data)) {
|
|
167
|
-
return
|
|
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(
|
|
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
|