@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.
Files changed (50) hide show
  1. package/jest.config.js +14 -0
  2. package/lib/cjs/components/PlatformKoa.js +60 -49
  3. package/lib/cjs/components/PlatformKoa.js.map +1 -1
  4. package/lib/cjs/index.js +0 -3
  5. package/lib/cjs/index.js.map +1 -1
  6. package/lib/cjs/interfaces/PlatformKoaSettings.js.map +1 -1
  7. package/lib/cjs/middlewares/resourceNotFoundMiddleware.js +3 -3
  8. package/lib/cjs/middlewares/resourceNotFoundMiddleware.js.map +1 -1
  9. package/lib/cjs/services/PlatformKoaHandler.js +14 -42
  10. package/lib/cjs/services/PlatformKoaHandler.js.map +1 -1
  11. package/lib/cjs/services/PlatformKoaRequest.js +8 -18
  12. package/lib/cjs/services/PlatformKoaRequest.js.map +1 -1
  13. package/lib/cjs/services/PlatformKoaResponse.js +3 -7
  14. package/lib/cjs/services/PlatformKoaResponse.js.map +1 -1
  15. package/lib/esm/components/PlatformKoa.js +61 -50
  16. package/lib/esm/components/PlatformKoa.js.map +1 -1
  17. package/lib/esm/index.js +0 -3
  18. package/lib/esm/index.js.map +1 -1
  19. package/lib/esm/interfaces/PlatformKoaSettings.js.map +1 -1
  20. package/lib/esm/middlewares/resourceNotFoundMiddleware.js +3 -3
  21. package/lib/esm/middlewares/resourceNotFoundMiddleware.js.map +1 -1
  22. package/lib/esm/services/PlatformKoaHandler.js +15 -43
  23. package/lib/esm/services/PlatformKoaHandler.js.map +1 -1
  24. package/lib/esm/services/PlatformKoaRequest.js +8 -18
  25. package/lib/esm/services/PlatformKoaRequest.js.map +1 -1
  26. package/lib/esm/services/PlatformKoaResponse.js +3 -8
  27. package/lib/esm/services/PlatformKoaResponse.js.map +1 -1
  28. package/lib/types/components/PlatformKoa.d.ts +12 -14
  29. package/lib/types/index.d.ts +0 -3
  30. package/lib/types/interfaces/PlatformKoaSettings.d.ts +1 -1
  31. package/lib/types/services/PlatformKoaHandler.d.ts +4 -7
  32. package/lib/types/services/PlatformKoaRequest.d.ts +3 -6
  33. package/lib/types/services/PlatformKoaResponse.d.ts +2 -3
  34. package/package.json +24 -16
  35. package/readme.md +2 -1
  36. package/lib/cjs/decorators/caseSensitive.js +0 -30
  37. package/lib/cjs/decorators/caseSensitive.js.map +0 -1
  38. package/lib/cjs/decorators/routerSettings.js +0 -31
  39. package/lib/cjs/decorators/routerSettings.js.map +0 -1
  40. package/lib/cjs/decorators/strict.js +0 -31
  41. package/lib/cjs/decorators/strict.js.map +0 -1
  42. package/lib/esm/decorators/caseSensitive.js +0 -26
  43. package/lib/esm/decorators/caseSensitive.js.map +0 -1
  44. package/lib/esm/decorators/routerSettings.js +0 -27
  45. package/lib/esm/decorators/routerSettings.js.map +0 -1
  46. package/lib/esm/decorators/strict.js +0 -27
  47. package/lib/esm/decorators/strict.js.map +0 -1
  48. package/lib/types/decorators/caseSensitive.d.ts +0 -22
  49. package/lib/types/decorators/routerSettings.d.ts +0 -24
  50. package/lib/types/decorators/strict.d.ts +0 -23
