@e22m4u/ts-rest-router 0.0.6 → 0.1.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-ru.md +227 -0
- package/README.md +227 -0
- package/dist/cjs/index.cjs +448 -153
- package/dist/esm/controller-registry.d.ts +53 -11
- package/dist/esm/controller-registry.js +242 -104
- package/dist/esm/debuggable-service.js +1 -1
- package/dist/esm/decorators/action/action-decorator.d.ts +7 -8
- package/dist/esm/decorators/action/action-decorator.js +1 -5
- package/dist/esm/decorators/action/action-decorator.spec.js +16 -16
- package/dist/esm/decorators/action/action-metadata.d.ts +0 -1
- package/dist/esm/decorators/after/after-decorator.d.ts +9 -0
- package/dist/esm/decorators/after/after-decorator.js +22 -0
- package/dist/esm/decorators/after/after-decorator.spec.d.ts +1 -0
- package/dist/esm/decorators/after/after-decorator.spec.js +115 -0
- package/dist/esm/decorators/after/after-metadata.d.ts +13 -0
- package/dist/esm/decorators/after/after-metadata.js +5 -0
- package/dist/esm/decorators/after/after-reflector.d.ts +22 -0
- package/dist/esm/decorators/after/after-reflector.js +29 -0
- package/dist/esm/decorators/after/after-reflector.spec.d.ts +1 -0
- package/dist/esm/decorators/after/after-reflector.spec.js +102 -0
- package/dist/esm/decorators/after/index.d.ts +3 -0
- package/dist/esm/decorators/after/index.js +3 -0
- package/dist/esm/decorators/before/before-decorator.d.ts +9 -0
- package/dist/esm/decorators/before/before-decorator.js +22 -0
- package/dist/esm/decorators/before/before-decorator.spec.d.ts +1 -0
- package/dist/esm/decorators/before/before-decorator.spec.js +115 -0
- package/dist/esm/decorators/before/before-metadata.d.ts +13 -0
- package/dist/esm/decorators/before/before-metadata.js +5 -0
- package/dist/esm/decorators/before/before-reflector.d.ts +22 -0
- package/dist/esm/decorators/before/before-reflector.js +29 -0
- package/dist/esm/decorators/before/before-reflector.spec.d.ts +1 -0
- package/dist/esm/decorators/before/before-reflector.spec.js +102 -0
- package/dist/esm/decorators/before/index.d.ts +3 -0
- package/dist/esm/decorators/before/index.js +3 -0
- package/dist/esm/decorators/controller/controller-decorator.d.ts +2 -1
- package/dist/esm/decorators/controller/controller-decorator.js +27 -6
- package/dist/esm/decorators/controller/controller-decorator.spec.js +37 -15
- package/dist/esm/decorators/controller/controller-metadata.d.ts +0 -1
- package/dist/esm/decorators/index.d.ts +2 -0
- package/dist/esm/decorators/index.js +2 -0
- package/dist/esm/decorators/request-context/request-context-decorator.d.ts +2 -3
- package/dist/esm/decorators/request-context/request-context-decorator.js +3 -6
- package/dist/esm/decorators/request-context/request-context-decorator.spec.js +2 -17
- package/dist/esm/decorators/request-context/request-context-metadata.d.ts +0 -1
- package/dist/esm/decorators/request-data/request-data-decorator.d.ts +7 -3
- package/dist/esm/decorators/request-data/request-data-decorator.js +16 -16
- package/dist/esm/decorators/request-data/request-data-decorator.spec.js +12 -10
- package/dist/esm/decorators/request-data/request-data-metadata.d.ts +0 -1
- package/dist/esm/utils/create-debugger.d.ts +35 -2
- package/dist/esm/utils/create-debugger.js +71 -5
- package/package.json +17 -17
- package/src/controller-registry.spec.ts +601 -275
- package/src/controller-registry.ts +263 -128
- package/src/debuggable-service.ts +1 -1
- package/src/decorators/action/action-decorator.spec.ts +16 -16
- package/src/decorators/action/action-decorator.ts +10 -12
- package/src/decorators/action/action-metadata.ts +0 -1
- package/src/decorators/after/after-decorator.spec.ts +92 -0
- package/src/decorators/after/after-decorator.ts +40 -0
- package/src/decorators/after/after-metadata.ts +17 -0
- package/src/decorators/after/after-reflector.spec.ts +107 -0
- package/src/decorators/after/after-reflector.ts +45 -0
- package/src/decorators/after/index.ts +3 -0
- package/src/decorators/before/before-decorator.spec.ts +92 -0
- package/src/decorators/before/before-decorator.ts +40 -0
- package/src/decorators/before/before-metadata.ts +17 -0
- package/src/decorators/before/before-reflector.spec.ts +111 -0
- package/src/decorators/before/before-reflector.ts +50 -0
- package/src/decorators/before/index.ts +3 -0
- package/src/decorators/controller/controller-decorator.spec.ts +33 -16
- package/src/decorators/controller/controller-decorator.ts +33 -6
- package/src/decorators/controller/controller-metadata.ts +0 -1
- package/src/decorators/index.ts +2 -0
- package/src/decorators/request-context/request-context-decorator.spec.ts +2 -15
- package/src/decorators/request-context/request-context-decorator.ts +3 -8
- package/src/decorators/request-context/request-context-metadata.ts +0 -1
- package/src/decorators/request-data/request-data-decorator.spec.ts +12 -11
- package/src/decorators/request-data/request-data-decorator.ts +41 -16
- package/src/decorators/request-data/request-data-metadata.ts +0 -1
- package/src/utils/create-debugger.ts +84 -7
package/README-ru.md
CHANGED
@@ -4,6 +4,14 @@
|
|
4
4
|
|
5
5
|
Реализация REST маршрутизатора на основе контроллеров для TypeScript.
|
6
6
|
|
7
|
+
#### Основные возможности
|
8
|
+
|
9
|
+
- Декларативное определение маршрутов через декораторы.
|
10
|
+
- Типизированные параметры запросов (body, query, params).
|
11
|
+
- Поддержка middleware до и после обработки запроса.
|
12
|
+
- Валидация входящих данных.
|
13
|
+
- Поддержка всех HTTP методов (GET, POST, PUT, DELETE и т.д.).
|
14
|
+
|
7
15
|
## Установка
|
8
16
|
|
9
17
|
```bash
|
@@ -22,6 +30,225 @@ npm install @e22m4u/ts-rest-router
|
|
22
30
|
}
|
23
31
|
```
|
24
32
|
|
33
|
+
## Базовое использование
|
34
|
+
|
35
|
+
Создание контроллера и методов
|
36
|
+
|
37
|
+
```ts
|
38
|
+
import {get} from '@e22m4u/ts-rest-router';
|
39
|
+
import {post} from '@e22m4u/ts-rest-router';
|
40
|
+
import {bodyProp} from '@e22m4u/ts-rest-router';
|
41
|
+
import {DataType} from '@e22m4u/ts-rest-router';
|
42
|
+
import {controller} from '@e22m4u/ts-rest-router';
|
43
|
+
|
44
|
+
@controller('/users') // путь контроллера
|
45
|
+
class UserController { // класс контроллера
|
46
|
+
@post('/login') // метод POST /users/login
|
47
|
+
async login(
|
48
|
+
@bodyProp('username', { // свойство тела запроса "username"
|
49
|
+
type: DataType.STRING, // тип параметра допускает только строки
|
50
|
+
required: true, // параметр является обязательным
|
51
|
+
})
|
52
|
+
username: string,
|
53
|
+
@bodyProp('password', { // свойство тела запроса "password"
|
54
|
+
type: DataType.STRING, // тип параметра допускает только строки
|
55
|
+
required: true, // параметр является обязательным
|
56
|
+
})
|
57
|
+
password: string,
|
58
|
+
) {
|
59
|
+
return { // если метод возвращает объект,
|
60
|
+
id: '123', // то результат будет представлен как
|
61
|
+
firstName: 'John', // "Content-Type: application/json"
|
62
|
+
lastName: 'Doe',
|
63
|
+
};
|
64
|
+
}
|
65
|
+
}
|
66
|
+
```
|
67
|
+
|
68
|
+
Регистрация контроллеров и запуск сервера
|
69
|
+
|
70
|
+
```ts
|
71
|
+
import http from 'http';
|
72
|
+
import {RestRouter} from '@e22m4u/ts-rest-router';
|
73
|
+
|
74
|
+
// создание роутера и регистрация контроллеров
|
75
|
+
const router = new RestRouter();
|
76
|
+
router.registerController(UserController);
|
77
|
+
router.registerController(ProductController);
|
78
|
+
|
79
|
+
// создание сервера и регистрация обработчика запросов
|
80
|
+
const server = new http.Server();
|
81
|
+
server.on('request', router.requestListener);
|
82
|
+
|
83
|
+
// запуск сервера
|
84
|
+
server.listen('8080', '0.0.0.0', () => {
|
85
|
+
console.log(`Server is running on http://localhost:8080`);
|
86
|
+
});
|
87
|
+
```
|
88
|
+
|
89
|
+
## Декораторы
|
90
|
+
|
91
|
+
Контроллер и методы:
|
92
|
+
|
93
|
+
- `@controller` - определяет класс как контроллер
|
94
|
+
- `@action` - базовый декоратор для методов
|
95
|
+
- `@get` - GET запросы
|
96
|
+
- `@post` - POST запросы
|
97
|
+
- `@put` - PUT запросы
|
98
|
+
- `@patch` - PATCH запросы
|
99
|
+
- `@del` - DELETE запросы
|
100
|
+
|
101
|
+
Хуки запроса:
|
102
|
+
|
103
|
+
- `@before` - middleware перед обработкой запроса
|
104
|
+
- `@after` - middleware после обработки запроса
|
105
|
+
|
106
|
+
Параметры запроса:
|
107
|
+
|
108
|
+
- `@param` - один параметр URL
|
109
|
+
- `@params` - все параметры URL как объект
|
110
|
+
- `@query` - один query параметр
|
111
|
+
- `@queries` - все query параметры как объект
|
112
|
+
- `@body` - тело запроса
|
113
|
+
- `@bodyProp` - свойство из тела запроса
|
114
|
+
- `@header` - один заголовок
|
115
|
+
- `@headers` - все заголовки как объект
|
116
|
+
- `@cookie` - одна cookie
|
117
|
+
- `@cookies` - все cookies как объект
|
118
|
+
- `@requestContext` - доступ к контексту запроса
|
119
|
+
- `@requestData` - универсальный декоратор для доступа к данным запроса
|
120
|
+
|
121
|
+
#### `@controller(options?: ControllerOptions)`
|
122
|
+
|
123
|
+
Определение контроллера.
|
124
|
+
|
125
|
+
```ts
|
126
|
+
@controller()
|
127
|
+
class UserController {
|
128
|
+
// методы контроллера
|
129
|
+
}
|
130
|
+
```
|
131
|
+
|
132
|
+
Определение пути контроллера.
|
133
|
+
|
134
|
+
```ts
|
135
|
+
@controller('/users') // путь контроллера
|
136
|
+
class UserController {
|
137
|
+
// методы контроллера
|
138
|
+
}
|
139
|
+
```
|
140
|
+
|
141
|
+
Дополнительные параметры декоратора.
|
142
|
+
|
143
|
+
```ts
|
144
|
+
@controller({
|
145
|
+
path: '/api', // путь контроллера
|
146
|
+
before: [authMiddleware], // middleware до обработки запроса
|
147
|
+
after: [loggerMiddleware], // middleware после обработки запроса
|
148
|
+
})
|
149
|
+
class UserController {
|
150
|
+
// методы контроллера
|
151
|
+
}
|
152
|
+
```
|
153
|
+
|
154
|
+
#### `@get(path: string, options?: ActionOptions)`
|
155
|
+
|
156
|
+
Определение метода GET.
|
157
|
+
|
158
|
+
```ts
|
159
|
+
@controller('/users') // путь контроллера
|
160
|
+
class UserController { // класс контроллера
|
161
|
+
@get('/whoAmI') // маршрут GET /users/whoAmI
|
162
|
+
async whoAmI() {
|
163
|
+
return { // если метод возвращает объект,
|
164
|
+
name: 'John', // то результат будет представлен
|
165
|
+
surname: 'Doe', // как "Content-Type: application/json"
|
166
|
+
};
|
167
|
+
}
|
168
|
+
}
|
169
|
+
```
|
170
|
+
|
171
|
+
Дополнительные параметры декоратора.
|
172
|
+
|
173
|
+
```ts
|
174
|
+
@controller('/users') // путь контроллера
|
175
|
+
class UserController { // класс контроллера
|
176
|
+
@get('/whoAmI', { // маршрут GET /users/whoAmI
|
177
|
+
before: [authMiddleware], // middleware до обработки запроса
|
178
|
+
after: [loggerMiddleware], // middleware после обработки запроса
|
179
|
+
})
|
180
|
+
async whoAmI() {
|
181
|
+
return {
|
182
|
+
name: 'John',
|
183
|
+
surname: 'Doe',
|
184
|
+
};
|
185
|
+
}
|
186
|
+
}
|
187
|
+
```
|
188
|
+
|
189
|
+
#### `@requestContext(propertyName?: string)`
|
190
|
+
|
191
|
+
Доступ к контексту запроса.
|
192
|
+
|
193
|
+
```ts
|
194
|
+
import {RequestContext} from '@e22m4u/js-trie-router';
|
195
|
+
|
196
|
+
@controller('/users') // путь контроллера
|
197
|
+
class UserController { // класс контроллера
|
198
|
+
@get('/:id') // маршрут GET /users/:id
|
199
|
+
findById(
|
200
|
+
@requestContext() // включениее контекста запроса
|
201
|
+
ctx: RequestContext, // в качестве параметра метода
|
202
|
+
) {
|
203
|
+
console.log(ctx.req); // IncomingMessage
|
204
|
+
console.log(ctx.res); // ServerResponse
|
205
|
+
console.log(ctx.params); // {id: 10}
|
206
|
+
console.log(ctx.query); // {include: 'city'}
|
207
|
+
console.log(ctx.headers); // {cookie: 'foo=bar; baz=qux;'}
|
208
|
+
console.log(ctx.cookie); // {foo: 'bar', baz: 'qux'}
|
209
|
+
console.log(ctx.method); // "GET"
|
210
|
+
console.log(ctx.path); // "/users/10?include=city"
|
211
|
+
console.log(ctx.pathname); // "/users/10"
|
212
|
+
// ...
|
213
|
+
}
|
214
|
+
}
|
215
|
+
```
|
216
|
+
|
217
|
+
Доступ к свойствам контекста.
|
218
|
+
|
219
|
+
```ts
|
220
|
+
import {ServerResponse} from 'http';
|
221
|
+
import {IncomingMessage} from 'http';
|
222
|
+
|
223
|
+
@controller('/users') // путь контроллера
|
224
|
+
class UserController { // класс контроллера
|
225
|
+
@get('/:id') // маршрут GET /users/:id
|
226
|
+
findById(
|
227
|
+
@requestContext('req') // декоратор контекста запроса
|
228
|
+
req: IncomingMessage, // включающий свойство "req"
|
229
|
+
@requestContext('res') // декоратор контекста запроса
|
230
|
+
res: ServerResponse, // включающий свойство "res"
|
231
|
+
) {
|
232
|
+
console.log(req); // IncomingMessage
|
233
|
+
console.log(res); // ServerResponse
|
234
|
+
}
|
235
|
+
}
|
236
|
+
```
|
237
|
+
|
238
|
+
Свойства контекста:
|
239
|
+
|
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
|
+
|
25
252
|
## Отладка
|
26
253
|
|
27
254
|
Установка переменной `DEBUG` включает вывод логов.
|
package/README.md
CHANGED
@@ -4,6 +4,14 @@
|
|
4
4
|
|
5
5
|
Controllers-based REST router implementation for TypeScript.
|
6
6
|
|
7
|
+
#### Key Features
|
8
|
+
|
9
|
+
- Declarative route definition using decorators.
|
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
|
+
|
7
15
|
## Installation
|
8
16
|
|
9
17
|
```bash
|
@@ -22,6 +30,225 @@ options to your `tsconfig.json` file.
|
|
22
30
|
}
|
23
31
|
```
|
24
32
|
|
33
|
+
## Basic Usage
|
34
|
+
|
35
|
+
Creating a controller and methods.
|
36
|
+
|
37
|
+
```ts
|
38
|
+
import {get} from '@e22m4u/ts-rest-router';
|
39
|
+
import {post} from '@e22m4u/ts-rest-router';
|
40
|
+
import {bodyProp} from '@e22m4u/ts-rest-router';
|
41
|
+
import {DataType} from '@e22m4u/ts-rest-router';
|
42
|
+
import {controller} from '@e22m4u/ts-rest-router';
|
43
|
+
|
44
|
+
@controller('/users') // controller path
|
45
|
+
class UserController { // controller class
|
46
|
+
@post('/login') // POST /users/login method
|
47
|
+
async login(
|
48
|
+
@bodyProp('username', { // "username" is request body property
|
49
|
+
type: DataType.STRING, // parameter type allows only strings
|
50
|
+
required: true, // parameter is required
|
51
|
+
})
|
52
|
+
username: string,
|
53
|
+
@bodyProp('password', { // "password" is request body property
|
54
|
+
type: DataType.STRING, // parameter type allows only strings
|
55
|
+
required: true, // parameter is required
|
56
|
+
})
|
57
|
+
password: string,
|
58
|
+
) {
|
59
|
+
return { // if method returns an object,
|
60
|
+
id: '123', // the result will be presented as
|
61
|
+
firstName: 'John', // "Content-Type: application/json"
|
62
|
+
lastName: 'Doe',
|
63
|
+
};
|
64
|
+
}
|
65
|
+
}
|
66
|
+
```
|
67
|
+
|
68
|
+
Registering controllers and starting the server.
|
69
|
+
|
70
|
+
```ts
|
71
|
+
import http from 'http';
|
72
|
+
import {RestRouter} from '@e22m4u/ts-rest-router';
|
73
|
+
|
74
|
+
// create router and register controllers
|
75
|
+
const router = new RestRouter();
|
76
|
+
router.registerController(UserController);
|
77
|
+
router.registerController(ProductController);
|
78
|
+
|
79
|
+
// create server and register request handler
|
80
|
+
const server = new http.Server();
|
81
|
+
server.on('request', router.requestListener);
|
82
|
+
|
83
|
+
// start server
|
84
|
+
server.listen('8080', '0.0.0.0', () => {
|
85
|
+
console.log(`Server is running on http://localhost:8080`);
|
86
|
+
});
|
87
|
+
```
|
88
|
+
|
89
|
+
## Decorators
|
90
|
+
|
91
|
+
Controller and methods:
|
92
|
+
|
93
|
+
- `@controller` - defines a class as a controller
|
94
|
+
- `@action` - base decorator for methods
|
95
|
+
- `@get` - GET requests
|
96
|
+
- `@post` - POST requests
|
97
|
+
- `@put` - PUT requests
|
98
|
+
- `@patch` - PATCH requests
|
99
|
+
- `@del` - DELETE requests
|
100
|
+
|
101
|
+
Request hooks:
|
102
|
+
|
103
|
+
- `@before` - middleware before request handling
|
104
|
+
- `@after` - middleware after request handling
|
105
|
+
|
106
|
+
Request parameters:
|
107
|
+
|
108
|
+
- `@param` - single URL parameter
|
109
|
+
- `@params` - all URL parameters as an object
|
110
|
+
- `@query` - single query parameter
|
111
|
+
- `@queries` - all query parameters as an object
|
112
|
+
- `@body` - request body
|
113
|
+
- `@bodyProp` - property from request body
|
114
|
+
- `@header` - single header
|
115
|
+
- `@headers` - all headers as an object
|
116
|
+
- `@cookie` - single cookie
|
117
|
+
- `@cookies` - all cookies as an object
|
118
|
+
- `@requestContext` - access to request context
|
119
|
+
- `@requestData` - universal decorator for accessing request data
|
120
|
+
|
121
|
+
#### `@controller(options?: ControllerOptions)`
|
122
|
+
|
123
|
+
Defining a controller.
|
124
|
+
|
125
|
+
```ts
|
126
|
+
@controller()
|
127
|
+
class UserController {
|
128
|
+
// controller methods
|
129
|
+
}
|
130
|
+
```
|
131
|
+
|
132
|
+
Defining controller path.
|
133
|
+
|
134
|
+
```ts
|
135
|
+
@controller('/users') // controller path
|
136
|
+
class UserController {
|
137
|
+
// controller methods
|
138
|
+
}
|
139
|
+
```
|
140
|
+
|
141
|
+
Additional decorator parameters.
|
142
|
+
|
143
|
+
```ts
|
144
|
+
@controller({
|
145
|
+
path: '/api', // controller path
|
146
|
+
before: [authMiddleware], // middleware before request processing
|
147
|
+
after: [loggerMiddleware], // middleware after request processing
|
148
|
+
})
|
149
|
+
class UserController {
|
150
|
+
// controller methods
|
151
|
+
}
|
152
|
+
```
|
153
|
+
|
154
|
+
#### `@get(path: string, options?: ActionOptions)`
|
155
|
+
|
156
|
+
Defining GET method.
|
157
|
+
|
158
|
+
```ts
|
159
|
+
@controller('/users') // controller path
|
160
|
+
class UserController { // controller class
|
161
|
+
@get('/whoAmI') // GET /users/whoAmI route
|
162
|
+
async whoAmI() {
|
163
|
+
return { // if method returns an object,
|
164
|
+
name: 'John', // the result will be presented
|
165
|
+
surname: 'Doe', // as "Content-Type: application/json"
|
166
|
+
};
|
167
|
+
}
|
168
|
+
}
|
169
|
+
```
|
170
|
+
|
171
|
+
Additional decorator parameters.
|
172
|
+
|
173
|
+
```ts
|
174
|
+
@controller('/users') // controller path
|
175
|
+
class UserController { // controller class
|
176
|
+
@get('/whoAmI', { // GET /users/whoAmI route
|
177
|
+
before: [authMiddleware], // middleware before request processing
|
178
|
+
after: [loggerMiddleware], // middleware after request processing
|
179
|
+
})
|
180
|
+
async whoAmI() {
|
181
|
+
return {
|
182
|
+
name: 'John',
|
183
|
+
surname: 'Doe',
|
184
|
+
};
|
185
|
+
}
|
186
|
+
}
|
187
|
+
```
|
188
|
+
|
189
|
+
#### `@requestContext(propertyName?: string)`
|
190
|
+
|
191
|
+
Access to request context.
|
192
|
+
|
193
|
+
```ts
|
194
|
+
import {RequestContext} from '@e22m4u/js-trie-router';
|
195
|
+
|
196
|
+
@controller('/users') // controller path
|
197
|
+
class UserController { // controller class
|
198
|
+
@get('/:id') // GET /users/:id route
|
199
|
+
findById(
|
200
|
+
@requestContext() // including request context
|
201
|
+
ctx: RequestContext, // as method parameter
|
202
|
+
) {
|
203
|
+
console.log(ctx.req); // IncomingMessage
|
204
|
+
console.log(ctx.res); // ServerResponse
|
205
|
+
console.log(ctx.params); // {id: 10}
|
206
|
+
console.log(ctx.query); // {include: 'city'}
|
207
|
+
console.log(ctx.headers); // {cookie: 'foo=bar; baz=qux;'}
|
208
|
+
console.log(ctx.cookie); // {foo: 'bar', baz: 'qux'}
|
209
|
+
console.log(ctx.method); // "GET"
|
210
|
+
console.log(ctx.path); // "/users/10?include=city"
|
211
|
+
console.log(ctx.pathname); // "/users/10"
|
212
|
+
// ...
|
213
|
+
}
|
214
|
+
}
|
215
|
+
```
|
216
|
+
|
217
|
+
Access to context properties.
|
218
|
+
|
219
|
+
```ts
|
220
|
+
import {ServerResponse} from 'http';
|
221
|
+
import {IncomingMessage} from 'http';
|
222
|
+
|
223
|
+
@controller('/users') // controller path
|
224
|
+
class UserController { // controller class
|
225
|
+
@get('/:id') // GET /users/:id route
|
226
|
+
findById(
|
227
|
+
@requestContext('req') // request context decorator
|
228
|
+
req: IncomingMessage, // including "req" property
|
229
|
+
@requestContext('res') // request context decorator
|
230
|
+
res: ServerResponse, // including "res" property
|
231
|
+
) {
|
232
|
+
console.log(req); // IncomingMessage
|
233
|
+
console.log(res); // ServerResponse
|
234
|
+
}
|
235
|
+
}
|
236
|
+
```
|
237
|
+
|
238
|
+
Context properties:
|
239
|
+
|
240
|
+
- `container: ServiceContainer` instance of [service container](https://npmjs.com/package/@e22m4u/js-service)
|
241
|
+
- `req: IncomingMessage` native incoming request stream
|
242
|
+
- `res: ServerResponse` native server response stream
|
243
|
+
- `params: ParsedParams` key-value object with path parameters
|
244
|
+
- `query: ParsedQuery` key-value object with query string parameters
|
245
|
+
- `headers: ParsedHeaders` key-value object with request headers
|
246
|
+
- `cookie: ParsedCookie` key-value object of parsed `cookie` header
|
247
|
+
- `method: string` request method in uppercase, e.g. `GET`, `POST`, etc.
|
248
|
+
- `path: string` path including query string, e.g. `/myPath?foo=bar`
|
249
|
+
- `pathname: string` request path, e.g. `/myMath`
|
250
|
+
- `body: unknown` request body
|
251
|
+
|
25
252
|
## Debugging
|
26
253
|
|
27
254
|
Set the `DEBUG` variable to enable log output.
|