@tsed/platform-koa 7.0.0-beta.11 → 7.0.0-beta.14
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/components/PlatformKoa.js +60 -49
- package/lib/cjs/components/PlatformKoa.js.map +1 -1
- package/lib/cjs/index.js +0 -3
- package/lib/cjs/index.js.map +1 -1
- package/lib/cjs/interfaces/PlatformKoaSettings.js.map +1 -1
- package/lib/cjs/middlewares/resourceNotFoundMiddleware.js +3 -3
- package/lib/cjs/middlewares/resourceNotFoundMiddleware.js.map +1 -1
- package/lib/cjs/services/PlatformKoaHandler.js +14 -42
- package/lib/cjs/services/PlatformKoaHandler.js.map +1 -1
- package/lib/cjs/services/PlatformKoaRequest.js +8 -18
- package/lib/cjs/services/PlatformKoaRequest.js.map +1 -1
- package/lib/cjs/services/PlatformKoaResponse.js +3 -7
- package/lib/cjs/services/PlatformKoaResponse.js.map +1 -1
- package/lib/esm/components/PlatformKoa.js +61 -50
- package/lib/esm/components/PlatformKoa.js.map +1 -1
- package/lib/esm/index.js +0 -3
- package/lib/esm/index.js.map +1 -1
- package/lib/esm/interfaces/PlatformKoaSettings.js.map +1 -1
- package/lib/esm/middlewares/resourceNotFoundMiddleware.js +3 -3
- package/lib/esm/middlewares/resourceNotFoundMiddleware.js.map +1 -1
- package/lib/esm/services/PlatformKoaHandler.js +15 -43
- package/lib/esm/services/PlatformKoaHandler.js.map +1 -1
- package/lib/esm/services/PlatformKoaRequest.js +8 -18
- package/lib/esm/services/PlatformKoaRequest.js.map +1 -1
- package/lib/esm/services/PlatformKoaResponse.js +3 -8
- package/lib/esm/services/PlatformKoaResponse.js.map +1 -1
- package/lib/types/components/PlatformKoa.d.ts +12 -14
- package/lib/types/index.d.ts +0 -3
- package/lib/types/interfaces/PlatformKoaSettings.d.ts +1 -1
- package/lib/types/services/PlatformKoaHandler.d.ts +4 -7
- package/lib/types/services/PlatformKoaRequest.d.ts +3 -6
- package/lib/types/services/PlatformKoaResponse.d.ts +2 -3
- package/package.json +24 -16
- package/readme.md +2 -1
- package/lib/cjs/decorators/caseSensitive.js +0 -30
- package/lib/cjs/decorators/caseSensitive.js.map +0 -1
- package/lib/cjs/decorators/routerSettings.js +0 -31
- package/lib/cjs/decorators/routerSettings.js.map +0 -1
- package/lib/cjs/decorators/strict.js +0 -31
- package/lib/cjs/decorators/strict.js.map +0 -1
- package/lib/esm/decorators/caseSensitive.js +0 -26
- package/lib/esm/decorators/caseSensitive.js.map +0 -1
- package/lib/esm/decorators/routerSettings.js +0 -27
- package/lib/esm/decorators/routerSettings.js.map +0 -1
- package/lib/esm/decorators/strict.js +0 -27
- package/lib/esm/decorators/strict.js.map +0 -1
- package/lib/types/decorators/caseSensitive.d.ts +0 -22
- package/lib/types/decorators/routerSettings.d.ts +0 -24
- package/lib/types/decorators/strict.d.ts +0 -23
|
@@ -1,17 +1,17 @@
|
|
|
1
1
|
import KoaRouter from "@koa/router";
|
|
2
|
-
import {
|
|
2
|
+
import { getContext, PlatformApplication, PlatformBuilder, PlatformHandler, PlatformRequest, PlatformResponse } from "@tsed/common";
|
|
3
3
|
import { PlatformExceptions } from "@tsed/platform-exceptions";
|
|
4
4
|
import { isFunction } from "@tsed/core";
|
|
5
5
|
import Koa from "koa";
|
|
6
|
+
import koaBodyParser from "koa-bodyparser";
|
|
7
|
+
// @ts-ignore
|
|
8
|
+
import koaQs from "koa-qs";
|
|
6
9
|
import { resourceNotFoundMiddleware } from "../middlewares/resourceNotFoundMiddleware.js";
|
|
7
|
-
import {
|
|
8
|
-
import { PlatformKoaRequest } from "../services/PlatformKoaRequest.js";
|
|
10
|
+
import { staticsMiddleware } from "../middlewares/staticsMiddleware.js";
|
|
9
11
|
import { PlatformKoaHandler } from "../services/PlatformKoaHandler.js";
|
|
12
|
+
import { PlatformKoaRequest } from "../services/PlatformKoaRequest.js";
|
|
13
|
+
import { PlatformKoaResponse } from "../services/PlatformKoaResponse.js";
|
|
10
14
|
import { getMulter } from "../utils/multer.js";
|
|
11
|
-
import { staticsMiddleware } from "../middlewares/staticsMiddleware.js";
|
|
12
|
-
// @ts-ignore
|
|
13
|
-
import koaQs from "koa-qs";
|
|
14
|
-
import koaBodyParser from "koa-bodyparser";
|
|
15
15
|
// @ts-ignore
|
|
16
16
|
KoaRouter.prototype.$$match = KoaRouter.prototype.match;
|
|
17
17
|
KoaRouter.prototype.match = function match(...args) {
|
|
@@ -28,22 +28,23 @@ KoaRouter.prototype.match = function match(...args) {
|
|
|
28
28
|
* @koa
|
|
29
29
|
*/
|
|
30
30
|
export class PlatformKoa {
|
|
31
|
+
injector;
|
|
32
|
+
providers = [
|
|
33
|
+
{
|
|
34
|
+
provide: PlatformResponse,
|
|
35
|
+
useClass: PlatformKoaResponse
|
|
36
|
+
},
|
|
37
|
+
{
|
|
38
|
+
provide: PlatformRequest,
|
|
39
|
+
useClass: PlatformKoaRequest
|
|
40
|
+
},
|
|
41
|
+
{
|
|
42
|
+
provide: PlatformHandler,
|
|
43
|
+
useClass: PlatformKoaHandler
|
|
44
|
+
}
|
|
45
|
+
];
|
|
31
46
|
constructor(injector) {
|
|
32
47
|
this.injector = injector;
|
|
33
|
-
this.providers = [
|
|
34
|
-
{
|
|
35
|
-
provide: PlatformResponse,
|
|
36
|
-
useClass: PlatformKoaResponse
|
|
37
|
-
},
|
|
38
|
-
{
|
|
39
|
-
provide: PlatformRequest,
|
|
40
|
-
useClass: PlatformKoaRequest
|
|
41
|
-
},
|
|
42
|
-
{
|
|
43
|
-
provide: PlatformHandler,
|
|
44
|
-
useClass: PlatformKoaHandler
|
|
45
|
-
}
|
|
46
|
-
];
|
|
47
48
|
}
|
|
48
49
|
/**
|
|
49
50
|
* Create new serverless application. In this mode, the component scan are disabled.
|
|
@@ -69,31 +70,50 @@ export class PlatformKoa {
|
|
|
69
70
|
}
|
|
70
71
|
onInit() {
|
|
71
72
|
const injector = this.injector;
|
|
72
|
-
const app = this.
|
|
73
|
+
const app = this.getPlatformApplication();
|
|
73
74
|
const listener = (error, ctx) => {
|
|
74
|
-
|
|
75
|
-
|
|
75
|
+
const $ctx = getContext();
|
|
76
|
+
injector.get(PlatformExceptions)?.catch(error, $ctx);
|
|
76
77
|
};
|
|
77
78
|
app.getApp().silent = true;
|
|
78
79
|
app.getApp().on("error", listener);
|
|
79
80
|
}
|
|
80
|
-
|
|
81
|
+
async beforeLoadRoutes() {
|
|
81
82
|
const app = this.injector.get(PlatformApplication);
|
|
82
|
-
|
|
83
|
-
app.
|
|
84
|
-
|
|
83
|
+
this.useContext();
|
|
84
|
+
app.use(resourceNotFoundMiddleware);
|
|
85
|
+
}
|
|
86
|
+
mapLayers(layers) {
|
|
87
|
+
const { settings } = this.injector;
|
|
88
|
+
const app = this.getPlatformApplication();
|
|
89
|
+
const options = settings.get("koa.router", {});
|
|
90
|
+
const rawRouter = new KoaRouter(options);
|
|
91
|
+
layers.forEach((layer) => {
|
|
92
|
+
switch (layer.method) {
|
|
93
|
+
case "statics":
|
|
94
|
+
rawRouter.use(layer.path, this.statics(layer.path, layer.opts));
|
|
95
|
+
break;
|
|
96
|
+
default:
|
|
97
|
+
rawRouter[layer.method](...layer.getArgs());
|
|
98
|
+
}
|
|
99
|
+
});
|
|
100
|
+
app.getApp().use(rawRouter.routes()).use(rawRouter.allowedMethods());
|
|
101
|
+
}
|
|
102
|
+
mapHandler(handler, metadata) {
|
|
103
|
+
if (metadata.isRawMiddleware()) {
|
|
104
|
+
return handler;
|
|
105
|
+
}
|
|
106
|
+
return async (koaContext, next) => {
|
|
107
|
+
const $ctx = getContext();
|
|
108
|
+
$ctx.next = next;
|
|
109
|
+
await handler($ctx);
|
|
110
|
+
};
|
|
85
111
|
}
|
|
86
112
|
useContext() {
|
|
87
|
-
const app = this.
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
app.getApp().use(async (ctx, next) => {
|
|
92
|
-
await invoke({
|
|
93
|
-
request: ctx.request,
|
|
94
|
-
response: ctx.response,
|
|
95
|
-
ctx
|
|
96
|
-
});
|
|
113
|
+
const app = this.getPlatformApplication();
|
|
114
|
+
app.use(async (koaContext, next) => {
|
|
115
|
+
const $ctx = getContext();
|
|
116
|
+
$ctx.upgrade({ request: koaContext.request, response: koaContext.response, koaContext });
|
|
97
117
|
return next();
|
|
98
118
|
});
|
|
99
119
|
return this;
|
|
@@ -108,18 +128,6 @@ export class PlatformKoa {
|
|
|
108
128
|
}
|
|
109
129
|
};
|
|
110
130
|
}
|
|
111
|
-
router(routerOptions = {}) {
|
|
112
|
-
var _a;
|
|
113
|
-
const { settings } = this.injector;
|
|
114
|
-
const options = Object.assign({}, ((_a = settings.koa) === null || _a === void 0 ? void 0 : _a.router) || {}, routerOptions);
|
|
115
|
-
const router = new KoaRouter(options);
|
|
116
|
-
return {
|
|
117
|
-
router,
|
|
118
|
-
callback() {
|
|
119
|
-
return [router.routes(), router.allowedMethods()];
|
|
120
|
-
}
|
|
121
|
-
};
|
|
122
|
-
}
|
|
123
131
|
multipart(options) {
|
|
124
132
|
return getMulter(options);
|
|
125
133
|
}
|
|
@@ -136,5 +144,8 @@ export class PlatformKoa {
|
|
|
136
144
|
}
|
|
137
145
|
return parser({ ...options, ...additionalOptions });
|
|
138
146
|
}
|
|
147
|
+
getPlatformApplication() {
|
|
148
|
+
return this.injector.get(PlatformApplication);
|
|
149
|
+
}
|
|
139
150
|
}
|
|
140
151
|
//# sourceMappingURL=PlatformKoa.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"PlatformKoa.js","sourceRoot":"","sources":["../../../src/components/PlatformKoa.ts"],"names":[],"mappings":"AAAA,OAAO,SAA8C,MAAM,aAAa,CAAC;AACzE,OAAO,EACL,aAAa,EAGb,mBAAmB,EACnB,eAAe,EACf,eAAe,EAGf,eAAe,EACf,gBAAgB,EAEjB,MAAM,cAAc,CAAC;AACtB,OAAO,EAAC,kBAAkB,EAAC,MAAM,2BAA2B,CAAC;AAC7D,OAAO,E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,QAAQ;gBACN,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,EAAE,MAAM,CAAC,cAAc,EAAE,CAAC,CAAC;YACpD,CAAC;SACF,CAAC;IACJ,CAAC;IAED,SAAS,CAAC,OAA+B;QACvC,OAAO,SAAS,CAAC,OAAO,CAAC,CAAC;IAC5B,CAAC;IAED,OAAO,CAAC,QAAgB,EAAE,OAA+B;QACvD,OAAO,iBAAiB,CAAC,OAAO,CAAC,CAAC;IACpC,CAAC;IAED,UAAU,CAAC,IAA4C,EAAE,oBAAyB,EAAE;QAClF,MAAM,IAAI,GAAG,IAAI,CAAC,QAAQ,CAAC,QAAQ,CAAC,GAAG,CAAC,gBAAgB,CAAC,CAAC;QAC1D,IAAI,MAAM,GAAQ,aAAa,CAAC;QAEhC,IAAI,OAAO,GAAY,EAAE,CAAC;QAE1B,IAAI,UAAU,CAAC,IAAI,CAAC,EAAE;YACpB,MAAM,GAAG,IAAI,CAAC;YACd,OAAO,GAAG,EAAE,CAAC;SACd;QAED,OAAO,MAAM,CAAC,EAAC,GAAG,OAAO,EAAE,GAAG,iBAAiB,EAAC,CAAC,CAAC;IACpD,CAAC;CACF","sourcesContent":["import KoaRouter, {RouterOptions as KoaRouterOptions} from \"@koa/router\";\nimport {\n createContext,\n InjectorService,\n PlatformAdapter,\n PlatformApplication,\n PlatformBuilder,\n PlatformHandler,\n PlatformMulter,\n PlatformMulterSettings,\n PlatformRequest,\n PlatformResponse,\n PlatformStaticsOptions\n} from \"@tsed/common\";\nimport {PlatformExceptions} from \"@tsed/platform-exceptions\";\nimport {isFunction, Type} from \"@tsed/core\";\nimport Koa, {Context, Next} from \"koa\";\nimport {resourceNotFoundMiddleware} from \"../middlewares/resourceNotFoundMiddleware\";\nimport {PlatformKoaResponse} from \"../services/PlatformKoaResponse\";\nimport {PlatformKoaRequest} from \"../services/PlatformKoaRequest\";\nimport {PlatformKoaHandler} from \"../services/PlatformKoaHandler\";\nimport {getMulter} from \"../utils/multer\";\nimport {staticsMiddleware} from \"../middlewares/staticsMiddleware\";\nimport send from \"koa-send\";\n// @ts-ignore\nimport koaQs from \"koa-qs\";\nimport koaBodyParser, {Options} from \"koa-bodyparser\";\n\ndeclare global {\n namespace TsED {\n export interface Application extends Koa {}\n }\n\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\n/**\n * @platform\n * @koa\n */\nexport class PlatformKoa implements PlatformAdapter<Koa, KoaRouter> {\n readonly providers = [\n {\n provide: PlatformResponse,\n useClass: PlatformKoaResponse\n },\n {\n provide: PlatformRequest,\n useClass: PlatformKoaRequest\n },\n {\n provide: PlatformHandler,\n useClass: PlatformKoaHandler\n }\n ];\n\n constructor(private injector: InjectorService) {}\n\n /**\n * Create new serverless application. In this mode, the component scan are disabled.\n * @param module\n * @param settings\n */\n static create(module: Type<any>, settings: Partial<TsED.Configuration> = {}) {\n return PlatformBuilder.create<Koa, KoaRouter>(module, {\n ...settings,\n adapter: PlatformKoa\n });\n }\n\n /**\n * Bootstrap a server application\n * @param module\n * @param settings\n */\n static async bootstrap(module: Type<any>, settings: Partial<TsED.Configuration> = {}) {\n return PlatformBuilder.bootstrap<Koa, KoaRouter>(module, {\n ...settings,\n adapter: PlatformKoa\n });\n }\n\n onInit() {\n const injector = this.injector;\n const app = this.injector.get<PlatformApplication>(PlatformApplication)!;\n\n const listener: any = (error: any, ctx: Koa.Context) => {\n injector.get<PlatformExceptions>(PlatformExceptions)?.catch(error, ctx.request.$ctx);\n };\n\n app.getApp().silent = true;\n app.getApp().on(\"error\", listener);\n }\n\n useRouter(): this {\n const app = this.injector.get<PlatformApplication<Koa>>(PlatformApplication)!;\n\n app.getApp().use(resourceNotFoundMiddleware);\n app.getApp().use(app.getRouter().routes()).use(app.getRouter().allowedMethods());\n\n return this;\n }\n\n useContext(): this {\n const app = this.injector.get<PlatformApplication<Koa>>(PlatformApplication)!;\n const {logger} = this.injector;\n logger.info(\"Mount app context\");\n\n const invoke = createContext(this.injector);\n\n app.getApp().use(async (ctx: Context, next: Next) => {\n await invoke({\n request: ctx.request as any,\n response: ctx.response as any,\n ctx\n });\n\n return next();\n });\n\n return this;\n }\n\n app() {\n const app = this.injector.settings.get(\"koa.app\") || new Koa();\n koaQs(app, \"extended\");\n\n return {\n app,\n callback() {\n return app.callback();\n }\n };\n }\n\n router(routerOptions: any = {}) {\n const {settings} = this.injector;\n\n const options = Object.assign({}, settings.koa?.router || {}, routerOptions);\n const router = new KoaRouter(options) as any;\n\n return {\n router,\n callback() {\n return [router.routes(), router.allowedMethods()];\n }\n };\n }\n\n multipart(options: PlatformMulterSettings): PlatformMulter {\n return getMulter(options);\n }\n\n statics(endpoint: string, options: PlatformStaticsOptions) {\n return staticsMiddleware(options);\n }\n\n bodyParser(type: \"json\" | \"urlencoded\" | \"raw\" | \"text\", additionalOptions: any = {}): any {\n const opts = this.injector.settings.get(`koa.bodyParser`);\n let parser: any = koaBodyParser;\n\n let options: Options = {};\n\n if (isFunction(opts)) {\n parser = opts;\n options = {};\n }\n\n return parser({...options, ...additionalOptions});\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"PlatformKoa.js","sourceRoot":"","sources":["../../../src/components/PlatformKoa.ts"],"names":[],"mappings":"AAAA,OAAO,SAA8C,MAAM,aAAa,CAAC;AACzE,OAAO,EACL,UAAU,EAGV,mBAAmB,EACnB,eAAe,EAEf,eAAe,EAGf,eAAe,EACf,gBAAgB,EAEjB,MAAM,cAAc,CAAC;AACtB,OAAO,EAAC,kBAAkB,EAAC,MAAM,2BAA2B,CAAC;AAC7D,OAAO,EAAC,UAAU,EAAO,MAAM,YAAY,CAAC;AAE5C,OAAO,GAAoB,MAAM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,EAAC,CAAC,CAAC;YAErG,OAAO,IAAI,EAAE,CAAC;QAChB,CAAC,CAAC,CAAC;QAEH,OAAO,IAAI,CAAC;IACd,CAAC;IAED,GAAG;QACD,MAAM,GAAG,GAAG,IAAI,CAAC,QAAQ,CAAC,QAAQ,CAAC,GAAG,CAAC,SAAS,CAAC,IAAI,IAAI,GAAG,EAAE,CAAC;QAC/D,KAAK,CAAC,GAAG,EAAE,UAAU,CAAC,CAAC;QAEvB,OAAO;YACL,GAAG;YACH,QAAQ;gBACN,OAAO,GAAG,CAAC,QAAQ,EAAE,CAAC;YACxB,CAAC;SACF,CAAC;IACJ,CAAC;IAED,SAAS,CAAC,OAA+B;QACvC,OAAO,SAAS,CAAC,OAAO,CAAC,CAAC;IAC5B,CAAC;IAED,OAAO,CAAC,QAAgB,EAAE,OAA+B;QACvD,OAAO,iBAAiB,CAAC,OAAO,CAAC,CAAC;IACpC,CAAC;IAED,UAAU,CAAC,IAA4C,EAAE,oBAAyB,EAAE;QAClF,MAAM,IAAI,GAAG,IAAI,CAAC,QAAQ,CAAC,QAAQ,CAAC,GAAG,CAAC,gBAAgB,CAAC,CAAC;QAC1D,IAAI,MAAM,GAAQ,aAAa,CAAC;QAEhC,IAAI,OAAO,GAAY,EAAE,CAAC;QAE1B,IAAI,UAAU,CAAC,IAAI,CAAC,EAAE;YACpB,MAAM,GAAG,IAAI,CAAC;YACd,OAAO,GAAG,EAAE,CAAC;SACd;QAED,OAAO,MAAM,CAAC,EAAC,GAAG,OAAO,EAAE,GAAG,iBAAiB,EAAC,CAAC,CAAC;IACpD,CAAC;IAEO,sBAAsB;QAC5B,OAAO,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAsB,mBAAmB,CAAE,CAAC;IACtE,CAAC;CACF","sourcesContent":["import KoaRouter, {RouterOptions as KoaRouterOptions} from \"@koa/router\";\nimport {\n getContext,\n InjectorService,\n PlatformAdapter,\n PlatformApplication,\n PlatformBuilder,\n PlatformContext,\n PlatformHandler,\n PlatformMulter,\n PlatformMulterSettings,\n PlatformRequest,\n PlatformResponse,\n PlatformStaticsOptions\n} from \"@tsed/common\";\nimport {PlatformExceptions} from \"@tsed/platform-exceptions\";\nimport {isFunction, Type} from \"@tsed/core\";\nimport {PlatformHandlerMetadata, PlatformLayer} from \"@tsed/platform-router\";\nimport Koa, {Context, Next} from \"koa\";\nimport koaBodyParser, {Options} from \"koa-bodyparser\";\n// @ts-ignore\nimport koaQs from \"koa-qs\";\nimport send from \"koa-send\";\nimport {resourceNotFoundMiddleware} from \"../middlewares/resourceNotFoundMiddleware\";\nimport {staticsMiddleware} from \"../middlewares/staticsMiddleware\";\nimport {PlatformKoaHandler} from \"../services/PlatformKoaHandler\";\nimport {PlatformKoaRequest} from \"../services/PlatformKoaRequest\";\nimport {PlatformKoaResponse} from \"../services/PlatformKoaResponse\";\nimport {getMulter} from \"../utils/multer\";\n\ndeclare global {\n namespace TsED {\n export interface Application extends Koa {}\n }\n\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\n/**\n * @platform\n * @koa\n */\nexport class PlatformKoa implements PlatformAdapter<Koa> {\n readonly providers = [\n {\n provide: PlatformResponse,\n useClass: PlatformKoaResponse\n },\n {\n provide: PlatformRequest,\n useClass: PlatformKoaRequest\n },\n {\n provide: PlatformHandler,\n useClass: PlatformKoaHandler\n }\n ];\n\n constructor(private injector: InjectorService) {}\n\n /**\n * Create new serverless application. In this mode, the component scan are disabled.\n * @param module\n * @param settings\n */\n static create(module: Type<any>, settings: Partial<TsED.Configuration> = {}) {\n return PlatformBuilder.create<Koa>(module, {\n ...settings,\n adapter: PlatformKoa\n });\n }\n\n /**\n * Bootstrap a server application\n * @param module\n * @param settings\n */\n static async bootstrap(module: Type<any>, settings: Partial<TsED.Configuration> = {}) {\n return PlatformBuilder.bootstrap<Koa>(module, {\n ...settings,\n adapter: PlatformKoa\n });\n }\n\n onInit() {\n const injector = this.injector;\n const app = this.getPlatformApplication();\n\n const listener: any = (error: any, ctx: Koa.Context) => {\n const $ctx = getContext<PlatformContext>()!;\n injector.get<PlatformExceptions>(PlatformExceptions)?.catch(error, $ctx);\n };\n\n app.getApp().silent = true;\n app.getApp().on(\"error\", listener);\n }\n\n async beforeLoadRoutes() {\n const app = this.injector.get<PlatformApplication<Koa>>(PlatformApplication)!;\n\n this.useContext();\n\n app.use(resourceNotFoundMiddleware);\n }\n\n mapLayers(layers: PlatformLayer[]) {\n const {settings} = this.injector;\n const app = this.getPlatformApplication();\n const options = settings.get(\"koa.router\", {});\n const rawRouter = new KoaRouter(options) as any;\n\n layers.forEach((layer) => {\n switch (layer.method) {\n case \"statics\":\n rawRouter.use(layer.path, this.statics(layer.path as string, layer.opts as any));\n break;\n\n default:\n rawRouter[layer.method](...layer.getArgs());\n }\n });\n\n app.getApp().use(rawRouter.routes()).use(rawRouter.allowedMethods());\n }\n\n mapHandler(handler: Function, metadata: PlatformHandlerMetadata) {\n if (metadata.isRawMiddleware()) {\n return handler;\n }\n\n return async (koaContext: Koa.Context, next: Koa.Next) => {\n const $ctx = getContext<PlatformContext>()!;\n $ctx.next = next;\n\n await handler($ctx);\n };\n }\n\n useContext(): this {\n const app = this.getPlatformApplication();\n\n app.use(async (koaContext: Context, next: Next) => {\n const $ctx = getContext<PlatformContext<PlatformKoaRequest, PlatformKoaResponse>>()!;\n\n $ctx.upgrade({request: koaContext.request as any, response: koaContext.response as any, koaContext});\n\n return next();\n });\n\n return this;\n }\n\n app() {\n const app = this.injector.settings.get(\"koa.app\") || new Koa();\n koaQs(app, \"extended\");\n\n return {\n app,\n callback() {\n return app.callback();\n }\n };\n }\n\n multipart(options: PlatformMulterSettings): PlatformMulter {\n return getMulter(options);\n }\n\n statics(endpoint: string, options: PlatformStaticsOptions) {\n return staticsMiddleware(options);\n }\n\n bodyParser(type: \"json\" | \"urlencoded\" | \"raw\" | \"text\", additionalOptions: any = {}): any {\n const opts = this.injector.settings.get(`koa.bodyParser`);\n let parser: any = koaBodyParser;\n\n let options: Options = {};\n\n if (isFunction(opts)) {\n parser = opts;\n options = {};\n }\n\n return parser({...options, ...additionalOptions});\n }\n\n private getPlatformApplication() {\n return this.injector.get<PlatformApplication>(PlatformApplication)!;\n }\n}\n"]}
|
package/lib/esm/index.js
CHANGED
|
@@ -3,11 +3,8 @@
|
|
|
3
3
|
*/
|
|
4
4
|
export * from "./exports.js";
|
|
5
5
|
export * from "./components/PlatformKoa.js";
|
|
6
|
-
export * from "./decorators/caseSensitive.js";
|
|
7
6
|
export * from "./decorators/ctx.js";
|
|
8
|
-
export * from "./decorators/routerSettings.js";
|
|
9
7
|
export * from "./decorators/state.js";
|
|
10
|
-
export * from "./decorators/strict.js";
|
|
11
8
|
export * from "./interfaces/PlatformKoaSettings.js";
|
|
12
9
|
export * from "./interfaces/interfaces.js";
|
|
13
10
|
export * from "./middlewares/resourceNotFoundMiddleware.js";
|
package/lib/esm/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,cAAc,WAAW,CAAC;AAC1B,cAAc,0BAA0B,CAAC;AACzC,cAAc,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,cAAc,WAAW,CAAC;AAC1B,cAAc,0BAA0B,CAAC;AACzC,cAAc,kBAAkB,CAAC;AACjC,cAAc,oBAAoB,CAAC;AACnC,cAAc,kCAAkC,CAAC;AACjD,cAAc,yBAAyB,CAAC;AACxC,cAAc,0CAA0C,CAAC;AACzD,cAAc,iCAAiC,CAAC;AAChD,cAAc,+BAA+B,CAAC;AAC9C,cAAc,+BAA+B,CAAC;AAC9C,cAAc,gCAAgC,CAAC;AAC/C,cAAc,gBAAgB,CAAC","sourcesContent":["/**\n * @file Automatically generated by barrelsby.\n */\n\nexport * from \"./exports\";\nexport * from \"./components/PlatformKoa\";\nexport * from \"./decorators/ctx\";\nexport * from \"./decorators/state\";\nexport * from \"./interfaces/PlatformKoaSettings\";\nexport * from \"./interfaces/interfaces\";\nexport * from \"./middlewares/resourceNotFoundMiddleware\";\nexport * from \"./middlewares/staticsMiddleware\";\nexport * from \"./services/PlatformKoaHandler\";\nexport * from \"./services/PlatformKoaRequest\";\nexport * from \"./services/PlatformKoaResponse\";\nexport * from \"./utils/multer\";\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"PlatformKoaSettings.js","sourceRoot":"","sources":["../../../src/interfaces/PlatformKoaSettings.ts"],"names":[],"mappings":"","sourcesContent":["import type {RouterOptions} from \"@koa/router\";\nimport type {Middleware} from \"koa\";\nimport type {Options} from \"koa-bodyparser\";\n\nexport interface PlatformKoaSettings {\n /**\n * Koa router options\n */\n router?: RouterOptions;\n /**\n * Body parser options\n * @param opts\n */\n bodyParser?: (opts?: Options) => Middleware | Options;\n}\n"]}
|
|
1
|
+
{"version":3,"file":"PlatformKoaSettings.js","sourceRoot":"","sources":["../../../src/interfaces/PlatformKoaSettings.ts"],"names":[],"mappings":"","sourcesContent":["import type {RouterOptions} from \"@koa/router\";\nimport type {Middleware} from \"koa\";\nimport type {Options} from \"koa-bodyparser\";\n\nexport interface PlatformKoaSettings {\n /**\n * Koa router options\n */\n router?: RouterOptions;\n /**\n * Body parser options\n * @param opts\n */\n bodyParser?: ((opts?: Options) => Middleware) | Options;\n}\n"]}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { getContext } from "@tsed/di";
|
|
1
2
|
import { PlatformExceptions } from "@tsed/platform-exceptions";
|
|
2
3
|
/**
|
|
3
4
|
* @ignore
|
|
@@ -5,12 +6,11 @@ import { PlatformExceptions } from "@tsed/platform-exceptions";
|
|
|
5
6
|
* @param next
|
|
6
7
|
*/
|
|
7
8
|
export async function resourceNotFoundMiddleware(ctx, next) {
|
|
8
|
-
|
|
9
|
-
const $ctx = ctx.request.$ctx;
|
|
9
|
+
const $ctx = getContext();
|
|
10
10
|
await next();
|
|
11
11
|
const status = ctx.status || 404;
|
|
12
12
|
if (status === 404 && !$ctx.isDone()) {
|
|
13
|
-
|
|
13
|
+
$ctx.injector.get(PlatformExceptions)?.resourceNotFound($ctx);
|
|
14
14
|
}
|
|
15
15
|
}
|
|
16
16
|
//# sourceMappingURL=resourceNotFoundMiddleware.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"resourceNotFoundMiddleware.js","sourceRoot":"","sources":["../../../src/middlewares/resourceNotFoundMiddleware.ts"],"names":[],"mappings":"AACA,OAAO,EAAC,kBAAkB,EAAC,MAAM,2BAA2B,CAAC;AAE7D;;;;GAIG;AACH,MAAM,CAAC,KAAK,UAAU,0BAA0B,CAAC,GAAQ,EAAE,IAAS
|
|
1
|
+
{"version":3,"file":"resourceNotFoundMiddleware.js","sourceRoot":"","sources":["../../../src/middlewares/resourceNotFoundMiddleware.ts"],"names":[],"mappings":"AACA,OAAO,EAAC,UAAU,EAAC,MAAM,UAAU,CAAC;AACpC,OAAO,EAAC,kBAAkB,EAAC,MAAM,2BAA2B,CAAC;AAE7D;;;;GAIG;AACH,MAAM,CAAC,KAAK,UAAU,0BAA0B,CAAC,GAAQ,EAAE,IAAS;IAClE,MAAM,IAAI,GAAG,UAAU,EAAoB,CAAC;IAC5C,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,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAqB,kBAAkB,CAAC,EAAE,gBAAgB,CAAC,IAAI,CAAC,CAAC;KACnF;AACH,CAAC","sourcesContent":["import {PlatformContext} from \"@tsed/common\";\nimport {getContext} from \"@tsed/di\";\nimport {PlatformExceptions} from \"@tsed/platform-exceptions\";\n\n/**\n * @ignore\n * @param ctx\n * @param next\n */\nexport async function resourceNotFoundMiddleware(ctx: any, next: any) {\n const $ctx = getContext<PlatformContext>()!;\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"]}
|
|
@@ -1,56 +1,28 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { PlatformHandler } from "@tsed/common";
|
|
2
|
+
import { isStream } from "@tsed/core";
|
|
2
3
|
import "./PlatformKoaRequest.js";
|
|
3
|
-
const OVERRIDE_TYPES = [HandlerType.ENDPOINT, HandlerType.MIDDLEWARE, HandlerType.ERR_MIDDLEWARE, HandlerType.CTX_FN];
|
|
4
4
|
export class PlatformKoaHandler extends PlatformHandler {
|
|
5
|
-
async flush(
|
|
6
|
-
if (data === undefined && ctx.getResponse().body) {
|
|
7
|
-
data = ctx.getResponse().body;
|
|
5
|
+
async flush($ctx) {
|
|
6
|
+
if ($ctx.data === undefined && $ctx.getResponse().body) {
|
|
7
|
+
$ctx.data = $ctx.getResponse().body;
|
|
8
8
|
}
|
|
9
|
-
return super.flush(
|
|
9
|
+
return super.flush($ctx);
|
|
10
10
|
}
|
|
11
|
-
|
|
12
|
-
if (
|
|
13
|
-
|
|
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
|
-
}
|
|
11
|
+
async onRequest(handler, $ctx) {
|
|
12
|
+
if ($ctx.error instanceof Error && !$ctx.handlerMetadata.hasErrorParam) {
|
|
13
|
+
return;
|
|
27
14
|
}
|
|
28
|
-
return super.onRequest(
|
|
15
|
+
return super.onRequest(handler, $ctx);
|
|
29
16
|
}
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
// istanbul ignore next
|
|
33
|
-
if ($ctx.isDone()) {
|
|
17
|
+
next(error, next, $ctx) {
|
|
18
|
+
if (isStream($ctx.data) || $ctx.isDone()) {
|
|
34
19
|
return;
|
|
35
20
|
}
|
|
36
|
-
$ctx.
|
|
37
|
-
|
|
38
|
-
this.throwError(error, $ctx);
|
|
21
|
+
if (error && !($ctx.handlerMetadata.isEndpoint() && !$ctx.handlerMetadata.isFinal())) {
|
|
22
|
+
$ctx.getApp().emit("error", error, $ctx.getRequest().ctx);
|
|
39
23
|
return;
|
|
40
24
|
}
|
|
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);
|
|
25
|
+
return !$ctx.handlerMetadata.isFinal() && next();
|
|
54
26
|
}
|
|
55
27
|
}
|
|
56
28
|
//# sourceMappingURL=PlatformKoaHandler.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"PlatformKoaHandler.js","sourceRoot":"","sources":["../../../src/services/PlatformKoaHandler.ts"],"names":[],"mappings":"AAAA,OAAO,EAAkB,
|
|
1
|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AAE,IAAI,IAAI,EAAE,CAAC;IACnD,CAAC;CACF","sourcesContent":["import {PlatformContext, PlatformHandler, PlatformParamsCallback} from \"@tsed/common\";\nimport {isStream} from \"@tsed/core\";\nimport {PlatformContextHandler} from \"@tsed/platform-router\";\nimport \"./PlatformKoaRequest\";\n\nexport class PlatformKoaHandler extends PlatformHandler {\n public async flush($ctx: PlatformContext) {\n if ($ctx.data === undefined && $ctx.getResponse().body) {\n $ctx.data = $ctx.getResponse().body;\n }\n\n return super.flush($ctx);\n }\n\n async onRequest(handler: PlatformParamsCallback, $ctx: PlatformContext): Promise<any> {\n if ($ctx.error instanceof Error && !$ctx.handlerMetadata.hasErrorParam) {\n return;\n }\n\n return super.onRequest(handler, $ctx);\n }\n\n public next(error: unknown, next: any, $ctx: PlatformContext) {\n if (isStream($ctx.data) || $ctx.isDone()) {\n return;\n }\n\n if (error && !($ctx.handlerMetadata.isEndpoint() && !$ctx.handlerMetadata.isFinal())) {\n $ctx.getApp().emit(\"error\", error, $ctx.getRequest().ctx);\n return;\n }\n\n return !$ctx.handlerMetadata.isFinal() && next();\n }\n}\n"]}
|
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
var _PlatformKoaRequest_ctx;
|
|
2
|
-
import { __classPrivateFieldGet, __classPrivateFieldSet } from "tslib";
|
|
3
1
|
import "@koa/router";
|
|
4
2
|
import { PlatformRequest } from "@tsed/common";
|
|
5
3
|
/**
|
|
@@ -7,34 +5,26 @@ import { PlatformRequest } from "@tsed/common";
|
|
|
7
5
|
* @koa
|
|
8
6
|
*/
|
|
9
7
|
export class PlatformKoaRequest extends PlatformRequest {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
_PlatformKoaRequest_ctx.set(this, void 0);
|
|
13
|
-
__classPrivateFieldSet(this, _PlatformKoaRequest_ctx, this.raw.ctx, "f");
|
|
8
|
+
get ctx() {
|
|
9
|
+
return this.$ctx.event.koaContext;
|
|
14
10
|
}
|
|
15
11
|
get protocol() {
|
|
16
|
-
return
|
|
12
|
+
return this.ctx.request.protocol;
|
|
17
13
|
}
|
|
18
14
|
get host() {
|
|
19
|
-
return
|
|
15
|
+
return this.ctx.request.host;
|
|
20
16
|
}
|
|
21
17
|
get secure() {
|
|
22
|
-
return
|
|
18
|
+
return this.ctx.request.secure;
|
|
23
19
|
}
|
|
24
20
|
get cookies() {
|
|
25
|
-
return
|
|
21
|
+
return this.ctx.cookie || this.ctx.cookies;
|
|
26
22
|
}
|
|
27
23
|
get session() {
|
|
28
|
-
return
|
|
24
|
+
return this.ctx.session;
|
|
29
25
|
}
|
|
30
26
|
getReq() {
|
|
31
|
-
return this.
|
|
32
|
-
}
|
|
33
|
-
destroy() {
|
|
34
|
-
// @ts-ignore
|
|
35
|
-
__classPrivateFieldSet(this, _PlatformKoaRequest_ctx, null, "f");
|
|
36
|
-
return super.destroy();
|
|
27
|
+
return this.ctx.request.req;
|
|
37
28
|
}
|
|
38
29
|
}
|
|
39
|
-
_PlatformKoaRequest_ctx = new WeakMap();
|
|
40
30
|
//# sourceMappingURL=PlatformKoaRequest.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"PlatformKoaRequest.js","sourceRoot":"","sources":["../../../src/services/PlatformKoaRequest.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"PlatformKoaRequest.js","sourceRoot":"","sources":["../../../src/services/PlatformKoaRequest.ts"],"names":[],"mappings":"AAAA,OAAO,aAAa,CAAC;AACrB,OAAO,EAAkB,eAAe,EAAC,MAAM,cAAc,CAAC;AAgB9D;;;GAGG;AACH,MAAM,OAAO,kBAAmB,SAAQ,eAA4B;IAClE,IAAI,GAAG;QACL,OAAO,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,UAAyB,CAAC;IACnD,CAAC;IAED,IAAI,QAAQ;QACV,OAAO,IAAI,CAAC,GAAG,CAAC,OAAO,CAAC,QAAQ,CAAC;IACnC,CAAC;IAED,IAAI,IAAI;QACN,OAAO,IAAI,CAAC,GAAG,CAAC,OAAO,CAAC,IAAI,CAAC;IAC/B,CAAC;IAED,IAAI,MAAM;QACR,OAAO,IAAI,CAAC,GAAG,CAAC,OAAO,CAAC,MAAM,CAAC;IACjC,CAAC;IAED,IAAI,OAAO;QACT,OAAO,IAAI,CAAC,GAAG,CAAC,MAAM,IAAI,IAAI,CAAC,GAAG,CAAC,OAAO,CAAC;IAC7C,CAAC;IAED,IAAI,OAAO;QACT,OAAO,IAAI,CAAC,GAAG,CAAC,OAAO,CAAC;IAC1B,CAAC;IAED,MAAM;QACJ,OAAO,IAAI,CAAC,GAAG,CAAC,OAAO,CAAC,GAAG,CAAC;IAC9B,CAAC;CACF","sourcesContent":["import \"@koa/router\";\nimport {PlatformContext, PlatformRequest} from \"@tsed/common\";\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 get ctx(): Koa.Context {\n return this.$ctx.event.koaContext as Koa.Context;\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() {\n return this.ctx.request.req;\n }\n}\n"]}
|
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
var _PlatformKoaResponse_ctx;
|
|
2
|
-
import { __classPrivateFieldGet, __classPrivateFieldSet } from "tslib";
|
|
3
1
|
import { PlatformResponse } from "@tsed/common";
|
|
4
2
|
import { getStatusMessage } from "@tsed/schema";
|
|
5
3
|
import encodeUrl from "encodeurl";
|
|
@@ -8,16 +6,14 @@ import encodeUrl from "encodeurl";
|
|
|
8
6
|
* @koa
|
|
9
7
|
*/
|
|
10
8
|
export class PlatformKoaResponse extends PlatformResponse {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
_PlatformKoaResponse_ctx.set(this, void 0);
|
|
14
|
-
__classPrivateFieldSet(this, _PlatformKoaResponse_ctx, this.raw.ctx, "f");
|
|
9
|
+
get ctx() {
|
|
10
|
+
return this.raw.ctx;
|
|
15
11
|
}
|
|
16
12
|
get statusCode() {
|
|
17
13
|
return this.raw.status;
|
|
18
14
|
}
|
|
19
15
|
get locals() {
|
|
20
|
-
return
|
|
16
|
+
return this.ctx.state;
|
|
21
17
|
}
|
|
22
18
|
/**
|
|
23
19
|
* Return the Node.js response object
|
|
@@ -97,5 +93,4 @@ export class PlatformKoaResponse extends PlatformResponse {
|
|
|
97
93
|
return this;
|
|
98
94
|
}
|
|
99
95
|
}
|
|
100
|
-
_PlatformKoaResponse_ctx = new WeakMap();
|
|
101
96
|
//# sourceMappingURL=PlatformKoaResponse.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"PlatformKoaResponse.js","sourceRoot":"","sources":["../../../src/services/PlatformKoaResponse.ts"],"names":[],"mappings":"
|
|
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;AAC9C,OAAO,SAAS,MAAM,WAAW,CAAC;AAQlC;;;GAGG;AACH,MAAM,OAAO,mBAAoB,SAAQ,gBAA8B;IACrE,IAAI,GAAG;QACL,OAAO,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC;IACtB,CAAC;IAED,IAAI,UAAU;QACZ,OAAO,IAAI,CAAC,GAAG,CAAC,MAAM,CAAC;IACzB,CAAC;IAED,IAAI,MAAM;QACR,OAAO,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC;IACxB,CAAC;I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,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\";\nimport encodeUrl from \"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 get ctx() {\n return 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(): any {\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 as any));\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"]}
|
|
@@ -1,20 +1,19 @@
|
|
|
1
1
|
/// <reference types="koa__router" />
|
|
2
|
-
import
|
|
2
|
+
import { RouterOptions as KoaRouterOptions } from "@koa/router";
|
|
3
3
|
import { InjectorService, PlatformAdapter, PlatformBuilder, PlatformHandler, PlatformMulter, PlatformMulterSettings, PlatformRequest, PlatformResponse, PlatformStaticsOptions } from "@tsed/common";
|
|
4
4
|
import { Type } from "@tsed/core";
|
|
5
|
+
import { PlatformHandlerMetadata, PlatformLayer } from "@tsed/platform-router";
|
|
5
6
|
import Koa from "koa";
|
|
6
|
-
import { PlatformKoaResponse } from "../services/PlatformKoaResponse";
|
|
7
|
-
import { PlatformKoaRequest } from "../services/PlatformKoaRequest";
|
|
8
|
-
import { PlatformKoaHandler } from "../services/PlatformKoaHandler";
|
|
9
7
|
import send from "koa-send";
|
|
8
|
+
import { PlatformKoaHandler } from "../services/PlatformKoaHandler";
|
|
9
|
+
import { PlatformKoaRequest } from "../services/PlatformKoaRequest";
|
|
10
|
+
import { PlatformKoaResponse } from "../services/PlatformKoaResponse";
|
|
10
11
|
declare global {
|
|
11
12
|
namespace TsED {
|
|
12
13
|
interface Application extends Koa {
|
|
13
14
|
}
|
|
14
15
|
}
|
|
15
16
|
namespace TsED {
|
|
16
|
-
interface Router extends KoaRouter {
|
|
17
|
-
}
|
|
18
17
|
interface RouterOptions extends KoaRouterOptions {
|
|
19
18
|
}
|
|
20
19
|
interface StaticsOptions extends send.SendOptions {
|
|
@@ -25,7 +24,7 @@ declare global {
|
|
|
25
24
|
* @platform
|
|
26
25
|
* @koa
|
|
27
26
|
*/
|
|
28
|
-
export declare class PlatformKoa implements PlatformAdapter<Koa
|
|
27
|
+
export declare class PlatformKoa implements PlatformAdapter<Koa> {
|
|
29
28
|
private injector;
|
|
30
29
|
readonly providers: ({
|
|
31
30
|
provide: typeof PlatformResponse;
|
|
@@ -43,25 +42,24 @@ export declare class PlatformKoa implements PlatformAdapter<Koa, KoaRouter> {
|
|
|
43
42
|
* @param module
|
|
44
43
|
* @param settings
|
|
45
44
|
*/
|
|
46
|
-
static create(module: Type<any>, settings?: Partial<TsED.Configuration>): PlatformBuilder<Koa<Koa.DefaultState, Koa.DefaultContext
|
|
45
|
+
static create(module: Type<any>, settings?: Partial<TsED.Configuration>): PlatformBuilder<Koa<Koa.DefaultState, Koa.DefaultContext>>;
|
|
47
46
|
/**
|
|
48
47
|
* Bootstrap a server application
|
|
49
48
|
* @param module
|
|
50
49
|
* @param settings
|
|
51
50
|
*/
|
|
52
|
-
static bootstrap(module: Type<any>, settings?: Partial<TsED.Configuration>): Promise<PlatformBuilder<Koa<Koa.DefaultState, Koa.DefaultContext
|
|
51
|
+
static bootstrap(module: Type<any>, settings?: Partial<TsED.Configuration>): Promise<PlatformBuilder<Koa<Koa.DefaultState, Koa.DefaultContext>>>;
|
|
53
52
|
onInit(): void;
|
|
54
|
-
|
|
53
|
+
beforeLoadRoutes(): Promise<void>;
|
|
54
|
+
mapLayers(layers: PlatformLayer[]): void;
|
|
55
|
+
mapHandler(handler: Function, metadata: PlatformHandlerMetadata): Function;
|
|
55
56
|
useContext(): this;
|
|
56
57
|
app(): {
|
|
57
58
|
app: any;
|
|
58
59
|
callback(): any;
|
|
59
60
|
};
|
|
60
|
-
router(routerOptions?: any): {
|
|
61
|
-
router: any;
|
|
62
|
-
callback(): any[];
|
|
63
|
-
};
|
|
64
61
|
multipart(options: PlatformMulterSettings): PlatformMulter;
|
|
65
62
|
statics(endpoint: string, options: PlatformStaticsOptions): any;
|
|
66
63
|
bodyParser(type: "json" | "urlencoded" | "raw" | "text", additionalOptions?: any): any;
|
|
64
|
+
private getPlatformApplication;
|
|
67
65
|
}
|
package/lib/types/index.d.ts
CHANGED
|
@@ -3,11 +3,8 @@
|
|
|
3
3
|
*/
|
|
4
4
|
export * from "./exports";
|
|
5
5
|
export * from "./components/PlatformKoa";
|
|
6
|
-
export * from "./decorators/caseSensitive";
|
|
7
6
|
export * from "./decorators/ctx";
|
|
8
|
-
export * from "./decorators/routerSettings";
|
|
9
7
|
export * from "./decorators/state";
|
|
10
|
-
export * from "./decorators/strict";
|
|
11
8
|
export * from "./interfaces/PlatformKoaSettings";
|
|
12
9
|
export * from "./interfaces/interfaces";
|
|
13
10
|
export * from "./middlewares/resourceNotFoundMiddleware";
|
|
@@ -1,10 +1,7 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { PlatformContext, PlatformHandler, PlatformParamsCallback } from "@tsed/common";
|
|
2
2
|
import "./PlatformKoaRequest";
|
|
3
3
|
export declare class PlatformKoaHandler extends PlatformHandler {
|
|
4
|
-
flush(
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
protected onError(error: unknown, requestOptions: OnRequestOptions): any;
|
|
8
|
-
protected onCtxRequest(requestOptions: OnRequestOptions): Promise<any>;
|
|
9
|
-
protected throwError(error: any, $ctx: PlatformContext): void;
|
|
4
|
+
flush($ctx: PlatformContext): Promise<import("@tsed/common").PlatformResponse<any>>;
|
|
5
|
+
onRequest(handler: PlatformParamsCallback, $ctx: PlatformContext): Promise<any>;
|
|
6
|
+
next(error: unknown, next: any, $ctx: PlatformContext): any;
|
|
10
7
|
}
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
2
|
import "@koa/router";
|
|
3
|
-
import {
|
|
4
|
-
import { IncomingMessage } from "http";
|
|
3
|
+
import { PlatformContext, PlatformRequest } from "@tsed/common";
|
|
5
4
|
import Koa from "koa";
|
|
6
5
|
declare module "koa" {
|
|
7
6
|
interface Request {
|
|
@@ -20,8 +19,7 @@ declare global {
|
|
|
20
19
|
* @koa
|
|
21
20
|
*/
|
|
22
21
|
export declare class PlatformKoaRequest extends PlatformRequest<Koa.Request> {
|
|
23
|
-
|
|
24
|
-
constructor(event: IncomingEvent, $ctx: PlatformContext);
|
|
22
|
+
get ctx(): Koa.Context;
|
|
25
23
|
get protocol(): string;
|
|
26
24
|
get host(): string;
|
|
27
25
|
get secure(): boolean;
|
|
@@ -29,6 +27,5 @@ export declare class PlatformKoaRequest extends PlatformRequest<Koa.Request> {
|
|
|
29
27
|
[p: string]: any;
|
|
30
28
|
};
|
|
31
29
|
get session(): any;
|
|
32
|
-
getReq(): IncomingMessage;
|
|
33
|
-
destroy(): void;
|
|
30
|
+
getReq(): import("http").IncomingMessage;
|
|
34
31
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
|
-
import {
|
|
2
|
+
import { PlatformResponse } from "@tsed/common";
|
|
3
3
|
import { ServerResponse } from "http";
|
|
4
4
|
import Koa from "koa";
|
|
5
5
|
declare global {
|
|
@@ -13,8 +13,7 @@ declare global {
|
|
|
13
13
|
* @koa
|
|
14
14
|
*/
|
|
15
15
|
export declare class PlatformKoaResponse extends PlatformResponse<Koa.Response> {
|
|
16
|
-
|
|
17
|
-
constructor(event: IncomingEvent, $ctx: PlatformContext);
|
|
16
|
+
get ctx(): Koa.Context;
|
|
18
17
|
get statusCode(): number;
|
|
19
18
|
get locals(): Koa.DefaultState;
|
|
20
19
|
/**
|