@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
@@ -0,0 +1,75 @@
1
+ /*
2
+ * Copyright (c) 2024-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 '@privateaim/kit';
9
+ import { buildDomainEventFullName } from '@privateaim/kit';
10
+ import { isLoggerUsable, useLogger } from '../logger';
11
+ import type {
12
+ EntityEventDestination,
13
+ EntityEventHandleOptions,
14
+ EntityEventPublishOptions,
15
+ IEntityEventHandler,
16
+ IEntityEventPublisher,
17
+ } from './types';
18
+
19
+ export class EntityEventPublisher implements IEntityEventPublisher {
20
+ protected handlers : Set<IEntityEventHandler>;
21
+
22
+ constructor() {
23
+ this.handlers = new Set<IEntityEventHandler>();
24
+ }
25
+
26
+ register(consumer: IEntityEventHandler) {
27
+ this.handlers.add(consumer);
28
+ }
29
+
30
+ async safePublish<T extends ObjectLiteral = ObjectLiteral>(
31
+ ctx: EntityEventPublishOptions<T>,
32
+ ) : Promise<void> {
33
+ try {
34
+ await this.publish(ctx);
35
+ } catch (e) {
36
+ if (isLoggerUsable()) {
37
+ useLogger().error(`Publishing event ${buildDomainEventFullName(ctx.metadata.ref_type, ctx.metadata.event)} failed`);
38
+ useLogger().error(e);
39
+ }
40
+ }
41
+ }
42
+
43
+ async publish<T extends ObjectLiteral = ObjectLiteral>(
44
+ ctx: EntityEventPublishOptions<T>,
45
+ ) : Promise<void> {
46
+ if (isLoggerUsable()) {
47
+ useLogger()
48
+ .debug(
49
+ `Publishing event ${buildDomainEventFullName(ctx.metadata.ref_type, ctx.metadata.event)}`,
50
+ );
51
+ }
52
+
53
+ let destinations : EntityEventDestination[] = [];
54
+ if (typeof ctx.destinations === 'function') {
55
+ destinations = ctx.destinations(ctx.data);
56
+ } else {
57
+ destinations = ctx.destinations;
58
+ }
59
+
60
+ const consumeContext : EntityEventHandleOptions = {
61
+ ...ctx,
62
+ destinations,
63
+ };
64
+
65
+ const handlers = this.handlers.values();
66
+ while (true) {
67
+ const handler = handlers.next();
68
+ if (handler.done) {
69
+ return;
70
+ }
71
+
72
+ await (handler.value as IEntityEventHandler).handle(consumeContext);
73
+ }
74
+ }
75
+ }
@@ -0,0 +1,22 @@
1
+ /*
2
+ * Copyright (c) 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 { Factory } from 'singa';
9
+ import { singa } from 'singa';
10
+ import type { EntityEventPublisher } from './module';
11
+
12
+ const instance = singa<EntityEventPublisher>({
13
+ name: 'entityEventPublisher',
14
+ });
15
+
16
+ export function setEntityEventPublisherFactory(factory: Factory<EntityEventPublisher>) {
17
+ instance.setFactory(factory);
18
+ }
19
+
20
+ export function useEntityEventPublisher() {
21
+ return instance.use();
22
+ }
@@ -0,0 +1,61 @@
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 type { ObjectLiteral } from '@privateaim/kit';
9
+
10
+ export type EntityEventDestination = {
11
+ namespace?: string | string[],
12
+ channel: string | string[]
13
+ };
14
+
15
+ export type EntityEventDestinations = EntityEventDestination[];
16
+
17
+ export type EntityEventDestinationsFn<
18
+ T extends ObjectLiteral =ObjectLiteral,
19
+ > = (data: T) => EntityEventDestination[];
20
+
21
+ export type EntityEventMetadata = {
22
+ ref_type: string,
23
+ ref_id?: string,
24
+
25
+ event: string,
26
+
27
+ request_path?: string | null,
28
+ request_method?: string | null;
29
+ request_ip_address?: string | null;
30
+ request_user_agent?: string | null;
31
+
32
+ actor_type?: string | null;
33
+ actor_id?: string | null;
34
+ actor_name?: string | null;
35
+ };
36
+
37
+ export type EntityEventPublishOptions<
38
+ T extends ObjectLiteral = ObjectLiteral,
39
+ > = {
40
+ data: T,
41
+ dataPrevious?: T,
42
+ metadata: EntityEventMetadata,
43
+ destinations: EntityEventDestinations | EntityEventDestinationsFn<T>
44
+ };
45
+
46
+ export interface IEntityEventPublisher {
47
+ publish(ctx: EntityEventPublishOptions) : Promise<void>;
48
+ }
49
+
50
+ export type EntityEventHandleOptions<
51
+ T extends ObjectLiteral = ObjectLiteral,
52
+ > = {
53
+ data: T,
54
+ dataPrevious?: T,
55
+ metadata: EntityEventMetadata,
56
+ destinations: EntityEventDestinations
57
+ };
58
+
59
+ export interface IEntityEventHandler {
60
+ handle(ctx: EntityEventHandleOptions) : Promise<void>;
61
+ }
@@ -6,9 +6,8 @@
6
6
  */