package/jest.config.js ADDED
@@ -0,0 +1,14 @@
1
+ // For a detailed explanation regarding each configuration property, visit:
2
+ // https://jestjs.io/docs/en/configuration.html
3
+
4
+ module.exports = {
5
+ ...require("@tsed/jest-config")(__dirname, "platform-koa"),
6
+ coverageThreshold: {
7
+ global: {
8
+ statements: 99.46,
9
+ branches: 85.45,
10
+ functions: 100,
11
+ lines: 99.45
12
+ }
13
+ }
14
+ };
@@ -7,15 +7,15 @@ const common_1 = require("@tsed/common");
7
7
  const platform_exceptions_1 = require("@tsed/platform-exceptions");
8
8
  const core_1 = require("@tsed/core");
9
9
  const koa_1 = tslib_1.__importDefault(require("koa"));
10
+ const koa_bodyparser_1 = tslib_1.__importDefault(require("koa-bodyparser"));
11
+ // @ts-ignore
12
+ const koa_qs_1 = tslib_1.__importDefault(require("koa-qs"));
10
13
  const resourceNotFoundMiddleware_1 = require("../middlewares/resourceNotFoundMiddleware");
11
- const PlatformKoaResponse_1 = require("../services/PlatformKoaResponse");
12
- const PlatformKoaRequest_1 = require("../services/PlatformKoaRequest");
14
+ const staticsMiddleware_1 = require("../middlewares/staticsMiddleware");
13
15
  const PlatformKoaHandler_1 = require("../services/PlatformKoaHandler");
16
+ const PlatformKoaRequest_1 = require("../services/PlatformKoaRequest");
17
+ const PlatformKoaResponse_1 = require("../services/PlatformKoaResponse");
14
18
  const multer_1 = require("../utils/multer");
15
- const staticsMiddleware_1 = require("../middlewares/staticsMiddleware");
16
- // @ts-ignore
17
- const koa_qs_1 = tslib_1.__importDefault(require("koa-qs"));
18
- const koa_bodyparser_1 = tslib_1.__importDefault(require("koa-bodyparser"));
19
19
  // @ts-ignore
20
20
  router_1.default.prototype.$$match = router_1.default.prototype.match;
