@tsed/common 7.0.0-beta.9 → 7.0.0-rc.1
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 +9 -0
- package/lib/cjs/builder/PlatformBuilder.js +86 -68
- package/lib/cjs/builder/PlatformBuilder.js.map +1 -1
- package/lib/cjs/config/interfaces/PlatformLoggerSettings.js.map +1 -1
- package/lib/cjs/config/services/PlatformConfiguration.js +0 -64
- package/lib/cjs/config/services/PlatformConfiguration.js.map +1 -1
- package/lib/cjs/decorators/params/error.js +1 -1
- package/lib/cjs/decorators/params/error.js.map +1 -1
- package/lib/cjs/domain/AnyToPromiseWithCtx.js +4 -5
- package/lib/cjs/domain/AnyToPromiseWithCtx.js.map +1 -1
- package/lib/cjs/domain/PlatformContext.js +46 -38
- package/lib/cjs/domain/PlatformContext.js.map +1 -1
- package/lib/cjs/domain/PlatformRouteDetails.js +0 -21
- package/lib/cjs/domain/PlatformRouteDetails.js.map +1 -1
- package/lib/cjs/exports.js +1 -0
- package/lib/cjs/exports.js.map +1 -1
- package/lib/cjs/index.js +1 -9
- package/lib/cjs/index.js.map +1 -1
- package/lib/cjs/interfaces/PlatformRouteOptions.js.map +1 -1
- package/lib/cjs/interfaces/Route.js.map +1 -1
- package/lib/cjs/middlewares/PlatformAcceptMimesMiddleware.js +2 -1
- package/lib/cjs/middlewares/PlatformAcceptMimesMiddleware.js.map +1 -1
- package/lib/cjs/middlewares/PlatformMulterMiddleware.js +2 -0
- package/lib/cjs/middlewares/PlatformMulterMiddleware.js.map +1 -1
- package/lib/cjs/services/FakeAdapter.js +6 -12
- package/lib/cjs/services/FakeAdapter.js.map +1 -1
- package/lib/cjs/services/Platform.js +30 -86
- package/lib/cjs/services/Platform.js.map +1 -1
- package/lib/cjs/services/PlatformAdapter.js +10 -0
- package/lib/cjs/services/PlatformAdapter.js.map +1 -1
- package/lib/cjs/services/PlatformApplication.js +23 -9
- package/lib/cjs/services/PlatformApplication.js.map +1 -1
- package/lib/cjs/services/PlatformHandler.js +112 -171
- package/lib/cjs/services/PlatformHandler.js.map +1 -1
- package/lib/cjs/services/PlatformMiddlewaresChain.js +5 -19
- package/lib/cjs/services/PlatformMiddlewaresChain.js.map +1 -1
- package/lib/cjs/services/PlatformRequest.js +16 -12
- package/lib/cjs/services/PlatformRequest.js.map +1 -1
- package/lib/cjs/services/PlatformResponse.js +19 -22
- package/lib/cjs/services/PlatformResponse.js.map +1 -1
- package/lib/cjs/services/PlatformTest.js +29 -9
- package/lib/cjs/services/PlatformTest.js.map +1 -1
- package/lib/cjs/utils/createContext.js +32 -21
- package/lib/cjs/utils/createContext.js.map +1 -1
- package/lib/cjs/utils/createHttpServer.js +3 -3
- package/lib/cjs/utils/createHttpServer.js.map +1 -1
- package/lib/cjs/utils/createHttpsServer.js +3 -3
- package/lib/cjs/utils/createHttpsServer.js.map +1 -1
- package/lib/cjs/utils/createInjector.js +6 -8
- package/lib/cjs/utils/createInjector.js.map +1 -1
- package/lib/cjs/utils/ensureContext.js +16 -0
- package/lib/cjs/utils/ensureContext.js.map +1 -0
- package/lib/cjs/utils/getConfiguration.js +6 -12
- package/lib/cjs/utils/getConfiguration.js.map +1 -1
- package/lib/cjs/utils/printRoutes.js +10 -8
- package/lib/cjs/utils/printRoutes.js.map +1 -1
- package/lib/cjs/utils/setResponseHeaders.js +8 -14
- package/lib/cjs/utils/setResponseHeaders.js.map +1 -1
- package/lib/esm/builder/PlatformBuilder.js +63 -68
- package/lib/esm/builder/PlatformBuilder.js.map +1 -1
- package/lib/esm/config/interfaces/PlatformLoggerSettings.js.map +1 -1
- package/lib/esm/config/services/PlatformConfiguration.js +0 -64
- package/lib/esm/config/services/PlatformConfiguration.js.map +1 -1
- package/lib/esm/decorators/params/error.js +1 -1
- package/lib/esm/decorators/params/error.js.map +1 -1
- package/lib/esm/domain/AnyToPromiseWithCtx.js +4 -5
- package/lib/esm/domain/AnyToPromiseWithCtx.js.map +1 -1
- package/lib/esm/domain/PlatformContext.js +46 -38
- package/lib/esm/domain/PlatformContext.js.map +1 -1
- package/lib/esm/domain/PlatformRouteDetails.js +1 -19
- package/lib/esm/domain/PlatformRouteDetails.js.map +1 -1
- package/lib/esm/exports.js +1 -0
- package/lib/esm/exports.js.map +1 -1
- package/lib/esm/index.js +1 -9
- package/lib/esm/index.js.map +1 -1
- package/lib/esm/interfaces/PlatformRouteOptions.js.map +1 -1
- package/lib/esm/interfaces/Route.js.map +1 -1
- package/lib/esm/middlewares/PlatformAcceptMimesMiddleware.js +2 -1
- package/lib/esm/middlewares/PlatformAcceptMimesMiddleware.js.map +1 -1
- package/lib/esm/middlewares/PlatformMulterMiddleware.js +2 -0
- package/lib/esm/middlewares/PlatformMulterMiddleware.js.map +1 -1
- package/lib/esm/services/FakeAdapter.js +6 -12
- package/lib/esm/services/FakeAdapter.js.map +1 -1
- package/lib/esm/services/Platform.js +31 -87
- package/lib/esm/services/Platform.js.map +1 -1
- package/lib/esm/services/PlatformAdapter.js +10 -0
- package/lib/esm/services/PlatformAdapter.js.map +1 -1
- package/lib/esm/services/PlatformApplication.js +23 -9
- package/lib/esm/services/PlatformApplication.js.map +1 -1
- package/lib/esm/services/PlatformHandler.js +114 -173
- package/lib/esm/services/PlatformHandler.js.map +1 -1
- package/lib/esm/services/PlatformMiddlewaresChain.js +5 -19
- package/lib/esm/services/PlatformMiddlewaresChain.js.map +1 -1
- package/lib/esm/services/PlatformRequest.js +16 -12
- package/lib/esm/services/PlatformRequest.js.map +1 -1
- package/lib/esm/services/PlatformResponse.js +19 -22
- package/lib/esm/services/PlatformResponse.js.map +1 -1
- package/lib/esm/services/PlatformTest.js +29 -9
- package/lib/esm/services/PlatformTest.js.map +1 -1
- package/lib/esm/utils/createContext.js +30 -20
- package/lib/esm/utils/createContext.js.map +1 -1
- package/lib/esm/utils/createHttpServer.js +3 -3
- package/lib/esm/utils/createHttpServer.js.map +1 -1
- package/lib/esm/utils/createHttpsServer.js +3 -3
- package/lib/esm/utils/createHttpsServer.js.map +1 -1
- package/lib/esm/utils/createInjector.js +7 -9
- package/lib/esm/utils/createInjector.js.map +1 -1
- package/lib/esm/utils/ensureContext.js +12 -0
- package/lib/esm/utils/ensureContext.js.map +1 -0
- package/lib/esm/utils/getConfiguration.js +8 -14
- package/lib/esm/utils/getConfiguration.js.map +1 -1
- package/lib/esm/utils/printRoutes.js +10 -8
- package/lib/esm/utils/printRoutes.js.map +1 -1
- package/lib/esm/utils/setResponseHeaders.js +8 -14
- package/lib/esm/utils/setResponseHeaders.js.map +1 -1
- package/lib/types/builder/PlatformBuilder.d.ts +17 -16
- package/lib/types/config/interfaces/PlatformLoggerSettings.d.ts +2 -43
- package/lib/types/config/services/PlatformConfiguration.d.ts +6 -22
- package/lib/types/domain/AnyToPromiseWithCtx.d.ts +1 -9
- package/lib/types/domain/PlatformContext.d.ts +32 -18
- package/lib/types/domain/PlatformRouteDetails.d.ts +4 -19
- package/lib/types/exports.d.ts +1 -0
- package/lib/types/index.d.ts +1 -9
- package/lib/types/interfaces/PlatformRouteOptions.d.ts +0 -1
- package/lib/types/interfaces/Route.d.ts +2 -3
- package/lib/types/middlewares/PlatformMulterMiddleware.d.ts +1 -1
- package/lib/types/services/FakeAdapter.d.ts +7 -6
- package/lib/types/services/Platform.d.ts +7 -28
- package/lib/types/services/PlatformAdapter.d.ts +22 -11
- package/lib/types/services/PlatformApplication.d.ts +14 -6
- package/lib/types/services/PlatformHandler.d.ts +21 -59
- package/lib/types/services/PlatformMiddlewaresChain.d.ts +1 -2
- package/lib/types/services/PlatformRequest.d.ts +8 -10
- package/lib/types/services/PlatformResponse.d.ts +11 -15
- package/lib/types/services/PlatformTest.d.ts +4 -3
- package/lib/types/utils/createContext.d.ts +4 -1
- package/lib/types/utils/createHttpsServer.d.ts +1 -0
- package/lib/types/utils/createInjector.d.ts +2 -2
- package/lib/types/utils/ensureContext.d.ts +2 -0
- package/lib/types/utils/listenServer.d.ts +2 -0
- package/package.json +28 -23
- package/readme.md +2 -1
- package/lib/cjs/builder/PlatformControllerBuilder.js +0 -82
- package/lib/cjs/builder/PlatformControllerBuilder.js.map +0 -1
- package/lib/cjs/config/interfaces/EndpointDirectoriesSettings.js +0 -3
- package/lib/cjs/config/interfaces/EndpointDirectoriesSettings.js.map +0 -1
- package/lib/cjs/domain/ControllerProvider.js +0 -51
- package/lib/cjs/domain/ControllerProvider.js.map +0 -1
- package/lib/cjs/domain/HandlerMetadata.js +0 -57
- package/lib/cjs/domain/HandlerMetadata.js.map +0 -1
- package/lib/cjs/middlewares/__mock__/FakeCtrl.js +0 -12
- package/lib/cjs/middlewares/__mock__/FakeCtrl.js.map +0 -1
- package/lib/cjs/middlewares/bindEndpointMiddleware.js +0 -13
- package/lib/cjs/middlewares/bindEndpointMiddleware.js.map +0 -1
- package/lib/cjs/services/PlatformRouter.js +0 -108
- package/lib/cjs/services/PlatformRouter.js.map +0 -1
- package/lib/cjs/utils/__mock__/Test1.js +0 -8
- package/lib/cjs/utils/__mock__/Test1.js.map +0 -1
- package/lib/cjs/utils/__mock__/Test2.js +0 -8
- package/lib/cjs/utils/__mock__/Test2.js.map +0 -1
- package/lib/cjs/utils/createHandlerMetadata.js +0 -49
- package/lib/cjs/utils/createHandlerMetadata.js.map +0 -1
- package/lib/cjs/utils/importProviders.js +0 -29
- package/lib/cjs/utils/importProviders.js.map +0 -1
- package/lib/cjs/utils/useCtxHandler.js +0 -15
- package/lib/cjs/utils/useCtxHandler.js.map +0 -1
- package/lib/esm/builder/PlatformControllerBuilder.js +0 -76
- package/lib/esm/builder/PlatformControllerBuilder.js.map +0 -1
- package/lib/esm/config/interfaces/EndpointDirectoriesSettings.js +0 -2
- package/lib/esm/config/interfaces/EndpointDirectoriesSettings.js.map +0 -1
- package/lib/esm/domain/ControllerProvider.js +0 -47
- package/lib/esm/domain/ControllerProvider.js.map +0 -1
- package/lib/esm/domain/HandlerMetadata.js +0 -53
- package/lib/esm/domain/HandlerMetadata.js.map +0 -1
- package/lib/esm/middlewares/__mock__/FakeCtrl.js +0 -9
- package/lib/esm/middlewares/__mock__/FakeCtrl.js.map +0 -1
- package/lib/esm/middlewares/bindEndpointMiddleware.js +0 -9
- package/lib/esm/middlewares/bindEndpointMiddleware.js.map +0 -1
- package/lib/esm/services/PlatformRouter.js +0 -105
- package/lib/esm/services/PlatformRouter.js.map +0 -1
- package/lib/esm/utils/__mock__/Test1.js +0 -4
- package/lib/esm/utils/__mock__/Test1.js.map +0 -1
- package/lib/esm/utils/__mock__/Test2.js +0 -4
- package/lib/esm/utils/__mock__/Test2.js.map +0 -1
- package/lib/esm/utils/createHandlerMetadata.js +0 -45
- package/lib/esm/utils/createHandlerMetadata.js.map +0 -1
- package/lib/esm/utils/importProviders.js +0 -25
- package/lib/esm/utils/importProviders.js.map +0 -1
- package/lib/esm/utils/useCtxHandler.js +0 -11
- package/lib/esm/utils/useCtxHandler.js.map +0 -1
- package/lib/types/builder/PlatformControllerBuilder.d.ts +0 -18
- package/lib/types/config/interfaces/EndpointDirectoriesSettings.d.ts +0 -3
- package/lib/types/domain/ControllerProvider.d.ts +0 -24
- package/lib/types/domain/HandlerMetadata.d.ts +0 -32
- package/lib/types/middlewares/__mock__/FakeCtrl.d.ts +0 -2
- package/lib/types/middlewares/bindEndpointMiddleware.d.ts +0 -6
- package/lib/types/services/PlatformRouter.d.ts +0 -51
- package/lib/types/utils/__mock__/Test1.d.ts +0 -3
- package/lib/types/utils/__mock__/Test2.d.ts +0 -3
- package/lib/types/utils/createHandlerMetadata.d.ts +0 -8
- package/lib/types/utils/importProviders.d.ts +0 -10
- package/lib/types/utils/useCtxHandler.d.ts +0 -13
|
@@ -5,9 +5,18 @@ import { Injectable, ProviderScope, Scope } from "@tsed/di";
|
|
|
5
5
|
* @platform
|
|
6
6
|
*/
|
|
7
7
|
let PlatformRequest = class PlatformRequest {
|
|
8
|
-
|
|
8
|
+
$ctx;
|
|
9
|
+
constructor($ctx) {
|
|
9
10
|
this.$ctx = $ctx;
|
|
10
|
-
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* The current @@PlatformResponse@@.
|
|
14
|
+
*/
|
|
15
|
+
get response() {
|
|
16
|
+
return this.$ctx.response;
|
|
17
|
+
}
|
|
18
|
+
get raw() {
|
|
19
|
+
return this.$ctx.event.request;
|
|
11
20
|
}
|
|
12
21
|
get secure() {
|
|
13
22
|
return this.raw.secure;
|
|
@@ -74,6 +83,9 @@ let PlatformRequest = class PlatformRequest {
|
|
|
74
83
|
get files() {
|
|
75
84
|
return this.raw.files;
|
|
76
85
|
}
|
|
86
|
+
get route() {
|
|
87
|
+
return this.$ctx.handlerMetadata?.path;
|
|
88
|
+
}
|
|
77
89
|
/**
|
|
78
90
|
* Return the original request framework instance
|
|
79
91
|
*/
|
|
@@ -105,14 +117,6 @@ let PlatformRequest = class PlatformRequest {
|
|
|
105
117
|
isAborted() {
|
|
106
118
|
return this.raw.aborted;
|
|
107
119
|
}
|
|
108
|
-
destroy() {
|
|
109
|
-
// @ts-ignore
|
|
110
|
-
delete this.raw;
|
|
111
|
-
// @ts-ignore
|
|
112
|
-
delete this.response;
|
|
113
|
-
// @ts-ignore
|
|
114
|
-
delete this.$ctx;
|
|
115
|
-
}
|
|
116
120
|
/**
|
|
117
121
|
* Return the Framework response object (express, koa, etc...)
|
|
118
122
|
*/
|
|
@@ -123,13 +127,13 @@ let PlatformRequest = class PlatformRequest {
|
|
|
123
127
|
* Return the Node.js response object
|
|
124
128
|
*/
|
|
125
129
|
getReq() {
|
|
126
|
-
return this.
|
|
130
|
+
return this.$ctx.event.request;
|
|
127
131
|
}
|
|
128
132
|
};
|
|
129
133
|
PlatformRequest = __decorate([
|
|
130
134
|
Injectable(),
|
|
131
135
|
Scope(ProviderScope.INSTANCE),
|
|
132
|
-
__metadata("design:paramtypes", [
|
|
136
|
+
__metadata("design:paramtypes", [Function])
|
|
133
137
|
], PlatformRequest);
|
|
134
138
|
export { PlatformRequest };
|
|
135
139
|
//# sourceMappingURL=PlatformRequest.js.map
|
|
@@ -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;
|
|
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,OAAO,IAAI,CAAC,GAAG,CAAC,MAAM,CAAC;IACzB,CAAC;IAED;;;OAGG;IACH,IAAI,IAAI;QACN,OAAO,IAAI,CAAC,GA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;QACJ,OAAO,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC;IACjC,CAAC;CACF,CAAA;AA5JY,eAAe;IAF3B,UAAU,EAAE;IACZ,KAAK,CAAC,aAAa,CAAC,QAAQ,CAAC;;GACjB,eAAe,CA4J3B;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,16 +1,24 @@
|
|
|
1
|
-
var PlatformResponse_1;
|
|
2
1
|
import { __decorate, __metadata } from "tslib";
|
|
3
2
|
import { isBoolean, isNumber, isStream, isString } from "@tsed/core";
|
|
4
3
|
import { Injectable, ProviderScope, Scope } from "@tsed/di";
|
|
5
|
-
import onFinished from "on-finished";
|
|
6
4
|
/**
|
|
7
5
|
* Platform Response abstraction layer.
|
|
8
6
|
* @platform
|
|
9
7
|
*/
|
|
10
|
-
let PlatformResponse =
|
|
11
|
-
|
|
8
|
+
let PlatformResponse = class PlatformResponse {
|
|
9
|
+
$ctx;
|
|
10
|
+
data;
|
|
11
|
+
constructor($ctx) {
|
|
12
12
|
this.$ctx = $ctx;
|
|
13
|
-
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* The current @@PlatformRequest@@.
|
|
16
|
+
*/
|
|
17
|
+
get request() {
|
|
18
|
+
return this.$ctx.request;
|
|
19
|
+
}
|
|
20
|
+
get raw() {
|
|
21
|
+
return this.$ctx.event.response;
|
|
14
22
|
}
|
|
15
23
|
/**
|
|
16
24
|
* Get the current statusCode
|
|
@@ -38,9 +46,6 @@ let PlatformResponse = PlatformResponse_1 = class PlatformResponse {
|
|
|
38
46
|
get res() {
|
|
39
47
|
return this.getRes();
|
|
40
48
|
}
|
|
41
|
-
static onFinished(res, cb) {
|
|
42
|
-
onFinished(res, cb);
|
|
43
|
-
}
|
|
44
49
|
/**
|
|
45
50
|
* Returns the HTTP response header specified by field. The match is case-insensitive.
|
|
46
51
|
*
|
|
@@ -199,7 +204,7 @@ let PlatformResponse = PlatformResponse_1 = class PlatformResponse {
|
|
|
199
204
|
* Send any data to your consumer.
|
|
200
205
|
*
|
|
201
206
|
* This method accept a ReadableStream, a plain object, boolean, string, number, null and undefined data.
|
|
202
|
-
* It
|
|
207
|
+
* It chooses the better way to send the data.
|
|
203
208
|
*
|
|
204
209
|
* @param data
|
|
205
210
|
*/
|
|
@@ -236,33 +241,25 @@ let PlatformResponse = PlatformResponse_1 = class PlatformResponse {
|
|
|
236
241
|
* @param cb
|
|
237
242
|
*/
|
|
238
243
|
onEnd(cb) {
|
|
239
|
-
|
|
244
|
+
const res = this.getRes();
|
|
245
|
+
res.on("finish", cb);
|
|
240
246
|
return this;
|
|
241
247
|
}
|
|
242
248
|
isDone() {
|
|
243
|
-
if (
|
|
249
|
+
if (this.$ctx.isFinished()) {
|
|
244
250
|
return true;
|
|
245
251
|
}
|
|
246
252
|
const res = this.getRes();
|
|
247
253
|
return Boolean(this.isHeadersSent() || res.writableEnded || res.writableFinished);
|
|
248
254
|
}
|
|
249
|
-
destroy() {
|
|
250
|
-
// @ts-ignore
|
|
251
|
-
delete this.raw;
|
|
252
|
-
delete this.data;
|
|
253
|
-
// @ts-ignore
|
|
254
|
-
delete this.request;
|
|
255
|
-
// @ts-ignore
|
|
256
|
-
delete this.$ctx;
|
|
257
|
-
}
|
|
258
255
|
isHeadersSent() {
|
|
259
256
|
return this.getRes().headersSent;
|
|
260
257
|
}
|
|
261
258
|
};
|
|
262
|
-
PlatformResponse =
|
|
259
|
+
PlatformResponse = __decorate([
|
|
263
260
|
Injectable(),
|
|
264
261
|
Scope(ProviderScope.INSTANCE),
|
|
265
|
-
__metadata("design:paramtypes", [
|
|
262
|
+
__metadata("design:paramtypes", [Function])
|
|
266
263
|
], PlatformResponse);
|
|
267
264
|
export { PlatformResponse };
|
|
268
265
|
//# sourceMappingURL=PlatformResponse.js.map
|
|
@@ -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;AAgBrC;;;GAGG;AAGH,IAAa,gBAAgB,wBAA7B,MAAa,gBAAgB;IAU3B,YAAY,EAAC,QAAQ,EAAgB,EAAY,IAAqB;QAArB,SAAI,GAAJ,IAAI,CAAiB;QACpE,IAAI,CA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ACjB,gBAAgB,CAsT5B;SAtTY,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 {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"]}
|
|
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,GAAJ,IAAI,CAAiB;IAAG,CAAC;IAE9C;;OAEG;IACH,IAAI,OAAO;QACT,OAAO,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC;IAC3B,CAAC;IAED,IAAI,GAAG;QACL,OAAO,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,QAAe,CAAC;IACzC,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;;;;;;;;OAQG;IACH,GAAG,CAAC,IAAY;QACd,OAAO,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;IAC5B,CAAC;IAED,UAAU;QACR,OAAO,IAAI,CAAC,GAAG,CAAC,UAAU,EAAE,CAAC;IAC/B,CAAC;IAED;;OAEG;IACH,WAAW;QACT,OAAO,IAAI,CAAC,GAAU,CAAC;IACzB,CAAC;IAED;;OA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C,MAAM,EAAE,CAAC;QAE1B,OAAO,OAAO,CAAC,IAAI,CAAC,aAAa,EAAE,IAAI,GAAG,CAAC,aAAa,IAAI,GAAG,CAAC,gBAAgB,CAAC,CAAC;IACpF,CAAC;IAED,aAAa;QACX,OAAO,IAAI,CAAC,MAAM,EAAE,CAAC,WAAW,CAAC;IACnC,CAAC;CACF,CAAA;AA5SY,gBAAgB;IAF5B,UAAU,EAAE;IACZ,KAAK,CAAC,aAAa,CAAC,QAAQ,CAAC;;GACjB,gBAAgB,CA4S5B;SA5SY,gBAAgB","sourcesContent":["import {isBoolean, isNumber, isStream, isString} from \"@tsed/core\";\nimport {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,14 +1,15 @@
|
|
|
1
1
|
import { DITest } from "@tsed/di";
|
|
2
|
+
import accepts from "accepts";
|
|
2
3
|
import { PlatformBuilder } from "../builder/PlatformBuilder.js";
|
|
3
4
|
import { PlatformContext } from "../domain/PlatformContext.js";
|
|
4
5
|
import { createInjector } from "../utils/createInjector.js";
|
|
5
|
-
import { PlatformApplication } from "./PlatformApplication.js";
|
|
6
6
|
import { getConfiguration } from "../utils/getConfiguration.js";
|
|
7
|
-
import
|
|
7
|
+
import { PlatformApplication } from "./PlatformApplication.js";
|
|
8
8
|
/**
|
|
9
9
|
* @platform
|
|
10
10
|
*/
|
|
11
11
|
export class PlatformTest extends DITest {
|
|
12
|
+
static adapter;
|
|
12
13
|
static async create(settings = {}) {
|
|
13
14
|
DITest.injector = PlatformTest.createInjector(getConfiguration(settings));
|
|
14
15
|
await DITest.createContainer(settings);
|
|
@@ -40,7 +41,13 @@ export class PlatformTest extends DITest {
|
|
|
40
41
|
// @ts-ignore
|
|
41
42
|
settings = DITest.configure(settings);
|
|
42
43
|
settings.adapter = adapter;
|
|
43
|
-
|
|
44
|
+
const configuration = getConfiguration(settings, mod);
|
|
45
|
+
const disableComponentsScan = configuration.disableComponentsScan || !!process.env.WEBPACK;
|
|
46
|
+
if (!disableComponentsScan) {
|
|
47
|
+
const { importProviders } = await import("@tsed/components-scan");
|
|
48
|
+
await importProviders(configuration);
|
|
49
|
+
}
|
|
50
|
+
instance = await PlatformBuilder.build(mod, configuration).bootstrap();
|
|
44
51
|
await instance.listen(!!listen);
|
|
45
52
|
// used by inject method
|
|
46
53
|
DITest.injector = instance.injector;
|
|
@@ -87,12 +94,14 @@ export class PlatformTest extends DITest {
|
|
|
87
94
|
* ```
|
|
88
95
|
*/
|
|
89
96
|
static callback() {
|
|
90
|
-
|
|
91
|
-
return (_a = DITest.injector.get(PlatformApplication)) === null || _a === void 0 ? void 0 : _a.callback();
|
|
97
|
+
return DITest.injector.get(PlatformApplication)?.callback();
|
|
92
98
|
}
|
|
93
99
|
static createRequest(options = {}) {
|
|
94
100
|
return {
|
|
95
101
|
headers: {},
|
|
102
|
+
method: "GET",
|
|
103
|
+
url: "/",
|
|
104
|
+
query: {},
|
|
96
105
|
get(key) {
|
|
97
106
|
return this.headers[key.toLowerCase()];
|
|
98
107
|
},
|
|
@@ -134,22 +143,29 @@ export class PlatformTest extends DITest {
|
|
|
134
143
|
this.headers[key.toLowerCase()] = value;
|
|
135
144
|
return this;
|
|
136
145
|
},
|
|
146
|
+
setHeader(key, value) {
|
|
147
|
+
this.headers[key.toLowerCase()] = value;
|
|
148
|
+
return this;
|
|
149
|
+
},
|
|
137
150
|
send(data) {
|
|
138
151
|
this.data = data;
|
|
139
152
|
},
|
|
140
153
|
json(data) {
|
|
141
154
|
this.data = data;
|
|
142
155
|
},
|
|
156
|
+
end(data) {
|
|
157
|
+
this.data = data;
|
|
158
|
+
},
|
|
143
159
|
...options
|
|
144
160
|
};
|
|
145
161
|
}
|
|
146
162
|
static createRequestContext(options = {}) {
|
|
147
|
-
var _a, _b, _c, _d;
|
|
148
163
|
const event = {
|
|
149
|
-
|
|
150
|
-
|
|
164
|
+
...options.event,
|
|
165
|
+
request: options?.request?.request || options?.event?.request || PlatformTest.createRequest(),
|
|
166
|
+
response: options?.response?.response || options?.event?.response || PlatformTest.createResponse()
|
|
151
167
|
};
|
|
152
|
-
|
|
168
|
+
const $ctx = new PlatformContext({
|
|
153
169
|
id: "id",
|
|
154
170
|
injector: DITest.injector,
|
|
155
171
|
logger: DITest.injector.logger,
|
|
@@ -157,6 +173,10 @@ export class PlatformTest extends DITest {
|
|
|
157
173
|
...options,
|
|
158
174
|
event
|
|
159
175
|
});
|
|
176
|
+
if (options.endpoint) {
|
|
177
|
+
$ctx.endpoint = options.endpoint;
|
|
178
|
+
}
|
|
179
|
+
return $ctx;
|
|
160
180
|
}
|
|
161
181
|
}
|
|
162
182
|
//# sourceMappingURL=PlatformTest.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"PlatformTest.js","sourceRoot":"","sources":["../../../src/services/PlatformTest.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,MAAM,EAAiC,MAAM,UAAU,CAAC;AAEhE,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,mBAAmB,EAAC,MAAM,uBAAuB,CAAC;AAC1D,OAAO,EAAC,gBAAgB,EAAC,MAAM,2BAA2B,CAAC;AAE3D,OAAO,OAAO,MAAM,SAAS,CAAC;AAG9B;;GAEG;AACH,MAAM,OAAO,YAAa,SAAQ,MAAM;IAGtC,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;;OAEG;IACH,MAAM,CAAC,cAAc,CAAC,WAAgB,EAAE;QACtC,OAAO,cAAc,CAAC;YACpB,QAAQ,EAAE,MAAM,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,GAA0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CAAC,CAAC;YACzC,CAAC;YACD,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,eAAe,CAAC;YACzB,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;IACL,CAAC;CACF","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\";\nimport {FakeAdapter} from \"./FakeAdapter\";\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"]}
|
|
1
|
+
{"version":3,"file":"PlatformTest.js","sourceRoot":"","sources":["../../../src/services/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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"]}
|
|
@@ -1,34 +1,44 @@
|
|
|
1
|
+
import { v4 } from "uuid";
|
|
1
2
|
import { PlatformContext } from "../domain/PlatformContext.js";
|
|
2
3
|
import { PlatformRequest } from "../services/PlatformRequest.js";
|
|
3
4
|
import { PlatformResponse } from "../services/PlatformResponse.js";
|
|
4
|
-
|
|
5
|
-
|
|
5
|
+
function defaultReqIdBuilder(req) {
|
|
6
|
+
return req.get("x-request-id") || v4().split("-").join("");
|
|
7
|
+
}
|
|
8
|
+
function mapIgnoreUrlPatterns(ignoreUrlPatterns) {
|
|
9
|
+
return ignoreUrlPatterns.map((pattern) => (typeof pattern === "string" ? new RegExp(pattern, "gi") : pattern));
|
|
10
|
+
}
|
|
11
|
+
export function buildIgnoreLog(ignoreUrlPatterns) {
|
|
12
|
+
if (ignoreUrlPatterns) {
|
|
13
|
+
ignoreUrlPatterns = mapIgnoreUrlPatterns(ignoreUrlPatterns);
|
|
14
|
+
return (ignore, data, url) => ignoreUrlPatterns?.find((reg) => !!url.match(reg));
|
|
15
|
+
}
|
|
16
|
+
}
|
|
6
17
|
/**
|
|
7
18
|
* Create the TsED context to wrap request, response, injector, etc...
|
|
8
19
|
* @param injector
|
|
9
20
|
* @ignore
|
|
10
21
|
*/
|
|
11
22
|
export function createContext(injector) {
|
|
12
|
-
|
|
13
|
-
const
|
|
14
|
-
const
|
|
15
|
-
const
|
|
16
|
-
|
|
17
|
-
|
|
23
|
+
const ResponseKlass = injector.getProvider(PlatformResponse)?.useClass;
|
|
24
|
+
const RequestKlass = injector.getProvider(PlatformRequest)?.useClass;
|
|
25
|
+
const { reqIdBuilder = defaultReqIdBuilder, ...loggerOptions } = injector.settings.logger;
|
|
26
|
+
const opts = {
|
|
27
|
+
...loggerOptions,
|
|
28
|
+
logger: injector.logger,
|
|
29
|
+
injector,
|
|
30
|
+
ResponseKlass,
|
|
31
|
+
RequestKlass
|
|
32
|
+
};
|
|
33
|
+
const ignoreLog = buildIgnoreLog(loggerOptions.ignoreUrlPatterns);
|
|
34
|
+
return async function invokeContext(event) {
|
|
35
|
+
const $ctx = new PlatformContext({
|
|
36
|
+
...opts,
|
|
18
37
|
event,
|
|
19
|
-
id: reqIdBuilder(event.request)
|
|
20
|
-
logger: injector.logger,
|
|
21
|
-
...loggerOptions,
|
|
22
|
-
injector,
|
|
23
|
-
ResponseKlass,
|
|
24
|
-
RequestKlass
|
|
25
|
-
});
|
|
26
|
-
ctx.response.onEnd(async () => {
|
|
27
|
-
await ctx.emit("$onResponse", ctx);
|
|
28
|
-
await ctx.destroy();
|
|
38
|
+
id: reqIdBuilder(event.request)
|
|
29
39
|
});
|
|
30
|
-
|
|
31
|
-
return ctx;
|
|
40
|
+
ignoreLog && $ctx.logger.alterIgnoreLog((ignore, data) => ignoreLog(ignore, data, $ctx.url));
|
|
41
|
+
return $ctx;
|
|
32
42
|
};
|
|
33
43
|
}
|
|
34
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,2BAA2B,CAAC;
|
|
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,EAAE,IAAI,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC;AAClI,CAAC;AAED,MAAM,UAAU,cAAc,CAAC,iBAAoC;IACjE,IAAI,iBAAiB,EAAE;QACrB,iBAAiB,GAAG,oBAAoB,CAAC,iBAAiB,CAAC,CAAC;QAC5D,OAAO,CAAC,MAAe,EAAE,IAAS,EAAE,GAAW,EAAE,EAAE,CAAC,iBAAiB,EAAE,IAAI,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,CAAC,CAAC,GAAG,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC;KACxG;AACH,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,aAAa,CAAC,QAAyB;IACrD,MAAM,aAAa,GAAG,QAAQ,CAAC,WAAW,CAAC,gBAAgB,CAAC,EAAE,QAAQ,CAAC;IACvE,MAAM,YAAY,GAAG,QAAQ,CAAC,WAAW,CAAC,eAAe,CAAC,EAAE,QAAQ,CAAC;IACrE,MAAM,EAAC,YAAY,GAAG,mBAAmB,EAAE,GAAG,aAAa,EAAC,GAAG,QAAQ,CAAC,QAAQ,CAAC,MAAM,CAAC;IAExF,MAAM,IAAI,GAAG;QACX,GAAG,aAAa;QAChB,MAAM,EAAE,QAAQ,CAAC,MAAM;QACvB,QAAQ;QACR,aAAa;QACb,YAAY;KACb,CAAC;IAEF,MAAM,SAAS,GAAG,cAAc,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"]}
|
|
@@ -4,8 +4,8 @@ import Http from "http";
|
|
|
4
4
|
import { listenServer } from "./listenServer.js";
|
|
5
5
|
export function createHttpServer(injector, requestListener) {
|
|
6
6
|
const { settings } = injector;
|
|
7
|
-
const httpPort = settings.
|
|
8
|
-
const httpOptions = settings.
|
|
7
|
+
const httpPort = settings.get("httpPort");
|
|
8
|
+
const httpOptions = settings.get("httpOptions");
|
|
9
9
|
const server = httpPort !== false ? Http.createServer(httpOptions, requestListener) : null;
|
|
10
10
|
injector.addProvider(Http.Server, {
|
|
11
11
|
scope: ProviderScope.SINGLETON,
|
|
@@ -16,7 +16,7 @@ export function createHttpServer(injector, requestListener) {
|
|
|
16
16
|
const hostInfo = getHostInfoFromPort("http", httpPort);
|
|
17
17
|
return async () => {
|
|
18
18
|
const resolvedHostInfo = await listenServer(injector, server, hostInfo);
|
|
19
|
-
settings.
|
|
19
|
+
settings.set("httpPort", `${resolvedHostInfo.address}:${resolvedHostInfo.port}`);
|
|
20
20
|
return server;
|
|
21
21
|
};
|
|
22
22
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createHttpServer.js","sourceRoot":"","sources":["../../../src/utils/createHttpServer.ts"],"names":[],"mappings":"AAAA,OAAO,EAAkB,aAAa,EAAC,MAAM,UAAU,CAAC;AACxD,OAAO,EAAC,mBAAmB,EAAC,MAAM,YAAY,CAAC;AAC/C,OAAO,IAAI,MAAM,MAAM,CAAC;AACxB,OAAO,EAAC,YAAY,EAAC,MAAM,gBAAgB,CAAC;AAE5C,MAAM,UAAU,gBAAgB,CAAC,QAAyB,EAAE,eAAqC;IAC/F,MAAM,EAAC,QAAQ,EAAC,GAAG,QAAQ,CAAC;IAC5B,MAAM,QAAQ,GAAG,QAAQ,CAAC,
|
|
1
|
+
{"version":3,"file":"createHttpServer.js","sourceRoot":"","sources":["../../../src/utils/createHttpServer.ts"],"names":[],"mappings":"AAAA,OAAO,EAAkB,aAAa,EAAC,MAAM,UAAU,CAAC;AACxD,OAAO,EAAC,mBAAmB,EAAC,MAAM,YAAY,CAAC;AAC/C,OAAO,IAAI,MAAM,MAAM,CAAC;AACxB,OAAO,EAAC,YAAY,EAAC,MAAM,gBAAgB,CAAC;AAE5C,MAAM,UAAU,gBAAgB,CAAC,QAAyB,EAAE,eAAqC;IAC/F,MAAM,EAAC,QAAQ,EAAC,GAAG,QAAQ,CAAC;IAC5B,MAAM,QAAQ,GAAG,QAAQ,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC;IAC1C,MAAM,WAAW,GAAG,QAAQ,CAAC,GAAG,CAAC,aAAa,CAAC,CAAC;IAEhD,MAAM,MAAM,GAAG,QAAQ,KAAK,KAAK,CAAC,CAAC,CAAC,IAAI,CAAC,YAAY,CAAC,WAAW,EAAE,eAAe,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC;IAE3F,QAAQ,CAAC,WAAW,CAAC,IAAI,CAAC,MAAM,EAAE;QAChC,KAAK,EAAE,aAAa,CAAC,SAAS;QAC9B,QAAQ,EAAE,MAAM;KACjB,CAAC,CAAC;IAEH,QAAQ,CAAC,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;IAE7B,IAAI,MAAM,EAAE;QACV,MAAM,QAAQ,GAAG,mBAAmB,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAC;QAEvD,OAAO,KAAK,IAAI,EAAE;YAChB,MAAM,gBAAgB,GAAG,MAAM,YAAY,CAAC,QAAQ,EAAE,MAAqB,EAAE,QAAQ,CAAC,CAAC;YACvF,QAAQ,CAAC,GAAG,CAAC,UAAU,EAAE,GAAG,gBAAgB,CAAC,OAAO,IAAI,gBAAgB,CAAC,IAAI,EAAE,CAAC,CAAC;YACjF,OAAO,MAAM,CAAC;QAChB,CAAC,CAAC;KACH;AACH,CAAC","sourcesContent":["import {InjectorService, ProviderScope} from \"@tsed/di\";\nimport {getHostInfoFromPort} from \"@tsed/core\";\nimport Http from \"http\";\nimport {listenServer} from \"./listenServer\";\n\nexport function createHttpServer(injector: InjectorService, requestListener: Http.RequestListener) {\n const {settings} = injector;\n const httpPort = settings.get(\"httpPort\");\n const httpOptions = settings.get(\"httpOptions\");\n\n const server = httpPort !== false ? Http.createServer(httpOptions, requestListener) : null;\n\n injector.addProvider(Http.Server, {\n scope: ProviderScope.SINGLETON,\n useValue: server\n });\n\n injector.invoke(Http.Server);\n\n if (server) {\n const hostInfo = getHostInfoFromPort(\"http\", httpPort);\n\n return async () => {\n const resolvedHostInfo = await listenServer(injector, server as Http.Server, hostInfo);\n settings.set(\"httpPort\", `${resolvedHostInfo.address}:${resolvedHostInfo.port}`);\n return server;\n };\n }\n}\n"]}
|
|
@@ -4,8 +4,8 @@ import Https from "https";
|
|
|
4
4
|
import { listenServer } from "./listenServer.js";
|
|
5
5
|
export function createHttpsServer(injector, requestListener) {
|
|
6
6
|
const { settings } = injector;
|
|
7
|
-
const httpsPort = settings.
|
|
8
|
-
const httpsOptions = settings.
|
|
7
|
+
const httpsPort = settings.get("httpsPort");
|
|
8
|
+
const httpsOptions = settings.get("httpsOptions");
|
|
9
9
|
const server = httpsPort !== false ? Https.createServer(httpsOptions, requestListener) : null;
|
|
10
10
|
injector.addProvider(Https.Server, {
|
|
11
11
|
scope: ProviderScope.SINGLETON,
|
|
@@ -16,7 +16,7 @@ export function createHttpsServer(injector, requestListener) {
|
|
|
16
16
|
const hostInfo = getHostInfoFromPort("https", httpsPort);
|
|
17
17
|
return async () => {
|
|
18
18
|
const resolvedHostInfo = await listenServer(injector, server, hostInfo);
|
|
19
|
-
settings.
|
|
19
|
+
settings.set("httpsPort", `${resolvedHostInfo.address}:${resolvedHostInfo.port}`);
|
|
20
20
|
return server;
|
|
21
21
|
};
|
|
22
22
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createHttpsServer.js","sourceRoot":"","sources":["../../../src/utils/createHttpsServer.ts"],"names":[],"mappings":"AAAA,OAAO,EAAkB,aAAa,EAAC,MAAM,UAAU,CAAC;AACxD,OAAO,EAAC,mBAAmB,EAAC,MAAM,YAAY,CAAC;AAC/C,OAAO,KAAK,MAAM,OAAO,CAAC;AAE1B,OAAO,EAAC,YAAY,EAAC,MAAM,gBAAgB,CAAC;AAE5C,MAAM,UAAU,iBAAiB,CAAC,QAAyB,EAAE,eAAsC;IACjG,MAAM,EAAC,QAAQ,EAAC,GAAG,QAAQ,CAAC;IAC5B,MAAM,SAAS,GAAG,QAAQ,CAAC,
|
|
1
|
+
{"version":3,"file":"createHttpsServer.js","sourceRoot":"","sources":["../../../src/utils/createHttpsServer.ts"],"names":[],"mappings":"AAAA,OAAO,EAAkB,aAAa,EAAC,MAAM,UAAU,CAAC;AACxD,OAAO,EAAC,mBAAmB,EAAC,MAAM,YAAY,CAAC;AAC/C,OAAO,KAAK,MAAM,OAAO,CAAC;AAE1B,OAAO,EAAC,YAAY,EAAC,MAAM,gBAAgB,CAAC;AAE5C,MAAM,UAAU,iBAAiB,CAAC,QAAyB,EAAE,eAAsC;IACjG,MAAM,EAAC,QAAQ,EAAC,GAAG,QAAQ,CAAC;IAC5B,MAAM,SAAS,GAAG,QAAQ,CAAC,GAAG,CAAC,WAAW,CAAC,CAAC;IAC5C,MAAM,YAAY,GAAG,QAAQ,CAAC,GAAG,CAAC,cAAc,CAAC,CAAC;IAElD,MAAM,MAAM,GAAG,SAAS,KAAK,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,YAAY,CAAC,YAAY,EAAE,eAAe,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC;IAE9F,QAAQ,CAAC,WAAW,CAAC,KAAK,CAAC,MAAM,EAAE;QACjC,KAAK,EAAE,aAAa,CAAC,SAAS;QAC9B,QAAQ,EAAE,MAAM;KACjB,CAAC,CAAC;IAEH,QAAQ,CAAC,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC;IAE9B,IAAI,MAAM,EAAE;QACV,MAAM,QAAQ,GAAG,mBAAmB,CAAC,OAAO,EAAE,SAAS,CAAC,CAAC;QAEzD,OAAO,KAAK,IAAI,EAAE;YAChB,MAAM,gBAAgB,GAAG,MAAM,YAAY,CAAC,QAAQ,EAAE,MAAsB,EAAE,QAAQ,CAAC,CAAC;YACxF,QAAQ,CAAC,GAAG,CAAC,WAAW,EAAE,GAAG,gBAAgB,CAAC,OAAO,IAAI,gBAAgB,CAAC,IAAI,EAAE,CAAC,CAAC;YAClF,OAAO,MAAM,CAAC;QAChB,CAAC,CAAC;KACH;AACH,CAAC","sourcesContent":["import {InjectorService, ProviderScope} from \"@tsed/di\";\nimport {getHostInfoFromPort} from \"@tsed/core\";\nimport Https from \"https\";\nimport Http from \"http\";\nimport {listenServer} from \"./listenServer\";\n\nexport function createHttpsServer(injector: InjectorService, requestListener?: Http.RequestListener) {\n const {settings} = injector;\n const httpsPort = settings.get(\"httpsPort\");\n const httpsOptions = settings.get(\"httpsOptions\");\n\n const server = httpsPort !== false ? Https.createServer(httpsOptions, requestListener) : null;\n\n injector.addProvider(Https.Server, {\n scope: ProviderScope.SINGLETON,\n useValue: server\n });\n\n injector.invoke(Https.Server);\n\n if (server) {\n const hostInfo = getHostInfoFromPort(\"https\", httpsPort);\n\n return async () => {\n const resolvedHostInfo = await listenServer(injector, server as Https.Server, hostInfo);\n settings.set(\"httpsPort\", `${resolvedHostInfo.address}:${resolvedHostInfo.port}`);\n return server;\n };\n }\n}\n"]}
|
|
@@ -1,20 +1,18 @@
|
|
|
1
|
-
import { InjectorService, setLoggerLevel } from "@tsed/di";
|
|
2
|
-
import { $log } from "@tsed/logger";
|
|
3
1
|
import { toMap } from "@tsed/core";
|
|
2
|
+
import { InjectorService, setLoggerConfiguration } from "@tsed/di";
|
|
3
|
+
import { $log } from "@tsed/logger";
|
|
4
4
|
import { PlatformConfiguration } from "../config/services/PlatformConfiguration.js";
|
|
5
|
-
import { PlatformHandler } from "../services/PlatformHandler.js";
|
|
6
|
-
import { PlatformResponse } from "../services/PlatformResponse.js";
|
|
7
|
-
import { PlatformRouter } from "../services/PlatformRouter.js";
|
|
8
|
-
import { PlatformApplication } from "../services/PlatformApplication.js";
|
|
9
5
|
import { Platform } from "../services/Platform.js";
|
|
10
|
-
import { PlatformRequest } from "../services/PlatformRequest.js";
|
|
11
6
|
import { PlatformAdapter } from "../services/PlatformAdapter.js";
|
|
7
|
+
import { PlatformApplication } from "../services/PlatformApplication.js";
|
|
8
|
+
import { PlatformHandler } from "../services/PlatformHandler.js";
|
|
9
|
+
import { PlatformRequest } from "../services/PlatformRequest.js";
|
|
10
|
+
import { PlatformResponse } from "../services/PlatformResponse.js";
|
|
12
11
|
$log.name = "TSED";
|
|
13
12
|
const DEFAULT_PROVIDERS = [
|
|
14
13
|
{ provide: PlatformHandler },
|
|
15
14
|
{ provide: PlatformResponse },
|
|
16
15
|
{ provide: PlatformRequest },
|
|
17
|
-
{ provide: PlatformRouter },
|
|
18
16
|
{ provide: PlatformApplication },
|
|
19
17
|
{ provide: Platform }
|
|
20
18
|
];
|
|
@@ -30,7 +28,7 @@ export function createInjector({ adapter, settings = {} }) {
|
|
|
30
28
|
});
|
|
31
29
|
}
|
|
32
30
|
injector.invoke(PlatformAdapter);
|
|
33
|
-
|
|
31
|
+
setLoggerConfiguration(injector);
|
|
34
32
|
const instance = injector.get(PlatformAdapter);
|
|
35
33
|
instance.providers = [...DEFAULT_PROVIDERS, ...instance.providers];
|
|
36
34
|
toMap(instance.providers, "provide").forEach((provider, token) => {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createInjector.js","sourceRoot":"","sources":["../../../src/utils/createInjector.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,
|
|
1
|
+
{"version":3,"file":"createInjector.js","sourceRoot":"","sources":["../../../src/utils/createInjector.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,KAAK,EAAO,MAAM,YAAY,CAAC;AACvC,OAAO,EAAC,eAAe,EAAgB,sBAAsB,EAAC,MAAM,UAAU,CAAC;AAC/E,OAAO,EAAC,IAAI,EAAC,MAAM,cAAc,CAAC;AAClC,OAAO,EAAC,qBAAqB,EAAC,MAAM,0CAA0C,CAAC;AAC/E,OAAO,EAAC,QAAQ,EAAC,MAAM,sBAAsB,CAAC;AAC9C,OAAO,EAAC,eAAe,EAAC,MAAM,6BAA6B,CAAC;AAC5D,OAAO,EAAC,mBAAmB,EAAC,MAAM,iCAAiC,CAAC;AACpE,OAAO,EAAC,eAAe,EAAC,MAAM,6BAA6B,CAAC;AAC5D,OAAO,EAAC,eAAe,EAAC,MAAM,6BAA6B,CAAC;AAC5D,OAAO,EAAC,gBAAgB,EAAC,MAAM,8BAA8B,CAAC;AAE9D,IAAI,CAAC,IAAI,GAAG,MAAM,CAAC;AAEnB,MAAM,iBAAiB,GAAG;IACxB,EAAC,OAAO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} from \"@tsed/core\";\nimport {InjectorService, ProviderOpts, setLoggerConfiguration} from \"@tsed/di\";\nimport {$log} from \"@tsed/logger\";\nimport {PlatformConfiguration} from \"../config/services/PlatformConfiguration\";\nimport {Platform} from \"../services/Platform\";\nimport {PlatformAdapter} from \"../services/PlatformAdapter\";\nimport {PlatformApplication} from \"../services/PlatformApplication\";\nimport {PlatformHandler} from \"../services/PlatformHandler\";\nimport {PlatformRequest} from \"../services/PlatformRequest\";\nimport {PlatformResponse} from \"../services/PlatformResponse\";\n\n$log.name = \"TSED\";\n\nconst DEFAULT_PROVIDERS = [\n {provide: PlatformHandler},\n {provide: PlatformResponse},\n {provide: PlatformRequest},\n {provide: PlatformApplication},\n {provide: Platform}\n];\n\ninterface CreateInjectorOptions {\n adapter?: Type<PlatformAdapter<any>>;\n settings?: Partial<TsED.Configuration>;\n}\n\nexport function createInjector({adapter, settings = {}}: CreateInjectorOptions) {\n const injector = new InjectorService();\n injector.addProvider(PlatformConfiguration);\n\n injector.settings = injector.invoke(PlatformConfiguration);\n injector.logger = $log;\n injector.settings.set(settings);\n\n if (adapter) {\n injector.addProvider(PlatformAdapter, {\n useClass: adapter\n });\n }\n\n injector.invoke(PlatformAdapter);\n\n setLoggerConfiguration(injector);\n\n const instance = injector.get<PlatformAdapter>(PlatformAdapter)!;\n\n instance.providers = [...DEFAULT_PROVIDERS, ...instance.providers];\n\n toMap<any, ProviderOpts>(instance.providers, \"provide\").forEach((provider, token) => {\n injector.addProvider(token, provider);\n });\n\n injector.invoke(PlatformApplication);\n\n return injector;\n}\n"]}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { getContext } from "@tsed/di";
|
|
2
|
+
export function ensureContext(request, cb, fallback) {
|
|
3
|
+
const $ctx = getContext();
|
|
4
|
+
if ($ctx) {
|
|
5
|
+
return cb($ctx);
|
|
6
|
+
}
|
|
7
|
+
if (!request?.$ctx && fallback) {
|
|
8
|
+
return fallback();
|
|
9
|
+
}
|
|
10
|
+
return request.$ctx.runInContext(() => cb(request.$ctx));
|
|
11
|
+
}
|
|
12
|
+
//# sourceMappingURL=ensureContext.js.map
|