@privateaim/server-kit 0.8.15 → 0.8.17

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 (154) hide show
  1. package/CHANGELOG.md +49 -0
  2. package/dist/constants.d.ts +6 -0
  3. package/dist/constants.d.ts.map +1 -0
  4. package/dist/core/component/handler/check.d.ts +4 -0
  5. package/dist/core/component/handler/check.d.ts.map +1 -0
  6. package/dist/core/component/handler/index.d.ts +3 -0
  7. package/dist/core/component/handler/index.d.ts.map +1 -0
  8. package/dist/core/component/handler/module.d.ts +11 -0
  9. package/dist/core/component/handler/module.d.ts.map +1 -0
  10. package/dist/core/component/handler/types.d.ts +11 -0
  11. package/dist/core/component/handler/types.d.ts.map +1 -0
  12. package/dist/core/component/index.d.ts +1 -0
  13. package/dist/core/component/index.d.ts.map +1 -1
  14. package/dist/core/queue-router/module.d.ts +3 -2
  15. package/dist/core/queue-router/module.d.ts.map +1 -1
  16. package/dist/core/queue-router/types.d.ts +7 -4
  17. package/dist/core/queue-router/types.d.ts.map +1 -1
  18. package/dist/index.cjs +262 -550
  19. package/dist/index.cjs.map +1 -1
  20. package/dist/index.d.ts +1 -0
  21. package/dist/index.d.ts.map +1 -1
  22. package/dist/index.mjs +250 -504
  23. package/dist/index.mjs.map +1 -1
  24. package/dist/services/amqp/index.d.ts +1 -0
  25. package/dist/services/amqp/index.d.ts.map +1 -1
  26. package/dist/services/amqp/module.d.ts +2 -5
  27. package/dist/services/amqp/module.d.ts.map +1 -1
  28. package/dist/services/amqp/singleton.d.ts +6 -0
  29. package/dist/services/amqp/singleton.d.ts.map +1 -0
  30. package/dist/services/entity-event/handlers/index.d.ts +3 -0
  31. package/dist/services/entity-event/handlers/index.d.ts.map +1 -0
  32. package/dist/services/entity-event/handlers/redis/helpers.d.ts +2 -0
  33. package/dist/services/entity-event/handlers/redis/helpers.d.ts.map +1 -0
  34. package/dist/services/entity-event/handlers/redis/index.d.ts +3 -0
  35. package/dist/services/entity-event/handlers/redis/index.d.ts.map +1 -0
  36. package/dist/services/entity-event/handlers/redis/module.d.ts +8 -0
  37. package/dist/services/entity-event/handlers/redis/module.d.ts.map +1 -0
  38. package/dist/services/entity-event/handlers/socket/index.d.ts.map +1 -0
  39. package/dist/services/entity-event/handlers/socket/module.d.ts +10 -0
  40. package/dist/services/entity-event/handlers/socket/module.d.ts.map +1 -0
  41. package/dist/services/entity-event/index.d.ts +5 -0
  42. package/dist/services/entity-event/index.d.ts.map +1 -0
  43. package/dist/services/entity-event/module.d.ts +10 -0
  44. package/dist/services/entity-event/module.d.ts.map +1 -0
  45. package/dist/services/entity-event/singleton.d.ts +5 -0
  46. package/dist/services/entity-event/singleton.d.ts.map +1 -0
  47. package/dist/services/entity-event/types.d.ts +38 -0
  48. package/dist/services/entity-event/types.d.ts.map +1 -0
  49. package/dist/services/entity-event/utils.d.ts +2 -0
  50. package/dist/services/entity-event/utils.d.ts.map +1 -0
  51. package/dist/services/index.d.ts +1 -3
  52. package/dist/services/index.d.ts.map +1 -1
  53. package/dist/services/logger/index.d.ts +1 -0
  54. package/dist/services/logger/index.d.ts.map +1 -1
  55. package/dist/services/logger/module.d.ts +1 -2
  56. package/dist/services/logger/module.d.ts.map +1 -1
  57. package/dist/services/logger/singleton.d.ts +1 -1
  58. package/dist/services/logger/transports/console.d.ts +8 -0
  59. package/dist/services/logger/transports/console.d.ts.map +1 -0
  60. package/dist/services/logger/transports/index.d.ts +3 -0
  61. package/dist/services/logger/transports/index.d.ts.map +1 -0
  62. package/dist/services/logger/transports/memory.d.ts +6 -0
  63. package/dist/services/logger/transports/memory.d.ts.map +1 -0
  64. package/dist/services/logger/types.d.ts +27 -7
  65. package/dist/services/logger/types.d.ts.map +1 -1
  66. package/package.json +2 -3
  67. package/src/constants.ts +12 -0
  68. package/src/core/component/handler/check.ts +23 -0
  69. package/src/{services/loki → core/component/handler}/index.ts +2 -2
  70. package/src/core/component/handler/module.ts +57 -0
  71. package/src/core/component/handler/types.ts +28 -0
  72. package/src/core/component/index.ts +1 -0
  73. package/src/core/queue-router/module.ts +30 -4
  74. package/src/core/queue-router/types.ts +12 -5
  75. package/src/index.ts +1 -0
  76. package/src/services/amqp/index.ts +1 -0
  77. package/src/services/amqp/module.ts +5 -19
  78. package/src/services/{loki → amqp}/singleton.ts +6 -6
  79. package/src/services/entity-event/handlers/index.ts +9 -0
  80. package/src/services/entity-event/handlers/redis/helpers.ts +28 -0
  81. package/src/services/{domain-event → entity-event/handlers}/redis/index.ts +1 -0
  82. package/src/services/entity-event/handlers/redis/module.ts +44 -0
  83. package/src/services/entity-event/handlers/socket/module.ts +72 -0
  84. package/src/services/{domain-event → entity-event}/index.ts +1 -2
  85. package/src/services/entity-event/module.ts +75 -0
  86. package/src/services/entity-event/singleton.ts +22 -0
  87. package/src/services/entity-event/types.ts +61 -0
  88. package/src/services/{domain-event → entity-event}/utils.ts +1 -13
  89. package/src/services/index.ts +1 -3
  90. package/src/services/logger/index.ts +1 -0
  91. package/src/services/logger/module.ts +11 -49
  92. package/src/services/logger/singleton.ts +1 -1
  93. package/src/services/logger/transports/console.ts +27 -0
  94. package/src/services/logger/transports/index.ts +9 -0
  95. package/src/services/logger/transports/memory.ts +18 -0
  96. package/src/services/logger/types.ts +27 -8
  97. package/test/data/error.ts +16 -0
  98. package/test/unit/logger.spec.ts +41 -20
  99. package/dist/services/domain-event/index.d.ts +0 -6
  100. package/dist/services/domain-event/index.d.ts.map +0 -1
  101. package/dist/services/domain-event/module.d.ts +0 -10
  102. package/dist/services/domain-event/module.d.ts.map +0 -1
  103. package/dist/services/domain-event/redis/index.d.ts.map +0 -1
  104. package/dist/services/domain-event/redis/module.d.ts +0 -8
  105. package/dist/services/domain-event/redis/module.d.ts.map +0 -1
  106. package/dist/services/domain-event/singleton.d.ts +0 -4
  107. package/dist/services/domain-event/singleton.d.ts.map +0 -1
  108. package/dist/services/domain-event/socket/index.d.ts +0 -2
  109. package/dist/services/domain-event/socket/index.d.ts.map +0 -1
  110. package/dist/services/domain-event/socket/module.d.ts +0 -8
  111. package/dist/services/domain-event/socket/module.d.ts.map +0 -1
  112. package/dist/services/domain-event/types.d.ts +0 -29
  113. package/dist/services/domain-event/types.d.ts.map +0 -1
  114. package/dist/services/domain-event/utils.d.ts +0 -4
  115. package/dist/services/domain-event/utils.d.ts.map +0 -1
  116. package/dist/services/log-store/entities/base.d.ts +0 -7
  117. package/dist/services/log-store/entities/base.d.ts.map +0 -1
  118. package/dist/services/log-store/entities/index.d.ts +0 -3
  119. package/dist/services/log-store/entities/index.d.ts.map +0 -1
  120. package/dist/services/log-store/entities/loki.d.ts +0 -13
  121. package/dist/services/log-store/entities/loki.d.ts.map +0 -1
  122. package/dist/services/log-store/entities/memory.d.ts +0 -11
  123. package/dist/services/log-store/entities/memory.d.ts.map +0 -1
  124. package/dist/services/log-store/index.d.ts +0 -4
  125. package/dist/services/log-store/index.d.ts.map +0 -1
  126. package/dist/services/log-store/singleton.d.ts +0 -6
  127. package/dist/services/log-store/singleton.d.ts.map +0 -1
  128. package/dist/services/log-store/types.d.ts +0 -27
  129. package/dist/services/log-store/types.d.ts.map +0 -1
  130. package/dist/services/logger/store.d.ts +0 -14
  131. package/dist/services/logger/store.d.ts.map +0 -1
  132. package/dist/services/loki/index.d.ts +0 -3
  133. package/dist/services/loki/index.d.ts.map +0 -1
  134. package/dist/services/loki/module.d.ts +0 -3
  135. package/dist/services/loki/module.d.ts.map +0 -1
  136. package/dist/services/loki/singleton.d.ts +0 -6
  137. package/dist/services/loki/singleton.d.ts.map +0 -1
  138. package/src/services/domain-event/module.ts +0 -54
  139. package/src/services/domain-event/redis/module.ts +0 -56
  140. package/src/services/domain-event/singleton.ts +0 -36
  141. package/src/services/domain-event/socket/module.ts +0 -68
  142. package/src/services/domain-event/types.ts +0 -50
  143. package/src/services/log-store/entities/base.ts +0 -25
  144. package/src/services/log-store/entities/index.ts +0 -9
  145. package/src/services/log-store/entities/loki.ts +0 -158
  146. package/src/services/log-store/entities/memory.ts +0 -100
  147. package/src/services/log-store/index.ts +0 -10
  148. package/src/services/log-store/singleton.ts +0 -36
  149. package/src/services/log-store/types.ts +0 -42
  150. package/src/services/logger/store.ts +0 -70
  151. package/src/services/loki/module.ts +0 -30
  152. package/test/unit/log-store.spec.ts +0 -21
  153. /package/dist/services/{domain-event/redis → entity-event/handlers/socket}/index.d.ts +0 -0
  154. /package/src/services/{domain-event → entity-event/handlers}/socket/index.ts +0 -0
