@tsed/platform-router 7.0.0-beta.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (51) hide show
  1. package/jest.config.js +14 -0
  2. package/lib/cjs/domain/PlatformHandlerMetadata.js +106 -0
  3. package/lib/cjs/domain/PlatformHandlerMetadata.js.map +1 -0
  4. package/lib/cjs/domain/PlatformHandlerType.js +14 -0
  5. package/lib/cjs/domain/PlatformHandlerType.js.map +1 -0
  6. package/lib/cjs/domain/PlatformLayer.js +40 -0
  7. package/lib/cjs/domain/PlatformLayer.js.map +1 -0
  8. package/lib/cjs/domain/PlatformRouter.js +113 -0
  9. package/lib/cjs/domain/PlatformRouter.js.map +1 -0
  10. package/lib/cjs/domain/PlatformRouters.js +131 -0
  11. package/lib/cjs/domain/PlatformRouters.js.map +1 -0
  12. package/lib/cjs/domain/SinglePathType.js +3 -0
  13. package/lib/cjs/domain/SinglePathType.js.map +1 -0
  14. package/lib/cjs/index.js +15 -0
  15. package/lib/cjs/index.js.map +1 -0
  16. package/lib/cjs/package.json +3 -0
  17. package/lib/cjs/utils/formatMethod.js +9 -0
  18. package/lib/cjs/utils/formatMethod.js.map +1 -0
  19. package/lib/cjs/utils/useContextHandler.js +15 -0
  20. package/lib/cjs/utils/useContextHandler.js.map +1 -0
  21. package/lib/esm/domain/PlatformHandlerMetadata.js +102 -0
  22. package/lib/esm/domain/PlatformHandlerMetadata.js.map +1 -0
  23. package/lib/esm/domain/PlatformHandlerType.js +11 -0
  24. package/lib/esm/domain/PlatformHandlerType.js.map +1 -0
  25. package/lib/esm/domain/PlatformLayer.js +36 -0
  26. package/lib/esm/domain/PlatformLayer.js.map +1 -0
  27. package/lib/esm/domain/PlatformRouter.js +110 -0
  28. package/lib/esm/domain/PlatformRouter.js.map +1 -0
  29. package/lib/esm/domain/PlatformRouters.js +128 -0
  30. package/lib/esm/domain/PlatformRouters.js.map +1 -0
  31. package/lib/esm/domain/SinglePathType.js +2 -0
  32. package/lib/esm/domain/SinglePathType.js.map +1 -0
  33. package/lib/esm/index.js +12 -0
  34. package/lib/esm/index.js.map +1 -0
  35. package/lib/esm/package.json +3 -0
  36. package/lib/esm/utils/formatMethod.js +5 -0
  37. package/lib/esm/utils/formatMethod.js.map +1 -0
  38. package/lib/esm/utils/useContextHandler.js +11 -0
  39. package/lib/esm/utils/useContextHandler.js.map +1 -0
  40. package/lib/types/domain/PlatformHandlerMetadata.d.ts +39 -0
  41. package/lib/types/domain/PlatformHandlerType.d.ts +9 -0
  42. package/lib/types/domain/PlatformLayer.d.ts +37 -0
  43. package/lib/types/domain/PlatformRouter.d.ts +28 -0
  44. package/lib/types/domain/PlatformRouters.d.ts +14 -0
  45. package/lib/types/domain/SinglePathType.d.ts +1 -0
  46. package/lib/types/index.d.ts +11 -0
  47. package/lib/types/utils/formatMethod.d.ts +1 -0
  48. package/lib/types/utils/useContextHandler.d.ts +13 -0
  49. package/package.json +72 -0
  50. package/readme.md +176 -0
  51. package/tsconfig.compile.esm.json +12 -0
