@tsed/platform-koa 6.87.2 → 6.90.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (140) hide show
  1. package/lib/{components → cjs/components}/PlatformKoa.d.ts +15 -8
  2. package/lib/cjs/components/PlatformKoa.js +89 -0
  3. package/lib/cjs/components/PlatformKoa.js.map +1 -0
  4. package/lib/{decorators → cjs/decorators}/caseSensitive.d.ts +0 -0
  5. package/lib/{decorators → cjs/decorators}/caseSensitive.js +1 -1
  6. package/lib/cjs/decorators/caseSensitive.js.map +1 -0
  7. package/lib/{decorators → cjs/decorators}/ctx.d.ts +0 -0
  8. package/lib/{decorators → cjs/decorators}/ctx.js +1 -1
  9. package/lib/cjs/decorators/ctx.js.map +1 -0
  10. package/lib/{decorators → cjs/decorators}/index.d.ts +0 -0
  11. package/lib/cjs/decorators/index.js +9 -0
  12. package/lib/cjs/decorators/index.js.map +1 -0
  13. package/lib/{decorators → cjs/decorators}/routerSettings.d.ts +0 -0
  14. package/lib/{decorators → cjs/decorators}/routerSettings.js +1 -1
  15. package/lib/cjs/decorators/routerSettings.js.map +1 -0
  16. package/lib/{decorators → cjs/decorators}/state.d.ts +0 -0
  17. package/lib/{decorators → cjs/decorators}/state.js +0 -0
  18. package/lib/cjs/decorators/state.js.map +1 -0
  19. package/lib/{decorators → cjs/decorators}/strict.d.ts +0 -0
  20. package/lib/{decorators → cjs/decorators}/strict.js +1 -1
  21. package/lib/cjs/decorators/strict.js.map +1 -0
  22. package/lib/{index.d.ts → cjs/index.d.ts} +0 -0
  23. package/lib/cjs/index.js +16 -0
  24. package/lib/cjs/index.js.map +1 -0
  25. package/lib/{interfaces → cjs/interfaces}/PlatformKoaSettings.d.ts +0 -0
  26. package/lib/{interfaces → cjs/interfaces}/PlatformKoaSettings.js +0 -0
  27. package/lib/cjs/interfaces/PlatformKoaSettings.js.map +1 -0
  28. package/lib/{interfaces → cjs/interfaces}/index.d.ts +0 -0
  29. package/lib/{interfaces → cjs/interfaces}/index.js +1 -1
  30. package/lib/cjs/interfaces/index.js.map +1 -0
  31. package/lib/{middlewares → cjs/middlewares}/resourceNotFoundMiddleware.d.ts +0 -0
  32. package/lib/{middlewares → cjs/middlewares}/resourceNotFoundMiddleware.js +0 -0
  33. package/lib/cjs/middlewares/resourceNotFoundMiddleware.js.map +1 -0
  34. package/lib/{middlewares → cjs/middlewares}/staticsMiddleware.d.ts +0 -0
  35. package/lib/{middlewares → cjs/middlewares}/staticsMiddleware.js +2 -2
  36. package/lib/cjs/middlewares/staticsMiddleware.js.map +1 -0
  37. package/lib/{services → cjs/services}/PlatformKoaApplication.d.ts +0 -0
  38. package/lib/{services → cjs/services}/PlatformKoaApplication.js +5 -4
  39. package/lib/cjs/services/PlatformKoaApplication.js.map +1 -0
  40. package/lib/{services → cjs/services}/PlatformKoaHandler.d.ts +0 -0
  41. package/lib/{services → cjs/services}/PlatformKoaHandler.js +0 -0
  42. package/lib/cjs/services/PlatformKoaHandler.js.map +1 -0
  43. package/lib/{services → cjs/services}/PlatformKoaRequest.d.ts +1 -1
  44. package/lib/cjs/services/PlatformKoaRequest.js +44 -0
  45. package/lib/cjs/services/PlatformKoaRequest.js.map +1 -0
  46. package/lib/{services → cjs/services}/PlatformKoaResponse.d.ts +2 -2
  47. package/lib/{services → cjs/services}/PlatformKoaResponse.js +8 -8
  48. package/lib/cjs/services/PlatformKoaResponse.js.map +1 -0
  49. package/lib/{services → cjs/services}/PlatformKoaRouter.d.ts +0 -0
  50. package/lib/{services → cjs/services}/PlatformKoaRouter.js +7 -7
  51. package/lib/cjs/services/PlatformKoaRouter.js.map +1 -0
  52. package/lib/{services → cjs/services}/index.d.ts +0 -0
  53. package/lib/cjs/services/index.js +9 -0
  54. package/lib/cjs/services/index.js.map +1 -0
  55. package/lib/{utils → cjs/utils}/multer.d.ts +0 -0
  56. package/lib/{utils → cjs/utils}/multer.js +1 -1
  57. package/lib/cjs/utils/multer.js.map +1 -0
  58. package/lib/esm/components/PlatformKoa.js +85 -0
  59. package/lib/esm/components/PlatformKoa.js.map +1 -0
  60. package/lib/esm/decorators/caseSensitive.js +26 -0
  61. package/lib/esm/decorators/caseSensitive.js.map +1 -0
  62. package/lib/esm/decorators/ctx.js +27 -0
  63. package/lib/esm/decorators/ctx.js.map +1 -0
  64. package/lib/esm/decorators/index.js +6 -0
  65. package/lib/esm/decorators/index.js.map +1 -0
  66. package/lib/esm/decorators/routerSettings.js +27 -0
  67. package/lib/esm/decorators/routerSettings.js.map +1 -0
  68. package/lib/esm/decorators/state.js +5 -0
  69. package/lib/esm/decorators/state.js.map +1 -0
  70. package/lib/esm/decorators/strict.js +27 -0
  71. package/lib/esm/decorators/strict.js.map +1 -0
  72. package/lib/esm/index.js +13 -0
  73. package/lib/esm/index.js.map +1 -0
  74. package/lib/esm/interfaces/PlatformKoaSettings.js +2 -0
  75. package/lib/esm/interfaces/PlatformKoaSettings.js.map +1 -0
  76. package/lib/esm/interfaces/index.js +2 -0
  77. package/lib/esm/interfaces/index.js.map +1 -0
  78. package/lib/esm/middlewares/resourceNotFoundMiddleware.js +16 -0
  79. package/lib/esm/middlewares/resourceNotFoundMiddleware.js.map +1 -0
  80. package/lib/esm/middlewares/staticsMiddleware.js +30 -0
  81. package/lib/esm/middlewares/staticsMiddleware.js.map +1 -0
  82. package/lib/esm/services/PlatformKoaApplication.js +29 -0
  83. package/lib/esm/services/PlatformKoaApplication.js.map +1 -0
  84. package/lib/esm/services/PlatformKoaHandler.js +56 -0
  85. package/lib/esm/services/PlatformKoaHandler.js.map +1 -0
  86. package/lib/esm/services/PlatformKoaRequest.js +40 -0
  87. package/lib/esm/services/PlatformKoaRequest.js.map +1 -0
  88. package/lib/esm/services/PlatformKoaResponse.js +101 -0
  89. package/lib/esm/services/PlatformKoaResponse.js.map +1 -0
  90. package/lib/esm/services/PlatformKoaRouter.js +42 -0
  91. package/lib/esm/services/PlatformKoaRouter.js.map +1 -0
  92. package/lib/esm/services/index.js +6 -0
  93. package/lib/esm/services/index.js.map +1 -0
  94. package/lib/esm/utils/multer.js +51 -0
  95. package/lib/esm/utils/multer.js.map +1 -0
  96. package/lib/types/components/PlatformKoa.d.ts +45 -0
  97. package/lib/types/decorators/caseSensitive.d.ts +22 -0
  98. package/lib/types/decorators/ctx.d.ts +19 -0
  99. package/lib/types/decorators/index.d.ts +5 -0
  100. package/lib/types/decorators/routerSettings.d.ts +24 -0
  101. package/lib/types/decorators/state.d.ts +38 -0
  102. package/lib/types/decorators/strict.d.ts +23 -0
  103. package/lib/types/index.d.ts +4 -0
  104. package/lib/types/interfaces/PlatformKoaSettings.d.ts +8 -0
  105. package/lib/types/interfaces/index.d.ts +15 -0
  106. package/lib/types/middlewares/resourceNotFoundMiddleware.d.ts +6 -0
  107. package/lib/types/middlewares/staticsMiddleware.d.ts +6 -0
  108. package/lib/types/services/PlatformKoaApplication.d.ts +22 -0
  109. package/lib/types/services/PlatformKoaHandler.d.ts +10 -0
  110. package/lib/types/services/PlatformKoaRequest.d.ts +34 -0
  111. package/lib/types/services/PlatformKoaResponse.d.ts +57 -0
  112. package/lib/types/services/PlatformKoaRouter.d.ts +22 -0
  113. package/lib/types/services/index.d.ts +5 -0
  114. package/lib/types/utils/multer.d.ts +4 -0
  115. package/package.json +26 -18
  116. package/tsconfig.compile.esm.json +23 -0
  117. package/lib/components/PlatformKoa.js +0 -78
  118. package/lib/components/PlatformKoa.js.map +0 -1
  119. package/lib/decorators/caseSensitive.js.map +0 -1
  120. package/lib/decorators/ctx.js.map +0 -1
  121. package/lib/decorators/index.js +0 -9
  122. package/lib/decorators/index.js.map +0 -1
  123. package/lib/decorators/routerSettings.js.map +0 -1
  124. package/lib/decorators/state.js.map +0 -1
  125. package/lib/decorators/strict.js.map +0 -1
  126. package/lib/index.js +0 -16
  127. package/lib/index.js.map +0 -1
  128. package/lib/interfaces/PlatformKoaSettings.js.map +0 -1
  129. package/lib/interfaces/index.js.map +0 -1
  130. package/lib/middlewares/resourceNotFoundMiddleware.js.map +0 -1
  131. package/lib/middlewares/staticsMiddleware.js.map +0 -1
  132. package/lib/services/PlatformKoaApplication.js.map +0 -1
  133. package/lib/services/PlatformKoaHandler.js.map +0 -1
  134. package/lib/services/PlatformKoaRequest.js +0 -44
  135. package/lib/services/PlatformKoaRequest.js.map +0 -1
  136. package/lib/services/PlatformKoaResponse.js.map +0 -1
  137. package/lib/services/PlatformKoaRouter.js.map +0 -1
  138. package/lib/services/index.js +0 -9
  139. package/lib/services/index.js.map +0 -1
  140. package/lib/utils/multer.js.map +0 -1
