@tsed/platform-koa 6.88.1 → 6.89.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/{components → cjs/components}/PlatformKoa.d.ts +0 -0
- package/lib/{components → cjs/components}/PlatformKoa.js +1 -1
- package/lib/cjs/components/PlatformKoa.js.map +1 -0
- package/lib/{decorators → cjs/decorators}/caseSensitive.d.ts +0 -0
- package/lib/{decorators → cjs/decorators}/caseSensitive.js +1 -1
- package/lib/cjs/decorators/caseSensitive.js.map +1 -0
- package/lib/{decorators → cjs/decorators}/ctx.d.ts +0 -0
- package/lib/{decorators → cjs/decorators}/ctx.js +1 -1
- package/lib/cjs/decorators/ctx.js.map +1 -0
- package/lib/{decorators → cjs/decorators}/index.d.ts +0 -0
- package/lib/cjs/decorators/index.js +9 -0
- package/lib/cjs/decorators/index.js.map +1 -0
- package/lib/{decorators → cjs/decorators}/routerSettings.d.ts +0 -0
- package/lib/{decorators → cjs/decorators}/routerSettings.js +1 -1
- package/lib/cjs/decorators/routerSettings.js.map +1 -0
- package/lib/{decorators → cjs/decorators}/state.d.ts +0 -0
- package/lib/{decorators → cjs/decorators}/state.js +0 -0
- package/lib/cjs/decorators/state.js.map +1 -0
- package/lib/{decorators → cjs/decorators}/strict.d.ts +0 -0
- package/lib/{decorators → cjs/decorators}/strict.js +1 -1
- package/lib/cjs/decorators/strict.js.map +1 -0
- package/lib/{index.d.ts → cjs/index.d.ts} +0 -0
- package/lib/{index.js → cjs/index.js} +4 -4
- package/lib/cjs/index.js.map +1 -0
- package/lib/{interfaces → cjs/interfaces}/PlatformKoaSettings.d.ts +0 -0
- package/lib/{interfaces → cjs/interfaces}/PlatformKoaSettings.js +0 -0
- package/lib/cjs/interfaces/PlatformKoaSettings.js.map +1 -0
- package/lib/{interfaces → cjs/interfaces}/index.d.ts +0 -0
- package/lib/{interfaces → cjs/interfaces}/index.js +1 -1
- package/lib/cjs/interfaces/index.js.map +1 -0
- package/lib/{middlewares → cjs/middlewares}/resourceNotFoundMiddleware.d.ts +0 -0
- package/lib/{middlewares → cjs/middlewares}/resourceNotFoundMiddleware.js +0 -0
- package/lib/cjs/middlewares/resourceNotFoundMiddleware.js.map +1 -0
- package/lib/{middlewares → cjs/middlewares}/staticsMiddleware.d.ts +0 -0
- package/lib/{middlewares → cjs/middlewares}/staticsMiddleware.js +2 -2
- package/lib/cjs/middlewares/staticsMiddleware.js.map +1 -0
- package/lib/{services → cjs/services}/PlatformKoaApplication.d.ts +0 -0
- package/lib/{services → cjs/services}/PlatformKoaApplication.js +5 -4
- package/lib/cjs/services/PlatformKoaApplication.js.map +1 -0
- package/lib/{services → cjs/services}/PlatformKoaHandler.d.ts +0 -0
- package/lib/{services → cjs/services}/PlatformKoaHandler.js +0 -0
- package/lib/cjs/services/PlatformKoaHandler.js.map +1 -0
- package/lib/{services → cjs/services}/PlatformKoaRequest.d.ts +0 -0
- package/lib/cjs/services/PlatformKoaRequest.js +44 -0
- package/lib/cjs/services/PlatformKoaRequest.js.map +1 -0
- package/lib/{services → cjs/services}/PlatformKoaResponse.d.ts +0 -0
- package/lib/{services → cjs/services}/PlatformKoaResponse.js +6 -6
- package/lib/cjs/services/PlatformKoaResponse.js.map +1 -0
- package/lib/{services → cjs/services}/PlatformKoaRouter.d.ts +0 -0
- package/lib/{services → cjs/services}/PlatformKoaRouter.js +7 -7
- package/lib/cjs/services/PlatformKoaRouter.js.map +1 -0
- package/lib/{services → cjs/services}/index.d.ts +0 -0
- package/lib/cjs/services/index.js +9 -0
- package/lib/cjs/services/index.js.map +1 -0
- package/lib/{utils → cjs/utils}/multer.d.ts +0 -0
- package/lib/{utils → cjs/utils}/multer.js +1 -1
- package/lib/cjs/utils/multer.js.map +1 -0
- package/lib/esm/components/PlatformKoa.js +74 -0
- package/lib/esm/components/PlatformKoa.js.map +1 -0
- package/lib/esm/decorators/caseSensitive.js +26 -0
- package/lib/esm/decorators/caseSensitive.js.map +1 -0
- package/lib/esm/decorators/ctx.js +27 -0
- package/lib/esm/decorators/ctx.js.map +1 -0
- package/lib/esm/decorators/index.js +6 -0
- package/lib/esm/decorators/index.js.map +1 -0
- package/lib/esm/decorators/routerSettings.js +27 -0
- package/lib/esm/decorators/routerSettings.js.map +1 -0
- package/lib/esm/decorators/state.js +5 -0
- package/lib/esm/decorators/state.js.map +1 -0
- package/lib/esm/decorators/strict.js +27 -0
- package/lib/esm/decorators/strict.js.map +1 -0
- package/lib/esm/index.js +13 -0
- package/lib/esm/index.js.map +1 -0
- package/lib/esm/interfaces/PlatformKoaSettings.js +2 -0
- package/lib/esm/interfaces/PlatformKoaSettings.js.map +1 -0
- package/lib/esm/interfaces/index.js +2 -0
- package/lib/esm/interfaces/index.js.map +1 -0
- package/lib/esm/middlewares/resourceNotFoundMiddleware.js +16 -0
- package/lib/esm/middlewares/resourceNotFoundMiddleware.js.map +1 -0
- package/lib/esm/middlewares/staticsMiddleware.js +30 -0
- package/lib/esm/middlewares/staticsMiddleware.js.map +1 -0
- package/lib/esm/services/PlatformKoaApplication.js +29 -0
- package/lib/esm/services/PlatformKoaApplication.js.map +1 -0
- package/lib/esm/services/PlatformKoaHandler.js +56 -0
- package/lib/esm/services/PlatformKoaHandler.js.map +1 -0
- package/lib/esm/services/PlatformKoaRequest.js +40 -0
- package/lib/esm/services/PlatformKoaRequest.js.map +1 -0
- package/lib/esm/services/PlatformKoaResponse.js +101 -0
- package/lib/esm/services/PlatformKoaResponse.js.map +1 -0
- package/lib/esm/services/PlatformKoaRouter.js +42 -0
- package/lib/esm/services/PlatformKoaRouter.js.map +1 -0
- package/lib/esm/services/index.js +6 -0
- package/lib/esm/services/index.js.map +1 -0
- package/lib/esm/utils/multer.js +51 -0
- package/lib/esm/utils/multer.js.map +1 -0
- package/lib/types/components/PlatformKoa.d.ts +38 -0
- package/lib/types/decorators/caseSensitive.d.ts +22 -0
- package/lib/types/decorators/ctx.d.ts +19 -0
- package/lib/types/decorators/index.d.ts +5 -0
- package/lib/types/decorators/routerSettings.d.ts +24 -0
- package/lib/types/decorators/state.d.ts +38 -0
- package/lib/types/decorators/strict.d.ts +23 -0
- package/lib/types/index.d.ts +4 -0
- package/lib/types/interfaces/PlatformKoaSettings.d.ts +8 -0
- package/lib/types/interfaces/index.d.ts +15 -0
- package/lib/types/middlewares/resourceNotFoundMiddleware.d.ts +6 -0
- package/lib/types/middlewares/staticsMiddleware.d.ts +6 -0
- package/lib/types/services/PlatformKoaApplication.d.ts +22 -0
- package/lib/types/services/PlatformKoaHandler.d.ts +10 -0
- package/lib/types/services/PlatformKoaRequest.d.ts +34 -0
- package/lib/types/services/PlatformKoaResponse.d.ts +57 -0
- package/lib/types/services/PlatformKoaRouter.d.ts +22 -0
- package/lib/types/services/index.d.ts +5 -0
- package/lib/types/utils/multer.d.ts +4 -0
- package/package.json +26 -18
- package/tsconfig.compile.esm.json +23 -0
- package/lib/components/PlatformKoa.js.map +0 -1
- package/lib/decorators/caseSensitive.js.map +0 -1
- package/lib/decorators/ctx.js.map +0 -1
- package/lib/decorators/index.js +0 -9
- package/lib/decorators/index.js.map +0 -1
- package/lib/decorators/routerSettings.js.map +0 -1
- package/lib/decorators/state.js.map +0 -1
- package/lib/decorators/strict.js.map +0 -1
- package/lib/index.js.map +0 -1
- package/lib/interfaces/PlatformKoaSettings.js.map +0 -1
- package/lib/interfaces/index.js.map +0 -1
- package/lib/middlewares/resourceNotFoundMiddleware.js.map +0 -1
- package/lib/middlewares/staticsMiddleware.js.map +0 -1
- package/lib/services/PlatformKoaApplication.js.map +0 -1
- package/lib/services/PlatformKoaHandler.js.map +0 -1
- package/lib/services/PlatformKoaRequest.js +0 -44
- package/lib/services/PlatformKoaRequest.js.map +0 -1
- package/lib/services/PlatformKoaResponse.js.map +0 -1
- package/lib/services/PlatformKoaRouter.js.map +0 -1
- package/lib/services/index.js +0 -9
- package/lib/services/index.js.map +0 -1
- package/lib/utils/multer.js.map +0 -1
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { UseParam } from "@tsed/common";
|
|
2
|
+
/**
|
|
3
|
+
* Return the original Koa context.
|
|
4
|
+
* @decorator
|
|
5
|
+
* @operation
|
|
6
|
+
* @input
|
|
7
|
+
* @koa
|
|
8
|
+
*/
|
|
9
|
+
export function KoaCtx() {
|
|
10
|
+
return UseParam("KOA_CTX", {
|
|
11
|
+
paramType: "KOA_CTX",
|
|
12
|
+
dataPath: "$ctx.request.ctx",
|
|
13
|
+
useConverter: false,
|
|
14
|
+
useValidation: false
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Return the original Koa context.
|
|
19
|
+
* @decorator
|
|
20
|
+
* @operation
|
|
21
|
+
* @input
|
|
22
|
+
* @koa
|
|
23
|
+
*/
|
|
24
|
+
export function Ctx() {
|
|
25
|
+
return KoaCtx();
|
|
26
|
+
}
|
|
27
|
+
//# sourceMappingURL=ctx.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ctx.js","sourceRoot":"","sources":["../../../src/decorators/ctx.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,QAAQ,EAAC,MAAM,cAAc,CAAC;AAGtC;;;;;;GAMG;AACH,MAAM,UAAU,MAAM;IACpB,OAAO,QAAQ,CAAC,SAAS,EAAE;QACzB,SAAS,EAAE,SAAS;QACpB,QAAQ,EAAE,kBAAkB;QAC5B,YAAY,EAAE,KAAK;QACnB,aAAa,EAAE,KAAK;KACrB,CAAC,CAAC;AACL,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,GAAG;IACjB,OAAO,MAAM,EAAE,CAAC;AAClB,CAAC","sourcesContent":["import {UseParam} from \"@tsed/common\";\nimport type {Context} from \"koa\";\n\n/**\n * Return the original Koa context.\n * @decorator\n * @operation\n * @input\n * @koa\n */\nexport function KoaCtx(): ParameterDecorator {\n return UseParam(\"KOA_CTX\", {\n paramType: \"KOA_CTX\",\n dataPath: \"$ctx.request.ctx\",\n useConverter: false,\n useValidation: false\n });\n}\n\n/**\n * Return the original Koa context.\n * @decorator\n * @operation\n * @input\n * @koa\n */\nexport function Ctx(): ParameterDecorator {\n return KoaCtx();\n}\n\nexport type Ctx = Context;\nexport type KoaCtx = Context;\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/decorators/index.ts"],"names":[],"mappings":"AAAA,cAAc,kBAAkB,CAAC;AACjC,cAAc,iBAAiB,CAAC;AAChC,cAAc,UAAU,CAAC;AACzB,cAAc,SAAS,CAAC;AACxB,cAAc,OAAO,CAAC","sourcesContent":["export * from \"./routerSettings\";\nexport * from \"./caseSensitive\";\nexport * from \"./strict\";\nexport * from \"./state\";\nexport * from \"./ctx\";\n"]}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { ROUTER_OPTIONS } from "@tsed/common";
|
|
2
|
+
import { StoreMerge } from "@tsed/core";
|
|
3
|
+
/**
|
|
4
|
+
* Specify the behavior of the router controller.
|
|
5
|
+
*
|
|
6
|
+
* ```typescript
|
|
7
|
+
* @Controller("/")
|
|
8
|
+
* @RouterSettings({sensitive: true})
|
|
9
|
+
* class MyCtrl {
|
|
10
|
+
*
|
|
11
|
+
* }
|
|
12
|
+
* ```
|
|
13
|
+
*
|
|
14
|
+
* Property | Description | Default
|
|
15
|
+
* ---|---|---
|
|
16
|
+
* sensitive | Enable case sensitivity. | Disabled by default, treating “/Foo” and “/foo” as the same.
|
|
17
|
+
* strict | Enable strict routing. | Disabled by default, “/foo” and “/foo/” are treated the same by the router.
|
|
18
|
+
*
|
|
19
|
+
* @returns {(target:any)=>void}
|
|
20
|
+
* @decorator
|
|
21
|
+
* @param routerOptions
|
|
22
|
+
* @koa
|
|
23
|
+
*/
|
|
24
|
+
export function RouterSettings(routerOptions) {
|
|
25
|
+
return StoreMerge(ROUTER_OPTIONS, routerOptions);
|
|
26
|
+
}
|
|
27
|
+
//# sourceMappingURL=routerSettings.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"routerSettings.js","sourceRoot":"","sources":["../../../src/decorators/routerSettings.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,cAAc,EAAC,MAAM,cAAc,CAAC;AAC5C,OAAO,EAAC,UAAU,EAAC,MAAM,YAAY,CAAC;AAGtC;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,MAAM,UAAU,cAAc,CAAC,aAA4B;IACzD,OAAO,UAAU,CAAC,cAAc,EAAE,aAAa,CAAC,CAAC;AACnD,CAAC","sourcesContent":["import {ROUTER_OPTIONS} from \"@tsed/common\";\nimport {StoreMerge} from \"@tsed/core\";\nimport {RouterOptions} from \"@koa/router\";\n\n/**\n * Specify the behavior of the router controller.\n *\n * ```typescript\n * @Controller(\"/\")\n * @RouterSettings({sensitive: true})\n * class MyCtrl {\n *\n * }\n * ```\n *\n * Property | Description | Default\n * ---|---|---\n * sensitive | Enable case sensitivity. | Disabled by default, treating “/Foo” and “/foo” as the same.\n * strict | Enable strict routing. | Disabled by default, “/foo” and “/foo/” are treated the same by the router.\n *\n * @returns {(target:any)=>void}\n * @decorator\n * @param routerOptions\n * @koa\n */\nexport function RouterSettings(routerOptions: RouterOptions): Function {\n return StoreMerge(ROUTER_OPTIONS, routerOptions);\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"state.js","sourceRoot":"","sources":["../../../src/decorators/state.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,MAAM,EAAC,MAAM,cAAc,CAAC;AAwCpC,MAAM,UAAU,KAAK,CAAC,GAAG,IAAW;IAClC,OAAQ,MAAc,CAAC,GAAG,IAAI,CAAC,CAAC;AAClC,CAAC","sourcesContent":["import {Locals} from \"@tsed/common\";\n\n/**\n * State return the value from koa.ctx.state object.\n *\n * #### Example\n *\n * ```typescript\n * * @Middleware()\n * class StateMiddleware {\n * use(@State() state: any) {\n * // set some on locals\n * state.user = \"user\"\n * }\n * }\n *\n * @Controller('/')\n * @UseBefore(StateMiddleware)\n * class MyCtrl {\n * @Get('/')\n * @View('home.ejs') // will use locals and returned data to render the page\n * get(@State('user') user: any) {\n * console.log('user', user);\n *\n * return {\n * description: 'Hello world'\n * }\n * }\n * }\n * ```\n *\n * @param expression The path of the property to get.\n * @decorator\n * @operation\n * @input\n * @response\n * @koa\n */\nexport function State(expression: string): ParameterDecorator;\nexport function State(): ParameterDecorator;\nexport function State(...args: any[]): ParameterDecorator {\n return (Locals as any)(...args);\n}\n"]}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { RouterSettings } from "./routerSettings";
|
|
2
|
+
/**
|
|
3
|
+
* Specify the behavior of the router controller.
|
|
4
|
+
*
|
|
5
|
+
* ```typescript
|
|
6
|
+
* @Controller("/")
|
|
7
|
+
* @Strict(true)
|
|
8
|
+
* class MyCtrl {
|
|
9
|
+
*
|
|
10
|
+
* }
|
|
11
|
+
* ```
|
|
12
|
+
*
|
|
13
|
+
* Property | Description | Default
|
|
14
|
+
* ---|---|---
|
|
15
|
+
* CaseSensitive | Enable case sensitivity. | Disabled by default, treating “/Foo” and “/foo” as the same.
|
|
16
|
+
* MergeParams | Preserve the req.params values from the parent router. If the parent and the child have conflicting param names, the child’s value take precedence. | false
|
|
17
|
+
* Strict | Enable strict routing. | Disabled by default, “/foo” and “/foo/” are treated the same by the router.
|
|
18
|
+
*
|
|
19
|
+
* @param strict
|
|
20
|
+
* @returns {Function}
|
|
21
|
+
* @decorator
|
|
22
|
+
* @koa
|
|
23
|
+
*/
|
|
24
|
+
export function Strict(strict) {
|
|
25
|
+
return RouterSettings({ strict });
|
|
26
|
+
}
|
|
27
|
+
//# sourceMappingURL=strict.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"strict.js","sourceRoot":"","sources":["../../../src/decorators/strict.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,cAAc,EAAC,MAAM,kBAAkB,CAAC;AAEhD;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,MAAM,UAAU,MAAM,CAAC,MAAe;IACpC,OAAO,cAAc,CAAC,EAAC,MAAM,EAAC,CAAC,CAAC;AAClC,CAAC","sourcesContent":["import {RouterSettings} from \"./routerSettings\";\n\n/**\n * Specify the behavior of the router controller.\n *\n * ```typescript\n * @Controller(\"/\")\n * @Strict(true)\n * class MyCtrl {\n *\n * }\n * ```\n *\n * Property | Description | Default\n * ---|---|---\n * CaseSensitive | Enable case sensitivity. | Disabled by default, treating “/Foo” and “/foo” as the same.\n * MergeParams | Preserve the req.params values from the parent router. If the parent and the child have conflicting param names, the child’s value take precedence. | false\n * Strict | Enable strict routing. | Disabled by default, “/foo” and “/foo/” are treated the same by the router.\n *\n * @param strict\n * @returns {Function}\n * @decorator\n * @koa\n */\nexport function Strict(strict: boolean) {\n return RouterSettings({strict});\n}\n"]}
|
package/lib/esm/index.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { PlatformBuilder, PlatformTest } from "@tsed/common";
|
|
2
|
+
import { PlatformKoa } from "./components/PlatformKoa";
|
|
3
|
+
PlatformTest.platformBuilder = PlatformKoa;
|
|
4
|
+
PlatformBuilder.currentPlatform = PlatformKoa;
|
|
5
|
+
// interfaces
|
|
6
|
+
export * from "./interfaces";
|
|
7
|
+
// decorators
|
|
8
|
+
export * from "./decorators";
|
|
9
|
+
// services
|
|
10
|
+
export * from "./services";
|
|
11
|
+
// components
|
|
12
|
+
export * from "./components/PlatformKoa";
|
|
13
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,eAAe,EAAE,YAAY,EAAC,MAAM,cAAc,CAAC;AAC3D,OAAO,EAAC,WAAW,EAAC,MAAM,0BAA0B,CAAC;AAErD,YAAY,CAAC,eAAe,GAAG,WAAW,CAAC;AAC3C,eAAe,CAAC,eAAe,GAAG,WAAkB,CAAC;AAErD,aAAa;AACb,cAAc,cAAc,CAAC;AAE7B,aAAa;AACb,cAAc,cAAc,CAAC;AAE7B,WAAW;AACX,cAAc,YAAY,CAAC;AAE3B,aAAa;AACb,cAAc,0BAA0B,CAAC","sourcesContent":["import {PlatformBuilder, PlatformTest} from \"@tsed/common\";\nimport {PlatformKoa} from \"./components/PlatformKoa\";\n\nPlatformTest.platformBuilder = PlatformKoa;\nPlatformBuilder.currentPlatform = PlatformKoa as any;\n\n// interfaces\nexport * from \"./interfaces\";\n\n// decorators\nexport * from \"./decorators\";\n\n// services\nexport * from \"./services\";\n\n// components\nexport * from \"./components/PlatformKoa\";\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"PlatformKoaSettings.js","sourceRoot":"","sources":["../../../src/interfaces/PlatformKoaSettings.ts"],"names":[],"mappings":"","sourcesContent":["import {RouterOptions} from \"@koa/router\";\n\nexport interface PlatformKoaSettings {\n /**\n * Global configuration for the Express.Router. See express [documentation](http://expressjs.com/en/api.html#express.router).\n */\n router: RouterOptions;\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/interfaces/index.ts"],"names":[],"mappings":"AAGA,cAAc,uBAAuB,CAAC","sourcesContent":["import Koa from \"koa\";\nimport {PlatformKoaSettings} from \"./PlatformKoaSettings\";\n\nexport * from \"./PlatformKoaSettings\";\n\ndeclare global {\n namespace TsED {\n export interface Configuration {\n /**\n * Configuration related to Koa platform application.\n */\n koa: PlatformKoaSettings;\n }\n\n export interface NextFunction extends Koa.Next {}\n }\n}\n"]}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { PlatformExceptions } from "@tsed/common";
|
|
2
|
+
/**
|
|
3
|
+
* @ignore
|
|
4
|
+
* @param ctx
|
|
5
|
+
* @param next
|
|
6
|
+
*/
|
|
7
|
+
export async function resourceNotFoundMiddleware(ctx, next) {
|
|
8
|
+
var _a;
|
|
9
|
+
const $ctx = ctx.request.$ctx;
|
|
10
|
+
await next();
|
|
11
|
+
const status = ctx.status || 404;
|
|
12
|
+
if (status === 404 && !$ctx.isDone()) {
|
|
13
|
+
(_a = $ctx.injector.get(PlatformExceptions)) === null || _a === void 0 ? void 0 : _a.resourceNotFound($ctx);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
//# sourceMappingURL=resourceNotFoundMiddleware.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"resourceNotFoundMiddleware.js","sourceRoot":"","sources":["../../../src/middlewares/resourceNotFoundMiddleware.ts"],"names":[],"mappings":"AAAA,OAAO,EAAkB,kBAAkB,EAAC,MAAM,cAAc,CAAC;AAEjE;;;;GAIG;AACH,MAAM,CAAC,KAAK,UAAU,0BAA0B,CAAC,GAAQ,EAAE,IAAS;;IAClE,MAAM,IAAI,GAAoB,GAAG,CAAC,OAAO,CAAC,IAAI,CAAC;IAC/C,MAAM,IAAI,EAAE,CAAC;IACb,MAAM,MAAM,GAAG,GAAG,CAAC,MAAM,IAAI,GAAG,CAAC;IAEjC,IAAI,MAAM,KAAK,GAAG,IAAI,CAAC,IAAI,CAAC,MAAM,EAAE,EAAE;QACpC,MAAA,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAqB,kBAAkB,CAAC,0CAAE,gBAAgB,CAAC,IAAI,CAAC,CAAC;KACnF;AACH,CAAC","sourcesContent":["import {PlatformContext, PlatformExceptions} from \"@tsed/common\";\n\n/**\n * @ignore\n * @param ctx\n * @param next\n */\nexport async function resourceNotFoundMiddleware(ctx: any, next: any) {\n const $ctx: PlatformContext = ctx.request.$ctx;\n await next();\n const status = ctx.status || 404;\n\n if (status === 404 && !$ctx.isDone()) {\n $ctx.injector.get<PlatformExceptions>(PlatformExceptions)?.resourceNotFound($ctx);\n }\n}\n"]}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import send from "koa-send";
|
|
2
|
+
/**
|
|
3
|
+
* @ignore
|
|
4
|
+
* @param options
|
|
5
|
+
*/
|
|
6
|
+
export function staticsMiddleware(options) {
|
|
7
|
+
if (options.index !== false)
|
|
8
|
+
options.index = options.index || "index.html";
|
|
9
|
+
return async (ctx, next) => {
|
|
10
|
+
if (["GET", "HEAD"].includes(ctx.method) && !ctx.body) {
|
|
11
|
+
let path = ctx.path;
|
|
12
|
+
if (ctx._matchedRoute && ctx._matchedRoute !== "/") {
|
|
13
|
+
path = path.replace(String(ctx._matchedRoute).replace("/(.*)", ""), "");
|
|
14
|
+
}
|
|
15
|
+
try {
|
|
16
|
+
const done = await send(ctx, path, options);
|
|
17
|
+
if (done) {
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
catch (err) {
|
|
22
|
+
if (err.status !== 404) {
|
|
23
|
+
throw err;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
return next();
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
//# sourceMappingURL=staticsMiddleware.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"staticsMiddleware.js","sourceRoot":"","sources":["../../../src/middlewares/staticsMiddleware.ts"],"names":[],"mappings":"AAEA,OAAO,IAAI,MAAM,UAAU,CAAC;AAE5B;;;GAGG;AACH,MAAM,UAAU,iBAAiB,CAAC,OAA+B;IAC/D,IAAI,OAAO,CAAC,KAAK,KAAK,KAAK;QAAE,OAAO,CAAC,KAAK,GAAG,OAAO,CAAC,KAAK,IAAI,YAAY,CAAC;IAE3E,OAAO,KAAK,EAAE,GAAY,EAAE,IAAS,EAAE,EAAE;QACvC,IAAI,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC,QAAQ,CAAC,GAAG,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,IAAI,EAAE;YACrD,IAAI,IAAI,GAAG,GAAG,CAAC,IAAI,CAAC;YAEpB,IAAI,GAAG,CAAC,aAAa,IAAI,GAAG,CAAC,aAAa,KAAK,GAAG,EAAE;gBAClD,IAAI,GAAG,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,GAAG,CAAC,aAAa,CAAC,CAAC,OAAO,CAAC,OAAO,EAAE,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC;aACzE;YACD,IAAI;gBACF,MAAM,IAAI,GAAG,MAAM,IAAI,CAAC,GAAU,EAAE,IAAI,EAAE,OAA2B,CAAC,CAAC;gBAEvE,IAAI,IAAI,EAAE;oBACR,OAAO;iBACR;aACF;YAAC,OAAO,GAAG,EAAE;gBACZ,IAAI,GAAG,CAAC,MAAM,KAAK,GAAG,EAAE;oBACtB,MAAM,GAAG,CAAC;iBACX;aACF;SACF;QAED,OAAO,IAAI,EAAE,CAAC;IAChB,CAAC,CAAC;AACJ,CAAC","sourcesContent":["import {PlatformStaticsOptions} from \"@tsed/common\";\nimport type {Context} from \"koa\";\nimport send from \"koa-send\";\n\n/**\n * @ignore\n * @param options\n */\nexport function staticsMiddleware(options: PlatformStaticsOptions): any {\n if (options.index !== false) options.index = options.index || \"index.html\";\n\n return async (ctx: Context, next: any) => {\n if ([\"GET\", \"HEAD\"].includes(ctx.method) && !ctx.body) {\n let path = ctx.path;\n\n if (ctx._matchedRoute && ctx._matchedRoute !== \"/\") {\n path = path.replace(String(ctx._matchedRoute).replace(\"/(.*)\", \"\"), \"\");\n }\n try {\n const done = await send(ctx as any, path, options as send.SendOptions);\n\n if (done) {\n return;\n }\n } catch (err) {\n if (err.status !== 404) {\n throw err;\n }\n }\n }\n\n return next();\n };\n}\n"]}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { __decorate, __metadata, __param } from "tslib";
|
|
2
|
+
import { Configuration, Inject, PlatformHandler } from "@tsed/common";
|
|
3
|
+
import Koa from "koa";
|
|
4
|
+
import { PlatformKoaRouter } from "./PlatformKoaRouter";
|
|
5
|
+
const koaQs = require("koa-qs");
|
|
6
|
+
/**
|
|
7
|
+
* @platform
|
|
8
|
+
* @express
|
|
9
|
+
*/
|
|
10
|
+
let PlatformKoaApplication = class PlatformKoaApplication extends PlatformKoaRouter {
|
|
11
|
+
constructor(platformHandler, configuration) {
|
|
12
|
+
super(platformHandler, configuration, {});
|
|
13
|
+
this.rawApp = configuration.get("koa.app") || new Koa();
|
|
14
|
+
koaQs(this.rawApp, "extended");
|
|
15
|
+
}
|
|
16
|
+
getApp() {
|
|
17
|
+
return this.rawApp;
|
|
18
|
+
}
|
|
19
|
+
callback() {
|
|
20
|
+
return this.getApp().callback();
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
PlatformKoaApplication = __decorate([
|
|
24
|
+
__param(0, Inject()),
|
|
25
|
+
__param(1, Configuration()),
|
|
26
|
+
__metadata("design:paramtypes", [PlatformHandler, Object])
|
|
27
|
+
], PlatformKoaApplication);
|
|
28
|
+
export { PlatformKoaApplication };
|
|
29
|
+
//# sourceMappingURL=PlatformKoaApplication.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"PlatformKoaApplication.js","sourceRoot":"","sources":["../../../src/services/PlatformKoaApplication.ts"],"names":[],"mappings":";AACA,OAAO,EAAC,aAAa,EAAE,MAAM,EAAuB,eAAe,EAAC,MAAM,cAAc,CAAC;AACzF,OAAO,GAAG,MAAM,KAAK,CAAC;AACtB,OAAO,EAAC,iBAAiB,EAAC,MAAM,qBAAqB,CAAC;AAEtD,MAAM,KAAK,GAAG,OAAO,CAAC,QAAQ,CAAC,CAAC;AAQhC;;;GAGG;AACH,IAAa,sBAAsB,GAAnC,MAAa,sBAAuB,SAAQ,iBAAiB;IAI3D,YAAsB,eAAgC,EAAmB,aAA4B;QACnG,KAAK,CAAC,eAAe,EAAE,aAAa,EAAE,EAAE,CAAC,CAAC;QAE1C,IAAI,CAAC,MAAM,GAAG,aAAa,CAAC,GAAG,CAAC,SAAS,CAAC,IAAI,IAAI,GAAG,EAAE,CAAC;QACxD,KAAK,CAAC,IAAI,CAAC,MAAM,EAAE,UAAU,CAAC,CAAC;IACjC,CAAC;IAED,MAAM;QACJ,OAAO,IAAI,CAAC,MAAM,CAAC;IACrB,CAAC;IAED,QAAQ;QACN,OAAO,IAAI,CAAC,MAAM,EAAE,CAAC,QAAQ,EAAE,CAAC;IAClC,CAAC;CACF,CAAA;AAlBY,sBAAsB;IAIpB,WAAA,MAAM,EAAE,CAAA;IAAoC,WAAA,aAAa,EAAE,CAAA;qCAAjC,eAAe;GAJ3C,sBAAsB,CAkBlC;SAlBY,sBAAsB","sourcesContent":["import KoaRouter from \"@koa/router\";\nimport {Configuration, Inject, PlatformApplication, PlatformHandler} from \"@tsed/common\";\nimport Koa from \"koa\";\nimport {PlatformKoaRouter} from \"./PlatformKoaRouter\";\n\nconst koaQs = require(\"koa-qs\");\n\ndeclare global {\n namespace TsED {\n export interface Application extends Koa {}\n }\n}\n\n/**\n * @platform\n * @express\n */\nexport class PlatformKoaApplication extends PlatformKoaRouter implements PlatformApplication<Koa, KoaRouter> {\n app: Koa;\n rawApp: Koa;\n\n constructor(@Inject() platformHandler: PlatformHandler, @Configuration() configuration: Configuration) {\n super(platformHandler, configuration, {});\n\n this.rawApp = configuration.get(\"koa.app\") || new Koa();\n koaQs(this.rawApp, \"extended\");\n }\n\n getApp(): Koa {\n return this.rawApp;\n }\n\n callback(): any {\n return this.getApp().callback();\n }\n}\n"]}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { HandlerType, PlatformHandler } from "@tsed/common";
|
|
2
|
+
import "./PlatformKoaRequest";
|
|
3
|
+
const OVERRIDE_TYPES = [HandlerType.ENDPOINT, HandlerType.MIDDLEWARE, HandlerType.ERR_MIDDLEWARE, HandlerType.CTX_FN];
|
|
4
|
+
export class PlatformKoaHandler extends PlatformHandler {
|
|
5
|
+
async flush(data, ctx) {
|
|
6
|
+
if (data === undefined && ctx.getResponse().body) {
|
|
7
|
+
data = ctx.getResponse().body;
|
|
8
|
+
}
|
|
9
|
+
return super.flush(data, ctx);
|
|
10
|
+
}
|
|
11
|
+
createRawHandler(metadata) {
|
|
12
|
+
if (OVERRIDE_TYPES.includes(metadata.type)) {
|
|
13
|
+
const handler = this.compileHandler(metadata);
|
|
14
|
+
return async (ctx, next) => handler({ next, $ctx: ctx.request.$ctx });
|
|
15
|
+
}
|
|
16
|
+
return super.createRawHandler(metadata);
|
|
17
|
+
}
|
|
18
|
+
async onRequest(requestOptions) {
|
|
19
|
+
const { metadata, $ctx } = requestOptions;
|
|
20
|
+
if ($ctx.data instanceof Error) {
|
|
21
|
+
if (metadata.hasErrorParam) {
|
|
22
|
+
requestOptions.err = $ctx.data;
|
|
23
|
+
}
|
|
24
|
+
else {
|
|
25
|
+
return this.onError($ctx.data, requestOptions);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
return super.onRequest(requestOptions);
|
|
29
|
+
}
|
|
30
|
+
onError(error, requestOptions) {
|
|
31
|
+
const { next, $ctx, metadata } = requestOptions;
|
|
32
|
+
// istanbul ignore next
|
|
33
|
+
if ($ctx.isDone()) {
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
$ctx.data = error;
|
|
37
|
+
if (!next || metadata.isFinal()) {
|
|
38
|
+
this.throwError(error, $ctx);
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
return next();
|
|
42
|
+
}
|
|
43
|
+
async onCtxRequest(requestOptions) {
|
|
44
|
+
const { $ctx } = requestOptions;
|
|
45
|
+
try {
|
|
46
|
+
return await super.onCtxRequest(requestOptions);
|
|
47
|
+
}
|
|
48
|
+
catch (error) {
|
|
49
|
+
this.throwError(error, $ctx);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
throwError(error, $ctx) {
|
|
53
|
+
$ctx.getApp().emit("error", error, $ctx.getRequest().ctx);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
//# sourceMappingURL=PlatformKoaHandler.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"PlatformKoaHandler.js","sourceRoot":"","sources":["../../../src/services/PlatformKoaHandler.ts"],"names":[],"mappings":"AAAA,OAAO,EAAkB,WAAW,EAAqC,eAAe,EAAC,MAAM,cAAc,CAAC;AAE9G,OAAO,sBAAsB,CAAC;AAE9B,MAAM,cAAc,GAAG,CAAC,WAAW,CAAC,QAAQ,EAAE,WAAW,CAAC,UAAU,EAAE,WAAW,CAAC,cAAc,EAAE,WAAW,CAAC,MAAM,CAAC,CAAC;AAEtH,MAAM,OAAO,kBAAmB,SAAQ,eAAe;IAC9C,KAAK,CAAC,KAAK,CAAC,IAAS,EAAE,GAAoB;QAChD,IAAI,IAAI,KAAK,SAAS,IAAI,GAAG,CAAC,WAAW,EAAE,CAAC,IAAI,EAAE;YAChD,IAAI,GAAG,GAAG,CAAC,WAAW,EAAE,CAAC,IAAI,CAAC;SAC/B;QAED,OAAO,KAAK,CAAC,KAAK,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC;IAChC,CAAC;IAES,gBAAgB,CAAC,QAAyB;QAClD,IAAI,cAAc,CAAC,QAAQ,CAAC,QAAQ,CAAC,IAAI,CAAC,EAAE;YAC1C,MAAM,OAAO,GAAG,IAAI,CAAC,cAAc,CAAC,QAAQ,CAAC,CAAC;YAC9C,OAAO,KAAK,EAAE,GAAgB,EAAE,IAAc,EAAE,EAAE,CAAC,OAAO,CAAC,EAAC,IAAI,EAAE,IAAI,EAAE,GAAG,CAAC,OAAO,CAAC,IAAI,EAAC,CAAC,CAAC;SAC5F;QAED,OAAO,KAAK,CAAC,gBAAgB,CAAC,QAAQ,CAAC,CAAC;IAC1C,CAAC;IAES,KAAK,CAAC,SAAS,CAAC,cAAgC;QACxD,MAAM,EAAC,QAAQ,EAAE,IAAI,EAAC,GAAG,cAAc,CAAC;QAExC,IAAI,IAAI,CAAC,IAAI,YAAY,KAAK,EAAE;YAC9B,IAAI,QAAQ,CAAC,aAAa,EAAE;gBAC1B,cAAc,CAAC,GAAG,GAAG,IAAI,CAAC,IAAI,CAAC;aAChC;iBAAM;gBACL,OAAO,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI,EAAE,cAAc,CAAC,CAAC;aAChD;SACF;QAED,OAAO,KAAK,CAAC,SAAS,CAAC,cAAc,CAAC,CAAC;IACzC,CAAC;IAES,OAAO,CAAC,KAAc,EAAE,cAAgC;QAChE,MAAM,EAAC,IAAI,EAAE,IAAI,EAAE,QAAQ,EAAC,GAAG,cAAc,CAAC;QAE9C,uBAAuB;QACvB,IAAI,IAAI,CAAC,MAAM,EAAE,EAAE;YACjB,OAAO;SACR;QAED,IAAI,CAAC,IAAI,GAAG,KAAK,CAAC;QAElB,IAAI,CAAC,IAAI,IAAI,QAAQ,CAAC,OAAO,EAAE,EAAE;YAC/B,IAAI,CAAC,UAAU,CAAC,KAAK,EAAE,IAAI,CAAC,CAAC;YAE7B,OAAO;SACR;QAED,OAAO,IAAI,EAAE,CAAC;IAChB,CAAC;IAES,KAAK,CAAC,YAAY,CAAC,cAAgC;QAC3D,MAAM,EAAC,IAAI,EAAC,GAAG,cAAc,CAAC;QAE9B,IAAI;YACF,OAAO,MAAM,KAAK,CAAC,YAAY,CAAC,cAAc,CAAC,CAAC;SACjD;QAAC,OAAO,KAAK,EAAE;YACd,IAAI,CAAC,UAAU,CAAC,KAAK,EAAE,IAAI,CAAC,CAAC;SAC9B;IACH,CAAC;IAES,UAAU,CAAC,KAAU,EAAE,IAAqB;QACpD,IAAI,CAAC,MAAM,EAAE,CAAC,IAAI,CAAC,OAAO,EAAE,KAAK,EAAE,IAAI,CAAC,UAAU,EAAE,CAAC,GAAG,CAAC,CAAC;IAC5D,CAAC;CACF","sourcesContent":["import {HandlerMetadata, HandlerType, OnRequestOptions, PlatformContext, PlatformHandler} from \"@tsed/common\";\nimport Koa from \"koa\";\nimport \"./PlatformKoaRequest\";\n\nconst OVERRIDE_TYPES = [HandlerType.ENDPOINT, HandlerType.MIDDLEWARE, HandlerType.ERR_MIDDLEWARE, HandlerType.CTX_FN];\n\nexport class PlatformKoaHandler extends PlatformHandler {\n public async flush(data: any, ctx: PlatformContext): Promise<void> {\n if (data === undefined && ctx.getResponse().body) {\n data = ctx.getResponse().body;\n }\n\n return super.flush(data, ctx);\n }\n\n protected createRawHandler(metadata: HandlerMetadata) {\n if (OVERRIDE_TYPES.includes(metadata.type)) {\n const handler = this.compileHandler(metadata);\n return async (ctx: Koa.Context, next: Koa.Next) => handler({next, $ctx: ctx.request.$ctx});\n }\n\n return super.createRawHandler(metadata);\n }\n\n protected async onRequest(requestOptions: OnRequestOptions): Promise<any> {\n const {metadata, $ctx} = requestOptions;\n\n if ($ctx.data instanceof Error) {\n if (metadata.hasErrorParam) {\n requestOptions.err = $ctx.data;\n } else {\n return this.onError($ctx.data, requestOptions);\n }\n }\n\n return super.onRequest(requestOptions);\n }\n\n protected onError(error: unknown, requestOptions: OnRequestOptions) {\n const {next, $ctx, metadata} = requestOptions;\n\n // istanbul ignore next\n if ($ctx.isDone()) {\n return;\n }\n\n $ctx.data = error;\n\n if (!next || metadata.isFinal()) {\n this.throwError(error, $ctx);\n\n return;\n }\n\n return next();\n }\n\n protected async onCtxRequest(requestOptions: OnRequestOptions): Promise<any> {\n const {$ctx} = requestOptions;\n\n try {\n return await super.onCtxRequest(requestOptions);\n } catch (error) {\n this.throwError(error, $ctx);\n }\n }\n\n protected throwError(error: any, $ctx: PlatformContext) {\n $ctx.getApp().emit(\"error\", error, $ctx.getRequest().ctx);\n }\n}\n"]}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
var _PlatformKoaRequest_ctx;
|
|
2
|
+
import { __classPrivateFieldGet, __classPrivateFieldSet } from "tslib";
|
|
3
|
+
import "@koa/router";
|
|
4
|
+
import { PlatformRequest } from "@tsed/common";
|
|
5
|
+
/**
|
|
6
|
+
* @platform
|
|
7
|
+
* @koa
|
|
8
|
+
*/
|
|
9
|
+
export class PlatformKoaRequest extends PlatformRequest {
|
|
10
|
+
constructor(event, $ctx) {
|
|
11
|
+
super(event, $ctx);
|
|
12
|
+
_PlatformKoaRequest_ctx.set(this, void 0);
|
|
13
|
+
__classPrivateFieldSet(this, _PlatformKoaRequest_ctx, this.raw.ctx, "f");
|
|
14
|
+
}
|
|
15
|
+
get protocol() {
|
|
16
|
+
return __classPrivateFieldGet(this, _PlatformKoaRequest_ctx, "f").request.protocol;
|
|
17
|
+
}
|
|
18
|
+
get host() {
|
|
19
|
+
return __classPrivateFieldGet(this, _PlatformKoaRequest_ctx, "f").request.host;
|
|
20
|
+
}
|
|
21
|
+
get secure() {
|
|
22
|
+
return __classPrivateFieldGet(this, _PlatformKoaRequest_ctx, "f").request.secure;
|
|
23
|
+
}
|
|
24
|
+
get cookies() {
|
|
25
|
+
return __classPrivateFieldGet(this, _PlatformKoaRequest_ctx, "f").cookie || __classPrivateFieldGet(this, _PlatformKoaRequest_ctx, "f").cookies;
|
|
26
|
+
}
|
|
27
|
+
get session() {
|
|
28
|
+
return __classPrivateFieldGet(this, _PlatformKoaRequest_ctx, "f").session;
|
|
29
|
+
}
|
|
30
|
+
getReq() {
|
|
31
|
+
return this.raw.req;
|
|
32
|
+
}
|
|
33
|
+
destroy() {
|
|
34
|
+
// @ts-ignore
|
|
35
|
+
__classPrivateFieldSet(this, _PlatformKoaRequest_ctx, null, "f");
|
|
36
|
+
return super.destroy();
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
_PlatformKoaRequest_ctx = new WeakMap();
|
|
40
|
+
//# sourceMappingURL=PlatformKoaRequest.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"PlatformKoaRequest.js","sourceRoot":"","sources":["../../../src/services/PlatformKoaRequest.ts"],"names":[],"mappings":";;AAAA,OAAO,aAAa,CAAC;AACrB,OAAO,EAAiC,eAAe,EAAC,MAAM,cAAc,CAAC;AAiB7E;;;GAGG;AACH,MAAM,OAAO,kBAAmB,SAAQ,eAA4B;IAGlE,YAAY,KAAoB,EAAE,IAAqB;QACrD,KAAK,CAAC,KAAK,EAAE,IAAI,CAAC,CAAC;QAHrB,0CAAkB;QAIhB,uBAAA,IAAI,2BAAQ,IAAI,CAAC,GAAG,CAAC,GAAG,MAAA,CAAC;IAC3B,CAAC;IAED,IAAI,QAAQ;QACV,OAAO,uBAAA,IAAI,+BAAK,CAAC,OAAO,CAAC,QAAQ,CAAC;IACpC,CAAC;IAED,IAAI,IAAI;QACN,OAAO,uBAAA,IAAI,+BAAK,CAAC,OAAO,CAAC,IAAI,CAAC;IAChC,CAAC;IAED,IAAI,MAAM;QACR,OAAO,uBAAA,IAAI,+BAAK,CAAC,OAAO,CAAC,MAAM,CAAC;IAClC,CAAC;IAED,IAAI,OAAO;QACT,OAAO,uBAAA,IAAI,+BAAK,CAAC,MAAM,IAAI,uBAAA,IAAI,+BAAK,CAAC,OAAO,CAAC;IAC/C,CAAC;IAED,IAAI,OAAO;QACT,OAAO,uBAAA,IAAI,+BAAK,CAAC,OAAO,CAAC;IAC3B,CAAC;IAED,MAAM;QACJ,OAAO,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC;IACtB,CAAC;IAED,OAAO;QACL,aAAa;QACb,uBAAA,IAAI,2BAAQ,IAAI,MAAA,CAAC;QACjB,OAAO,KAAK,CAAC,OAAO,EAAE,CAAC;IACzB,CAAC;CACF","sourcesContent":["import \"@koa/router\";\nimport {IncomingEvent, PlatformContext, PlatformRequest} from \"@tsed/common\";\nimport {IncomingMessage} from \"http\";\nimport Koa from \"koa\";\n\ndeclare module \"koa\" {\n export interface Request {\n id: string;\n $ctx: PlatformContext;\n }\n}\n\ndeclare global {\n namespace TsED {\n export interface Request extends Koa.Request {}\n }\n}\n\n/**\n * @platform\n * @koa\n */\nexport class PlatformKoaRequest extends PlatformRequest<Koa.Request> {\n #ctx: Koa.Context;\n\n constructor(event: IncomingEvent, $ctx: PlatformContext) {\n super(event, $ctx);\n this.#ctx = this.raw.ctx;\n }\n\n get protocol(): string {\n return this.#ctx.request.protocol;\n }\n\n get host(): string {\n return this.#ctx.request.host;\n }\n\n get secure(): boolean {\n return this.#ctx.request.secure;\n }\n\n get cookies(): {[p: string]: any} {\n return this.#ctx.cookie || this.#ctx.cookies;\n }\n\n get session(): any {\n return this.#ctx.session;\n }\n\n getReq(): IncomingMessage {\n return this.raw.req;\n }\n\n destroy() {\n // @ts-ignore\n this.#ctx = null;\n return super.destroy();\n }\n}\n"]}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
var _PlatformKoaResponse_ctx;
|
|
2
|
+
import { __classPrivateFieldGet, __classPrivateFieldSet } from "tslib";
|
|
3
|
+
import { PlatformResponse } from "@tsed/common";
|
|
4
|
+
import { getStatusMessage } from "@tsed/schema";
|
|
5
|
+
const encodeUrl = require("encodeurl");
|
|
6
|
+
/**
|
|
7
|
+
* @platform
|
|
8
|
+
* @koa
|
|
9
|
+
*/
|
|
10
|
+
export class PlatformKoaResponse extends PlatformResponse {
|
|
11
|
+
constructor(event, $ctx) {
|
|
12
|
+
super(event, $ctx);
|
|
13
|
+
_PlatformKoaResponse_ctx.set(this, void 0);
|
|
14
|
+
__classPrivateFieldSet(this, _PlatformKoaResponse_ctx, this.raw.ctx, "f");
|
|
15
|
+
}
|
|
16
|
+
get statusCode() {
|
|
17
|
+
return this.raw.status;
|
|
18
|
+
}
|
|
19
|
+
get locals() {
|
|
20
|
+
return __classPrivateFieldGet(this, _PlatformKoaResponse_ctx, "f").state;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Return the Node.js response object
|
|
24
|
+
*/
|
|
25
|
+
getRes() {
|
|
26
|
+
return this.raw.res;
|
|
27
|
+
}
|
|
28
|
+
hasStatus() {
|
|
29
|
+
// KOA set 404 by default
|
|
30
|
+
return this.statusCode !== 404;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Sets the HTTP status for the response.
|
|
34
|
+
*
|
|
35
|
+
* @param status
|
|
36
|
+
*/
|
|
37
|
+
status(status) {
|
|
38
|
+
this.raw.status = status;
|
|
39
|
+
return this;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Set `Content-Type` response header with `type` through `mime.lookup()`
|
|
43
|
+
* when it does not contain "/", or set the Content-Type to `type` otherwise.
|
|
44
|
+
*
|
|
45
|
+
* Examples:
|
|
46
|
+
*
|
|
47
|
+
* res.type('.html');
|
|
48
|
+
* res.type('html');
|
|
49
|
+
* res.type('json');
|
|
50
|
+
* res.type('application/json');
|
|
51
|
+
* res.type('png');
|
|
52
|
+
*/
|
|
53
|
+
contentType(contentType) {
|
|
54
|
+
this.raw.type = contentType;
|
|
55
|
+
return this;
|
|
56
|
+
}
|
|
57
|
+
getHeaders() {
|
|
58
|
+
return this.raw.headers;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Send any data to your consumer.
|
|
62
|
+
*
|
|
63
|
+
* This method accept a ReadableStream, a plain object, boolean, string, number, null and undefined data.
|
|
64
|
+
* It choose the better way to send the data.
|
|
65
|
+
*
|
|
66
|
+
* @param data
|
|
67
|
+
*/
|
|
68
|
+
body(data) {
|
|
69
|
+
this.raw.body = data;
|
|
70
|
+
return this;
|
|
71
|
+
}
|
|
72
|
+
getBody() {
|
|
73
|
+
return this.raw.body;
|
|
74
|
+
}
|
|
75
|
+
redirect(status, url) {
|
|
76
|
+
status = status || 302;
|
|
77
|
+
// Set location header
|
|
78
|
+
url = this.location(url).raw.get("Location");
|
|
79
|
+
this.body(`${getStatusMessage(status)}. Redirecting to ${url}`);
|
|
80
|
+
this.status(status);
|
|
81
|
+
this.setHeader("Content-Length", Buffer.byteLength(this.raw.body));
|
|
82
|
+
if (this.request.method === "HEAD") {
|
|
83
|
+
this.getRes().end();
|
|
84
|
+
}
|
|
85
|
+
else {
|
|
86
|
+
this.getRes().end(this.getBody());
|
|
87
|
+
}
|
|
88
|
+
return this;
|
|
89
|
+
}
|
|
90
|
+
location(location) {
|
|
91
|
+
// "back" is an alias for the referrer
|
|
92
|
+
if (location === "back") {
|
|
93
|
+
location = this.request.get("Referrer") || "/";
|
|
94
|
+
}
|
|
95
|
+
// set location
|
|
96
|
+
this.raw.set("Location", encodeUrl(location));
|
|
97
|
+
return this;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
_PlatformKoaResponse_ctx = new WeakMap();
|
|
101
|
+
//# sourceMappingURL=PlatformKoaResponse.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"PlatformKoaResponse.js","sourceRoot":"","sources":["../../../src/services/PlatformKoaResponse.ts"],"names":[],"mappings":";;AAAA,OAAO,EAAiC,gBAAgB,EAAC,MAAM,cAAc,CAAC;AAG9E,OAAO,EAAC,gBAAgB,EAAC,MAAM,cAAc,CAAC;AAE9C,MAAM,SAAS,GAAG,OAAO,CAAC,WAAW,CAAC,CAAC;AAQvC;;;GAGG;AACH,MAAM,OAAO,mBAAoB,SAAQ,gBAA8B;IAGrE,YAAY,KAAoB,EAAE,IAAqB;QACrD,KAAK,CAAC,KAAK,EAAE,IAAI,CAAC,CAAC;QAHrB,2CAAkB;QAIhB,uBAAA,IAAI,4BAAQ,IAAI,CAAC,GAAG,CAAC,GAAG,MAAA,CAAC;IAC3B,CAAC;IAED,IAAI,UAAU;QACZ,OAAO,IAAI,CAAC,GAAG,CAAC,MAAM,CAAC;IACzB,CAAC;IAED,IAAI,MAAM;QACR,OAAO,uBAAA,IAAI,gCAAK,CAAC,KAAK,CAAC;IACzB,CAAC;IAED;;OAEG;IACH,MAAM;QACJ,OAAO,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC;IACtB,CAAC;IAED,SAAS;QACP,yBAAyB;QACzB,OAAO,IAAI,CAAC,UAAU,KAAK,GAAG,CAAC;IACjC,CAAC;IAED;;;;OAIG;IACH,MAAM,CAAC,MAAc;QACnB,IAAI,CAAC,GAAG,CAAC,MAAM,GAAG,MAAM,CAAC;QAEzB,OAAO,IAAI,CAAC;IACd,CAAC;IAED;;;;;;;;;;;OAWG;IACH,WAAW,CAAC,WAAmB;QAC7B,IAAI,CAAC,GAAG,CAAC,IAAI,GAAG,WAAW,CAAC;QAE5B,OAAO,IAAI,CAAC;IACd,CAAC;IAED,UAAU;QACR,OAAO,IAAI,CAAC,GAAG,CAAC,OAAO,CAAC;IAC1B,CAAC;IAED;;;;;;;OAOG;IACH,IAAI,CAAC,IAAS;QACZ,IAAI,CAAC,GAAG,CAAC,IAAI,GAAG,IAAI,CAAC;QAErB,OAAO,IAAI,CAAC;IACd,CAAC;IAED,OAAO;QACL,OAAO,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC;IACvB,CAAC;IAED,QAAQ,CAAC,MAAc,EAAE,GAAW;QAClC,MAAM,GAAG,MAAM,IAAI,GAAG,CAAC;QACvB,sBAAsB;QACtB,GAAG,GAAG,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC;QAE7C,IAAI,CAAC,IAAI,CAAC,GAAG,gBAAgB,CAAC,MAAM,CAAC,oBAAoB,GAAG,EAAE,CAAC,CAAC;QAChE,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;QACpB,IAAI,CAAC,SAAS,CAAC,gBAAgB,EAAE,MAAM,CAAC,UAAU,CAAC,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC,CAAC;QAEnE,IAAI,IAAI,CAAC,OAAO,CAAC,MAAM,KAAK,MAAM,EAAE;YAClC,IAAI,CAAC,MAAM,EAAE,CAAC,GAAG,EAAE,CAAC;SACrB;aAAM;YACL,IAAI,CAAC,MAAM,EAAE,CAAC,GAAG,CAAC,IAAI,CAAC,OAAO,EAAE,CAAC,CAAC;SACnC;QAED,OAAO,IAAI,CAAC;IACd,CAAC;IAED,QAAQ,CAAC,QAAgB;QACvB,sCAAsC;QACtC,IAAI,QAAQ,KAAK,MAAM,EAAE;YACvB,QAAQ,GAAG,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,UAAU,CAAC,IAAI,GAAG,CAAC;SAChD;QAED,eAAe;QACf,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,UAAU,EAAE,SAAS,CAAC,QAAQ,CAAC,CAAC,CAAC;QAE9C,OAAO,IAAI,CAAC;IACd,CAAC;CACF","sourcesContent":["import {IncomingEvent, PlatformContext, PlatformResponse} from \"@tsed/common\";\nimport {ServerResponse} from \"http\";\nimport Koa from \"koa\";\nimport {getStatusMessage} from \"@tsed/schema\";\n\nconst encodeUrl = require(\"encodeurl\");\n\ndeclare global {\n namespace TsED {\n export interface Response extends Koa.Response {}\n }\n}\n\n/**\n * @platform\n * @koa\n */\nexport class PlatformKoaResponse extends PlatformResponse<Koa.Response> {\n #ctx: Koa.Context;\n\n constructor(event: IncomingEvent, $ctx: PlatformContext) {\n super(event, $ctx);\n this.#ctx = this.raw.ctx;\n }\n\n get statusCode() {\n return this.raw.status;\n }\n\n get locals() {\n return this.#ctx.state;\n }\n\n /**\n * Return the Node.js response object\n */\n getRes(): ServerResponse {\n return this.raw.res;\n }\n\n hasStatus() {\n // KOA set 404 by default\n return this.statusCode !== 404;\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 `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.type = contentType;\n\n return this;\n }\n\n getHeaders() {\n return this.raw.headers;\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): this {\n this.raw.body = data;\n\n return this;\n }\n\n getBody() {\n return this.raw.body;\n }\n\n redirect(status: number, url: string): this {\n status = status || 302;\n // Set location header\n url = this.location(url).raw.get(\"Location\");\n\n this.body(`${getStatusMessage(status)}. Redirecting to ${url}`);\n this.status(status);\n this.setHeader(\"Content-Length\", Buffer.byteLength(this.raw.body));\n\n if (this.request.method === \"HEAD\") {\n this.getRes().end();\n } else {\n this.getRes().end(this.getBody());\n }\n\n return this;\n }\n\n location(location: string): this {\n // \"back\" is an alias for the referrer\n if (location === \"back\") {\n location = this.request.get(\"Referrer\") || \"/\";\n }\n\n // set location\n this.raw.set(\"Location\", encodeUrl(location));\n\n return this;\n }\n}\n"]}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { __decorate, __metadata, __param } from "tslib";
|
|
2
|
+
import KoaRouter from "@koa/router";
|
|
3
|
+
import { PLATFORM_ROUTER_OPTIONS, PlatformHandler, PlatformRouter } from "@tsed/common";
|
|
4
|
+
import { Configuration, Inject } from "@tsed/di";
|
|
5
|
+
import { staticsMiddleware } from "../middlewares/staticsMiddleware";
|
|
6
|
+
import { getMulter } from "../utils/multer";
|
|
7
|
+
// @ts-ignore
|
|
8
|
+
KoaRouter.prototype.$$match = KoaRouter.prototype.match;
|
|
9
|
+
KoaRouter.prototype.match = function match(...args) {
|
|
10
|
+
const matched = this.$$match(...args);
|
|
11
|
+
if (matched) {
|
|
12
|
+
if (matched.path.length) {
|
|
13
|
+
matched.route = true;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
return matched;
|
|
17
|
+
};
|
|
18
|
+
let PlatformKoaRouter = class PlatformKoaRouter extends PlatformRouter {
|
|
19
|
+
constructor(platform, configuration, routerOptions) {
|
|
20
|
+
var _a;
|
|
21
|
+
super(platform);
|
|
22
|
+
const options = Object.assign({}, ((_a = configuration.koa) === null || _a === void 0 ? void 0 : _a.router) || {}, routerOptions);
|
|
23
|
+
this.rawRouter = new KoaRouter(options);
|
|
24
|
+
}
|
|
25
|
+
callback() {
|
|
26
|
+
return [this.getRouter().routes(), this.getRouter().allowedMethods()];
|
|
27
|
+
}
|
|
28
|
+
multer(options) {
|
|
29
|
+
return getMulter(options);
|
|
30
|
+
}
|
|
31
|
+
statics(path, options) {
|
|
32
|
+
this.rawRouter.use(path, staticsMiddleware(options));
|
|
33
|
+
return this;
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
PlatformKoaRouter = __decorate([
|
|
37
|
+
__param(1, Configuration()),
|
|
38
|
+
__param(2, Inject(PLATFORM_ROUTER_OPTIONS)),
|
|
39
|
+
__metadata("design:paramtypes", [PlatformHandler, Object, Object])
|
|
40
|
+
], PlatformKoaRouter);
|
|
41
|
+
export { PlatformKoaRouter };
|
|
42
|
+
//# sourceMappingURL=PlatformKoaRouter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"PlatformKoaRouter.js","sourceRoot":"","sources":["../../../src/services/PlatformKoaRouter.ts"],"names":[],"mappings":";AAAA,OAAO,SAAS,MAAM,aAAa,CAAC;AAGpC,OAAO,EACL,uBAAuB,EACvB,eAAe,EAGf,cAAc,EAEf,MAAM,cAAc,CAAC;AACtB,OAAO,EAAC,aAAa,EAAE,MAAM,EAAC,MAAM,UAAU,CAAC;AAE/C,OAAO,EAAC,iBAAiB,EAAC,MAAM,kCAAkC,CAAC;AACnE,OAAO,EAAC,SAAS,EAAC,MAAM,iBAAiB,CAAC;AAY1C,aAAa;AACb,SAAS,CAAC,SAAS,CAAC,OAAO,GAAG,SAAS,CAAC,SAAS,CAAC,KAAK,CAAC;AACxD,SAAS,CAAC,SAAS,CAAC,KAAK,GAAG,SAAS,KAAK,CAAC,GAAG,IAAW;IACvD,MAAM,OAAO,GAAG,IAAI,CAAC,OAAO,CAAC,GAAG,IAAI,CAAC,CAAC;IACtC,IAAI,OAAO,EAAE;QACX,IAAI,OAAO,CAAC,IAAI,CAAC,MAAM,EAAE;YACvB,OAAO,CAAC,KAAK,GAAG,IAAI,CAAC;SACtB;KACF;IAED,OAAO,OAAO,CAAC;AACjB,CAAC,CAAC;AAEF,IAAa,iBAAiB,GAA9B,MAAa,iBAAkB,SAAQ,cAAyB;IAC9D,YACE,QAAyB,EACR,aAA4B,EACZ,aAAkB;;QAEnD,KAAK,CAAC,QAAQ,CAAC,CAAC;QAEhB,MAAM,OAAO,GAAG,MAAM,CAAC,MAAM,CAAC,EAAE,EAAE,CAAA,MAAA,aAAa,CAAC,GAAG,0CAAE,MAAM,KAAI,EAAE,EAAE,aAAa,CAAC,CAAC;QAClF,IAAI,CAAC,SAAS,GAAG,IAAI,SAAS,CAAC,OAAO,CAAQ,CAAC;IACjD,CAAC;IAED,QAAQ;QACN,OAAO,CAAC,IAAI,CAAC,SAAS,EAAE,CAAC,MAAM,EAAE,EAAE,IAAI,CAAC,SAAS,EAAE,CAAC,cAAc,EAAE,CAAC,CAAC;IACxE,CAAC;IAED,MAAM,CAAC,OAA+B;QACpC,OAAO,SAAS,CAAC,OAAO,CAAC,CAAC;IAC5B,CAAC;IAED,OAAO,CAAC,IAAY,EAAE,OAA+B;QACnD,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,IAAI,EAAE,iBAAiB,CAAC,OAAO,CAAQ,CAAC,CAAC;QAE5D,OAAO,IAAI,CAAC;IACd,CAAC;CACF,CAAA;AAzBY,iBAAiB;IAGzB,WAAA,aAAa,EAAE,CAAA;IACf,WAAA,MAAM,CAAC,uBAAuB,CAAC,CAAA;qCAFtB,eAAe;GAFhB,iBAAiB,CAyB7B;SAzBY,iBAAiB","sourcesContent":["import KoaRouter from \"@koa/router\";\nimport {RouterOptions as KoaRouterOptions} from \"@koa/router\";\n\nimport {\n PLATFORM_ROUTER_OPTIONS,\n PlatformHandler,\n PlatformMulter,\n PlatformMulterSettings,\n PlatformRouter,\n PlatformStaticsOptions\n} from \"@tsed/common\";\nimport {Configuration, Inject} from \"@tsed/di\";\nimport send from \"koa-send\";\nimport {staticsMiddleware} from \"../middlewares/staticsMiddleware\";\nimport {getMulter} from \"../utils/multer\";\n\ndeclare global {\n namespace TsED {\n export interface Router extends KoaRouter {}\n\n export interface RouterOptions extends KoaRouterOptions {}\n\n export interface StaticsOptions extends send.SendOptions {}\n }\n}\n\n// @ts-ignore\nKoaRouter.prototype.$$match = KoaRouter.prototype.match;\nKoaRouter.prototype.match = function match(...args: any[]) {\n const matched = this.$$match(...args);\n if (matched) {\n if (matched.path.length) {\n matched.route = true;\n }\n }\n\n return matched;\n};\n\nexport class PlatformKoaRouter extends PlatformRouter<KoaRouter> {\n constructor(\n platform: PlatformHandler,\n @Configuration() configuration: Configuration,\n @Inject(PLATFORM_ROUTER_OPTIONS) routerOptions: any\n ) {\n super(platform);\n\n const options = Object.assign({}, configuration.koa?.router || {}, routerOptions);\n this.rawRouter = new KoaRouter(options) as any;\n }\n\n callback() {\n return [this.getRouter().routes(), this.getRouter().allowedMethods()];\n }\n\n multer(options: PlatformMulterSettings): PlatformMulter {\n return getMulter(options);\n }\n\n statics(path: string, options: PlatformStaticsOptions) {\n this.rawRouter.use(path, staticsMiddleware(options) as any);\n\n return this;\n }\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/services/index.ts"],"names":[],"mappings":"AAAA,cAAc,0BAA0B,CAAC;AACzC,cAAc,sBAAsB,CAAC;AACrC,cAAc,qBAAqB,CAAC;AACpC,cAAc,sBAAsB,CAAC;AACrC,cAAc,uBAAuB,CAAC","sourcesContent":["export * from \"./PlatformKoaApplication\";\nexport * from \"./PlatformKoaHandler\";\nexport * from \"./PlatformKoaRouter\";\nexport * from \"./PlatformKoaRequest\";\nexport * from \"./PlatformKoaResponse\";\n"]}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { promisify } from "util";
|
|
2
|
+
/**
|
|
3
|
+
* @ignore
|
|
4
|
+
*/
|
|
5
|
+
function createRawMiddleware(middleware) {
|
|
6
|
+
return async (request) => {
|
|
7
|
+
const ctx = request.ctx;
|
|
8
|
+
await middleware(ctx.req, ctx.res);
|
|
9
|
+
const forwardKey = (key) => {
|
|
10
|
+
if (ctx.req[key]) {
|
|
11
|
+
ctx.request[key] = ctx.req[key];
|
|
12
|
+
ctx[key] = ctx.req[key];
|
|
13
|
+
delete ctx.req[key];
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
if ("request" in ctx) {
|
|
17
|
+
if (ctx.req.body) {
|
|
18
|
+
ctx.request.body = ctx.req.body;
|
|
19
|
+
delete ctx.req.body;
|
|
20
|
+
}
|
|
21
|
+
forwardKey("file");
|
|
22
|
+
forwardKey("files");
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* @ignore
|
|
28
|
+
*/
|
|
29
|
+
function makePromise(multer, name) {
|
|
30
|
+
// istanbul ignore next
|
|
31
|
+
if (!multer[name])
|
|
32
|
+
return;
|
|
33
|
+
const fn = multer[name];
|
|
34
|
+
multer[name] = function apply(...args) {
|
|
35
|
+
const middleware = Reflect.apply(fn, this, args);
|
|
36
|
+
return createRawMiddleware(promisify(middleware));
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* @ignore
|
|
41
|
+
*/
|
|
42
|
+
export function getMulter(options) {
|
|
43
|
+
const m = require("multer")(options);
|
|
44
|
+
makePromise(m, "any");
|
|
45
|
+
makePromise(m, "array");
|
|
46
|
+
makePromise(m, "fields");
|
|
47
|
+
makePromise(m, "none");
|
|
48
|
+
makePromise(m, "single");
|
|
49
|
+
return m;
|
|
50
|
+
}
|
|
51
|
+
//# sourceMappingURL=multer.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"multer.js","sourceRoot":"","sources":["../../../src/utils/multer.ts"],"names":[],"mappings":"AACA,OAAO,EAAC,SAAS,EAAC,MAAM,MAAM,CAAC;AAE/B;;GAEG;AACH,SAAS,mBAAmB,CAAC,UAAe;IAC1C,OAAO,KAAK,EAAE,OAAoB,EAAE,EAAE;QACpC,MAAM,GAAG,GAAQ,OAAO,CAAC,GAAG,CAAC;QAE7B,MAAM,UAAU,CAAC,GAAG,CAAC,GAAG,EAAE,GAAG,CAAC,GAAG,CAAC,CAAC;QAEnC,MAAM,UAAU,GAAG,CAAC,GAAW,EAAE,EAAE;YACjC,IAAI,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE;gBAChB,GAAG,CAAC,OAAO,CAAC,GAAG,CAAC,GAAG,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;gBAChC,GAAG,CAAC,GAAG,CAAC,GAAG,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;gBACxB,OAAO,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;aACrB;QACH,CAAC,CAAC;QAEF,IAAI,SAAS,IAAI,GAAG,EAAE;YACpB,IAAI,GAAG,CAAC,GAAG,CAAC,IAAI,EAAE;gBAChB,GAAG,CAAC,OAAO,CAAC,IAAI,GAAG,GAAG,CAAC,GAAG,CAAC,IAAI,CAAC;gBAChC,OAAO,GAAG,CAAC,GAAG,CAAC,IAAI,CAAC;aACrB;YAED,UAAU,CAAC,MAAM,CAAC,CAAC;YACnB,UAAU,CAAC,OAAO,CAAC,CAAC;SACrB;IACH,CAAC,CAAC;AACJ,CAAC;AAED;;GAEG;AACH,SAAS,WAAW,CAAC,MAAW,EAAE,IAAY;IAC5C,uBAAuB;IACvB,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC;QAAE,OAAO;IAE1B,MAAM,EAAE,GAAG,MAAM,CAAC,IAAI,CAAC,CAAC;IAExB,MAAM,CAAC,IAAI,CAAC,GAAG,SAAS,KAAK,CAAC,GAAG,IAAW;QAC1C,MAAM,UAAU,GAAG,OAAO,CAAC,KAAK,CAAC,EAAE,EAAE,IAAI,EAAE,IAAI,CAAC,CAAC;QAEjD,OAAO,mBAAmB,CAAC,SAAS,CAAC,UAAU,CAAC,CAAC,CAAC;IACpD,CAAC,CAAC;AACJ,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,SAAS,CAAC,OAAY;IACpC,MAAM,CAAC,GAAG,OAAO,CAAC,QAAQ,CAAC,CAAC,OAAO,CAAC,CAAC;IAErC,WAAW,CAAC,CAAC,EAAE,KAAK,CAAC,CAAC;IACtB,WAAW,CAAC,CAAC,EAAE,OAAO,CAAC,CAAC;IACxB,WAAW,CAAC,CAAC,EAAE,QAAQ,CAAC,CAAC;IACzB,WAAW,CAAC,CAAC,EAAE,MAAM,CAAC,CAAC;IACvB,WAAW,CAAC,CAAC,EAAE,QAAQ,CAAC,CAAC;IAEzB,OAAO,CAAC,CAAC;AACX,CAAC","sourcesContent":["import Koa from \"koa\";\nimport {promisify} from \"util\";\n\n/**\n * @ignore\n */\nfunction createRawMiddleware(middleware: any) {\n return async (request: Koa.Request) => {\n const ctx: any = request.ctx;\n\n await middleware(ctx.req, ctx.res);\n\n const forwardKey = (key: string) => {\n if (ctx.req[key]) {\n ctx.request[key] = ctx.req[key];\n ctx[key] = ctx.req[key];\n delete ctx.req[key];\n }\n };\n\n if (\"request\" in ctx) {\n if (ctx.req.body) {\n ctx.request.body = ctx.req.body;\n delete ctx.req.body;\n }\n\n forwardKey(\"file\");\n forwardKey(\"files\");\n }\n };\n}\n\n/**\n * @ignore\n */\nfunction makePromise(multer: any, name: string) {\n // istanbul ignore next\n if (!multer[name]) return;\n\n const fn = multer[name];\n\n multer[name] = function apply(...args: any[]) {\n const middleware = Reflect.apply(fn, this, args);\n\n return createRawMiddleware(promisify(middleware));\n };\n}\n\n/**\n * @ignore\n */\nexport function getMulter(options: any) {\n const m = require(\"multer\")(options);\n\n makePromise(m, \"any\");\n makePromise(m, \"array\");\n makePromise(m, \"fields\");\n makePromise(m, \"none\");\n makePromise(m, \"single\");\n\n return m;\n}\n"]}
|