@@ -0,0 +1,14 @@
1
+ import { Hooks } from "@tsed/core";
2
+ import { InjectorService, TokenProvider } from "@tsed/di";
3
+ import { PlatformParams } from "@tsed/platform-params";
4
+ import { PlatformLayer } from "./PlatformLayer";
5
+ import { PlatformRouter } from "./PlatformRouter";
6
+ export declare class PlatformRouters {
7
+ protected readonly injector: InjectorService;
8
+ protected readonly platformParams: PlatformParams;
9
+ readonly hooks: Hooks;
10
+ constructor(injector: InjectorService, platformParams: PlatformParams);
11
+ prebuild(): void;
12
+ from(token: TokenProvider, parentMiddlewares?: any[]): PlatformRouter;
13
+ getLayers(router: PlatformRouter): PlatformLayer[];
14
+ }
@@ -0,0 +1 @@
1
+ export declare type SinglePathType = string | RegExp;
@@ -0,0 +1,11 @@
1
+ /**
2
+ * @file Automatically generated by barrelsby.
3
+ */
4
+ export * from "./domain/PlatformHandlerMetadata";
5
+ export * from "./domain/PlatformHandlerType";
6
+ export * from "./domain/PlatformLayer";
7
+ export * from "./domain/PlatformRouter";
8
+ export * from "./domain/PlatformRouters";
9
+ export * from "./domain/SinglePathType";
10
+ export * from "./utils/formatMethod";
11
+ export * from "./utils/useContextHandler";
@@ -0,0 +1 @@
1
+ export declare function formatMethod(method: string | undefined): string;
@@ -0,0 +1,13 @@
1
+ import { DIContext } from "@tsed/di";
2
+ import { PlatformHandlerType } from "../domain/PlatformHandlerType";
3
+ export declare type PlatformContextHandler<Context = DIContext> = ($ctx: Context) => any | Promise<any>;
4
+ /**
5
+ * Create Ts.ED context handler
6
+ * @param fn
7
+ * @ignore
8
+ */
9
+ export declare function useContextHandler(fn: PlatformContextHandler & {
10
+ type?: PlatformHandlerType;
11
+ }): PlatformContextHandler<DIContext> & {
12
+ type?: PlatformHandlerType | undefined;
13
+ };
package/package.json ADDED
@@ -0,0 +1,72 @@
1
+ {
2
+ "name": "@tsed/platform-router",
3
+ "version": "7.0.0-beta.14",
4
+ "description": "Router",
5
+ "private": false,
6
+ "source": "./src/index.ts",
7
+ "main": "./lib/cjs/index.js",
8
+ "module": "./lib/esm/index.js",
9
+ "typings": "./lib/types/index.d.ts",
10
+ "exports": {
11
+ "types": "./lib/types/index.d.ts",
12
+ "import": "./lib/esm/index.js",
13
+ "require": "./lib/cjs/index.js",
14
+ "default": "./lib/esm/index.js"
15
+ },
16
+ "scripts": {
17
+ "build": "yarn barrels && yarn run build:esm && yarn run build:cjs",
18
+ "build:cjs": "tsc --build tsconfig.compile.json",
19
+ "build:esm": "tsc --build tsconfig.compile.esm.json",
20
+ "barrels": "yarn barrelsby --delete -d ./src -e \"\\.spec\\.ts\" -e \"__mock__\" -e \".benchmark.ts\"",
21
+ "test": "cross-env NODE_ENV=test yarn jest --max-workers=2"
22
+ },
23
+ "dependencies": {
24
+ "tslib": "2.4.0"
25
+ },
26
+ "devDependencies": {
27
+ "@tsed/core": "7.0.0-beta.14",
28
+ "@tsed/di": "7.0.0-beta.14",
29
+ "@tsed/exceptions": "7.0.0-beta.14",
30
+ "@tsed/json-mapper": "7.0.0-beta.14",
31
+ "@tsed/platform-params": "7.0.0-beta.14",
32
+ "@tsed/schema": "7.0.0-beta.14"
33
+ },
34
+ "peerDependencies": {
35
+ "@tsed/core": "^7.0.0-beta.14",
36
+ "@tsed/di": "^7.0.0-beta.14",
37
+ "@tsed/exceptions": "^7.0.0-beta.14",
38
+ "@tsed/json-mapper": "^7.0.0-beta.14",
39
+ "@tsed/platform-params": "^7.0.0-beta.14",
40
+ "@tsed/schema": "^7.0.0-beta.14"
41
+ },
42
+ "peerDependenciesMeta": {
43
+ "@tsed/core": {
44
+ "optional": false
45
+ },
46
+ "@tsed/di": {
47
+ "optional": false
48
+ },
49
+ "@tsed/exceptions": {
50
+ "optional": false
51
+ },
52
+ "@tsed/json-mapper": {
53
+ "optional": false
54
+ },
55
+ "@tsed/schema": {
56
+ "optional": false
57
+ },
58
+ "@tsed/platform-params": {
59
+ "optional": false
60
+ }
61
+ },
62
+ "repository": "https://github.com/tsedio/tsed",
63
+ "bugs": {
64
+ "url": "https://github.com/tsedio/tsed/issues"
65
+ },
66
+ "homepage": "https://github.com/tsedio/tsed/tree/beta/packages/platform/platform-router",
67
+ "author": "Romain Lenzotti",
68
+ "license": "MIT",
69
+ "publishConfig": {
70
+ "tag": "beta"
71
+ }
72
+ }
package/readme.md ADDED
@@ -0,0 +1,176 @@
1
+ <p style="text-align: center" align="center">
2
+ <a href="https://tsed.io" target="_blank"><img src="https://tsed.io/tsed-og.png" width="200" alt="Ts.ED logo"/></a>
3
+ </p>
4
+
5
+ <div align="center">
6
+ <h1>@tsed/platform-router</h1>
7
+
8
+ [![Build & Release](https://github.com/tsedio/tsed/workflows/Build%20&%20Release/badge.svg)](https://github.com/tsedio/tsed/actions?query=workflow%3A%22Build+%26+Release%22)
9
+ [![PR Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](https://github.com/tsedio/tsed/blob/master/CONTRIBUTING.md)
10
+ [![Coverage Status](https://coveralls.io/repos/github/tsedio/tsed/badge.svg?branch=production)](https://coveralls.io/github/tsedio/tsed?branch=production)
11
+ [![npm version](https://badge.fury.io/js/%40tsed%2Fcommon.svg)](https://badge.fury.io/js/%40tsed%2Fcommon)
12
+ [![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release)
13
+ [![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square)](https://github.com/prettier/prettier)
14
+ [![github](https://img.shields.io/static/v1?label=Github%20sponsor&message=%E2%9D%A4&logo=GitHub&color=%23fe8e86)](https://github.com/sponsors/romakita)
15
+ [![opencollective](https://img.shields.io/static/v1?label=OpenCollective%20sponsor&message=%E2%9D%A4&logo=OpenCollective&color=%23fe8e86)](https://opencollective.com/tsed)
16
+
17
+ </div>
18
+
19
+ <div align="center">
20
+ <a href="https://tsed.io/">Website</a>
21
+ <span>&nbsp;&nbsp;•&nbsp;&nbsp;</span>
22
+ <a href="https://tsed.io/getting-started/">Getting started</a>
23
+ <span>&nbsp;&nbsp;•&nbsp;&nbsp;</span>
24
+ <a href="https://slack.tsed.io">Slack</a>
25
+ <span>&nbsp;&nbsp;•&nbsp;&nbsp;</span>
26
+ <a href="https://twitter.com/TsED_io">Twitter</a>
27
+ </div>
28
+
29
+ <hr />
30
+
31
+ A package of Ts.ED framework. See website: https://tsed.io/
32
+
33
+ # Installation
34
+
35
+ ```bash
36
+ npm install --save @tsed/platform-router
37
+ ```
38
+
39
+ ## Usage
40
+
41
+ This module generates routers from decorated class.
42
+
43
+ ```typescript
44
+ import express from "express";
45
+ import {Controller, InjectorService} from "@tsed/di";
46
+ import {PlatformHandlerType, PlatformHandlerMetadata, PlatformRouter, PlatformLayer} from "@tsed/platform-router";
47
+ import {Delete, Get, Head, Options, Patch, Post, Put} from "@tsed/schema";
48
+
49
+ @Controller("/nested")
50
+ class NestedController {
51
+ @Get("/")
52
+ get() {}
53
+
54
+ @Post("/")
55
+ post() {}
56
+
57
+ @Put("/:id")
58
+ put() {}
59
+
60
+ @Delete("/:id")
61
+ delete() {}
62
+
63
+ @Head("/:id")
64
+ head() {}
65
+
66
+ @Options("/:id")
67
+ option() {}
68
+
69
+ @Patch("/:id")
70
+ patch() {}
71
+ }
72
+
73
+ @Controller({path: "/controller", children: [NestedController]})
74
+ @UseBefore(function useBefore() {})
75
+ class MyController {
76
+ @Get("/")
77
+ get() {}
78
+
79
+ @Post("/")
80
+ post() {}
81
+
82
+ @Put("/:id")
83
+ put() {}
84
+
85
+ @Delete("/:id")
86
+ delete() {}
87
+
88
+ @Head("/:id")
89
+ head() {}
90
+
91
+ @Options("/:id")
92
+ option() {}
93
+
94
+ @Patch("/:id")
95
+ patch() {}
96
+ }
97
+
98
+ const injector = new InjectorService();
99
+ const expressApp = express();
100
+
101
+ injector.addProvider(MyController);
102
+ injector.addProvider(NestedController);
103
+
104
+ const appRouter = new PlatformRouter(injector);
105
+
106
+ appRouter.use("/rest", MyController);
107
+
108
+ // transform handlerMetadata to a compatible handler for the targeted framework (Express.js, Koa.js, etc...)
109
+ PlatformRouter.hooks.on("alterHandler", (handlerMetadata: PlatformHandlerMetadata) => {
110
+ const handler = handlerMetadata.compileHandler(injector);
111
+
112
+ switch (handlerMetadata.type) {
113
+ default:
114
+ case PlatformHandlerType.RAW_FN: // native express.js/koa.js handler
115
+ return handler;
116
+ case PlatformHandlerType.MIDDLEWARE:
117
+ case PlatformHandlerType.ENDPOINT:
118
+ return (req, res, next) => {
119
+ return next();
120
+ };
121
+ case PlatformHandlerType.ERR_MIDDLEWARE:
122
+ return (error: unknown, req, res, next) => {
123
+ return next();
124
+ };
125
+ case PlatformHandlerType.CUSTOM:
126
+ return (req, res, next) => {
127
+ return next();
128
+ };
129
+ }
130
+ });
131
+
132
+ // bind layers to the framework router
133
+ appRouter.getLayers().forEach((layer: PlatformLayer) => {
134
+ const {method, args} = layer;
135
+
136
+ expressApp[method](...layer.args);
137
+ });
138
+
139
+ expressApp.listen(3000);
140
+ ```
141
+
142
+ ## Contributors
143
+
144
+ Please read [contributing guidelines here](https://tsed.io/CONTRIBUTING.html).
145
+
146
+ <a href="https://github.com/tsedio/ts-express-decorators/graphs/contributors"><img src="https://opencollective.com/tsed/contributors.svg?width=890" /></a>
147
+
148
+ ## Backers
149
+
150
+ Thank you to all our backers! 🙏 [[Become a backer](https://opencollective.com/tsed#backer)]
151
+
152
+ <a href="https://opencollective.com/tsed#backers" target="_blank"><img src="https://opencollective.com/tsed/tiers/backer.svg?width=890"></a>
153
+
154
+ ## Sponsors
155
+
156
+ Support this project by becoming a sponsor. Your logo will show up here with a link to your
157
+ website. [[Become a sponsor](https://opencollective.com/tsed#sponsor)]
158
+
159
+ ## License
160
+
161
+ The MIT License (MIT)
162
+
163
+ Copyright (c) 2016 - 2018 Romain Lenzotti
164
+
165
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
166
+ documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
167
+ rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit
168
+ persons to whom the Software is furnished to do so, subject to the following conditions:
169
+
170
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
171
+ Software.
172
+
173
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
174
+ WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
175
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
176
+ OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,12 @@
1
+ {
2
+ "extends": "./tsconfig.compile.json",
3
+ "compilerOptions": {
4
+ "baseUrl": ".",
5
+ "module": "ES2020",
6
+ "rootDir": "src",
7
+ "outDir": "./lib/esm",
8
+ "declaration": true,
9
+ "declarationDir": "./lib/types"
10
+ },
11
+ "exclude": ["node_modules", "test", "lib", "benchmark", "coverage", "spec", "**/*.benchmark.ts", "**/*.spec.ts", "keys", "jest.config.js"]
12
+ }