@e22m4u/ts-rest-router 0.1.1 → 0.2.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.
- package/README.md +118 -118
- package/dist/cjs/index.cjs +346 -360
- package/dist/esm/controller-registry.js +28 -28
- package/dist/esm/debuggable-service.d.ts +10 -1
- package/dist/esm/debuggable-service.js +14 -3
- package/dist/esm/decorators/after-action/after-action-decorator.d.ts +9 -0
- package/dist/esm/decorators/{after/after-decorator.js → after-action/after-action-decorator.js} +7 -7
- package/dist/esm/decorators/{before/before-decorator.spec.js → after-action/after-action-decorator.spec.js} +19 -19
- package/dist/esm/decorators/{after/after-metadata.d.ts → after-action/after-action-metadata.d.ts} +4 -4
- package/dist/esm/decorators/after-action/after-action-metadata.js +5 -0
- package/dist/esm/decorators/{after/after-reflector.d.ts → after-action/after-action-reflector.d.ts} +5 -5
- package/dist/esm/decorators/{before/before-reflector.js → after-action/after-action-reflector.js} +6 -6
- package/dist/esm/decorators/{before/before-reflector.spec.js → after-action/after-action-reflector.spec.js} +23 -23
- package/dist/esm/decorators/after-action/index.d.ts +3 -0
- package/dist/esm/decorators/after-action/index.js +3 -0
- package/dist/esm/decorators/before-action/before-action-decorator.d.ts +9 -0
- package/dist/esm/decorators/{before/before-decorator.js → before-action/before-action-decorator.js} +7 -7
- package/dist/esm/decorators/{after/after-decorator.spec.js → before-action/before-action-decorator.spec.js} +19 -19
- package/dist/esm/decorators/{before/before-metadata.d.ts → before-action/before-action-metadata.d.ts} +4 -4
- package/dist/esm/decorators/before-action/before-action-metadata.js +5 -0
- package/dist/esm/decorators/{before/before-reflector.d.ts → before-action/before-action-reflector.d.ts} +5 -5
- package/dist/esm/decorators/{after/after-reflector.js → before-action/before-action-reflector.js} +6 -6
- package/dist/esm/decorators/{after/after-reflector.spec.js → before-action/before-action-reflector.spec.js} +23 -23
- package/dist/esm/decorators/before-action/index.d.ts +3 -0
- package/dist/esm/decorators/before-action/index.js +3 -0
- package/dist/esm/decorators/index.d.ts +4 -4
- package/dist/esm/decorators/index.js +4 -4
- package/dist/esm/decorators/request-context/request-context-decorator.d.ts +4 -4
- package/dist/esm/decorators/request-context/request-context-decorator.js +4 -4
- package/dist/esm/decorators/request-context/request-context-decorator.spec.js +6 -0
- package/dist/esm/decorators/request-data/request-data-decorator.d.ts +10 -10
- package/dist/esm/decorators/request-data/request-data-decorator.js +10 -10
- package/dist/esm/decorators/request-data/request-data-decorator.spec.js +44 -32
- package/dist/esm/decorators/request-data/request-data-metadata.d.ts +1 -1
- package/dist/esm/decorators/rest-action/index.d.ts +3 -0
- package/dist/esm/decorators/rest-action/index.js +3 -0
- package/dist/esm/decorators/rest-action/rest-action-decorator.d.ts +57 -0
- package/dist/esm/decorators/rest-action/rest-action-decorator.js +52 -0
- package/dist/esm/decorators/rest-action/rest-action-decorator.spec.js +401 -0
- package/dist/esm/decorators/{action/action-metadata.d.ts → rest-action/rest-action-metadata.d.ts} +6 -6
- package/dist/esm/decorators/rest-action/rest-action-metadata.js +5 -0
- package/dist/esm/decorators/rest-action/rest-action-reflector.d.ts +22 -0
- package/dist/esm/decorators/{action/action-reflector.js → rest-action/rest-action-reflector.js} +6 -6
- package/dist/esm/decorators/{action/action-reflector.spec.js → rest-action/rest-action-reflector.spec.js} +13 -13
- package/dist/esm/decorators/rest-controller/index.d.ts +3 -0
- package/dist/esm/decorators/rest-controller/index.js +3 -0
- package/dist/esm/decorators/rest-controller/rest-controller-decorator.d.ts +14 -0
- package/dist/esm/decorators/{controller/controller-decorator.js → rest-controller/rest-controller-decorator.js} +5 -5
- package/dist/esm/decorators/{controller/controller-decorator.spec.js → rest-controller/rest-controller-decorator.spec.js} +15 -15
- package/dist/esm/decorators/{controller/controller-metadata.d.ts → rest-controller/rest-controller-metadata.d.ts} +5 -5
- package/dist/esm/decorators/rest-controller/rest-controller-metadata.js +5 -0
- package/dist/esm/decorators/rest-controller/rest-controller-reflector.d.ts +20 -0
- package/dist/esm/decorators/rest-controller/rest-controller-reflector.js +24 -0
- package/dist/esm/decorators/{controller/controller-reflector.spec.js → rest-controller/rest-controller-reflector.spec.js} +12 -12
- package/dist/esm/errors/not-a-controller-error.js +1 -1
- package/dist/esm/types.d.ts +0 -10
- package/dist/esm/utils/index.d.ts +0 -1
- package/dist/esm/utils/index.js +0 -1
- package/package.json +19 -19
- package/src/controller-registry.spec.ts +122 -122
- package/src/controller-registry.ts +33 -35
- package/src/debuggable-service.ts +17 -4
- package/src/decorators/{after/after-decorator.spec.ts → after-action/after-action-decorator.spec.ts} +19 -19
- package/src/decorators/{before/before-decorator.ts → after-action/after-action-decorator.ts} +9 -9
- package/src/decorators/{after/after-metadata.ts → after-action/after-action-metadata.ts} +5 -5
- package/src/decorators/{before/before-reflector.spec.ts → after-action/after-action-reflector.spec.ts} +33 -23
- package/src/decorators/{before/before-reflector.ts → after-action/after-action-reflector.ts} +13 -9
- package/src/decorators/after-action/index.ts +3 -0
- package/src/decorators/{before/before-decorator.spec.ts → before-action/before-action-decorator.spec.ts} +19 -19
- package/src/decorators/{after/after-decorator.ts → before-action/before-action-decorator.ts} +9 -9
- package/src/decorators/before-action/before-action-metadata.ts +17 -0
- package/src/decorators/{after/after-reflector.spec.ts → before-action/before-action-reflector.spec.ts} +40 -23
- package/src/decorators/{after/after-reflector.ts → before-action/before-action-reflector.ts} +18 -9
- package/src/decorators/before-action/index.ts +3 -0
- package/src/decorators/index.ts +4 -4
- package/src/decorators/request-context/request-context-decorator.spec.ts +7 -0
- package/src/decorators/request-context/request-context-decorator.ts +4 -4
- package/src/decorators/request-data/request-data-decorator.spec.ts +45 -32
- package/src/decorators/request-data/request-data-decorator.ts +10 -10
- package/src/decorators/request-data/request-data-metadata.ts +1 -1
- package/src/decorators/rest-action/index.ts +3 -0
- package/src/decorators/rest-action/rest-action-decorator.spec.ts +325 -0
- package/src/decorators/rest-action/rest-action-decorator.ts +166 -0
- package/src/decorators/{action/action-metadata.ts → rest-action/rest-action-metadata.ts} +7 -7
- package/src/decorators/{action/action-reflector.spec.ts → rest-action/rest-action-reflector.spec.ts} +13 -13
- package/src/decorators/rest-action/rest-action-reflector.ts +41 -0
- package/src/decorators/rest-controller/index.ts +3 -0
- package/src/decorators/{controller/controller-decorator.spec.ts → rest-controller/rest-controller-decorator.spec.ts} +16 -16
- package/src/decorators/{controller/controller-decorator.ts → rest-controller/rest-controller-decorator.ts} +14 -10
- package/src/decorators/{controller/controller-metadata.ts → rest-controller/rest-controller-metadata.ts} +6 -7
- package/src/decorators/{controller/controller-reflector.spec.ts → rest-controller/rest-controller-reflector.spec.ts} +21 -12
- package/src/decorators/rest-controller/rest-controller-reflector.ts +32 -0
- package/src/errors/not-a-controller-error.ts +4 -1
- package/src/types.ts +0 -10
- package/src/utils/index.ts +0 -1
- package/README-ru.md +0 -268
- package/dist/esm/decorators/action/action-decorator.d.ts +0 -52
- package/dist/esm/decorators/action/action-decorator.js +0 -62
- package/dist/esm/decorators/action/action-decorator.spec.js +0 -59
- package/dist/esm/decorators/action/action-metadata.js +0 -5
- package/dist/esm/decorators/action/action-reflector.d.ts +0 -22
- package/dist/esm/decorators/action/index.d.ts +0 -3
- package/dist/esm/decorators/action/index.js +0 -3
- package/dist/esm/decorators/after/after-decorator.d.ts +0 -9
- package/dist/esm/decorators/after/after-metadata.js +0 -5
- package/dist/esm/decorators/after/index.d.ts +0 -3
- package/dist/esm/decorators/after/index.js +0 -3
- package/dist/esm/decorators/before/before-decorator.d.ts +0 -9
- package/dist/esm/decorators/before/before-metadata.js +0 -5
- package/dist/esm/decorators/before/index.d.ts +0 -3
- package/dist/esm/decorators/before/index.js +0 -3
- package/dist/esm/decorators/controller/controller-decorator.d.ts +0 -14
- package/dist/esm/decorators/controller/controller-metadata.js +0 -5
- package/dist/esm/decorators/controller/controller-reflector.d.ts +0 -20
- package/dist/esm/decorators/controller/controller-reflector.js +0 -24
- package/dist/esm/decorators/controller/index.d.ts +0 -3
- package/dist/esm/decorators/controller/index.js +0 -3
- package/dist/esm/utils/create-debugger.d.ts +0 -44
- package/dist/esm/utils/create-debugger.js +0 -81
- package/dist/esm/utils/create-debugger.spec.d.ts +0 -1
- package/dist/esm/utils/create-debugger.spec.js +0 -8
- package/src/decorators/action/action-decorator.spec.ts +0 -42
- package/src/decorators/action/action-decorator.ts +0 -98
- package/src/decorators/action/action-reflector.ts +0 -38
- package/src/decorators/action/index.ts +0 -3
- package/src/decorators/after/index.ts +0 -3
- package/src/decorators/before/before-metadata.ts +0 -17
- package/src/decorators/before/index.ts +0 -3
- package/src/decorators/controller/controller-reflector.ts +0 -28
- package/src/decorators/controller/index.ts +0 -3
- package/src/utils/create-debugger.spec.ts +0 -9
- package/src/utils/create-debugger.ts +0 -98
- /package/dist/esm/decorators/{action/action-decorator.spec.d.ts → after-action/after-action-decorator.spec.d.ts} +0 -0
- /package/dist/esm/decorators/{action/action-reflector.spec.d.ts → after-action/after-action-reflector.spec.d.ts} +0 -0
- /package/dist/esm/decorators/{after/after-decorator.spec.d.ts → before-action/before-action-decorator.spec.d.ts} +0 -0
- /package/dist/esm/decorators/{after/after-reflector.spec.d.ts → before-action/before-action-reflector.spec.d.ts} +0 -0
- /package/dist/esm/decorators/{before/before-decorator.spec.d.ts → rest-action/rest-action-decorator.spec.d.ts} +0 -0
- /package/dist/esm/decorators/{before/before-reflector.spec.d.ts → rest-action/rest-action-reflector.spec.d.ts} +0 -0
- /package/dist/esm/decorators/{controller/controller-decorator.spec.d.ts → rest-controller/rest-controller-decorator.spec.d.ts} +0 -0
- /package/dist/esm/decorators/{controller/controller-reflector.spec.d.ts → rest-controller/rest-controller-reflector.spec.d.ts} +0 -0
package/README.md
CHANGED
@@ -1,27 +1,25 @@
|
|
1
1
|
# @e22m4u/ts-rest-router
|
2
2
|
|
3
|
-
|
3
|
+
REST маршрутизатор на основе контроллеров для TypeScript.
|
4
4
|
|
5
|
-
|
5
|
+
#### Основные возможности
|
6
6
|
|
7
|
-
|
7
|
+
- Декларативное определение маршрутов через декораторы.
|
8
|
+
- Типизированные параметры запросов (body, query, params).
|
9
|
+
- Поддержка middleware до и после обработки запроса.
|
10
|
+
- Валидация входящих данных.
|
11
|
+
- Поддержка всех HTTP методов (GET, POST, PUT, PATCH и DELETE).
|
8
12
|
|
9
|
-
|
10
|
-
- Typed request parameters (body, query, params).
|
11
|
-
- Pre and post request middleware support.
|
12
|
-
- Input data validation.
|
13
|
-
- Support for all HTTP methods (GET, POST, PUT, DELETE, etc.).
|
14
|
-
|
15
|
-
## Installation
|
13
|
+
## Установка
|
16
14
|
|
17
15
|
```bash
|
18
16
|
npm install @e22m4u/ts-rest-router
|
19
17
|
```
|
20
18
|
|
21
|
-
####
|
19
|
+
#### Поддержка декораторов
|
22
20
|
|
23
|
-
|
24
|
-
|
21
|
+
Для включения поддержки декораторов, добавьте указанные
|
22
|
+
ниже опции в файл `tsconfig.json` вашего проекта.
|
25
23
|
|
26
24
|
```json
|
27
25
|
{
|
@@ -30,152 +28,154 @@ options to your `tsconfig.json` file.
|
|
30
28
|
}
|
31
29
|
```
|
32
30
|
|
33
|
-
##
|
31
|
+
## Базовое использование
|
34
32
|
|
35
|
-
|
33
|
+
Создание контроллера и методов.
|
36
34
|
|
37
35
|
```ts
|
38
|
-
import {get} from '@e22m4u/ts-rest-router';
|
39
|
-
import {post} from '@e22m4u/ts-rest-router';
|
40
|
-
import {field} from '@e22m4u/ts-rest-router';
|
41
36
|
import {DataType} from '@e22m4u/ts-rest-router';
|
42
|
-
import {
|
43
|
-
|
44
|
-
@
|
45
|
-
|
46
|
-
|
37
|
+
import {getAction} from '@e22m4u/ts-rest-router';
|
38
|
+
import {postAction} from '@e22m4u/ts-rest-router';
|
39
|
+
import {requestField} from '@e22m4u/ts-rest-router';
|
40
|
+
import {restController} from '@e22m4u/ts-rest-router';
|
41
|
+
|
42
|
+
@restController('/users') // путь контроллера
|
43
|
+
class UserController { // класс контроллера
|
44
|
+
@postAction('/login') // метод POST /users/login
|
47
45
|
async login(
|
48
|
-
@
|
49
|
-
type: DataType.STRING,
|
50
|
-
required: true,
|
46
|
+
@requestField('username', { // поле "username" в теле запроса
|
47
|
+
type: DataType.STRING, // тип параметра допускает только строки
|
48
|
+
required: true, // параметр является обязательным
|
51
49
|
})
|
52
50
|
username: string,
|
53
|
-
@
|
54
|
-
type: DataType.STRING,
|
55
|
-
required: true,
|
51
|
+
@requestField('password', { // поле "password" в теле запроса
|
52
|
+
type: DataType.STRING, // тип параметра допускает только строки
|
53
|
+
required: true, // параметр является обязательным
|
56
54
|
})
|
57
55
|
password: string,
|
58
56
|
) {
|
59
|
-
return {
|
60
|
-
id: '123',
|
61
|
-
firstName: 'John',
|
57
|
+
return { // если метод возвращает объект,
|
58
|
+
id: '123', // то результат будет представлен как
|
59
|
+
firstName: 'John', // "Content-Type: application/json"
|
62
60
|
lastName: 'Doe',
|
63
61
|
};
|
64
62
|
}
|
65
63
|
}
|
66
64
|
```
|
67
65
|
|
68
|
-
|
66
|
+
Регистрация контроллеров и запуск сервера.
|
69
67
|
|
70
68
|
```ts
|
71
69
|
import http from 'http';
|
72
70
|
import {RestRouter} from '@e22m4u/ts-rest-router';
|
73
71
|
|
74
|
-
//
|
72
|
+
// создание маршрутизатора и регистрация контроллеров
|
75
73
|
const router = new RestRouter();
|
76
74
|
router.registerController(UserController);
|
77
75
|
router.registerController(ProductController);
|
78
76
|
|
79
|
-
//
|
77
|
+
// создание сервера и регистрация обработчика запросов
|
80
78
|
const server = new http.Server();
|
81
79
|
server.on('request', router.requestListener);
|
82
80
|
|
83
|
-
//
|
81
|
+
// запуск сервера
|
84
82
|
server.listen('8080', '0.0.0.0', () => {
|
85
83
|
console.log(`Server is running on http://localhost:8080`);
|
86
84
|
});
|
87
85
|
```
|
88
86
|
|
89
|
-
##
|
87
|
+
## Декораторы
|
90
88
|
|
91
|
-
|
89
|
+
Контроллер и методы:
|
92
90
|
|
93
|
-
- `@
|
94
|
-
- `@
|
95
|
-
- `@
|
96
|
-
- `@
|
97
|
-
- `@
|
98
|
-
- `@
|
99
|
-
- `@
|
91
|
+
- `@restController` - определяет класс как контроллер;
|
92
|
+
- `@restAction` - базовый декоратор для методов;
|
93
|
+
- `@getAction` - метод GET;
|
94
|
+
- `@postAction` - метод POST;
|
95
|
+
- `@putAction` - метод PUT;
|
96
|
+
- `@patchAction` - метод PATCH;
|
97
|
+
- `@deleteAction` - метод DELETE;
|
100
98
|
|
101
|
-
|
99
|
+
Хуки запроса:
|
102
100
|
|
103
|
-
- `@
|
104
|
-
- `@
|
101
|
+
- `@beforeAction` - middleware перед обработкой запроса;
|
102
|
+
- `@afterAction` - middleware после обработки запроса;
|
105
103
|
|
106
|
-
|
104
|
+
Параметры запроса:
|
107
105
|
|
108
|
-
- `@
|
109
|
-
- `@
|
110
|
-
- `@
|
111
|
-
- `@
|
112
|
-
- `@
|
113
|
-
- `@
|
114
|
-
- `@
|
115
|
-
- `@
|
116
|
-
- `@
|
117
|
-
- `@
|
118
|
-
- `@requestContext` -
|
119
|
-
- `@requestData` -
|
106
|
+
- `@requestParam` - определенный URL параметр;
|
107
|
+
- `@requestParams` - все параметры URL как объект;
|
108
|
+
- `@requestQuery` - определенный query параметр;
|
109
|
+
- `@requestQueries` - все query параметры как объект;
|
110
|
+
- `@requestBody` - тело запроса;
|
111
|
+
- `@requestField` - поле в теле запроса;
|
112
|
+
- `@requestHeader` - определенный заголовок запроса;
|
113
|
+
- `@requestHeaders` - все заголовки запроса как объект;
|
114
|
+
- `@requestCookie` - определенный cookie запроса;
|
115
|
+
- `@requestCookies` - все cookies запроса как объект;
|
116
|
+
- `@requestContext` - доступ к контексту запроса;
|
117
|
+
- `@requestData` - доступ к данным запроса;
|
118
|
+
- `@httpRequest` - экземпляр `IncomingMessage`;
|
119
|
+
- `@httpResponse` - экземпляр `ServerResponse`;
|
120
120
|
|
121
|
-
#### `@
|
121
|
+
#### `@restController(options?: ControllerOptions)`
|
122
122
|
|
123
|
-
|
123
|
+
Определение контроллера.
|
124
124
|
|
125
125
|
```ts
|
126
|
-
@
|
126
|
+
@restController()
|
127
127
|
class UserController {
|
128
|
-
//
|
128
|
+
// методы контроллера
|
129
129
|
}
|
130
130
|
```
|
131
131
|
|
132
|
-
|
132
|
+
Определение пути контроллера.
|
133
133
|
|
134
134
|
```ts
|
135
|
-
@
|
135
|
+
@restController('/users') // путь контроллера
|
136
136
|
class UserController {
|
137
|
-
//
|
137
|
+
// методы контроллера
|
138
138
|
}
|
139
139
|
```
|
140
140
|
|
141
|
-
|
141
|
+
Дополнительные параметры декоратора.
|
142
142
|
|
143
143
|
```ts
|
144
|
-
@
|
145
|
-
path: '/api', //
|
146
|
-
before: [authMiddleware], // middleware
|
147
|
-
after: [loggerMiddleware], // middleware
|
144
|
+
@restController({
|
145
|
+
path: '/api', // путь контроллера
|
146
|
+
before: [authMiddleware], // middleware до обработки запроса
|
147
|
+
after: [loggerMiddleware], // middleware после обработки запроса
|
148
148
|
})
|
149
149
|
class UserController {
|
150
|
-
//
|
150
|
+
// методы контроллера
|
151
151
|
}
|
152
152
|
```
|
153
153
|
|
154
|
-
#### `@
|
154
|
+
#### `@getAction(path: string, options?: ActionOptions)`
|
155
155
|
|
156
|
-
|
156
|
+
Определение метода GET.
|
157
157
|
|
158
158
|
```ts
|
159
|
-
@
|
160
|
-
class UserController {
|
161
|
-
@
|
159
|
+
@restController('/users') // путь контроллера
|
160
|
+
class UserController { // класс контроллера
|
161
|
+
@getAction('/whoAmI') // маршрут GET /users/whoAmI
|
162
162
|
async whoAmI() {
|
163
|
-
return {
|
164
|
-
name: 'John',
|
165
|
-
surname: 'Doe',
|
163
|
+
return { // если метод возвращает объект,
|
164
|
+
name: 'John', // то результат будет представлен
|
165
|
+
surname: 'Doe', // как "Content-Type: application/json"
|
166
166
|
};
|
167
167
|
}
|
168
168
|
}
|
169
169
|
```
|
170
170
|
|
171
|
-
|
171
|
+
Дополнительные параметры декоратора.
|
172
172
|
|
173
173
|
```ts
|
174
|
-
@
|
175
|
-
class UserController { //
|
176
|
-
@
|
177
|
-
before: [authMiddleware], // middleware
|
178
|
-
after: [loggerMiddleware], // middleware
|
174
|
+
@restController('/users') // путь контроллера
|
175
|
+
class UserController { // класс контроллера
|
176
|
+
@getAction('/whoAmI', { // маршрут GET /users/whoAmI
|
177
|
+
before: [authMiddleware], // middleware до обработки запроса
|
178
|
+
after: [loggerMiddleware], // middleware после обработки запроса
|
179
179
|
})
|
180
180
|
async whoAmI() {
|
181
181
|
return {
|
@@ -188,17 +188,17 @@ class UserController { // controller class
|
|
188
188
|
|
189
189
|
#### `@requestContext(propertyName?: string)`
|
190
190
|
|
191
|
-
|
191
|
+
Доступ к контексту запроса.
|
192
192
|
|
193
193
|
```ts
|
194
194
|
import {RequestContext} from '@e22m4u/js-trie-router';
|
195
195
|
|
196
|
-
@
|
197
|
-
class UserController { //
|
198
|
-
@
|
196
|
+
@restController('/users') // путь контроллера
|
197
|
+
class UserController { // класс контроллера
|
198
|
+
@getAction('/:id') // маршрут GET /users/:id
|
199
199
|
findById(
|
200
|
-
@requestContext() //
|
201
|
-
ctx: RequestContext, //
|
200
|
+
@requestContext() // включениее контекста запроса
|
201
|
+
ctx: RequestContext, // в качестве параметра метода
|
202
202
|
) {
|
203
203
|
console.log(ctx.req); // IncomingMessage
|
204
204
|
console.log(ctx.res); // ServerResponse
|
@@ -214,20 +214,20 @@ class UserController { // controller class
|
|
214
214
|
}
|
215
215
|
```
|
216
216
|
|
217
|
-
|
217
|
+
Доступ к свойствам контекста.
|
218
218
|
|
219
219
|
```ts
|
220
220
|
import {ServerResponse} from 'http';
|
221
221
|
import {IncomingMessage} from 'http';
|
222
222
|
|
223
|
-
@
|
224
|
-
class UserController { //
|
225
|
-
@
|
223
|
+
@restController('/users') // путь контроллера
|
224
|
+
class UserController { // класс контроллера
|
225
|
+
@getAction('/:id') // маршрут GET /users/:id
|
226
226
|
findById(
|
227
|
-
@requestContext('req') //
|
228
|
-
req: IncomingMessage, //
|
229
|
-
@requestContext('res') //
|
230
|
-
res: ServerResponse, //
|
227
|
+
@requestContext('req') // декоратор контекста запроса
|
228
|
+
req: IncomingMessage, // включающий свойство "req"
|
229
|
+
@requestContext('res') // декоратор контекста запроса
|
230
|
+
res: ServerResponse, // включающий свойство "res"
|
231
231
|
) {
|
232
232
|
console.log(req); // IncomingMessage
|
233
233
|
console.log(res); // ServerResponse
|
@@ -235,34 +235,34 @@ class UserController { // controller class
|
|
235
235
|
}
|
236
236
|
```
|
237
237
|
|
238
|
-
|
238
|
+
Свойства контекста:
|
239
239
|
|
240
|
-
- `container: ServiceContainer`
|
241
|
-
- `req: IncomingMessage`
|
242
|
-
- `res: ServerResponse`
|
243
|
-
- `params: ParsedParams`
|
244
|
-
- `query: ParsedQuery`
|
245
|
-
- `headers: ParsedHeaders`
|
246
|
-
- `cookie: ParsedCookie`
|
247
|
-
- `method: string`
|
248
|
-
- `path: string`
|
249
|
-
- `pathname: string`
|
250
|
-
- `body: unknown`
|
240
|
+
- `container: ServiceContainer` экземпляр [сервис-контейнера](https://npmjs.com/package/@e22m4u/js-service)
|
241
|
+
- `req: IncomingMessage` нативный поток входящего запроса
|
242
|
+
- `res: ServerResponse` нативный поток ответа сервера
|
243
|
+
- `params: ParsedParams` объект ключ-значение с параметрами пути
|
244
|
+
- `query: ParsedQuery` объект ключ-значение с параметрами строки запроса
|
245
|
+
- `headers: ParsedHeaders` объект ключ-значение с заголовками запроса
|
246
|
+
- `cookie: ParsedCookie` объект ключ-значение разобранного заголовка `cookie`
|
247
|
+
- `method: string` метод запроса в верхнем регистре, например `GET`, `POST` и т.д.
|
248
|
+
- `path: string` путь включающий строку запроса, например `/myPath?foo=bar`
|
249
|
+
- `pathname: string` путь запроса, например `/myMath`
|
250
|
+
- `body: unknown` тело запроса
|
251
251
|
|
252
|
-
##
|
252
|
+
## Отладка
|
253
253
|
|
254
|
-
|
254
|
+
Установка переменной `DEBUG` включает вывод логов.
|
255
255
|
|
256
256
|
```bash
|
257
257
|
DEBUG=tsRestRouter* npm run test
|
258
258
|
```
|
259
259
|
|
260
|
-
##
|
260
|
+
## Тесты
|
261
261
|
|
262
262
|
```bash
|
263
263
|
npm run test
|
264
264
|
```
|
265
265
|
|
266
|
-
##
|
266
|
+
## Лицензия
|
267
267
|
|
268
268
|
MIT
|