@e22m4u/ts-rest-router 0.6.5 → 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 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: {username: string; email: string},
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: (qty) => qty > 0 || 'Quantity must be positive',
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 = { id: 1, name: 'John Doe' };
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
- throw createError(401, 'Unauthorized');
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
@@ -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: () => import_js_trie_router4.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,
@@ -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';
@@ -1,6 +1,6 @@
1
1
  import { Constructor } from './types.js';
2
2
  import { DebuggableService } from './debuggable-service.js';
3
- import { HookType, PostHandlerHook, PreHandlerHook } from '@e22m4u/js-trie-router';
3
+ import { RouterHookType, PostHandlerHook, PreHandlerHook } from '@e22m4u/js-trie-router';
4
4
  import { ControllerRootOptions } from './controller-registry.js';
5
5
  /**
6
6
  * Rest router.
@@ -23,12 +23,12 @@ export declare class RestRouter extends DebuggableService {
23
23
  * @param type
24
24
  * @param hook
25
25
  */
26
- addHook(type: typeof HookType.PRE_HANDLER, hook: PreHandlerHook): this;
26
+ addHook(type: typeof RouterHookType.PRE_HANDLER, hook: PreHandlerHook): this;
27
27
  /**
28
28
  * Add hook.
29
29
  *
30
30
  * @param type
31
31
  * @param hook
32
32
  */
33
- addHook(type: typeof HookType.POST_HANDLER, hook: PostHandlerHook): this;
33
+ addHook(type: typeof RouterHookType.POST_HANDLER, hook: PostHandlerHook): this;
34
34
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@e22m4u/ts-rest-router",
3
- "version": "0.6.5",
3
+ "version": "0.6.7",
4
4
  "description": "Декларативный REST-маршрутизатор на основе контроллеров для TypeScript",
5
5
  "author": "Mikhail Evstropov <e22m4u@yandex.ru>",
6
6
  "license": "MIT",
@@ -45,7 +45,7 @@
45
45
  "@e22m4u/js-debug": "~0.3.2",
46
46
  "@e22m4u/js-format": "~0.2.0",
47
47
  "@e22m4u/js-service": "~0.4.4",
48
- "@e22m4u/js-trie-router": "~0.3.3",
48
+ "@e22m4u/js-trie-router": "~0.3.4",
49
49
  "@e22m4u/ts-reflector": "~0.1.8",
50
50
  "http-errors": "~2.0.0"
51
51
  },
@@ -2,7 +2,7 @@
2
2
  import {
3
3
  createRequestMock,
4
4
  createResponseMock,
5
- HookType,
5
+ RouterHookType,
6
6
  HttpMethod,
7
7
  ParsedCookies,
8
8
  ParsedHeaders,
@@ -182,7 +182,9 @@ describe('ControllerRegistry', function () {
182
182
  });
183
183
  const matching = routeReg.matchRouteByRequest(req);
184
184
  expect(matching).to.be.not.empty;
185
- const res = matching!.route.hookRegistry.getHooks(HookType.PRE_HANDLER);
185
+ const res = matching!.route.hookRegistry.getHooks(
186
+ RouterHookType.PRE_HANDLER,
187
+ );
186
188
  expect(res).to.be.eql([PRE_HANDLER_1]);
187
189
  });
188
190
 
@@ -202,7 +204,9 @@ describe('ControllerRegistry', function () {
202
204
  });
203
205
  const matching = routeReg.matchRouteByRequest(req);
204
206
  expect(matching).to.be.not.empty;
205
- const res = matching!.route.hookRegistry.getHooks(HookType.PRE_HANDLER);
207
+ const res = matching!.route.hookRegistry.getHooks(
208
+ RouterHookType.PRE_HANDLER,
209
+ );
206
210
  expect(res).to.be.eql([PRE_HANDLER_1]);
207
211
  });
208
212
 
@@ -222,7 +226,9 @@ describe('ControllerRegistry', function () {
222
226
  });
223
227
  const matching = routeReg.matchRouteByRequest(req);
224
228
  expect(matching).to.be.not.empty;
225
- const res = matching!.route.hookRegistry.getHooks(HookType.PRE_HANDLER);
229
+ const res = matching!.route.hookRegistry.getHooks(
230
+ RouterHookType.PRE_HANDLER,
231
+ );
226
232
  expect(res).to.be.eql([PRE_HANDLER_1]);
227
233
  });
228
234
 
@@ -241,7 +247,9 @@ describe('ControllerRegistry', function () {
241
247
  });
242
248
  const matching = routeReg.matchRouteByRequest(req);
243
249
  expect(matching).to.be.not.empty;
244
- const res = matching!.route.hookRegistry.getHooks(HookType.PRE_HANDLER);
250
+ const res = matching!.route.hookRegistry.getHooks(
251
+ RouterHookType.PRE_HANDLER,
252
+ );
245
253
  expect(res).to.be.eql([PRE_HANDLER_1]);
246
254
  });
247
255
 
@@ -260,7 +268,9 @@ describe('ControllerRegistry', function () {
260
268
  });