@@ -1,5 +1,5 @@
1
1
  import { type Factory } from 'singa';
2
- import type { Logger } from 'winston';
2
+ import type { Logger } from './types';
3
3
  export declare function setLoggerFactory(factory: Factory<Logger>): void;
4
4
  export declare function isLoggerUsable(): boolean;
5
5
  export declare function setLogger(input: Logger): void;
@@ -0,0 +1,8 @@
1
+ import { transports } from 'winston';
2
+ import type { TransportStreamOptions } from 'winston-transport';
3
+ import type { LoggerTransport } from '../types';
4
+ export declare class LoggerConsoleTransport extends transports.Console {
5
+ constructor(options?: TransportStreamOptions);
6
+ }
7
+ export declare function createLoggerConsoleTransport(options?: TransportStreamOptions): LoggerTransport;
8
+ //# sourceMappingURL=console.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"console.d.ts","sourceRoot":"","sources":["../../../../src/services/logger/transports/console.ts"],"names":[],"mappings":"AAQA,OAAO,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AACrC,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,mBAAmB,CAAC;AAEhE,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,UAAU,CAAC;AAEhD,qBAAa,sBAAuB,SAAQ,UAAU,CAAC,OAAO;gBAC9C,OAAO,GAAE,sBAA2B;CAQnD;AAED,wBAAgB,4BAA4B,CAAC,OAAO,GAAE,sBAA2B,GAAI,eAAe,CAEnG"}
@@ -0,0 +1,3 @@
1
+ export * from './console';
2
+ export * from './memory';
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/services/logger/transports/index.ts"],"names":[],"mappings":"AAOA,cAAc,WAAW,CAAC;AAC1B,cAAc,UAAU,CAAC"}
@@ -0,0 +1,6 @@
1
+ import Transport from 'winston-transport';
2
+ export declare class LoggerMemoryTransport extends Transport {
3
+ items: Record<PropertyKey, any>[];
4
+ log(info: Record<PropertyKey, any>, next: () => void): any;
5
+ }
6
+ //# sourceMappingURL=memory.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"memory.d.ts","sourceRoot":"","sources":["../../../../src/services/logger/transports/memory.ts"],"names":[],"mappings":"AAOA,OAAO,SAAS,MAAM,mBAAmB,CAAC;AAE1C,qBAAa,qBAAsB,SAAQ,SAAS;IACzC,KAAK,EAAG,MAAM,CAAC,WAAW,EAAE,GAAG,CAAC,EAAE,CAAM;IAE/C,GAAG,CAAC,IAAI,EAAE,MAAM,CAAC,WAAW,EAAE,GAAG,CAAC,EAAE,IAAI,EAAE,MAAM,IAAI,GAAG,GAAG;CAK7D"}
@@ -1,11 +1,31 @@
1
- import type { Logger, LoggerOptions } from 'winston';
2
- import type { LogStore } from '../log-store';
1
+ import type Transport from 'winston-transport';
2
+ import type { LoggerOptions } from 'winston';
3
+ export type LoggerTransport = Transport;
3
4
  export type LoggerCreateContext = {
4
- directory?: string;
5
5
  options?: Partial<LoggerOptions>;
6
- store?: LogStore;
7
- labels?: Record<string, string>;
6
+ transports?: LoggerTransport[];
8
7
  };
