@tsed/common 7.0.0-beta.1 → 7.0.0-beta.10
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/lib/cjs/builder/PlatformBuilder.js +38 -27
- package/lib/cjs/builder/PlatformBuilder.js.map +1 -1
- package/lib/cjs/builder/PlatformControllerBuilder.js +5 -8
- package/lib/cjs/builder/PlatformControllerBuilder.js.map +1 -1
- package/lib/cjs/config/interfaces/PlatformMulterSettings.js.map +1 -1
- package/lib/cjs/config/services/PlatformConfiguration.js +2 -2
- package/lib/cjs/config/services/PlatformConfiguration.js.map +1 -1
- package/lib/cjs/decorators/multer/multipartFile.js.map +1 -1
- package/lib/cjs/domain/EndpointMetadata.js +2 -66
- package/lib/cjs/domain/EndpointMetadata.js.map +1 -1
- package/lib/cjs/domain/PlatformContext.js.map +1 -1
- package/lib/cjs/domain/PlatformRouteDetails.js.map +1 -1
- package/lib/cjs/exports.js +31 -0
- package/lib/cjs/exports.js.map +1 -0
- package/lib/cjs/index.js +66 -30
- package/lib/cjs/index.js.map +1 -1
- package/lib/cjs/interfaces/PlatformRouteOptions.js.map +1 -1
- package/lib/cjs/middlewares/PlatformAcceptMimesMiddleware.js +8 -8
- package/lib/cjs/middlewares/PlatformAcceptMimesMiddleware.js.map +1 -1
- package/lib/cjs/middlewares/PlatformMulterMiddleware.js +25 -13
- package/lib/cjs/middlewares/PlatformMulterMiddleware.js.map +1 -1
- package/lib/cjs/middlewares/__mock__/FakeCtrl.js +1 -1
- package/lib/cjs/middlewares/bindEndpointMiddleware.js.map +1 -1
- package/lib/cjs/package.json +3 -0
- package/lib/cjs/services/FakeAdapter.js +55 -0
- package/lib/cjs/services/FakeAdapter.js.map +1 -0
- package/lib/cjs/services/Platform.js +30 -9
- package/lib/cjs/services/Platform.js.map +1 -1
- package/lib/cjs/services/PlatformAdapter.js +14 -0
- package/lib/cjs/services/PlatformAdapter.js.map +1 -0
- package/lib/cjs/services/PlatformApplication.js +9 -11
- package/lib/cjs/services/PlatformApplication.js.map +1 -1
- package/lib/cjs/services/PlatformHandler.js +11 -9
- package/lib/cjs/services/PlatformHandler.js.map +1 -1
- package/lib/cjs/services/PlatformMiddlewaresChain.js +56 -15
- package/lib/cjs/services/PlatformMiddlewaresChain.js.map +1 -1
- package/lib/cjs/services/PlatformRequest.js +2 -2
- package/lib/cjs/services/PlatformResponse.js +5 -5
- package/lib/cjs/services/PlatformResponse.js.map +1 -1
- package/lib/cjs/services/PlatformRouter.js +14 -32
- package/lib/cjs/services/PlatformRouter.js.map +1 -1
- package/lib/cjs/services/PlatformTest.js +3 -2
- package/lib/cjs/services/PlatformTest.js.map +1 -1
- package/lib/cjs/utils/__mock__/Test1.js +8 -0
- package/lib/cjs/utils/__mock__/Test1.js.map +1 -0
- package/lib/cjs/utils/__mock__/Test2.js +8 -0
- package/lib/cjs/utils/__mock__/Test2.js.map +1 -0
- package/lib/cjs/utils/createContext.js +2 -2
- package/lib/cjs/utils/createContext.js.map +1 -1
- package/lib/cjs/utils/createHandlerMetadata.js +2 -2
- package/lib/cjs/utils/createHandlerMetadata.js.map +1 -1
- package/lib/cjs/utils/createHttpServer.js +3 -8
- package/lib/cjs/utils/createHttpServer.js.map +1 -1
- package/lib/cjs/utils/createHttpsServer.js +3 -8
- package/lib/cjs/utils/createHttpsServer.js.map +1 -1
- package/lib/cjs/utils/createInjector.js +11 -3
- package/lib/cjs/utils/createInjector.js.map +1 -1
- package/lib/cjs/utils/importProviders.js +29 -0
- package/lib/cjs/utils/importProviders.js.map +1 -0
- package/lib/cjs/utils/setResponseHeaders.js.map +1 -1
- package/lib/esm/builder/PlatformBuilder.js +25 -14
- package/lib/esm/builder/PlatformBuilder.js.map +1 -1
- package/lib/esm/builder/PlatformControllerBuilder.js +8 -11
- package/lib/esm/builder/PlatformControllerBuilder.js.map +1 -1
- package/lib/esm/config/interfaces/PlatformMulterSettings.js.map +1 -1
- package/lib/esm/config/services/PlatformConfiguration.js.map +1 -1
- package/lib/esm/decorators/multer/multerFileSize.js +1 -1
- package/lib/esm/decorators/multer/multerOptions.js +1 -1
- package/lib/esm/decorators/multer/multipartFile.js +1 -1
- package/lib/esm/decorators/multer/multipartFile.js.map +1 -1
- package/lib/esm/domain/ControllerProvider.js +1 -1
- package/lib/esm/domain/EndpointMetadata.js +1 -66
- package/lib/esm/domain/EndpointMetadata.js.map +1 -1
- package/lib/esm/domain/HandlerMetadata.js +1 -1
- package/lib/esm/domain/PlatformContext.js +3 -3
- package/lib/esm/domain/PlatformContext.js.map +1 -1
- package/lib/esm/domain/PlatformRouteDetails.js.map +1 -1
- package/lib/esm/exports.js +13 -0
- package/lib/esm/exports.js.map +1 -0
- package/lib/esm/index.js +66 -27
- package/lib/esm/index.js.map +1 -1
- package/lib/esm/interfaces/PlatformRouteOptions.js.map +1 -1
- package/lib/esm/middlewares/PlatformMulterMiddleware.js +16 -5
- package/lib/esm/middlewares/PlatformMulterMiddleware.js.map +1 -1
- package/lib/esm/middlewares/bindEndpointMiddleware.js.map +1 -1
- package/lib/esm/package.json +3 -0
- package/lib/esm/services/FakeAdapter.js +51 -0
- package/lib/esm/services/FakeAdapter.js.map +1 -0
- package/lib/esm/services/Platform.js +31 -10
- package/lib/esm/services/Platform.js.map +1 -1
- package/lib/esm/services/PlatformAdapter.js +10 -0
- package/lib/esm/services/PlatformAdapter.js.map +1 -0
- package/lib/esm/services/PlatformApplication.js +11 -13
- package/lib/esm/services/PlatformApplication.js.map +1 -1
- package/lib/esm/services/PlatformHandler.js +13 -11
- package/lib/esm/services/PlatformHandler.js.map +1 -1
- package/lib/esm/services/PlatformMiddlewaresChain.js +61 -20
- package/lib/esm/services/PlatformMiddlewaresChain.js.map +1 -1
- package/lib/esm/services/PlatformResponse.js +1 -1
- package/lib/esm/services/PlatformResponse.js.map +1 -1
- package/lib/esm/services/PlatformRouter.js +13 -31
- package/lib/esm/services/PlatformRouter.js.map +1 -1
- package/lib/esm/services/PlatformTest.js +7 -7
- package/lib/esm/services/PlatformTest.js.map +1 -1
- package/lib/esm/utils/__mock__/Test1.js +4 -0
- package/lib/esm/utils/__mock__/Test1.js.map +1 -0
- package/lib/esm/utils/__mock__/Test2.js +4 -0
- package/lib/esm/utils/__mock__/Test2.js.map +1 -0
- package/lib/esm/utils/createContext.js +5 -5
- package/lib/esm/utils/createContext.js.map +1 -1
- package/lib/esm/utils/createHandlerMetadata.js +3 -3
- package/lib/esm/utils/createHandlerMetadata.js.map +1 -1
- package/lib/esm/utils/createHttpServer.js +3 -8
- package/lib/esm/utils/createHttpServer.js.map +1 -1
- package/lib/esm/utils/createHttpsServer.js +3 -8
- package/lib/esm/utils/createHttpsServer.js.map +1 -1
- package/lib/esm/utils/createInjector.js +18 -10
- package/lib/esm/utils/createInjector.js.map +1 -1
- package/lib/esm/utils/importProviders.js +25 -0
- package/lib/esm/utils/importProviders.js.map +1 -0
- package/lib/esm/utils/setResponseHeaders.js.map +1 -1
- package/lib/esm/utils/useCtxHandler.js +1 -1
- package/lib/types/builder/PlatformBuilder.d.ts +7 -6
- package/lib/types/builder/PlatformControllerBuilder.d.ts +2 -1
- package/lib/types/config/interfaces/PlatformMulterSettings.d.ts +6 -130
- package/lib/types/config/services/PlatformConfiguration.d.ts +2 -1
- package/lib/types/domain/EndpointMetadata.d.ts +1 -50
- package/lib/types/domain/PlatformContext.d.ts +1 -1
- package/lib/types/domain/PlatformRouteDetails.d.ts +2 -3
- package/lib/types/exports.d.ts +12 -0
- package/lib/types/index.d.ts +63 -17
- package/lib/types/interfaces/PlatformRouteOptions.d.ts +1 -1
- package/lib/types/middlewares/PlatformMulterMiddleware.d.ts +8 -3
- package/lib/types/middlewares/bindEndpointMiddleware.d.ts +2 -1
- package/lib/types/services/FakeAdapter.d.ts +30 -0
- package/lib/types/services/Platform.d.ts +10 -1
- package/lib/types/services/PlatformAdapter.d.ts +47 -0
- package/lib/types/services/PlatformApplication.d.ts +3 -3
- package/lib/types/services/PlatformHandler.d.ts +1 -1
- package/lib/types/services/PlatformMiddlewaresChain.d.ts +6 -2
- package/lib/types/services/PlatformResponse.d.ts +6 -6
- package/lib/types/services/PlatformRouter.d.ts +10 -7
- package/lib/types/services/PlatformTest.d.ts +2 -2
- package/lib/types/utils/__mock__/Test1.d.ts +3 -0
- package/lib/types/utils/__mock__/Test2.d.ts +3 -0
- package/lib/types/utils/createHandlerMetadata.d.ts +1 -1
- package/lib/types/utils/createHttpServer.d.ts +1 -1
- package/lib/types/utils/createHttpsServer.d.ts +2 -1
- package/lib/types/utils/createInjector.d.ts +5 -3
- package/lib/types/utils/importProviders.d.ts +10 -0
- package/package.json +38 -26
- package/readme.md +50 -57
- package/tsconfig.compile.esm.json +2 -12
- package/lib/cjs/config/index.js +0 -7
- package/lib/cjs/config/index.js.map +0 -1
- package/lib/cjs/config/interfaces/index.js +0 -8
- package/lib/cjs/config/interfaces/index.js.map +0 -1
- package/lib/cjs/decorators/httpServer.js +0 -32
- package/lib/cjs/decorators/httpServer.js.map +0 -1
- package/lib/cjs/decorators/httpsServer.js +0 -32
- package/lib/cjs/decorators/httpsServer.js.map +0 -1
- package/lib/cjs/decorators/index.js +0 -21
- package/lib/cjs/decorators/index.js.map +0 -1
- package/lib/cjs/decorators/method/acceptMime.js +0 -28
- package/lib/cjs/decorators/method/acceptMime.js.map +0 -1
- package/lib/cjs/decorators/method/endpointFn.js +0 -15
- package/lib/cjs/decorators/method/endpointFn.js.map +0 -1
- package/lib/cjs/decorators/method/location.js +0 -6
- package/lib/cjs/decorators/method/location.js.map +0 -1
- package/lib/cjs/decorators/method/redirect.js +0 -6
- package/lib/cjs/decorators/method/redirect.js.map +0 -1
- package/lib/cjs/decorators/method/route.js +0 -13
- package/lib/cjs/decorators/method/route.js.map +0 -1
- package/lib/cjs/decorators/method/view.js +0 -34
- package/lib/cjs/decorators/method/view.js.map +0 -1
- package/lib/cjs/decorators/multer/index.js +0 -7
- package/lib/cjs/decorators/multer/index.js.map +0 -1
- package/lib/cjs/domain/index.js +0 -10
- package/lib/cjs/domain/index.js.map +0 -1
- package/lib/cjs/interfaces/PathType.js +0 -3
- package/lib/cjs/interfaces/PathType.js.map +0 -1
- package/lib/cjs/interfaces/PlatformAdapter.js +0 -3
- package/lib/cjs/interfaces/PlatformAdapter.js.map +0 -1
- package/lib/cjs/interfaces/index.js +0 -21
- package/lib/cjs/interfaces/index.js.map +0 -1
- package/lib/cjs/middlewares/index.js +0 -7
- package/lib/cjs/middlewares/index.js.map +0 -1
- package/lib/cjs/services/FakeRawDriver.js +0 -26
- package/lib/cjs/services/FakeRawDriver.js.map +0 -1
- package/lib/cjs/services/index.js +0 -12
- package/lib/cjs/services/index.js.map +0 -1
- package/lib/cjs/utils/index.js +0 -11
- package/lib/cjs/utils/index.js.map +0 -1
- package/lib/esm/config/index.js +0 -4
- package/lib/esm/config/index.js.map +0 -1
- package/lib/esm/config/interfaces/index.js +0 -5
- package/lib/esm/config/interfaces/index.js.map +0 -1
- package/lib/esm/decorators/httpServer.js +0 -28
- package/lib/esm/decorators/httpServer.js.map +0 -1
- package/lib/esm/decorators/httpsServer.js +0 -28
- package/lib/esm/decorators/httpsServer.js.map +0 -1
- package/lib/esm/decorators/index.js +0 -18
- package/lib/esm/decorators/index.js.map +0 -1
- package/lib/esm/decorators/method/acceptMime.js +0 -24
- package/lib/esm/decorators/method/acceptMime.js.map +0 -1
- package/lib/esm/decorators/method/endpointFn.js +0 -11
- package/lib/esm/decorators/method/endpointFn.js.map +0 -1
- package/lib/esm/decorators/method/location.js +0 -2
- package/lib/esm/decorators/method/location.js.map +0 -1
- package/lib/esm/decorators/method/redirect.js +0 -2
- package/lib/esm/decorators/method/redirect.js.map +0 -1
- package/lib/esm/decorators/method/route.js +0 -2
- package/lib/esm/decorators/method/route.js.map +0 -1
- package/lib/esm/decorators/method/view.js +0 -30
- package/lib/esm/decorators/method/view.js.map +0 -1
- package/lib/esm/decorators/multer/index.js +0 -4
- package/lib/esm/decorators/multer/index.js.map +0 -1
- package/lib/esm/domain/index.js +0 -7
- package/lib/esm/domain/index.js.map +0 -1
- package/lib/esm/interfaces/PathType.js +0 -2
- package/lib/esm/interfaces/PathType.js.map +0 -1
- package/lib/esm/interfaces/PlatformAdapter.js +0 -2
- package/lib/esm/interfaces/PlatformAdapter.js.map +0 -1
- package/lib/esm/interfaces/index.js +0 -18
- package/lib/esm/interfaces/index.js.map +0 -1
- package/lib/esm/middlewares/index.js +0 -4
- package/lib/esm/middlewares/index.js.map +0 -1
- package/lib/esm/services/FakeRawDriver.js +0 -22
- package/lib/esm/services/FakeRawDriver.js.map +0 -1
- package/lib/esm/services/index.js +0 -9
- package/lib/esm/services/index.js.map +0 -1
- package/lib/esm/utils/index.js +0 -8
- package/lib/esm/utils/index.js.map +0 -1
- package/lib/types/config/index.d.ts +0 -3
- package/lib/types/config/interfaces/index.d.ts +0 -97
- package/lib/types/decorators/httpServer.d.ts +0 -28
- package/lib/types/decorators/httpsServer.d.ts +0 -28
- package/lib/types/decorators/index.d.ts +0 -14
- package/lib/types/decorators/method/acceptMime.d.ts +0 -19
- package/lib/types/decorators/method/endpointFn.d.ts +0 -9
- package/lib/types/decorators/method/location.d.ts +0 -1
- package/lib/types/decorators/method/redirect.d.ts +0 -1
- package/lib/types/decorators/method/route.d.ts +0 -1
- package/lib/types/decorators/method/view.d.ts +0 -25
- package/lib/types/decorators/multer/index.d.ts +0 -3
- package/lib/types/domain/index.d.ts +0 -6
- package/lib/types/interfaces/PathType.d.ts +0 -1
- package/lib/types/interfaces/PlatformAdapter.d.ts +0 -19
- package/lib/types/interfaces/index.d.ts +0 -17
- package/lib/types/middlewares/index.d.ts +0 -3
- package/lib/types/services/FakeRawDriver.d.ts +0 -15
- package/lib/types/services/index.d.ts +0 -8
- package/lib/types/utils/index.d.ts +0 -7
|
@@ -106,7 +106,7 @@ let PlatformHandler = class PlatformHandler {
|
|
|
106
106
|
return;
|
|
107
107
|
}
|
|
108
108
|
const resolver = new AnyToPromiseWithCtx_1.AnyToPromiseWithCtx({ $ctx, err });
|
|
109
|
-
return
|
|
109
|
+
return (0, di_1.runInContext)($ctx, async () => {
|
|
110
110
|
try {
|
|
111
111
|
const { state, data, status, headers } = await resolver.call(handler);
|
|
112
112
|
if (state === core_1.AnyToPromiseStatus.RESOLVED) {
|
|
@@ -130,22 +130,24 @@ let PlatformHandler = class PlatformHandler {
|
|
|
130
130
|
}
|
|
131
131
|
async onError(er, requestOptions) {
|
|
132
132
|
const { next, $ctx, metadata } = requestOptions;
|
|
133
|
-
$ctx
|
|
133
|
+
if ($ctx) {
|
|
134
|
+
$ctx.data = er;
|
|
135
|
+
}
|
|
134
136
|
if (!next) {
|
|
135
137
|
throw er;
|
|
136
138
|
}
|
|
137
139
|
// istanbul ignore next
|
|
138
|
-
if (!$ctx ||
|
|
140
|
+
if (!$ctx || $ctx.isDone()) {
|
|
139
141
|
logger_1.$log.warn({
|
|
140
142
|
name: "HEADERS_SENT",
|
|
141
143
|
message: `An error was caught after the headers were sent to the client. The error comes from the handler: ${metadata.toString()}`,
|
|
142
144
|
stack: er.stack,
|
|
143
145
|
origin: er,
|
|
144
|
-
request_id: $ctx.id
|
|
146
|
+
request_id: $ctx === null || $ctx === void 0 ? void 0 : $ctx.id
|
|
145
147
|
});
|
|
146
148
|
return;
|
|
147
149
|
}
|
|
148
|
-
return next
|
|
150
|
+
return next(er);
|
|
149
151
|
}
|
|
150
152
|
/**
|
|
151
153
|
* Manage success scenario
|
|
@@ -219,15 +221,15 @@ let PlatformHandler = class PlatformHandler {
|
|
|
219
221
|
].filter(Boolean);
|
|
220
222
|
}
|
|
221
223
|
};
|
|
222
|
-
|
|
224
|
+
tslib_1.__decorate([
|
|
223
225
|
(0, di_1.Inject)(),
|
|
224
|
-
|
|
226
|
+
tslib_1.__metadata("design:type", platform_response_filter_1.PlatformResponseFilter)
|
|
225
227
|
], PlatformHandler.prototype, "responseFilter", void 0);
|
|
226
|
-
PlatformHandler =
|
|
228
|
+
PlatformHandler = tslib_1.__decorate([
|
|
227
229
|
(0, di_1.Injectable)({
|
|
228
230
|
scope: di_1.ProviderScope.SINGLETON
|
|
229
231
|
}),
|
|
230
|
-
|
|
232
|
+
tslib_1.__metadata("design:paramtypes", [di_1.InjectorService, platform_params_1.PlatformParams])
|
|
231
233
|
], PlatformHandler);
|
|
232
234
|
exports.PlatformHandler = PlatformHandler;
|
|
233
235
|
//# sourceMappingURL=PlatformHandler.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"PlatformHandler.js","sourceRoot":"","sources":["../../../src/services/PlatformHandler.ts"],"names":[],"mappings":";;;;AAAA,qCAAoE;AACpE,iCAAsF;AACtF,yCAAkC;AAClC,2DAAiF;AACjF,6EAAsE;AACtE,uEAAkE;AAElE,+DAA0D;AAE1D,2DAAsD;AAEtD,0EAAqE;AACrE,oEAA+D;AAY/D;;;GAGG;AAIH,IAAa,eAAe,GAA5B,MAAa,eAAe;IAI1B,YAAsB,QAAyB,EAAY,MAAsB;QAA3D,aAAQ,GAAR,QAAQ,CAAiB;QAAY,WAAM,GAAN,MAAM,CAAgB;IAAG,CAAC;IAErF;;;;OAIG;IACH,aAAa,CAAC,KAA+C,EAAE,UAAwC,EAAE;QACvG,OAAO,IAAI,CAAC,gBAAgB,CAAC,IAAA,6CAAqB,EAAC,IAAI,CAAC,QAAQ,EAAE,KAAK,EAAE,OAAO,CAAC,CAAC,CAAC;IACrF,CAAC;IAED;;;;OAIG;IACH,mBAAmB,CAAC,QAAkB,EAAE,WAAmB;QACzD,MAAM,OAAO,GAAG,IAAI,CAAC,cAAc,CACjC,IAAI,iCAAe,CAAC;YAClB,KAAK,EAAE,QAAQ,CAAC,OAAO;YACvB,MAAM,EAAE,QAAQ,CAAC,QAAQ;YACzB,IAAI,EAAE,yBAAW,CAAC,MAAM;YACxB,KAAK,EAAE,QAAQ,CAAC,KAAK;YACrB,WAAW;SACZ,CAAC,CACH,CAAC;QAEF,OAAO,KAAK,EAAE,IAAqB,EAAE,IAAU,EAAE,EAAE,CACjD,OAAO,CAAC;YAC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AC,QAAQ,CAAC,MAAM,EAAE,IAAI,IAAI,IAAI,IAAI,EAAE,CAAC;IACnD,CAAC;IAES,cAAc,CAAC,QAAyB;QAChD,OAAO,KAAK,EAAE,KAAgC,EAAE,EAAE,CAChD;YACE,QAAQ,CAAC,aAAa,IAAI,KAAK,CAAC,GAAG;YACnC,KAAK,CAAC,IAAI,CAAC,OAAO,CAAC,OAAO;YAC1B,KAAK,CAAC,IAAI,CAAC,QAAQ,CAAC,QAAQ;YAC5B,QAAQ,CAAC,eAAe,IAAI,KAAK,CAAC,IAAI;SACvC,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;IACtB,CAAC;CACF,CAAA;AA/OC;IADC,IAAA,WAAM,GAAE;2CACiB,iDAAsB;uDAAC;AAFtC,eAAe;IAH3B,IAAA,eAAU,EAAC;QACV,KAAK,EAAE,kBAAa,CAAC,SAAS;KAC/B,CAAC;kDAKgC,oBAAe,EAAoB,gCAAc;GAJtE,eAAe,CAiP3B;AAjPY,0CAAe","sourcesContent":["import {AnyToPromiseStatus, isFunction, isStream} from \"@tsed/core\";\nimport {Inject, Injectable, InjectorService, Provider, ProviderScope} from \"@tsed/di\";\nimport {$log} from \"@tsed/logger\";\nimport {ArgScope, HandlerWithScope, PlatformParams} from \"@tsed/platform-params\";\nimport {PlatformResponseFilter} from \"@tsed/platform-response-filter\";\nimport {AnyToPromiseWithCtx} from \"../domain/AnyToPromiseWithCtx\";\nimport {EndpointMetadata} from \"../domain/EndpointMetadata\";\nimport {HandlerMetadata} from \"../domain/HandlerMetadata\";\nimport {PlatformContext} from \"../domain/PlatformContext\";\nimport {HandlerType} from \"../interfaces/HandlerType\";\nimport {PlatformRouteWithoutHandlers} from \"../interfaces/PlatformRouteOptions\";\nimport {createHandlerMetadata} from \"../utils/createHandlerMetadata\";\nimport {setResponseHeaders} from \"../utils/setResponseHeaders\";\n\nexport interface OnRequestOptions {\n $ctx: PlatformContext;\n metadata: HandlerMetadata;\n handler: HandlerWithScope;\n next?: any;\n err?: any;\n\n [key: string]: any;\n}\n\n/**\n * Platform Handler abstraction layer. Wrap original class method to a pure platform handler (Express, Koa, etc...).\n * @platform\n */\n@Injectable({\n scope: ProviderScope.SINGLETON\n})\nexport class PlatformHandler {\n @Inject()\n protected responseFilter: PlatformResponseFilter;\n\n constructor(protected injector: InjectorService, protected params: PlatformParams) {}\n\n /**\n * Create a native middleware based on the given metadata and return an instance of AnyToPromiseWithCtx\n * @param input\n * @param options\n */\n createHandler(input: EndpointMetadata | HandlerMetadata | any, options: PlatformRouteWithoutHandlers = {}) {\n return this.createRawHandler(createHandlerMetadata(this.injector, input, options));\n }\n\n /**\n * Create injectable handler from the given provider\n * @param provider\n * @param propertyKey\n */\n createCustomHandler(provider: Provider, propertyKey: string) {\n const handler = this.compileHandler(\n new HandlerMetadata({\n token: provider.provide,\n target: provider.useClass,\n type: HandlerType.CUSTOM,\n scope: provider.scope,\n propertyKey\n })\n );\n\n return async ($ctx: PlatformContext, next?: any) =>\n handler({\n $ctx,\n next\n });\n }\n\n /**\n * Send the response to the consumer.\n * @param data\n * @param ctx\n * @protected\n */\n async flush(data: any, ctx: PlatformContext) {\n const {response} = ctx;\n\n if (!response.isDone()) {\n data = await this.responseFilter.serialize(data, ctx);\n data = await this.responseFilter.transform(data, ctx);\n response.body(data);\n }\n }\n\n compileHandler(metadata: HandlerMetadata) {\n if (metadata.type === HandlerType.CTX_FN) {\n return async (scope: Record<string, any>) => {\n // @ts-ignore\n return this.onCtxRequest({\n ...scope,\n handler: () => metadata.handler(scope.$ctx),\n metadata\n });\n };\n }\n\n const promise = this.params.compileHandler<PlatformContext>({\n token: metadata.token,\n propertyKey: metadata.propertyKey,\n getCustomArgs: metadata.injectable ? undefined : this.getDefaultArgs(metadata)\n });\n\n return async (scope: Record<string, any>) => {\n const handler = await promise;\n // @ts-ignore\n return this.onRequest({\n ...scope,\n handler,\n metadata\n });\n };\n }\n\n protected async onCtxRequest(requestOptions: OnRequestOptions): Promise<any> {\n await requestOptions.handler(requestOptions);\n\n return this.next(requestOptions);\n }\n\n /**\n * Call handler when a request his handle\n * @param requestOptions\n */\n protected async onRequest(requestOptions: OnRequestOptions): Promise<any> {\n const {$ctx, metadata, err, handler} = requestOptions;\n // istanbul ignore next\n if (!$ctx || $ctx.isDone()) {\n $log.error({\n name: \"HEADERS_SENT\",\n message: `An endpoint is called but the response is already send to the client. The call comes from the handler: ${metadata.toString()}`\n });\n return;\n }\n\n const resolver = new AnyToPromiseWithCtx({$ctx, err});\n\n return this.injector.runInContext($ctx, async () => {\n try {\n const {state, data, status, headers} = await resolver.call(handler);\n\n if (state === AnyToPromiseStatus.RESOLVED) {\n if (status) {\n $ctx.response.status(status);\n }\n\n if (headers) {\n $ctx.response.setHeaders(headers);\n }\n\n if (data !== undefined) {\n $ctx.data = data;\n }\n\n // Can be canceled by the handler itself\n return await this.onSuccess($ctx.data, requestOptions);\n }\n } catch (er) {\n return this.onError(er, requestOptions);\n }\n });\n }\n\n protected async onError(er: Error, requestOptions: OnRequestOptions) {\n const {next, $ctx, metadata} = requestOptions;\n $ctx.data = er;\n\n if (!next) {\n throw er;\n }\n\n // istanbul ignore next\n if (!$ctx || $ctx?.isDone()) {\n $log.warn({\n name: \"HEADERS_SENT\",\n message: `An error was caught after the headers were sent to the client. The error comes from the handler: ${metadata.toString()}`,\n stack: er.stack,\n origin: er,\n request_id: $ctx.id\n });\n\n return;\n }\n\n return next && next(er);\n }\n\n /**\n * Manage success scenario\n * @param data\n * @param requestOptions\n * @protected\n */\n protected async onSuccess(data: any, requestOptions: OnRequestOptions) {\n const {metadata, $ctx, next} = requestOptions;\n\n // istanbul ignore next\n if ($ctx?.isDone()) {\n return;\n }\n\n // set headers each times that an endpoint is called\n if (metadata.type === HandlerType.ENDPOINT) {\n this.setHeaders($ctx);\n }\n\n // call returned middleware\n if (isFunction(data) && !isStream(data)) {\n return this.callReturnedMiddleware(data, $ctx, next);\n }\n\n if (metadata.isFinal()) {\n return this.flush(data, $ctx);\n }\n\n return this.next(requestOptions);\n }\n\n /**\n * Call the returned middleware by the handler.\n * @param middleware\n * @param ctx\n * @param next\n * @protected\n */\n protected callReturnedMiddleware(middleware: any, ctx: PlatformContext, next: any) {\n return middleware(ctx.getRequest(), ctx.getResponse(), next);\n }\n\n /**\n * create Raw handler\n * @param metadata\n */\n protected createRawHandler(metadata: HandlerMetadata) {\n if ([HandlerType.RAW_ERR_FN, HandlerType.RAW_FN].includes(metadata.type)) {\n return metadata.handler;\n }\n\n const handler = this.compileHandler(metadata);\n\n return async (request: any, response: any, next: any) => {\n return handler({\n next,\n $ctx: request.$ctx\n });\n };\n }\n\n /**\n * Set response headers\n * @param ctx\n * @protected\n */\n protected setHeaders(ctx: PlatformContext) {\n return setResponseHeaders(ctx);\n }\n\n protected next(requestOptions: OnRequestOptions) {\n const {$ctx, next} = requestOptions;\n\n return !$ctx.response.isDone() && next && next();\n }\n\n protected getDefaultArgs(metadata: HandlerMetadata) {\n return async (scope: ArgScope<PlatformContext>) =>\n [\n metadata.hasErrorParam && scope.err,\n scope.$ctx.request.request,\n scope.$ctx.response.response,\n metadata.hasNextFunction && scope.next\n ].filter(Boolean);\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"PlatformHandler.js","sourceRoot":"","sources":["../../../src/services/PlatformHandler.ts"],"names":[],"mappings":";;;;AAAA,qCAAoE;AACpE,iCAAoG;AACpG,yCAAkC;AAClC,2DAAiF;AACjF,6EAAsE;AAEtE,uEAAkE;AAClE,+DAA0D;AAE1D,2DAAsD;AAEtD,0EAAqE;AACrE,oEAA+D;AAY/D;;;GAGG;AAIH,IAAa,eAAe,GAA5B,MAAa,eAAe;IAI1B,YAAsB,QAAyB,EAAY,MAAsB;QAA3D,aAAQ,GAAR,QAAQ,CAAiB;QAAY,WAAM,GAAN,MAAM,CAAgB;IAAG,CAAC;IAErF;;;;OAIG;IACH,aAAa,CAAC,KAA+C,EAAE,UAAwC,EAAE;QACvG,OAAO,IAAI,CAAC,gBAAgB,CAAC,IAAA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AAM,OAAO,GAAG,IAAI,CAAC,cAAc,CAAC,QAAQ,CAAC,CAAC;QAE9C,OAAO,KAAK,EAAE,OAAY,EAAE,QAAa,EAAE,IAAS,EAAE,EAAE;YACtD,OAAO,OAAO,CAAC;gBACb,IAAI;gBACJ,IAAI,EAAE,OAAO,CAAC,IAAI;aACnB,CAAC,CAAC;QACL,CAAC,CAAC;IACJ,CAAC;IAED;;;;OAIG;IACO,UAAU,CAAC,GAAoB;QACvC,OAAO,IAAA,uCAAkB,EAAC,GAAG,CAAC,CAAC;IACjC,CAAC;IAES,IAAI,CAAC,cAAgC;QAC7C,MAAM,EAAC,IAAI,EAAE,IAAI,EAAC,GAAG,cAAc,CAAC;QAEpC,OAAO,CAAC,IAAI,CAAC,QAAQ,CAAC,MAAM,EAAE,IAAI,IAAI,IAAI,IAAI,EAAE,CAAC;IACnD,CAAC;IAES,cAAc,CAAC,QAAyB;QAChD,OAAO,KAAK,EAAE,KAAgC,EAAE,EAAE,CAChD;YACE,QAAQ,CAAC,aAAa,IAAI,KAAK,CAAC,GAAG;YACnC,KAAK,CAAC,IAAI,CAAC,OAAO,CAAC,OAAO;YAC1B,KAAK,CAAC,IAAI,CAAC,QAAQ,CAAC,QAAQ;YAC5B,QAAQ,CAAC,eAAe,IAAI,KAAK,CAAC,IAAI;SACvC,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;IACtB,CAAC;CACF,CAAA;AAlPC;IADC,IAAA,WAAM,GAAE;sCACiB,iDAAsB;uDAAC;AAFtC,eAAe;IAH3B,IAAA,eAAU,EAAC;QACV,KAAK,EAAE,kBAAa,CAAC,SAAS;KAC/B,CAAC;6CAKgC,oBAAe,EAAoB,gCAAc;GAJtE,eAAe,CAoP3B;AApPY,0CAAe","sourcesContent":["import {AnyToPromiseStatus, isFunction, isStream} from \"@tsed/core\";\nimport {Inject, Injectable, InjectorService, Provider, ProviderScope, runInContext} from \"@tsed/di\";\nimport {$log} from \"@tsed/logger\";\nimport {ArgScope, HandlerWithScope, PlatformParams} from \"@tsed/platform-params\";\nimport {PlatformResponseFilter} from \"@tsed/platform-response-filter\";\nimport {EndpointMetadata} from \"@tsed/schema\";\nimport {AnyToPromiseWithCtx} from \"../domain/AnyToPromiseWithCtx\";\nimport {HandlerMetadata} from \"../domain/HandlerMetadata\";\nimport {PlatformContext} from \"../domain/PlatformContext\";\nimport {HandlerType} from \"../interfaces/HandlerType\";\nimport {PlatformRouteWithoutHandlers} from \"../interfaces/PlatformRouteOptions\";\nimport {createHandlerMetadata} from \"../utils/createHandlerMetadata\";\nimport {setResponseHeaders} from \"../utils/setResponseHeaders\";\n\nexport interface OnRequestOptions {\n $ctx: PlatformContext;\n metadata: HandlerMetadata;\n handler: HandlerWithScope;\n next?: any;\n err?: any;\n\n [key: string]: any;\n}\n\n/**\n * Platform Handler abstraction layer. Wrap original class method to a pure platform handler (Express, Koa, etc...).\n * @platform\n */\n@Injectable({\n scope: ProviderScope.SINGLETON\n})\nexport class PlatformHandler {\n @Inject()\n protected responseFilter: PlatformResponseFilter;\n\n constructor(protected injector: InjectorService, protected params: PlatformParams) {}\n\n /**\n * Create a native middleware based on the given metadata and return an instance of AnyToPromiseWithCtx\n * @param input\n * @param options\n */\n createHandler(input: EndpointMetadata | HandlerMetadata | any, options: PlatformRouteWithoutHandlers = {}) {\n return this.createRawHandler(createHandlerMetadata(this.injector, input, options));\n }\n\n /**\n * Create injectable handler from the given provider\n * @param provider\n * @param propertyKey\n */\n createCustomHandler(provider: Provider, propertyKey: string) {\n const handler = this.compileHandler(\n new HandlerMetadata({\n token: provider.provide,\n target: provider.useClass,\n type: HandlerType.CUSTOM,\n scope: provider.scope,\n propertyKey\n })\n );\n\n return async ($ctx: PlatformContext, next?: any) =>\n handler({\n $ctx,\n next\n });\n }\n\n /**\n * Send the response to the consumer.\n * @param data\n * @param ctx\n * @protected\n */\n async flush(data: any, ctx: PlatformContext) {\n const {response} = ctx;\n\n if (!response.isDone()) {\n data = await this.responseFilter.serialize(data, ctx);\n data = await this.responseFilter.transform(data, ctx);\n response.body(data);\n }\n }\n\n compileHandler(metadata: HandlerMetadata) {\n if (metadata.type === HandlerType.CTX_FN) {\n return async (scope: Record<string, any>) => {\n // @ts-ignore\n return this.onCtxRequest({\n ...scope,\n handler: () => metadata.handler(scope.$ctx),\n metadata\n });\n };\n }\n\n const promise = this.params.compileHandler<PlatformContext>({\n token: metadata.token,\n propertyKey: metadata.propertyKey,\n getCustomArgs: metadata.injectable ? undefined : this.getDefaultArgs(metadata)\n });\n\n return async (scope: Record<string, any>) => {\n const handler = await promise;\n // @ts-ignore\n return this.onRequest({\n ...scope,\n handler,\n metadata\n });\n };\n }\n\n protected async onCtxRequest(requestOptions: OnRequestOptions): Promise<any> {\n await requestOptions.handler(requestOptions);\n\n return this.next(requestOptions);\n }\n\n /**\n * Call handler when a request his handle\n * @param requestOptions\n */\n protected async onRequest(requestOptions: OnRequestOptions): Promise<any> {\n const {$ctx, metadata, err, handler} = requestOptions;\n // istanbul ignore next\n if (!$ctx || $ctx.isDone()) {\n $log.error({\n name: \"HEADERS_SENT\",\n message: `An endpoint is called but the response is already send to the client. The call comes from the handler: ${metadata.toString()}`\n });\n return;\n }\n\n const resolver = new AnyToPromiseWithCtx({$ctx, err});\n\n return runInContext($ctx, async () => {\n try {\n const {state, data, status, headers} = await resolver.call(handler);\n\n if (state === AnyToPromiseStatus.RESOLVED) {\n if (status) {\n $ctx.response.status(status);\n }\n\n if (headers) {\n $ctx.response.setHeaders(headers);\n }\n\n if (data !== undefined) {\n $ctx.data = data;\n }\n\n // Can be canceled by the handler itself\n return await this.onSuccess($ctx.data, requestOptions);\n }\n } catch (er) {\n return this.onError(er, requestOptions);\n }\n });\n }\n\n protected async onError(er: Error, requestOptions: OnRequestOptions) {\n const {next, $ctx, metadata} = requestOptions;\n\n if ($ctx) {\n $ctx.data = er;\n }\n\n if (!next) {\n throw er;\n }\n\n // istanbul ignore next\n if (!$ctx || $ctx.isDone()) {\n $log.warn({\n name: \"HEADERS_SENT\",\n message: `An error was caught after the headers were sent to the client. The error comes from the handler: ${metadata.toString()}`,\n stack: er.stack,\n origin: er,\n request_id: $ctx?.id\n });\n\n return;\n }\n\n return next(er);\n }\n\n /**\n * Manage success scenario\n * @param data\n * @param requestOptions\n * @protected\n */\n protected async onSuccess(data: any, requestOptions: OnRequestOptions) {\n const {metadata, $ctx, next} = requestOptions;\n\n // istanbul ignore next\n if ($ctx?.isDone()) {\n return;\n }\n\n // set headers each times that an endpoint is called\n if (metadata.type === HandlerType.ENDPOINT) {\n this.setHeaders($ctx);\n }\n\n // call returned middleware\n if (isFunction(data) && !isStream(data)) {\n return this.callReturnedMiddleware(data, $ctx, next);\n }\n\n if (metadata.isFinal()) {\n return this.flush(data, $ctx);\n }\n\n return this.next(requestOptions);\n }\n\n /**\n * Call the returned middleware by the handler.\n * @param middleware\n * @param ctx\n * @param next\n * @protected\n */\n protected callReturnedMiddleware(middleware: any, ctx: PlatformContext, next: any) {\n return middleware(ctx.getRequest(), ctx.getResponse(), next);\n }\n\n /**\n * create Raw handler\n * @param metadata\n */\n protected createRawHandler(metadata: HandlerMetadata) {\n if ([HandlerType.RAW_ERR_FN, HandlerType.RAW_FN].includes(metadata.type)) {\n return metadata.handler;\n }\n\n const handler = this.compileHandler(metadata);\n\n return async (request: any, response: any, next: any) => {\n return handler({\n next,\n $ctx: request.$ctx\n });\n };\n }\n\n /**\n * Set response headers\n * @param ctx\n * @protected\n */\n protected setHeaders(ctx: PlatformContext) {\n return setResponseHeaders(ctx);\n }\n\n protected next(requestOptions: OnRequestOptions) {\n const {$ctx, next} = requestOptions;\n\n return !$ctx.response.isDone() && next && next();\n }\n\n protected getDefaultArgs(metadata: HandlerMetadata) {\n return async (scope: ArgScope<PlatformContext>) =>\n [\n metadata.hasErrorParam && scope.err,\n scope.$ctx.request.request,\n scope.$ctx.response.response,\n metadata.hasNextFunction && scope.next\n ].filter(Boolean);\n }\n}\n"]}
|
|
@@ -4,30 +4,71 @@ exports.PlatformMiddlewaresChain = void 0;
|
|
|
4
4
|
const tslib_1 = require("tslib");
|
|
5
5
|
const di_1 = require("@tsed/di");
|
|
6
6
|
const platform_params_1 = require("@tsed/platform-params");
|
|
7
|
+
const schema_1 = require("@tsed/schema");
|
|
7
8
|
const bindEndpointMiddleware_1 = require("../middlewares/bindEndpointMiddleware");
|
|
8
9
|
const PlatformAcceptMimesMiddleware_1 = require("../middlewares/PlatformAcceptMimesMiddleware");
|
|
9
10
|
const PlatformMulterMiddleware_1 = require("../middlewares/PlatformMulterMiddleware");
|
|
10
11
|
const useCtxHandler_1 = require("../utils/useCtxHandler");
|
|
12
|
+
const PlatformAdapter_1 = require("../services/PlatformAdapter");
|
|
13
|
+
const core_1 = require("@tsed/core");
|
|
11
14
|
let PlatformMiddlewaresChain = class PlatformMiddlewaresChain {
|
|
12
|
-
get(provider, operationRoute) {
|
|
15
|
+
get(provider, operationRoute, parentMiddlewares = []) {
|
|
13
16
|
const { endpoint } = operationRoute;
|
|
14
17
|
const { beforeMiddlewares, middlewares: mldwrs, afterMiddlewares } = endpoint;
|
|
15
|
-
const { middlewares: { use, useAfter } } = provider;
|
|
16
|
-
const
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
18
|
+
const { middlewares: { useBefore, use, useAfter } } = provider;
|
|
19
|
+
const allMiddlewares = [
|
|
20
|
+
...parentMiddlewares,
|
|
21
|
+
...useBefore,
|
|
22
|
+
...beforeMiddlewares,
|
|
23
|
+
...use,
|
|
24
|
+
...mldwrs,
|
|
25
|
+
endpoint,
|
|
26
|
+
...afterMiddlewares,
|
|
27
|
+
...useAfter
|
|
28
|
+
];
|
|
29
|
+
const { ACCEPT_MIMES, FILE, RAW_BODY, BODY } = this.getParamTypes(allMiddlewares, operationRoute);
|
|
30
|
+
return [
|
|
31
|
+
(0, useCtxHandler_1.useCtxHandler)((0, bindEndpointMiddleware_1.bindEndpointMiddleware)(endpoint)),
|
|
32
|
+
ACCEPT_MIMES && PlatformAcceptMimesMiddleware_1.PlatformAcceptMimesMiddleware,
|
|
33
|
+
FILE && PlatformMulterMiddleware_1.PlatformMulterMiddleware,
|
|
34
|
+
!FILE && RAW_BODY && this.adapter.bodyParser("raw"),
|
|
35
|
+
!FILE && !RAW_BODY && BODY && this.adapter.bodyParser("json"),
|
|
36
|
+
!FILE && !RAW_BODY && BODY && this.adapter.bodyParser("urlencoded"),
|
|
37
|
+
...allMiddlewares
|
|
38
|
+
].filter(Boolean);
|
|
39
|
+
}
|
|
40
|
+
hasAcceptMimes(operationRoute) {
|
|
41
|
+
return operationRoute.endpoint.acceptMimes.length || this.acceptMimes.length;
|
|
42
|
+
}
|
|
43
|
+
getParamTypes(middlewares, operationRoute) {
|
|
44
|
+
return middlewares.filter(core_1.isClass).reduce((paramTypes, token) => {
|
|
45
|
+
if (token !== operationRoute.endpoint) {
|
|
46
|
+
const entity = schema_1.JsonEntityStore.fromMethod(token, "use");
|
|
47
|
+
if (entity.decoratorType === "method") {
|
|
48
|
+
const { FILE, RAW_BODY, BODY } = entity.getParamTypes();
|
|
49
|
+
paramTypes.FILE = paramTypes.FILE || FILE;
|
|
50
|
+
paramTypes.RAW_BODY = paramTypes.RAW_BODY || RAW_BODY;
|
|
51
|
+
paramTypes.BODY = paramTypes.BODY || BODY;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
return paramTypes;
|
|
55
|
+
}, {
|
|
56
|
+
ACCEPT_MIMES: this.hasAcceptMimes(operationRoute),
|
|
57
|
+
FILE: operationRoute.has(platform_params_1.ParamTypes.FILES),
|
|
58
|
+
RAW_BODY: operationRoute.has(platform_params_1.ParamTypes.RAW_BODY),
|
|
59
|
+
BODY: operationRoute.has(platform_params_1.ParamTypes.BODY)
|
|
60
|
+
});
|
|
28
61
|
}
|
|
29
62
|
};
|
|
30
|
-
|
|
63
|
+
tslib_1.__decorate([
|
|
64
|
+
(0, di_1.Constant)("acceptMimes", []),
|
|
65
|
+
tslib_1.__metadata("design:type", Array)
|
|
66
|
+
], PlatformMiddlewaresChain.prototype, "acceptMimes", void 0);
|
|
67
|
+
tslib_1.__decorate([
|
|
68
|
+
(0, di_1.Inject)(),
|
|
69
|
+
tslib_1.__metadata("design:type", PlatformAdapter_1.PlatformAdapter)
|
|
70
|
+
], PlatformMiddlewaresChain.prototype, "adapter", void 0);
|
|
71
|
+
PlatformMiddlewaresChain = tslib_1.__decorate([
|
|
31
72
|
(0, di_1.Injectable)()
|
|
32
73
|
], PlatformMiddlewaresChain);
|
|
33
74
|
exports.PlatformMiddlewaresChain = PlatformMiddlewaresChain;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"PlatformMiddlewaresChain.js","sourceRoot":"","sources":["../../../src/services/PlatformMiddlewaresChain.ts"],"names":[],"mappings":";;;;AAAA,
|
|
1
|
+
{"version":3,"file":"PlatformMiddlewaresChain.js","sourceRoot":"","sources":["../../../src/services/PlatformMiddlewaresChain.ts"],"names":[],"mappings":";;;;AAAA,iCAAsD;AACtD,2DAAiD;AACjD,yCAAiE;AAEjE,kFAA6E;AAC7E,gGAA2F;AAC3F,sFAAiF;AACjF,0DAAqD;AACrD,iEAA4D;AAC5D,qCAAmC;AAGnC,IAAa,wBAAwB,GAArC,MAAa,wBAAwB;IAOnC,GAAG,CAAC,QAA4B,EAAE,cAAkC,EAAE,oBAA2B,EAAE;QACjG,MAAM,EAAC,QAAQ,EAAC,GAAG,cAAc,CAAC;QAClC,MAAM,EAAC,iBAAiB,EAAE,WAAW,EAAE,MAAM,EAAE,gBAAgB,EAAC,GAAG,QAAQ,CAAC;QAE5E,MAAM,EACJ,WAAW,EAAE,EAAC,SAAS,EAAE,GAAG,EAAE,QAAQ,EAAC,EACxC,GAAG,QAAQ,CAAC;QAEb,MAAM,cAAc,GAAG;YACrB,GAAG,iBAAiB;YACpB,GAAG,SAAS;YACZ,GAAG,iB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AI,EAAC,GAAG,MAAM,CAAC,aAAa,EAAE,CAAC;oBACtD,UAAU,CAAC,IAAI,GAAG,UAAU,CAAC,IAAI,IAAI,IAAI,CAAC;oBAC1C,UAAU,CAAC,QAAQ,GAAG,UAAU,CAAC,QAAQ,IAAI,QAAQ,CAAC;oBACtD,UAAU,CAAC,IAAI,GAAG,UAAU,CAAC,IAAI,IAAI,IAAI,CAAC;iBAC3C;aACF;YAED,OAAO,UAAU,CAAC;QACpB,CAAC,EACD;YACE,YAAY,EAAE,IAAI,CAAC,cAAc,CAAC,cAAc,CAAC;YACjD,IAAI,EAAE,cAAc,CAAC,GAAG,CAAC,4BAAU,CAAC,KAAK,CAAC;YAC1C,QAAQ,EAAE,cAAc,CAAC,GAAG,CAAC,4BAAU,CAAC,QAAQ,CAAC;YACjD,IAAI,EAAE,cAAc,CAAC,GAAG,CAAC,4BAAU,CAAC,IAAI,CAAC;SAC1C,CACF,CAAC;IACJ,CAAC;CACF,CAAA;AAjEC;IADC,IAAA,aAAQ,EAAC,aAAa,EAAE,EAAE,CAAC;;6DACI;AAGhC;IADC,IAAA,WAAM,GAAE;sCACU,iCAAe;yDAAC;AALxB,wBAAwB;IADpC,IAAA,eAAU,GAAE;GACA,wBAAwB,CAmEpC;AAnEY,4DAAwB","sourcesContent":["import {Constant, Inject, Injectable} from \"@tsed/di\";\nimport {ParamTypes} from \"@tsed/platform-params\";\nimport {JsonEntityStore, JsonOperationRoute} from \"@tsed/schema\";\nimport {ControllerProvider} from \"../domain/ControllerProvider\";\nimport {bindEndpointMiddleware} from \"../middlewares/bindEndpointMiddleware\";\nimport {PlatformAcceptMimesMiddleware} from \"../middlewares/PlatformAcceptMimesMiddleware\";\nimport {PlatformMulterMiddleware} from \"../middlewares/PlatformMulterMiddleware\";\nimport {useCtxHandler} from \"../utils/useCtxHandler\";\nimport {PlatformAdapter} from \"../services/PlatformAdapter\";\nimport {isClass} from \"@tsed/core\";\n\n@Injectable()\nexport class PlatformMiddlewaresChain {\n @Constant(\"acceptMimes\", [])\n protected acceptMimes: string[];\n\n @Inject()\n protected adapter: PlatformAdapter;\n\n get(provider: ControllerProvider, operationRoute: JsonOperationRoute, parentMiddlewares: any[] = []) {\n const {endpoint} = operationRoute;\n const {beforeMiddlewares, middlewares: mldwrs, afterMiddlewares} = endpoint;\n\n const {\n middlewares: {useBefore, use, useAfter}\n } = provider;\n\n const allMiddlewares = [\n ...parentMiddlewares,\n ...useBefore,\n ...beforeMiddlewares,\n ...use,\n ...mldwrs,\n endpoint,\n ...afterMiddlewares,\n ...useAfter\n ];\n\n const {ACCEPT_MIMES, FILE, RAW_BODY, BODY} = this.getParamTypes(allMiddlewares, operationRoute);\n\n return [\n useCtxHandler(bindEndpointMiddleware(endpoint)),\n ACCEPT_MIMES && PlatformAcceptMimesMiddleware,\n FILE && PlatformMulterMiddleware,\n !FILE && RAW_BODY && this.adapter.bodyParser(\"raw\"),\n !FILE && !RAW_BODY && BODY && this.adapter.bodyParser(\"json\"),\n !FILE && !RAW_BODY && BODY && this.adapter.bodyParser(\"urlencoded\"),\n ...allMiddlewares\n ].filter(Boolean);\n }\n\n protected hasAcceptMimes(operationRoute: JsonOperationRoute) {\n return operationRoute.endpoint.acceptMimes.length || this.acceptMimes.length;\n }\n\n protected getParamTypes(middlewares: any[], operationRoute: JsonOperationRoute) {\n return middlewares.filter(isClass).reduce(\n (paramTypes, token) => {\n if (token !== operationRoute.endpoint) {\n const entity = JsonEntityStore.fromMethod(token, \"use\");\n\n if (entity.decoratorType === \"method\") {\n const {FILE, RAW_BODY, BODY} = entity.getParamTypes();\n paramTypes.FILE = paramTypes.FILE || FILE;\n paramTypes.RAW_BODY = paramTypes.RAW_BODY || RAW_BODY;\n paramTypes.BODY = paramTypes.BODY || BODY;\n }\n }\n\n return paramTypes;\n },\n {\n ACCEPT_MIMES: this.hasAcceptMimes(operationRoute),\n FILE: operationRoute.has(ParamTypes.FILES),\n RAW_BODY: operationRoute.has(ParamTypes.RAW_BODY),\n BODY: operationRoute.has(ParamTypes.BODY)\n }\n );\n }\n}\n"]}
|
|
@@ -129,10 +129,10 @@ let PlatformRequest = class PlatformRequest {
|
|
|
129
129
|
return this.raw;
|
|
130
130
|
}
|
|
131
131
|
};
|
|
132
|
-
PlatformRequest =
|
|
132
|
+
PlatformRequest = tslib_1.__decorate([
|
|
133
133
|
(0, di_1.Injectable)(),
|
|
134
134
|
(0, di_1.Scope)(di_1.ProviderScope.INSTANCE),
|
|
135
|
-
|
|
135
|
+
tslib_1.__metadata("design:paramtypes", [Object, Function])
|
|
136
136
|
], PlatformRequest);
|
|
137
137
|
exports.PlatformRequest = PlatformRequest;
|
|
138
138
|
//# sourceMappingURL=PlatformRequest.js.map
|
|
@@ -5,7 +5,7 @@ exports.PlatformResponse = void 0;
|
|
|
5
5
|
const tslib_1 = require("tslib");
|
|
6
6
|
const core_1 = require("@tsed/core");
|
|
7
7
|
const di_1 = require("@tsed/di");
|
|
8
|
-
const
|
|
8
|
+
const on_finished_1 = tslib_1.__importDefault(require("on-finished"));
|
|
9
9
|
/**
|
|
10
10
|
* Platform Response abstraction layer.
|
|
11
11
|
* @platform
|
|
@@ -42,7 +42,7 @@ let PlatformResponse = PlatformResponse_1 = class PlatformResponse {
|
|
|
42
42
|
return this.getRes();
|
|
43
43
|
}
|
|
44
44
|
static onFinished(res, cb) {
|
|
45
|
-
|
|
45
|
+
(0, on_finished_1.default)(res, cb);
|
|
46
46
|
}
|
|
47
47
|
/**
|
|
48
48
|
* Returns the HTTP response header specified by field. The match is case-insensitive.
|
|
@@ -191,7 +191,7 @@ let PlatformResponse = PlatformResponse_1 = class PlatformResponse {
|
|
|
191
191
|
* @param options
|
|
192
192
|
*/
|
|
193
193
|
async render(path, options = {}) {
|
|
194
|
-
const { PlatformViews } = await Promise.resolve().then(() =>
|
|
194
|
+
const { PlatformViews } = await Promise.resolve().then(() => tslib_1.__importStar(require("@tsed/platform-views")));
|
|
195
195
|
const platformViews = await this.$ctx.injector.lazyInvoke(PlatformViews);
|
|
196
196
|
return platformViews.render(path, {
|
|
197
197
|
...this.locals,
|
|
@@ -262,10 +262,10 @@ let PlatformResponse = PlatformResponse_1 = class PlatformResponse {
|
|
|
262
262
|
return this.getRes().headersSent;
|
|
263
263
|
}
|
|
264
264
|
};
|
|
265
|
-
PlatformResponse = PlatformResponse_1 =
|
|
265
|
+
PlatformResponse = PlatformResponse_1 = tslib_1.__decorate([
|
|
266
266
|
(0, di_1.Injectable)(),
|
|
267
267
|
(0, di_1.Scope)(di_1.ProviderScope.INSTANCE),
|
|
268
|
-
|
|
268
|
+
tslib_1.__metadata("design:paramtypes", [Object, Function])
|
|
269
269
|
], PlatformResponse);
|
|
270
270
|
exports.PlatformResponse = PlatformResponse;
|
|
271
271
|
//# sourceMappingURL=PlatformResponse.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"PlatformResponse.js","sourceRoot":"","sources":["../../../src/services/PlatformResponse.ts"],"names":[],"mappings":";;;;;AAAA,qCAAmE;AACnE,iCAA0D;AAM1D,MAAM,UAAU,GAAG,OAAO,CAAC,aAAa,CAAC,CAAC;AAa1C;;;GAGG;AAGH,IAAa,gBAAgB,wBAA7B,MAAa,gBAAgB;IAU3B,YAAY,EAAC,QAAQ,EAAgB,EAAY,IAAqB;QAArB,SAAI,GAAJ,IAAI,CAAiB;QACpE,IAAI,CAAC,GAAG,GAAG,QAAe,CAAC;IAC7B,CAAC;IAED;;OAEG;IACH,IAAI,UAAU;QACZ,OAAO,IAAI,CAAC,GAAG,CAAC,UAAU,CAAC;IAC7B,CAAC;IAED;;;;OAIG;IACH,IAAI,MAAM;QACR,OAAO,IAAI,CAAC,GAAG,CAAC,MAAM,CAAC;IACzB,CAAC;IAED;;OAEG;IACH,IAAI,QAAQ;QACV,OAAO,IAAI,CAAC,WAAW,EAAE,CAAC;IAC5B,CAAC;IAED;;OAEG;IACH,IAAI,GAAG;QACL,OAAO,IAAI,CAAC,MAAM,EAAE,CAAC;IACvB,CAAC;IAED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,IAAI,CAAC;IACd,CAAC;IAED,OAAO;QACL,OAAO,IAAI,CAAC,IAAI,CAAC;IACnB,CAAC;IAED;;;OAGG;IACH,KAAK,CAAC,EAAY;QAChB,kBAAgB,CAAC,UAAU,CAAC,IAAI,CAAC,MAAM,EAAE,EAAE,EAAE,CAAC,CAAC;QAE/C,OAAO,IAAI,CAAC;IACd,CAAC;IAED,MAAM;QACJ,IAAI,CAAC,IAAI,CAAC,GAAG,EAAE;YACb,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,aAAa;QACb,OAAO,IAAI,CAAC,GAAG,CAAC;QAChB,OAAO,IAAI,CAAC,IAAI,CAAC;QACjB,aAAa;QACb,OAAO,IAAI,CAAC,OAAO,CAAC;QACpB,aAAa;QACb,OAAO,IAAI,CAAC,IAAI,CAAC;IACnB,CAAC;IAED,aAAa;QACX,OAAO,IAAI,CAAC,MAAM,EAAE,CAAC,WAAW,CAAC;IACnC,CAAC;CACF,CAAA;AAtTY,gBAAgB;IAF5B,IAAA,eAAU,GAAE;IACZ,IAAA,UAAK,EAAC,kBAAa,CAAC,QAAQ,CAAC;;GACjB,gBAAgB,CAsT5B;AAtTY,4CAAgB","sourcesContent":["import {isBoolean, isNumber, isStream, isString} from \"@tsed/core\";\nimport {Injectable, ProviderScope, Scope} from \"@tsed/di\";\nimport {ServerResponse} from \"http\";\nimport {IncomingEvent} from \"../interfaces/IncomingEvent\";\nimport type {PlatformRequest} from \"./PlatformRequest\";\nimport type {PlatformContext} from \"../domain/PlatformContext\";\n\nconst onFinished = require(\"on-finished\");\n\ndeclare global {\n namespace TsED {\n // @ts-ignore\n export interface Response {\n // req: any;\n }\n }\n}\n\nexport type HeaderValue = Array<boolean | number | string> | boolean | number | string;\n\n/**\n * Platform Response abstraction layer.\n * @platform\n */\n@Injectable()\n@Scope(ProviderScope.INSTANCE)\nexport class PlatformResponse<T extends Record<string, any> = any> {\n /**\n * The current @@PlatformRequest@@.\n */\n public request: PlatformRequest;\n\n data: any;\n\n raw: T;\n\n constructor({response}: IncomingEvent, protected $ctx: PlatformContext) {\n this.raw = 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 static onFinished(res: any, cb: Function) {\n onFinished(res, cb);\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(): Record<string, HeaderValue> {\n return this.raw.getHeaders();\n }\n\n /**\n * Return the Framework response object (express, koa, etc...)\n */\n getResponse<Res = T>(): Res {\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: Record<string, HeaderValue>) {\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: ReadableStream | 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 choose 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: Function): this {\n PlatformResponse.onFinished(this.getRes(), cb);\n\n return this;\n }\n\n isDone(): boolean {\n if (!this.raw) {\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 // @ts-ignore\n delete this.raw;\n delete this.data;\n // @ts-ignore\n delete this.request;\n // @ts-ignore\n delete this.$ctx;\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,qCAAmE;AACnE,iCAA0D;AAE1D,sEAAqC;AAgBrC;;;GAGG;AAGH,IAAa,gBAAgB,wBAA7B,MAAa,gBAAgB;IAU3B,YAAY,EAAC,QAAQ,EAAgB,EAAY,IAAqB;QAArB,SAAI,GAAJ,IAAI,CAAiB;QACpE,IAAI,CAAC,GAAG,GAAG,QAAe,CAAC;IAC7B,CAAC;IAED;;OAEG;IACH,IAAI,UA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AAgB","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 {IncomingEvent} from \"../interfaces/IncomingEvent\";\nimport type {PlatformRequest} from \"./PlatformRequest\";\nimport type {PlatformContext} from \"../domain/PlatformContext\";\n\ndeclare global {\n namespace TsED {\n // @ts-ignore\n export interface Response {\n // req: any;\n }\n }\n}\n\nexport type HeaderValue = Array<boolean | number | string> | boolean | number | string;\n\n/**\n * Platform Response abstraction layer.\n * @platform\n */\n@Injectable()\n@Scope(ProviderScope.INSTANCE)\nexport class PlatformResponse<T extends Record<string, any> = any> {\n /**\n * The current @@PlatformRequest@@.\n */\n public request: PlatformRequest;\n\n data: any;\n\n raw: T;\n\n constructor({response}: IncomingEvent, protected $ctx: PlatformContext) {\n this.raw = 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 static onFinished(res: any, cb: (er: Error | null, message: string) => void) {\n onFinished(res, cb);\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<Res = T>(): Res {\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 choose 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 PlatformResponse.onFinished(this.getRes(), cb);\n\n return this;\n }\n\n isDone(): boolean {\n if (!this.raw) {\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 // @ts-ignore\n delete this.raw;\n delete this.data;\n // @ts-ignore\n delete this.request;\n // @ts-ignore\n delete this.$ctx;\n }\n\n isHeadersSent() {\n return this.getRes().headersSent;\n }\n}\n"]}
|
|
@@ -4,9 +4,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
4
4
|
exports.PlatformRouter = exports.PLATFORM_ROUTER_OPTIONS = void 0;
|
|
5
5
|
const tslib_1 = require("tslib");
|
|
6
6
|
const di_1 = require("@tsed/di");
|
|
7
|
-
const util_1 = require("util");
|
|
8
|
-
const FakeRawDriver_1 = require("./FakeRawDriver");
|
|
9
7
|
const PlatformHandler_1 = require("./PlatformHandler");
|
|
8
|
+
const PlatformAdapter_1 = require("./PlatformAdapter");
|
|
10
9
|
/**
|
|
11
10
|
* @ignore
|
|
12
11
|
*/
|
|
@@ -16,10 +15,13 @@ exports.PLATFORM_ROUTER_OPTIONS = Symbol.for("PlatformRouterOptions");
|
|
|
16
15
|
* @platform
|
|
17
16
|
*/
|
|
18
17
|
let PlatformRouter = PlatformRouter_1 = class PlatformRouter {
|
|
19
|
-
constructor(platformHandler) {
|
|
18
|
+
constructor(platformHandler, adapter, routerOptions = {}) {
|
|
20
19
|
this.platformHandler = platformHandler;
|
|
20
|
+
this.adapter = adapter;
|
|
21
21
|
this.isBuilt = false;
|
|
22
|
-
|
|
22
|
+
const { router, callback } = adapter.router(routerOptions);
|
|
23
|
+
this.rawRouter = this.raw = router;
|
|
24
|
+
this.callback = callback;
|
|
23
25
|
}
|
|
24
26
|
/**
|
|
25
27
|
* Create a new instance of PlatformRouter
|
|
@@ -31,12 +33,6 @@ let PlatformRouter = PlatformRouter_1 = class PlatformRouter {
|
|
|
31
33
|
locals.set(exports.PLATFORM_ROUTER_OPTIONS, routerOptions);
|
|
32
34
|
return injector.invoke(PlatformRouter_1, locals);
|
|
33
35
|
}
|
|
34
|
-
static createRawRouter() {
|
|
35
|
-
return (0, FakeRawDriver_1.createFakeRawDriver)();
|
|
36
|
-
}
|
|
37
|
-
callback() {
|
|
38
|
-
return this.raw;
|
|
39
|
-
}
|
|
40
36
|
getRouter() {
|
|
41
37
|
return this.rawRouter;
|
|
42
38
|
}
|
|
@@ -76,26 +72,10 @@ let PlatformRouter = PlatformRouter_1 = class PlatformRouter {
|
|
|
76
72
|
return this.addRoute({ method: "options", path, handlers, isFinal: true });
|
|
77
73
|
}
|
|
78
74
|
statics(path, options) {
|
|
79
|
-
return this;
|
|
75
|
+
return this.use(path, this.adapter.statics(path, options));
|
|
80
76
|
}
|
|
81
77
|
multer(options) {
|
|
82
|
-
|
|
83
|
-
const makePromise = (multer, name) => {
|
|
84
|
-
// istanbul ignore next
|
|
85
|
-
if (!multer[name])
|
|
86
|
-
return;
|
|
87
|
-
const fn = multer[name];
|
|
88
|
-
multer[name] = function apply(...args) {
|
|
89
|
-
const middleware = Reflect.apply(fn, this, args);
|
|
90
|
-
return (req, res) => (0, util_1.promisify)(middleware)(req, res);
|
|
91
|
-
};
|
|
92
|
-
};
|
|
93
|
-
makePromise(m, "any");
|
|
94
|
-
makePromise(m, "array");
|
|
95
|
-
makePromise(m, "fields");
|
|
96
|
-
makePromise(m, "none");
|
|
97
|
-
makePromise(m, "single");
|
|
98
|
-
return m;
|
|
78
|
+
return this.adapter.multipart(options);
|
|
99
79
|
}
|
|
100
80
|
mapHandlers(handlers, options = {}) {
|
|
101
81
|
return handlers.reduce((list, handler, index) => {
|
|
@@ -112,15 +92,17 @@ let PlatformRouter = PlatformRouter_1 = class PlatformRouter {
|
|
|
112
92
|
}, []);
|
|
113
93
|
}
|
|
114
94
|
};
|
|
115
|
-
|
|
95
|
+
tslib_1.__decorate([
|
|
116
96
|
(0, di_1.Inject)(),
|
|
117
|
-
|
|
97
|
+
tslib_1.__metadata("design:type", di_1.InjectorService)
|
|
118
98
|
], PlatformRouter.prototype, "injector", void 0);
|
|
119
|
-
PlatformRouter = PlatformRouter_1 =
|
|
99
|
+
PlatformRouter = PlatformRouter_1 = tslib_1.__decorate([
|
|
120
100
|
(0, di_1.Injectable)({
|
|
121
101
|
scope: di_1.ProviderScope.INSTANCE
|
|
122
102
|
}),
|
|
123
|
-
(0,
|
|
103
|
+
tslib_1.__param(2, (0, di_1.Inject)(exports.PLATFORM_ROUTER_OPTIONS)),
|
|
104
|
+
tslib_1.__metadata("design:paramtypes", [PlatformHandler_1.PlatformHandler,
|
|
105
|
+
PlatformAdapter_1.PlatformAdapter, Object])
|
|
124
106
|
], PlatformRouter);
|
|
125
107
|
exports.PlatformRouter = PlatformRouter;
|
|
126
108
|
//# sourceMappingURL=PlatformRouter.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"PlatformRouter.js","sourceRoot":"","sources":["../../../src/services/PlatformRouter.ts"],"names":[],"mappings":";;;;;AAAA,iCAAsF;AACtF
|
|
1
|
+
{"version":3,"file":"PlatformRouter.js","sourceRoot":"","sources":["../../../src/services/PlatformRouter.ts"],"names":[],"mappings":";;;;;AAAA,iCAAsF;AACtF,uDAAkD;AAIlD,uDAAkD;AAGlD;;GAEG;AACU,QAAA,uBAAuB,GAAG,MAAM,CAAC,GAAG,CAAC,uBAAuB,CAAC,CAAC;AAS3E;;;GAGG;AAIH,IAAa,cAAc,sBAA3B,MAAa,cAAc;IAUzB,YACY,eAAgC,EACjC,OAAqC,EACb,gBAAwC,EAAE;QAFjE,oBAAe,GAAf,eAAe,CAAiB;QACjC,YAAO,GAAP,OAAO,CAA8B;QAThD,YAAO,GAAY,KAAK,CAAC;QAYvB,MAAM,EAAC,MAAM,EAAE,QAAQ,EAAC,GAAG,OAAO,CAAC,MAAM,CAAC,aAAa,CAAC,CAAC;QAEzD,IAAI,CAAC,SAAS,GAAG,IAAI,CAAC,GAAG,GAAG,MAAM,CAAC;QACnC,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;IAC3B,CAAC;IAED;;;;OAIG;IACH,MAAM,CAAC,MAAM,CAAC,QAAyB,EAAE,gBAAqB,EAAE;QAC9D,MAAM,MAAM,GAAG,IAAI,GAAG,EAAE,CAAC;QACzB,MAAM,CAAC,GAAG,CAAC,+BAAuB,EAAE,aAAa,CAAC,CAAC;QAEnD,OAAO,QAAQ,CAAC,MAAM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AAE,KAAK,EAAE,EAAE;YAC9C,IAAI,OAAO,OAAO,KAAK,QAAQ,EAAE;gBAC/B,OAAO,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;aAC7B;YAED,IAAI,OAAO,YAAY,gBAAc,EAAE;gBACrC,OAAO,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,QAAQ,EAAE,CAAC,CAAC;aACxC;YAED,OAAO,IAAI,CAAC,MAAM,CAChB,IAAI,CAAC,eAAe,CAAC,aAAa,CAAC,OAAO,EAAE;gBAC1C,GAAG,OAAO;gBACV,OAAO,EAAE,OAAO,CAAC,OAAO,CAAC,CAAC,CAAC,KAAK,KAAK,QAAQ,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,KAAK;aACjE,CAAC,CACH,CAAC;QACJ,CAAC,EAAE,EAAE,CAAC,CAAC;IACT,CAAC;CACF,CAAA;AAzGC;IADC,IAAA,WAAM,GAAE;sCACC,oBAAe;gDAAC;AANf,cAAc;IAH1B,IAAA,eAAU,EAAC;QACV,KAAK,EAAE,kBAAa,CAAC,QAAQ;KAC9B,CAAC;IAcG,mBAAA,IAAA,WAAM,EAAC,+BAAuB,CAAC,CAAA;6CAFL,iCAAe;QACxB,iCAAe;GAZxB,cAAc,CA+G1B;AA/GY,wCAAc","sourcesContent":["import {Inject, Injectable, InjectorService, PathType, ProviderScope} from \"@tsed/di\";\nimport {PlatformHandler} from \"./PlatformHandler\";\nimport {PlatformRouteOptions, PlatformRouteWithoutHandlers} from \"../interfaces/PlatformRouteOptions\";\nimport {PlatformStaticsOptions} from \"../config/interfaces/PlatformStaticsSettings\";\nimport {PlatformMulter, PlatformMulterSettings} from \"../config/interfaces/PlatformMulterSettings\";\nimport {PlatformAdapter} from \"./PlatformAdapter\";\nimport {RouterOptions} from \"express\";\n\n/**\n * @ignore\n */\nexport const PLATFORM_ROUTER_OPTIONS = Symbol.for(\"PlatformRouterOptions\");\n\ndeclare global {\n namespace TsED {\n // @ts-ignore\n export interface Router {}\n }\n}\n\n/**\n * Platform Router abstraction layer.\n * @platform\n */\n@Injectable({\n scope: ProviderScope.INSTANCE\n})\nexport class PlatformRouter<App = TsED.Application, Router = TsED.Router> {\n rawRouter: Router;\n raw: any;\n isBuilt: boolean = false;\n\n @Inject()\n injector: InjectorService;\n\n callback: () => any;\n\n constructor(\n protected platformHandler: PlatformHandler,\n readonly adapter: PlatformAdapter<App, Router>,\n @Inject(PLATFORM_ROUTER_OPTIONS) routerOptions: Partial<RouterOptions> = {}\n ) {\n const {router, callback} = adapter.router(routerOptions);\n\n this.rawRouter = this.raw = router;\n this.callback = callback;\n }\n\n /**\n * Create a new instance of PlatformRouter\n * @param injector\n * @param routerOptions\n */\n static create(injector: InjectorService, routerOptions: any = {}) {\n const locals = new Map();\n locals.set(PLATFORM_ROUTER_OPTIONS, routerOptions);\n\n return injector.invoke<PlatformRouter>(PlatformRouter, locals);\n }\n\n getRouter(): Router {\n return this.rawRouter;\n }\n\n use(...handlers: any[]) {\n // @ts-ignore\n this.getRouter().use(...this.mapHandlers(handlers));\n\n return this;\n }\n\n addRoute(options: Partial<PlatformRouteOptions>) {\n const {method, path, handlers, ...opts} = options;\n\n // @ts-ignore\n this.getRouter()[method](path, ...this.mapHandlers(handlers, {method, path, ...opts}));\n\n return this;\n }\n\n all(path: PathType, ...handlers: any[]) {\n return this.addRoute({method: \"all\", path, handlers, isFinal: true});\n }\n\n get(path: PathType, ...handlers: any[]) {\n return this.addRoute({method: \"get\", path, handlers, isFinal: true});\n }\n\n post(path: PathType, ...handlers: any[]) {\n return this.addRoute({method: \"post\", path, handlers, isFinal: true});\n }\n\n put(path: PathType, ...handlers: any[]) {\n return this.addRoute({method: \"put\", path, handlers, isFinal: true});\n }\n\n delete(path: PathType, ...handlers: any[]) {\n return this.addRoute({method: \"delete\", path, handlers, isFinal: true});\n }\n\n patch(path: PathType, ...handlers: any[]) {\n return this.addRoute({method: \"patch\", path, handlers, isFinal: true});\n }\n\n head(path: PathType, ...handlers: any[]) {\n return this.addRoute({method: \"head\", path, handlers, isFinal: true});\n }\n\n options(path: PathType, ...handlers: any[]) {\n return this.addRoute({method: \"options\", path, handlers, isFinal: true});\n }\n\n statics(path: string, options: PlatformStaticsOptions) {\n return this.use(path, this.adapter.statics(path, options));\n }\n\n multer(options: PlatformMulterSettings): PlatformMulter {\n return this.adapter.multipart(options);\n }\n\n protected mapHandlers(handlers: any[], options: PlatformRouteWithoutHandlers = {}): any[] {\n return handlers.reduce((list, handler, index) => {\n if (typeof handler === \"string\") {\n return list.concat(handler);\n }\n\n if (handler instanceof PlatformRouter) {\n return list.concat(handler.callback());\n }\n\n return list.concat(\n this.platformHandler.createHandler(handler, {\n ...options,\n isFinal: options.isFinal ? index === handlers.length - 1 : false\n })\n );\n }, []);\n }\n}\n"]}
|
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.PlatformTest = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
4
5
|
const di_1 = require("@tsed/di");
|
|
5
6
|
const PlatformBuilder_1 = require("../builder/PlatformBuilder");
|
|
6
7
|
const PlatformContext_1 = require("../domain/PlatformContext");
|
|
7
8
|
const createInjector_1 = require("../utils/createInjector");
|
|
8
9
|
const PlatformApplication_1 = require("./PlatformApplication");
|
|
9
10
|
const getConfiguration_1 = require("../utils/getConfiguration");
|
|
11
|
+
const accepts_1 = tslib_1.__importDefault(require("accepts"));
|
|
10
12
|
/**
|
|
11
13
|
* @platform
|
|
12
14
|
*/
|
|
@@ -20,7 +22,6 @@ class PlatformTest extends di_1.DITest {
|
|
|
20
22
|
*/
|
|
21
23
|
static createInjector(settings = {}) {
|
|
22
24
|
return (0, createInjector_1.createInjector)({
|
|
23
|
-
providers: [],
|
|
24
25
|
settings: di_1.DITest.configure({ httpPort: false, httpsPort: false, ...settings })
|
|
25
26
|
});
|
|
26
27
|
}
|
|
@@ -100,7 +101,7 @@ class PlatformTest extends di_1.DITest {
|
|
|
100
101
|
return this.headers[key.toLowerCase()];
|
|
101
102
|
},
|
|
102
103
|
accepts(mime) {
|
|
103
|
-
return
|
|
104
|
+
return (0, accepts_1.default)(this).types([].concat(mime));
|
|
104
105
|
},
|
|
105
106
|
...options
|
|
106
107
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"PlatformTest.js","sourceRoot":"","sources":["../../../src/services/PlatformTest.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"PlatformTest.js","sourceRoot":"","sources":["../../../src/services/PlatformTest.ts"],"names":[],"mappings":";;;;AAAA,iCAAgE;AAEhE,gEAA2D;AAC3D,+DAAkF;AAClF,4DAAuD;AACvD,+DAA0D;AAC1D,gEAA2D;AAE3D,8DAA8B;AAE9B;;GAEG;AACH,MAAa,YAAa,SAAQ,WAAM;IAGtC,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC,WAA0B,EAAE;QAC9C,WAAM,CAAC,QAAQ,GAAG,YAAY,CAAC,cAAc,CAAC,IAAA,mCAAgB,EAAC,QAAQ,CAAC,CAAC,CAAC;QAC1E,MAAM,WAAM,CAAC,eAAe,CAAC,QAAQ,CAAC,CAAC;IACzC,CAAC;IAED;;OAEG;IACH,MAAM,CAAC,cAAc,CAAC,WAAgB,EAAE;QACtC,OAAO,IAAA,+BAAc,EAAC;YACpB,QAAQ,EAAE,WAAM,CAAC,SAAS,CAAC,EAAC,QAAQ,EAAE,KAAK,EAAE,SAAS,EAAE,KAAK,EAAE,GAAG,QAAQ,EAAC,CAAC;SAC7E,CAAC,CAAC;IACL,CAAC;IAED;;;;;;;OAOG;IACH,MAAM,CAAC,SAAS,CAAC,GAAQ,EAAE,EAAC,MAAM,EAAE,GAAG,QAAQ,KAA0D,EAAE;QACzG,OAAO,KAAK,UAAU,MAAM;YAC1B,IAAI,QAAyB,CAAC;YAC9B,MAAM,OAAO,GAA0B,QAAQ,CAAC,QAAQ,IAAI,QAAQ,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,UAAU;gBACR,OAAO,IAAI,CAAC,OAAO,CAAC;YACtB,CAAC;YACD,GAAG,CAAC,GAAW,EAAE,KAAU;gBACzB,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,WAAW,EAAE,CAAC,GAAG,KAAK,CAAC;gBACxC,OAAO,IAAI,CAAC;YACd,CAAC;YACD,IAAI,CAAC,IAAS;gBACZ,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;YACnB,CAAC;YACD,IAAI,CAAC,IAAS;gBACZ,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,OAAO,EAAE,CAAA,MAAA,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,OAAO,0CAAE,OAAO,MAAI,MAAA,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,KAAK,0CAAE,OAAO,CAAA,IAAI,YAAY,CAAC,aAAa,EAAE;YAC7F,QAAQ,EAAE,CAAA,MAAA,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,QAAQ,0CAAE,QAAQ,MAAI,MAAA,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,KAAK,0CAAE,QAAQ,CAAA,IAAI,YAAY,CAAC,cAAc,EAAE;SACnG,CAAC;QAEF,OAAO,IAAI,iCAAe,CAAC;YACzB,EAAE,EAAE,IAAI;YACR,QAAQ,EAAE,WAAM,CAAC,QAAQ;YACzB,MAAM,EAAE,WAAM,CAAC,QAAQ,CAAC,MAAM;YAC9B,GAAG,EAAE,GAAG;YACR,GAAG,OAAO;YACV,KAAK;SACN,CAAC,CAAC;IACL,CAAC;CACF;AAvKD,oCAuKC","sourcesContent":["import {DITest, DITestOptions, InjectorService} from \"@tsed/di\";\nimport {Type} from \"@tsed/core\";\nimport {PlatformBuilder} from \"../builder/PlatformBuilder\";\nimport {PlatformContext, PlatformContextOptions} from \"../domain/PlatformContext\";\nimport {createInjector} from \"../utils/createInjector\";\nimport {PlatformApplication} from \"./PlatformApplication\";\nimport {getConfiguration} from \"../utils/getConfiguration\";\nimport {PlatformAdapter, PlatformBuilderSettings} from \"./PlatformAdapter\";\nimport accepts from \"accepts\";\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 instance = await PlatformBuilder.build(mod, settings).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 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 send(data: any) {\n this.data = data;\n },\n json(data: any) {\n this.data = data;\n },\n ...options\n };\n }\n\n static createRequestContext(options: Partial<PlatformContextOptions & any> = {}) {\n const event = {\n request: options?.request?.request || options?.event?.request || PlatformTest.createRequest(),\n response: options?.response?.response || options?.event?.response || PlatformTest.createResponse()\n };\n\n return new PlatformContext({\n id: \"id\",\n injector: DITest.injector,\n logger: DITest.injector.logger,\n url: \"/\",\n ...options,\n event\n });\n }\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Test1.js","sourceRoot":"","sources":["../../../../src/utils/__mock__/Test1.ts"],"names":[],"mappings":";;;AAAa,QAAA,MAAM,GAAG,OAAO,CAAC;AAE9B,MAAa,KAAK;CAAG;AAArB,sBAAqB","sourcesContent":["export const value1 = \"value\";\n\nexport class Test1 {}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Test2.js","sourceRoot":"","sources":["../../../../src/utils/__mock__/Test2.ts"],"names":[],"mappings":";;;AAAa,QAAA,MAAM,GAAG,OAAO,CAAC;AAE9B,MAAa,KAAK;CAAG;AAArB,sBAAqB","sourcesContent":["export const value2 = \"value\";\n\nexport class Test2 {}\n"]}
|
|
@@ -4,8 +4,8 @@ exports.createContext = void 0;
|
|
|
4
4
|
const PlatformContext_1 = require("../domain/PlatformContext");
|
|
5
5
|
const PlatformRequest_1 = require("../services/PlatformRequest");
|
|
6
6
|
const PlatformResponse_1 = require("../services/PlatformResponse");
|
|
7
|
-
const
|
|
8
|
-
const defaultReqIdBuilder = (req) => req.get("x-request-id") ||
|
|
7
|
+
const uuid_1 = require("uuid");
|
|
8
|
+
const defaultReqIdBuilder = (req) => req.get("x-request-id") || (0, uuid_1.v4)().replace(/-/gi, "");
|
|
9
9
|
/**
|
|
10
10
|
* Create the TsED context to wrap request, response, injector, etc...
|
|
11
11
|
* @param injector
|