@tsed/common 7.0.0-beta.12 → 7.0.0-beta.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/jest.config.js +14 -0
- package/lib/cjs/builder/PlatformBuilder.js +83 -63
- package/lib/cjs/builder/PlatformBuilder.js.map +1 -1
- package/lib/cjs/config/interfaces/PlatformLoggerSettings.js.map +1 -1
- package/lib/cjs/config/services/PlatformConfiguration.js +0 -70
- package/lib/cjs/config/services/PlatformConfiguration.js.map +1 -1
- package/lib/cjs/decorators/params/error.js +1 -1
- package/lib/cjs/decorators/params/error.js.map +1 -1
- package/lib/cjs/domain/AnyToPromiseWithCtx.js +4 -5
- package/lib/cjs/domain/AnyToPromiseWithCtx.js.map +1 -1
- package/lib/cjs/domain/PlatformContext.js +82 -32
- package/lib/cjs/domain/PlatformContext.js.map +1 -1
- package/lib/cjs/domain/PlatformRouteDetails.js +0 -21
- package/lib/cjs/domain/PlatformRouteDetails.js.map +1 -1
- package/lib/cjs/exports.js +1 -0
- package/lib/cjs/exports.js.map +1 -1
- package/lib/cjs/index.js +1 -7
- package/lib/cjs/index.js.map +1 -1
- package/lib/cjs/interfaces/Route.js.map +1 -1
- package/lib/cjs/middlewares/PlatformAcceptMimesMiddleware.js +2 -1
- package/lib/cjs/middlewares/PlatformAcceptMimesMiddleware.js.map +1 -1
- package/lib/cjs/middlewares/PlatformMulterMiddleware.js +2 -0
- package/lib/cjs/middlewares/PlatformMulterMiddleware.js.map +1 -1
- package/lib/cjs/services/FakeAdapter.js +6 -12
- package/lib/cjs/services/FakeAdapter.js.map +1 -1
- package/lib/cjs/services/Platform.js +43 -85
- package/lib/cjs/services/Platform.js.map +1 -1
- package/lib/cjs/services/PlatformAdapter.js +10 -0
- package/lib/cjs/services/PlatformAdapter.js.map +1 -1
- package/lib/cjs/services/PlatformApplication.js +23 -9
- package/lib/cjs/services/PlatformApplication.js.map +1 -1
- package/lib/cjs/services/PlatformHandler.js +88 -161
- package/lib/cjs/services/PlatformHandler.js.map +1 -1
- package/lib/cjs/services/PlatformMiddlewaresChain.js +5 -19
- package/lib/cjs/services/PlatformMiddlewaresChain.js.map +1 -1
- package/lib/cjs/services/PlatformRequest.js +17 -12
- package/lib/cjs/services/PlatformRequest.js.map +1 -1
- package/lib/cjs/services/PlatformResponse.js +20 -20
- package/lib/cjs/services/PlatformResponse.js.map +1 -1
- package/lib/cjs/services/PlatformTest.js +20 -8
- package/lib/cjs/services/PlatformTest.js.map +1 -1
- package/lib/cjs/utils/createContext.js +30 -21
- package/lib/cjs/utils/createContext.js.map +1 -1
- package/lib/cjs/utils/createHttpServer.js +3 -3
- package/lib/cjs/utils/createHttpServer.js.map +1 -1
- package/lib/cjs/utils/createHttpsServer.js +3 -3
- package/lib/cjs/utils/createHttpsServer.js.map +1 -1
- package/lib/cjs/utils/createInjector.js +6 -8
- package/lib/cjs/utils/createInjector.js.map +1 -1
- package/lib/cjs/utils/ensureContext.js +16 -0
- package/lib/cjs/utils/ensureContext.js.map +1 -0
- package/lib/cjs/utils/getConfiguration.js +2 -12
- package/lib/cjs/utils/getConfiguration.js.map +1 -1
- package/lib/cjs/utils/printRoutes.js +10 -8
- package/lib/cjs/utils/printRoutes.js.map +1 -1
- package/lib/cjs/utils/setResponseHeaders.js +7 -13
- package/lib/cjs/utils/setResponseHeaders.js.map +1 -1
- package/lib/esm/builder/PlatformBuilder.js +60 -63
- package/lib/esm/builder/PlatformBuilder.js.map +1 -1
- package/lib/esm/config/interfaces/PlatformLoggerSettings.js.map +1 -1
- package/lib/esm/config/services/PlatformConfiguration.js +0 -70
- package/lib/esm/config/services/PlatformConfiguration.js.map +1 -1
- package/lib/esm/decorators/params/error.js +1 -1
- package/lib/esm/decorators/params/error.js.map +1 -1
- package/lib/esm/domain/AnyToPromiseWithCtx.js +4 -5
- package/lib/esm/domain/AnyToPromiseWithCtx.js.map +1 -1
- package/lib/esm/domain/PlatformContext.js +82 -32
- package/lib/esm/domain/PlatformContext.js.map +1 -1
- package/lib/esm/domain/PlatformRouteDetails.js +1 -19
- package/lib/esm/domain/PlatformRouteDetails.js.map +1 -1
- package/lib/esm/exports.js +1 -0
- package/lib/esm/exports.js.map +1 -1
- package/lib/esm/index.js +1 -7
- package/lib/esm/index.js.map +1 -1
- package/lib/esm/interfaces/Route.js.map +1 -1
- package/lib/esm/middlewares/PlatformAcceptMimesMiddleware.js +2 -1
- package/lib/esm/middlewares/PlatformAcceptMimesMiddleware.js.map +1 -1
- package/lib/esm/middlewares/PlatformMulterMiddleware.js +2 -0
- package/lib/esm/middlewares/PlatformMulterMiddleware.js.map +1 -1
- package/lib/esm/services/FakeAdapter.js +6 -12
- package/lib/esm/services/FakeAdapter.js.map +1 -1
- package/lib/esm/services/Platform.js +44 -86
- package/lib/esm/services/Platform.js.map +1 -1
- package/lib/esm/services/PlatformAdapter.js +10 -0
- package/lib/esm/services/PlatformAdapter.js.map +1 -1
- package/lib/esm/services/PlatformApplication.js +23 -9
- package/lib/esm/services/PlatformApplication.js.map +1 -1
- package/lib/esm/services/PlatformHandler.js +90 -163
- package/lib/esm/services/PlatformHandler.js.map +1 -1
- package/lib/esm/services/PlatformMiddlewaresChain.js +5 -19
- package/lib/esm/services/PlatformMiddlewaresChain.js.map +1 -1
- package/lib/esm/services/PlatformRequest.js +17 -12
- package/lib/esm/services/PlatformRequest.js.map +1 -1
- package/lib/esm/services/PlatformResponse.js +20 -20
- package/lib/esm/services/PlatformResponse.js.map +1 -1
- package/lib/esm/services/PlatformTest.js +20 -8
- package/lib/esm/services/PlatformTest.js.map +1 -1
- package/lib/esm/utils/createContext.js +28 -20
- package/lib/esm/utils/createContext.js.map +1 -1
- package/lib/esm/utils/createHttpServer.js +3 -3
- package/lib/esm/utils/createHttpServer.js.map +1 -1
- package/lib/esm/utils/createHttpsServer.js +3 -3
- package/lib/esm/utils/createHttpsServer.js.map +1 -1
- package/lib/esm/utils/createInjector.js +7 -9
- package/lib/esm/utils/createInjector.js.map +1 -1
- package/lib/esm/utils/ensureContext.js +12 -0
- package/lib/esm/utils/ensureContext.js.map +1 -0
- package/lib/esm/utils/getConfiguration.js +4 -14
- package/lib/esm/utils/getConfiguration.js.map +1 -1
- package/lib/esm/utils/printRoutes.js +10 -8
- package/lib/esm/utils/printRoutes.js.map +1 -1
- package/lib/esm/utils/setResponseHeaders.js +7 -13
- package/lib/esm/utils/setResponseHeaders.js.map +1 -1
- package/lib/types/builder/PlatformBuilder.d.ts +17 -16
- package/lib/types/config/interfaces/PlatformLoggerSettings.d.ts +2 -43
- package/lib/types/config/services/PlatformConfiguration.d.ts +1 -22
- package/lib/types/domain/AnyToPromiseWithCtx.d.ts +1 -9
- package/lib/types/domain/PlatformContext.d.ts +36 -15
- package/lib/types/domain/PlatformRouteDetails.d.ts +4 -19
- package/lib/types/exports.d.ts +1 -0
- package/lib/types/index.d.ts +1 -7
- package/lib/types/interfaces/Route.d.ts +2 -3
- package/lib/types/middlewares/PlatformMulterMiddleware.d.ts +1 -1
- package/lib/types/services/FakeAdapter.d.ts +7 -6
- package/lib/types/services/Platform.d.ts +11 -28
- package/lib/types/services/PlatformAdapter.d.ts +19 -10
- package/lib/types/services/PlatformApplication.d.ts +14 -6
- package/lib/types/services/PlatformHandler.d.ts +19 -55
- package/lib/types/services/PlatformMiddlewaresChain.d.ts +1 -2
- package/lib/types/services/PlatformRequest.d.ts +8 -9
- package/lib/types/services/PlatformResponse.d.ts +10 -13
- package/lib/types/services/PlatformTest.d.ts +4 -3
- package/lib/types/utils/createContext.d.ts +4 -1
- package/lib/types/utils/createHttpsServer.d.ts +1 -0
- package/lib/types/utils/createInjector.d.ts +2 -2
- package/lib/types/utils/ensureContext.d.ts +2 -0
- package/lib/types/utils/listenServer.d.ts +2 -0
- package/package.json +23 -21
- package/lib/cjs/builder/PlatformControllerBuilder.js +0 -82
- package/lib/cjs/builder/PlatformControllerBuilder.js.map +0 -1
- package/lib/cjs/domain/ControllerProvider.js +0 -51
- package/lib/cjs/domain/ControllerProvider.js.map +0 -1
- package/lib/cjs/domain/HandlerMetadata.js +0 -57
- package/lib/cjs/domain/HandlerMetadata.js.map +0 -1
- package/lib/cjs/middlewares/__mock__/FakeCtrl.js +0 -12
- package/lib/cjs/middlewares/__mock__/FakeCtrl.js.map +0 -1
- package/lib/cjs/middlewares/bindEndpointMiddleware.js +0 -13
- package/lib/cjs/middlewares/bindEndpointMiddleware.js.map +0 -1
- package/lib/cjs/services/PlatformRouter.js +0 -108
- package/lib/cjs/services/PlatformRouter.js.map +0 -1
- package/lib/cjs/utils/__mock__/Test1.js +0 -8
- package/lib/cjs/utils/__mock__/Test1.js.map +0 -1
- package/lib/cjs/utils/__mock__/Test2.js +0 -8
- package/lib/cjs/utils/__mock__/Test2.js.map +0 -1
- package/lib/cjs/utils/__mock__/controllers/rest/TestCtrl.js +0 -12
- package/lib/cjs/utils/__mock__/controllers/rest/TestCtrl.js.map +0 -1
- package/lib/cjs/utils/__mock__/module1/Module1.js +0 -19
- package/lib/cjs/utils/__mock__/module1/Module1.js.map +0 -1
- package/lib/cjs/utils/__mock__/module1/controllers/M1Ctrl1.js +0 -17
- package/lib/cjs/utils/__mock__/module1/controllers/M1Ctrl1.js.map +0 -1
- package/lib/cjs/utils/__mock__/module1/services/M1Service.js +0 -17
- package/lib/cjs/utils/__mock__/module1/services/M1Service.js.map +0 -1
- package/lib/cjs/utils/__mock__/module1/submodule/SubModule.js +0 -15
- package/lib/cjs/utils/__mock__/module1/submodule/SubModule.js.map +0 -1
- package/lib/cjs/utils/__mock__/module1/submodule/SubService.js +0 -12
- package/lib/cjs/utils/__mock__/module1/submodule/SubService.js.map +0 -1
- package/lib/cjs/utils/__mock__/module2/Module2.js +0 -18
- package/lib/cjs/utils/__mock__/module2/Module2.js.map +0 -1
- package/lib/cjs/utils/__mock__/module2/controllers/M2Ctrl.js +0 -12
- package/lib/cjs/utils/__mock__/module2/controllers/M2Ctrl.js.map +0 -1
- package/lib/cjs/utils/createHandlerMetadata.js +0 -49
- package/lib/cjs/utils/createHandlerMetadata.js.map +0 -1
- package/lib/cjs/utils/useCtxHandler.js +0 -15
- package/lib/cjs/utils/useCtxHandler.js.map +0 -1
- package/lib/esm/builder/PlatformControllerBuilder.js +0 -76
- package/lib/esm/builder/PlatformControllerBuilder.js.map +0 -1
- package/lib/esm/domain/ControllerProvider.js +0 -47
- package/lib/esm/domain/ControllerProvider.js.map +0 -1
- package/lib/esm/domain/HandlerMetadata.js +0 -53
- package/lib/esm/domain/HandlerMetadata.js.map +0 -1
- package/lib/esm/middlewares/__mock__/FakeCtrl.js +0 -9
- package/lib/esm/middlewares/__mock__/FakeCtrl.js.map +0 -1
- package/lib/esm/middlewares/bindEndpointMiddleware.js +0 -9
- package/lib/esm/middlewares/bindEndpointMiddleware.js.map +0 -1
- package/lib/esm/services/PlatformRouter.js +0 -105
- package/lib/esm/services/PlatformRouter.js.map +0 -1
- package/lib/esm/utils/__mock__/Test1.js +0 -4
- package/lib/esm/utils/__mock__/Test1.js.map +0 -1
- package/lib/esm/utils/__mock__/Test2.js +0 -4
- package/lib/esm/utils/__mock__/Test2.js.map +0 -1
- package/lib/esm/utils/__mock__/controllers/rest/TestCtrl.js +0 -9
- package/lib/esm/utils/__mock__/controllers/rest/TestCtrl.js.map +0 -1
- package/lib/esm/utils/__mock__/module1/Module1.js +0 -16
- package/lib/esm/utils/__mock__/module1/Module1.js.map +0 -1
- package/lib/esm/utils/__mock__/module1/controllers/M1Ctrl1.js +0 -14
- package/lib/esm/utils/__mock__/module1/controllers/M1Ctrl1.js.map +0 -1
- package/lib/esm/utils/__mock__/module1/services/M1Service.js +0 -14
- package/lib/esm/utils/__mock__/module1/services/M1Service.js.map +0 -1
- package/lib/esm/utils/__mock__/module1/submodule/SubModule.js +0 -12
- package/lib/esm/utils/__mock__/module1/submodule/SubModule.js.map +0 -1
- package/lib/esm/utils/__mock__/module1/submodule/SubService.js +0 -9
- package/lib/esm/utils/__mock__/module1/submodule/SubService.js.map +0 -1
- package/lib/esm/utils/__mock__/module2/Module2.js +0 -15
- package/lib/esm/utils/__mock__/module2/Module2.js.map +0 -1
- package/lib/esm/utils/__mock__/module2/controllers/M2Ctrl.js +0 -9
- package/lib/esm/utils/__mock__/module2/controllers/M2Ctrl.js.map +0 -1
- package/lib/esm/utils/createHandlerMetadata.js +0 -45
- package/lib/esm/utils/createHandlerMetadata.js.map +0 -1
- package/lib/esm/utils/useCtxHandler.js +0 -11
- package/lib/esm/utils/useCtxHandler.js.map +0 -1
- package/lib/types/builder/PlatformControllerBuilder.d.ts +0 -18
- package/lib/types/domain/ControllerProvider.d.ts +0 -24
- package/lib/types/domain/HandlerMetadata.d.ts +0 -32
- package/lib/types/middlewares/__mock__/FakeCtrl.d.ts +0 -2
- package/lib/types/middlewares/bindEndpointMiddleware.d.ts +0 -6
- package/lib/types/services/PlatformRouter.d.ts +0 -51
- package/lib/types/utils/__mock__/Test1.d.ts +0 -3
- package/lib/types/utils/__mock__/Test2.d.ts +0 -3
- package/lib/types/utils/__mock__/controllers/rest/TestCtrl.d.ts +0 -2
- package/lib/types/utils/__mock__/module1/Module1.d.ts +0 -2
- package/lib/types/utils/__mock__/module1/controllers/M1Ctrl1.d.ts +0 -5
- package/lib/types/utils/__mock__/module1/services/M1Service.d.ts +0 -5
- package/lib/types/utils/__mock__/module1/submodule/SubModule.d.ts +0 -2
- package/lib/types/utils/__mock__/module1/submodule/SubService.d.ts +0 -2
- package/lib/types/utils/__mock__/module2/Module2.d.ts +0 -2
- package/lib/types/utils/__mock__/module2/controllers/M2Ctrl.d.ts +0 -2
- package/lib/types/utils/createHandlerMetadata.d.ts +0 -8
- package/lib/types/utils/useCtxHandler.d.ts +0 -13
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Platform.js","sourceRoot":"","sources":["../../../src/services/Platform.ts"],"names":[],"mappings":";;;;
|
|
1
|
+
{"version":3,"file":"Platform.js","sourceRoot":"","sources":["../../../src/services/Platform.ts"],"names":[],"mappings":";;;;AAAA,iCAAmF;AACn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QACN,mDAAwB;GARlD,QAAQ,CAoEpB;AApEY,4BAAQ","sourcesContent":["import {Injectable, InjectorService, ProviderScope, TokenProvider} from \"@tsed/di\";\nimport {PlatformHandlerMetadata, PlatformRouters} from \"@tsed/platform-router\";\nimport {ControllerProvider} from \"@tsed/di\";\nimport {Route, RouteController} from \"../interfaces/Route\";\nimport {PlatformApplication} from \"./PlatformApplication\";\nimport {PlatformHandler} from \"./PlatformHandler\";\nimport {PlatformMiddlewaresChain} from \"./PlatformMiddlewaresChain\";\n\n/**\n * `Platform` is used to provide all routes collected by annotation `@Controller`.\n *\n * @platform\n */\n@Injectable({\n scope: ProviderScope.SINGLETON,\n imports: []\n})\nexport class Platform {\n #controllers: Map<string, RouteController> = new Map();\n\n constructor(\n readonly injector: InjectorService,\n readonly platformApplication: PlatformApplication,\n readonly platformRouters: PlatformRouters,\n readonly platformHandler: PlatformHandler,\n readonly platformMiddlewaresChain: PlatformMiddlewaresChain\n ) {\n // configure the router module\n platformRouters.hooks\n .on(\"alterEndpointHandlers\", platformMiddlewaresChain.get.bind(platformMiddlewaresChain))\n .on(\"alterHandler\", (handler: Function, handlerMetadata: PlatformHandlerMetadata) => {\n handler = handlerMetadata.isRawMiddleware() ? handler : this.platformHandler.createHandler(handler as any, handlerMetadata);\n\n return platformApplication.adapter.mapHandler(handler, handlerMetadata);\n });\n\n platformRouters.prebuild();\n }\n\n get app() {\n return this.platformApplication;\n }\n\n public addRoutes(routes: Route[]) {\n routes.forEach((routeSettings) => {\n this.addRoute(routeSettings.route, routeSettings.token);\n });\n }\n\n public addRoute(route: string, token: TokenProvider) {\n const provider = this.injector.getProvider(token) as ControllerProvider;\n\n if (!provider || provider.hasParent()) {\n return this;\n }\n\n const router = this.platformRouters.from(provider.token);\n\n this.app.use(route, router);\n\n return this;\n }\n\n public getLayers() {\n this.#controllers = new Map();\n\n return this.platformRouters.getLayers(this.app).map((layer) => {\n if (layer.isProvider()) {\n this.#controllers.set(layer.provider.token, {\n route: String(layer.path).split(layer.provider.path)[0],\n provider: layer.provider\n });\n }\n\n return layer;\n });\n }\n\n /**\n * Get all controllers mounted on the application.\n * @returns {RouteController[]}\n */\n public getMountedControllers(): RouteController[] {\n return [...this.#controllers.values()];\n }\n}\n"]}
|
|
@@ -4,6 +4,16 @@ exports.PlatformAdapter = void 0;
|
|
|
4
4
|
const di_1 = require("@tsed/di");
|
|
5
5
|
const FakeAdapter_1 = require("./FakeAdapter");
|
|
6
6
|
class PlatformAdapter {
|
|
7
|
+
/**
|
|
8
|
+
* Load providers in top priority
|
|
9
|
+
*/
|
|
10
|
+
providers;
|
|
11
|
+
/**
|
|
12
|
+
* Called after the injector instantiation
|
|
13
|
+
*/
|
|
14
|
+
onInit;
|
|
15
|
+
beforeLoadRoutes;
|
|
16
|
+
afterLoadRoutes;
|
|
7
17
|
}
|
|
8
18
|
exports.PlatformAdapter = PlatformAdapter;
|
|
9
19
|
(0, di_1.registerProvider)({
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"PlatformAdapter.js","sourceRoot":"","sources":["../../../src/services/PlatformAdapter.ts"],"names":[],"mappings":";;;AACA,iCAAyE;
|
|
1
|
+
{"version":3,"file":"PlatformAdapter.js","sourceRoot":"","sources":["../../../src/services/PlatformAdapter.ts"],"names":[],"mappings":";;;AACA,iCAAyE;AAKzE,+CAA0C;AAE1C,MAAsB,eAAe;IACnC;;OAEG;IACH,SAAS,CAAiB;IAC1B;;OAEG;IACH,MAAM,CAAa;IACnB,gBAAgB,CAAsB;IACtC,eAAe,CAAsB;CAsCtC;AAhDD,0CAgDC;AAMD,IAAA,qBAAgB,EAAC;IACf,OAAO,EAAE,eAAe;IACxB,IAAI,EAAE,CAAC,oBAAe,CAAC;IACvB,QAAQ,EAAE,yBAAW;CACtB,CAAC,CAAC","sourcesContent":["import {Type} from \"@tsed/core\";\nimport {InjectorService, ProviderOpts, registerProvider} from \"@tsed/di\";\nimport {PlatformContextHandler, PlatformHandlerMetadata, PlatformLayer} from \"@tsed/platform-router\";\nimport {PlatformMulter, PlatformMulterSettings} from \"../config/interfaces/PlatformMulterSettings\";\nimport {PlatformStaticsOptions} from \"../config/interfaces/PlatformStaticsSettings\";\nimport {PlatformContext} from \"../domain/PlatformContext\";\nimport {FakeAdapter} from \"./FakeAdapter\";\n\nexport abstract class PlatformAdapter<App = TsED.Application> {\n /**\n * Load providers in top priority\n */\n providers: ProviderOpts[];\n /**\n * Called after the injector instantiation\n */\n onInit?: () => any;\n beforeLoadRoutes?: () => Promise<any>;\n afterLoadRoutes?: () => Promise<any>;\n /**\n * create initial context\n */\n abstract useContext: () => any;\n /**\n * Map router layer to the targeted framework\n */\n abstract mapLayers: (layer: PlatformLayer[]) => void;\n /**\n * Map handler to the targeted framework\n */\n abstract mapHandler: (handler: Function, layer: PlatformHandlerMetadata) => Function;\n\n /**\n * Return the app instance\n */\n abstract app(): {app: App; callback(): any};\n\n /**\n * Return the statics middlewares\n * @param endpoint\n * @param options\n */\n abstract statics(endpoint: string, options: PlatformStaticsOptions): any;\n\n /**\n * Return the multipart middleware\n * @param options\n */\n abstract multipart(options: PlatformMulterSettings): PlatformMulter;\n\n /**\n * Return the body parser for the given\n * @param type\n * @param opts\n */\n abstract bodyParser(type: string, opts?: Record<string, any>): any;\n}\n\nexport interface PlatformBuilderSettings<App = TsED.Application> extends Partial<TsED.Configuration> {\n adapter?: Type<PlatformAdapter<App>>;\n}\n\nregisterProvider({\n provide: PlatformAdapter,\n deps: [InjectorService],\n useClass: FakeAdapter\n});\n"]}
|
|
@@ -3,30 +3,44 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.PlatformApplication = void 0;
|
|
4
4
|
const tslib_1 = require("tslib");
|
|
5
5
|
const di_1 = require("@tsed/di");
|
|
6
|
-
const
|
|
7
|
-
const PlatformRouter_1 = require("./PlatformRouter");
|
|
6
|
+
const platform_router_1 = require("@tsed/platform-router");
|
|
8
7
|
const PlatformAdapter_1 = require("./PlatformAdapter");
|
|
9
8
|
/**
|
|
10
9
|
* `PlatformApplication` is used to provide all routes collected by annotation `@Controller`.
|
|
11
10
|
*
|
|
12
11
|
* @platform
|
|
13
12
|
*/
|
|
14
|
-
let PlatformApplication = class PlatformApplication extends
|
|
15
|
-
|
|
16
|
-
|
|
13
|
+
let PlatformApplication = class PlatformApplication extends platform_router_1.PlatformRouter {
|
|
14
|
+
adapter;
|
|
15
|
+
injector;
|
|
16
|
+
rawApp;
|
|
17
|
+
rawCallback;
|
|
18
|
+
constructor(adapter, injector) {
|
|
19
|
+
super(injector);
|
|
20
|
+
this.adapter = adapter;
|
|
21
|
+
this.injector = injector;
|
|
17
22
|
const { app, callback } = adapter.app();
|
|
18
|
-
this.rawApp =
|
|
19
|
-
this.
|
|
23
|
+
this.rawApp = app;
|
|
24
|
+
this.rawCallback = callback;
|
|
20
25
|
}
|
|
21
26
|
getApp() {
|
|
22
|
-
return this.
|
|
27
|
+
return this.rawApp;
|
|
28
|
+
}
|
|
29
|
+
multer(options) {
|
|
30
|
+
return this.adapter.multipart(options);
|
|
31
|
+
}
|
|
32
|
+
callback(req, res) {
|
|
33
|
+
if (req && res) {
|
|
34
|
+
return this.callback()(req, res);
|
|
35
|
+
}
|
|
36
|
+
return this.rawCallback();
|
|
23
37
|
}
|
|
24
38
|
};
|
|
25
39
|
PlatformApplication = tslib_1.__decorate([
|
|
26
40
|
(0, di_1.Injectable)({
|
|
27
41
|
scope: di_1.ProviderScope.SINGLETON
|
|
28
42
|
}),
|
|
29
|
-
tslib_1.__metadata("design:paramtypes", [
|
|
43
|
+
tslib_1.__metadata("design:paramtypes", [PlatformAdapter_1.PlatformAdapter, di_1.InjectorService])
|
|
30
44
|
], PlatformApplication);
|
|
31
45
|
exports.PlatformApplication = PlatformApplication;
|
|
32
46
|
//# sourceMappingURL=PlatformApplication.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"PlatformApplication.js","sourceRoot":"","sources":["../../../src/services/PlatformApplication.ts"],"names":[],"mappings":";;;;AAAA,
|
|
1
|
+
{"version":3,"file":"PlatformApplication.js","sourceRoot":"","sources":["../../../src/services/PlatformApplication.ts"],"names":[],"mappings":";;;;AAAA,iCAAoE;AACpE,2DAAqD;AAGrD,uDAAkD;AASlD;;;;GAIG;AAIH,IAAa,mBAAmB,GAAhC,MAAa,mBAA4C,SAAQ,gCAAc;IAI1D;IAAsC;IAHzD,MAAM,CAAM;IACZ,WAAW,CAAY;IAEvB,YAAmB,OAA6B,EAAS,QAAyB;QAChF,KAAK,CAAC,QAAQ,CAAC,CAAC;QADC,YAAO,GAAP,OAAO,CAAsB;QAAS,aAAQ,GAAR,QAAQ,CAAiB;QAEhF,MAAM,EAAC,GAAG,EAAE,QAAQ,EAAC,GAAG,OAAO,CAAC,GAAG,EAAE,CAAC;QAEtC,IAAI,CAAC,MAAM,GAAG,GAAG,CAAC;QAClB,IAAI,CAAC,WAAW,GAAG,QAAQ,CAAC;IAC9B,CAAC;IAED,MAAM;QACJ,OAAO,IAAI,CAAC,MAAM,CAAC;IACrB,CAAC;IAED,MAAM,CAAC,OAA+B;QACpC,OAAO,IAAI,CAAC,OAAO,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC;IACzC,CAAC;IAID,QAAQ,CAAC,GAAqB,EAAE,GAAoB;QAClD,IAAI,GAAG,IAAI,GAAG,EAAE;YACd,OAAO,IAAI,CAAC,QAAQ,EAAE,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC;SAClC;QAED,OAAO,IAAI,CAAC,WAAW,EAAE,CAAC;IAC5B,CAAC;CACF,CAAA;AA7BY,mBAAmB;IAH/B,IAAA,eAAU,EAAC;QACV,KAAK,EAAE,kBAAa,CAAC,SAAS;KAC/B,CAAC;6CAK4B,iCAAe,EAAwB,oBAAe;GAJvE,mBAAmB,CA6B/B;AA7BY,kDAAmB","sourcesContent":["import {Injectable, InjectorService, ProviderScope} from \"@tsed/di\";\nimport {PlatformRouter} from \"@tsed/platform-router\";\nimport {IncomingMessage, ServerResponse} from \"http\";\nimport {PlatformMulterSettings} from \"../config/interfaces/PlatformMulterSettings\";\nimport {PlatformAdapter} from \"./PlatformAdapter\";\n\ndeclare global {\n namespace TsED {\n // @ts-ignore\n export interface Application {}\n }\n}\n\n/**\n * `PlatformApplication` is used to provide all routes collected by annotation `@Controller`.\n *\n * @platform\n */\n@Injectable({\n scope: ProviderScope.SINGLETON\n})\nexport class PlatformApplication<App = TsED.Application> extends PlatformRouter {\n rawApp: App;\n rawCallback: () => any;\n\n constructor(public adapter: PlatformAdapter<App>, public injector: InjectorService) {\n super(injector);\n const {app, callback} = adapter.app();\n\n this.rawApp = app;\n this.rawCallback = callback;\n }\n\n getApp(): App {\n return this.rawApp;\n }\n\n multer(options: PlatformMulterSettings) {\n return this.adapter.multipart(options);\n }\n\n callback(): (req: IncomingMessage, res: ServerResponse) => any;\n callback(req: IncomingMessage, res: ServerResponse): any;\n callback(req?: IncomingMessage, res?: ServerResponse) {\n if (req && res) {\n return this.callback()(req, res);\n }\n\n return this.rawCallback();\n }\n}\n"]}
|
|
@@ -7,229 +7,156 @@ const di_1 = require("@tsed/di");
|
|
|
7
7
|
const logger_1 = require("@tsed/logger");
|
|
8
8
|
const platform_params_1 = require("@tsed/platform-params");
|
|
9
9
|
const platform_response_filter_1 = require("@tsed/platform-response-filter");
|
|
10
|
+
const platform_router_1 = require("@tsed/platform-router");
|
|
11
|
+
const util_1 = require("util");
|
|
10
12
|
const AnyToPromiseWithCtx_1 = require("../domain/AnyToPromiseWithCtx");
|
|
11
|
-
const HandlerMetadata_1 = require("../domain/HandlerMetadata");
|
|
12
|
-
const HandlerType_1 = require("../interfaces/HandlerType");
|
|
13
|
-
const createHandlerMetadata_1 = require("../utils/createHandlerMetadata");
|
|
14
13
|
const setResponseHeaders_1 = require("../utils/setResponseHeaders");
|
|
15
14
|
/**
|
|
16
15
|
* Platform Handler abstraction layer. Wrap original class method to a pure platform handler (Express, Koa, etc...).
|
|
17
16
|
* @platform
|
|
18
17
|
*/
|
|
19
18
|
let PlatformHandler = class PlatformHandler {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
createHandler(input, options = {}) {
|
|
30
|
-
return this.createRawHandler((0, createHandlerMetadata_1.createHandlerMetadata)(this.injector, input, options));
|
|
19
|
+
responseFilter;
|
|
20
|
+
platformRouters;
|
|
21
|
+
platformParams;
|
|
22
|
+
createHandler(handler, handlerMetadata) {
|
|
23
|
+
return async ($ctx) => {
|
|
24
|
+
$ctx.handlerMetadata = handlerMetadata;
|
|
25
|
+
const error = await (0, core_1.catchAsyncError)(() => this.onRequest(handler, $ctx));
|
|
26
|
+
return this.next(error, $ctx.next, $ctx);
|
|
27
|
+
};
|
|
31
28
|
}
|
|
32
29
|
/**
|
|
33
|
-
* Create injectable handler from the given provider
|
|
34
30
|
* @param provider
|
|
35
31
|
* @param propertyKey
|
|
36
32
|
*/
|
|
37
33
|
createCustomHandler(provider, propertyKey) {
|
|
38
|
-
const
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
type: HandlerType_1.HandlerType.CUSTOM,
|
|
42
|
-
scope: provider.scope,
|
|
34
|
+
const metadata = new platform_router_1.PlatformHandlerMetadata({
|
|
35
|
+
provider,
|
|
36
|
+
type: platform_router_1.PlatformHandlerType.CUSTOM,
|
|
43
37
|
propertyKey
|
|
44
|
-
}));
|
|
45
|
-
return async ($ctx, next) => handler({
|
|
46
|
-
$ctx,
|
|
47
|
-
next
|
|
48
38
|
});
|
|
39
|
+
const handler = this.platformParams.compileHandler(metadata.store);
|
|
40
|
+
return async ($ctx) => {
|
|
41
|
+
$ctx.set(platform_router_1.PlatformHandlerMetadata, metadata);
|
|
42
|
+
// @ts-ignore
|
|
43
|
+
return this.onRequest(handler, $ctx);
|
|
44
|
+
};
|
|
49
45
|
}
|
|
50
46
|
/**
|
|
51
47
|
* Send the response to the consumer.
|
|
52
|
-
* @param data
|
|
53
|
-
* @param ctx
|
|
54
48
|
* @protected
|
|
49
|
+
* @param $ctx
|
|
55
50
|
*/
|
|
56
|
-
async flush(
|
|
57
|
-
const { response } = ctx;
|
|
51
|
+
async flush($ctx) {
|
|
52
|
+
const { response } = $ctx;
|
|
58
53
|
if (!response.isDone()) {
|
|
59
|
-
data = await this.responseFilter.serialize(data, ctx);
|
|
60
|
-
data = await this.responseFilter.transform(data, ctx);
|
|
54
|
+
let data = await this.responseFilter.serialize($ctx.data, $ctx);
|
|
55
|
+
data = await this.responseFilter.transform(data, $ctx);
|
|
61
56
|
response.body(data);
|
|
62
57
|
}
|
|
58
|
+
return response;
|
|
63
59
|
}
|
|
64
|
-
|
|
65
|
-
if (
|
|
66
|
-
return
|
|
67
|
-
// @ts-ignore
|
|
68
|
-
return this.onCtxRequest({
|
|
69
|
-
...scope,
|
|
70
|
-
handler: () => metadata.handler(scope.$ctx),
|
|
71
|
-
metadata
|
|
72
|
-
});
|
|
73
|
-
};
|
|
60
|
+
setResponseHeaders($ctx) {
|
|
61
|
+
if (!$ctx.response.isDone()) {
|
|
62
|
+
return (0, setResponseHeaders_1.setResponseHeaders)($ctx);
|
|
74
63
|
}
|
|
75
|
-
const promise = this.params.compileHandler({
|
|
76
|
-
token: metadata.token,
|
|
77
|
-
propertyKey: metadata.propertyKey,
|
|
78
|
-
getCustomArgs: metadata.injectable ? undefined : this.getDefaultArgs(metadata)
|
|
79
|
-
});
|
|
80
|
-
return async (scope) => {
|
|
81
|
-
const handler = await promise;
|
|
82
|
-
// @ts-ignore
|
|
83
|
-
return this.onRequest({
|
|
84
|
-
...scope,
|
|
85
|
-
handler,
|
|
86
|
-
metadata
|
|
87
|
-
});
|
|
88
|
-
};
|
|
89
64
|
}
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
65
|
+
/**
|
|
66
|
+
* @param error
|
|
67
|
+
* @param next
|
|
68
|
+
* @param $ctx
|
|
69
|
+
*/
|
|
70
|
+
next(error, next, $ctx) {
|
|
71
|
+
if ((0, core_1.isStream)($ctx.data) || $ctx.isDone()) {
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
return error ? next(error) : next();
|
|
93
75
|
}
|
|
94
76
|
/**
|
|
95
77
|
* Call handler when a request his handle
|
|
96
|
-
* @param requestOptions
|
|
97
78
|
*/
|
|
98
|
-
async onRequest(
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
if (!$ctx || $ctx.isDone()) {
|
|
79
|
+
async onRequest(handler, $ctx) {
|
|
80
|
+
// istanbul ignore next$
|
|
81
|
+
if ($ctx.isDone()) {
|
|
102
82
|
logger_1.$log.error({
|
|
103
83
|
name: "HEADERS_SENT",
|
|
104
|
-
message: `An endpoint is called but the response is already send to the client. The call comes from the handler: ${
|
|
84
|
+
message: `An endpoint is called but the response is already send to the client. The call comes from the handler: ${$ctx.handlerMetadata.toString()}`
|
|
105
85
|
});
|
|
106
86
|
return;
|
|
107
87
|
}
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
if (state === core_1.AnyToPromiseStatus.RESOLVED) {
|
|
113
|
-
if (status) {
|
|
114
|
-
$ctx.response.status(status);
|
|
115
|
-
}
|
|
116
|
-
if (headers) {
|
|
117
|
-
$ctx.response.setHeaders(headers);
|
|
118
|
-
}
|
|
119
|
-
if (data !== undefined) {
|
|
120
|
-
$ctx.data = data;
|
|
121
|
-
}
|
|
122
|
-
// Can be canceled by the handler itself
|
|
123
|
-
return await this.onSuccess($ctx.data, requestOptions);
|
|
124
|
-
}
|
|
88
|
+
try {
|
|
89
|
+
const { handlerMetadata } = $ctx;
|
|
90
|
+
if (handlerMetadata.type === platform_router_1.PlatformHandlerType.CTX_FN) {
|
|
91
|
+
return await handler({ $ctx });
|
|
125
92
|
}
|
|
126
|
-
|
|
127
|
-
|
|
93
|
+
const resolver = new AnyToPromiseWithCtx_1.AnyToPromiseWithCtx($ctx);
|
|
94
|
+
const { state, data, status, headers } = await resolver.call(handler);
|
|
95
|
+
// Note: restore previous handler metadata (for OIDC)
|
|
96
|
+
$ctx.handlerMetadata = handlerMetadata;
|
|
97
|
+
if (state === core_1.AnyToPromiseStatus.RESOLVED) {
|
|
98
|
+
if (status) {
|
|
99
|
+
$ctx.response.status(status);
|
|
100
|
+
}
|
|
101
|
+
if (headers) {
|
|
102
|
+
$ctx.response.setHeaders(headers);
|
|
103
|
+
}
|
|
104
|
+
if (data !== undefined) {
|
|
105
|
+
$ctx.data = data;
|
|
106
|
+
}
|
|
107
|
+
// Can be canceled by the handler itself
|
|
108
|
+
return await this.onSuccess($ctx);
|
|
128
109
|
}
|
|
129
|
-
});
|
|
130
|
-
}
|
|
131
|
-
async onError(er, requestOptions) {
|
|
132
|
-
const { next, $ctx, metadata } = requestOptions;
|
|
133
|
-
if ($ctx) {
|
|
134
|
-
$ctx.data = er;
|
|
135
110
|
}
|
|
136
|
-
|
|
137
|
-
|
|
111
|
+
catch (error) {
|
|
112
|
+
return this.onError(error, $ctx);
|
|
138
113
|
}
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
message: `An error was caught after the headers were sent to the client. The error comes from the handler: ${metadata.toString()}`,
|
|
144
|
-
stack: er.stack,
|
|
145
|
-
origin: er,
|
|
146
|
-
request_id: $ctx === null || $ctx === void 0 ? void 0 : $ctx.id
|
|
147
|
-
});
|
|
148
|
-
return;
|
|
149
|
-
}
|
|
150
|
-
return next(er);
|
|
114
|
+
}
|
|
115
|
+
async onError(error, $ctx) {
|
|
116
|
+
$ctx.error = error;
|
|
117
|
+
throw error;
|
|
151
118
|
}
|
|
152
119
|
/**
|
|
153
120
|
* Manage success scenario
|
|
154
|
-
* @param
|
|
155
|
-
* @param requestOptions
|
|
121
|
+
* @param $ctx
|
|
156
122
|
* @protected
|
|
157
123
|
*/
|
|
158
|
-
async onSuccess(
|
|
159
|
-
const { metadata, $ctx, next } = requestOptions;
|
|
124
|
+
async onSuccess($ctx) {
|
|
160
125
|
// istanbul ignore next
|
|
161
|
-
if ($ctx
|
|
126
|
+
if ($ctx.isDone()) {
|
|
162
127
|
return;
|
|
163
128
|
}
|
|
129
|
+
$ctx.error = null;
|
|
130
|
+
const metadata = $ctx.handlerMetadata;
|
|
164
131
|
// set headers each times that an endpoint is called
|
|
165
|
-
if (metadata.
|
|
166
|
-
this.
|
|
132
|
+
if (metadata.isEndpoint()) {
|
|
133
|
+
this.setResponseHeaders($ctx);
|
|
167
134
|
}
|
|
168
135
|
// call returned middleware
|
|
169
|
-
if ((0, core_1.isFunction)(data) && !(0, core_1.isStream)(data)) {
|
|
170
|
-
return
|
|
136
|
+
if ((0, core_1.isFunction)($ctx.data) && !(0, core_1.isStream)($ctx.data)) {
|
|
137
|
+
return (0, util_1.promisify)($ctx.data)($ctx.getRequest(), $ctx.getResponse());
|
|
171
138
|
}
|
|
172
139
|
if (metadata.isFinal()) {
|
|
173
|
-
return this.flush(
|
|
174
|
-
}
|
|
175
|
-
return this.next(requestOptions);
|
|
176
|
-
}
|
|
177
|
-
/**
|
|
178
|
-
* Call the returned middleware by the handler.
|
|
179
|
-
* @param middleware
|
|
180
|
-
* @param ctx
|
|
181
|
-
* @param next
|
|
182
|
-
* @protected
|
|
183
|
-
*/
|
|
184
|
-
callReturnedMiddleware(middleware, ctx, next) {
|
|
185
|
-
return middleware(ctx.getRequest(), ctx.getResponse(), next);
|
|
186
|
-
}
|
|
187
|
-
/**
|
|
188
|
-
* create Raw handler
|
|
189
|
-
* @param metadata
|
|
190
|
-
*/
|
|
191
|
-
createRawHandler(metadata) {
|
|
192
|
-
if ([HandlerType_1.HandlerType.RAW_ERR_FN, HandlerType_1.HandlerType.RAW_FN].includes(metadata.type)) {
|
|
193
|
-
return metadata.handler;
|
|
140
|
+
return this.flush($ctx);
|
|
194
141
|
}
|
|
195
|
-
const handler = this.compileHandler(metadata);
|
|
196
|
-
return async (request, response, next) => {
|
|
197
|
-
return handler({
|
|
198
|
-
next,
|
|
199
|
-
$ctx: request.$ctx
|
|
200
|
-
});
|
|
201
|
-
};
|
|
202
|
-
}
|
|
203
|
-
/**
|
|
204
|
-
* Set response headers
|
|
205
|
-
* @param ctx
|
|
206
|
-
* @protected
|
|
207
|
-
*/
|
|
208
|
-
setHeaders(ctx) {
|
|
209
|
-
return (0, setResponseHeaders_1.setResponseHeaders)(ctx);
|
|
210
|
-
}
|
|
211
|
-
next(requestOptions) {
|
|
212
|
-
const { $ctx, next } = requestOptions;
|
|
213
|
-
return !$ctx.response.isDone() && next && next();
|
|
214
|
-
}
|
|
215
|
-
getDefaultArgs(metadata) {
|
|
216
|
-
return async (scope) => [
|
|
217
|
-
metadata.hasErrorParam && scope.err,
|
|
218
|
-
scope.$ctx.request.request,
|
|
219
|
-
scope.$ctx.response.response,
|
|
220
|
-
metadata.hasNextFunction && scope.next
|
|
221
|
-
].filter(Boolean);
|
|
222
142
|
}
|
|
223
143
|
};
|
|
224
144
|
tslib_1.__decorate([
|
|
225
145
|
(0, di_1.Inject)(),
|
|
226
146
|
tslib_1.__metadata("design:type", platform_response_filter_1.PlatformResponseFilter)
|
|
227
147
|
], PlatformHandler.prototype, "responseFilter", void 0);
|
|
148
|
+
tslib_1.__decorate([
|
|
149
|
+
(0, di_1.Inject)(),
|
|
150
|
+
tslib_1.__metadata("design:type", platform_router_1.PlatformRouters)
|
|
151
|
+
], PlatformHandler.prototype, "platformRouters", void 0);
|
|
152
|
+
tslib_1.__decorate([
|
|
153
|
+
(0, di_1.Inject)(),
|
|
154
|
+
tslib_1.__metadata("design:type", platform_params_1.PlatformParams)
|
|
155
|
+
], PlatformHandler.prototype, "platformParams", void 0);
|
|
228
156
|
PlatformHandler = tslib_1.__decorate([
|
|
229
157
|
(0, di_1.Injectable)({
|
|
230
158
|
scope: di_1.ProviderScope.SINGLETON
|
|
231
|
-
})
|
|
232
|
-
tslib_1.__metadata("design:paramtypes", [di_1.InjectorService, platform_params_1.PlatformParams])
|
|
159
|
+
})
|
|
233
160
|
], PlatformHandler);
|
|
234
161
|
exports.PlatformHandler = PlatformHandler;
|
|
235
162
|
//# sourceMappingURL=PlatformHandler.js.map
|
|
@@ -1 +1 @@
|
|
|
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,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;YACN,IAAI;YACJ,IAAI;SACL,CAAC,CAAC;IACP,CAAC;IAED;;;;;OAKG;IACH,KAAK,CAAC,KAAK,CAAC,IAAS,EAAE,GAAoB;QACzC,MAAM,EAAC,QAAQ,EAAC,GAAG,GAAG,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AC,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"]}
|
|
1
|
+
{"version":3,"file":"PlatformHandler.js","sourceRoot":"","sources":["../../../src/services/PlatformHandler.ts"],"names":[],"mappings":";;;;AAAA,qCAAqF;AACrF,iCAAqE;AACrE,yCAAkC;AAClC,2DAA6E;AAC7E,6EAAsE;AACtE,2DAA4H;AAC5H,+BAA+B;AAC/B,uEAAkE;AAElE,oEAA+D;AAE/D;;;GAGG;AAIH,IAAa,eAAe,GAA5B,MAAa,eAAe;IAEhB,cAAc,CAAyB;IAGvC,eAAe,CAAkB;IAGjC,cAAc,CAAiB;IAEzC,aAAa,CAAC,OAA+B,EAAE,eAAwC;QACrF,OAAO,KAAK,EAAE,IAAqB,EAAE,EAAE;YACrC,IAAI,CAAC,eAAe,GAAG,eAAe,CAAC;YAEvC,MAAM,KAAK,GAAG,MAAM,IAAA,sBAAe,EAAC,GAAG,EAAE,CAAC,IAAI,CAAC,SAAS,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC,CAAC;YAEzE,OAAO,IAAI,CAAC,IAAI,CAAC,KAAK,EAAE,IAAI,CAAC,IAAI,EAA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V,KAAK,EAAE,kBAAa,CAAC,SAAS;KAC/B,CAAC;GACW,eAAe,CAgK3B;AAhKY,0CAAe","sourcesContent":["import {AnyToPromiseStatus, catchAsyncError, isFunction, isStream} from \"@tsed/core\";\nimport {Inject, Injectable, Provider, ProviderScope} from \"@tsed/di\";\nimport {$log} from \"@tsed/logger\";\nimport {PlatformParams, PlatformParamsCallback} from \"@tsed/platform-params\";\nimport {PlatformResponseFilter} from \"@tsed/platform-response-filter\";\nimport {PlatformContextHandler, PlatformHandlerMetadata, PlatformHandlerType, PlatformRouters} from \"@tsed/platform-router\";\nimport {promisify} from \"util\";\nimport {AnyToPromiseWithCtx} from \"../domain/AnyToPromiseWithCtx\";\nimport {PlatformContext} from \"../domain/PlatformContext\";\nimport {setResponseHeaders} from \"../utils/setResponseHeaders\";\n\n/**\n * Platform Handler abstraction layer. Wrap original class method to a pure platform handler (Express, Koa, etc...).\n * @platform\n */\n@Injectable({\n scope: ProviderScope.SINGLETON\n})\nexport class PlatformHandler {\n @Inject()\n protected responseFilter: PlatformResponseFilter;\n\n @Inject()\n protected platformRouters: PlatformRouters;\n\n @Inject()\n protected platformParams: PlatformParams;\n\n createHandler(handler: PlatformParamsCallback, handlerMetadata: PlatformHandlerMetadata): PlatformContextHandler {\n return async ($ctx: PlatformContext) => {\n $ctx.handlerMetadata = handlerMetadata;\n\n const error = await catchAsyncError(() => this.onRequest(handler, $ctx));\n\n return this.next(error, $ctx.next, $ctx);\n };\n }\n\n /**\n * @param provider\n * @param propertyKey\n */\n createCustomHandler(provider: Provider, propertyKey: string) {\n const metadata = new PlatformHandlerMetadata({\n provider,\n type: PlatformHandlerType.CUSTOM,\n propertyKey\n });\n\n const handler = this.platformParams.compileHandler(metadata.store);\n\n return async ($ctx: PlatformContext) => {\n $ctx.set(PlatformHandlerMetadata, metadata);\n\n // @ts-ignore\n return this.onRequest(handler, $ctx);\n };\n }\n\n /**\n * Send the response to the consumer.\n * @protected\n * @param $ctx\n */\n async flush($ctx: PlatformContext) {\n const {response} = $ctx;\n\n if (!response.isDone()) {\n let data = await this.responseFilter.serialize($ctx.data, $ctx as any);\n data = await this.responseFilter.transform(data, $ctx as any);\n response.body(data);\n }\n\n return response;\n }\n\n public setResponseHeaders($ctx: PlatformContext) {\n if (!$ctx.response.isDone()) {\n return setResponseHeaders($ctx);\n }\n }\n\n /**\n * @param error\n * @param next\n * @param $ctx\n */\n next(error: unknown, next: Function, $ctx: PlatformContext) {\n if (isStream($ctx.data) || $ctx.isDone()) {\n return;\n }\n\n return error ? next(error) : next();\n }\n\n /**\n * Call handler when a request his handle\n */\n async onRequest(handler: PlatformParamsCallback, $ctx: PlatformContext): Promise<any> {\n // istanbul ignore next$\n if ($ctx.isDone()) {\n $log.error({\n name: \"HEADERS_SENT\",\n message: `An endpoint is called but the response is already send to the client. The call comes from the handler: ${$ctx.handlerMetadata.toString()}`\n });\n return;\n }\n\n try {\n const {handlerMetadata} = $ctx;\n if (handlerMetadata.type === PlatformHandlerType.CTX_FN) {\n return await handler({$ctx});\n }\n\n const resolver = new AnyToPromiseWithCtx($ctx);\n\n const {state, data, status, headers} = await resolver.call(handler);\n\n // Note: restore previous handler metadata (for OIDC)\n $ctx.handlerMetadata = handlerMetadata;\n\n if (state === AnyToPromiseStatus.RESOLVED) {\n if (status) {\n $ctx.response.status(status);\n }\n\n if (headers) {\n $ctx.response.setHeaders(headers);\n }\n\n if (data !== undefined) {\n $ctx.data = data;\n }\n\n // Can be canceled by the handler itself\n return await this.onSuccess($ctx);\n }\n } catch (error) {\n return this.onError(error, $ctx);\n }\n }\n\n protected async onError(error: unknown, $ctx: PlatformContext) {\n $ctx.error = error;\n\n throw error;\n }\n\n /**\n * Manage success scenario\n * @param $ctx\n * @protected\n */\n protected async onSuccess($ctx: PlatformContext) {\n // istanbul ignore next\n if ($ctx.isDone()) {\n return;\n }\n\n $ctx.error = null;\n\n const metadata = $ctx.handlerMetadata;\n\n // set headers each times that an endpoint is called\n\n if (metadata.isEndpoint()) {\n this.setResponseHeaders($ctx);\n }\n\n // call returned middleware\n if (isFunction($ctx.data) && !isStream($ctx.data)) {\n return promisify($ctx.data)($ctx.getRequest(), $ctx.getResponse());\n }\n\n if (metadata.isFinal()) {\n return this.flush($ctx);\n }\n }\n}\n"]}
|
|
@@ -2,33 +2,19 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.PlatformMiddlewaresChain = void 0;
|
|
4
4
|
const tslib_1 = require("tslib");
|
|
5
|
+
const core_1 = require("@tsed/core");
|
|
5
6
|
const di_1 = require("@tsed/di");
|
|
6
7
|
const platform_params_1 = require("@tsed/platform-params");
|
|
7
8
|
const schema_1 = require("@tsed/schema");
|
|
8
|
-
const bindEndpointMiddleware_1 = require("../middlewares/bindEndpointMiddleware");
|
|
9
9
|
const PlatformAcceptMimesMiddleware_1 = require("../middlewares/PlatformAcceptMimesMiddleware");
|
|
10
10
|
const PlatformMulterMiddleware_1 = require("../middlewares/PlatformMulterMiddleware");
|
|
11
|
-
const useCtxHandler_1 = require("../utils/useCtxHandler");
|
|
12
11
|
const PlatformAdapter_1 = require("../services/PlatformAdapter");
|
|
13
|
-
const core_1 = require("@tsed/core");
|
|
14
12
|
let PlatformMiddlewaresChain = class PlatformMiddlewaresChain {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
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
|
-
];
|
|
13
|
+
acceptMimes;
|
|
14
|
+
adapter;
|
|
15
|
+
get(allMiddlewares, operationRoute) {
|
|
29
16
|
const { ACCEPT_MIMES, FILE, RAW_BODY, BODY } = this.getParamTypes(allMiddlewares, operationRoute);
|
|
30
17
|
return [
|
|
31
|
-
(0, useCtxHandler_1.useCtxHandler)((0, bindEndpointMiddleware_1.bindEndpointMiddleware)(endpoint)),
|
|
32
18
|
ACCEPT_MIMES && PlatformAcceptMimesMiddleware_1.PlatformAcceptMimesMiddleware,
|
|
33
19
|
FILE && PlatformMulterMiddleware_1.PlatformMulterMiddleware,
|
|
34
20
|
!FILE && RAW_BODY && this.adapter.bodyParser("raw"),
|
|
@@ -56,7 +42,7 @@ let PlatformMiddlewaresChain = class PlatformMiddlewaresChain {
|
|
|
56
42
|
ACCEPT_MIMES: this.hasAcceptMimes(operationRoute),
|
|
57
43
|
FILE: operationRoute.has(platform_params_1.ParamTypes.FILES),
|
|
58
44
|
RAW_BODY: operationRoute.has(platform_params_1.ParamTypes.RAW_BODY),
|
|
59
|
-
BODY: operationRoute.has(platform_params_1.ParamTypes.BODY)
|
|
45
|
+
BODY: operationRoute.has(platform_params_1.ParamTypes.BODY) || operationRoute.method === "ALL"
|
|
60
46
|
});
|
|
61
47
|
}
|
|
62
48
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"PlatformMiddlewaresChain.js","sourceRoot":"","sources":["../../../src/services/PlatformMiddlewaresChain.ts"],"names":[],"mappings":";;;;AAAA,iCAAsD;AACtD,2DAAiD;AACjD,yCAAiE;
|
|
1
|
+
{"version":3,"file":"PlatformMiddlewaresChain.js","sourceRoot":"","sources":["../../../src/services/PlatformMiddlewaresChain.ts"],"names":[],"mappings":";;;;AAAA,qCAAmC;AACnC,iCAAsD;AACtD,2DAAiD;AACjD,yCAAiE;AACjE,gGAA2F;AAC3F,sFAAiF;AACjF,iEAA4D;AAG5D,IAAa,wBAAwB,GAArC,MAAa,wBAAwB;IAEzB,WAAW,CAAW;IAGtB,OAAO,CAAkB;IAEnC,GAAG,CAAC,cAAqB,EAAE,cAAkC;QAC3D,MAAM,EAAC,YAAY,EAAE,IAAI,EAAE,QAAQ,EAAE,IAAI,EAAC,GAAG,IAA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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,IAAI,cAAc,CAAC,MAAM,KAAK,KAAK;SAC7E,CACF,CAAC;IACJ,CAAC;CACF,CAAA;AA9CC;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,CAgDpC;AAhDY,4DAAwB","sourcesContent":["import {isClass} from \"@tsed/core\";\nimport {Constant, Inject, Injectable} from \"@tsed/di\";\nimport {ParamTypes} from \"@tsed/platform-params\";\nimport {JsonEntityStore, JsonOperationRoute} from \"@tsed/schema\";\nimport {PlatformAcceptMimesMiddleware} from \"../middlewares/PlatformAcceptMimesMiddleware\";\nimport {PlatformMulterMiddleware} from \"../middlewares/PlatformMulterMiddleware\";\nimport {PlatformAdapter} from \"../services/PlatformAdapter\";\n\n@Injectable()\nexport class PlatformMiddlewaresChain {\n @Constant(\"acceptMimes\", [])\n protected acceptMimes: string[];\n\n @Inject()\n protected adapter: PlatformAdapter;\n\n get(allMiddlewares: any[], operationRoute: JsonOperationRoute) {\n const {ACCEPT_MIMES, FILE, RAW_BODY, BODY} = this.getParamTypes(allMiddlewares, operationRoute);\n\n return [\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) || operationRoute.method === \"ALL\"\n }\n );\n }\n}\n"]}
|