@tsed/platform-koa 7.0.0-alpha.2 → 7.0.0-beta.11

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 (118) hide show
  1. package/lib/cjs/components/PlatformKoa.js +145 -0
  2. package/lib/cjs/components/PlatformKoa.js.map +1 -0
  3. package/lib/{decorators → cjs/decorators}/caseSensitive.js +1 -1
  4. package/lib/cjs/decorators/caseSensitive.js.map +1 -0
  5. package/lib/{decorators → cjs/decorators}/ctx.js +1 -1
  6. package/lib/cjs/decorators/ctx.js.map +1 -0
  7. package/lib/{decorators → cjs/decorators}/routerSettings.js +1 -1
  8. package/lib/cjs/decorators/routerSettings.js.map +1 -0
  9. package/lib/{decorators → cjs/decorators}/state.js +0 -0
  10. package/lib/cjs/decorators/state.js.map +1 -0
  11. package/lib/{decorators → cjs/decorators}/strict.js +1 -1
  12. package/lib/cjs/decorators/strict.js.map +1 -0
  13. package/lib/cjs/exports.js +7 -0
  14. package/lib/cjs/exports.js.map +1 -0
  15. package/lib/cjs/index.js +22 -0
  16. package/lib/cjs/index.js.map +1 -0
  17. package/lib/{interfaces → cjs/interfaces}/PlatformKoaSettings.js +0 -0
  18. package/lib/cjs/interfaces/PlatformKoaSettings.js.map +1 -0
  19. package/lib/{interfaces/index.js → cjs/interfaces/interfaces.js} +1 -1
  20. package/lib/cjs/interfaces/interfaces.js.map +1 -0
  21. package/lib/{middlewares → cjs/middlewares}/resourceNotFoundMiddleware.js +2 -2
  22. package/lib/cjs/middlewares/resourceNotFoundMiddleware.js.map +1 -0
  23. package/lib/{middlewares → cjs/middlewares}/staticsMiddleware.js +1 -1
  24. package/lib/cjs/middlewares/staticsMiddleware.js.map +1 -0
  25. package/lib/cjs/package.json +3 -0
  26. package/lib/{services → cjs/services}/PlatformKoaHandler.js +0 -0
  27. package/lib/cjs/services/PlatformKoaHandler.js.map +1 -0
  28. package/lib/cjs/services/PlatformKoaRequest.js +44 -0
  29. package/lib/cjs/services/PlatformKoaRequest.js.map +1 -0
  30. package/lib/{services → cjs/services}/PlatformKoaResponse.js +10 -10
  31. package/lib/cjs/services/PlatformKoaResponse.js.map +1 -0
  32. package/lib/cjs/utils/multer.js +80 -0
  33. package/lib/cjs/utils/multer.js.map +1 -0
  34. package/lib/esm/components/PlatformKoa.js +140 -0
  35. package/lib/esm/components/PlatformKoa.js.map +1 -0
  36. package/lib/esm/decorators/caseSensitive.js +26 -0
  37. package/lib/esm/decorators/caseSensitive.js.map +1 -0
  38. package/lib/esm/decorators/ctx.js +27 -0
  39. package/lib/esm/decorators/ctx.js.map +1 -0
  40. package/lib/esm/decorators/routerSettings.js +27 -0
  41. package/lib/esm/decorators/routerSettings.js.map +1 -0
  42. package/lib/esm/decorators/state.js +5 -0
  43. package/lib/esm/decorators/state.js.map +1 -0
  44. package/lib/esm/decorators/strict.js +27 -0
  45. package/lib/esm/decorators/strict.js.map +1 -0
  46. package/lib/esm/exports.js +5 -0
  47. package/lib/esm/exports.js.map +1 -0
  48. package/lib/esm/index.js +19 -0
  49. package/lib/esm/index.js.map +1 -0
  50. package/lib/esm/interfaces/PlatformKoaSettings.js +2 -0
  51. package/lib/esm/interfaces/PlatformKoaSettings.js.map +1 -0
  52. package/lib/esm/interfaces/interfaces.js +2 -0
  53. package/lib/esm/interfaces/interfaces.js.map +1 -0
  54. package/lib/esm/middlewares/resourceNotFoundMiddleware.js +16 -0
  55. package/lib/esm/middlewares/resourceNotFoundMiddleware.js.map +1 -0
  56. package/lib/esm/middlewares/staticsMiddleware.js +30 -0
  57. package/lib/esm/middlewares/staticsMiddleware.js.map +1 -0
  58. package/lib/esm/package.json +3 -0
  59. package/lib/esm/services/PlatformKoaHandler.js +56 -0
  60. package/lib/esm/services/PlatformKoaHandler.js.map +1 -0
  61. package/lib/esm/services/PlatformKoaRequest.js +40 -0
  62. package/lib/esm/services/PlatformKoaRequest.js.map +1 -0
  63. package/lib/esm/services/PlatformKoaResponse.js +101 -0
  64. package/lib/esm/services/PlatformKoaResponse.js.map +1 -0
  65. package/lib/{utils → esm/utils}/multer.js +6 -8
  66. package/lib/esm/utils/multer.js.map +1 -0
  67. package/lib/types/components/PlatformKoa.d.ts +67 -0
  68. package/lib/{decorators → types/decorators}/caseSensitive.d.ts +0 -0
  69. package/lib/{decorators → types/decorators}/ctx.d.ts +0 -0
  70. package/lib/{decorators → types/decorators}/routerSettings.d.ts +0 -0
  71. package/lib/{decorators → types/decorators}/state.d.ts +0 -0
  72. package/lib/{decorators → types/decorators}/strict.d.ts +0 -0
  73. package/lib/types/exports.d.ts +1 -0
  74. package/lib/types/index.d.ts +18 -0
  75. package/lib/types/interfaces/PlatformKoaSettings.d.ts +15 -0
  76. package/lib/{interfaces/index.d.ts → types/interfaces/interfaces.d.ts} +0 -0
  77. package/lib/{middlewares → types/middlewares}/resourceNotFoundMiddleware.d.ts +0 -0
  78. package/lib/{middlewares → types/middlewares}/staticsMiddleware.d.ts +0 -0
  79. package/lib/{services → types/services}/PlatformKoaHandler.d.ts +0 -0
  80. package/lib/{services → types/services}/PlatformKoaRequest.d.ts +1 -1
  81. package/lib/{services → types/services}/PlatformKoaResponse.d.ts +3 -3
  82. package/lib/{utils → types/utils}/multer.d.ts +0 -0
  83. package/package.json +70 -39
  84. package/readme.md +0 -2
  85. package/tsconfig.compile.esm.json +12 -0
  86. package/lib/components/PlatformKoa.d.ts +0 -32
  87. package/lib/components/PlatformKoa.js +0 -65
  88. package/lib/components/PlatformKoa.js.map +0 -1
  89. package/lib/decorators/caseSensitive.js.map +0 -1
  90. package/lib/decorators/ctx.js.map +0 -1
  91. package/lib/decorators/index.d.ts +0 -5
  92. package/lib/decorators/index.js +0 -9
  93. package/lib/decorators/index.js.map +0 -1
  94. package/lib/decorators/routerSettings.js.map +0 -1
  95. package/lib/decorators/state.js.map +0 -1
  96. package/lib/decorators/strict.js.map +0 -1
  97. package/lib/index.d.ts +0 -4
  98. package/lib/index.js +0 -16
  99. package/lib/index.js.map +0 -1
  100. package/lib/interfaces/PlatformKoaSettings.d.ts +0 -8
  101. package/lib/interfaces/PlatformKoaSettings.js.map +0 -1
  102. package/lib/interfaces/index.js.map +0 -1
  103. package/lib/middlewares/resourceNotFoundMiddleware.js.map +0 -1
  104. package/lib/middlewares/staticsMiddleware.js.map +0 -1
  105. package/lib/services/PlatformKoaApplication.d.ts +0 -22
  106. package/lib/services/PlatformKoaApplication.js +0 -31
  107. package/lib/services/PlatformKoaApplication.js.map +0 -1
  108. package/lib/services/PlatformKoaHandler.js.map +0 -1
  109. package/lib/services/PlatformKoaRequest.js +0 -44
  110. package/lib/services/PlatformKoaRequest.js.map +0 -1
  111. package/lib/services/PlatformKoaResponse.js.map +0 -1
  112. package/lib/services/PlatformKoaRouter.d.ts +0 -22
  113. package/lib/services/PlatformKoaRouter.js +0 -45
  114. package/lib/services/PlatformKoaRouter.js.map +0 -1
  115. package/lib/services/index.d.ts +0 -5
  116. package/lib/services/index.js +0 -9
  117. package/lib/services/index.js.map +0 -1
  118. package/lib/utils/multer.js.map +0 -1
