@tsed/common 7.0.0-beta.16 → 7.0.0-beta.19
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 +6 -0
- package/jest.config.js +1 -6
- package/lib/cjs/builder/PlatformBuilder.js +4 -1
- package/lib/cjs/builder/PlatformBuilder.js.map +1 -1
- package/lib/cjs/domain/PlatformContext.js +15 -57
- package/lib/cjs/domain/PlatformContext.js.map +1 -1
- 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/PlatformAdapter.js.map +1 -1
- package/lib/cjs/services/PlatformHandler.js +70 -56
- package/lib/cjs/services/PlatformHandler.js.map +1 -1
- package/lib/cjs/services/PlatformRequest.js +0 -1
- package/lib/cjs/services/PlatformRequest.js.map +1 -1
- package/lib/cjs/services/PlatformResponse.js +4 -7
- package/lib/cjs/services/PlatformResponse.js.map +1 -1
- package/lib/cjs/services/PlatformTest.js +1 -0
- package/lib/cjs/services/PlatformTest.js.map +1 -1
- package/lib/cjs/utils/createContext.js +3 -3
- package/lib/cjs/utils/createContext.js.map +1 -1
- package/lib/cjs/utils/setResponseHeaders.js +1 -1
- package/lib/cjs/utils/setResponseHeaders.js.map +1 -1
- package/lib/esm/builder/PlatformBuilder.js +4 -1
- package/lib/esm/builder/PlatformBuilder.js.map +1 -1
- package/lib/esm/domain/PlatformContext.js +15 -57
- package/lib/esm/domain/PlatformContext.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/PlatformAdapter.js.map +1 -1
- package/lib/esm/services/PlatformHandler.js +72 -58
- package/lib/esm/services/PlatformHandler.js.map +1 -1
- package/lib/esm/services/PlatformRequest.js +0 -1
- package/lib/esm/services/PlatformRequest.js.map +1 -1
- package/lib/esm/services/PlatformResponse.js +4 -7
- package/lib/esm/services/PlatformResponse.js.map +1 -1
- package/lib/esm/services/PlatformTest.js +1 -0
- package/lib/esm/services/PlatformTest.js.map +1 -1
- package/lib/esm/utils/createContext.js +3 -3
- package/lib/esm/utils/createContext.js.map +1 -1
- package/lib/esm/utils/setResponseHeaders.js +1 -1
- package/lib/esm/utils/setResponseHeaders.js.map +1 -1
- package/lib/types/builder/PlatformBuilder.d.ts +2 -2
- package/lib/types/domain/PlatformContext.d.ts +6 -13
- package/lib/types/interfaces/PlatformRouteOptions.d.ts +0 -1
- package/lib/types/services/Platform.d.ts +1 -5
- package/lib/types/services/PlatformAdapter.d.ts +3 -1
- package/lib/types/services/PlatformHandler.d.ts +13 -15
- package/lib/types/services/PlatformRequest.d.ts +0 -1
- package/lib/types/services/PlatformResponse.d.ts +2 -3
- package/package.json +16 -16
|
@@ -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;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 {
|
|
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 {PlatformHandlerMetadata, PlatformLayer} from \"@tsed/platform-router\";\nimport {IncomingMessage, ServerResponse} from \"http\";\nimport {PlatformMulter, PlatformMulterSettings} from \"../config/interfaces/PlatformMulterSettings\";\nimport {PlatformStaticsOptions} from \"../config/interfaces/PlatformStaticsSettings\";\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(): (req: IncomingMessage, res: ServerResponse) => void};\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,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
|
|
@@ -35,7 +57,7 @@ let PlatformHandler = class PlatformHandler {
|
|
|
35
57
|
});
|
|
36
58
|
const handler = this.platformParams.compileHandler(metadata.store);
|
|
37
59
|
return async ($ctx) => {
|
|
38
|
-
$ctx.
|
|
60
|
+
$ctx.handlerMetadata = metadata;
|
|
39
61
|
// @ts-ignore
|
|
40
62
|
return this.onRequest(handler, $ctx);
|
|
41
63
|
};
|
|
@@ -47,34 +69,25 @@ let PlatformHandler = class PlatformHandler {
|
|
|
47
69
|
*/
|
|
48
70
|
async flush($ctx) {
|
|
49
71
|
const { response } = $ctx;
|
|
50
|
-
if (
|
|
72
|
+
if (!$ctx.isDone()) {
|
|
51
73
|
let data = await this.responseFilter.serialize($ctx.data, $ctx);
|
|
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,EAAE,UAAU,EAAE,QAAQ,EAAC,MAAM,YAAY,CAAC;AACrF,OAAO,EAAC,MAAM,EAAE,UAAU,EAAY,aAAa,EAAC,MAAM,UAAU,CAAC;AACrE,OAAO,EAAC,IAAI,EAAC,MAAM,cAAc,CAAC;AAClC,OAAO,EAAC,cAAc,EAAyB,MAAM,uBAAuB,CAAC;AAC7E,OAAO,EAAC,sBAAsB,EAAC,MAAM,gCAAgC,CAAC;AACtE,OAAO,EAAyB,uBAAuB,EAAE,mBAAmB,EAAE,eAAe,EAAC,MAAM,uBAAuB,CAAC;AAC5H,OAAO,EAAC,SAAS,EAAC,MAAM,MAAM,CAAC;AAC/B,OAAO,EAAC,mBAAmB,EAAC,MAAM,+BAA+B,CAAC;AAElE,OAAO,EAAC,kBAAkB,EAAC,MAAM,6BAA6B,CAAC;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,eAAe,CAAC,GAAG,EAAE,CAAC,IAAI,CAAC,SAAS,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC,CAAC;YAEzE,OAAO,IAAI,CAAC,IAAI,CAAC,KAAK,EAAE,IAAI,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;QAC3C,CAAC,CAAC;IACJ,CAAC;IAED;;;OAGG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ream} 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.set(PlatformHandlerMetadata, 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 (!response.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.is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CAAC,WAAW,EAAE,CAAC,CAAC;qBACpE;oBAED,IAAI,IAAI,KAAK,yBAAyB,CAAC,MAAM,EAAE;wBAC7C,OAAO,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;qBACzB;iBACF;aACF;SACF;QAAC,OAAO,KAAK,EAAE;YACd,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;YAEnB,MAAM,KAAK,CAAC;SACb;IACH,CAAC;IAED,KAAK,CAAC,QAAQ,CAAC,IAAqB;QAClC,IAAI,CAAC,KAAK,GAAG,MAAM,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"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"PlatformRequest.js","sourceRoot":"","sources":["../../../src/services/PlatformRequest.ts"],"names":[],"mappings":";AAAA,OAAO,EAAC,UAAU,EAAE,aAAa,EAAE,KAAK,EAAC,MAAM,UAAU,CAAC;AAc1D;;;GAGG;AAGH,IAAa,eAAe,GAA5B,MAAa,eAAe;IACL;IAArB,YAAqB,IAAqB;QAArB,SAAI,GAAJ,IAAI,CAAiB;IAAG,CAAC;IAE9C;;OAEG;IACH,IAAI,QAAQ;QACV,OAAO,IAAI,CAAC,IAAI,CAAC,QAAQ,CAAC;IAC5B,CAAC;IAED,IAAI,GAAG;QACL,OAAO,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,OAAc,CAAC;IACxC,CAAC;IAED,IAAI,MAAM;QACR,OAAO,IAAI,CAAC,GAAG,CAAC,MAAM,CAAC;IACzB,CAAC;IAED,IAAI,IAAI;QACN,OAAO,IAAI,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;IAC1B,CAAC;IAED,IAAI,QAAQ;QACV,OAAO,IAAI,CAAC,GAAG,CAAC,QAAQ,CAAC;IAC3B,CAAC;IAED;;;;OAIG;IACH,IAAI,GAAG;QACL,OAAO,IAAI,CAAC,GAAG,CAAC,WAAW,IAAI,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC;IAC9C,CAAC;IAED,IAAI,OAAO;QACT,OAAO,IAAI,CAAC,GAAG,CAAC,OAAO,CAAC;IAC1B,CAAC;IAED,IAAI,MAAM;QACR,OAA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A4J3B;SA5JY,eAAe","sourcesContent":["import {Injectable, ProviderScope, Scope} from \"@tsed/di\";\nimport {IncomingHttpHeaders, IncomingMessage} from \"http\";\nimport type {PlatformContext} from \"../domain/PlatformContext\";\nimport type {PlatformResponse} from \"./PlatformResponse\";\n\ndeclare global {\n namespace TsED {\n // @ts-ignore\n export interface Request {\n id: string;\n }\n }\n}\n\n/**\n * Platform Request abstraction layer.\n * @platform\n */\n@Injectable()\n@Scope(ProviderScope.INSTANCE)\nexport class PlatformRequest<Req extends {[key: string]: any} = any> {\n constructor(readonly $ctx: PlatformContext) {}\n\n /**\n * The current @@PlatformResponse@@.\n */\n get response(): PlatformResponse {\n return this.$ctx.response;\n }\n\n get raw(): Req {\n return this.$ctx.event.request as any;\n }\n\n get secure(): boolean {\n return this.raw.secure;\n }\n\n get host(): string {\n return this.get(\"host\");\n }\n\n get protocol(): string {\n return this.raw.protocol;\n }\n\n /**\n * Get the url of the request.\n *\n * Is equivalent of `express.response.originalUrl || express.response.url`.\n */\n get url(): string {\n return this.raw.originalUrl || this.raw.url;\n }\n\n get headers(): IncomingHttpHeaders {\n return this.raw.headers;\n }\n\n get method(): string {\n return this.raw.method;\n }\n\n /**\n * Contains key-value pairs of data submitted in the request body. By default, it is `undefined`, and is populated when you use\n * `body-parsing` middleware such as `express.json()` or `express.urlencoded()`.\n */\n get body(): any {\n return this.raw.body;\n }\n\n get rawBody(): any {\n return this.raw.rawBody || this.raw.body;\n }\n\n /**\n * When using `cookie-parser` middleware, this property is an object that contains cookies sent by the request.\n * If the request contains no cookies, it defaults to `{}`.\n */\n get cookies(): {[key: string]: any} {\n return this.raw.cookies;\n }\n\n /**\n * This property is an object containing properties mapped to the named route `parameters`.\n * For example, if you have the route `/user/:name`, then the `name` property is available as `req.params.name`.\n * This object defaults to `{}`.\n */\n get params(): {[key: string]: any} {\n return this.raw.params;\n }\n\n /**\n * This property is an object containing a property for each query string parameter in the route.\n * When query parser is set to disabled, it is an empty object `{}`, otherwise it is the result of the configured query parser.\n */\n get query(): {[key: string]: any} {\n return this.raw.query;\n }\n\n /**\n * This property is an object containing a property for each session attributes set by any code.\n * It require to install a middleware like express-session to work.\n */\n get session(): {[key: string]: any} {\n return this.raw.session as any;\n }\n\n get files() {\n return this.raw.files;\n }\n\n get route() {\n return this.$ctx.handlerMetadata?.path;\n }\n\n /**\n * Return the original request framework instance\n */\n get request() {\n return this.getRequest();\n }\n\n /**\n * Return the original request node.js instance\n */\n get req() {\n return this.getReq();\n }\n\n /**\n * Returns the HTTP request header specified by field. The match is case-insensitive.\n *\n * ```typescript\n * request.get('Content-Type') // => \"text/plain\"\n * ```\n *\n * @param name\n */\n get(name: string) {\n return this.raw.get(name);\n }\n\n /**\n * Checks if the specified content types are acceptable, based on the request’s Accept HTTP header field. The method returns the best match, or if none of the specified content types is acceptable, returns false (in which case, the application should respond with 406 \"Not Acceptable\").\n *\n * The type value may be a single MIME type string (such as “application/json”), an extension name such as “json”, a comma-delimited list, or an array. For a list or array, the method returns the best match (if any).\n *\n * @param mime\n */\n accepts(mime: string): string | false;\n\n accepts(mime: string[]): string[] | false;\n\n accepts(mime?: string | string[]): string | string[] | false {\n // @ts-ignore\n return this.raw.accepts(mime);\n }\n\n isAborted() {\n return this.raw.aborted;\n }\n\n /**\n * Return the Framework response object (express, koa, etc...)\n */\n getRequest<R = Req>(): R {\n return this.raw as any;\n }\n\n /**\n * Return the Node.js response object\n */\n getReq(): IncomingMessage {\n return this.$ctx.event.request;\n }\n}\n"]}
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { __decorate, __metadata } from "tslib";
|
|
2
2
|
import { isBoolean, isNumber, isStream, isString } from "@tsed/core";
|
|
3
3
|
import { Injectable, ProviderScope, Scope } from "@tsed/di";
|
|
4
|
-
import onFinished from "on-finished";
|
|
5
4
|
/**
|
|
6
5
|
* Platform Response abstraction layer.
|
|
7
6
|
* @platform
|
|
@@ -72,7 +71,7 @@ let PlatformResponse = class PlatformResponse {
|
|
|
72
71
|
* Return the Node.js response object
|
|
73
72
|
*/
|
|
74
73
|
getRes() {
|
|
75
|
-
return this
|
|
74
|
+
return this.raw;
|
|
76
75
|
}
|
|
77
76
|
hasStatus() {
|
|
78
77
|
return this.statusCode !== 200;
|
|
@@ -242,19 +241,17 @@ let PlatformResponse = class PlatformResponse {
|
|
|
242
241
|
* @param cb
|
|
243
242
|
*/
|
|
244
243
|
onEnd(cb) {
|
|
245
|
-
|
|
244
|
+
const res = this.getRes();
|
|
245
|
+
res.on("finish", cb);
|
|
246
246
|
return this;
|
|
247
247
|
}
|
|
248
248
|
isDone() {
|
|
249
|
-
if (this.
|
|
249
|
+
if (this.$ctx.isFinished()) {
|
|
250
250
|
return true;
|
|
251
251
|
}
|
|
252
252
|
const res = this.getRes();
|
|
253
253
|
return Boolean(this.isHeadersSent() || res.writableEnded || res.writableFinished);
|
|
254
254
|
}
|
|
255
|
-
destroy() {
|
|
256
|
-
this.data = undefined;
|
|
257
|
-
}
|
|
258
255
|
isHeadersSent() {
|
|
259
256
|
return this.getRes().headersSent;
|
|
260
257
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"PlatformResponse.js","sourceRoot":"","sources":["../../../src/services/PlatformResponse.ts"],"names":[],"mappings":";AAAA,OAAO,EAAC,SAAS,EAAE,QAAQ,EAAE,QAAQ,EAAE,QAAQ,EAAC,MAAM,YAAY,CAAC;AACnE,OAAO,EAAC,UAAU,EAAE,aAAa,EAAE,KAAK,EAAC,MAAM,UAAU,CAAC;AAE1D,OAAO,UAAU,MAAM,aAAa,CAAC;AAWrC;;;GAGG;AAGH,IAAa,gBAAgB,GAA7B,MAAa,gBAAgB;IAGN;IAFrB,IAAI,CAAM;IAEV,YAAqB,IAAqB;QAArB,SAAI,GAAJ,IAAI,CAAiB;IAAG,CAAC;IAE9C;;OAEG;IACH,IAAI,OAAO;QACT,OAAO,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC;IAC3B,CAAC;IAED,IAAI,GAAG;QACL,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B,OAAO,IAAI,CAAC;SACb;QAED,IAAI,SAAS,CAAC,IAAI,CAAC,IAAI,QAAQ,CAAC,IAAI,CAAC,IAAI,QAAQ,CAAC,IAAI,CAAC,IAAI,IAAI,KAAK,IAAI,EAAE;YACxE,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;YAEpB,OAAO,IAAI,CAAC;SACb;QAED,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QAEpB,OAAO,IAAI,CAAC;IACd,CAAC;IAED,OAAO;QACL,OAAO,IAAI,CAAC,IAAI,CAAC;IACnB,CAAC;IAED;;;OAGG;IACH,KAAK,CAAC,EAA+C;QACnD,UAAU,CAAC,IAAI,CAAC,MAAM,EAAE,EAAE,EAAS,CAAC,CAAC;QACrC,OAAO,IAAI,CAAC;IACd,CAAC;IAED,MAAM;QACJ,IAAI,IAAI,CAAC,GAAG,CAAC,MAAM,EAAE;YACnB,OAAO,IAAI,CAAC;SACb;QAED,MAAM,GAAG,GAAG,IAAI,CAAC,MAAM,EAAE,CAAC;QAE1B,OAAO,OAAO,CAAC,IAAI,CAAC,aAAa,EAAE,IAAI,GAAG,CAAC,aAAa,IAAI,GAAG,CAAC,gBAAgB,CAAC,CAAC;IACpF,CAAC;IAED,OAAO;QACL,IAAI,CAAC,IAAI,GAAG,SAAS,CAAC;IACxB,CAAC;IAED,aAAa;QACX,OAAO,IAAI,CAAC,MAAM,EAAE,CAAC,WAAW,CAAC;IACnC,CAAC;CACF,CAAA;AA7SY,gBAAgB;IAF5B,UAAU,EAAE;IACZ,KAAK,CAAC,aAAa,CAAC,QAAQ,CAAC;;GACjB,gBAAgB,CA6S5B;SA7SY,gBAAgB","sourcesContent":["import {isBoolean, isNumber, isStream, isString} from \"@tsed/core\";\nimport {Injectable, ProviderScope, Scope} from \"@tsed/di\";\nimport {OutgoingHttpHeaders, ServerResponse} from \"http\";\nimport onFinished from \"on-finished\";\nimport type {PlatformContext} from \"../domain/PlatformContext\";\nimport {PlatformRequest} from \"./PlatformRequest\";\n\ndeclare global {\n namespace TsED {\n // @ts-ignore\n export interface Response {}\n }\n}\n\n/**\n * Platform Response abstraction layer.\n * @platform\n */\n@Injectable()\n@Scope(ProviderScope.INSTANCE)\nexport class PlatformResponse<Res extends Record<string, any> = any> {\n data: any;\n\n constructor(readonly $ctx: PlatformContext) {}\n\n /**\n * The current @@PlatformRequest@@.\n */\n get request(): PlatformRequest {\n return this.$ctx.request;\n }\n\n get raw(): Res {\n return this.$ctx.event.response as any;\n }\n\n /**\n * Get the current statusCode\n */\n get statusCode() {\n return this.raw.statusCode;\n }\n\n /**\n * An object that contains response local variables scoped to the request, and therefore available only to the view(s) rendered during that request / response cycle (if any). Otherwise, this property is identical to app.locals.\n *\n * This property is useful for exposing request-level information such as the request path name, authenticated user, user settings, and so on.\n */\n get locals() {\n return this.raw.locals;\n }\n\n /**\n * Return the original response framework instance\n */\n get response() {\n return this.getResponse();\n }\n\n /**\n * Return the original response node.js instance\n */\n get res() {\n return this.getRes();\n }\n\n /**\n * Returns the HTTP response header specified by field. The match is case-insensitive.\n *\n * ```typescript\n * response.get('Content-Type') // => \"text/plain\"\n * ```\n *\n * @param name\n */\n get(name: string) {\n return this.raw.get(name);\n }\n\n getHeaders(): OutgoingHttpHeaders {\n return this.raw.getHeaders();\n }\n\n /**\n * Return the Framework response object (express, koa, etc...)\n */\n getResponse<R = Res>(): R {\n return this.raw as any;\n }\n\n /**\n * Return the Node.js response object\n */\n getRes(): ServerResponse {\n return this.$ctx.event.response;\n }\n\n hasStatus() {\n return this.statusCode !== 200;\n }\n\n /**\n * Sets the HTTP status for the response.\n *\n * @param status\n */\n status(status: number) {\n this.raw.status(status);\n\n return this;\n }\n\n /**\n * Set header `field` to `val`, or pass\n * an object of header fields.\n *\n * Examples:\n * ```typescript\n * response.setHeaders({ Accept: 'text/plain', 'X-API-Key': 'tobi' });\n * ```\n *\n * Aliased as `res.header()`.\n */\n setHeaders(headers: OutgoingHttpHeaders) {\n // apply headers\n Object.entries(headers).forEach(([key, item]) => {\n this.setHeader(key, item);\n });\n\n return this;\n }\n\n setHeader(key: string, item: any) {\n if (key.toLowerCase() === \"location\") {\n return this.location(String(item));\n }\n\n this.raw.set(key, item);\n\n return this;\n }\n\n /**\n * Set `Content-Type` response header with `type` through `mime.lookup()`\n * when it does not contain \"/\", or set the Content-Type to `type` otherwise.\n *\n * Examples:\n *\n * res.type('.html');\n * res.type('html');\n * res.type('json');\n * res.type('application/json');\n * res.type('png');\n */\n contentType(contentType: string) {\n this.raw.contentType(contentType);\n\n return this;\n }\n\n contentLength(length: number) {\n this.setHeader(\"Content-Length\", length);\n return this;\n }\n\n getContentLength() {\n if (this.get(\"Content-Length\")) {\n return parseInt(this.get(\"Content-Length\"), 10) || 0;\n }\n }\n\n getContentType() {\n return (this.get(\"Content-Type\") || \"\").split(\";\")[0];\n }\n\n /**\n * Sets the HTTP response Content-Disposition header field to “attachment”.\n * If a filename is given, then it sets the Content-Type based on the extension name via res.type(), and sets the Content-Disposition “filename=” parameter.\n *\n * ```typescript\n * res.attachment()\n * // Content-Disposition: attachment\n *\n * res.attachment('path/to/logo.png')\n * // Content-Disposition: attachment; filename=\"logo.png\"\n * // Content-Type: image/png\n * ```\n *\n * @param filename\n */\n attachment(filename: string) {\n this.raw.attachment(filename);\n return this;\n }\n\n /**\n * Redirects to the URL derived from the specified path, with specified status, a positive integer that corresponds to an [HTTP status code](http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html).\n * If not specified, status defaults to `302 Found`.\n *\n * @param status\n * @param url\n */\n redirect(status: number, url: string) {\n this.raw.redirect(status, url);\n\n return this;\n }\n\n /**\n * Sets the response Location HTTP header to the specified path parameter.\n *\n * @param location\n */\n location(location: string) {\n this.raw.location(location);\n\n return this;\n }\n\n /**\n * Stream the given data.\n *\n * @param data\n */\n stream(data: any) {\n data.pipe(this.raw);\n\n return this;\n }\n\n /**\n * Renders a view and sends the rendered HTML string to the client.\n *\n * @param path\n * @param options\n */\n async render(path: string, options: any = {}) {\n const {PlatformViews} = await import(\"@tsed/platform-views\");\n const platformViews = await this.$ctx.injector.lazyInvoke(PlatformViews);\n\n return platformViews.render(path, {\n ...this.locals,\n ...options\n });\n }\n\n /**\n * Send any data to your consumer.\n *\n * This method accept a ReadableStream, a plain object, boolean, string, number, null and undefined data.\n * It chooses the better way to send the data.\n *\n * @param data\n */\n body(data: any) {\n this.data = data;\n\n if (data === undefined) {\n this.raw.send();\n\n return this;\n }\n\n if (isStream(data)) {\n this.stream(data);\n\n return this;\n }\n\n if (Buffer.isBuffer(data)) {\n if (!this.getContentType()) {\n this.contentType(\"application/octet-stream\");\n }\n\n this.contentLength(data.length);\n this.raw.send(data);\n\n return this;\n }\n\n if (isBoolean(data) || isNumber(data) || isString(data) || data === null) {\n this.raw.send(data);\n\n return this;\n }\n\n this.raw.json(data);\n\n return this;\n }\n\n getBody() {\n return this.data;\n }\n\n /**\n * Add a listener to handler the end of the request/response.\n * @param cb\n */\n onEnd(cb: (er: Error | null, message: string) => void): this {\n onFinished(this.getRes(), cb as any);\n return this;\n }\n\n isDone(): boolean {\n if (this.raw.isDone) {\n return true;\n }\n\n const res = this.getRes();\n\n return Boolean(this.isHeadersSent() || res.writableEnded || res.writableFinished);\n }\n\n destroy() {\n this.data = undefined;\n }\n\n isHeadersSent() {\n return this.getRes().headersSent;\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"PlatformResponse.js","sourceRoot":"","sources":["../../../src/services/PlatformResponse.ts"],"names":[],"mappings":";AAAA,OAAO,EAAC,SAAS,EAAE,QAAQ,EAAE,QAAQ,EAAE,QAAQ,EAAC,MAAM,YAAY,CAAC;AACnE,OAAO,EAAC,UAAU,EAAE,aAAa,EAAE,KAAK,EAAC,MAAM,UAAU,CAAC;AAY1D;;;GAGG;AAGH,IAAa,gBAAgB,GAA7B,MAAa,gBAAgB;IAGN;IAFrB,IAAI,CAAM;IAEV,YAAqB,IAAqB;QAArB,SAAI,G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port {Injectable, ProviderScope, Scope} from \"@tsed/di\";\nimport {OutgoingHttpHeaders, ServerResponse} from \"http\";\nimport type {PlatformContext} from \"../domain/PlatformContext\";\nimport type {PlatformRequest} from \"./PlatformRequest\";\n\ndeclare global {\n namespace TsED {\n // @ts-ignore\n export interface Response {}\n }\n}\n\n/**\n * Platform Response abstraction layer.\n * @platform\n */\n@Injectable()\n@Scope(ProviderScope.INSTANCE)\nexport class PlatformResponse<Res extends Record<string, any> = any> {\n data: any;\n\n constructor(readonly $ctx: PlatformContext) {}\n\n /**\n * The current @@PlatformRequest@@.\n */\n get request(): PlatformRequest {\n return this.$ctx.request;\n }\n\n get raw(): Res {\n return this.$ctx.event.response as any;\n }\n\n /**\n * Get the current statusCode\n */\n get statusCode() {\n return this.raw.statusCode;\n }\n\n /**\n * An object that contains response local variables scoped to the request, and therefore available only to the view(s) rendered during that request / response cycle (if any). Otherwise, this property is identical to app.locals.\n *\n * This property is useful for exposing request-level information such as the request path name, authenticated user, user settings, and so on.\n */\n get locals() {\n return this.raw.locals;\n }\n\n /**\n * Return the original response framework instance\n */\n get response() {\n return this.getResponse();\n }\n\n /**\n * Return the original response node.js instance\n */\n get res() {\n return this.getRes();\n }\n\n /**\n * Returns the HTTP response header specified by field. The match is case-insensitive.\n *\n * ```typescript\n * response.get('Content-Type') // => \"text/plain\"\n * ```\n *\n * @param name\n */\n get(name: string) {\n return this.raw.get(name);\n }\n\n getHeaders(): OutgoingHttpHeaders {\n return this.raw.getHeaders();\n }\n\n /**\n * Return the Framework response object (express, koa, etc...)\n */\n getResponse<R = Res>(): R {\n return this.raw as any;\n }\n\n /**\n * Return the Node.js response object\n */\n getRes(): ServerResponse {\n return this.raw as any;\n }\n\n hasStatus() {\n return this.statusCode !== 200;\n }\n\n /**\n * Sets the HTTP status for the response.\n *\n * @param status\n */\n status(status: number) {\n this.raw.status(status);\n\n return this;\n }\n\n /**\n * Set header `field` to `val`, or pass\n * an object of header fields.\n *\n * Examples:\n * ```typescript\n * response.setHeaders({ Accept: 'text/plain', 'X-API-Key': 'tobi' });\n * ```\n *\n * Aliased as `res.header()`.\n */\n setHeaders(headers: OutgoingHttpHeaders) {\n // apply headers\n Object.entries(headers).forEach(([key, item]) => {\n this.setHeader(key, item);\n });\n\n return this;\n }\n\n setHeader(key: string, item: any) {\n if (key.toLowerCase() === \"location\") {\n return this.location(String(item));\n }\n\n this.raw.set(key, item);\n\n return this;\n }\n\n /**\n * Set `Content-Type` response header with `type` through `mime.lookup()`\n * when it does not contain \"/\", or set the Content-Type to `type` otherwise.\n *\n * Examples:\n *\n * res.type('.html');\n * res.type('html');\n * res.type('json');\n * res.type('application/json');\n * res.type('png');\n */\n contentType(contentType: string) {\n this.raw.contentType(contentType);\n\n return this;\n }\n\n contentLength(length: number) {\n this.setHeader(\"Content-Length\", length);\n return this;\n }\n\n getContentLength() {\n if (this.get(\"Content-Length\")) {\n return parseInt(this.get(\"Content-Length\"), 10) || 0;\n }\n }\n\n getContentType() {\n return (this.get(\"Content-Type\") || \"\").split(\";\")[0];\n }\n\n /**\n * Sets the HTTP response Content-Disposition header field to “attachment”.\n * If a filename is given, then it sets the Content-Type based on the extension name via res.type(), and sets the Content-Disposition “filename=” parameter.\n *\n * ```typescript\n * res.attachment()\n * // Content-Disposition: attachment\n *\n * res.attachment('path/to/logo.png')\n * // Content-Disposition: attachment; filename=\"logo.png\"\n * // Content-Type: image/png\n * ```\n *\n * @param filename\n */\n attachment(filename: string) {\n this.raw.attachment(filename);\n return this;\n }\n\n /**\n * Redirects to the URL derived from the specified path, with specified status, a positive integer that corresponds to an [HTTP status code](http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html).\n * If not specified, status defaults to `302 Found`.\n *\n * @param status\n * @param url\n */\n redirect(status: number, url: string) {\n this.raw.redirect(status, url);\n\n return this;\n }\n\n /**\n * Sets the response Location HTTP header to the specified path parameter.\n *\n * @param location\n */\n location(location: string) {\n this.raw.location(location);\n\n return this;\n }\n\n /**\n * Stream the given data.\n *\n * @param data\n */\n stream(data: any) {\n data.pipe(this.raw);\n\n return this;\n }\n\n /**\n * Renders a view and sends the rendered HTML string to the client.\n *\n * @param path\n * @param options\n */\n async render(path: string, options: any = {}) {\n const {PlatformViews} = await import(\"@tsed/platform-views\");\n const platformViews = await this.$ctx.injector.lazyInvoke(PlatformViews);\n\n return platformViews.render(path, {\n ...this.locals,\n ...options\n });\n }\n\n /**\n * Send any data to your consumer.\n *\n * This method accept a ReadableStream, a plain object, boolean, string, number, null and undefined data.\n * It chooses the better way to send the data.\n *\n * @param data\n */\n body(data: any) {\n this.data = data;\n\n if (data === undefined) {\n this.raw.send();\n\n return this;\n }\n\n if (isStream(data)) {\n this.stream(data);\n\n return this;\n }\n\n if (Buffer.isBuffer(data)) {\n if (!this.getContentType()) {\n this.contentType(\"application/octet-stream\");\n }\n\n this.contentLength(data.length);\n this.raw.send(data);\n\n return this;\n }\n\n if (isBoolean(data) || isNumber(data) || isString(data) || data === null) {\n this.raw.send(data);\n\n return this;\n }\n\n this.raw.json(data);\n\n return this;\n }\n\n getBody() {\n return this.data;\n }\n\n /**\n * Add a listener to handler the end of the request/response.\n * @param cb\n */\n onEnd(cb: () => Promise<void> | void): this {\n const res: any = this.getRes();\n\n res.on(\"finish\", cb);\n\n return this;\n }\n\n isDone(): boolean {\n if (this.$ctx.isFinished()) {\n return true;\n }\n\n const res = this.getRes();\n\n return Boolean(this.isHeadersSent() || res.writableEnded || res.writableFinished);\n }\n\n isHeadersSent() {\n return this.getRes().headersSent;\n }\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"PlatformTest.js","sourceRoot":"","sources":["../../../src/services/PlatformTest.ts"],"names":[],"mappings":"AACA,OAAO,EAAC,MAAM,EAAiC,MAAM,UAAU,CAAC;AAChE,OAAO,OAAO,MAAM,SAAS,CAAC;AAC9B,OAAO,EAAC,eAAe,EAAC,MAAM,4BAA4B,CAAC;AAC3D,OAAO,EAAC,eAAe,EAAyB,MAAM,2BA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AK;SACN,CAAC,CAAC;QAEH,IAAI,OAAO,CAAC,QAAQ,EAAE;YACpB,IAAI,CAAC,QAAQ,GAAG,OAAO,CAAC,QAAQ,CAAC;SAClC;QAED,OAAO,IAAI,CAAC;IACd,CAAC;CACF","sourcesContent":["import {Type} from \"@tsed/core\";\nimport {DITest, DITestOptions, InjectorService} from \"@tsed/di\";\nimport accepts from \"accepts\";\nimport {PlatformBuilder} from \"../builder/PlatformBuilder\";\nimport {PlatformContext, PlatformContextOptions} from \"../domain/PlatformContext\";\nimport {createInjector} from \"../utils/createInjector\";\nimport {getConfiguration} from \"../utils/getConfiguration\";\nimport {PlatformAdapter, PlatformBuilderSettings} from \"./PlatformAdapter\";\nimport {PlatformApplication} from \"./PlatformApplication\";\n\n/**\n * @platform\n */\nexport class PlatformTest extends DITest {\n public static adapter: Type<PlatformAdapter>;\n\n static async create(settings: DITestOptions = {}) {\n DITest.injector = PlatformTest.createInjector(getConfiguration(settings));\n await DITest.createContainer(settings);\n }\n\n /**\n * Create a new injector with the right default services\n */\n static createInjector(settings: any = {}): InjectorService {\n return createInjector({\n settings: DITest.configure({httpPort: false, httpsPort: false, ...settings})\n });\n }\n\n /**\n * Load the server silently without listening port and configure it on test profile.\n * @decorator\n * @param mod\n * @param listen\n * @param settings\n * @returns {Promise<void>}\n */\n static bootstrap(mod: any, {listen, ...settings}: Partial<PlatformBuilderSettings & {listen: boolean}> = {}): () => Promise<void> {\n return async function before(): Promise<void> {\n let instance: PlatformBuilder;\n const adapter: Type<PlatformAdapter> = settings.platform || settings.adapter || PlatformTest.adapter;\n\n /* istanbul ignore next */\n if (!adapter) {\n throw new Error(\n \"Platform adapter is not specified. Have you added at least `import @tsed/platform-express` (or equivalent) on your Server.ts ?\"\n );\n }\n\n // @ts-ignore\n settings = DITest.configure(settings);\n settings.adapter = adapter as any;\n\n const configuration = getConfiguration(settings, mod);\n const disableComponentsScan = configuration.disableComponentsScan || !!process.env.WEBPACK;\n\n if (!disableComponentsScan) {\n const {importProviders} = await import(\"@tsed/components-scan\");\n await importProviders(configuration);\n }\n\n instance = await PlatformBuilder.build(mod, configuration).bootstrap();\n await instance.listen(!!listen);\n\n // used by inject method\n DITest.injector = instance.injector;\n };\n }\n\n /**\n * It injects services into the test function where you can alter, spy on, and manipulate them.\n *\n * The inject function has two parameters\n *\n * * an array of Service dependency injection tokens,\n * * a test function whose parameters correspond exactly to each item in the injection token array.\n *\n * @param targets\n * @param func\n */\n static inject<T>(targets: any[], func: (...args: any[]) => Promise<T> | T): () => Promise<T> {\n return async (): Promise<T> => {\n if (!DITest.hasInjector()) {\n await PlatformTest.create();\n }\n\n const injector: InjectorService = DITest.injector;\n const deps = [];\n\n for (const target of targets) {\n deps.push(injector.has(target) ? injector.get(target) : await injector.invoke(target));\n }\n\n return await func(...deps);\n };\n }\n\n /**\n * Return the raw application (express or koa).\n * Use this callback with SuperTest.\n *\n * ```typescript\n * let request: SuperTest.SuperTest<SuperTest.Test>;\n * beforeEach(PlatformTest.bootstrap(Server, {\n * mount: {\n * \"/rest\": [ProductsController]\n * }\n * }));\n * beforeEach(() => {\n * request = SuperTest(PlatformTest.callback());\n * });\n * ```\n */\n static callback() {\n return DITest.injector.get<PlatformApplication>(PlatformApplication)?.callback();\n }\n\n static createRequest(options: any = {}): any {\n return {\n headers: {},\n method: \"GET\",\n url: \"/\",\n get(key: string) {\n return this.headers[key.toLowerCase()];\n },\n accepts(mime?: string | string[]) {\n return accepts(this).types([].concat(mime as never));\n },\n ...options\n };\n }\n\n static createResponse(options: any = {}): any {\n return {\n headers: {},\n locals: {},\n statusCode: 200,\n status(code: number) {\n this.statusCode = code;\n return this;\n },\n contentType(content: string) {\n this.set(\"content-type\", content);\n },\n contentLength(content: number) {\n this.set(\"content-length\", content);\n },\n redirect(status: number, path: string) {\n this.statusCode = status;\n this.set(\"location\", path);\n },\n location(path: string) {\n this.set(\"location\", path);\n },\n get(key: string) {\n return this.headers[key.toLowerCase()];\n },\n getHeaders() {\n return this.headers;\n },\n set(key: string, value: any) {\n this.headers[key.toLowerCase()] = value;\n return this;\n },\n setHeader(key: string, value: any) {\n this.headers[key.toLowerCase()] = value;\n return this;\n },\n send(data: any) {\n this.data = data;\n },\n json(data: any) {\n this.data = data;\n },\n end(data: any) {\n this.data = data;\n },\n ...options\n };\n }\n\n static createRequestContext(options: Partial<PlatformContextOptions & any> = {}) {\n const event = {\n ...options.event,\n request: options?.request?.request || options?.event?.request || PlatformTest.createRequest(),\n response: options?.response?.response || options?.event?.response || PlatformTest.createResponse()\n };\n\n const $ctx = new PlatformContext({\n id: \"id\",\n injector: DITest.injector,\n logger: DITest.injector.logger,\n url: \"/\",\n ...options,\n event\n });\n\n if (options.endpoint) {\n $ctx.endpoint = options.endpoint;\n }\n\n return $ctx;\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"PlatformTest.js","sourceRoot":"","sources":["../../../src/services/PlatformTest.ts"],"names":[],"mappings":"AACA,OAAO,EAAC,MAAM,EAAiC,MAAM,UAAU,CAAC;AAChE,OAAO,OAAO,MAAM,SAAS,CAAC;AAC9B,OAAO,EAAC,eAAe,EAAC,MAAM,4BAA4B,CAAC;AAC3D,OAAO,EAAC,eAAe,EAAyB,MAAM,2BAA2B,CAAC;AAClF,OAAO,EAAC,cAAc,EAAC,MAAM,yBAAyB,CAAC;AACvD,OAAO,EAAC,gBAAgB,EAAC,MAAM,2BAA2B,CAAC;AAE3D,OAAO,EAAC,mBAAmB,EAAC,MAAM,uBAAuB,CAAC;AAE1D;;GAEG;AACH,MAAM,OAAO,YAAa,SAAQ,MAAM;IAC/B,MAAM,CAAC,OAAO,CAAwB;IAE7C,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC,WAA0B,EAAE;QAC9C,MAAM,CAAC,QAAQ,GAAG,YAAY,CAAC,cAAc,CAAC,gBAAgB,CAAC,QAAQ,CAAC,CAAC,CAAC;QAC1E,MAAM,MAAM,CAAC,eAAe,CAAC,QAAQ,CAAC,CAAC;IACzC,CAAC;IAED;;OA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AAG,IAAI,CAAC;YACnB,CAAC;YACD,IAAI,CAAC,IAAS;gBACZ,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;YACnB,CAAC;YACD,GAAG,CAAC,IAAS;gBACX,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;YACnB,CAAC;YACD,GAAG,OAAO;SACX,CAAC;IACJ,CAAC;IAED,MAAM,CAAC,oBAAoB,CAAC,UAAiD,EAAE;QAC7E,MAAM,KAAK,GAAG;YACZ,GAAG,OAAO,CAAC,KAAK;YAChB,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,OAAO,IAAI,OAAO,EAAE,KAAK,EAAE,OAAO,IAAI,YAAY,CAAC,aAAa,EAAE;YAC7F,QAAQ,EAAE,OAAO,EAAE,QAAQ,EAAE,QAAQ,IAAI,OAAO,EAAE,KAAK,EAAE,QAAQ,IAAI,YAAY,CAAC,cAAc,EAAE;SACnG,CAAC;QAEF,MAAM,IAAI,GAAG,IAAI,eAAe,CAAC;YAC/B,EAAE,EAAE,IAAI;YACR,QAAQ,EAAE,MAAM,CAAC,QAAQ;YACzB,MAAM,EAAE,MAAM,CAAC,QAAQ,CAAC,MAAM;YAC9B,GAAG,EAAE,GAAG;YACR,GAAG,OAAO;YACV,KAAK;SACN,CAAC,CAAC;QAEH,IAAI,OAAO,CAAC,QAAQ,EAAE;YACpB,IAAI,CAAC,QAAQ,GAAG,OAAO,CAAC,QAAQ,CAAC;SAClC;QAED,OAAO,IAAI,CAAC;IACd,CAAC;CACF","sourcesContent":["import {Type} from \"@tsed/core\";\nimport {DITest, DITestOptions, InjectorService} from \"@tsed/di\";\nimport accepts from \"accepts\";\nimport {PlatformBuilder} from \"../builder/PlatformBuilder\";\nimport {PlatformContext, PlatformContextOptions} from \"../domain/PlatformContext\";\nimport {createInjector} from \"../utils/createInjector\";\nimport {getConfiguration} from \"../utils/getConfiguration\";\nimport {PlatformAdapter, PlatformBuilderSettings} from \"./PlatformAdapter\";\nimport {PlatformApplication} from \"./PlatformApplication\";\n\n/**\n * @platform\n */\nexport class PlatformTest extends DITest {\n public static adapter: Type<PlatformAdapter>;\n\n static async create(settings: DITestOptions = {}) {\n DITest.injector = PlatformTest.createInjector(getConfiguration(settings));\n await DITest.createContainer(settings);\n }\n\n /**\n * Create a new injector with the right default services\n */\n static createInjector(settings: any = {}): InjectorService {\n return createInjector({\n settings: DITest.configure({httpPort: false, httpsPort: false, ...settings})\n });\n }\n\n /**\n * Load the server silently without listening port and configure it on test profile.\n * @decorator\n * @param mod\n * @param listen\n * @param settings\n * @returns {Promise<void>}\n */\n static bootstrap(mod: any, {listen, ...settings}: Partial<PlatformBuilderSettings & {listen: boolean}> = {}): () => Promise<void> {\n return async function before(): Promise<void> {\n let instance: PlatformBuilder;\n const adapter: Type<PlatformAdapter> = settings.platform || settings.adapter || PlatformTest.adapter;\n\n /* istanbul ignore next */\n if (!adapter) {\n throw new Error(\n \"Platform adapter is not specified. Have you added at least `import @tsed/platform-express` (or equivalent) on your Server.ts ?\"\n );\n }\n\n // @ts-ignore\n settings = DITest.configure(settings);\n settings.adapter = adapter as any;\n\n const configuration = getConfiguration(settings, mod);\n const disableComponentsScan = configuration.disableComponentsScan || !!process.env.WEBPACK;\n\n if (!disableComponentsScan) {\n const {importProviders} = await import(\"@tsed/components-scan\");\n await importProviders(configuration);\n }\n\n instance = await PlatformBuilder.build(mod, configuration).bootstrap();\n await instance.listen(!!listen);\n\n // used by inject method\n DITest.injector = instance.injector;\n };\n }\n\n /**\n * It injects services into the test function where you can alter, spy on, and manipulate them.\n *\n * The inject function has two parameters\n *\n * * an array of Service dependency injection tokens,\n * * a test function whose parameters correspond exactly to each item in the injection token array.\n *\n * @param targets\n * @param func\n */\n static inject<T>(targets: any[], func: (...args: any[]) => Promise<T> | T): () => Promise<T> {\n return async (): Promise<T> => {\n if (!DITest.hasInjector()) {\n await PlatformTest.create();\n }\n\n const injector: InjectorService = DITest.injector;\n const deps = [];\n\n for (const target of targets) {\n deps.push(injector.has(target) ? injector.get(target) : await injector.invoke(target));\n }\n\n return await func(...deps);\n };\n }\n\n /**\n * Return the raw application (express or koa).\n * Use this callback with SuperTest.\n *\n * ```typescript\n * let request: SuperTest.SuperTest<SuperTest.Test>;\n * beforeEach(PlatformTest.bootstrap(Server, {\n * mount: {\n * \"/rest\": [ProductsController]\n * }\n * }));\n * beforeEach(() => {\n * request = SuperTest(PlatformTest.callback());\n * });\n * ```\n */\n static callback() {\n return DITest.injector.get<PlatformApplication>(PlatformApplication)?.callback();\n }\n\n static createRequest(options: any = {}): any {\n return {\n headers: {},\n method: \"GET\",\n url: \"/\",\n query: {},\n get(key: string) {\n return this.headers[key.toLowerCase()];\n },\n accepts(mime?: string | string[]) {\n return accepts(this).types([].concat(mime as never));\n },\n ...options\n };\n }\n\n static createResponse(options: any = {}): any {\n return {\n headers: {},\n locals: {},\n statusCode: 200,\n status(code: number) {\n this.statusCode = code;\n return this;\n },\n contentType(content: string) {\n this.set(\"content-type\", content);\n },\n contentLength(content: number) {\n this.set(\"content-length\", content);\n },\n redirect(status: number, path: string) {\n this.statusCode = status;\n this.set(\"location\", path);\n },\n location(path: string) {\n this.set(\"location\", path);\n },\n get(key: string) {\n return this.headers[key.toLowerCase()];\n },\n getHeaders() {\n return this.headers;\n },\n set(key: string, value: any) {\n this.headers[key.toLowerCase()] = value;\n return this;\n },\n setHeader(key: string, value: any) {\n this.headers[key.toLowerCase()] = value;\n return this;\n },\n send(data: any) {\n this.data = data;\n },\n json(data: any) {\n this.data = data;\n },\n end(data: any) {\n this.data = data;\n },\n ...options\n };\n }\n\n static createRequestContext(options: Partial<PlatformContextOptions & any> = {}) {\n const event = {\n ...options.event,\n request: options?.request?.request || options?.event?.request || PlatformTest.createRequest(),\n response: options?.response?.response || options?.event?.response || PlatformTest.createResponse()\n };\n\n const $ctx = new PlatformContext({\n id: \"id\",\n injector: DITest.injector,\n logger: DITest.injector.logger,\n url: \"/\",\n ...options,\n event\n });\n\n if (options.endpoint) {\n $ctx.endpoint = options.endpoint;\n }\n\n return $ctx;\n }\n}\n"]}
|
|
@@ -32,13 +32,13 @@ export function createContext(injector) {
|
|
|
32
32
|
};
|
|
33
33
|
const ignoreLog = buildIgnoreLog(loggerOptions.ignoreUrlPatterns);
|
|
34
34
|
return async function invokeContext(event) {
|
|
35
|
-
const ctx = new PlatformContext({
|
|
35
|
+
const $ctx = new PlatformContext({
|
|
36
36
|
...opts,
|
|
37
37
|
event,
|
|
38
38
|
id: reqIdBuilder(event.request)
|
|
39
39
|
});
|
|
40
|
-
ignoreLog && ctx.logger.alterIgnoreLog((ignore, data) => ignoreLog(ignore, data, ctx.url));
|
|
41
|
-
return ctx;
|
|
40
|
+
ignoreLog && $ctx.logger.alterIgnoreLog((ignore, data) => ignoreLog(ignore, data, $ctx.url));
|
|
41
|
+
return $ctx;
|
|
42
42
|
};
|
|
43
43
|
}
|
|
44
44
|
//# sourceMappingURL=createContext.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createContext.js","sourceRoot":"","sources":["../../../src/utils/createContext.ts"],"names":[],"mappings":"AACA,OAAO,EAAC,EAAE,EAAC,MAAM,MAAM,CAAC;AACxB,OAAO,EAAC,eAAe,EAAC,MAAM,2BAA2B,CAAC;AAE1D,OAAO,EAAC,eAAe,EAAC,MAAM,6BAA6B,CAAC;AAC5D,OAAO,EAAC,gBAAgB,EAAC,MAAM,8BAA8B,CAAC;AAE9D,SAAS,mBAAmB,CAAC,GAAQ;IACnC,OAAO,GAAG,CAAC,GAAG,CAAC,cAAc,CAAC,IAAI,EAAE,EAAE,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;AAC7D,CAAC;AAED,SAAS,oBAAoB,CAAC,iBAAwB;IACpD,OAAO,iBAAiB,CAAC,GAAG,CAAC,CAAC,OAAwB,EAAE,EAAE,CAAC,CAAC,OAAO,OAAO,KAAK,QAAQ,CAAC,CAAC,CAAC,IAAI,MAAM,CAAC,OAAO,EAA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c,CAAC,aAAa,CAAC,iBAAiB,CAAC,CAAC;IAElE,OAAO,KAAK,UAAU,aAAa,CAAC,KAAoB;QACtD,MAAM,IAAI,GAAG,IAAI,eAAe,CAAC;YAC/B,GAAG,IAAI;YACP,KAAK;YACL,EAAE,EAAE,YAAY,CAAC,KAAK,CAAC,OAAO,CAAC;SAChC,CAAC,CAAC;QAEH,SAAS,IAAI,IAAI,CAAC,MAAM,CAAC,cAAc,CAAC,CAAC,MAAM,EAAE,IAAI,EAAE,EAAE,CAAC,SAAS,CAAC,MAAM,EAAE,IAAI,EAAE,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC;QAE7F,OAAO,IAAI,CAAC;IACd,CAAC,CAAC;AACJ,CAAC","sourcesContent":["import {InjectorService} from \"@tsed/di\";\nimport {v4} from \"uuid\";\nimport {PlatformContext} from \"../domain/PlatformContext\";\nimport {IncomingEvent} from \"../interfaces/IncomingEvent\";\nimport {PlatformRequest} from \"../services/PlatformRequest\";\nimport {PlatformResponse} from \"../services/PlatformResponse\";\n\nfunction defaultReqIdBuilder(req: any) {\n return req.get(\"x-request-id\") || v4().split(\"-\").join(\"\");\n}\n\nfunction mapIgnoreUrlPatterns(ignoreUrlPatterns: any[]) {\n return ignoreUrlPatterns.map((pattern: string | RegExp) => (typeof pattern === \"string\" ? new RegExp(pattern, \"gi\") : pattern));\n}\n\nexport function buildIgnoreLog(ignoreUrlPatterns: any[] | undefined) {\n if (ignoreUrlPatterns) {\n ignoreUrlPatterns = mapIgnoreUrlPatterns(ignoreUrlPatterns);\n return (ignore: boolean, data: any, url: string) => ignoreUrlPatterns?.find((reg) => !!url.match(reg));\n }\n}\n\n/**\n * Create the TsED context to wrap request, response, injector, etc...\n * @param injector\n * @ignore\n */\nexport function createContext(injector: InjectorService) {\n const ResponseKlass = injector.getProvider(PlatformResponse)?.useClass;\n const RequestKlass = injector.getProvider(PlatformRequest)?.useClass;\n const {reqIdBuilder = defaultReqIdBuilder, ...loggerOptions} = injector.settings.logger;\n\n const opts = {\n ...loggerOptions,\n logger: injector.logger,\n injector,\n ResponseKlass,\n RequestKlass\n };\n\n const ignoreLog = buildIgnoreLog(loggerOptions.ignoreUrlPatterns);\n\n return async function invokeContext(event: IncomingEvent) {\n const $ctx = new PlatformContext({\n ...opts,\n event,\n id: reqIdBuilder(event.request)\n });\n\n ignoreLog && $ctx.logger.alterIgnoreLog((ignore, data) => ignoreLog(ignore, data, $ctx.url));\n\n return $ctx;\n };\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"setResponseHeaders.js","sourceRoot":"","sources":["../../../src/utils/setResponseHeaders.ts"],"names":[],"mappings":"AAEA;;GAEG;AACH,MAAM,UAAU,kBAAkB,CAAC,GAAoB;IACrD,MAAM,EAAC,QAAQ,EAAE,QAAQ,EAAC,GAAG,GAAG,CAAC;IACjC,MAAM,EAAC,SAAS,EAAC,GAAG,QAAQ,CAAC;IAE7B,IAAI,
|
|
1
|
+
{"version":3,"file":"setResponseHeaders.js","sourceRoot":"","sources":["../../../src/utils/setResponseHeaders.ts"],"names":[],"mappings":"AAEA;;GAEG;AACH,MAAM,UAAU,kBAAkB,CAAC,GAAoB;IACrD,MAAM,EAAC,QAAQ,EAAE,QAAQ,EAAC,GAAG,GAAG,CAAC;IACjC,MAAM,EAAC,SAAS,EAAC,GAAG,QAAQ,CAAC;IAE7B,IAAI,GAAG,CAAC,MAAM,EAAE,EAAE;QAChB,OAAO;KACR;IAED,IAAI,CAAC,QAAQ,CAAC,SAAS,EAAE,EAAE;QACzB,kDAAkD;QAClD,QAAQ,CAAC,MAAM,CAAC,SAAS,CAAC,SAAS,EAAE,CAAC,CAAC;KACxC;IAED,MAAM,UAAU,GAAG,QAAQ,CAAC,UAAU,CAAC;IACvC,MAAM,OAAO,GAAG,SAAS,CAAC,YAAY,CAAC,UAAU,CAAC,CAAC;IAEnD,MAAM,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,GAAG,EAAE,IAAI,CAAQ,EAAE,EAAE;QACrD,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE;YACtB,QAAQ,CAAC,SAAS,CAAC,GAAG,EAAE,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC;SAC/C;IACH,CAAC,CAAC,CAAC;IAEH,IAAI,SAAS,CAAC,aAAa,CAAC,UAAU,CAAC,EAAE;QACvC,QAAQ,CAAC,QAAQ,CAAC,UAAU,EAAE,QAAQ,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC,CAAC;KACzD;AACH,CAAC","sourcesContent":["import {PlatformContext} from \"../domain/PlatformContext\";\n\n/**\n * @ignore\n */\nexport function setResponseHeaders(ctx: PlatformContext) {\n const {response, endpoint} = ctx;\n const {operation} = endpoint;\n\n if (ctx.isDone()) {\n return;\n }\n\n if (!response.hasStatus()) {\n // apply status only if the isn't already modified\n response.status(operation.getStatus());\n }\n\n const statusCode = response.statusCode;\n const headers = operation.getHeadersOf(statusCode);\n\n Object.entries(headers).forEach(([key, item]: any[]) => {\n if (!response.get(key)) {\n response.setHeader(key, String(item.example));\n }\n });\n\n if (operation.isRedirection(statusCode)) {\n response.redirect(statusCode, response.get(\"location\"));\n }\n}\n"]}
|
|
@@ -53,8 +53,8 @@ export declare class PlatformBuilder<App = TsED.Application> {
|
|
|
53
53
|
* @param settings
|
|
54
54
|
*/
|
|
55
55
|
static bootstrap<App = TsED.Application>(module: Type<any>, settings: PlatformBuilderSettings<App>): Promise<PlatformBuilder<App>>;
|
|
56
|
-
callback(): (req: IncomingMessage, res: ServerResponse) =>
|
|
57
|
-
callback(req: IncomingMessage, res: ServerResponse):
|
|
56
|
+
callback(): (req: IncomingMessage, res: ServerResponse) => void;
|
|
57
|
+
callback(req: IncomingMessage, res: ServerResponse): void;
|
|
58
58
|
log(...data: any[]): any;
|
|
59
59
|
/**
|
|
60
60
|
* Add classes to the components list
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
|
-
import {
|
|
2
|
+
import { DIContext, DIContextOptions } from "@tsed/di";
|
|
3
3
|
import { PlatformHandlerMetadata } from "@tsed/platform-router";
|
|
4
4
|
import { EndpointMetadata } from "@tsed/schema";
|
|
5
5
|
import { IncomingMessage, ServerResponse } from "http";
|
|
@@ -18,7 +18,7 @@ export interface PlatformContextOptions extends DIContextOptions {
|
|
|
18
18
|
ignoreUrlPatterns?: any[];
|
|
19
19
|
endpoint?: EndpointMetadata;
|
|
20
20
|
}
|
|
21
|
-
export declare class PlatformContext<PReq extends PlatformRequest = PlatformRequest, PRes extends PlatformResponse = PlatformResponse> extends DIContext
|
|
21
|
+
export declare class PlatformContext<PReq extends PlatformRequest = PlatformRequest, PRes extends PlatformResponse = PlatformResponse> extends DIContext {
|
|
22
22
|
#private;
|
|
23
23
|
event: IncomingEvent;
|
|
24
24
|
/**
|
|
@@ -41,26 +41,20 @@ export declare class PlatformContext<PReq extends PlatformRequest = PlatformRequ
|
|
|
41
41
|
* The current @@PlatformRequest@@.
|
|
42
42
|
*/
|
|
43
43
|
readonly request: PReq;
|
|
44
|
-
private ignoreUrlPatterns;
|
|
45
|
-
constructor(options: PlatformContextOptions);
|
|
46
44
|
/**
|
|
47
45
|
* The current @@EndpointMetadata@@ resolved by Ts.ED during the request.
|
|
48
46
|
*/
|
|
49
|
-
|
|
50
|
-
set endpoint(endpoint: EndpointMetadata);
|
|
47
|
+
endpoint: EndpointMetadata;
|
|
51
48
|
/**
|
|
52
49
|
* The current @@PlatformHandlerMetadata@@ resolved by Ts.ED during the request.
|
|
53
50
|
*/
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
* The current @@PlatformHandlerMetadata@@ resolved by Ts.ED during the request.
|
|
57
|
-
*/
|
|
58
|
-
set handlerMetadata(metadata: PlatformHandlerMetadata);
|
|
51
|
+
handlerMetadata: PlatformHandlerMetadata;
|
|
52
|
+
constructor(options: PlatformContextOptions);
|
|
59
53
|
get url(): string;
|
|
60
54
|
get app(): PlatformApplication<TsED.Application>;
|
|
61
55
|
start(): Promise<void>;
|
|
62
56
|
finish(): Promise<void>;
|
|
63
|
-
|
|
57
|
+
isFinished(): boolean;
|
|
64
58
|
isDone(): any;
|
|
65
59
|
/**
|
|
66
60
|
* Return the framework request instance (Express, Koa, etc...)
|
|
@@ -82,5 +76,4 @@ export declare class PlatformContext<PReq extends PlatformRequest = PlatformRequ
|
|
|
82
76
|
* Return the original application instance.
|
|
83
77
|
*/
|
|
84
78
|
getApp<T = any>(): T;
|
|
85
|
-
upgrade(event: IncomingEvent): void;
|
|
86
79
|
}
|