@@ -0,0 +1,85 @@
1
+ import { createContext, PlatformApplication, PlatformBuilder, PlatformExceptions, PlatformHandler, PlatformRequest, PlatformResponse, PlatformRouter } from "@tsed/common";
2
+ import { resourceNotFoundMiddleware } from "../middlewares/resourceNotFoundMiddleware";
3
+ import { PlatformKoaApplication, PlatformKoaHandler, PlatformKoaRequest, PlatformKoaResponse, PlatformKoaRouter } from "../services";
4
+ /**
5
+ * @platform
6
+ * @koa
7
+ */
8
+ export class PlatformKoa {
9
+ constructor(platform) {
10
+ this.platform = platform;
11
+ this.providers = [
12
+ {
13
+ provide: PlatformResponse,
14
+ useClass: PlatformKoaResponse
15
+ },
16
+ {
17
+ provide: PlatformRequest,
18
+ useClass: PlatformKoaRequest
19
+ },
20
+ {
21
+ provide: PlatformHandler,
22
+ useClass: PlatformKoaHandler
23
+ },
24
+ {
25
+ provide: PlatformRouter,
26
+ useClass: PlatformKoaRouter
27
+ },
28
+ {
29
+ provide: PlatformApplication,
30
+ useClass: PlatformKoaApplication
31
+ }
32
+ ];
33
+ }
34
+ /**
35
+ * Create new serverless application. In this mode, the component scan are disabled.
36
+ * @param module
37
+ * @param settings
38
+ */
39
+ static create(module, settings = {}) {
40
+ return PlatformBuilder.create(module, {
41
+ ...settings,
42
+ adapter: PlatformKoa
43
+ });
44
+ }
45
+ /**
46
+ * Bootstrap a server application
47
+ * @param module
48
+ * @param settings
49
+ */
50
+ static async bootstrap(module, settings = {}) {
51
+ return PlatformBuilder.bootstrap(module, {
52
+ ...settings,
53
+ adapter: PlatformKoa
54
+ });
55
+ }
56
+ onInit() {
57
+ const { injector, app } = this.platform;
58
+ const listener = (error, ctx) => {
59
+ var _a;
60
+ (_a = injector.get(PlatformExceptions)) === null || _a === void 0 ? void 0 : _a.catch(error, ctx.request.$ctx);
61
+ };
62
+ app.getApp().silent = true;
63
+ app.getApp().on("error", listener);
64
+ }
65
+ useRouter() {
66
+ const { app } = this.platform;
67
+ app.getApp().use(resourceNotFoundMiddleware).use(app.getRouter().routes()).use(app.getRouter().allowedMethods());
68
+ return this;
69
+ }
70
+ useContext() {
71
+ const { injector, app, logger } = this.platform;
72
+ logger.info("Mount app context");
73
+ const invoke = createContext(injector);
74
+ app.getApp().use(async (ctx, next) => {
75
+ await invoke({
76
+ request: ctx.request,
77
+ response: ctx.response,
78
+ ctx
79
+ });
80
+ return next();
81
+ });
82
+ return this;
83
+ }
84
+ }
85
+ //# sourceMappingURL=PlatformKoa.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"PlatformKoa.js","sourceRoot":"","sources":["../../../src/components/PlatformKoa.ts"],"names":[],"mappings":"AACA,OAAO,EACL,aAAa,EAEb,mBAAmB,EACnB,eAAe,EACf,kBAAkB,EAClB,eAAe,EACf,eAAe,EACf,gBAAgB,EAChB,cAAc,EACf,MAAM,cAAc,CAAC;AAGtB,OAAO,EAAC,0BAA0B,EAAC,MAAM,2CAA2C,CAAC;AACrF,OAAO,EAAC,sBAAsB,EAAE,kBAAkB,EAAE,kBAAkB,EAAE,mBAAmB,EAAE,iBAAiB,EAAC,MAAM,aAAa,CAAC;AAEnI;;;GAGG;AACH,MAAM,OAAO,WAAW;IAwBtB,YAAoB,QAAyC;QAAzC,aAAQ,GAAR,QAAQ,CAAiC;QAvBpD,cAAS,GAAG;YACnB;gBACE,OAAO,EAAE,gBAAgB;gBACzB,QAAQ,EAAE,mBAAmB;aAC9B;YACD;gBACE,OAAO,EAAE,eAAe;gBACxB,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C,GAAG,CAAC,GAAG,CAAC,SAAS,EAAE,CAAC,MAAM,EAAE,CAAC,CAAC,GAAG,CAAC,GAAG,CAAC,SAAS,EAAE,CAAC,cAAc,EAAE,CAAC,CAAC;QAEjH,OAAO,IAAI,CAAC;IACd,CAAC;IAED,UAAU;QACR,MAAM,EAAC,QAAQ,EAAE,GAAG,EAAE,MAAM,EAAC,GAAG,IAAI,CAAC,QAAQ,CAAC;QAC9C,MAAM,CAAC,IAAI,CAAC,mBAAmB,CAAC,CAAC;QAEjC,MAAM,MAAM,GAAG,aAAa,CAAC,QAAQ,CAAC,CAAC;QAEvC,GAAG,CAAC,MAAM,EAAE,CAAC,GAAG,CAAC,KAAK,EAAE,GAAY,EAAE,IAAU,EAAE,EAAE;YAClD,MAAM,MAAM,CAAC;gBACX,OAAO,EAAE,GAAG,CAAC,OAAc;gBAC3B,QAAQ,EAAE,GAAG,CAAC,QAAe;gBAC7B,GAAG;aACJ,CAAC,CAAC;YAEH,OAAO,IAAI,EAAE,CAAC;QAChB,CAAC,CAAC,CAAC;QAEH,OAAO,IAAI,CAAC;IACd,CAAC;CACF","sourcesContent":["import KoaRouter from \"@koa/router\";\nimport {\n createContext,\n PlatformAdapter,\n PlatformApplication,\n PlatformBuilder,\n PlatformExceptions,\n PlatformHandler,\n PlatformRequest,\n PlatformResponse,\n PlatformRouter\n} from \"@tsed/common\";\nimport {Type} from \"@tsed/core\";\nimport Koa, {Context, Next} from \"koa\";\nimport {resourceNotFoundMiddleware} from \"../middlewares/resourceNotFoundMiddleware\";\nimport {PlatformKoaApplication, PlatformKoaHandler, PlatformKoaRequest, PlatformKoaResponse, PlatformKoaRouter} from \"../services\";\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 provide: PlatformRouter,\n useClass: PlatformKoaRouter\n },\n {\n provide: PlatformApplication,\n useClass: PlatformKoaApplication\n }\n ];\n\n constructor(private platform: PlatformBuilder<Koa, KoaRouter>) {}\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, app} = this.platform;\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.platform;\n app.getApp().use(resourceNotFoundMiddleware).use(app.getRouter().routes()).use(app.getRouter().allowedMethods());\n\n return this;\n }\n\n useContext(): this {\n const {injector, app, logger} = this.platform;\n logger.info(\"Mount app context\");\n\n const invoke = createContext(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"]}
@@ -0,0 +1,26 @@
1
+ import { RouterSettings } from "./routerSettings";
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("KOA_CTX", {
11
+ paramType: "KOA_CTX",
12
+ dataPath: "$ctx.request.ctx",
13
+ useConverter: false,
14
+ useValidation: false
15
+ });
16
+ }
17
+ /**
18
+ * Return the original Koa context.
19
+ * @decorator
20
+ * @operation
21
+ * @input
22
+ * @koa
23
+ */
24
+ export function Ctx() {
25
+ return KoaCtx();
26
+ }
27
+ //# sourceMappingURL=ctx.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ctx.js","sourceRoot":"","sources":["../../../src/decorators/ctx.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,QAAQ,EAAC,MAAM,cAAc,CAAC;AAGtC;;;;;;GAMG;AACH,MAAM,UAAU,MAAM;IACpB,OAAO,QAAQ,CAAC,SAAS,EAAE;QACzB,SAAS,EAAE,SAAS;QACpB,QAAQ,EAAE,kBAAkB;QAC5B,YAAY,EAAE,KAAK;QACnB,aAAa,EAAE,KAAK;KACrB,CAAC,CAAC;AACL,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,GAAG;IACjB,OAAO,MAAM,EAAE,CAAC;AAClB,CAAC","sourcesContent":["import {UseParam} from \"@tsed/common\";\nimport type {Context} from \"koa\";\n\n/**\n * Return the original Koa context.\n * @decorator\n * @operation\n * @input\n * @koa\n */\nexport function KoaCtx(): ParameterDecorator {\n return UseParam(\"KOA_CTX\", {\n paramType: \"KOA_CTX\",\n dataPath: \"$ctx.request.ctx\",\n useConverter: false,\n useValidation: false\n });\n}\n\n/**\n * Return the original Koa context.\n * @decorator\n * @operation\n * @input\n * @koa\n */\nexport function Ctx(): ParameterDecorator {\n return KoaCtx();\n}\n\nexport type Ctx = Context;\nexport type KoaCtx = Context;\n"]}
@@ -0,0 +1,6 @@
1
+ export * from "./routerSettings";
2
+ export * from "./caseSensitive";
3
+ export * from "./strict";
4
+ export * from "./state";
5
+ export * from "./ctx";
6
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/decorators/index.ts"],"names":[],"mappings":"AAAA,cAAc,kBAAkB,CAAC;AACjC,cAAc,iBAAiB,CAAC;AAChC,cAAc,UAAU,CAAC;AACzB,cAAc,SAAS,CAAC;AACxB,cAAc,OAAO,CAAC","sourcesContent":["export * from \"./routerSettings\";\nexport * from \"./caseSensitive\";\nexport * from \"./strict\";\nexport * from \"./state\";\nexport * from \"./ctx\";\n"]}
@@ -0,0 +1,27 @@
1
+ import { ROUTER_OPTIONS } from "@tsed/common";
2
+ import { StoreMerge } from "@tsed/core";
3
+ /**
4
+ * Specify the behavior of the router controller.
5
+ *
6
+ * ```typescript
7
+ * @Controller("/")
8
+ * @RouterSettings({sensitive: true})
9
+ * class MyCtrl {
10
+ *
11
+ * }
12
+ * ```
13
+ *
14
+ * Property | Description | Default
15
+ * ---|---|---
16
+ * sensitive | Enable case sensitivity. | Disabled by default, treating “/Foo” and “/foo” as the same.
17
+ * strict | Enable strict routing. | Disabled by default, “/foo” and “/foo/” are treated the same by the router.
18
+ *
19
+ * @returns {(target:any)=>void}
20
+ * @decorator
21
+ * @param routerOptions
22
+ * @koa
23
+ */
24
+ export function RouterSettings(routerOptions) {
25
+ return StoreMerge(ROUTER_OPTIONS, routerOptions);
26
+ }
27
+ //# sourceMappingURL=routerSettings.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"routerSettings.js","sourceRoot":"","sources":["../../../src/decorators/routerSettings.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,cAAc,EAAC,MAAM,cAAc,CAAC;AAC5C,OAAO,EAAC,UAAU,EAAC,MAAM,YAAY,CAAC;AAGtC;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,MAAM,UAAU,cAAc,CAAC,aAA4B;IACzD,OAAO,UAAU,CAAC,cAAc,EAAE,aAAa,CAAC,CAAC;AACnD,CAAC","sourcesContent":["import {ROUTER_OPTIONS} from \"@tsed/common\";\nimport {StoreMerge} from \"@tsed/core\";\nimport {RouterOptions} from \"@koa/router\";\n\n/**\n * Specify the behavior of the router controller.\n *\n * ```typescript\n * @Controller(\"/\")\n * @RouterSettings({sensitive: true})\n * class MyCtrl {\n *\n * }\n * ```\n *\n * Property | Description | Default\n * ---|---|---\n * sensitive | Enable case sensitivity. | Disabled by default, treating “/Foo” and “/foo” as the same.\n * strict | Enable strict routing. | Disabled by default, “/foo” and “/foo/” are treated the same by the router.\n *\n * @returns {(target:any)=>void}\n * @decorator\n * @param routerOptions\n * @koa\n */\nexport function RouterSettings(routerOptions: RouterOptions): Function {\n return StoreMerge(ROUTER_OPTIONS, routerOptions);\n}\n"]}
@@ -0,0 +1,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";
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,13 @@
1
+ import { PlatformBuilder, PlatformTest } from "@tsed/common";
2
+ import { PlatformKoa } from "./components/PlatformKoa";
3
+ PlatformTest.adapter = PlatformKoa;
4
+ PlatformBuilder.adapter = PlatformKoa;
5
+ // interfaces
6
+ export * from "./interfaces";
7
+ // decorators
8
+ export * from "./decorators";
9
+ // services
10
+ export * from "./services";
11
+ // components
12
+ export * from "./components/PlatformKoa";
13
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,eAAe,EAAE,YAAY,EAAC,MAAM,cAAc,CAAC;AAC3D,OAAO,EAAC,WAAW,EAAC,MAAM,0BAA0B,CAAC;AAErD,YAAY,CAAC,OAAO,GAAG,WAAW,CAAC;AACnC,eAAe,CAAC,OAAO,GAAG,WAAW,CAAC;AAEtC,aAAa;AACb,cAAc,cAAc,CAAC;AAE7B,aAAa;AACb,cAAc,cAAc,CAAC;AAE7B,WAAW;AACX,cAAc,YAAY,CAAC;AAE3B,aAAa;AACb,cAAc,0BAA0B,CAAC","sourcesContent":["import {PlatformBuilder, PlatformTest} from \"@tsed/common\";\nimport {PlatformKoa} from \"./components/PlatformKoa\";\n\nPlatformTest.adapter = PlatformKoa;\nPlatformBuilder.adapter = PlatformKoa;\n\n// interfaces\nexport * from \"./interfaces\";\n\n// decorators\nexport * from \"./decorators\";\n\n// services\nexport * from \"./services\";\n\n// components\nexport * from \"./components/PlatformKoa\";\n"]}
@@ -0,0 +1,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 {RouterOptions} from \"@koa/router\";\n\nexport interface PlatformKoaSettings {\n /**\n * Global configuration for the Express.Router. See express [documentation](http://expressjs.com/en/api.html#express.router).\n */\n router: RouterOptions;\n}\n"]}
@@ -0,0 +1,2 @@
1
+ export * from "./PlatformKoaSettings";
2
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/interfaces/index.ts"],"names":[],"mappings":"AAGA,cAAc,uBAAuB,CAAC","sourcesContent":["import Koa from \"koa\";\nimport {PlatformKoaSettings} from \"./PlatformKoaSettings\";\n\nexport * from \"./PlatformKoaSettings\";\n\ndeclare global {\n namespace TsED {\n export interface Configuration {\n /**\n * Configuration related to Koa platform application.\n */\n koa: PlatformKoaSettings;\n }\n\n export interface NextFunction extends Koa.Next {}\n }\n}\n"]}
@@ -0,0 +1,16 @@
1
+ import { PlatformExceptions } from "@tsed/common";
2
+ /**
3
+ * @ignore
4
+ * @param ctx
5
+ * @param next
6
+ */
7
+ export async function resourceNotFoundMiddleware(ctx, next) {
8
+ var _a;
9
+ const $ctx = ctx.request.$ctx;
10
+ await next();
11
+ const status = ctx.status || 404;
12
+ if (status === 404 && !$ctx.isDone()) {
13
+ (_a = $ctx.injector.get(PlatformExceptions)) === null || _a === void 0 ? void 0 : _a.resourceNotFound($ctx);
14
+ }
15
+ }
16
+ //# sourceMappingURL=resourceNotFoundMiddleware.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"resourceNotFoundMiddleware.js","sourceRoot":"","sources":["../../../src/middlewares/resourceNotFoundMiddleware.ts"],"names":[],"mappings":"AAAA,OAAO,EAAkB,kBAAkB,EAAC,MAAM,cAAc,CAAC;AAEjE;;;;GAIG;AACH,MAAM,CAAC,KAAK,UAAU,0BAA0B,CAAC,GAAQ,EAAE,IAAS;;IAClE,MAAM,IAAI,GAAoB,GAAG,CAAC,OAAO,CAAC,IAAI,CAAC;IAC/C,MAAM,IAAI,EAAE,CAAC;IACb,MAAM,MAAM,GAAG,GAAG,CAAC,MAAM,IAAI,GAAG,CAAC;IAEjC,IAAI,MAAM,KAAK,GAAG,IAAI,CAAC,IAAI,CAAC,MAAM,EAAE,EAAE;QACpC,MAAA,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAqB,kBAAkB,CAAC,0CAAE,gBAAgB,CAAC,IAAI,CAAC,CAAC;KACnF;AACH,CAAC","sourcesContent":["import {PlatformContext, PlatformExceptions} from \"@tsed/common\";\n\n/**\n * @ignore\n * @param ctx\n * @param next\n */\nexport async function resourceNotFoundMiddleware(ctx: any, next: any) {\n const $ctx: PlatformContext = ctx.request.$ctx;\n await next();\n const status = ctx.status || 404;\n\n if (status === 404 && !$ctx.isDone()) {\n $ctx.injector.get<PlatformExceptions>(PlatformExceptions)?.resourceNotFound($ctx);\n }\n}\n"]}
@@ -0,0 +1,30 @@
1
+ import send from "koa-send";
2
+ /**
3
+ * @ignore
4
+ * @param options
5
+ */
6
+ export function staticsMiddleware(options) {
7
+ if (options.index !== false)
8
+ options.index = options.index || "index.html";
9
+ return async (ctx, next) => {
10
+ if (["GET", "HEAD"].includes(ctx.method) && !ctx.body) {
11
+ let path = ctx.path;
12
+ if (ctx._matchedRoute && ctx._matchedRoute !== "/") {
13
+ path = path.replace(String(ctx._matchedRoute).replace("/(.*)", ""), "");
14
+ }
15
+ try {
16
+ const done = await send(ctx, path, options);
17
+ if (done) {
18
+ return;
19
+ }
20
+ }
21
+ catch (err) {
22
+ if (err.status !== 404) {
23
+ throw err;
24
+ }
25
+ }
26
+ }
27
+ return next();
28
+ };
29
+ }
30
+ //# sourceMappingURL=staticsMiddleware.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"staticsMiddleware.js","sourceRoot":"","sources":["../../../src/middlewares/staticsMiddleware.ts"],"names":[],"mappings":"AAEA,OAAO,IAAI,MAAM,UAAU,CAAC;AAE5B;;;GAGG;AACH,MAAM,UAAU,iBAAiB,CAAC,OAA+B;IAC/D,IAAI,OAAO,CAAC,KAAK,KAAK,KAAK;QAAE,OAAO,CAAC,KAAK,GAAG,OAAO,CAAC,KAAK,IAAI,YAAY,CAAC;IAE3E,OAAO,KAAK,EAAE,GAAY,EAAE,IAAS,EAAE,EAAE;QACvC,IAAI,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC,QAAQ,CAAC,GAAG,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,IAAI,EAAE;YACrD,IAAI,IAAI,GAAG,GAAG,CAAC,IAAI,CAAC;YAEpB,IAAI,GAAG,CAAC,aAAa,IAAI,GAAG,CAAC,aAAa,KAAK,GAAG,EAAE;gBAClD,IAAI,GAAG,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,GAAG,CAAC,aAAa,CAAC,CAAC,OAAO,CAAC,OAAO,EAAE,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC;aACzE;YACD,IAAI;gBACF,MAAM,IAAI,GAAG,MAAM,IAAI,CAAC,GAAU,EAAE,IAAI,EAAE,OAA2B,CAAC,CAAC;gBAEvE,IAAI,IAAI,EAAE;oBACR,OAAO;iBACR;aACF;YAAC,OAAO,GAAG,EAAE;gBACZ,IAAI,GAAG,CAAC,MAAM,KAAK,GAAG,EAAE;oBACtB,MAAM,GAAG,CAAC;iBACX;aACF;SACF;QAED,OAAO,IAAI,EAAE,CAAC;IAChB,CAAC,CAAC;AACJ,CAAC","sourcesContent":["import {PlatformStaticsOptions} from \"@tsed/common\";\nimport type {Context} from \"koa\";\nimport send from \"koa-send\";\n\n/**\n * @ignore\n * @param options\n */\nexport function staticsMiddleware(options: PlatformStaticsOptions): any {\n if (options.index !== false) options.index = options.index || \"index.html\";\n\n return async (ctx: Context, next: any) => {\n if ([\"GET\", \"HEAD\"].includes(ctx.method) && !ctx.body) {\n let path = ctx.path;\n\n if (ctx._matchedRoute && ctx._matchedRoute !== \"/\") {\n path = path.replace(String(ctx._matchedRoute).replace(\"/(.*)\", \"\"), \"\");\n }\n try {\n const done = await send(ctx as any, path, options as send.SendOptions);\n\n if (done) {\n return;\n }\n } catch (err) {\n if (err.status !== 404) {\n throw err;\n }\n }\n }\n\n return next();\n };\n}\n"]}
@@ -0,0 +1,29 @@
1
+ import { __decorate, __metadata, __param } from "tslib";
2
+ import { Configuration, Inject, PlatformHandler } from "@tsed/common";
3
+ import Koa from "koa";
4
+ import { PlatformKoaRouter } from "./PlatformKoaRouter";
5
+ const koaQs = require("koa-qs");
6
+ /**
7
+ * @platform
8
+ * @express
9
+ */
10
+ let PlatformKoaApplication = class PlatformKoaApplication extends PlatformKoaRouter {
11
+ constructor(platformHandler, configuration) {
12
+ super(platformHandler, configuration, {});
13
+ this.rawApp = configuration.get("koa.app") || new Koa();
14
+ koaQs(this.rawApp, "extended");
15
+ }
16
+ getApp() {
17
+ return this.rawApp;
18
+ }
19
+ callback() {
20
+ return this.getApp().callback();
21
+ }
22
+ };
23
+ PlatformKoaApplication = __decorate([
24
+ __param(0, Inject()),
25
+ __param(1, Configuration()),
26
+ __metadata("design:paramtypes", [PlatformHandler, Object])
27
+ ], PlatformKoaApplication);
28
+ export { PlatformKoaApplication };
29
+ //# sourceMappingURL=PlatformKoaApplication.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"PlatformKoaApplication.js","sourceRoot":"","sources":["../../../src/services/PlatformKoaApplication.ts"],"names":[],"mappings":";AACA,OAAO,EAAC,aAAa,EAAE,MAAM,EAAuB,eAAe,EAAC,MAAM,cAAc,CAAC;AACzF,OAAO,GAAG,MAAM,KAAK,CAAC;AACtB,OAAO,EAAC,iBAAiB,EAAC,MAAM,qBAAqB,CAAC;AAEtD,MAAM,KAAK,GAAG,OAAO,CAAC,QAAQ,CAAC,CAAC;AAQhC;;;GAGG;AACH,IAAa,sBAAsB,GAAnC,MAAa,sBAAuB,SAAQ,iBAAiB;IAI3D,YAAsB,eAAgC,EAAmB,aAA4B;QACnG,KAAK,CAAC,eAAe,EAAE,aAAa,EAAE,EAAE,CAAC,CAAC;QAE1C,IAAI,CAAC,MAAM,GAAG,aAAa,CAAC,GAAG,CAAC,SAAS,CAAC,IAAI,IAAI,GAAG,EAAE,CAAC;QACxD,KAAK,CAAC,IAAI,CAAC,MAAM,EAAE,UAAU,CAAC,CAAC;IACjC,CAAC;IAED,MAAM;QACJ,OAAO,IAAI,CAAC,MAAM,CAAC;IACrB,CAAC;IAED,QAAQ;QACN,OAAO,IAAI,CAAC,MAAM,EAAE,CAAC,QAAQ,EAAE,CAAC;IAClC,CAAC;CACF,CAAA;AAlBY,sBAAsB;IAIpB,WAAA,MAAM,EAAE,CAAA;IAAoC,WAAA,aAAa,EAAE,CAAA;qCAAjC,eAAe;GAJ3C,sBAAsB,CAkBlC;SAlBY,sBAAsB","sourcesContent":["import KoaRouter from \"@koa/router\";\nimport {Configuration, Inject, PlatformApplication, PlatformHandler} from \"@tsed/common\";\nimport Koa from \"koa\";\nimport {PlatformKoaRouter} from \"./PlatformKoaRouter\";\n\nconst koaQs = require(\"koa-qs\");\n\ndeclare global {\n namespace TsED {\n export interface Application extends Koa {}\n }\n}\n\n/**\n * @platform\n * @express\n */\nexport class PlatformKoaApplication extends PlatformKoaRouter implements PlatformApplication<Koa, KoaRouter> {\n app: Koa;\n rawApp: Koa;\n\n constructor(@Inject() platformHandler: PlatformHandler, @Configuration() configuration: Configuration) {\n super(platformHandler, configuration, {});\n\n this.rawApp = configuration.get(\"koa.app\") || new Koa();\n koaQs(this.rawApp, \"extended\");\n }\n\n getApp(): Koa {\n return this.rawApp;\n }\n\n callback(): any {\n return this.getApp().callback();\n }\n}\n"]}
@@ -0,0 +1,56 @@
1
+ import { HandlerType, PlatformHandler } from "@tsed/common";
2
+ import "./PlatformKoaRequest";
3
+ const OVERRIDE_TYPES = [HandlerType.ENDPOINT, HandlerType.MIDDLEWARE, HandlerType.ERR_MIDDLEWARE, HandlerType.CTX_FN];
4
+ export class PlatformKoaHandler extends PlatformHandler {
5
+ async flush(data, ctx) {
6
+ if (data === undefined && ctx.getResponse().body) {
7
+ data = ctx.getResponse().body;
8
+ }
9
+ return super.flush(data, ctx);
10
+ }
11
+ createRawHandler(metadata) {
12
+ if (OVERRIDE_TYPES.includes(metadata.type)) {
13
+ const handler = this.compileHandler(metadata);
14
+ return async (ctx, next) => handler({ next, $ctx: ctx.request.$ctx });
15
+ }
16
+ return super.createRawHandler(metadata);
17
+ }
18
+ async onRequest(requestOptions) {
19
+ const { metadata, $ctx } = requestOptions;
20
+ if ($ctx.data instanceof Error) {
21
+ if (metadata.hasErrorParam) {
22
+ requestOptions.err = $ctx.data;
23
+ }
24
+ else {
25
+ return this.onError($ctx.data, requestOptions);
26
+ }
27
+ }
28
+ return super.onRequest(requestOptions);
29
+ }
30
+ onError(error, requestOptions) {
31
+ const { next, $ctx, metadata } = requestOptions;
32
+ // istanbul ignore next
33
+ if ($ctx.isDone()) {
34
+ return;
35
+ }
36
+ $ctx.data = error;
37
+ if (!next || metadata.isFinal()) {
38
+ this.throwError(error, $ctx);
39
+ return;
40
+ }
41
+ return next();
42
+ }
43
+ async onCtxRequest(requestOptions) {
44
+ const { $ctx } = requestOptions;
45
+ try {
46
+ return await super.onCtxRequest(requestOptions);
47
+ }
48
+ catch (error) {
49
+ this.throwError(error, $ctx);
50
+ }
51
+ }
52
+ throwError(error, $ctx) {
53
+ $ctx.getApp().emit("error", error, $ctx.getRequest().ctx);
54
+ }
55
+ }
56
+ //# sourceMappingURL=PlatformKoaHandler.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"PlatformKoaHandler.js","sourceRoot":"","sources":["../../../src/services/PlatformKoaHandler.ts"],"names":[],"mappings":"AAAA,OAAO,EAAkB,WAAW,EAAqC,eAAe,EAAC,MAAM,cAAc,CAAC;AAE9G,OAAO,sBAAsB,CAAC;AAE9B,MAAM,cAAc,GAAG,CAAC,WAAW,CAAC,QAAQ,EAAE,WAAW,CAAC,UAAU,EAAE,WAAW,CAAC,cAAc,EAAE,WAAW,CAAC,MAAM,CAAC,CAAC;AAEtH,MAAM,OAAO,kBAAmB,SAAQ,eAAe;IAC9C,KAAK,CAAC,KAAK,CAAC,IAAS,EAAE,GAAoB;QAChD,IAAI,IAAI,KAAK,SAAS,IAAI,GAAG,CAAC,WAAW,EAAE,CAAC,IAAI,EAAE;YAChD,IAAI,GAAG,GAAG,CAAC,WAAW,EAAE,CAAC,IAAI,CAAC;SAC/B;QAED,OAAO,KAAK,CAAC,KAAK,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC;IAChC,CAAC;IAES,gBAAgB,CAAC,QAAyB;QAClD,IAAI,cAAc,CAAC,QAAQ,CAAC,QAAQ,CAAC,IAAI,CAAC,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M,KAAK,CAAC,YAAY,CAAC,cAAc,CAAC,CAAC;SACjD;QAAC,OAAO,KAAK,EAAE;YACd,IAAI,CAAC,UAAU,CAAC,KAAK,EAAE,IAAI,CAAC,CAAC;SAC9B;IACH,CAAC;IAES,UAAU,CAAC,KAAU,EAAE,IAAqB;QACpD,IAAI,CAAC,MAAM,EAAE,CAAC,IAAI,CAAC,OAAO,EAAE,KAAK,EAAE,IAAI,CAAC,UAAU,EAAE,CAAC,GAAG,CAAC,CAAC;IAC5D,CAAC;CACF","sourcesContent":["import {HandlerMetadata, HandlerType, OnRequestOptions, PlatformContext, PlatformHandler} from \"@tsed/common\";\nimport Koa from \"koa\";\nimport \"./PlatformKoaRequest\";\n\nconst OVERRIDE_TYPES = [HandlerType.ENDPOINT, HandlerType.MIDDLEWARE, HandlerType.ERR_MIDDLEWARE, HandlerType.CTX_FN];\n\nexport class PlatformKoaHandler extends PlatformHandler {\n public async flush(data: any, ctx: PlatformContext): Promise<void> {\n if (data === undefined && ctx.getResponse().body) {\n data = ctx.getResponse().body;\n }\n\n return super.flush(data, ctx);\n }\n\n protected createRawHandler(metadata: HandlerMetadata) {\n if (OVERRIDE_TYPES.includes(metadata.type)) {\n const handler = this.compileHandler(metadata);\n return async (ctx: Koa.Context, next: Koa.Next) => handler({next, $ctx: ctx.request.$ctx});\n }\n\n return super.createRawHandler(metadata);\n }\n\n protected async onRequest(requestOptions: OnRequestOptions): Promise<any> {\n const {metadata, $ctx} = requestOptions;\n\n if ($ctx.data instanceof Error) {\n if (metadata.hasErrorParam) {\n requestOptions.err = $ctx.data;\n } else {\n return this.onError($ctx.data, requestOptions);\n }\n }\n\n return super.onRequest(requestOptions);\n }\n\n protected onError(error: unknown, requestOptions: OnRequestOptions) {\n const {next, $ctx, metadata} = requestOptions;\n\n // istanbul ignore next\n if ($ctx.isDone()) {\n return;\n }\n\n $ctx.data = error;\n\n if (!next || metadata.isFinal()) {\n this.throwError(error, $ctx);\n\n return;\n }\n\n return next();\n }\n\n protected async onCtxRequest(requestOptions: OnRequestOptions): Promise<any> {\n const {$ctx} = requestOptions;\n\n try {\n return await super.onCtxRequest(requestOptions);\n } catch (error) {\n this.throwError(error, $ctx);\n }\n }\n\n protected throwError(error: any, $ctx: PlatformContext) {\n $ctx.getApp().emit(\"error\", error, $ctx.getRequest().ctx);\n }\n}\n"]}
@@ -0,0 +1,40 @@
1
+ var _PlatformKoaRequest_ctx;
2
+ import { __classPrivateFieldGet, __classPrivateFieldSet } from "tslib";
3
+ import "@koa/router";
4
+ import { PlatformRequest } from "@tsed/common";
5
+ /**
6
+ * @platform
7
+ * @koa
8
+ */
9
+ export class PlatformKoaRequest extends PlatformRequest {
10
+ constructor(event, $ctx) {
11
+ super(event, $ctx);
12
+ _PlatformKoaRequest_ctx.set(this, void 0);
13
+ __classPrivateFieldSet(this, _PlatformKoaRequest_ctx, this.raw.ctx, "f");
14
+ }
15
+ get protocol() {
16
+ return __classPrivateFieldGet(this, _PlatformKoaRequest_ctx, "f").request.protocol;
17
+ }
18
+ get host() {
19
+ return __classPrivateFieldGet(this, _PlatformKoaRequest_ctx, "f").request.host;
20
+ }
21
+ get secure() {
22
+ return __classPrivateFieldGet(this, _PlatformKoaRequest_ctx, "f").request.secure;
23
+ }
24
+ get cookies() {
25
+ return __classPrivateFieldGet(this, _PlatformKoaRequest_ctx, "f").cookie || __classPrivateFieldGet(this, _PlatformKoaRequest_ctx, "f").cookies;
26
+ }
27
+ get session() {
28
+ return __classPrivateFieldGet(this, _PlatformKoaRequest_ctx, "f").session;
29
+ }
30
+ getReq() {
31
+ return this.raw.req;
32
+ }
33
+ destroy() {
34
+ // @ts-ignore
35
+ __classPrivateFieldSet(this, _PlatformKoaRequest_ctx, null, "f");
36
+ return super.destroy();
37
+ }
38
+ }
39
+ _PlatformKoaRequest_ctx = new WeakMap();
40
+ //# sourceMappingURL=PlatformKoaRequest.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"PlatformKoaRequest.js","sourceRoot":"","sources":["../../../src/services/PlatformKoaRequest.ts"],"names":[],"mappings":";;AAAA,OAAO,aAAa,CAAC;AACrB,OAAO,EAAiC,eAAe,EAAC,MAAM,cAAc,CAAC;AAiB7E;;;GAGG;AACH,MAAM,OAAO,kBAAmB,SAAQ,eAA4B;IAGlE,YAAY,KAAoB,EAAE,IAAqB;QACrD,KAAK,CAAC,KAAK,EAAE,IAAI,CAAC,CAAC;QAHrB,0CAAkB;QAIhB,uBAAA,IAAI,2BAAQ,IAAI,CAAC,GAAG,CAAC,GAAG,MAAA,CAAC;IAC3B,CAAC;IAED,IAAI,QAAQ;QACV,OAAO,uBAAA,IAAI,+BAAK,CAAC,OAAO,CAAC,QAAQ,CAAC;IACpC,CAAC;IAED,IAAI,IAAI;QACN,OAAO,uBAAA,IAAI,+BAAK,CAAC,OAAO,CAAC,IAAI,CAAC;IAChC,CAAC;IAED,IAAI,MAAM;QACR,OAAO,uBAAA,IAAI,+BAAK,CAAC,OAAO,CAAC,MAAM,CAAC;IAClC,CAAC;IAED,IAAI,OAAO;QACT,OAAO,uBAAA,IAAI,+BAAK,CAAC,MAAM,IAAI,uBAAA,IAAI,+BAAK,CAAC,OAAO,CAAC;IAC/C,CAAC;IAED,IAAI,OAAO;QACT,OAAO,uBAAA,IAAI,+BAAK,CAAC,OAAO,CAAC;IAC3B,CAAC;IAED,MAAM;QACJ,OAAO,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC;IACtB,CAAC;IAED,OAAO;QACL,aAAa;QACb,uBAAA,IAAI,2BAAQ,IAAI,MAAA,CAAC;QACjB,OAAO,KAAK,CAAC,OAAO,EAAE,CAAC;IACzB,CAAC;CACF","sourcesContent":["import \"@koa/router\";\nimport {IncomingEvent, PlatformContext, PlatformRequest} from \"@tsed/common\";\nimport {IncomingMessage} from \"http\";\nimport Koa from \"koa\";\n\ndeclare module \"koa\" {\n export interface Request {\n id: string;\n $ctx: PlatformContext;\n }\n}\n\ndeclare global {\n namespace TsED {\n export interface Request extends Koa.Request {}\n }\n}\n\n/**\n * @platform\n * @koa\n */\nexport class PlatformKoaRequest extends PlatformRequest<Koa.Request> {\n #ctx: Koa.Context;\n\n constructor(event: IncomingEvent, $ctx: PlatformContext) {\n super(event, $ctx);\n this.#ctx = this.raw.ctx;\n }\n\n get protocol(): string {\n return this.#ctx.request.protocol;\n }\n\n get host(): string {\n return this.#ctx.request.host;\n }\n\n get secure(): boolean {\n return this.#ctx.request.secure;\n }\n\n get cookies(): {[p: string]: any} {\n return this.#ctx.cookie || this.#ctx.cookies;\n }\n\n get session(): any {\n return this.#ctx.session;\n }\n\n getReq(): IncomingMessage {\n return this.raw.req;\n }\n\n destroy() {\n // @ts-ignore\n this.#ctx = null;\n return super.destroy();\n }\n}\n"]}
@@ -0,0 +1,101 @@
1
+ var _PlatformKoaResponse_ctx;
2
+ import { __classPrivateFieldGet, __classPrivateFieldSet } from "tslib";
3
+ import { PlatformResponse } from "@tsed/common";
4
+ import { getStatusMessage } from "@tsed/schema";
5
+ const encodeUrl = require("encodeurl");
6
+ /**
7
+ * @platform
8
+ * @koa
9
+ */
10
+ export class PlatformKoaResponse extends PlatformResponse {
11
+ constructor(event, $ctx) {
12
+ super(event, $ctx);
13
+ _PlatformKoaResponse_ctx.set(this, void 0);
14
+ __classPrivateFieldSet(this, _PlatformKoaResponse_ctx, this.raw.ctx, "f");
15
+ }
16
+ get statusCode() {
17
+ return this.raw.status;
18
+ }
19
+ get locals() {
20
+ return __classPrivateFieldGet(this, _PlatformKoaResponse_ctx, "f").state;
21
+ }
22
+ /**
23
+ * Return the Node.js response object
24
+ */
25
+ getRes() {
26
+ return this.raw.res;
27
+ }
28
+ hasStatus() {
29
+ // KOA set 404 by default
30
+ return this.statusCode !== 404;
31
+ }
32
+ /**
33
+ * Sets the HTTP status for the response.
34
+ *
35
+ * @param status
36
+ */
37
+ status(status) {
38
+ this.raw.status = status;
39
+ return this;
40
+ }
41
+ /**
42
+ * Set `Content-Type` response header with `type` through `mime.lookup()`
43
+ * when it does not contain "/", or set the Content-Type to `type` otherwise.
44
+ *
45
+ * Examples:
46
+ *
47
+ * res.type('.html');
48
+ * res.type('html');
49
+ * res.type('json');
50
+ * res.type('application/json');
51
+ * res.type('png');
52
+ */
53
+ contentType(contentType) {
54
+ this.raw.type = contentType;
55
+ return this;
56
+ }
57
+ getHeaders() {
58
+ return this.raw.headers;
59
+ }
60
+ /**
61
+ * Send any data to your consumer.
62
+ *
63
+ * This method accept a ReadableStream, a plain object, boolean, string, number, null and undefined data.
64
+ * It choose the better way to send the data.
65
+ *
66
+ * @param data
67
+ */
68
+ body(data) {
69
+ this.raw.body = data;
70
+ return this;
71
+ }
72
+ getBody() {
73
+ return this.raw.body;
74
+ }
75
+ redirect(status, url) {
76
+ status = status || 302;
77
+ // Set location header
78
+ url = this.location(url).raw.get("Location");
79
+ this.body(`${getStatusMessage(status)}. Redirecting to ${url}`);
80
+ this.status(status);
81
+ this.setHeader("Content-Length", Buffer.byteLength(this.raw.body));
82
+ if (this.request.method === "HEAD") {
83
+ this.getRes().end();
84
+ }
85
+ else {
86
+ this.getRes().end(this.getBody());
87
+ }
88
+ return this;
89
+ }
90
+ location(location) {
91
+ // "back" is an alias for the referrer
92
+ if (location === "back") {
93
+ location = this.request.get("Referrer") || "/";
94
+ }
95
+ // set location
96
+ this.raw.set("Location", encodeUrl(location));
97
+ return this;
98
+ }
99
+ }
100
+ _PlatformKoaResponse_ctx = new WeakMap();
101
+ //# sourceMappingURL=PlatformKoaResponse.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"PlatformKoaResponse.js","sourceRoot":"","sources":["../../../src/services/PlatformKoaResponse.ts"],"names":[],"mappings":";;AAAA,OAAO,EAAiC,gBAAgB,EAAC,MAAM,cAAc,CAAC;AAG9E,OAAO,EAAC,gBAAgB,EAAC,MAAM,cAAc,CAAC;AAE9C,MAAM,SAAS,GAAG,OAAO,CAAC,WAAW,CAAC,CAAC;AAQvC;;;GAGG;AACH,MAAM,OAAO,mBAAoB,SAAQ,gB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AAC,CAAC;QAE7C,IAAI,CAAC,IAAI,CAAC,GAAG,gBAAgB,CAAC,MAAM,CAAC,oBAAoB,GAAG,EAAE,CAAC,CAAC;QAChE,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;QACpB,IAAI,CAAC,SAAS,CAAC,gBAAgB,EAAE,MAAM,CAAC,UAAU,CAAC,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC,CAAC;QAEnE,IAAI,IAAI,CAAC,OAAO,CAAC,MAAM,KAAK,MAAM,EAAE;YAClC,IAAI,CAAC,MAAM,EAAE,CAAC,GAAG,EAAE,CAAC;SACrB;aAAM;YACL,IAAI,CAAC,MAAM,EAAE,CAAC,GAAG,CAAC,IAAI,CAAC,OAAO,EAAE,CAAC,CAAC;SACnC;QAED,OAAO,IAAI,CAAC;IACd,CAAC;IAED,QAAQ,CAAC,QAAgB;QACvB,sCAAsC;QACtC,IAAI,QAAQ,KAAK,MAAM,EAAE;YACvB,QAAQ,GAAG,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,UAAU,CAAC,IAAI,GAAG,CAAC;SAChD;QAED,eAAe;QACf,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,UAAU,EAAE,SAAS,CAAC,QAAQ,CAAC,CAAC,CAAC;QAE9C,OAAO,IAAI,CAAC;IACd,CAAC;CACF","sourcesContent":["import {IncomingEvent, PlatformContext, PlatformResponse} from \"@tsed/common\";\nimport {ServerResponse} from \"http\";\nimport Koa from \"koa\";\nimport {getStatusMessage} from \"@tsed/schema\";\n\nconst encodeUrl = require(\"encodeurl\");\n\ndeclare global {\n namespace TsED {\n export interface Response extends Koa.Response {}\n }\n}\n\n/**\n * @platform\n * @koa\n */\nexport class PlatformKoaResponse extends PlatformResponse<Koa.Response> {\n #ctx: Koa.Context;\n\n constructor(event: IncomingEvent, $ctx: PlatformContext) {\n super(event, $ctx);\n this.#ctx = this.raw.ctx;\n }\n\n get statusCode() {\n return this.raw.status;\n }\n\n get locals() {\n return this.#ctx.state;\n }\n\n /**\n * Return the Node.js response object\n */\n getRes(): ServerResponse {\n return this.raw.res;\n }\n\n hasStatus() {\n // KOA set 404 by default\n return this.statusCode !== 404;\n }\n\n /**\n * Sets the HTTP status for the response.\n *\n * @param status\n */\n status(status: number) {\n this.raw.status = status;\n\n return this;\n }\n\n /**\n * Set `Content-Type` response header with `type` through `mime.lookup()`\n * when it does not contain \"/\", or set the Content-Type to `type` otherwise.\n *\n * Examples:\n *\n * res.type('.html');\n * res.type('html');\n * res.type('json');\n * res.type('application/json');\n * res.type('png');\n */\n contentType(contentType: string) {\n this.raw.type = contentType;\n\n return this;\n }\n\n getHeaders() {\n return this.raw.headers;\n }\n\n /**\n * Send any data to your consumer.\n *\n * This method accept a ReadableStream, a plain object, boolean, string, number, null and undefined data.\n * It choose the better way to send the data.\n *\n * @param data\n */\n body(data: any): this {\n this.raw.body = data;\n\n return this;\n }\n\n getBody() {\n return this.raw.body;\n }\n\n redirect(status: number, url: string): this {\n status = status || 302;\n // Set location header\n url = this.location(url).raw.get(\"Location\");\n\n this.body(`${getStatusMessage(status)}. Redirecting to ${url}`);\n this.status(status);\n this.setHeader(\"Content-Length\", Buffer.byteLength(this.raw.body));\n\n if (this.request.method === \"HEAD\") {\n this.getRes().end();\n } else {\n this.getRes().end(this.getBody());\n }\n\n return this;\n }\n\n location(location: string): this {\n // \"back\" is an alias for the referrer\n if (location === \"back\") {\n location = this.request.get(\"Referrer\") || \"/\";\n }\n\n // set location\n this.raw.set(\"Location\", encodeUrl(location));\n\n return this;\n }\n}\n"]}