261
269
  const matching = routeReg.matchRouteByRequest(req);
262
270
  expect(matching).to.be.not.empty;
263
- const res = matching!.route.hookRegistry.getHooks(HookType.PRE_HANDLER);
271
+ const res = matching!.route.hookRegistry.getHooks(
272
+ RouterHookType.PRE_HANDLER,
273
+ );
264
274
  expect(res).to.be.eql([PRE_HANDLER_1]);
265
275
  });
266
276
  });
@@ -283,7 +293,9 @@ describe('ControllerRegistry', function () {
283
293
  });
284
294
  const matching = routeReg.matchRouteByRequest(req);
285
295
  expect(matching).to.be.not.empty;
286
- const res = matching!.route.hookRegistry.getHooks(HookType.PRE_HANDLER);
296
+ const res = matching!.route.hookRegistry.getHooks(
297
+ RouterHookType.PRE_HANDLER,
298
+ );
287
299
  expect(res).to.be.eql([PRE_HANDLER_1, PRE_HANDLER_2]);
288
300
  });
289
301
 
@@ -303,7 +315,9 @@ describe('ControllerRegistry', function () {
303
315
  });
304
316
  const matching = routeReg.matchRouteByRequest(req);
305
317
  expect(matching).to.be.not.empty;
306
- const res = matching!.route.hookRegistry.getHooks(HookType.PRE_HANDLER);
318
+ const res = matching!.route.hookRegistry.getHooks(
319
+ RouterHookType.PRE_HANDLER,
320
+ );
307
321
  expect(res).to.be.eql([PRE_HANDLER_1, PRE_HANDLER_2]);
308
322
  });
309
323
 
@@ -323,7 +337,9 @@ describe('ControllerRegistry', function () {
323
337
  });
324
338
  const matching = routeReg.matchRouteByRequest(req);
325
339
  expect(matching).to.be.not.empty;
326
- const res = matching!.route.hookRegistry.getHooks(HookType.PRE_HANDLER);
340
+ const res = matching!.route.hookRegistry.getHooks(
341
+ RouterHookType.PRE_HANDLER,
342
+ );
327
343
  expect(res).to.be.eql([PRE_HANDLER_1, PRE_HANDLER_2]);
328
344
  });
329
345
 
@@ -344,7 +360,9 @@ describe('ControllerRegistry', function () {
344
360
  });
345
361
  const matching = routeReg.matchRouteByRequest(req);
346
362
  expect(matching).to.be.not.empty;
347
- const res = matching!.route.hookRegistry.getHooks(HookType.PRE_HANDLER);
363
+ const res = matching!.route.hookRegistry.getHooks(
364
+ RouterHookType.PRE_HANDLER,
365
+ );
348
366
  expect(res).to.be.eql([PRE_HANDLER_1, PRE_HANDLER_2]);
349
367
  });
350
368
 
@@ -365,7 +383,9 @@ describe('ControllerRegistry', function () {
365
383
  });
366
384
  const matching = routeReg.matchRouteByRequest(req);
367
385
  expect(matching).to.be.not.empty;
368
- const res = matching!.route.hookRegistry.getHooks(HookType.PRE_HANDLER);
386
+ const res = matching!.route.hookRegistry.getHooks(
387
+ RouterHookType.PRE_HANDLER,
388
+ );
369
389
  expect(res).to.be.eql([PRE_HANDLER_1, PRE_HANDLER_2]);
370
390
  });
371
391
  });
@@ -387,7 +407,7 @@ describe('ControllerRegistry', function () {
387
407
  const matching = routeReg.matchRouteByRequest(req);
388
408
  expect(matching).to.be.not.empty;
389
409
  const res = matching!.route.hookRegistry.getHooks(
390
- HookType.POST_HANDLER,
410
+ RouterHookType.POST_HANDLER,
391
411
  );
392
412
  expect(res).to.be.eql([POST_HANDLER_1]);
393
413
  });
@@ -409,7 +429,7 @@ describe('ControllerRegistry', function () {
409
429
  const matching = routeReg.matchRouteByRequest(req);
410
430
  expect(matching).to.be.not.empty;
411
431
  const res = matching!.route.hookRegistry.getHooks(
412
- HookType.POST_HANDLER,
432
+ RouterHookType.POST_HANDLER,
413
433
  );
414
434
  expect(res).to.be.eql([POST_HANDLER_1]);
415
435
  });
@@ -431,7 +451,7 @@ describe('ControllerRegistry', function () {
431
451
  const matching = routeReg.matchRouteByRequest(req);
432
452
  expect(matching).to.be.not.empty;
433
453
  const res = matching!.route.hookRegistry.getHooks(
434
- HookType.POST_HANDLER,
454
+ RouterHookType.POST_HANDLER,
435
455
  );
436
456
  expect(res).to.be.eql([POST_HANDLER_1]);
437
457
  });
