@e22m4u/ts-rest-router 0.0.7 → 0.1.1

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 (62) hide show
  1. package/README-ru.md +114 -91
  2. package/README.md +106 -85
  3. package/dist/cjs/index.cjs +427 -123
  4. package/dist/esm/controller-registry.d.ts +53 -11
  5. package/dist/esm/controller-registry.js +242 -104
  6. package/dist/esm/debuggable-service.js +1 -1
  7. package/dist/esm/decorators/after/after-decorator.d.ts +9 -0
  8. package/dist/esm/decorators/after/after-decorator.js +22 -0
  9. package/dist/esm/decorators/after/after-decorator.spec.d.ts +1 -0
  10. package/dist/esm/decorators/after/after-decorator.spec.js +115 -0
  11. package/dist/esm/decorators/after/after-metadata.d.ts +13 -0
  12. package/dist/esm/decorators/after/after-metadata.js +5 -0
  13. package/dist/esm/decorators/after/after-reflector.d.ts +22 -0
  14. package/dist/esm/decorators/after/after-reflector.js +29 -0
  15. package/dist/esm/decorators/after/after-reflector.spec.d.ts +1 -0
  16. package/dist/esm/decorators/after/after-reflector.spec.js +102 -0
  17. package/dist/esm/decorators/after/index.d.ts +3 -0
  18. package/dist/esm/decorators/after/index.js +3 -0
  19. package/dist/esm/decorators/before/before-decorator.d.ts +9 -0
  20. package/dist/esm/decorators/before/before-decorator.js +22 -0
  21. package/dist/esm/decorators/before/before-decorator.spec.d.ts +1 -0
  22. package/dist/esm/decorators/before/before-decorator.spec.js +115 -0
  23. package/dist/esm/decorators/before/before-metadata.d.ts +13 -0
  24. package/dist/esm/decorators/before/before-metadata.js +5 -0
  25. package/dist/esm/decorators/before/before-reflector.d.ts +22 -0
  26. package/dist/esm/decorators/before/before-reflector.js +29 -0
  27. package/dist/esm/decorators/before/before-reflector.spec.d.ts +1 -0
  28. package/dist/esm/decorators/before/before-reflector.spec.js +102 -0
  29. package/dist/esm/decorators/before/index.d.ts +3 -0
  30. package/dist/esm/decorators/before/index.js +3 -0
  31. package/dist/esm/decorators/controller/controller-decorator.d.ts +2 -1
  32. package/dist/esm/decorators/controller/controller-decorator.js +26 -1
  33. package/dist/esm/decorators/controller/controller-decorator.spec.js +28 -0
  34. package/dist/esm/decorators/index.d.ts +2 -0
  35. package/dist/esm/decorators/index.js +2 -0
  36. package/dist/esm/decorators/request-data/request-data-decorator.d.ts +1 -1
  37. package/dist/esm/decorators/request-data/request-data-decorator.js +1 -1
  38. package/dist/esm/decorators/request-data/request-data-decorator.spec.js +5 -5
  39. package/dist/esm/utils/create-debugger.d.ts +35 -2
  40. package/dist/esm/utils/create-debugger.js +71 -5
  41. package/package.json +1 -1
  42. package/src/controller-registry.spec.ts +597 -275
  43. package/src/controller-registry.ts +263 -128
  44. package/src/debuggable-service.ts +1 -1
  45. package/src/decorators/after/after-decorator.spec.ts +92 -0
  46. package/src/decorators/after/after-decorator.ts +40 -0
  47. package/src/decorators/after/after-metadata.ts +17 -0
  48. package/src/decorators/after/after-reflector.spec.ts +107 -0
  49. package/src/decorators/after/after-reflector.ts +45 -0
  50. package/src/decorators/after/index.ts +3 -0
  51. package/src/decorators/before/before-decorator.spec.ts +92 -0
  52. package/src/decorators/before/before-decorator.ts +40 -0
  53. package/src/decorators/before/before-metadata.ts +17 -0
  54. package/src/decorators/before/before-reflector.spec.ts +111 -0
  55. package/src/decorators/before/before-reflector.ts +50 -0
  56. package/src/decorators/before/index.ts +3 -0
  57. package/src/decorators/controller/controller-decorator.spec.ts +22 -0
  58. package/src/decorators/controller/controller-decorator.ts +29 -1
  59. package/src/decorators/index.ts +2 -0
  60. package/src/decorators/request-data/request-data-decorator.spec.ts +5 -5
  61. package/src/decorators/request-data/request-data-decorator.ts +1 -1
  62. package/src/utils/create-debugger.ts +84 -7