7
7
 
8
8
  import { isObject } from '@privateaim/kit';
9
- import type { DomainEventChannelName } from './types';
10
9
 
11
- export function transformEventData<T>(input: T) : T {
10
+ export function transformEntityEventData<T>(input: T) : T {
12
11
  if (isObject(input)) {
13
12
  const keys = Object.keys(input);
14
13
  for (let i = 0; i < keys.length; i++) {
@@ -21,14 +20,3 @@ export function transformEventData<T>(input: T) : T {
21
20
 
22
21
  return input;
23
22
  }
24
-
25
- export function buildEventChannelName(
26
- input: DomainEventChannelName,
27
- id?: string,
28
- ) : string {
29
- if (typeof input === 'string') {
30
- return input;
31
- }
32
-
33
- return input(id);
34
- }
@@ -9,9 +9,7 @@ export * from './amqp';
9
9
  export * from './authup';
10
10
  export * from './authup-client-authentication-hook';
11
11
  export * from './cache';
12
- export * from './domain-event';
13
- export * from './loki';
14
- export * from './log-store';
12
+ export * from './entity-event';
15
13
  export * from './logger';
16
14
  export * from './redis';
17
15
  export * from './vault';
@@ -8,3 +8,4 @@
8
8
  export * from './module';
9
9
  export * from './types';
10
10
  export * from './singleton';
11
+ export * from './transports';
@@ -5,57 +5,19 @@
5
5
  * view the LICENSE file that was distributed with this source code.
6
6
  */
7
7
 
8
- import { read } from 'envix';
9
- import path from 'node:path';
10
- import { EnvironmentName } from 'typeorm-extension';
11
- import type { Logger } from 'winston';
12
- import { createLogger as create, format, transports } from 'winston';
13
- import type { LoggerCreateContext, LoggerTransports } from './types';
14
- import { LogStoreTransport } from './store';
15
- import { useLogStore } from '../log-store';
8
+ import { createLogger as create, format } from 'winston';
9
+ import { createLoggerConsoleTransport } from './transports';
10
+ import type {
11
+ Logger, LoggerCreateContext, LoggerTransport,
12
+ } from './types';
16
13
 
17
14
  export function createLogger(ctx: LoggerCreateContext = {}) : Logger {
18
- const store = ctx.store || useLogStore();
15
+ const transports : LoggerTransport[] = [
16
+ ...(ctx.transports || []),
17
+ ];
19
18
 
20
- let loggerTransports : LoggerTransports;
21
- if (read('env') === EnvironmentName.PRODUCTION) {
22
- loggerTransports = [
23
- new transports.Console({
24
- level: 'info',
25
- }),
26
- new transports.File({
27
- filename: path.join(ctx.directory || process.cwd(), 'access.log'),
28
- level: 'http',
29
- maxsize: 10 * 1024 * 1024, // 10MB
30
- maxFiles: 5,
31
- }),
32
- new transports.File({
33
- filename: path.join(ctx.directory || process.cwd(), 'error.log'),
34
- level: 'warn',
35
- maxsize: 10 * 1024 * 1024, // 10MB
36
- maxFiles: 5,
37
- }),
38
- new LogStoreTransport(
39
- store,
40
- {
41
- level: 'http',
42
- labels: ctx.labels,
43
- },
44
- ),
45
- ];
46
- } else {
47
- loggerTransports = [
48
- new transports.Console({
49
- level: 'debug',
50
- }),
51
- new LogStoreTransport(
52
- store,
53
- {
54
- level: 'http',
55
- labels: ctx.labels,
56
- },
57
- ),
58
- ];
19
+ if (transports.length === 0) {
20
+ transports.push(createLoggerConsoleTransport());
59
21
  }
60
22
 
61
23
  return create({
@@ -65,7 +27,7 @@ export function createLogger(ctx: LoggerCreateContext = {}) : Logger {
65
27
  format.simple(),
66
28
  ),
67
29
  level: 'debug',
68
- transports: loggerTransports,
30
+ transports,
69
31
  // todo: deeply merge options
70
32
  ...(ctx.options || {}),
71
33
  });
@@ -6,7 +6,7 @@
6
6
  */
7
7
 
8
8
  import { type Factory, singa } from 'singa';
9
- import type { Logger } from 'winston';
9
+ import type { Logger } from './types';
10
10
 
11
11
  const instance = singa<Logger>({
12
12
  name: 'logger',
@@ -0,0 +1,27 @@
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 { read } from 'envix';
9
+ import { transports } from 'winston';
10
+ import type { TransportStreamOptions } from 'winston-transport';
11
+ import { EnvironmentName } from '../../../constants';
12
+ import type { LoggerTransport } from '../types';
13
+
14
+ export class LoggerConsoleTransport extends transports.Console {
15
+ constructor(options: TransportStreamOptions = {}) {
16
+ super({
17
+ level: read('env') === EnvironmentName.PRODUCTION ?
18
+ 'info' :
19
+ 'debug',
20
+ ...options,
21
+ });
22
+ }
23
+ }
24
+
25
+ export function createLoggerConsoleTransport(options: TransportStreamOptions = {}) : LoggerTransport {
26
+ return new LoggerConsoleTransport(options);
27
+ }
@@ -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 './console';
9
+ export * from './memory';
@@ -0,0 +1,18 @@
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 Transport from 'winston-transport';
9
+
10
+ export class LoggerMemoryTransport extends Transport {
11
+ public items : Record<PropertyKey, any>[] = [];
12
+
13
+ log(info: Record<PropertyKey, any>, next: () => void): any {
14
+ this.items.push(info);
15
+
16
+ next();
17
+ }
18
+ }
@@ -5,18 +5,37 @@
5
5
  * view the LICENSE file that was distributed with this source code.
6
6
  */
7
7
 
8
- import type { Logger, LoggerOptions } from 'winston';
9
- import type { LogStore } from '../log-store';
8
+ import type Transport from 'winston-transport';
9
+ import type { LoggerOptions } from 'winston';
10
+
11
+ export type LoggerTransport = Transport;
10
12
 
11
13
  export type LoggerCreateContext = {
12
- directory?: string,
13
14
  options?: Partial<LoggerOptions>,
14
- store?: LogStore,
15
- labels?: Record<string, string>,
15
+ transports?: LoggerTransport[]
16
+ };
17
+
18
+ type LogEntry = {
19
+ /**
20
+ * Log message.
21
+ */
22
+ message: string | Error,
23
+ [key: string]: any
16
24
  };
17
25
 
18
- export type LoggerTransports = LoggerOptions['transports'];
26
+ export interface LoggerLevelFn<OUT = any> {
27
+ (message: string, ...meta: Omit<LogEntry, 'message'>[]): OUT,
28
+ (message: LogEntry): OUT,
29
+ (message:string): OUT,
30
+ }
19
31
 
20
- export type {
21
- Logger,
32
+ export type Logger = {
33
+ emerg: LoggerLevelFn<Logger>,
34
+ alert: LoggerLevelFn<Logger>,
35
+ crit: LoggerLevelFn<Logger>,
36
+ error: LoggerLevelFn<Logger>,
37
+ warn: LoggerLevelFn<Logger>,
38
+ notice: LoggerLevelFn<Logger>,
39
+ info: LoggerLevelFn<Logger>,
40
+ debug: LoggerLevelFn<Logger>,
22
41
  };
@@ -0,0 +1,16 @@
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 class CustomError extends Error {
9
+ code: string;
10
+
11
+ constructor(message: string, options: ErrorOptions = {}) {
12
+ super(message, options);
13
+
14
+ this.code = 'foo';
15
+ }
16
+ }
@@ -1,36 +1,57 @@
1
1
  /*
2
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.
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
6
  */
7
7
 
8
- import { wait } from '@privateaim/kit';
9
- import { MemoryLogStore, createLogger } from '../../src';
8
+ import { createLogger } from 'winston';
9
+ import { LoggerMemoryTransport } from '../../src';
10
+ import { CustomError } from '../data/error';
10
11
 
11
- describe('logger', () => {
12
- it('should work with store', async () => {
13
- const store = new MemoryLogStore();
14
- store.setLabels({ app: 'app' });
12
+ describe('src/logger', () => {
13
+ it('should log simple message', () => {
14
+ const memoryTransport = new LoggerMemoryTransport();
15
15
 
16
16
  const logger = createLogger({
17
- store,
17
+ transports: [
18
+ memoryTransport,
19
+ ],
18
20
  });
19
21
 
20
- logger.info('foo', { meta: 'bar' });
22
+ logger.info('This is a log message', {
23
+ foo: 'bar',
24
+ });
21
25
 
22
- await wait(0);
26
+ const [item] = memoryTransport.items;
27
+ expect(item).toBeDefined();
28
+ expect(item.message).toEqual('This is a log message');
29
+ expect(item.foo).toEqual('bar');
30
+ });
23
31
 
24
- expect(store.items).toHaveLength(1);
32
+ it('should log error', () => {
33
+ const memoryTransport = new LoggerMemoryTransport();
25
34
 
26
- const [item] = store.items;
35
+ const logger = createLogger({
36
+ transports: [
37
+ memoryTransport,
38
+ ],
39
+ });
27
40
 
28
- expect(item.level).toEqual('info');
29
- expect(item.message).toEqual('foo');
30
- expect(item.labels).toEqual({
31
- app: 'app',
32
- meta: 'bar',
41
+ const error = new CustomError('This is a log message');
42
+ logger.info({
43
+ message: error,
44
+ foo: 'bar',
33
45
  });
34
- expect(item.time).toBeDefined();
46
+
47
+ const [item] = memoryTransport.items;
48
+ expect(item).toBeDefined();
49
+
50
+ expect(item.message?.message).toEqual(error.message);
51
+ expect(item.message?.stack).toEqual(error.stack);
52
+ expect(item.message?.code).toEqual(error.code);
53
+
54
+ expect(item.foo).toEqual('bar');
55
+ expect(item.code).toBeUndefined();
35
56
  });
36
57
  });
@@ -1,6 +0,0 @@
1
- export * from './module';
2
- export * from './redis';
3
- export * from './singleton';
4
- export * from './socket';
5
- export * from './types';
6
- //# sourceMappingURL=index.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/services/domain-event/index.ts"],"names":[],"mappings":"AAOA,cAAc,UAAU,CAAC;AACzB,cAAc,SAAS,CAAC;AACxB,cAAc,aAAa,CAAC;AAC5B,cAAc,UAAU,CAAC;AACzB,cAAc,SAAS,CAAC"}
@@ -1,10 +0,0 @@
1
- import type { ObjectLiteral } from '@privateaim/kit';
2
- import type { DomainEventPublishOptions, IDomainEventPublisher } from './types';
3
- export declare class DomainEventPublisher implements IDomainEventPublisher {
4
- protected publishers: Set<IDomainEventPublisher>;
5
- constructor();
6
- addPublisher(publisher: IDomainEventPublisher): void;
7
- safePublish<T extends ObjectLiteral = ObjectLiteral>(ctx: DomainEventPublishOptions<T>): Promise<void>;
8
- publish<T extends ObjectLiteral = ObjectLiteral>(ctx: DomainEventPublishOptions<T>): Promise<void>;
9
- }
10
- //# sourceMappingURL=module.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"module.d.ts","sourceRoot":"","sources":["../../../src/services/domain-event/module.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAGrD,OAAO,KAAK,EAAE,yBAAyB,EAAE,qBAAqB,EAAE,MAAM,SAAS,CAAC;AAEhF,qBAAa,oBAAqB,YAAW,qBAAqB;IAC9D,SAAS,CAAC,UAAU,EAAG,GAAG,CAAC,qBAAqB,CAAC,CAAC;;IAMlD,YAAY,CAAC,SAAS,EAAE,qBAAqB;IAIvC,WAAW,CAAC,CAAC,SAAS,aAAa,GAAG,aAAa,EACrD,GAAG,EAAE,yBAAyB,CAAC,CAAC,CAAC,GACjC,OAAO,CAAC,IAAI,CAAC;IAWX,OAAO,CAAC,CAAC,SAAS,aAAa,GAAG,aAAa,EACjD,GAAG,EAAE,yBAAyB,CAAC,CAAC,CAAC,GACjC,OAAO,CAAC,IAAI,CAAC;CAepB"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/services/domain-event/redis/index.ts"],"names":[],"mappings":"AAOA,cAAc,UAAU,CAAC"}
@@ -1,8 +0,0 @@
1
- import type { Client } from 'redis-extension';
2
- import type { DomainEventPublishOptions, IDomainEventPublisher } from '../types';
3
- export declare class DomainEventRedisPublisher implements IDomainEventPublisher {
4
- protected driver: Client;
5
- constructor(client: Client);
6
- publish(ctx: DomainEventPublishOptions): Promise<void>;
7
- }
8
- //# sourceMappingURL=module.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"module.d.ts","sourceRoot":"","sources":["../../../../src/services/domain-event/redis/module.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAC;AAE9C,OAAO,KAAK,EAAE,yBAAyB,EAAE,qBAAqB,EAAE,MAAM,UAAU,CAAC;AAGjF,qBAAa,yBAA0B,YAAW,qBAAqB;IACnE,SAAS,CAAC,MAAM,EAAG,MAAM,CAAC;gBAEd,MAAM,EAAE,MAAM;IAIpB,OAAO,CAAC,GAAG,EAAE,yBAAyB,GAAI,OAAO,CAAC,IAAI,CAAC;CAoChE"}
@@ -1,4 +0,0 @@
1
- import { DomainEventPublisher } from './module';
2
- export declare function useDomainEventPublisherSinga(): import("singa").Singa<DomainEventPublisher>;
3
- export declare function useDomainEventPublisher(): DomainEventPublisher;
4
- //# sourceMappingURL=singleton.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"singleton.d.ts","sourceRoot":"","sources":["../../../src/services/domain-event/singleton.ts"],"names":[],"mappings":"AASA,OAAO,EAAE,oBAAoB,EAAE,MAAM,UAAU,CAAC;AAoBhD,wBAAgB,4BAA4B,gDAE3C;AAED,wBAAgB,uBAAuB,yBAEtC"}
@@ -1,2 +0,0 @@
1
- export * from './module';
2
- //# sourceMappingURL=index.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/services/domain-event/socket/index.ts"],"names":[],"mappings":"AAOA,cAAc,UAAU,CAAC"}
@@ -1,8 +0,0 @@
1
- import type { Client } from 'redis-extension';
2
- import type { DomainEventPublishOptions, IDomainEventPublisher } from '../types';
3
- export declare class DomainEventSocketPublisher implements IDomainEventPublisher {
4
- protected client: Client;
5
- constructor(client: Client);
6
- publish(ctx: DomainEventPublishOptions): Promise<void>;
7
- }
8
- //# sourceMappingURL=module.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"module.d.ts","sourceRoot":"","sources":["../../../../src/services/domain-event/socket/module.ts"],"names":[],"mappings":"AASA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAC;AAC9C,OAAO,KAAK,EAAE,yBAAyB,EAAE,qBAAqB,EAAE,MAAM,UAAU,CAAC;AAGjF,qBAAa,0BAA2B,YAAW,qBAAqB;IACpE,SAAS,CAAC,MAAM,EAAG,MAAM,CAAC;gBAEd,MAAM,EAAE,MAAM;IAIpB,OAAO,CAAC,GAAG,EAAE,yBAAyB,GAAI,OAAO,CAAC,IAAI,CAAC;CA+ChE"}
@@ -1,29 +0,0 @@
1
- import type { ObjectLiteral } from '@privateaim/kit';
2
- export type DomainEventChannelName = string | ((id?: string) => string);
3
- export type DomainEventNamespaceName<T extends ObjectLiteral = ObjectLiteral> = string | ((data: T) => string);
4
- export type DomainEventDestination<T extends ObjectLiteral = ObjectLiteral> = {
5
- namespace?: DomainEventNamespaceName<T>;
6
- channel: DomainEventChannelName;
7
- };
8
- export type DomainEventDestinations<T extends ObjectLiteral = ObjectLiteral> = DomainEventDestination<T>[];
9
- export type DomainEventIdentity = {
10
- id?: string;
11
- name?: string;
12
- type?: string;
13
- ipAddress?: string;
14
- };
15
- export type DomainEventMetadata = {
16
- domain: string;
17
- event: string;
18
- identity?: DomainEventIdentity;
19
- };
20
- export type DomainEventPublishOptions<T extends ObjectLiteral = ObjectLiteral> = {
21
- data: T;
22
- dataPrevious?: T;
23
- metadata: DomainEventMetadata;
24
- destinations: DomainEventDestinations<T['data']>;
25
- };
26
- export interface IDomainEventPublisher {
27
- publish(ctx: DomainEventPublishOptions): Promise<void>;
28
- }
29
- //# sourceMappingURL=types.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/services/domain-event/types.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAErD,MAAM,MAAM,sBAAsB,GAAG,MAAM,GAAG,CAAC,CAAC,EAAE,CAAC,EAAE,MAAM,KAAK,MAAM,CAAC,CAAC;AACxE,MAAM,MAAM,wBAAwB,CAChC,CAAC,SAAS,aAAa,GAAG,aAAa,IACvC,MAAM,GAAG,CAAC,CAAC,IAAI,EAAE,CAAC,KAAK,MAAM,CAAC,CAAC;AAEnC,MAAM,MAAM,sBAAsB,CAC9B,CAAC,SAAS,aAAa,GAAG,aAAa,IACvC;IACA,SAAS,CAAC,EAAE,wBAAwB,CAAC,CAAC,CAAC,CAAC;IACxC,OAAO,EAAE,sBAAsB,CAAA;CAClC,CAAC;AAEF,MAAM,MAAM,uBAAuB,CACnC,CAAC,SAAS,aAAa,GAAE,aAAa,IAClC,sBAAsB,CAAC,CAAC,CAAC,EAAE,CAAC;AAEhC,MAAM,MAAM,mBAAmB,GAAG;IAC9B,EAAE,CAAC,EAAE,MAAM,CAAC;IACZ,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,SAAS,CAAC,EAAE,MAAM,CAAC;CACtB,CAAC;AAEF,MAAM,MAAM,mBAAmB,GAAG;IAC9B,MAAM,EAAE,MAAM,CAAC;IACf,KAAK,EAAE,MAAM,CAAC;IACd,QAAQ,CAAC,EAAE,mBAAmB,CAAC;CAClC,CAAC;AAEF,MAAM,MAAM,yBAAyB,CACjC,CAAC,SAAS,aAAa,GAAG,aAAa,IACvC;IACA,IAAI,EAAE,CAAC,CAAC;IACR,YAAY,CAAC,EAAE,CAAC,CAAC;IACjB,QAAQ,EAAE,mBAAmB,CAAC;IAC9B,YAAY,EAAE,uBAAuB,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAA;CACnD,CAAC;AAEF,MAAM,WAAW,qBAAqB;IAClC,OAAO,CAAC,GAAG,EAAE,yBAAyB,GAAI,OAAO,CAAC,IAAI,CAAC,CAAC;CAC3D"}
@@ -1,4 +0,0 @@
1
- import type { DomainEventChannelName } from './types';
2
- export declare function transformEventData<T>(input: T): T;
3
- export declare function buildEventChannelName(input: DomainEventChannelName, id?: string): string;
4
- //# sourceMappingURL=utils.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../../src/services/domain-event/utils.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,SAAS,CAAC;AAEtD,wBAAgB,kBAAkB,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,GAAI,CAAC,CAYlD;AAED,wBAAgB,qBAAqB,CACjC,KAAK,EAAE,sBAAsB,EAC7B,EAAE,CAAC,EAAE,MAAM,GACX,MAAM,CAMT"}
@@ -1,7 +0,0 @@
1
- export declare abstract class BaseLogStore {
2
- protected labels: Record<string, string>;
3
- setLabels(labels: Record<string, string>): void;
4
- getLabels(): Record<string, string>;
5
- extendLabels(labels: Record<string, string>): void;
6
- }
7
- //# sourceMappingURL=base.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"base.d.ts","sourceRoot":"","sources":["../../../../src/services/log-store/entities/base.ts"],"names":[],"mappings":"AAOA,8BAAsB,YAAY;IAC9B,SAAS,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAEzC,SAAS,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,IAAI;IAI/C,SAAS,IAAK,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC;IAIpC,YAAY,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,IAAI;CAMrD"}
@@ -1,3 +0,0 @@
1
- export * from './loki';
2
- export * from './memory';
3
- //# sourceMappingURL=index.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/services/log-store/entities/index.ts"],"names":[],"mappings":"AAOA,cAAc,QAAQ,CAAC;AACvB,cAAc,UAAU,CAAC"}
@@ -1,13 +0,0 @@
1
- import type { Log } from '@privateaim/kit';
2
- import type { LokiClient } from '../../loki';
3
- import type { LogInput, LogStore, LogStoreDeleteOptions, LogStoreQueryOptions } from '../types';
4
- import { BaseLogStore } from './base';
5
- export declare class LokiLogStore extends BaseLogStore implements LogStore {
6
- protected instance: LokiClient;
7
- constructor(instance: LokiClient, labels?: Record<string, string>);
8
- write(message: string | LogInput, labels?: Record<string, string>): Promise<Log>;
9
- delete(options: LogStoreDeleteOptions): Promise<void>;
10
- query(input: LogStoreQueryOptions): Promise<[Log[], number]>;
11
- protected buildQuery(labels: Record<string, any>): string;
12
- }
13
- //# sourceMappingURL=loki.d.ts.map