@@ -452,7 +472,7 @@ describe('ControllerRegistry', function () {
452
472
  const matching = routeReg.matchRouteByRequest(req);
453
473
  expect(matching).to.be.not.empty;
454
474
  const res = matching!.route.hookRegistry.getHooks(
455
- HookType.POST_HANDLER,
475
+ RouterHookType.POST_HANDLER,
456
476
  );
457
477
  expect(res).to.be.eql([POST_HANDLER_1]);
458
478
  });
@@ -473,7 +493,7 @@ describe('ControllerRegistry', function () {
473
493
  const matching = routeReg.matchRouteByRequest(req);
474
494
  expect(matching).to.be.not.empty;
475
495
  const res = matching!.route.hookRegistry.getHooks(
476
- HookType.POST_HANDLER,
496
+ RouterHookType.POST_HANDLER,
477
497
  );
478
498
  expect(res).to.be.eql([POST_HANDLER_1]);
479
499
  });
@@ -498,7 +518,7 @@ describe('ControllerRegistry', function () {
498
518
  const matching = routeReg.matchRouteByRequest(req);
499
519
  expect(matching).to.be.not.empty;
500
520
  const res = matching!.route.hookRegistry.getHooks(
501
- HookType.POST_HANDLER,
521
+ RouterHookType.POST_HANDLER,
502
522
  );
503
523
  expect(res).to.be.eql([POST_HANDLER_1, POST_HANDLER_2]);
504
524
  });
@@ -520,7 +540,7 @@ describe('ControllerRegistry', function () {
520
540
  const matching = routeReg.matchRouteByRequest(req);
521
541
  expect(matching).to.be.not.empty;
522
542
  const res = matching!.route.hookRegistry.getHooks(
523
- HookType.POST_HANDLER,
543
+ RouterHookType.POST_HANDLER,
524
544
  );
525
545
  expect(res).to.be.eql([POST_HANDLER_1, POST_HANDLER_2]);
526
546
  });
@@ -542,7 +562,7 @@ describe('ControllerRegistry', function () {
542
562
  const matching = routeReg.matchRouteByRequest(req);
543
563
  expect(matching).to.be.not.empty;
544
564
  const res = matching!.route.hookRegistry.getHooks(
545
- HookType.POST_HANDLER,
565
+ RouterHookType.POST_HANDLER,
546
566
  );
547
567
  expect(res).to.be.eql([POST_HANDLER_1, POST_HANDLER_2]);
548
568
  });
@@ -565,7 +585,7 @@ describe('ControllerRegistry', function () {
565
585
  const matching = routeReg.matchRouteByRequest(req);
566
586
  expect(matching).to.be.not.empty;
567
587
  const res = matching!.route.hookRegistry.getHooks(
568
- HookType.POST_HANDLER,
588
+ RouterHookType.POST_HANDLER,
569
589
  );
570
590
  expect(res).to.be.eql([POST_HANDLER_1, POST_HANDLER_2]);
571
591
  });
@@ -588,7 +608,7 @@ describe('ControllerRegistry', function () {
588
608
  const matching = routeReg.matchRouteByRequest(req);
589
609
  expect(matching).to.be.not.empty;
590
610
  const res = matching!.route.hookRegistry.getHooks(
591
- HookType.POST_HANDLER,
611
+ RouterHookType.POST_HANDLER,
592
612
  );
593
613
  expect(res).to.be.eql([POST_HANDLER_1, POST_HANDLER_2]);
594
614
  });
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';
@@ -4,7 +4,7 @@ import {DebuggableService} from './debuggable-service.js';
4
4
  import {
5
5
  TrieRouter,
6
6
  RouterHook,
7
- HookType,
7
+ RouterHookType,
8
8
  PostHandlerHook,
9
9
  PreHandlerHook,
10
10
  } from '@e22m4u/js-trie-router';
@@ -45,7 +45,7 @@ export class RestRouter extends DebuggableService {
45
45
  * @param type
46
46
  * @param hook
47
47
  */
48
- addHook(type: typeof HookType.PRE_HANDLER, hook: PreHandlerHook): this;
48
+ addHook(type: typeof RouterHookType.PRE_HANDLER, hook: PreHandlerHook): this;
49
49
 
50
50
  /**
51
51
  * Add hook.
@@ -53,7 +53,10 @@ export class RestRouter extends DebuggableService {
53
53
  * @param type
54
54
  * @param hook
55
55
  */
56
- addHook(type: typeof HookType.POST_HANDLER, hook: PostHandlerHook): this;
56
+ addHook(
57
+ type: typeof RouterHookType.POST_HANDLER,
58
+ hook: PostHandlerHook,
59
+ ): this;
57
60
 
58
61
  /**
59
62
  * Add hook.
@@ -61,7 +64,7 @@ export class RestRouter extends DebuggableService {
61
64
  * @param type
62
65
  * @param hook
63
66
  */
64
- addHook(type: HookType, hook: RouterHook) {
67
+ addHook(type: RouterHookType, hook: RouterHook) {
65
68
  this.getService(TrieRouter).addHook(type, hook);
66
69
  return this;
67
70
  }