@@ -0,0 +1,140 @@
1
+ import KoaRouter from "@koa/router";
2
+ import { createContext, PlatformApplication, PlatformBuilder, PlatformHandler, PlatformRequest, PlatformResponse } from "@tsed/common";
3
+ import { PlatformExceptions } from "@tsed/platform-exceptions";
4
+ import { isFunction } from "@tsed/core";
5
+ import Koa from "koa";
6
+ import { resourceNotFoundMiddleware } from "../middlewares/resourceNotFoundMiddleware.js";
7
+ import { PlatformKoaResponse } from "../services/PlatformKoaResponse.js";
8
+ import { PlatformKoaRequest } from "../services/PlatformKoaRequest.js";
9
+ import { PlatformKoaHandler } from "../services/PlatformKoaHandler.js";
10
+ 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
+ // @ts-ignore
16
+ KoaRouter.prototype.$$match = KoaRouter.prototype.match;
17
+ KoaRouter.prototype.match = function match(...args) {
18
+ const matched = this.$$match(...args);
19
+ if (matched) {
20
+ if (matched.path.length) {
21
+ matched.route = true;
22
+ }
23
+ }
24
+ return matched;
25
+ };
26
+ /**
27
+ * @platform
28
+ * @koa
29
+ */
30
+ export class PlatformKoa {
31
+ constructor(injector) {
32
+ 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
+ /**
49
+ * Create new serverless application. In this mode, the component scan are disabled.
50
+ * @param module
51
+ * @param settings
52
+ */
53
+ static create(module, settings = {}) {
54
+ return PlatformBuilder.create(module, {
55
+ ...settings,
56
+ adapter: PlatformKoa
57
+ });
58
+ }
59
+ /**
60
+ * Bootstrap a server application
61
+ * @param module
62
+ * @param settings
63
+ */
64
+ static async bootstrap(module, settings = {}) {
65
+ return PlatformBuilder.bootstrap(module, {
66
+ ...settings,
67
+ adapter: PlatformKoa
68
+ });
69
+ }
70
+ onInit() {
71
+ const injector = this.injector;
72
+ const app = this.injector.get(PlatformApplication);
73
+ const listener = (error, ctx) => {
74
+ var _a;
75
+ (_a = injector.get(PlatformExceptions)) === null || _a === void 0 ? void 0 : _a.catch(error, ctx.request.$ctx);
76
+ };
77
+ app.getApp().silent = true;
78
+ app.getApp().on("error", listener);
79
+ }
80
+ useRouter() {
81
+ const app = this.injector.get(PlatformApplication);
82
+ app.getApp().use(resourceNotFoundMiddleware);
83
+ app.getApp().use(app.getRouter().routes()).use(app.getRouter().allowedMethods());
84
+ return this;
85
+ }
86
+ useContext() {
87
+ const app = this.injector.get(PlatformApplication);
88
+ const { logger } = this.injector;
89
+ logger.info("Mount app context");
90
+ const invoke = createContext(this.injector);
91
+ app.getApp().use(async (ctx, next) => {
92
+ await invoke({
93
+ request: ctx.request,
94
+ response: ctx.response,
95
+ ctx
96
+ });
97
+ return next();
98
+ });
99
+ return this;
100
+ }
101
+ app() {
102
+ const app = this.injector.settings.get("koa.app") || new Koa();
103
+ koaQs(app, "extended");
104
+ return {
105
+ app,
106
+ callback() {
107
+ return app.callback();
108
+ }
109
+ };
110
+ }
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
+ multipart(options) {
124
+ return getMulter(options);
125
+ }
126
+ statics(endpoint, options) {
127
+ return staticsMiddleware(options);
128
+ }
129
+ bodyParser(type, additionalOptions = {}) {
130
+ const opts = this.injector.settings.get(`koa.bodyParser`);
131
+ let parser = koaBodyParser;
132
+ let options = {};
133
+ if (isFunction(opts)) {
134
+ parser = opts;
135
+ options = {};
136
+ }
137
+ return parser({ ...options, ...additionalOptions });
138
+ }
139
+ }
140
+ //# sourceMappingURL=PlatformKoa.js.map
@@ -0,0 +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,EAAC,UAAU,EAAO,MAAM,YAAY,CAAC;AAC5C,OAAO,GAAoB,MAAM,KAAK,CAAC;AACvC,OAAO,EAAC,0BAA0B,EAAC,MAAM,2CAA2C,CAAC;AACrF,OAAO,EAAC,mBAAmB,EAAC,MAAM,iCAAiC,CAAC;AACpE,OAAO,EAAC,kBAAkB,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;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"]}
@@ -0,0 +1,26 @@
1
+ import { RouterSettings } from "./routerSettings.js";
2
+ /**
3
+ * Specify the behavior of the router controller.
4
+ *
5
+ * ```typescript
6
+ * @Controller("/")
7
+ * @CaseSensitive(true)
8
+ * class MyCtrl {
9
+ *
10
+ * }
11
+ * ```
12
+ *
13
+ * Property | Description | Default
14
+ * ---|---|---
15
+ * CaseSensitive | Enable case sensitivity. | Disabled by default, treating “/Foo” and “/foo” as the same.
16
+ * Strict | Enable strict routing. | Disabled by default, “/foo” and “/foo/” are treated the same by the router.
17
+ *
18
+ * @returns {Function}
19
+ * @decorator
20
+ * @koa
21
+ * @param sensitive
22
+ */
23
+ export function CaseSensitive(sensitive) {
24
+ return RouterSettings({ sensitive });
25
+ }
26
+ //# sourceMappingURL=caseSensitive.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"caseSensitive.js","sourceRoot":"","sources":["../../../src/decorators/caseSensitive.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,cAAc,EAAC,MAAM,kBAAkB,CAAC;AAEhD;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,MAAM,UAAU,aAAa,CAAC,SAAkB;IAC9C,OAAO,cAAc,CAAC,EAAC,SAAS,EAAC,CAAC,CAAC;AACrC,CAAC","sourcesContent":["import {RouterSettings} from \"./routerSettings\";\n\n/**\n * Specify the behavior of the router controller.\n *\n * ```typescript\n * @Controller(\"/\")\n * @CaseSensitive(true)\n * class MyCtrl {\n *\n * }\n * ```\n *\n * Property | Description | Default\n * ---|---|---\n * CaseSensitive | Enable case sensitivity. | Disabled by default, treating “/Foo” and “/foo” as the same.\n * Strict | Enable strict routing. | Disabled by default, “/foo” and “/foo/” are treated the same by the router.\n *\n * @returns {Function}\n * @decorator\n * @koa\n * @param sensitive\n */\nexport function CaseSensitive(sensitive: boolean) {\n return RouterSettings({sensitive});\n}\n"]}
@@ -0,0 +1,27 @@
1
+ import { UseParam } from "@tsed/common";
2
+ /**
3
+ * Return the original Koa context.
4
+ * @decorator
5
+ * @operation
6
+ * @input
7
+ * @koa
8
+ */
9
+ export function KoaCtx() {
10
+ return UseParam({
11
+ paramType: "KOA_CTX",
12
+ dataPath: "$ctx.request.ctx",
13
+ useConverter: false,
14
+ useValidation: false
15
+ });
16
+ }
17
+ /**
18
+ * Return the original Koa context.
19
+ * @decorator
20
+ * @operation
21
+ * @input
22
+ * @koa
23
+ */
24
+ export function Ctx() {
25
+ return KoaCtx();
26
+ }
27
+ //# sourceMappingURL=ctx.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ctx.js","sourceRoot":"","sources":["../../../src/decorators/ctx.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,QAAQ,EAAC,MAAM,cAAc,CAAC;AAGtC;;;;;;GAMG;AACH,MAAM,UAAU,MAAM;IACpB,OAAO,QAAQ,CAAC;QACd,SAAS,EAAE,SAAS;QACpB,QAAQ,EAAE,kBAAkB;QAC5B,YAAY,EAAE,KAAK;QACnB,aAAa,EAAE,KAAK;KACrB,CAAC,CAAC;AACL,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,GAAG;IACjB,OAAO,MAAM,EAAE,CAAC;AAClB,CAAC","sourcesContent":["import {UseParam} from \"@tsed/common\";\nimport type {Context} from \"koa\";\n\n/**\n * Return the original Koa context.\n * @decorator\n * @operation\n * @input\n * @koa\n */\nexport function KoaCtx(): ParameterDecorator {\n return UseParam({\n paramType: \"KOA_CTX\",\n dataPath: \"$ctx.request.ctx\",\n useConverter: false,\n useValidation: false\n });\n}\n\n/**\n * Return the original Koa context.\n * @decorator\n * @operation\n * @input\n * @koa\n */\nexport function Ctx(): ParameterDecorator {\n return KoaCtx();\n}\n\nexport type Ctx = Context;\nexport type KoaCtx = Context;\n"]}
@@ -0,0 +1,27 @@
1
+ import { ROUTER_OPTIONS } from "@tsed/common";
2
+ import { StoreMerge } from "@tsed/core";
3
+ /**
4
+ * Specify the behavior of the router controller.
5
+ *
6
+ * ```typescript
7
+ * @Controller("/")
8
+ * @RouterSettings({sensitive: true})
9
+ * class MyCtrl {
10
+ *
11
+ * }
12
+ * ```
13
+ *
14
+ * Property | Description | Default
15
+ * ---|---|---
16
+ * sensitive | Enable case sensitivity. | Disabled by default, treating “/Foo” and “/foo” as the same.
17
+ * strict | Enable strict routing. | Disabled by default, “/foo” and “/foo/” are treated the same by the router.
18
+ *
19
+ * @returns {(target:any)=>void}
20
+ * @decorator
21
+ * @param routerOptions
22
+ * @koa
23
+ */
24
+ export function RouterSettings(routerOptions) {
25
+ return StoreMerge(ROUTER_OPTIONS, routerOptions);
26
+ }
27
+ //# sourceMappingURL=routerSettings.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"routerSettings.js","sourceRoot":"","sources":["../../../src/decorators/routerSettings.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,cAAc,EAAC,MAAM,cAAc,CAAC;AAC5C,OAAO,EAAC,UAAU,EAAC,MAAM,YAAY,CAAC;AAGtC;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,MAAM,UAAU,cAAc,CAAC,aAA4B;IACzD,OAAO,UAAU,CAAC,cAAc,EAAE,aAAa,CAAC,CAAC;AACnD,CAAC","sourcesContent":["import {ROUTER_OPTIONS} from \"@tsed/common\";\nimport {StoreMerge} from \"@tsed/core\";\nimport {RouterOptions} from \"@koa/router\";\n\n/**\n * Specify the behavior of the router controller.\n *\n * ```typescript\n * @Controller(\"/\")\n * @RouterSettings({sensitive: true})\n * class MyCtrl {\n *\n * }\n * ```\n *\n * Property | Description | Default\n * ---|---|---\n * sensitive | Enable case sensitivity. | Disabled by default, treating “/Foo” and “/foo” as the same.\n * strict | Enable strict routing. | Disabled by default, “/foo” and “/foo/” are treated the same by the router.\n *\n * @returns {(target:any)=>void}\n * @decorator\n * @param routerOptions\n * @koa\n */\nexport function RouterSettings(routerOptions: RouterOptions): Function {\n return StoreMerge(ROUTER_OPTIONS, routerOptions);\n}\n"]}
@@ -0,0 +1,5 @@
1
+ import { Locals } from "@tsed/common";
2
+ export function State(...args) {
3
+ return Locals(...args);
4
+ }
5
+ //# sourceMappingURL=state.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"state.js","sourceRoot":"","sources":["../../../src/decorators/state.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,MAAM,EAAC,MAAM,cAAc,CAAC;AAwCpC,MAAM,UAAU,KAAK,CAAC,GAAG,IAAW;IAClC,OAAQ,MAAc,CAAC,GAAG,IAAI,CAAC,CAAC;AAClC,CAAC","sourcesContent":["import {Locals} from \"@tsed/common\";\n\n/**\n * State return the value from koa.ctx.state object.\n *\n * #### Example\n *\n * ```typescript\n * * @Middleware()\n * class StateMiddleware {\n * use(@State() state: any) {\n * // set some on locals\n * state.user = \"user\"\n * }\n * }\n *\n * @Controller('/')\n * @UseBefore(StateMiddleware)\n * class MyCtrl {\n * @Get('/')\n * @View('home.ejs') // will use locals and returned data to render the page\n * get(@State('user') user: any) {\n * console.log('user', user);\n *\n * return {\n * description: 'Hello world'\n * }\n * }\n * }\n * ```\n *\n * @param expression The path of the property to get.\n * @decorator\n * @operation\n * @input\n * @response\n * @koa\n */\nexport function State(expression: string): ParameterDecorator;\nexport function State(): ParameterDecorator;\nexport function State(...args: any[]): ParameterDecorator {\n return (Locals as any)(...args);\n}\n"]}
@@ -0,0 +1,27 @@
1
+ import { RouterSettings } from "./routerSettings.js";
2
+ /**
3
+ * Specify the behavior of the router controller.
4
+ *
5
+ * ```typescript
6
+ * @Controller("/")
7
+ * @Strict(true)
8
+ * class MyCtrl {
9
+ *
10
+ * }
11
+ * ```
12
+ *
13
+ * Property | Description | Default
14
+ * ---|---|---
15
+ * CaseSensitive | Enable case sensitivity. | Disabled by default, treating “/Foo” and “/foo” as the same.
16
+ * MergeParams | Preserve the req.params values from the parent router. If the parent and the child have conflicting param names, the child’s value take precedence. | false
17
+ * Strict | Enable strict routing. | Disabled by default, “/foo” and “/foo/” are treated the same by the router.
18
+ *
19
+ * @param strict
20
+ * @returns {Function}
21
+ * @decorator
22
+ * @koa
23
+ */
24
+ export function Strict(strict) {
25
+ return RouterSettings({ strict });
26
+ }
27
+ //# sourceMappingURL=strict.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"strict.js","sourceRoot":"","sources":["../../../src/decorators/strict.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,cAAc,EAAC,MAAM,kBAAkB,CAAC;AAEhD;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,MAAM,UAAU,MAAM,CAAC,MAAe;IACpC,OAAO,cAAc,CAAC,EAAC,MAAM,EAAC,CAAC,CAAC;AAClC,CAAC","sourcesContent":["import {RouterSettings} from \"./routerSettings\";\n\n/**\n * Specify the behavior of the router controller.\n *\n * ```typescript\n * @Controller(\"/\")\n * @Strict(true)\n * class MyCtrl {\n *\n * }\n * ```\n *\n * Property | Description | Default\n * ---|---|---\n * CaseSensitive | Enable case sensitivity. | Disabled by default, treating “/Foo” and “/foo” as the same.\n * MergeParams | Preserve the req.params values from the parent router. If the parent and the child have conflicting param names, the child’s value take precedence. | false\n * Strict | Enable strict routing. | Disabled by default, “/foo” and “/foo/” are treated the same by the router.\n *\n * @param strict\n * @returns {Function}\n * @decorator\n * @koa\n */\nexport function Strict(strict: boolean) {\n return RouterSettings({strict});\n}\n"]}
@@ -0,0 +1,5 @@
1
+ import { PlatformBuilder, PlatformTest } from "@tsed/common";
2
+ import { PlatformKoa } from "./components/PlatformKoa.js";
3
+ PlatformTest.adapter = PlatformKoa;
4
+ PlatformBuilder.adapter = PlatformKoa;
5
+ //# sourceMappingURL=exports.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"exports.js","sourceRoot":"","sources":["../../src/exports.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,eAAe,EAAE,YAAY,EAAC,MAAM,cAAc,CAAC;AAC3D,OAAO,EAAC,WAAW,EAAC,MAAM,0BAA0B,CAAC;AAErD,YAAY,CAAC,OAAO,GAAG,WAAW,CAAC;AACnC,eAAe,CAAC,OAAO,GAAG,WAAW,CAAC","sourcesContent":["import {PlatformBuilder, PlatformTest} from \"@tsed/common\";\nimport {PlatformKoa} from \"./components/PlatformKoa\";\n\nPlatformTest.adapter = PlatformKoa;\nPlatformBuilder.adapter = PlatformKoa;\n"]}
@@ -0,0 +1,19 @@
1
+ /**
2
+ * @file Automatically generated by barrelsby.
3
+ */
4
+ export * from "./exports.js";
5
+ export * from "./components/PlatformKoa.js";
6
+ export * from "./decorators/caseSensitive.js";
7
+ export * from "./decorators/ctx.js";
8
+ export * from "./decorators/routerSettings.js";
9
+ export * from "./decorators/state.js";
10
+ export * from "./decorators/strict.js";
11
+ export * from "./interfaces/PlatformKoaSettings.js";
12
+ export * from "./interfaces/interfaces.js";
13
+ export * from "./middlewares/resourceNotFoundMiddleware.js";
14
+ export * from "./middlewares/staticsMiddleware.js";
15
+ export * from "./services/PlatformKoaHandler.js";
16
+ export * from "./services/PlatformKoaRequest.js";
17
+ export * from "./services/PlatformKoaResponse.js";
18
+ export * from "./utils/multer.js";
19
+ //# sourceMappingURL=index.js.map
@@ -0,0 +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,4BAA4B,CAAC;AAC3C,cAAc,kBAAkB,CAAC;AACjC,cAAc,6BAA6B,CAAC;AAC5C,cAAc,oBAAoB,CAAC;AACnC,cAAc,qBAAqB,CAAC;AACpC,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/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"]}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=PlatformKoaSettings.js.map
@@ -0,0 +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"]}
@@ -0,0 +1,2 @@
1
+ export * from "./PlatformKoaSettings.js";
2
+ //# sourceMappingURL=interfaces.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"interfaces.js","sourceRoot":"","sources":["../../../src/interfaces/interfaces.ts"],"names":[],"mappings":"AAGA,cAAc,uBAAuB,CAAC","sourcesContent":["import Koa from \"koa\";\nimport {PlatformKoaSettings} from \"./PlatformKoaSettings\";\n\nexport * from \"./PlatformKoaSettings\";\n\ndeclare global {\n namespace TsED {\n export interface Configuration {\n /**\n * Configuration related to Koa platform application.\n */\n koa: PlatformKoaSettings;\n }\n\n export interface NextFunction extends Koa.Next {}\n }\n}\n"]}
@@ -0,0 +1,16 @@
1
+ import { PlatformExceptions } from "@tsed/platform-exceptions";
2
+ /**
3
+ * @ignore
4
+ * @param ctx
5
+ * @param next
6
+ */
7
+ export async function resourceNotFoundMiddleware(ctx, next) {
8
+ var _a;
9
+ const $ctx = ctx.request.$ctx;
10
+ await next();
11
+ const status = ctx.status || 404;
12
+ if (status === 404 && !$ctx.isDone()) {
13
+ (_a = $ctx.injector.get(PlatformExceptions)) === null || _a === void 0 ? void 0 : _a.resourceNotFound($ctx);
14
+ }
15
+ }
16
+ //# sourceMappingURL=resourceNotFoundMiddleware.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"resourceNotFoundMiddleware.js","sourceRoot":"","sources":["../../../src/middlewares/resourceNotFoundMiddleware.ts"],"names":[],"mappings":"AACA,OAAO,EAAC,kBAAkB,EAAC,MAAM,2BAA2B,CAAC;AAE7D;;;;GAIG;AACH,MAAM,CAAC,KAAK,UAAU,0BAA0B,CAAC,GAAQ,EAAE,IAAS;;IAClE,MAAM,IAAI,GAAoB,GAAG,CAAC,OAAO,CAAC,IAAI,CAAC;IAC/C,MAAM,IAAI,EAAE,CAAC;IACb,MAAM,MAAM,GAAG,GAAG,CAAC,MAAM,IAAI,GAAG,CAAC;IAEjC,IAAI,MAAM,KAAK,GAAG,IAAI,CAAC,IAAI,CAAC,MAAM,EAAE,EAAE;QACpC,MAAA,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAqB,kBAAkB,CAAC,0CAAE,gBAAgB,CAAC,IAAI,CAAC,CAAC;KACnF;AACH,CAAC","sourcesContent":["import {PlatformContext} 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"]}
@@ -0,0 +1,30 @@
1
+ import send from "koa-send";
2
+ /**
3
+ * @ignore
4
+ * @param options
5
+ */
6
+ export function staticsMiddleware(options) {
7
+ if (options.index !== false)
8
+ options.index = options.index || "index.html";
9
+ return async (ctx, next) => {
10
+ if (["GET", "HEAD"].includes(ctx.method) && !ctx.body) {
11
+ let path = ctx.path;
12
+ if (ctx._matchedRoute && ctx._matchedRoute !== "/") {
13
+ path = path.replace(String(ctx._matchedRoute).replace("/(.*)", ""), "");
14
+ }
15
+ try {
16
+ const done = await send(ctx, path, options);
17
+ if (done) {
18
+ return;
19
+ }
20
+ }
21
+ catch (err) {
22
+ if (err.status !== 404) {
23
+ throw err;
24
+ }
25
+ }
26
+ }
27
+ return next();
28
+ };
29
+ }
30
+ //# sourceMappingURL=staticsMiddleware.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"staticsMiddleware.js","sourceRoot":"","sources":["../../../src/middlewares/staticsMiddleware.ts"],"names":[],"mappings":"AAEA,OAAO,IAAI,MAAM,UAAU,CAAC;AAE5B;;;GAGG;AACH,MAAM,UAAU,iBAAiB,CAAC,OAA+B;IAC/D,IAAI,OAAO,CAAC,KAAK,KAAK,KAAK;QAAE,OAAO,CAAC,KAAK,GAAG,OAAO,CAAC,KAAK,IAAI,YAAY,CAAC;IAE3E,OAAO,KAAK,EAAE,GAAY,EAAE,IAAS,EAAE,EAAE;QACvC,IAAI,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC,QAAQ,CAAC,GAAG,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,IAAI,EAAE;YACrD,IAAI,IAAI,GAAG,GAAG,CAAC,IAAI,CAAC;YAEpB,IAAI,GAAG,CAAC,aAAa,IAAI,GAAG,CAAC,aAAa,KAAK,GAAG,EAAE;gBAClD,IAAI,GAAG,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,GAAG,CAAC,aAAa,CAAC,CAAC,OAAO,CAAC,OAAO,EAAE,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC;aACzE;YACD,IAAI;gBACF,MAAM,IAAI,GAAG,MAAM,IAAI,CAAC,GAAU,EAAE,IAAI,EAAE,OAA2B,CAAC,CAAC;gBAEvE,IAAI,IAAI,EAAE;oBACR,OAAO;iBACR;aACF;YAAC,OAAO,GAAG,EAAE;gBACZ,IAAI,GAAG,CAAC,MAAM,KAAK,GAAG,EAAE;oBACtB,MAAM,GAAG,CAAC;iBACX;aACF;SACF;QAED,OAAO,IAAI,EAAE,CAAC;IAChB,CAAC,CAAC;AACJ,CAAC","sourcesContent":["import {PlatformStaticsOptions} from \"@tsed/common\";\nimport type {Context} from \"koa\";\nimport send from \"koa-send\";\n\n/**\n * @ignore\n * @param options\n */\nexport function staticsMiddleware(options: PlatformStaticsOptions): any {\n if (options.index !== false) options.index = options.index || \"index.html\";\n\n return async (ctx: Context, next: any) => {\n if ([\"GET\", \"HEAD\"].includes(ctx.method) && !ctx.body) {\n let path = ctx.path;\n\n if (ctx._matchedRoute && ctx._matchedRoute !== \"/\") {\n path = path.replace(String(ctx._matchedRoute).replace(\"/(.*)\", \"\"), \"\");\n }\n try {\n const done = await send(ctx as any, path, options as send.SendOptions);\n\n if (done) {\n return;\n }\n } catch (err) {\n if (err.status !== 404) {\n throw err;\n }\n }\n }\n\n return next();\n };\n}\n"]}
@@ -0,0 +1,3 @@
1
+ {
2
+ "type": "module"
3
+ }
@@ -0,0 +1,56 @@
1
+ import { HandlerType, PlatformHandler } from "@tsed/common";
2
+ import "./PlatformKoaRequest.js";
3
+ const OVERRIDE_TYPES = [HandlerType.ENDPOINT, HandlerType.MIDDLEWARE, HandlerType.ERR_MIDDLEWARE, HandlerType.CTX_FN];
4
+ export class PlatformKoaHandler extends PlatformHandler {
5
+ async flush(data, ctx) {
6
+ if (data === undefined && ctx.getResponse().body) {
7
+ data = ctx.getResponse().body;
8
+ }
9
+ return super.flush(data, ctx);
10
+ }
11
+ createRawHandler(metadata) {
12
+ if (OVERRIDE_TYPES.includes(metadata.type)) {
13
+ const handler = this.compileHandler(metadata);
14
+ return async (ctx, next) => handler({ next, $ctx: ctx.request.$ctx });
15
+ }
16
+ return super.createRawHandler(metadata);
17
+ }
18
+ async onRequest(requestOptions) {
19
+ const { metadata, $ctx } = requestOptions;
20
+ if ($ctx.data instanceof Error) {
21
+ if (metadata.hasErrorParam) {
22
+ requestOptions.err = $ctx.data;
23
+ }
24
+ else {
25
+ return this.onError($ctx.data, requestOptions);
26
+ }
27
+ }
28
+ return super.onRequest(requestOptions);
29
+ }
30
+ onError(error, requestOptions) {
31
+ const { next, $ctx, metadata } = requestOptions;
32
+ // istanbul ignore next
33
+ if ($ctx.isDone()) {
34
+ return;
35
+ }
36
+ $ctx.data = error;
37
+ if (!next || metadata.isFinal()) {
38
+ this.throwError(error, $ctx);
39
+ return;
40
+ }
41
+ return next();
42
+ }
43
+ async onCtxRequest(requestOptions) {
44
+ const { $ctx } = requestOptions;
45
+ try {
46
+ return await super.onCtxRequest(requestOptions);
47
+ }
48
+ catch (error) {
49
+ this.throwError(error, $ctx);
50
+ }
51
+ }
52
+ throwError(error, $ctx) {
53
+ $ctx.getApp().emit("error", error, $ctx.getRequest().ctx);
54
+ }
55
+ }
56
+ //# sourceMappingURL=PlatformKoaHandler.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"PlatformKoaHandler.js","sourceRoot":"","sources":["../../../src/services/PlatformKoaHandler.ts"],"names":[],"mappings":"AAAA,OAAO,EAAkB,WAAW,EAAqC,eAAe,EAAC,MAAM,cAAc,CAAC;AAE9G,OAAO,sBAAsB,CAAC;AAE9B,MAAM,cAAc,GAAG,CAAC,WAAW,CAAC,QAAQ,EAAE,WAAW,CAAC,UAAU,EAAE,WAAW,CAAC,cAAc,EAAE,WAAW,CAAC,MAAM,CAAC,CAAC;AAEtH,MAAM,OAAO,kBAAmB,SAAQ,eAAe;IAC9C,KAAK,CAAC,KAAK,CAAC,IAAS,EAAE,GAAoB;QAChD,IAAI,IAAI,KAAK,SAAS,IAAI,GAAG,CAAC,WAAW,EAAE,CAAC,IAAI,EAAE;YAChD,IAAI,GAAG,GAAG,CAAC,WAAW,EAAE,CAAC,IAAI,CAAC;SAC/B;QAED,OAAO,KAAK,CAAC,KAAK,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC;IAChC,CAAC;IAES,gBAAgB,CAAC,QAAyB;QAClD,IAAI,cAAc,CAAC,QAAQ,CAAC,QAAQ,CAAC,IAAI,CAAC,EAAE;YAC1C,MAAM,OAAO,GAAG,IAAI,CAAC,cAAc,CAAC,QAAQ,CAAC,CAAC;YAC9C,OAAO,KAAK,EAAE,GAAgB,EAAE,IAAc,EAAE,EAAE,CAAC,OAAO,CAAC,EAAC,IAAI,EAAE,IAAI,EAAE,GAAG,CAAC,OAAO,CAAC,IAAI,EAAC,CAAC,CAAC;SAC5F;QAED,OAAO,KAAK,CAAC,gBAAgB,CAAC,QAAQ,CAAC,CAAC;IAC1C,CAAC;IAES,KAAK,CAAC,SAAS,CAAC,cAAgC;QACxD,MAAM,EAAC,QAAQ,EAAE,IAAI,EAAC,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} from \"@tsed/common\";\nimport Koa from \"koa\";\nimport \"./PlatformKoaRequest\";\n\nconst OVERRIDE_TYPES = [HandlerType.ENDPOINT, HandlerType.MIDDLEWARE, HandlerType.ERR_MIDDLEWARE, HandlerType.CTX_FN];\n\nexport class PlatformKoaHandler extends PlatformHandler {\n public async flush(data: any, ctx: PlatformContext): Promise<void> {\n if (data === undefined && ctx.getResponse().body) {\n data = ctx.getResponse().body;\n }\n\n return super.flush(data, ctx);\n }\n\n protected createRawHandler(metadata: HandlerMetadata) {\n if (OVERRIDE_TYPES.includes(metadata.type)) {\n const handler = this.compileHandler(metadata);\n return async (ctx: Koa.Context, next: Koa.Next) => handler({next, $ctx: ctx.request.$ctx});\n }\n\n return super.createRawHandler(metadata);\n }\n\n protected async onRequest(requestOptions: OnRequestOptions): Promise<any> {\n const {metadata, $ctx} = requestOptions;\n\n if ($ctx.data instanceof Error) {\n if (metadata.hasErrorParam) {\n requestOptions.err = $ctx.data;\n } else {\n return this.onError($ctx.data, requestOptions);\n }\n }\n\n return super.onRequest(requestOptions);\n }\n\n protected onError(error: unknown, requestOptions: OnRequestOptions) {\n const {next, $ctx, metadata} = requestOptions;\n\n // istanbul ignore next\n if ($ctx.isDone()) {\n return;\n }\n\n $ctx.data = error;\n\n if (!next || metadata.isFinal()) {\n this.throwError(error, $ctx);\n\n return;\n }\n\n return next();\n }\n\n protected async onCtxRequest(requestOptions: OnRequestOptions): Promise<any> {\n const {$ctx} = requestOptions;\n\n try {\n return await super.onCtxRequest(requestOptions);\n } catch (error) {\n this.throwError(error, $ctx);\n }\n }\n\n protected throwError(error: any, $ctx: PlatformContext) {\n $ctx.getApp().emit(\"error\", error, $ctx.getRequest().ctx);\n }\n}\n"]}
@@ -0,0 +1,40 @@
1
+ var _PlatformKoaRequest_ctx;
2
+ import { __classPrivateFieldGet, __classPrivateFieldSet } from "tslib";
3
+ import "@koa/router";
4
+ import { PlatformRequest } from "@tsed/common";
5
+ /**
6
+ * @platform
7
+ * @koa
8
+ */
9
+ export class PlatformKoaRequest extends PlatformRequest {
10
+ constructor(event, $ctx) {
11
+ super(event, $ctx);
12
+ _PlatformKoaRequest_ctx.set(this, void 0);
13
+ __classPrivateFieldSet(this, _PlatformKoaRequest_ctx, this.raw.ctx, "f");
14
+ }
15
+ get protocol() {
16
+ return __classPrivateFieldGet(this, _PlatformKoaRequest_ctx, "f").request.protocol;
17
+ }
18
+ get host() {
19
+ return __classPrivateFieldGet(this, _PlatformKoaRequest_ctx, "f").request.host;
20
+ }
21
+ get secure() {
22
+ return __classPrivateFieldGet(this, _PlatformKoaRequest_ctx, "f").request.secure;
23
+ }
24
+ get cookies() {
25
+ return __classPrivateFieldGet(this, _PlatformKoaRequest_ctx, "f").cookie || __classPrivateFieldGet(this, _PlatformKoaRequest_ctx, "f").cookies;
26
+ }
27
+ get session() {
28
+ return __classPrivateFieldGet(this, _PlatformKoaRequest_ctx, "f").session;
29
+ }
30
+ getReq() {
31
+ return this.raw.req;
32
+ }
33
+ destroy() {
34
+ // @ts-ignore
35
+ __classPrivateFieldSet(this, _PlatformKoaRequest_ctx, null, "f");
36
+ return super.destroy();
37
+ }
38
+ }
39
+ _PlatformKoaRequest_ctx = new WeakMap();
40
+ //# sourceMappingURL=PlatformKoaRequest.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"PlatformKoaRequest.js","sourceRoot":"","sources":["../../../src/services/PlatformKoaRequest.ts"],"names":[],"mappings":";;AAAA,OAAO,aAAa,CAAC;AACrB,OAAO,EAAiC,eAAe,EAAC,MAAM,cAAc,CAAC;AAiB7E;;;GAGG;AACH,MAAM,OAAO,kBAAmB,SAAQ,eAA4B;IAGlE,YAAY,KAAoB,EAAE,IAAqB;QACrD,KAAK,CAAC,KAAK,EAAE,IAAI,CAAC,CAAC;QAHrB,0CAAkB;QAIhB,uBAAA,IAAI,2BAAQ,IAAI,CAAC,GAAG,CAAC,GAAG,MAAA,CAAC;IAC3B,CAAC;IAED,IAAI,QAAQ;QACV,OAAO,uBAAA,IAAI,+BAAK,CAAC,OAAO,CAAC,QAAQ,CAAC;IACpC,CAAC;IAED,IAAI,IAAI;QACN,OAAO,uBAAA,IAAI,+BAAK,CAAC,OAAO,CAAC,IAAI,CAAC;IAChC,CAAC;IAED,IAAI,MAAM;QACR,OAAO,uBAAA,IAAI,+BAAK,CAAC,OAAO,CAAC,MAAM,CAAC;IAClC,CAAC;IAED,IAAI,OAAO;QACT,OAAO,uBAAA,IAAI,+BAAK,CAAC,MAAM,IAAI,uBAAA,IAAI,+BAAK,CAAC,OAAO,CAAC;IAC/C,CAAC;IAED,IAAI,OAAO;QACT,OAAO,uBAAA,IAAI,+BAAK,CAAC,OAAO,CAAC;IAC3B,CAAC;IAED,MAAM;QACJ,OAAO,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC;IACtB,CAAC;IAED,OAAO;QACL,aAAa;QACb,uBAAA,IAAI,2BAAQ,IAAI,MAAA,CAAC;QACjB,OAAO,KAAK,CAAC,OAAO,EAAE,CAAC;IACzB,CAAC;CACF","sourcesContent":["import \"@koa/router\";\nimport {IncomingEvent, PlatformContext, PlatformRequest} from \"@tsed/common\";\nimport {IncomingMessage} from \"http\";\nimport Koa from \"koa\";\n\ndeclare module \"koa\" {\n export interface Request {\n id: string;\n $ctx: PlatformContext;\n }\n}\n\ndeclare global {\n namespace TsED {\n export interface Request extends Koa.Request {}\n }\n}\n\n/**\n * @platform\n * @koa\n */\nexport class PlatformKoaRequest extends PlatformRequest<Koa.Request> {\n #ctx: Koa.Context;\n\n constructor(event: IncomingEvent, $ctx: PlatformContext) {\n super(event, $ctx);\n this.#ctx = this.raw.ctx;\n }\n\n get protocol(): string {\n return this.#ctx.request.protocol;\n }\n\n get host(): string {\n return this.#ctx.request.host;\n }\n\n get secure(): boolean {\n return this.#ctx.request.secure;\n }\n\n get cookies(): {[p: string]: any} {\n return this.#ctx.cookie || this.#ctx.cookies;\n }\n\n get session(): any {\n return this.#ctx.session;\n }\n\n getReq(): IncomingMessage {\n return this.raw.req;\n }\n\n destroy() {\n // @ts-ignore\n this.#ctx = null;\n return super.destroy();\n }\n}\n"]}
@@ -0,0 +1,101 @@
1
+ var _PlatformKoaResponse_ctx;
2
+ import { __classPrivateFieldGet, __classPrivateFieldSet } from "tslib";
3
+ import { PlatformResponse } from "@tsed/common";
4
+ import { getStatusMessage } from "@tsed/schema";
5
+ import encodeUrl from "encodeurl";
6
+ /**
7
+ * @platform
8
+ * @koa
9
+ */
10
+ export class PlatformKoaResponse extends PlatformResponse {
11
+ constructor(event, $ctx) {
12
+ super(event, $ctx);
13
+ _PlatformKoaResponse_ctx.set(this, void 0);
14
+ __classPrivateFieldSet(this, _PlatformKoaResponse_ctx, this.raw.ctx, "f");
15
+ }
16
+ get statusCode() {
17
+ return this.raw.status;
18
+ }
19
+ get locals() {
20
+ return __classPrivateFieldGet(this, _PlatformKoaResponse_ctx, "f").state;
21
+ }
22
+ /**
23
+ * Return the Node.js response object
24
+ */
25
+ getRes() {
26
+ return this.raw.res;
27
+ }
28
+ hasStatus() {
29
+ // KOA set 404 by default
30
+ return this.statusCode !== 404;
31
+ }
32
+ /**
33
+ * Sets the HTTP status for the response.
34
+ *
35
+ * @param status
36
+ */
37
+ status(status) {
38
+ this.raw.status = status;
39
+ return this;
40
+ }
41
+ /**
42
+ * Set `Content-Type` response header with `type` through `mime.lookup()`
43
+ * when it does not contain "/", or set the Content-Type to `type` otherwise.
44
+ *
45
+ * Examples:
46
+ *
47
+ * res.type('.html');
48
+ * res.type('html');
49
+ * res.type('json');
50
+ * res.type('application/json');
51
+ * res.type('png');
52
+ */
53
+ contentType(contentType) {
54
+ this.raw.type = contentType;
55
+ return this;
56
+ }
57
+ getHeaders() {
58
+ return this.raw.headers;
59
+ }
60
+ /**
61
+ * Send any data to your consumer.
62
+ *
63
+ * This method accept a ReadableStream, a plain object, boolean, string, number, null and undefined data.
64
+ * It choose the better way to send the data.
65
+ *
66
+ * @param data
67
+ */
68
+ body(data) {
69
+ this.raw.body = data;
70
+ return this;
71
+ }
72
+ getBody() {
73
+ return this.raw.body;
74
+ }
75
+ redirect(status, url) {
76
+ status = status || 302;
77
+ // Set location header
78
+ url = this.location(url).raw.get("Location");
79
+ this.body(`${getStatusMessage(status)}. Redirecting to ${url}`);
80
+ this.status(status);
81
+ this.setHeader("Content-Length", Buffer.byteLength(this.raw.body));
82
+ if (this.request.method === "HEAD") {
83
+ this.getRes().end();
84
+ }
85
+ else {
86
+ this.getRes().end(this.getBody());
87
+ }
88
+ return this;
89
+ }
90
+ location(location) {
91
+ // "back" is an alias for the referrer
92
+ if (location === "back") {
93
+ location = this.request.get("Referrer") || "/";
94
+ }
95
+ // set location
96
+ this.raw.set("Location", encodeUrl(location));
97
+ return this;
98
+ }
99
+ }
100
+ _PlatformKoaResponse_ctx = new WeakMap();
101
+ //# sourceMappingURL=PlatformKoaResponse.js.map