@tsed/common 7.0.0-beta.18 → 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.
- package/coverage.json +4 -4
- package/lib/cjs/interfaces/PlatformRouteOptions.js.map +1 -1
- package/lib/cjs/services/Platform.js +3 -17
- package/lib/cjs/services/Platform.js.map +1 -1
- package/lib/cjs/services/PlatformHandler.js +68 -54
- package/lib/cjs/services/PlatformHandler.js.map +1 -1
- package/lib/esm/interfaces/PlatformRouteOptions.js.map +1 -1
- package/lib/esm/services/Platform.js +3 -17
- package/lib/esm/services/Platform.js.map +1 -1
- package/lib/esm/services/PlatformHandler.js +70 -56
- package/lib/esm/services/PlatformHandler.js.map +1 -1
- package/lib/types/interfaces/PlatformRouteOptions.d.ts +0 -1
- package/lib/types/services/Platform.d.ts +1 -5
- package/lib/types/services/PlatformHandler.d.ts +13 -15
- package/package.json +16 -16
package/coverage.json
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"PlatformRouteOptions.js","sourceRoot":"","sources":["../../../src/interfaces/PlatformRouteOptions.ts"],"names":[],"mappings":"","sourcesContent":["import {Type} from \"@tsed/core\";\nimport {PathType} from \"@tsed/di\";\n\n/**\n * @ignore\n */\nexport interface PlatformRouteOptions {\n token?: Type<any>;\n path?: PathType | undefined;\n method?: string;\n
|
|
1
|
+
{"version":3,"file":"PlatformRouteOptions.js","sourceRoot":"","sources":["../../../src/interfaces/PlatformRouteOptions.ts"],"names":[],"mappings":"","sourcesContent":["import {Type} from \"@tsed/core\";\nimport {PathType} from \"@tsed/di\";\n\n/**\n * @ignore\n */\nexport interface PlatformRouteOptions {\n token?: Type<any>;\n path?: PathType | undefined;\n method?: string;\n handlers: any[];\n}\n\n/**\n * @ignore\n */\nexport type PlatformRouteWithoutHandlers = Partial<Omit<PlatformRouteOptions, \"handlers\">>;\n"]}
|
|
@@ -6,7 +6,6 @@ const di_1 = require("@tsed/di");
|
|
|
6
6
|
const platform_router_1 = require("@tsed/platform-router");
|
|
7
7
|
const PlatformApplication_1 = require("./PlatformApplication");
|
|
8
8
|
const PlatformHandler_1 = require("./PlatformHandler");
|
|
9
|
-
const PlatformMiddlewaresChain_1 = require("./PlatformMiddlewaresChain");
|
|
10
9
|
/**
|
|
11
10
|
* `Platform` is used to provide all routes collected by annotation `@Controller`.
|
|
12
11
|
*
|
|
@@ -16,22 +15,11 @@ let Platform = class Platform {
|
|
|
16
15
|
injector;
|
|
17
16
|
platformApplication;
|
|
18
17
|
platformRouters;
|
|
19
|
-
platformHandler;
|
|
20
|
-
platformMiddlewaresChain;
|
|
21
18
|
#controllers = new Map();
|
|
22
|
-
constructor(injector, platformApplication, platformRouters
|
|
19
|
+
constructor(injector, platformApplication, platformRouters) {
|
|
23
20
|
this.injector = injector;
|
|
24
21
|
this.platformApplication = platformApplication;
|
|
25
22
|
this.platformRouters = platformRouters;
|
|
26
|
-
this.platformHandler = platformHandler;
|
|
27
|
-
this.platformMiddlewaresChain = platformMiddlewaresChain;
|
|
28
|
-
// configure the router module
|
|
29
|
-
platformRouters.hooks
|
|
30
|
-
.on("alterEndpointHandlers", platformMiddlewaresChain.get.bind(platformMiddlewaresChain))
|
|
31
|
-
.on("alterHandler", (handler, handlerMetadata) => {
|
|
32
|
-
handler = handlerMetadata.isRawMiddleware() ? handler : this.platformHandler.createHandler(handler, handlerMetadata);
|
|
33
|
-
return platformApplication.adapter.mapHandler(handler, handlerMetadata);
|
|
34
|
-
});
|
|
35
23
|
platformRouters.prebuild();
|
|
36
24
|
}
|
|
37
25
|
get app() {
|
|
@@ -74,13 +62,11 @@ let Platform = class Platform {
|
|
|
74
62
|
Platform = tslib_1.__decorate([
|
|
75
63
|
(0, di_1.Injectable)({
|
|
76
64
|
scope: di_1.ProviderScope.SINGLETON,
|
|
77
|
-
imports: []
|
|
65
|
+
imports: [PlatformHandler_1.PlatformHandler]
|
|
78
66
|
}),
|
|
79
67
|
tslib_1.__metadata("design:paramtypes", [di_1.InjectorService,
|
|
80
68
|
PlatformApplication_1.PlatformApplication,
|
|
81
|
-
platform_router_1.PlatformRouters
|
|
82
|
-
PlatformHandler_1.PlatformHandler,
|
|
83
|
-
PlatformMiddlewaresChain_1.PlatformMiddlewaresChain])
|
|
69
|
+
platform_router_1.PlatformRouters])
|
|
84
70
|
], Platform);
|
|
85
71
|
exports.Platform = Platform;
|
|
86
72
|
//# sourceMappingURL=Platform.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Platform.js","sourceRoot":"","sources":["../../../src/services/Platform.ts"],"names":[],"mappings":";;;;AAAA,
|
|
1
|
+
{"version":3,"file":"Platform.js","sourceRoot":"","sources":["../../../src/services/Platform.ts"],"names":[],"mappings":";;;;AAAA,iCAAuG;AACvG,2DAAsD;AAEtD,+DAA0D;AAC1D,uDAAkD;AAElD;;;;GAIG;AAKH,IAAa,QAAQ,GAArB,MAAa,QAAQ;IAIR;IACA;IACA;IALX,YAAY,GAAiC,IAAI,GAAG,EAAE,CAAC;IAEvD,YACW,QAAyB,EACzB,mBAAwC,EACxC,eAAgC;QAFhC,aAAQ,GAAR,QAAQ,CAAiB;QACzB,wBAAmB,GAAnB,mBAAmB,CAAqB;QACxC,oBAAe,GAAf,eAAe,CAAiB;QAEzC,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,C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ider} from \"@tsed/di\";\nimport {PlatformRouters} from \"@tsed/platform-router\";\nimport {Route, RouteController} from \"../interfaces/Route\";\nimport {PlatformApplication} from \"./PlatformApplication\";\nimport {PlatformHandler} from \"./PlatformHandler\";\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: [PlatformHandler]\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 ) {\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"]}
|
|
@@ -5,26 +5,48 @@ 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");
|
|
10
11
|
const platform_router_1 = require("@tsed/platform-router");
|
|
11
12
|
const util_1 = require("util");
|
|
12
13
|
const AnyToPromiseWithCtx_1 = require("../domain/AnyToPromiseWithCtx");
|
|
13
14
|
const setResponseHeaders_1 = require("../utils/setResponseHeaders");
|
|
15
|
+
const PlatformApplication_1 = require("./PlatformApplication");
|
|
16
|
+
const PlatformMiddlewaresChain_1 = require("./PlatformMiddlewaresChain");
|
|
14
17
|
/**
|
|
15
18
|
* Platform Handler abstraction layer. Wrap original class method to a pure platform handler (Express, Koa, etc...).
|
|
16
19
|
* @platform
|
|
17
20
|
*/
|
|
18
21
|
let PlatformHandler = class PlatformHandler {
|
|
19
|
-
responseFilter;
|
|
20
22
|
platformRouters;
|
|
23
|
+
responseFilter;
|
|
21
24
|
platformParams;
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
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
|
+
});
|
|
39
|
+
}
|
|
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;
|
|
28
50
|
}
|
|
29
51
|
/**
|
|
30
52
|
* @param provider
|
|
@@ -55,29 +77,20 @@ let PlatformHandler = class PlatformHandler {
|
|
|
55
77
|
data = await this.responseFilter.transform(data, $ctx);
|
|
56
78
|
response.body(data);
|
|
57
79
|
}
|
|
58
|
-
return response;
|
|
59
|
-
}
|
|
60
|
-
setResponseHeaders($ctx) {
|
|
61
|
-
if (!$ctx.response.isDone()) {
|
|
62
|
-
return (0, setResponseHeaders_1.setResponseHeaders)($ctx);
|
|
63
|
-
}
|
|
64
80
|
}
|
|
65
81
|
/**
|
|
66
|
-
* @param error
|
|
67
|
-
* @param next
|
|
68
82
|
* @param $ctx
|
|
69
83
|
*/
|
|
70
|
-
next(
|
|
84
|
+
next($ctx) {
|
|
71
85
|
if ((0, core_1.isStream)($ctx.data) || $ctx.isDone()) {
|
|
72
86
|
return;
|
|
73
87
|
}
|
|
74
|
-
return error ? next(error) : next();
|
|
88
|
+
return $ctx.error ? $ctx.next($ctx.error) : $ctx.next();
|
|
75
89
|
}
|
|
76
90
|
/**
|
|
77
91
|
* Call handler when a request his handle
|
|
78
92
|
*/
|
|
79
93
|
async onRequest(handler, $ctx) {
|
|
80
|
-
// istanbul ignore next$
|
|
81
94
|
if ($ctx.isDone()) {
|
|
82
95
|
logger_1.$log.error({
|
|
83
96
|
name: "HEADERS_SENT",
|
|
@@ -85,13 +98,16 @@ let PlatformHandler = class PlatformHandler {
|
|
|
85
98
|
});
|
|
86
99
|
return;
|
|
87
100
|
}
|
|
101
|
+
if (($ctx.error instanceof Error && !$ctx.handlerMetadata.hasErrorParam) || ($ctx.handlerMetadata.hasErrorParam && !$ctx.error)) {
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
88
104
|
try {
|
|
89
105
|
const { handlerMetadata } = $ctx;
|
|
90
106
|
if (handlerMetadata.type === platform_router_1.PlatformHandlerType.CTX_FN) {
|
|
91
107
|
return await handler({ $ctx });
|
|
92
108
|
}
|
|
93
109
|
const resolver = new AnyToPromiseWithCtx_1.AnyToPromiseWithCtx($ctx);
|
|
94
|
-
const { state, data, status, headers } = await resolver.call(handler);
|
|
110
|
+
const { state, type, data, status, headers } = await resolver.call(handler);
|
|
95
111
|
// Note: restore previous handler metadata (for OIDC)
|
|
96
112
|
$ctx.handlerMetadata = handlerMetadata;
|
|
97
113
|
if (state === core_1.AnyToPromiseStatus.RESOLVED) {
|
|
@@ -104,59 +120,57 @@ let PlatformHandler = class PlatformHandler {
|
|
|
104
120
|
if (data !== undefined) {
|
|
105
121
|
$ctx.data = data;
|
|
106
122
|
}
|
|
107
|
-
|
|
108
|
-
|
|
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);
|
|
128
|
+
}
|
|
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());
|
|
132
|
+
}
|
|
133
|
+
if (type === core_1.AnyToPromiseResponseTypes.STREAM) {
|
|
134
|
+
return this.flush($ctx);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
109
137
|
}
|
|
110
138
|
}
|
|
111
139
|
catch (error) {
|
|
112
|
-
|
|
140
|
+
$ctx.error = error;
|
|
141
|
+
throw error;
|
|
113
142
|
}
|
|
114
143
|
}
|
|
115
|
-
async
|
|
116
|
-
$ctx.error =
|
|
117
|
-
|
|
118
|
-
}
|
|
119
|
-
/**
|
|
120
|
-
* Manage success scenario
|
|
121
|
-
* @param $ctx
|
|
122
|
-
* @protected
|
|
123
|
-
*/
|
|
124
|
-
async onSuccess($ctx) {
|
|
125
|
-
// istanbul ignore next
|
|
126
|
-
if ($ctx.isDone()) {
|
|
127
|
-
return;
|
|
128
|
-
}
|
|
129
|
-
$ctx.error = null;
|
|
130
|
-
const metadata = $ctx.handlerMetadata;
|
|
131
|
-
// set headers each times that an endpoint is called
|
|
132
|
-
if (metadata.isEndpoint()) {
|
|
133
|
-
this.setResponseHeaders($ctx);
|
|
134
|
-
}
|
|
135
|
-
// call returned middleware
|
|
136
|
-
if ((0, core_1.isFunction)($ctx.data) && !(0, core_1.isStream)($ctx.data)) {
|
|
137
|
-
return (0, util_1.promisify)($ctx.data)($ctx.getRequest(), $ctx.getResponse());
|
|
138
|
-
}
|
|
139
|
-
if (metadata.isFinal()) {
|
|
140
|
-
return this.flush($ctx);
|
|
141
|
-
}
|
|
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);
|
|
142
147
|
}
|
|
143
148
|
};
|
|
144
149
|
tslib_1.__decorate([
|
|
145
150
|
(0, di_1.Inject)(),
|
|
146
151
|
tslib_1.__metadata("design:type", platform_response_filter_1.PlatformResponseFilter)
|
|
147
152
|
], PlatformHandler.prototype, "responseFilter", void 0);
|
|
148
|
-
tslib_1.__decorate([
|
|
149
|
-
(0, di_1.Inject)(),
|
|
150
|
-
tslib_1.__metadata("design:type", platform_router_1.PlatformRouters)
|
|
151
|
-
], PlatformHandler.prototype, "platformRouters", void 0);
|
|
152
153
|
tslib_1.__decorate([
|
|
153
154
|
(0, di_1.Inject)(),
|
|
154
155
|
tslib_1.__metadata("design:type", platform_params_1.PlatformParams)
|
|
155
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);
|
|
156
169
|
PlatformHandler = tslib_1.__decorate([
|
|
157
170
|
(0, di_1.Injectable)({
|
|
158
171
|
scope: di_1.ProviderScope.SINGLETON
|
|
159
|
-
})
|
|
172
|
+
}),
|
|
173
|
+
tslib_1.__metadata("design:paramtypes", [platform_router_1.PlatformRouters])
|
|
160
174
|
], PlatformHandler);
|
|
161
175
|
exports.PlatformHandler = PlatformHandler;
|
|
162
176
|
//# sourceMappingURL=PlatformHandler.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"PlatformHandler.js","sourceRoot":"","sources":["../../../src/services/PlatformHandler.ts"],"names":[],"mappings":";;;;AAAA,qCAAqF;AACrF,iCAAqE;AACrE,yCAAkC;AAClC,2DAA6E;AAC7E,6EAAsE;AACtE,2DAA4H;AAC5H,+BAA+B;AAC/B,uEAAkE;AAElE,oEAA+D;AAE/D;;;GAGG;AAIH,IAAa,eAAe,GAA5B,MAAa,eAAe;IAEhB,cAAc,CAAyB;IAGvC,eAAe,CAAkB;IAGjC,cAAc,CAAiB;IAEzC,aAAa,CAAC,OAA+B,EAAE,eAAwC;QACrF,OAAO,KAAK,EAAE,IAAqB,EAAE,EAAE;YACrC,IAAI,CAAC,eAAe,GAAG,eAAe,CAAC;YAEvC,MAAM,KAAK,GAAG,MAAM,IAAA,sBAAe,EAAC,GAAG,EAAE,CAAC,IAAI,CAAC,SAAS,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC,CAAC;YAEzE,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;AAR9B,eAAe;IAH3B,IAAA,eAAU,EAAC;QACV,KAAK,EAAE,kBAAa,CAAC,SAAS;KAC/B,CAAC;GACW,eAAe,CAgK3B;AAhKY,0CAAe","sourcesContent":["import {AnyToPromiseStatus, catchAsyncError, isFunction, isStream} from \"@tsed/core\";\nimport {Inject, Injectable, Provider, ProviderScope} from \"@tsed/di\";\nimport {$log} from \"@tsed/logger\";\nimport {PlatformParams, PlatformParamsCallback} from \"@tsed/platform-params\";\nimport {PlatformResponseFilter} from \"@tsed/platform-response-filter\";\nimport {PlatformContextHandler, PlatformHandlerMetadata, PlatformHandlerType, PlatformRouters} from \"@tsed/platform-router\";\nimport {promisify} from \"util\";\nimport {AnyToPromiseWithCtx} from \"../domain/AnyToPromiseWithCtx\";\nimport {PlatformContext} from \"../domain/PlatformContext\";\nimport {setResponseHeaders} from \"../utils/setResponseHeaders\";\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 platformRouters: PlatformRouters;\n\n @Inject()\n protected platformParams: PlatformParams;\n\n createHandler(handler: PlatformParamsCallback, handlerMetadata: PlatformHandlerMetadata): PlatformContextHandler {\n return async ($ctx: PlatformContext) => {\n $ctx.handlerMetadata = handlerMetadata;\n\n const error = await catchAsyncError(() => this.onRequest(handler, $ctx));\n\n return this.next(error, $ctx.next, $ctx);\n };\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 return response;\n }\n\n public setResponseHeaders($ctx: PlatformContext) {\n if (!$ctx.response.isDone()) {\n return setResponseHeaders($ctx);\n }\n }\n\n /**\n * @param error\n * @param next\n * @param $ctx\n */\n next(error: unknown, next: Function, $ctx: PlatformContext) {\n if (isStream($ctx.data) || $ctx.isDone()) {\n return;\n }\n\n return error ? next(error) : next();\n }\n\n /**\n * Call handler when a request his handle\n */\n async onRequest(handler: PlatformParamsCallback, $ctx: PlatformContext): Promise<any> {\n // istanbul ignore next$\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 try {\n const {handlerMetadata} = $ctx;\n if (handlerMetadata.type === PlatformHandlerType.CTX_FN) {\n return await handler({$ctx});\n }\n\n const resolver = new AnyToPromiseWithCtx($ctx);\n\n const {state, 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 // Can be canceled by the handler itself\n return await this.onSuccess($ctx);\n }\n } catch (error) {\n return this.onError(error, $ctx);\n }\n }\n\n protected async onError(error: unknown, $ctx: PlatformContext) {\n $ctx.error = error;\n\n throw error;\n }\n\n /**\n * Manage success scenario\n * @param $ctx\n * @protected\n */\n protected async onSuccess($ctx: PlatformContext) {\n // istanbul ignore next\n if ($ctx.isDone()) {\n return;\n }\n\n $ctx.error = null;\n\n const metadata = $ctx.handlerMetadata;\n\n // set headers each times that an endpoint is called\n\n if (metadata.isEndpoint()) {\n this.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 (metadata.isFinal()) {\n return this.flush($ctx);\n }\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,oCAAkB,EAAC,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;QAC3E,CAAC,CAAC;aACD,EAAE,CAAC,cAAc,EAAE,CAAC,eAAwC,EAAE,EAAE;YAC/D,OAAO,IAAI,CAAC,mBAAmB,CAAC,OAAO,CAAC,UAAU,CAAC,IAAI,CAAC,aAAa,CAAC,eAAe,CAAC,EAAE,eAAe,CAAC,CAAC;QAC3G,CAAC,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AA,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"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"PlatformRouteOptions.js","sourceRoot":"","sources":["../../../src/interfaces/PlatformRouteOptions.ts"],"names":[],"mappings":"","sourcesContent":["import {Type} from \"@tsed/core\";\nimport {PathType} from \"@tsed/di\";\n\n/**\n * @ignore\n */\nexport interface PlatformRouteOptions {\n token?: Type<any>;\n path?: PathType | undefined;\n method?: string;\n
|
|
1
|
+
{"version":3,"file":"PlatformRouteOptions.js","sourceRoot":"","sources":["../../../src/interfaces/PlatformRouteOptions.ts"],"names":[],"mappings":"","sourcesContent":["import {Type} from \"@tsed/core\";\nimport {PathType} from \"@tsed/di\";\n\n/**\n * @ignore\n */\nexport interface PlatformRouteOptions {\n token?: Type<any>;\n path?: PathType | undefined;\n method?: string;\n handlers: any[];\n}\n\n/**\n * @ignore\n */\nexport type PlatformRouteWithoutHandlers = Partial<Omit<PlatformRouteOptions, \"handlers\">>;\n"]}
|
|
@@ -3,7 +3,6 @@ import { Injectable, InjectorService, ProviderScope } from "@tsed/di";
|
|
|
3
3
|
import { PlatformRouters } from "@tsed/platform-router";
|
|
4
4
|
import { PlatformApplication } from "./PlatformApplication.js";
|
|
5
5
|
import { PlatformHandler } from "./PlatformHandler.js";
|
|
6
|
-
import { PlatformMiddlewaresChain } from "./PlatformMiddlewaresChain.js";
|
|
7
6
|
/**
|
|
8
7
|
* `Platform` is used to provide all routes collected by annotation `@Controller`.
|
|
9
8
|
*
|
|
@@ -13,22 +12,11 @@ let Platform = class Platform {
|
|
|
13
12
|
injector;
|
|
14
13
|
platformApplication;
|
|
15
14
|
platformRouters;
|
|
16
|
-
platformHandler;
|
|
17
|
-
platformMiddlewaresChain;
|
|
18
15
|
#controllers = new Map();
|
|
19
|
-
constructor(injector, platformApplication, platformRouters
|
|
16
|
+
constructor(injector, platformApplication, platformRouters) {
|
|
20
17
|
this.injector = injector;
|
|
21
18
|
this.platformApplication = platformApplication;
|
|
22
19
|
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
20
|
platformRouters.prebuild();
|
|
33
21
|
}
|
|
34
22
|
get app() {
|
|
@@ -71,13 +59,11 @@ let Platform = class Platform {
|
|
|
71
59
|
Platform = __decorate([
|
|
72
60
|
Injectable({
|
|
73
61
|
scope: ProviderScope.SINGLETON,
|
|
74
|
-
imports: []
|
|
62
|
+
imports: [PlatformHandler]
|
|
75
63
|
}),
|
|
76
64
|
__metadata("design:paramtypes", [InjectorService,
|
|
77
65
|
PlatformApplication,
|
|
78
|
-
PlatformRouters
|
|
79
|
-
PlatformHandler,
|
|
80
|
-
PlatformMiddlewaresChain])
|
|
66
|
+
PlatformRouters])
|
|
81
67
|
], Platform);
|
|
82
68
|
export { Platform };
|
|
83
69
|
//# sourceMappingURL=Platform.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Platform.js","sourceRoot":"","sources":["../../../src/services/Platform.ts"],"names":[],"mappings":";AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"Platform.js","sourceRoot":"","sources":["../../../src/services/Platform.ts"],"names":[],"mappings":";AAAA,OAAO,EAAqB,UAAU,EAAE,eAAe,EAAE,aAAa,EAAgB,MAAM,UAAU,CAAC;AACvG,OAAO,EAAC,eAAe,EAAC,MAAM,uBAAuB,CAAC;AAEtD,OAAO,EAAC,mBAAmB,EAAC,MAAM,uBAAuB,CAAC;AAC1D,OAAO,EAAC,eAAe,EAAC,MAAM,mBAAmB,CAAC;AAElD;;;;GA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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;AAzDY,QAAQ;IAJpB,UAAU,CAAC;QACV,KAAK,EAAE,aAAa,CAAC,SAAS;QAC9B,OAAO,EAAE,CAAC,eAAe,CAAC;KAC3B,CAAC;qCAKqB,eAAe;QACJ,mBAAmB;QACvB,eAAe;GANhC,QAAQ,CAyDpB;SAzDY,QAAQ","sourcesContent":["import {ControllerProvider, Injectable, InjectorService, ProviderScope, TokenProvider} from \"@tsed/di\";\nimport {PlatformRouters} from \"@tsed/platform-router\";\nimport {Route, RouteController} from \"../interfaces/Route\";\nimport {PlatformApplication} from \"./PlatformApplication\";\nimport {PlatformHandler} from \"./PlatformHandler\";\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: [PlatformHandler]\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 ) {\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,27 +1,49 @@
|
|
|
1
1
|
import { __decorate, __metadata } from "tslib";
|
|
2
|
-
import { AnyToPromiseStatus, catchAsyncError, isFunction, isStream } from "@tsed/core";
|
|
2
|
+
import { AnyToPromiseResponseTypes, AnyToPromiseStatus, catchAsyncError, isFunction, isStream } from "@tsed/core";
|
|
3
3
|
import { Inject, Injectable, ProviderScope } from "@tsed/di";
|
|
4
4
|
import { $log } from "@tsed/logger";
|
|
5
|
+
import { PlatformExceptions } from "@tsed/platform-exceptions";
|
|
5
6
|
import { PlatformParams } from "@tsed/platform-params";
|
|
6
7
|
import { PlatformResponseFilter } from "@tsed/platform-response-filter";
|
|
7
|
-
import { PlatformHandlerMetadata, PlatformHandlerType, PlatformRouters } from "@tsed/platform-router";
|
|
8
|
+
import { PlatformHandlerMetadata, PlatformHandlerType, PlatformRouters, useResponseHandler } from "@tsed/platform-router";
|
|
8
9
|
import { promisify } from "util";
|
|
9
10
|
import { AnyToPromiseWithCtx } from "../domain/AnyToPromiseWithCtx.js";
|
|
10
11
|
import { setResponseHeaders } from "../utils/setResponseHeaders.js";
|
|
12
|
+
import { PlatformApplication } from "./PlatformApplication.js";
|
|
13
|
+
import { PlatformMiddlewaresChain } from "./PlatformMiddlewaresChain.js";
|
|
11
14
|
/**
|
|
12
15
|
* Platform Handler abstraction layer. Wrap original class method to a pure platform handler (Express, Koa, etc...).
|
|
13
16
|
* @platform
|
|
14
17
|
*/
|
|
15
18
|
let PlatformHandler = class PlatformHandler {
|
|
16
|
-
responseFilter;
|
|
17
19
|
platformRouters;
|
|
20
|
+
responseFilter;
|
|
18
21
|
platformParams;
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
22
|
+
platformExceptions;
|
|
23
|
+
platformApplication;
|
|
24
|
+
platformMiddlewaresChain;
|
|
25
|
+
constructor(platformRouters) {
|
|
26
|
+
this.platformRouters = platformRouters;
|
|
27
|
+
// configure the router module
|
|
28
|
+
platformRouters.hooks
|
|
29
|
+
.on("alterEndpointHandlers", (allMiddlewares, operationRoute) => {
|
|
30
|
+
allMiddlewares = this.platformMiddlewaresChain.get(allMiddlewares, operationRoute);
|
|
31
|
+
return [...allMiddlewares, useResponseHandler(this.onFinish.bind(this))];
|
|
32
|
+
})
|
|
33
|
+
.on("alterHandler", (handlerMetadata) => {
|
|
34
|
+
return this.platformApplication.adapter.mapHandler(this.createHandler(handlerMetadata), handlerMetadata);
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
createHandler(handlerMetadata) {
|
|
38
|
+
if (handlerMetadata.isInjectable()) {
|
|
39
|
+
const handler = this.platformParams.compileHandler(handlerMetadata);
|
|
40
|
+
return async ($ctx) => {
|
|
41
|
+
$ctx.handlerMetadata = handlerMetadata;
|
|
42
|
+
await catchAsyncError(() => this.onRequest(handler, $ctx));
|
|
43
|
+
return this.next($ctx);
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
return handlerMetadata.handler;
|
|
25
47
|
}
|
|
26
48
|
/**
|
|
27
49
|
* @param provider
|
|
@@ -52,29 +74,20 @@ let PlatformHandler = class PlatformHandler {
|
|
|
52
74
|
data = await this.responseFilter.transform(data, $ctx);
|
|
53
75
|
response.body(data);
|
|
54
76
|
}
|
|
55
|
-
return response;
|
|
56
|
-
}
|
|
57
|
-
setResponseHeaders($ctx) {
|
|
58
|
-
if (!$ctx.response.isDone()) {
|
|
59
|
-
return setResponseHeaders($ctx);
|
|
60
|
-
}
|
|
61
77
|
}
|
|
62
78
|
/**
|
|
63
|
-
* @param error
|
|
64
|
-
* @param next
|
|
65
79
|
* @param $ctx
|
|
66
80
|
*/
|
|
67
|
-
next(
|
|
81
|
+
next($ctx) {
|
|
68
82
|
if (isStream($ctx.data) || $ctx.isDone()) {
|
|
69
83
|
return;
|
|
70
84
|
}
|
|
71
|
-
return error ? next(error) : next();
|
|
85
|
+
return $ctx.error ? $ctx.next($ctx.error) : $ctx.next();
|
|
72
86
|
}
|
|
73
87
|
/**
|
|
74
88
|
* Call handler when a request his handle
|
|
75
89
|
*/
|
|
76
90
|
async onRequest(handler, $ctx) {
|
|
77
|
-
// istanbul ignore next$
|
|
78
91
|
if ($ctx.isDone()) {
|
|
79
92
|
$log.error({
|
|
80
93
|
name: "HEADERS_SENT",
|
|
@@ -82,13 +95,16 @@ let PlatformHandler = class PlatformHandler {
|
|
|
82
95
|
});
|
|
83
96
|
return;
|
|
84
97
|
}
|
|
98
|
+
if (($ctx.error instanceof Error && !$ctx.handlerMetadata.hasErrorParam) || ($ctx.handlerMetadata.hasErrorParam && !$ctx.error)) {
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
85
101
|
try {
|
|
86
102
|
const { handlerMetadata } = $ctx;
|
|
87
103
|
if (handlerMetadata.type === PlatformHandlerType.CTX_FN) {
|
|
88
104
|
return await handler({ $ctx });
|
|
89
105
|
}
|
|
90
106
|
const resolver = new AnyToPromiseWithCtx($ctx);
|
|
91
|
-
const { state, data, status, headers } = await resolver.call(handler);
|
|
107
|
+
const { state, type, data, status, headers } = await resolver.call(handler);
|
|
92
108
|
// Note: restore previous handler metadata (for OIDC)
|
|
93
109
|
$ctx.handlerMetadata = handlerMetadata;
|
|
94
110
|
if (state === AnyToPromiseStatus.RESOLVED) {
|
|
@@ -101,59 +117,57 @@ let PlatformHandler = class PlatformHandler {
|
|
|
101
117
|
if (data !== undefined) {
|
|
102
118
|
$ctx.data = data;
|
|
103
119
|
}
|
|
104
|
-
|
|
105
|
-
|
|
120
|
+
if (!$ctx.isDone()) {
|
|
121
|
+
$ctx.error = null;
|
|
122
|
+
// set headers each times that an endpoint is called
|
|
123
|
+
if (handlerMetadata.isEndpoint()) {
|
|
124
|
+
setResponseHeaders($ctx);
|
|
125
|
+
}
|
|
126
|
+
// call returned middleware
|
|
127
|
+
if (isFunction($ctx.data) && !isStream($ctx.data)) {
|
|
128
|
+
return promisify($ctx.data)($ctx.getRequest(), $ctx.getResponse());
|
|
129
|
+
}
|
|
130
|
+
if (type === AnyToPromiseResponseTypes.STREAM) {
|
|
131
|
+
return this.flush($ctx);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
106
134
|
}
|
|
107
135
|
}
|
|
108
136
|
catch (error) {
|
|
109
|
-
|
|
137
|
+
$ctx.error = error;
|
|
138
|
+
throw error;
|
|
110
139
|
}
|
|
111
140
|
}
|
|
112
|
-
async
|
|
113
|
-
$ctx.error =
|
|
114
|
-
|
|
115
|
-
}
|
|
116
|
-
/**
|
|
117
|
-
* Manage success scenario
|
|
118
|
-
* @param $ctx
|
|
119
|
-
* @protected
|
|
120
|
-
*/
|
|
121
|
-
async onSuccess($ctx) {
|
|
122
|
-
// istanbul ignore next
|
|
123
|
-
if ($ctx.isDone()) {
|
|
124
|
-
return;
|
|
125
|
-
}
|
|
126
|
-
$ctx.error = null;
|
|
127
|
-
const metadata = $ctx.handlerMetadata;
|
|
128
|
-
// set headers each times that an endpoint is called
|
|
129
|
-
if (metadata.isEndpoint()) {
|
|
130
|
-
this.setResponseHeaders($ctx);
|
|
131
|
-
}
|
|
132
|
-
// call returned middleware
|
|
133
|
-
if (isFunction($ctx.data) && !isStream($ctx.data)) {
|
|
134
|
-
return promisify($ctx.data)($ctx.getRequest(), $ctx.getResponse());
|
|
135
|
-
}
|
|
136
|
-
if (metadata.isFinal()) {
|
|
137
|
-
return this.flush($ctx);
|
|
138
|
-
}
|
|
141
|
+
async onFinish($ctx) {
|
|
142
|
+
$ctx.error = await catchAsyncError(() => this.flush($ctx));
|
|
143
|
+
return $ctx.error && this.platformExceptions.catch($ctx.error, $ctx);
|
|
139
144
|
}
|
|
140
145
|
};
|
|
141
146
|
__decorate([
|
|
142
147
|
Inject(),
|
|
143
148
|
__metadata("design:type", PlatformResponseFilter)
|
|
144
149
|
], PlatformHandler.prototype, "responseFilter", void 0);
|
|
145
|
-
__decorate([
|
|
146
|
-
Inject(),
|
|
147
|
-
__metadata("design:type", PlatformRouters)
|
|
148
|
-
], PlatformHandler.prototype, "platformRouters", void 0);
|
|
149
150
|
__decorate([
|
|
150
151
|
Inject(),
|
|
151
152
|
__metadata("design:type", PlatformParams)
|
|
152
153
|
], PlatformHandler.prototype, "platformParams", void 0);
|
|
154
|
+
__decorate([
|
|
155
|
+
Inject(),
|
|
156
|
+
__metadata("design:type", PlatformExceptions)
|
|
157
|
+
], PlatformHandler.prototype, "platformExceptions", void 0);
|
|
158
|
+
__decorate([
|
|
159
|
+
Inject(),
|
|
160
|
+
__metadata("design:type", PlatformApplication)
|
|
161
|
+
], PlatformHandler.prototype, "platformApplication", void 0);
|
|
162
|
+
__decorate([
|
|
163
|
+
Inject(),
|
|
164
|
+
__metadata("design:type", PlatformMiddlewaresChain)
|
|
165
|
+
], PlatformHandler.prototype, "platformMiddlewaresChain", void 0);
|
|
153
166
|
PlatformHandler = __decorate([
|
|
154
167
|
Injectable({
|
|
155
168
|
scope: ProviderScope.SINGLETON
|
|
156
|
-
})
|
|
169
|
+
}),
|
|
170
|
+
__metadata("design:paramtypes", [PlatformRouters])
|
|
157
171
|
], PlatformHandler);
|
|
158
172
|
export { PlatformHandler };
|
|
159
173
|
//# sourceMappingURL=PlatformHandler.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"PlatformHandler.js","sourceRoot":"","sources":["../../../src/services/PlatformHandler.ts"],"names":[],"mappings":";AAAA,OAAO,EAAC,kBAAkB,EAAE,eAAe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;OAIG;IACO,KAAK,CAAC,SAAS,CAAC,IAAqB;QAC7C,uBAAuB;QACvB,IAAI,IAAI,CAAC,MAAM,EAAE,EAAE;YACjB,OAAO;SACR;QAED,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC;QAElB,MAAM,QAAQ,GAAG,IAAI,CAAC,eAAe,CAAC;QAEtC,oDAAoD;QAEpD,IAAI,QAAQ,CAAC,UAAU,EAAE,EAAE;YACzB,IAAI,CAAC,kBAAkB,CAAC,IAAI,CAAC,CAAC;SAC/B;QAED,2BAA2B;QAC3B,IAAI,UAAU,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE;YACjD,OAAO,SAAS,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,IAAI,CAAC,UAAU,EAAE,EAAE,IAAI,CAAC,WAAW,EAAE,CAAC,CAAC;SACpE;QAED,IAAI,QAAQ,CAAC,OAAO,EAAE,EAAE;YACtB,OAAO,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;SACzB;IACH,CAAC;CACF,CAAA;AA9JC;IADC,MAAM,EAAE;8BACiB,sBAAsB;uDAAC;AAGjD;IADC,MAAM,EAAE;8BACkB,eAAe;wDAAC;AAG3C;IADC,MAAM,EAAE;8BACiB,cAAc;uDAAC;AAR9B,eAAe;IAH3B,UAAU,CAAC;QACV,KAAK,EAAE,aAAa,CAAC,SAAS;KAC/B,CAAC;GACW,eAAe,CAgK3B;SAhKY,eAAe","sourcesContent":["import {AnyToPromiseStatus, catchAsyncError, isFunction, isStream} from \"@tsed/core\";\nimport {Inject, Injectable, Provider, ProviderScope} from \"@tsed/di\";\nimport {$log} from \"@tsed/logger\";\nimport {PlatformParams, PlatformParamsCallback} from \"@tsed/platform-params\";\nimport {PlatformResponseFilter} from \"@tsed/platform-response-filter\";\nimport {PlatformContextHandler, PlatformHandlerMetadata, PlatformHandlerType, PlatformRouters} from \"@tsed/platform-router\";\nimport {promisify} from \"util\";\nimport {AnyToPromiseWithCtx} from \"../domain/AnyToPromiseWithCtx\";\nimport {PlatformContext} from \"../domain/PlatformContext\";\nimport {setResponseHeaders} from \"../utils/setResponseHeaders\";\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 platformRouters: PlatformRouters;\n\n @Inject()\n protected platformParams: PlatformParams;\n\n createHandler(handler: PlatformParamsCallback, handlerMetadata: PlatformHandlerMetadata): PlatformContextHandler {\n return async ($ctx: PlatformContext) => {\n $ctx.handlerMetadata = handlerMetadata;\n\n const error = await catchAsyncError(() => this.onRequest(handler, $ctx));\n\n return this.next(error, $ctx.next, $ctx);\n };\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 return response;\n }\n\n public setResponseHeaders($ctx: PlatformContext) {\n if (!$ctx.response.isDone()) {\n return setResponseHeaders($ctx);\n }\n }\n\n /**\n * @param error\n * @param next\n * @param $ctx\n */\n next(error: unknown, next: Function, $ctx: PlatformContext) {\n if (isStream($ctx.data) || $ctx.isDone()) {\n return;\n }\n\n return error ? next(error) : next();\n }\n\n /**\n * Call handler when a request his handle\n */\n async onRequest(handler: PlatformParamsCallback, $ctx: PlatformContext): Promise<any> {\n // istanbul ignore next$\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 try {\n const {handlerMetadata} = $ctx;\n if (handlerMetadata.type === PlatformHandlerType.CTX_FN) {\n return await handler({$ctx});\n }\n\n const resolver = new AnyToPromiseWithCtx($ctx);\n\n const {state, 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 // Can be canceled by the handler itself\n return await this.onSuccess($ctx);\n }\n } catch (error) {\n return this.onError(error, $ctx);\n }\n }\n\n protected async onError(error: unknown, $ctx: PlatformContext) {\n $ctx.error = error;\n\n throw error;\n }\n\n /**\n * Manage success scenario\n * @param $ctx\n * @protected\n */\n protected async onSuccess($ctx: PlatformContext) {\n // istanbul ignore next\n if ($ctx.isDone()) {\n return;\n }\n\n $ctx.error = null;\n\n const metadata = $ctx.handlerMetadata;\n\n // set headers each times that an endpoint is called\n\n if (metadata.isEndpoint()) {\n this.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 (metadata.isFinal()) {\n return this.flush($ctx);\n }\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"PlatformHandler.js","sourceRoot":"","sources":["../../../src/services/PlatformHandler.ts"],"names":[],"mappings":";AAAA,OAAO,EAAC,yBAAyB,EAAE,kBAAkB,EAAE,eAAe,EAAE,UAAU,EAAE,QAAQ,EAAC,MAAM,YAAY,CAAC;AAChH,OAAO,EAAC,M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K,CAAC;SACb;IACH,CAAC;IAED,KAAK,CAAC,QAAQ,CAAC,IAAqB;QAClC,IAAI,CAAC,KAAK,GAAG,MAAM,eAAe,CAAC,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,MAAM,EAAE;8BACiB,sBAAsB;uDAAC;AAGjD;IADC,MAAM,EAAE;8BACiB,cAAc;uDAAC;AAGzC;IADC,MAAM,EAAE;8BACqB,kBAAkB;2DAAC;AAGjD;IADC,MAAM,EAAE;8BACsB,mBAAmB;4DAAC;AAGnD;IADC,MAAM,EAAE;8BAC2B,wBAAwB;iEAAC;AAdlD,eAAe;IAH3B,UAAU,CAAC;QACV,KAAK,EAAE,aAAa,CAAC,SAAS;KAC/B,CAAC;qCAiBuC,eAAe;GAhB3C,eAAe,CAqK3B;SArKY,eAAe","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,8 +2,6 @@ import { InjectorService, TokenProvider } from "@tsed/di";
|
|
|
2
2
|
import { PlatformRouters } from "@tsed/platform-router";
|
|
3
3
|
import { Route, RouteController } from "../interfaces/Route";
|
|
4
4
|
import { PlatformApplication } from "./PlatformApplication";
|
|
5
|
-
import { PlatformHandler } from "./PlatformHandler";
|
|
6
|
-
import { PlatformMiddlewaresChain } from "./PlatformMiddlewaresChain";
|
|
7
5
|
/**
|
|
8
6
|
* `Platform` is used to provide all routes collected by annotation `@Controller`.
|
|
9
7
|
*
|
|
@@ -14,9 +12,7 @@ export declare class Platform {
|
|
|
14
12
|
readonly injector: InjectorService;
|
|
15
13
|
readonly platformApplication: PlatformApplication;
|
|
16
14
|
readonly platformRouters: PlatformRouters;
|
|
17
|
-
|
|
18
|
-
readonly platformMiddlewaresChain: PlatformMiddlewaresChain;
|
|
19
|
-
constructor(injector: InjectorService, platformApplication: PlatformApplication, platformRouters: PlatformRouters, platformHandler: PlatformHandler, platformMiddlewaresChain: PlatformMiddlewaresChain);
|
|
15
|
+
constructor(injector: InjectorService, platformApplication: PlatformApplication, platformRouters: PlatformRouters);
|
|
20
16
|
get app(): PlatformApplication<TsED.Application>;
|
|
21
17
|
addRoutes(routes: Route[]): void;
|
|
22
18
|
addRoute(route: string, token: TokenProvider): this;
|
|
@@ -1,17 +1,24 @@
|
|
|
1
1
|
import { Provider } from "@tsed/di";
|
|
2
|
+
import { PlatformExceptions } from "@tsed/platform-exceptions";
|
|
2
3
|
import { PlatformParams, PlatformParamsCallback } from "@tsed/platform-params";
|
|
3
4
|
import { PlatformResponseFilter } from "@tsed/platform-response-filter";
|
|
4
|
-
import {
|
|
5
|
+
import { PlatformHandlerMetadata, PlatformRouters } from "@tsed/platform-router";
|
|
5
6
|
import { PlatformContext } from "../domain/PlatformContext";
|
|
7
|
+
import { PlatformApplication } from "./PlatformApplication";
|
|
8
|
+
import { PlatformMiddlewaresChain } from "./PlatformMiddlewaresChain";
|
|
6
9
|
/**
|
|
7
10
|
* Platform Handler abstraction layer. Wrap original class method to a pure platform handler (Express, Koa, etc...).
|
|
8
11
|
* @platform
|
|
9
12
|
*/
|
|
10
13
|
export declare class PlatformHandler {
|
|
11
|
-
protected responseFilter: PlatformResponseFilter;
|
|
12
14
|
protected platformRouters: PlatformRouters;
|
|
15
|
+
protected responseFilter: PlatformResponseFilter;
|
|
13
16
|
protected platformParams: PlatformParams;
|
|
14
|
-
|
|
17
|
+
protected platformExceptions: PlatformExceptions;
|
|
18
|
+
protected platformApplication: PlatformApplication;
|
|
19
|
+
protected platformMiddlewaresChain: PlatformMiddlewaresChain;
|
|
20
|
+
constructor(platformRouters: PlatformRouters);
|
|
21
|
+
createHandler(handlerMetadata: PlatformHandlerMetadata): any;
|
|
15
22
|
/**
|
|
16
23
|
* @param provider
|
|
17
24
|
* @param propertyKey
|
|
@@ -22,23 +29,14 @@ export declare class PlatformHandler {
|
|
|
22
29
|
* @protected
|
|
23
30
|
* @param $ctx
|
|
24
31
|
*/
|
|
25
|
-
flush($ctx: PlatformContext): Promise<
|
|
26
|
-
setResponseHeaders($ctx: PlatformContext): void;
|
|
32
|
+
flush($ctx: PlatformContext): Promise<void>;
|
|
27
33
|
/**
|
|
28
|
-
* @param error
|
|
29
|
-
* @param next
|
|
30
34
|
* @param $ctx
|
|
31
35
|
*/
|
|
32
|
-
next(
|
|
36
|
+
next($ctx: PlatformContext): any;
|
|
33
37
|
/**
|
|
34
38
|
* Call handler when a request his handle
|
|
35
39
|
*/
|
|
36
40
|
onRequest(handler: PlatformParamsCallback, $ctx: PlatformContext): Promise<any>;
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* Manage success scenario
|
|
40
|
-
* @param $ctx
|
|
41
|
-
* @protected
|
|
42
|
-
*/
|
|
43
|
-
protected onSuccess($ctx: PlatformContext): Promise<any>;
|
|
41
|
+
onFinish($ctx: PlatformContext): Promise<unknown>;
|
|
44
42
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tsed/common",
|
|
3
|
-
"version": "7.0.0-beta.
|
|
3
|
+
"version": "7.0.0-beta.20",
|
|
4
4
|
"description": "A TypeScript Framework on top of Express",
|
|
5
5
|
"private": false,
|
|
6
6
|
"source": "./src/index.ts",
|
|
@@ -62,23 +62,23 @@
|
|
|
62
62
|
}
|
|
63
63
|
],
|
|
64
64
|
"dependencies": {
|
|
65
|
-
"@tsed/components-scan": "7.0.0-beta.
|
|
66
|
-
"@tsed/core": "7.0.0-beta.
|
|
67
|
-
"@tsed/di": "7.0.0-beta.
|
|
68
|
-
"@tsed/exceptions": "7.0.0-beta.
|
|
69
|
-
"@tsed/json-mapper": "7.0.0-beta.
|
|
65
|
+
"@tsed/components-scan": "7.0.0-beta.20",
|
|
66
|
+
"@tsed/core": "7.0.0-beta.20",
|
|
67
|
+
"@tsed/di": "7.0.0-beta.20",
|
|
68
|
+
"@tsed/exceptions": "7.0.0-beta.20",
|
|
69
|
+
"@tsed/json-mapper": "7.0.0-beta.20",
|
|
70
70
|
"@tsed/logger": ">=6.2.0",
|
|
71
71
|
"@tsed/logger-file": ">=6.2.0",
|
|
72
|
-
"@tsed/perf": "7.0.0-beta.
|
|
73
|
-
"@tsed/platform-cache": "7.0.0-beta.
|
|
74
|
-
"@tsed/platform-exceptions": "7.0.0-beta.
|
|
75
|
-
"@tsed/platform-log-middleware": "7.0.0-beta.
|
|
76
|
-
"@tsed/platform-middlewares": "7.0.0-beta.
|
|
77
|
-
"@tsed/platform-params": "7.0.0-beta.
|
|
78
|
-
"@tsed/platform-response-filter": "7.0.0-beta.
|
|
79
|
-
"@tsed/platform-router": "7.0.0-beta.
|
|
80
|
-
"@tsed/platform-views": "7.0.0-beta.
|
|
81
|
-
"@tsed/schema": "7.0.0-beta.
|
|
72
|
+
"@tsed/perf": "7.0.0-beta.20",
|
|
73
|
+
"@tsed/platform-cache": "7.0.0-beta.20",
|
|
74
|
+
"@tsed/platform-exceptions": "7.0.0-beta.20",
|
|
75
|
+
"@tsed/platform-log-middleware": "7.0.0-beta.20",
|
|
76
|
+
"@tsed/platform-middlewares": "7.0.0-beta.20",
|
|
77
|
+
"@tsed/platform-params": "7.0.0-beta.20",
|
|
78
|
+
"@tsed/platform-response-filter": "7.0.0-beta.20",
|
|
79
|
+
"@tsed/platform-router": "7.0.0-beta.20",
|
|
80
|
+
"@tsed/platform-views": "7.0.0-beta.20",
|
|
81
|
+
"@tsed/schema": "7.0.0-beta.20",
|
|
82
82
|
"@types/json-schema": "7.0.11",
|
|
83
83
|
"@types/on-finished": "2.3.1",
|
|
84
84
|
"on-finished": "2.4.1",
|