@tsed/platform-koa 6.66.0 → 6.69.0-rc.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/decorators/ctx.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ctx.js","sourceRoot":"","sources":["../../src/decorators/ctx.ts"],"names":[],"mappings":";;;AAAA,yCAAsC;AAGtC;;;;;;GAMG;AACH,SAAgB,MAAM;IACpB,OAAO,iBAAQ,CAAC,SAAS,EAAE;QACzB,YAAY,EAAE,KAAK;QACnB,aAAa,EAAE,KAAK;KACrB,CAAC,CAAC;AACL,CAAC;
|
|
1
|
+
{"version":3,"file":"ctx.js","sourceRoot":"","sources":["../../src/decorators/ctx.ts"],"names":[],"mappings":";;;AAAA,yCAAsC;AAGtC;;;;;;GAMG;AACH,SAAgB,MAAM;IACpB,OAAO,iBAAQ,CAAC,SAAS,EAAE;QACzB,SAAS,EAAE,SAAS;QACpB,QAAQ,EAAE,kBAAkB;QAC5B,YAAY,EAAE,KAAK;QACnB,aAAa,EAAE,KAAK;KACrB,CAAC,CAAC;AACL,CAAC;AAPD,wBAOC;AAED;;;;;;GAMG;AACH,SAAgB,GAAG;IACjB,OAAO,MAAM,EAAE,CAAC;AAClB,CAAC;AAFD,kBAEC","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"]}
|
|
@@ -1,9 +1,8 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { HandlerMetadata, OnRequestOptions, PlatformContext, PlatformHandler } from "@tsed/common";
|
|
2
2
|
import "./PlatformKoaRequest";
|
|
3
3
|
export declare class PlatformKoaHandler extends PlatformHandler {
|
|
4
|
-
protected getArg(type: ParamTypes | string, h: HandlerContext): any;
|
|
5
|
-
protected createRawHandler(metadata: HandlerMetadata): any;
|
|
6
4
|
flush(data: any, ctx: PlatformContext): Promise<void>;
|
|
5
|
+
protected createRawHandler(metadata: HandlerMetadata): any;
|
|
7
6
|
protected onRequest(requestOptions: OnRequestOptions): Promise<any>;
|
|
8
7
|
protected onError(error: unknown, requestOptions: OnRequestOptions): any;
|
|
9
8
|
protected onCtxRequest(requestOptions: OnRequestOptions): Promise<any>;
|
|
@@ -3,47 +3,21 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.PlatformKoaHandler = void 0;
|
|
4
4
|
const common_1 = require("@tsed/common");
|
|
5
5
|
require("./PlatformKoaRequest");
|
|
6
|
+
const OVERRIDE_TYPES = [common_1.HandlerType.ENDPOINT, common_1.HandlerType.MIDDLEWARE, common_1.HandlerType.ERR_MIDDLEWARE, common_1.HandlerType.CTX_FN];
|
|
6
7
|
class PlatformKoaHandler extends common_1.PlatformHandler {
|
|
7
|
-
getArg(type, h) {
|
|
8
|
-
const koaCtx = h.request.ctx;
|
|
9
|
-
switch (String(type)) {
|
|
10
|
-
case "KOA_CTX":
|
|
11
|
-
return koaCtx;
|
|
12
|
-
case common_1.ParamTypes.LOCALS:
|
|
13
|
-
return koaCtx.state;
|
|
14
|
-
default:
|
|
15
|
-
return super.getArg(type, h);
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
createRawHandler(metadata) {
|
|
19
|
-
switch (metadata.type) {
|
|
20
|
-
default:
|
|
21
|
-
return super.createRawHandler(metadata);
|
|
22
|
-
case common_1.HandlerType.CTX_FN:
|
|
23
|
-
return async (ctx, next) => this.onCtxRequest({
|
|
24
|
-
metadata,
|
|
25
|
-
next,
|
|
26
|
-
$ctx: ctx.request.$ctx
|
|
27
|
-
});
|
|
28
|
-
case common_1.HandlerType.RAW_ERR_FN:
|
|
29
|
-
case common_1.HandlerType.RAW_FN:
|
|
30
|
-
return metadata.handler;
|
|
31
|
-
case common_1.HandlerType.ENDPOINT:
|
|
32
|
-
case common_1.HandlerType.MIDDLEWARE:
|
|
33
|
-
case common_1.HandlerType.ERR_MIDDLEWARE:
|
|
34
|
-
return (ctx, next) => this.onRequest({
|
|
35
|
-
$ctx: ctx.request.$ctx,
|
|
36
|
-
metadata,
|
|
37
|
-
next
|
|
38
|
-
});
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
8
|
async flush(data, ctx) {
|
|
42
9
|
if (data === undefined && ctx.getResponse().body) {
|
|
43
10
|
data = ctx.getResponse().body;
|
|
44
11
|
}
|
|
45
12
|
return super.flush(data, ctx);
|
|
46
13
|
}
|
|
14
|
+
createRawHandler(metadata) {
|
|
15
|
+
if (OVERRIDE_TYPES.includes(metadata.type)) {
|
|
16
|
+
const handler = this.compileHandler(metadata);
|
|
17
|
+
return async (ctx, next) => handler({ next, $ctx: ctx.request.$ctx });
|
|
18
|
+
}
|
|
19
|
+
return super.createRawHandler(metadata);
|
|
20
|
+
}
|
|
47
21
|
async onRequest(requestOptions) {
|
|
48
22
|
const { metadata, $ctx } = requestOptions;
|
|
49
23
|
if ($ctx.data instanceof Error) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"PlatformKoaHandler.js","sourceRoot":"","sources":["../../src/services/PlatformKoaHandler.ts"],"names":[],"mappings":";;;AAAA,
|
|
1
|
+
{"version":3,"file":"PlatformKoaHandler.js","sourceRoot":"","sources":["../../src/services/PlatformKoaHandler.ts"],"names":[],"mappings":";;;AAAA,yCAA8G;AAE9G,gCAA8B;AAE9B,MAAM,cAAc,GAAG,CAAC,oBAAW,CAAC,QAAQ,EAAE,oBAAW,CAAC,UAAU,EAAE,oBAAW,CAAC,cAAc,EAAE,oBAA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G,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;AAhED,gDAgEC","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"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tsed/platform-koa",
|
|
3
|
-
"version": "6.
|
|
3
|
+
"version": "6.69.0-rc.1",
|
|
4
4
|
"description": "Koa package for Ts.ED framework",
|
|
5
5
|
"source": "./src/index.ts",
|
|
6
6
|
"main": "./lib/index.js",
|
|
@@ -33,7 +33,7 @@
|
|
|
33
33
|
"bugs": {
|
|
34
34
|
"url": "https://github.com/tsedio/tsed/issues"
|
|
35
35
|
},
|
|
36
|
-
"homepage": "https://github.com/tsedio/tsed/tree/
|
|
36
|
+
"homepage": "https://github.com/tsedio/tsed/tree/rc//packages/platform/platform-koa",
|
|
37
37
|
"repository": "https://github.com/tsedio/tsed",
|
|
38
38
|
"scripts": {
|
|
39
39
|
"build": "tsc --build tsconfig.compile.json",
|
|
@@ -41,10 +41,10 @@
|
|
|
41
41
|
},
|
|
42
42
|
"devDependencies": {
|
|
43
43
|
"@koa/cors": "3.1.0",
|
|
44
|
-
"@tsed/common": "6.
|
|
45
|
-
"@tsed/core": "6.
|
|
46
|
-
"@tsed/di": "6.
|
|
47
|
-
"@tsed/platform-test-utils": "6.
|
|
44
|
+
"@tsed/common": "6.69.0-rc.1",
|
|
45
|
+
"@tsed/core": "6.69.0-rc.1",
|
|
46
|
+
"@tsed/di": "6.69.0-rc.1",
|
|
47
|
+
"@tsed/platform-test-utils": "6.69.0-rc.1",
|
|
48
48
|
"@types/koa": "2.11.3",
|
|
49
49
|
"@types/koa-bodyparser": "4.3.0",
|
|
50
50
|
"@types/koa-compose": "3.2.5",
|