9
- export type LoggerTransports = LoggerOptions['transports'];
10
- export type { Logger, };
8
+ type LogEntry = {
9
+ /**
10
+ * Log message.
11
+ */
12
+ message: string | Error;
13
+ [key: string]: any;
14
+ };
15
+ export interface LoggerLevelFn<OUT = any> {
16
+ (message: string, ...meta: Omit<LogEntry, 'message'>[]): OUT;
17
+ (message: LogEntry): OUT;
18
+ (message: string): OUT;
19
+ }
20
+ export type Logger = {
21
+ emerg: LoggerLevelFn<Logger>;
22
+ alert: LoggerLevelFn<Logger>;
23
+ crit: LoggerLevelFn<Logger>;
24
+ error: LoggerLevelFn<Logger>;
25
+ warn: LoggerLevelFn<Logger>;
26
+ notice: LoggerLevelFn<Logger>;
27
+ info: LoggerLevelFn<Logger>;
28
+ debug: LoggerLevelFn<Logger>;
29
+ };
30
+ export {};
11
31
  //# sourceMappingURL=types.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/services/logger/types.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EAAE,MAAM,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AACrD,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAC;AAE7C,MAAM,MAAM,mBAAmB,GAAG;IAC9B,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,OAAO,CAAC,EAAE,OAAO,CAAC,aAAa,CAAC,CAAC;IACjC,KAAK,CAAC,EAAE,QAAQ,CAAC;IACjB,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CACnC,CAAC;AAEF,MAAM,MAAM,gBAAgB,GAAG,aAAa,CAAC,YAAY,CAAC,CAAC;AAE3D,YAAY,EACR,MAAM,GACT,CAAC"}
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/services/logger/types.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,SAAS,MAAM,mBAAmB,CAAC;AAC/C,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAE7C,MAAM,MAAM,eAAe,GAAG,SAAS,CAAC;AAExC,MAAM,MAAM,mBAAmB,GAAG;IAC9B,OAAO,CAAC,EAAE,OAAO,CAAC,aAAa,CAAC,CAAC;IACjC,UAAU,CAAC,EAAE,eAAe,EAAE,CAAA;CACjC,CAAC;AAEF,KAAK,QAAQ,GAAG;IACZ;;OAEG;IACH,OAAO,EAAE,MAAM,GAAG,KAAK,CAAC;IACxB,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG,CAAA;CACrB,CAAC;AAEF,MAAM,WAAW,aAAa,CAAC,GAAG,GAAG,GAAG;IACpC,CAAC,OAAO,EAAE,MAAM,EAAE,GAAG,IAAI,EAAE,IAAI,CAAC,QAAQ,EAAE,SAAS,CAAC,EAAE,GAAG,GAAG,CAAC;IAC7D,CAAC,OAAO,EAAE,QAAQ,GAAG,GAAG,CAAC;IACzB,CAAC,OAAO,EAAC,MAAM,GAAG,GAAG,CAAC;CACzB;AAED,MAAM,MAAM,MAAM,GAAG;IACjB,KAAK,EAAE,aAAa,CAAC,MAAM,CAAC,CAAC;IAC7B,KAAK,EAAE,aAAa,CAAC,MAAM,CAAC,CAAC;IAC7B,IAAI,EAAE,aAAa,CAAC,MAAM,CAAC,CAAC;IAC5B,KAAK,EAAE,aAAa,CAAC,MAAM,CAAC,CAAC;IAC7B,IAAI,EAAE,aAAa,CAAC,MAAM,CAAC,CAAC;IAC5B,MAAM,EAAE,aAAa,CAAC,MAAM,CAAC,CAAC;IAC9B,IAAI,EAAE,aAAa,CAAC,MAAM,CAAC,CAAC;IAC5B,KAAK,EAAE,aAAa,CAAC,MAAM,CAAC,CAAC;CAChC,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@privateaim/server-kit",
3
- "version": "0.8.15",
3
+ "version": "0.8.17",
4
4
  "exports": {
5
5
  "./package.json": "./package.json",
6
6
  ".": {
@@ -28,9 +28,8 @@
28
28
  },
29
29
  "dependencies": {
30
30
  "@ebec/http": "^2.3.0",
31
- "@hapic/loki": "^1.2.0",
32
31
  "@isaacs/ttlcache": "^1.4.1",
33
- "@privateaim/kit": "^0.8.15",
32
+ "@privateaim/kit": "^0.8.17",
34
33
  "@socket.io/redis-emitter": "^5.1.0",
35
34
  "envix": "^1.5.0",
36
35
  "hapic": "^2.7.0",
@@ -0,0 +1,12 @@
1
+ /*
2
+ * Copyright (c) 2025.
3
+ * Author Peter Placzek (tada5hi)
4
+ * For the full copyright and license information,
5
+ * view the LICENSE file that was distributed with this source code.
6
+ */
7
+
8
+ export enum EnvironmentName {
9
+ TEST = 'test',
10
+ DEVELOPMENT = 'development',
11
+ PRODUCTION = 'production',
12
+ }
@@ -0,0 +1,23 @@
1
+ /*
2
+ * Copyright (c) 2025.
3
+ * Author Peter Placzek (tada5hi)
4
+ * For the full copyright and license information,
5
+ * view the LICENSE file that was distributed with this source code.
6
+ */
7
+
8
+ import { isObject } from '@privateaim/kit';
9
+ import { hasOwnProperty } from '../../../utils';
10
+
11
+ import type { ComponentHandler, ComponentHandlerFn } from './types';
12
+
13
+ export function isComponentHandler(input: unknown) : input is ComponentHandler {
14
+ if (!isObject(input)) {
15
+ return false;
16
+ }
17
+
18
+ return !(!hasOwnProperty(input, 'execute') || typeof input.execute !== 'function');
19
+ }
20
+
21
+ export function isComponentHandlerFn(input: unknown) : input is ComponentHandlerFn {
22
+ return typeof input === 'function';
23
+ }
@@ -1,9 +1,9 @@
1
1
  /*
2
- * Copyright (c) 2024.
2
+ * Copyright (c) 2025.
3
3
  * Author Peter Placzek (tada5hi)
4
4
  * For the full copyright and license information,
5
5
  * view the LICENSE file that was distributed with this source code.
6
6
  */
7
7
 
8
8
  export * from './module';
9
- export * from './singleton';
9
+ export * from './types';
@@ -0,0 +1,57 @@
1
+ /*
2
+ * Copyright (c) 2025.
3
+ * Author Peter Placzek (tada5hi)
4
+ * For the full copyright and license information,
5
+ * view the LICENSE file that was distributed with this source code.
6
+ */
7
+
8
+ import type { ObjectLiteral } from '../../../type';
9
+ import { isComponentHandlerFn } from './check';
10
+ import type { ComponentHandler, ComponentHandlerFn } from './types';
11
+
12
+ export class ComponentHandlers {
13
+ protected handlers: Record<string, ComponentHandlerFn | ComponentHandler>;
14
+
15
+ constructor() {
16
+ this.handlers = {};
17
+ }
18
+
19
+ mount<K extends string>(key: K, fn: ComponentHandler<K, any> | ComponentHandlerFn<K, any>) {
20
+ this.handlers[key] = fn;
21
+ }
22
+
23
+ unmount(key: string) {
24
+ delete this.handlers[key];
25
+ }
26
+
27
+ async setup() : Promise<void> {
28
+ const keys = Object.keys(this.handlers);
29
+ for (let i = 0; i < keys.length; i++) {
30
+ const handler = this.handlers[keys[i]];
31
+
32
+ if (
33
+ !isComponentHandlerFn(handler) &&
34
+ handler.setup
35
+ ) {
36
+ await handler.setup();
37
+ }
38
+ }
39
+ }
40
+
41
+ async execute(
42
+ key: string,
43
+ value: ObjectLiteral = {},
44
+ metadata: ObjectLiteral = {},
45
+ ) : Promise<void> {
46
+ const handler = this.handlers[key];
47
+ if (!handler) {
48
+ return;
49
+ }
50
+
51
+ if (isComponentHandlerFn(handler)) {
52
+ await handler(value, { key, metadata });
53
+ } else {
54
+ await handler.handle(value, { key, metadata });
55
+ }
56
+ }
57
+ }
@@ -0,0 +1,28 @@
1
+ /*
2
+ * Copyright (c) 2025.
3
+ * Author Peter Placzek (tada5hi)
4
+ * For the full copyright and license information,
5
+ * view the LICENSE file that was distributed with this source code.
6
+ */
7
+
8
+ import type { ObjectLiteral } from '../../../type';
9
+
10
+ export type ComponentHandlerContext<
11
+ KEY extends string = string,
12
+ > = {
13
+ key: KEY,
14
+ metadata: ObjectLiteral
15
+ };
16
+
17
+ export type ComponentHandlerFn<
18
+ KEY extends string = string,
19
+ VALUE extends ObjectLiteral = ObjectLiteral,
20
+ > = (value: VALUE, context: ComponentHandlerContext<KEY>) => Promise<void> | void;
21
+
22
+ export type ComponentHandler<
23
+ KEY extends string = string,
24
+ VALUE extends ObjectLiteral = ObjectLiteral,
25
+ > = {
26
+ setup?: () => Promise<void> | void;
27
+ handle: ComponentHandlerFn<KEY, VALUE>
28
+ };
@@ -6,5 +6,6 @@
6
6
  */
7
7
 
8
8
  export * from './error';
9
+ export * from './handler';
9
10
  export * from './helper';
10
11
  export * from './type';
@@ -13,7 +13,7 @@ import { isQueueRouterPayload } from './helpers';
13
13
  import type {
14
14
  QueueRouterHandler,
15
15
  QueueRouterHandlers,
16
- QueueRouterPayload, QueueRouterRouting,
16
+ QueueRouterPayload, QueueRouterPublishOptions, QueueRouterRouting,
17
17
  } from './types';
18
18
 
19
19
  export class QueueRouter {
@@ -27,7 +27,12 @@ export class QueueRouter {
27
27
 
28
28
  //----------------------------------------------------------------
29
29
 
30
- publish(message: QueueRouterPayload) : Promise<boolean> {
30
+ publish(
31
+ message: QueueRouterPayload,
32
+ options: QueueRouterPublishOptions = {},
33
+ ) : Promise<boolean> {
34
+ options.logging ??= true;
35
+
31
36
  let exchange : Client;
32
37
  if (message.metadata.routing.type === 'work') {
33
38
  exchange = this.driver.of({
@@ -41,7 +46,10 @@ export class QueueRouter {
41
46
  });
42
47
  }
43
48
 
44
- if (isLoggerUsable()) {
49
+ if (
50
+ options.logging &&
51
+ isLoggerUsable()
52
+ ) {
45
53
  useLogger()
46
54
  .debug(`Publishing queue message ${message.type} in ${message.metadata.routing.key}`);
47
55
  }
@@ -54,6 +62,15 @@ export class QueueRouter {
54
62
  });
55
63
  }
56
64
 
65
+ consumeAny(
66
+ routing: QueueRouterRouting,
67
+ fn: (payload: QueueRouterPayload) => Promise<void> | void,
68
+ ) {
69
+ return this.consume(routing, {
70
+ $any: (payload) => fn(payload),
71
+ });
72
+ }
73
+
57
74
  consume(routing: QueueRouterRouting, handlers: QueueRouterHandlers) : Promise<void> {
58
75
  let exchange : Client;
59
76
  if (routing.type === 'work') {
@@ -113,7 +130,16 @@ export class QueueRouter {
113
130
  return;
114
131
  }
115
132
 
116
- await handler(payload);
133
+ try {
134
+ await handler(payload);
135
+ } catch (e) {
136
+ if (isLoggerUsable()) {
137
+ useLogger()
138
+ .error(e);
139
+ }
140
+
141
+ throw e;
142
+ }
117
143
  },
118
144
  });
119
145
  }
@@ -26,6 +26,10 @@ export type QueueRouterPayloadMetadata = {
26
26
  routing: QueueRouterRouting
27
27
  };
28
28
 
29
+ export type QueueRouterPublishOptions = {
30
+ logging?: boolean
31
+ };
32
+
29
33
  export type QueueRouterPayload<
30
34
  T = string,
31
35
  D = Record<string, any>,
@@ -36,18 +40,21 @@ export type QueueRouterPayload<
36
40
  metadata: QueueRouterPayloadMetadata
37
41
  };
38
42
 
39
- export type QueueRouterPayloadInput<T = string, D = Record<string, any>> = {
43
+ export type QueueRouterPayloadInput<
44
+ KEY = string,
45
+ VALUE = Record<string, any>,
46
+ > = {
40
47
  id?: string,
41
- type: T,
42
- data?: D,
48
+ type: KEY,
49
+ data?: VALUE,
43
50
  metadata: Partial<Pick<QueueRouterPayloadMetadata, 'timestamp'>> &
44
51
  Omit<QueueRouterPayloadMetadata, 'timestamp'>
45
52
  };
46
53
 
47
54
  export type QueueRouterHandler<
48
- T = string,
55
+ KEY = string,
49
56
  D = Record<string, any>,
50
- > = (message: QueueRouterPayload<T, D>) => Promise<void> | void;
57
+ > = (message: QueueRouterPayload<KEY, D>) => Promise<void> | void;
51
58
 
52
59
  export type QueueRouterHandlers<
53
60
  R extends Record<string, Record<string, any>> = Record<string, Record<string, any>>,
package/src/index.ts CHANGED
@@ -5,6 +5,7 @@
5
5
  * view the LICENSE file that was distributed with this source code.
6
6
  */
7
7
 
8
+ export * from './constants';
8
9
  export * from './core';
9
10
  export * from './services';
10
11
  export * from './type';
@@ -6,3 +6,4 @@
6
6
  */
7
7
 
8
8
  export * from './module';
9
+ export * from './singleton';
@@ -1,26 +1,12 @@
1
1
  /*
2
- * Copyright (c) 2024.
2
+ * Copyright (c) 2025.
3
3
  * Author Peter Placzek (tada5hi)
4
4
  * For the full copyright and license information,
5
5
  * view the LICENSE file that was distributed with this source code.
6
6
  */
7
7
 
8
- import type { Client } from 'amqp-extension';
9
- import type { Factory } from 'singa';
10
- import { singa } from 'singa';
8
+ import { Client as AmqpClient } from 'amqp-extension';
11
9
 
12
- const instance = singa<Client>({
13
- name: 'amqp',
14
- });
15
-
16
- export function setAmqpClientFactory(factory: Factory<Client>) {
17
- instance.setFactory(factory);
18
- }
19
-
20
- export function isAmqpClientUsable() {
21
- return instance.has() || instance.hasFactory();
22
- }
23
-
24
- export function useAmqpClient() {
25
- return instance.use();
26
- }
10
+ export {
11
+ AmqpClient,
12
+ };
@@ -5,22 +5,22 @@
5
5
  * view the LICENSE file that was distributed with this source code.
6
6
  */
7
7
 
8
+ import type { Client } from 'amqp-extension';
8
9
  import type { Factory } from 'singa';
9
10
  import { singa } from 'singa';
10
- import type { LokiClient } from '@hapic/loki';
11
11
 
12
- const instance = singa<LokiClient>({
13
- name: 'loki',
12
+ const instance = singa<Client>({
13
+ name: 'amqp',
14
14
  });
15
15
 
16
- export function setLokiFactory(factory: Factory<LokiClient>) {
16
+ export function setAmqpClientFactory(factory: Factory<Client>) {
17
17
  instance.setFactory(factory);
18
18
  }
19
19
 
20
- export function isLokiClientUsable() {
20
+ export function isAmqpClientUsable() {
21
21
  return instance.has() || instance.hasFactory();
22
22
  }
23
23
 
24
- export function useLokiClient() {
24
+ export function useAmqpClient() {
25
25
  return instance.use();
26
26
  }
@@ -0,0 +1,9 @@
1
+ /*
2
+ * Copyright (c) 2025.
3
+ * Author Peter Placzek (tada5hi)
4
+ * For the full copyright and license information,
5
+ * view the LICENSE file that was distributed with this source code.
6
+ */
7
+
8
+ export * from './redis';
9
+ export * from './socket';
@@ -0,0 +1,28 @@
1
+ /*
2
+ * Copyright (c) 2025.
3
+ * Author Peter Placzek (tada5hi)
4
+ * For the full copyright and license information,
5
+ * view the LICENSE file that was distributed with this source code.
6
+ */
7
+
8
+ function stringify(input: string | string[]) {
9
+ return typeof input === 'string' ? input : input.join('/');
10
+ }
11
+
12
+ export function buildEntityEventRedisChannel(
13
+ channel: string | string[],
14
+ namespace?: string | string[],
15
+ ) {
16
+ const channelNormalized = stringify(channel);
17
+
18
+ let namespaceNormalized : string | undefined;
19
+ if (namespace) {
20
+ namespaceNormalized = stringify(namespace);
21
+ }
22
+
23
+ if (typeof namespaceNormalized === 'undefined') {
24
+ return channelNormalized;
25
+ }
26
+
27
+ return `${namespaceNormalized}/${channelNormalized}`;
28
+ }
@@ -6,3 +6,4 @@
6
6
  */
7
7
 
8
8
  export * from './module';
9
+ export * from './helpers';
@@ -0,0 +1,44 @@
1
+ /*
2
+ * Copyright (c) 2023-2024.
3
+ * Author Peter Placzek (tada5hi)
4
+ * For the full copyright and license information,
5
+ * view the LICENSE file that was distributed with this source code.
6
+ */
7
+
8
+ import type { Client } from 'redis-extension';
9
+ import type { DomainEventRecord } from '@privateaim/kit';
10
+ import type { EntityEventHandleOptions, IEntityEventHandler } from '../../types';
11
+ import { transformEntityEventData } from '../../utils';
12
+ import { buildEntityEventRedisChannel } from './helpers';
13
+
14
+ export class EntityEventRedisHandler implements IEntityEventHandler {
15
+ protected driver : Client;
16
+
17
+ constructor(client: Client) {
18
+ this.driver = client;
19
+ }
20
+
21
+ async handle(ctx: EntityEventHandleOptions) : Promise<void> {
22
+ const payload : DomainEventRecord = {
23
+ type: ctx.metadata.ref_type,
24
+ event: ctx.metadata.event,
25
+ data: transformEntityEventData(ctx.data),
26
+ };
27
+
28
+ const payloadSerialized = JSON.stringify(payload);
29
+
30
+ const pipeline = this.driver.pipeline();
31
+ for (let i = 0; i < ctx.destinations.length; i++) {
32
+ const destination = ctx.destinations[i];
33
+
34
+ const key = buildEntityEventRedisChannel(
35
+ destination.channel,
36
+ destination.namespace,
37
+ );
38
+
39
+ pipeline.publish(key, payloadSerialized);
40
+ }
41
+
42
+ await pipeline.exec();
43
+ }
44
+ }
@@ -0,0 +1,72 @@
1
+ /*
2
+ * Copyright (c) 2022-2024.
3
+ * Author Peter Placzek (tada5hi)
4
+ * For the full copyright and license information,
5
+ * view the LICENSE file that was distributed with this source code.
6
+ */
7
+
8
+ import { buildDomainEventFullName } from '@privateaim/kit';
9
+ import { Emitter } from '@socket.io/redis-emitter';
10
+ import type { Client } from 'redis-extension';
11
+ import type { EntityEventHandleOptions, IEntityEventHandler } from '../../types';
12
+ import { transformEntityEventData } from '../../utils';
13
+
14
+ export class EntityEventSocketHandler implements IEntityEventHandler {
15
+ protected client : Client;
16
+
17
+ constructor(client: Client) {
18
+ this.client = client;
19
+ }
20
+
21
+ async handle(ctx: EntityEventHandleOptions) : Promise<void> {
22
+ ctx.data = transformEntityEventData(ctx.data);
23
+
24
+ for (let i = 0; i < ctx.destinations.length; i++) {
25
+ const destination = ctx.destinations[i];
26
+
27
+ const namespace = this.buildNamespace(destination.namespace);
28
+ const roomName = this.buildChannel(destination.channel);
29
+
30
+ const fullEventName = buildDomainEventFullName(
31
+ ctx.metadata.ref_type,
32
+ ctx.metadata.event,
33
+ );
34
+
35
+ const emitter = new Emitter(this.client, {}, namespace);
36
+
37
+ emitter
38
+ .in(roomName)
39
+ .emit(fullEventName, {
40
+ data: {
41
+ data: ctx.data,
42
+ type: ctx.metadata.ref_type,
43
+ event: ctx.metadata.event,
44
+ },
45
+ meta: {
46
+ namespace,
47
+ roomName,
48
+ },
49
+ });
50
+ }
51
+ }
52
+
53
+ protected buildNamespace(namespace?: string | string[]) {
54
+ if (typeof namespace === 'undefined') {
55
+ return '/';
56
+ }
57
+
58
+ if (typeof namespace === 'string') {
59
+ return namespace.startsWith('/') ? namespace : `/${namespace}`;
60
+ }
61
+
62
+ return `/${namespace.join('/')}`;
63
+ }
64
+
65
+ protected buildChannel(channel: string | string[]) {
66
+ if (typeof channel === 'string') {
67
+ return channel;
68
+ }
69
+
70
+ return channel.join('/');
71
+ }
72
+ }
@@ -6,7 +6,6 @@
6
6
  */
7
7
 
8
8
  export * from './module';
9
- export * from './redis';
9
+ export * from './handlers';
10
10
  export * from './singleton';
11
- export * from './socket';
12
11
  export * from './types';