@e22m4u/ts-rest-router 0.6.6 → 0.6.7
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 +20 -22
- package/dist/cjs/index.cjs +4 -1
- package/dist/esm/index.d.ts +1 -0
- package/dist/esm/index.js +1 -0
- package/package.json +1 -1
- package/src/index.ts +1 -0
package/README.md
CHANGED
|
@@ -29,7 +29,7 @@
|
|
|
29
29
|
- **Производительность и гибкая архитектура**
|
|
30
30
|
Основан на
|
|
31
31
|
[@e22m4u/js-trie-router](https://www.npmjs.com/package/@e22m4u/js-trie-router)
|
|
32
|
-
для маршрутизации на базе
|
|
32
|
+
для маршрутизации на базе _префиксного дерева_ и
|
|
33
33
|
[@e22m4u/js-service](https://www.npmjs.com/package/@e22m4u/js-service)
|
|
34
34
|
для внедрения зависимостей.
|
|
35
35
|
|
|
@@ -86,9 +86,7 @@ import {
|
|
|
86
86
|
} from '@e22m4u/ts-rest-router';
|
|
87
87
|
|
|
88
88
|
// Временное хранилище данных
|
|
89
|
-
const users = [
|
|
90
|
-
{id: 1, name: 'John Doe'},
|
|
91
|
-
];
|
|
89
|
+
const users = [{id: 1, name: 'John Doe'}];
|
|
92
90
|
|
|
93
91
|
// 1. Декоратор @restController определяет класс как контроллер
|
|
94
92
|
// и устанавливает базовый путь для всех его маршрутов.
|
|
@@ -113,7 +111,7 @@ export class UserController {
|
|
|
113
111
|
properties: {
|
|
114
112
|
name: {
|
|
115
113
|
type: DataType.STRING,
|
|
116
|
-
required: true
|
|
114
|
+
required: true,
|
|
117
115
|
},
|
|
118
116
|
},
|
|
119
117
|
})
|
|
@@ -148,7 +146,7 @@ async function bootstrap() {
|
|
|
148
146
|
console.log('Server is running on http://localhost:3000');
|
|
149
147
|
console.log('Try GET http://localhost:3000/users');
|
|
150
148
|
console.log(
|
|
151
|
-
'Try POST http://localhost:3000/users with body {"name": "Jane Doe"}'
|
|
149
|
+
'Try POST http://localhost:3000/users with body {"name": "Jane Doe"}',
|
|
152
150
|
);
|
|
153
151
|
});
|
|
154
152
|
}
|
|
@@ -265,7 +263,10 @@ class UserController {
|
|
|
265
263
|
},
|
|
266
264
|
},
|
|
267
265
|
})
|
|
268
|
-
body: {
|
|
266
|
+
body: {
|
|
267
|
+
username: string;
|
|
268
|
+
email: string;
|
|
269
|
+
},
|
|
269
270
|
) {
|
|
270
271
|
return {id: 1, ...body};
|
|
271
272
|
}
|
|
@@ -349,7 +350,7 @@ type DataSchema = {
|
|
|
349
350
|
items?: DataSchema; // Схема для элементов массива (для type: DataType.ARRAY)
|
|
350
351
|
properties?: {[key: string]: DataSchema}; // Схема для свойств объекта
|
|
351
352
|
validate?: (value: any) => boolean | string; // Пользовательская функция
|
|
352
|
-
}
|
|
353
|
+
};
|
|
353
354
|
```
|
|
354
355
|
|
|
355
356
|
**Пример сложной валидации:**
|
|
@@ -377,20 +378,22 @@ class OrderController {
|
|
|
377
378
|
properties: {
|
|
378
379
|
id: {
|
|
379
380
|
type: DataType.NUMBER,
|
|
380
|
-
required: true
|
|
381
|
+
required: true,
|
|
381
382
|
},
|
|
382
383
|
quantity: {
|
|
383
384
|
type: DataType.NUMBER,
|
|
384
385
|
required: true,
|
|
385
386
|
// Валидатор: количество должно быть больше 0
|
|
386
|
-
validate:
|
|
387
|
+
validate: qty => qty > 0 || 'Quantity must be positive',
|
|
387
388
|
},
|
|
388
389
|
},
|
|
389
390
|
},
|
|
390
391
|
},
|
|
391
392
|
},
|
|
392
393
|
})
|
|
393
|
-
orderData: {
|
|
394
|
+
orderData: {
|
|
395
|
+
/* ... */
|
|
396
|
+
},
|
|
394
397
|
) {
|
|
395
398
|
// ...
|
|
396
399
|
}
|
|
@@ -505,12 +508,12 @@ export async function authHook(context: RequestContext) {
|
|
|
505
508
|
import {Service} from '@e22m4u/js-service';
|
|
506
509
|
|
|
507
510
|
export class AuthService extends Service {
|
|
508
|
-
public currentUser?: {id: number; name: string
|
|
511
|
+
public currentUser?: {id: number; name: string};
|
|
509
512
|
|
|
510
513
|
async authenticate(token?: string) {
|
|
511
514
|
// Логика проверки токена и поиска пользователя в БД...
|
|
512
515
|
if (token === 'valid-token') {
|
|
513
|
-
this.currentUser = {
|
|
516
|
+
this.currentUser = {id: 1, name: 'John Doe'};
|
|
514
517
|
}
|
|
515
518
|
}
|
|
516
519
|
}
|
|
@@ -528,11 +531,7 @@ import {authHook} from './auth.hook';
|
|
|
528
531
|
import createError from 'http-errors';
|
|
529
532
|
import {Service} from '@e22m4u/js-service';
|
|
530
533
|
import {AuthService} from './auth.service';
|
|
531
|
-
import {
|
|
532
|
-
getAction,
|
|
533
|
-
restController,
|
|
534
|
-
beforeAction,
|
|
535
|
-
} from '@e22m4u/ts-rest-router';
|
|
534
|
+
import {getAction, restController, beforeAction} from '@e22m4u/ts-rest-router';
|
|
536
535
|
|
|
537
536
|
@beforeAction(authHook)
|
|
538
537
|
@restController('profile')
|
|
@@ -543,9 +542,8 @@ export class ProfileController extends Service {
|
|
|
543
542
|
// который был создан и зарегистрирован в хуке.
|
|
544
543
|
const authService = this.getService(AuthService);
|
|
545
544
|
|
|
546
|
-
if (!authService.currentUser)
|
|
547
|
-
|
|
548
|
-
|
|
545
|
+
if (!authService.currentUser) throw createError(401, 'Unauthorized');
|
|
546
|
+
|
|
549
547
|
return authService.currentUser;
|
|
550
548
|
}
|
|
551
549
|
}
|
|
@@ -667,4 +665,4 @@ npm run test
|
|
|
667
665
|
|
|
668
666
|
## Лицензия
|
|
669
667
|
|
|
670
|
-
MIT
|
|
668
|
+
MIT
|
package/dist/cjs/index.cjs
CHANGED
|
@@ -32,7 +32,7 @@ __export(index_exports, {
|
|
|
32
32
|
RESPONSE_BODY_METADATA_KEY: () => RESPONSE_BODY_METADATA_KEY,
|
|
33
33
|
REST_ACTIONS_METADATA_KEY: () => REST_ACTIONS_METADATA_KEY,
|
|
34
34
|
REST_CONTROLLER_METADATA_KEY: () => REST_CONTROLLER_METADATA_KEY,
|
|
35
|
-
RequestContext: () =>
|
|
35
|
+
RequestContext: () => import_js_trie_router5.RequestContext,
|
|
36
36
|
RequestContextReflector: () => RequestContextReflector,
|
|
37
37
|
RequestDataReflector: () => RequestDataReflector,
|
|
38
38
|
RequestDataSource: () => RequestDataSource,
|
|
@@ -40,6 +40,7 @@ __export(index_exports, {
|
|
|
40
40
|
RestActionReflector: () => RestActionReflector,
|
|
41
41
|
RestControllerReflector: () => RestControllerReflector,
|
|
42
42
|
RestRouter: () => RestRouter,
|
|
43
|
+
RouterHookType: () => import_js_trie_router4.RouterHookType,
|
|
43
44
|
afterAction: () => afterAction,
|
|
44
45
|
beforeAction: () => beforeAction,
|
|
45
46
|
deleteAction: () => deleteAction,
|
|
@@ -980,6 +981,7 @@ var RestRouter = _RestRouter;
|
|
|
980
981
|
|
|
981
982
|
// dist/esm/index.js
|
|
982
983
|
var import_js_trie_router4 = require("@e22m4u/js-trie-router");
|
|
984
|
+
var import_js_trie_router5 = require("@e22m4u/js-trie-router");
|
|
983
985
|
// Annotate the CommonJS export names for ESM import in node:
|
|
984
986
|
0 && (module.exports = {
|
|
985
987
|
AFTER_ACTION_METADATA_KEY,
|
|
@@ -1001,6 +1003,7 @@ var import_js_trie_router4 = require("@e22m4u/js-trie-router");
|
|
|
1001
1003
|
RestActionReflector,
|
|
1002
1004
|
RestControllerReflector,
|
|
1003
1005
|
RestRouter,
|
|
1006
|
+
RouterHookType,
|
|
1004
1007
|
afterAction,
|
|
1005
1008
|
beforeAction,
|
|
1006
1009
|
deleteAction,
|
package/dist/esm/index.d.ts
CHANGED
|
@@ -4,6 +4,7 @@ export * from './decorators/index.js';
|
|
|
4
4
|
export * from './data-schema-types.js';
|
|
5
5
|
export * from './controller-registry.js';
|
|
6
6
|
export { RouteHandler } from '@e22m4u/js-trie-router';
|
|
7
|
+
export { RouterHookType } from '@e22m4u/js-trie-router';
|
|
7
8
|
export { RequestContext } from '@e22m4u/js-trie-router';
|
|
8
9
|
export { RoutePreHandler } from '@e22m4u/js-trie-router';
|
|
9
10
|
export { RoutePostHandler } from '@e22m4u/js-trie-router';
|
package/dist/esm/index.js
CHANGED
|
@@ -3,4 +3,5 @@ export * from './errors/index.js';
|
|
|
3
3
|
export * from './decorators/index.js';
|
|
4
4
|
export * from './data-schema-types.js';
|
|
5
5
|
export * from './controller-registry.js';
|
|
6
|
+
export { RouterHookType } from '@e22m4u/js-trie-router';
|
|
6
7
|
export { RequestContext } from '@e22m4u/js-trie-router';
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -5,6 +5,7 @@ export * from './data-schema-types.js';
|
|
|
5
5
|
export * from './controller-registry.js';
|
|
6
6
|
|
|
7
7
|
export {RouteHandler} from '@e22m4u/js-trie-router';
|
|
8
|
+
export {RouterHookType} from '@e22m4u/js-trie-router';
|
|
8
9
|
export {RequestContext} from '@e22m4u/js-trie-router';
|
|
9
10
|
export {RoutePreHandler} from '@e22m4u/js-trie-router';
|
|
10
11
|
export {RoutePostHandler} from '@e22m4u/js-trie-router';
|