@@ -1,21 +1,98 @@
1
- import DebugFactory from 'debug';
1
+ import {inspect} from 'util';
2
+ import DebugModule from 'debug';
3
+ import {AnyObject} from '../types.js';
2
4
  import {format} from '@e22m4u/js-format';
3
5
 
4
6
  /**
5
7
  * Debugger.
6
8
  */
7
- export type Debugger = (...args: Parameters<typeof format>) => void;
9
+ export type Debugger = (
10
+ messageOrData: string | unknown,
11
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
12
+ ...args: any[]
13
+ ) => void;
14
+
15
+ /**
16
+ * Colorize string.
17
+ *
18
+ * @param input
19
+ */
20
+ function colorizeString(input: string): string {
21
+ const c = Number(DebugModule['selectColor'](input));
22
+ const colorCode = '\u001B[3' + (c < 8 ? c : '8;5;' + c);
23
+ return `${colorCode};1m${input}\u001B[0m`;
24
+ }
8
25
 
9
26
  /**
10
27
  * Create debugger.
11
28
  *
29
+ * Base usage:
30
+ * ```ts
31
+ * const debug = createDebugger('myService');
32
+ * debug('Service created.');
33
+ * // ujut:myService Service created.
34
+ * ```
35
+ *
36
+ * Nested namespaces:
37
+ * ```ts
38
+ * const debug1 = debug.bind('namespace1');
39
+ * const debug2 = debug.bind('namespace2');
40
+ * debug1('Application started'); // ujut:myService [namespace1] Application started
41
+ * debug2('Connection established'); // ujut:myService [namespace2] Connection established
42
+ * ```
43
+ *
44
+ * Value inspection:
45
+ * ```ts
46
+ * debug({foo: 'lorem', bar: 'ipsum'})
47
+ * // ujut:myService {
48
+ * // ujut:myService "foo": "lorem",
49
+ * // ujut:myService "bar": "ipsum"
50
+ * // ujut:myService }
51
+ * ```
52
+ *
53
+ * Titled inspection output:
54
+ * ```ts
55
+ * debug({foo: 'lorem', bar: 'ipsum'}, 'My awesome output:')
56
+ * // ujut:myService My awesome output:
57
+ * // ujut:myService {
58
+ * // ujut:myService "foo": "lorem",
59
+ * // ujut:myService "bar": "ipsum"
60
+ * // ujut:myService }
61
+ * ```
62
+ *
12
63
  * @param name
13
64
  */
14
65
  export function createDebugger(name: string): Debugger {
15
- const debug = DebugFactory(`tsRestRouter:${name}`);
16
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
17
- return function (message: string, ...args: any[]) {
18
- const interpolatedMessage = format(message, ...args);
19
- return debug(interpolatedMessage);
66
+ const debuggerName = `tsRestRouter:${name}`;
67
+ // включить вывод логов можно принудительно
68
+ // if (!process.env.DEBUG) DebugModule.enable('ujut*');
69
+ const debug = DebugModule(debuggerName);
70
+ return function (
71
+ this: unknown,
72
+ messageOrData: string | unknown,
73
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
74
+ ...args: any[]
75
+ ) {
76
+ let prefix = '';
77
+ if (typeof this === 'string') {
78
+ const isDebugUsesColors = (debug as AnyObject).useColors;
79
+ prefix = isDebugUsesColors ? colorizeString(`[${this}] `) : `[${this}] `;
80
+ }
81
+ if (typeof messageOrData === 'string') {
82
+ const interpolatedMessage = format(messageOrData, ...args);
83
+ return debug(prefix + interpolatedMessage);
84
+ }
85
+ const inspectOptions = {
86
+ showHidden: false,
87
+ depth: null,
88
+ colors: true,
89
+ compact: false,
90
+ };
91
+ const multiString = inspect(messageOrData, inspectOptions);
92
+ const rows = multiString.split('\n');
93
+ const colorizedDebuggerName = colorizeString(debuggerName);
94
+ [...args, ...rows].forEach(v =>
95
+ console.log(` ${colorizedDebuggerName} ${prefix}${v}`),
96
+ );
20
97
  };
21
98
  }