21
21
  router_1.default.prototype.match = function match(...args) {
@@ -32,22 +32,23 @@ router_1.default.prototype.match = function match(...args) {
32
32
  * @koa
33
33
  */
34
34
  class PlatformKoa {
35
+ injector;
36
+ providers = [
37
+ {
38
+ provide: common_1.PlatformResponse,
39
+ useClass: PlatformKoaResponse_1.PlatformKoaResponse
40
+ },
41
+ {
42
+ provide: common_1.PlatformRequest,
43
+ useClass: PlatformKoaRequest_1.PlatformKoaRequest
44
+ },
45
+ {
46
+ provide: common_1.PlatformHandler,
47
+ useClass: PlatformKoaHandler_1.PlatformKoaHandler
48
+ }
49
+ ];
35
50
  constructor(injector) {
36
51
  this.injector = injector;
37
- this.providers = [
38
- {
39
- provide: common_1.PlatformResponse,
40
- useClass: PlatformKoaResponse_1.PlatformKoaResponse
41
- },
42
- {
43
- provide: common_1.PlatformRequest,
44
- useClass: PlatformKoaRequest_1.PlatformKoaRequest
45
- },
46
- {
47
- provide: common_1.PlatformHandler,
48
- useClass: PlatformKoaHandler_1.PlatformKoaHandler
49
- }
50
- ];
51
52
  }
52
53
  /**
53
54
  * Create new serverless application. In this mode, the component scan are disabled.
@@ -73,31 +74,50 @@ class PlatformKoa {
73
74
  }
74
75
  onInit() {
75
76
  const injector = this.injector;
76
- const app = this.injector.get(common_1.PlatformApplication);
77
+ const app = this.getPlatformApplication();
77
78
  const listener = (error, ctx) => {
78
- var _a;
79
- (_a = injector.get(platform_exceptions_1.PlatformExceptions)) === null || _a === void 0 ? void 0 : _a.catch(error, ctx.request.$ctx);
79
+ const $ctx = (0, common_1.getContext)();
80
+ injector.get(platform_exceptions_1.PlatformExceptions)?.catch(error, $ctx);
80
81
  };
81
82
  app.getApp().silent = true;
82
83
  app.getApp().on("error", listener);
83
84
  }
84
- useRouter() {
85
+ async beforeLoadRoutes() {
85
86
  const app = this.injector.get(common_1.PlatformApplication);
86
- app.getApp().use(resourceNotFoundMiddleware_1.resourceNotFoundMiddleware);
87
- app.getApp().use(app.getRouter().routes()).use(app.getRouter().allowedMethods());
88
- return this;
87
+ this.useContext();
88
+ app.use(resourceNotFoundMiddleware_1.resourceNotFoundMiddleware);
89
+ }
90
+ mapLayers(layers) {
91
+ const { settings } = this.injector;
92
+ const app = this.getPlatformApplication();
93
+ const options = settings.get("koa.router", {});
94
+ const rawRouter = new router_1.default(options);
95
+ layers.forEach((layer) => {
96
+ switch (layer.method) {
97
+ case "statics":
98
+ rawRouter.use(layer.path, this.statics(layer.path, layer.opts));
99
+ break;
100
+ default:
101
+ rawRouter[layer.method](...layer.getArgs());
102
+ }
103
+ });
104
+ app.getApp().use(rawRouter.routes()).use(rawRouter.allowedMethods());
105
+ }
106
+ mapHandler(handler, metadata) {
107
+ if (metadata.isRawMiddleware()) {
108
+ return handler;
109
+ }
110
+ return async (koaContext, next) => {
111
+ const $ctx = (0, common_1.getContext)();
112
+ $ctx.next = next;
113
+ await handler($ctx);
114
+ };
89
115
  }
90
116
  useContext() {
91
- const app = this.injector.get(common_1.PlatformApplication);
92
- const { logger } = this.injector;
93
- logger.info("Mount app context");
94
- const invoke = (0, common_1.createContext)(this.injector);
95
- app.getApp().use(async (ctx, next) => {
96
- await invoke({
97
- request: ctx.request,
98
- response: ctx.response,
99
- ctx
100
- });
117
+ const app = this.getPlatformApplication();
118
+ app.use(async (koaContext, next) => {
119
+ const $ctx = (0, common_1.getContext)();
120
+ $ctx.upgrade({ request: koaContext.request, response: koaContext.response, koaContext });
101
121
  return next();
102
122
  });
103
123
  return this;
@@ -112,18 +132,6 @@ class PlatformKoa {
112
132
  }
113
133
  };
114
134
  }
115
- router(routerOptions = {}) {
116
- var _a;
117
- const { settings } = this.injector;
118
- const options = Object.assign({}, ((_a = settings.koa) === null || _a === void 0 ? void 0 : _a.router) || {}, routerOptions);
119
- const router = new router_1.default(options);
120
- return {
121
- router,
122
- callback() {
123
- return [router.routes(), router.allowedMethods()];
124
- }
125
- };
126
- }
127
135
  multipart(options) {
128
136
  return (0, multer_1.getMulter)(options);
129
137
  }
@@ -140,6 +148,9 @@ class PlatformKoa {
140
148
  }
141
149
  return parser({ ...options, ...additionalOptions });
142
150
  }
151
+ getPlatformApplication() {
152
+ return this.injector.get(common_1.PlatformApplication);
153
+ }
143
154
  }
144
155
  exports.PlatformKoa = PlatformKoa;
145
156
  //# sourceMappingURL=PlatformKoa.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"PlatformKoa.js","sourceRoot":"","sources":["../../../src/components/PlatformKoa.ts"],"names":[],"mappings":";;;;AAAA,iEAAyE;AACzE,yCAYsB;AACtB,mEAA6D;AAC7D,qCAA4C;AAC5C,sDAAuC;AACvC,0FAAqF;AACrF,yEAAoE;AACpE,uEAAkE;AAClE,uEAAkE;AAClE,4CAA0C;AAC1C,wEAAmE;AAEnE,aAAa;AACb,4DAA2B;AAC3B,4EAAsD;AAgBtD,aAAa;AACb,gBAAS,CAAC,SAAS,CAAC,OAAO,GAAG,gBAAS,CAAC,SAAS,CAAC,KAAK,CAAC;AACxD,gBAAS,CAAC,SAAS,CAAC,KAAK,GAAG,SAAS,KAAK,CAAC,GAAG,IAAW;IACvD,MAAM,OAAO,GAAG,IAAI,CAAC,OAAO,CAAC,GAAG,IAA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AAE,GAAG,iBAAiB,EAAC,CAAC,CAAC;IACpD,CAAC;CACF;AAlID,kCAkIC","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,iEAAyE;AACzE,yCAasB;AACtB,mEAA6D;AAC7D,qCAA4C;AAE5C,sDAAuC;AACvC,4EAAsD;AACtD,aAAa;AACb,4DAA2B;AAE3B,0FAAqF;AACrF,wEAAmE;AACnE,uEAAkE;AAClE,uEAAkE;AAClE,yEAAoE;AACpE,4CAA0C;AAgB1C,aAAa;AACb,gBAAS,CAAC,SAAS,CAAC,OAAO,GAAG,gBAAS,CAAC,SAAS,CAAC,KAAK,CAAC;AACxD,gBAAS,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;;;GAGG;AACH,MAAa,WAAW;IAgBF;IAfX,SAAS,GAAG;QACnB;YACE,OAAO,EAAE,yBAAgB;YACzB,QAAQ,EAAE,yCAAmB;SAC9B;QACD;YACE,OAAO,EAAE,wBAAe;YACxB,QAAQ,EAAE,uCAAkB;SAC7B;QACD;YACE,OAAO,EAAE,wBAAe;YACxB,QAAQ,EAAE,uCAAkB;SAC7B;KACF,CAAC;IAEF,YAAoB,QAAyB;QAAzB,aAAQ,GAAR,QAAQ,CAAiB;IAAG,CAA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D,kCAmJC","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/cjs/index.js CHANGED
@@ -6,11 +6,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
6
6
  const tslib_1 = require("tslib");
7
7
  tslib_1.__exportStar(require("./exports"), exports);
8
8
  tslib_1.__exportStar(require("./components/PlatformKoa"), exports);
9
- tslib_1.__exportStar(require("./decorators/caseSensitive"), exports);
10
9
  tslib_1.__exportStar(require("./decorators/ctx"), exports);
11
- tslib_1.__exportStar(require("./decorators/routerSettings"), exports);
12
10
  tslib_1.__exportStar(require("./decorators/state"), exports);
13
- tslib_1.__exportStar(require("./decorators/strict"), exports);
14
11
  tslib_1.__exportStar(require("./interfaces/PlatformKoaSettings"), exports);
15
12
  tslib_1.__exportStar(require("./interfaces/interfaces"), exports);
16
13
  tslib_1.__exportStar(require("./middlewares/resourceNotFoundMiddleware"), exports);
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";AAAA;;GAEG;;;AAEH,oDAA0B;AAC1B,mEAAyC;AACzC,qEAA2C;AAC3C,2DAAiC;AACjC,sEAA4C;AAC5C,6DAAmC;AACnC,8DAAoC;AACpC,2EAAiD;AACjD,kEAAwC;AACxC,mFAAyD;AACzD,0EAAgD;AAChD,wEAA8C;AAC9C,wEAA8C;AAC9C,yEAA+C;AAC/C,yDAA+B","sourcesContent":["/**\n * @file Automatically generated by barrelsby.\n */\n\nexport * from \"./exports\";\nexport * from \"./components/PlatformKoa\";\nexport * from \"./decorators/caseSensitive\";\nexport * from \"./decorators/ctx\";\nexport * from \"./decorators/routerSettings\";\nexport * from \"./decorators/state\";\nexport * from \"./decorators/strict\";\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
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";AAAA;;GAEG;;;AAEH,oDAA0B;AAC1B,mEAAyC;AACzC,2DAAiC;AACjC,6DAAmC;AACnC,2EAAiD;AACjD,kEAAwC;AACxC,mFAAyD;AACzD,0EAAgD;AAChD,wEAA8C;AAC9C,wEAA8C;AAC9C,yEAA+C;AAC/C,yDAA+B","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,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.resourceNotFoundMiddleware = void 0;
4
+ const di_1 = require("@tsed/di");
4
5
  const platform_exceptions_1 = require("@tsed/platform-exceptions");
5
6
  /**
6
7
  * @ignore
@@ -8,12 +9,11 @@ const platform_exceptions_1 = require("@tsed/platform-exceptions");
8
9
  * @param next
9
10
  */
10
11
  async function resourceNotFoundMiddleware(ctx, next) {
11
- var _a;
12
- const $ctx = ctx.request.$ctx;
12
+ const $ctx = (0, di_1.getContext)();
13
13
  await next();
14
14
  const status = ctx.status || 404;
15
15
  if (status === 404 && !$ctx.isDone()) {
16
- (_a = $ctx.injector.get(platform_exceptions_1.PlatformExceptions)) === null || _a === void 0 ? void 0 : _a.resourceNotFound($ctx);
16
+ $ctx.injector.get(platform_exceptions_1.PlatformExceptions)?.resourceNotFound($ctx);
17
17
  }
18
18
  }
19
19
  exports.resourceNotFoundMiddleware = resourceNotFoundMiddleware;
@@ -1 +1 @@
1
- {"version":3,"file":"resourceNotFoundMiddleware.js","sourceRoot":"","sources":["../../../src/middlewares/resourceNotFoundMiddleware.ts"],"names":[],"mappings":";;;AACA,mEAA6D;AAE7D;;;;GAIG;AACI,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,wCAAkB,CAAC,0CAAE,gBAAgB,CAAC,IAAI,CAAC,CAAC;KACnF;AACH,CAAC;AARD,gEAQC","sourcesContent":["import {PlatformContext} from \"@tsed/common\";\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: 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"]}
1
+ {"version":3,"file":"resourceNotFoundMiddleware.js","sourceRoot":"","sources":["../../../src/middlewares/resourceNotFoundMiddleware.ts"],"names":[],"mappings":";;;AACA,iCAAoC;AACpC,mEAA6D;AAE7D;;;;GAIG;AACI,KAAK,UAAU,0BAA0B,CAAC,GAAQ,EAAE,IAAS;IAClE,MAAM,IAAI,GAAG,IAAA,eAAU,GAAoB,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,wCAAkB,CAAC,EAAE,gBAAgB,CAAC,IAAI,CAAC,CAAC;KACnF;AACH,CAAC;AARD,gEAQC","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"]}
@@ -2,58 +2,30 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.PlatformKoaHandler = void 0;
4
4
  const common_1 = require("@tsed/common");
5
+ const core_1 = require("@tsed/core");
5
6
  require("./PlatformKoaRequest");
6
- const OVERRIDE_TYPES = [common_1.HandlerType.ENDPOINT, common_1.HandlerType.MIDDLEWARE, common_1.HandlerType.ERR_MIDDLEWARE, common_1.HandlerType.CTX_FN];
7
7
  class PlatformKoaHandler extends common_1.PlatformHandler {
8
- async flush(data, ctx) {
9
- if (data === undefined && ctx.getResponse().body) {
10
- data = ctx.getResponse().body;
8
+ async flush($ctx) {
9
+ if ($ctx.data === undefined && $ctx.getResponse().body) {
10
+ $ctx.data = $ctx.getResponse().body;
11
11
  }
12
- return super.flush(data, ctx);
12
+ return super.flush($ctx);
13
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
- }
21
- async onRequest(requestOptions) {
22
- const { metadata, $ctx } = requestOptions;
23
- if ($ctx.data instanceof Error) {
24
- if (metadata.hasErrorParam) {
25
- requestOptions.err = $ctx.data;
26
- }
27
- else {
28
- return this.onError($ctx.data, requestOptions);
29
- }
14
+ async onRequest(handler, $ctx) {
15
+ if ($ctx.error instanceof Error && !$ctx.handlerMetadata.hasErrorParam) {
16
+ return;
30
17
  }
31
- return super.onRequest(requestOptions);
18
+ return super.onRequest(handler, $ctx);
32
19
  }
33
- onError(error, requestOptions) {
34
- const { next, $ctx, metadata } = requestOptions;
35
- // istanbul ignore next
36
- if ($ctx.isDone()) {
20
+ next(error, next, $ctx) {
21
+ if ((0, core_1.isStream)($ctx.data) || $ctx.isDone()) {
37
22
  return;
38
23
  }
39
- $ctx.data = error;
40
- if (!next || metadata.isFinal()) {
41
- this.throwError(error, $ctx);
24
+ if (error && !($ctx.handlerMetadata.isEndpoint() && !$ctx.handlerMetadata.isFinal())) {
25
+ $ctx.getApp().emit("error", error, $ctx.getRequest().ctx);
42
26
  return;
43
27
  }
44
- return next();
45
- }
46
- async onCtxRequest(requestOptions) {
47
- const { $ctx } = requestOptions;
48
- try {
49
- return await super.onCtxRequest(requestOptions);
50
- }
51
- catch (error) {
52
- this.throwError(error, $ctx);
53
- }
54
- }
55
- throwError(error, $ctx) {
56
- $ctx.getApp().emit("error", error, $ctx.getRequest().ctx);
28
+ return !$ctx.handlerMetadata.isFinal() && next();
57
29
  }
58
30
  }
59
31
  exports.PlatformKoaHandler = PlatformKoaHandler;
@@ -1 +1 @@
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,oBAAW,CAAC,MAAM,CAAC,CAAC;AAEtH,MAAa,kBAAmB,SAAQ,wBAAe;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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I,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"]}
1
+ {"version":3,"file":"PlatformKoaHandler.js","sourceRoot":"","sources":["../../../src/services/PlatformKoaHandler.ts"],"names":[],"mappings":";;;AAAA,yCAAsF;AACtF,qCAAoC;AAEpC,gCAA8B;AAE9B,MAAa,kBAAmB,SAAQ,wBAAe;IAC9C,KAAK,CAAC,KAAK,CAAC,IAAqB;QACtC,IAAI,IAAI,CAAC,IAAI,KAAK,SAAS,IAAI,IAAI,CAAC,WAAW,EAAE,CAAC,IAAI,EAAE;YACtD,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,WAAW,EAAE,CAAC,IAAI,CAAC;SACrC;QAED,OAAO,KAAK,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;IAC3B,CAAC;IAED,KAAK,CAAC,SAAS,CAAC,OAA+B,EAAE,IAAqB;QACpE,IAAI,IAAI,CAAC,KAAK,YAAY,KAAK,IAAI,CAAC,IAAI,CAAC,eAAe,CAAC,aAAa,EAAE;YACtE,OAAO;SACR;QAED,OAAO,KAAK,CAAC,SAAS,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC;IACxC,CAAC;IAEM,IAAI,CAAC,KAAc,EAAE,IAAS,EAAE,IAAqB;QAC1D,IAAI,IAAA,eAAQ,EAAC,IAAI,CAAC,IAAI,CAAC,IAAI,IAAI,CAAC,MAAM,EAAE,EAAE;YACxC,OAAO;SACR;QAED,IAAI,KAAK,IAAI,CAAC,CAAC,IAAI,CAAC,eAAe,CAAC,UAAU,EAAE,IAAI,CAAC,IAAI,CAAC,eAAe,CAAC,OAAO,EAAE,CAAC,EAAE;YACpF,IAAI,CAAC,MAAM,EAAE,CAAC,IAAI,CAAC,OAAO,EAAE,KAAK,EAAE,IAAI,CAAC,UAAU,EAAE,CAAC,GAAG,CAAC,CAAC;YAC1D,OAAO;SACR;QAED,OAAO,CAAC,IAAI,CAAC,eAAe,CAAC,OAAO,EAAE,IAAI,IAAI,EAAE,CAAC;IACnD,CAAC;CACF;AA7BD,gDA6BC","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,8 +1,6 @@
1
1
  "use strict";
2
- var _PlatformKoaRequest_ctx;
3
2
  Object.defineProperty(exports, "__esModule", { value: true });
4
3
  exports.PlatformKoaRequest = void 0;
5
- const tslib_1 = require("tslib");
6
4
  require("@koa/router");
7
5
  const common_1 = require("@tsed/common");
8
6
  /**
@@ -10,35 +8,27 @@ const common_1 = require("@tsed/common");
10
8
  * @koa
11
9
  */
12
10
  class PlatformKoaRequest extends common_1.PlatformRequest {
13
- constructor(event, $ctx) {
14
- super(event, $ctx);
15
- _PlatformKoaRequest_ctx.set(this, void 0);
16
- tslib_1.__classPrivateFieldSet(this, _PlatformKoaRequest_ctx, this.raw.ctx, "f");
11
+ get ctx() {
12
+ return this.$ctx.event.koaContext;
17
13
  }
18
14
  get protocol() {
19
- return tslib_1.__classPrivateFieldGet(this, _PlatformKoaRequest_ctx, "f").request.protocol;
15
+ return this.ctx.request.protocol;
20
16
  }
21
17
  get host() {
22
- return tslib_1.__classPrivateFieldGet(this, _PlatformKoaRequest_ctx, "f").request.host;
18
+ return this.ctx.request.host;
23
19
  }
24
20
  get secure() {
25
- return tslib_1.__classPrivateFieldGet(this, _PlatformKoaRequest_ctx, "f").request.secure;
21
+ return this.ctx.request.secure;
26
22
  }
27
23
  get cookies() {
28
- return tslib_1.__classPrivateFieldGet(this, _PlatformKoaRequest_ctx, "f").cookie || tslib_1.__classPrivateFieldGet(this, _PlatformKoaRequest_ctx, "f").cookies;
24
+ return this.ctx.cookie || this.ctx.cookies;
29
25
  }
30
26
  get session() {
31
- return tslib_1.__classPrivateFieldGet(this, _PlatformKoaRequest_ctx, "f").session;
27
+ return this.ctx.session;
32
28
  }
33
29
  getReq() {
34
- return this.raw.req;
35
- }
36
- destroy() {
37
- // @ts-ignore
38
- tslib_1.__classPrivateFieldSet(this, _PlatformKoaRequest_ctx, null, "f");
39
- return super.destroy();
30
+ return this.ctx.request.req;
40
31
  }
41
32
  }
42
33
  exports.PlatformKoaRequest = PlatformKoaRequest;
43
- _PlatformKoaRequest_ctx = new WeakMap();
44
34
  //# sourceMappingURL=PlatformKoaRequest.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"PlatformKoaRequest.js","sourceRoot":"","sources":["../../../src/services/PlatformKoaRequest.ts"],"names":[],"mappings":";;;;;AAAA,uBAAqB;AACrB,yCAA6E;AAiB7E;;;GAGG;AACH,MAAa,kBAAmB,SAAQ,wBAA4B;IAGlE,YAAY,KAAoB,EAAE,IAAqB;QACrD,KAAK,CAAC,KAAK,EAAE,IAAI,CAAC,CAAC;QAHrB,0CAAkB;QAIhB,+BAAA,IAAI,2BAAQ,IAAI,CAAC,GAAG,CAAC,GAAG,MAAA,CAAC;IAC3B,CAAC;IAED,IAAI,QAAQ;QACV,OAAO,+BAAA,IAAI,+BAAK,CAAC,OAAO,CAAC,QAAQ,CAAC;IACpC,CAAC;IAED,IAAI,IAAI;QACN,OAAO,+BAAA,IAAI,+BAAK,CAAC,OAAO,CAAC,IAAI,CAAC;IAChC,CAAC;IAED,IAAI,MAAM;QACR,OAAO,+BAAA,IAAI,+BAAK,CAAC,OAAO,CAAC,MAAM,CAAC;IAClC,CAAC;IAED,IAAI,OAAO;QACT,OAAO,+BAAA,IAAI,+BAAK,CAAC,MAAM,IAAI,+BAAA,IAAI,+BAAK,CAAC,OAAO,CAAC;IAC/C,CAAC;IAED,IAAI,OAAO;QACT,OAAO,+BAAA,IAAI,+BAAK,CAAC,OAAO,CAAC;IAC3B,CAAC;IAED,MAAM;QACJ,OAAO,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC;IACtB,CAAC;IAED,OAAO;QACL,aAAa;QACb,+BAAA,IAAI,2BAAQ,IAAI,MAAA,CAAC;QACjB,OAAO,KAAK,CAAC,OAAO,EAAE,CAAC;IACzB,CAAC;CACF;AArCD,gDAqCC","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"]}
1
+ {"version":3,"file":"PlatformKoaRequest.js","sourceRoot":"","sources":["../../../src/services/PlatformKoaRequest.ts"],"names":[],"mappings":";;;AAAA,uBAAqB;AACrB,yCAA8D;AAgB9D;;;GAGG;AACH,MAAa,kBAAmB,SAAQ,wBAA4B;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;AA5BD,gDA4BC","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,4 @@
1
1
  "use strict";
2
- var _PlatformKoaResponse_ctx;
3
2
  Object.defineProperty(exports, "__esModule", { value: true });
4
3
  exports.PlatformKoaResponse = void 0;
5
4
  const tslib_1 = require("tslib");
@@ -11,16 +10,14 @@ const encodeurl_1 = tslib_1.__importDefault(require("encodeurl"));
11
10
  * @koa
12
11
  */
13
12
  class PlatformKoaResponse extends common_1.PlatformResponse {
14
- constructor(event, $ctx) {
15
- super(event, $ctx);
16
- _PlatformKoaResponse_ctx.set(this, void 0);
17
- tslib_1.__classPrivateFieldSet(this, _PlatformKoaResponse_ctx, this.raw.ctx, "f");
13
+ get ctx() {
14
+ return this.raw.ctx;
18
15
  }
19
16
  get statusCode() {
20
17
  return this.raw.status;
21
18
  }
22
19
  get locals() {
23
- return tslib_1.__classPrivateFieldGet(this, _PlatformKoaResponse_ctx, "f").state;
20
+ return this.ctx.state;
24
21
  }
25
22
  /**
26
23
  * Return the Node.js response object
@@ -101,5 +98,4 @@ class PlatformKoaResponse extends common_1.PlatformResponse {
101
98
  }
102
99
  }
103
100
  exports.PlatformKoaResponse = PlatformKoaResponse;
104
- _PlatformKoaResponse_ctx = new WeakMap();
105
101
  //# sourceMappingURL=PlatformKoaResponse.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"PlatformKoaResponse.js","sourceRoot":"","sources":["../../../src/services/PlatformKoaResponse.ts"],"names":[],"mappings":";;;;;AAAA,yCAA8E;AAG9E,yCAA8C;AAC9C,kEAAkC;AAQlC;;;GAGG;AACH,MAAa,mBAAoB,SAAQ,yBAA8B;IAGrE,YAAY,KAAoB,EAAE,IAAqB;QACrD,KAAK,CAAC,KAAK,EAAE,IAAI,CAAC,CAAC;QAHrB,2CAAkB;QAIhB,+BAAA,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,+BAAA,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,IAAA,yBAAgB,EAAC,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,IAAW,CAAC,CAAC,CAAC;QAE1E,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,IAAA,mBAAS,EAAC,QAAQ,CAAC,CAAC,CAAC;QAE9C,OAAO,IAAI,CAAC;IACd,CAAC;CACF;AA5GD,kDA4GC","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 #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(): 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
+ {"version":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C,QAAQ,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC;QAE7C,IAAI,CAAC,IAAI,CAAC,GAAG,IAAA,yBAAgB,EAAC,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,IAAW,CAAC,CAAC,CAAC;QAE1E,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,IAAA,mBAAS,EAAC,QAAQ,CAAC,CAAC,CAAC;QAE9C,OAAO,IAAI,CAAC;IACd,CAAC;CACF;AAzGD,kDAyGC","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"]}