@privateaim/server-kit 0.8.8 → 0.8.9

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 (58) hide show
  1. package/CHANGELOG.md +19 -0
  2. package/dist/index.cjs +212 -113
  3. package/dist/index.cjs.map +1 -1
  4. package/dist/index.d.ts +0 -1
  5. package/dist/index.d.ts.map +1 -1
  6. package/dist/index.mjs +210 -112
  7. package/dist/index.mjs.map +1 -1
  8. package/dist/services/domain-event/index.d.ts +6 -0
  9. package/dist/services/domain-event/index.d.ts.map +1 -0
  10. package/dist/services/domain-event/module.d.ts +9 -0
  11. package/dist/services/domain-event/module.d.ts.map +1 -0
  12. package/dist/services/domain-event/redis/index.d.ts +2 -0
  13. package/dist/services/domain-event/redis/index.d.ts.map +1 -0
  14. package/dist/services/domain-event/redis/module.d.ts +8 -0
  15. package/dist/services/domain-event/redis/module.d.ts.map +1 -0
  16. package/dist/services/domain-event/singleton.d.ts +3 -0
  17. package/dist/services/domain-event/singleton.d.ts.map +1 -0
  18. package/dist/services/domain-event/socket/index.d.ts +2 -0
  19. package/dist/services/domain-event/socket/index.d.ts.map +1 -0
  20. package/dist/services/domain-event/socket/module.d.ts +8 -0
  21. package/dist/services/domain-event/socket/module.d.ts.map +1 -0
  22. package/dist/services/domain-event/type.d.ts +15 -0
  23. package/dist/services/domain-event/type.d.ts.map +1 -0
  24. package/dist/services/domain-event/utils.d.ts +4 -0
  25. package/dist/services/domain-event/utils.d.ts.map +1 -0
  26. package/dist/services/index.d.ts +1 -0
  27. package/dist/services/index.d.ts.map +1 -1
  28. package/package.json +4 -5
  29. package/src/index.ts +0 -1
  30. package/src/{domain-event → services/domain-event}/index.ts +2 -1
  31. package/src/services/domain-event/module.ts +40 -0
  32. package/src/{domain-event → services/domain-event}/redis/index.ts +1 -1
  33. package/src/services/domain-event/redis/module.ts +37 -0
  34. package/src/services/domain-event/singleton.ts +32 -0
  35. package/src/{domain-event → services/domain-event}/socket/index.ts +1 -1
  36. package/src/services/domain-event/socket/module.ts +57 -0
  37. package/src/{domain-event → services/domain-event}/type.ts +13 -0
  38. package/src/{domain-event → services/domain-event}/utils.ts +2 -2
  39. package/src/services/index.ts +1 -0
  40. package/dist/domain-event/index.d.ts +0 -5
  41. package/dist/domain-event/index.d.ts.map +0 -1
  42. package/dist/domain-event/publish.d.ts +0 -5
  43. package/dist/domain-event/publish.d.ts.map +0 -1
  44. package/dist/domain-event/redis/index.d.ts +0 -2
  45. package/dist/domain-event/redis/index.d.ts.map +0 -1
  46. package/dist/domain-event/redis/publish.d.ts +0 -5
  47. package/dist/domain-event/redis/publish.d.ts.map +0 -1
  48. package/dist/domain-event/socket/index.d.ts +0 -2
  49. package/dist/domain-event/socket/index.d.ts.map +0 -1
  50. package/dist/domain-event/socket/publish.d.ts +0 -5
  51. package/dist/domain-event/socket/publish.d.ts.map +0 -1
  52. package/dist/domain-event/type.d.ts +0 -7
  53. package/dist/domain-event/type.d.ts.map +0 -1
  54. package/dist/domain-event/utils.d.ts +0 -4
  55. package/dist/domain-event/utils.d.ts.map +0 -1
  56. package/src/domain-event/publish.ts +0 -21
  57. package/src/domain-event/redis/publish.ts +0 -41
  58. package/src/domain-event/socket/publish.ts +0 -60
@@ -0,0 +1,57 @@
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 { DomainEventPublishContext, IDomainEventPublisher } from '../type';
12
+ import { buildEventChannelName, transformEventData } from '../utils';
13
+
14
+ export class DomainEventSocketPublisher implements IDomainEventPublisher {
15
+ protected client : Client;
16
+
17
+ constructor(client: Client) {
18
+ this.client = client;
19
+ }
20
+
21
+ async publish(ctx: DomainEventPublishContext) : Promise<void> {
22
+ ctx.data = transformEventData(ctx.data);
23
+
24
+ for (let i = 0; i < ctx.destinations.length; i++) {
25
+ let namespace : string;
26
+ if (ctx.destinations[i].namespace) {
27
+ namespace = ctx.destinations[i].namespace;
28
+ } else {
29
+ namespace = '/';
30
+ }
31
+
32
+ const emitter = new Emitter(this.client, {}, namespace);
33
+
34
+ const fullEventName = buildDomainEventFullName(ctx.data.type, ctx.data.event);
35
+
36
+ const rooms : string[] = [
37
+ buildEventChannelName(ctx.destinations[i].channel),
38
+ ];
39
+
40
+ if (typeof ctx.destinations[i].channel === 'function') {
41
+ rooms.push(buildEventChannelName(ctx.destinations[i].channel, ctx.data.data.id));
42
+ }
43
+
44
+ for (let j = 0; j < rooms.length; j++) {
45
+ emitter
46
+ .in(rooms[j])
47
+ .emit(fullEventName, {
48
+ ...ctx.data,
49
+ meta: {
50
+ namespace,
51
+ roomName: rooms[j],
52
+ },
53
+ });
54
+ }
55
+ }
56
+ }
57
+ }
@@ -5,6 +5,8 @@
5
5
  * view the LICENSE file that was distributed with this source code.
6
6
  */
7
7
 
8
+ import type { DomainEventRecord } from '@privateaim/kit';
9
+
8
10
  export type DomainEventChannelName = string | ((id?: string | number) => string);
9
11
  export type DomainEventDestination = {
10
12
  namespace?: string,
@@ -12,3 +14,14 @@ export type DomainEventDestination = {
12
14
  };
13
15
 
14
16
  export type DomainEventDestinations = DomainEventDestination[];
17
+
18
+ export type DomainEventPublishContext<
19
+ T extends DomainEventRecord = DomainEventRecord,
20
+ > = {
21
+ data: T,
22
+ destinations: DomainEventDestinations
23
+ };
24
+
25
+ export interface IDomainEventPublisher {
26
+ publish(ctx: DomainEventPublishContext) : Promise<void>;
27
+ }
@@ -8,7 +8,7 @@
8
8
  import { isObject } from '@privateaim/kit';
9
9
  import type { DomainEventChannelName } from './type';
10
10
 
11
- export function transformDomainEventData<T>(input: T) : T {
11
+ export function transformEventData<T>(input: T) : T {
12
12
  if (isObject(input)) {
13
13
  const keys = Object.keys(input);
14
14
  for (let i = 0; i < keys.length; i++) {
@@ -22,7 +22,7 @@ export function transformDomainEventData<T>(input: T) : T {
22
22
  return input;
23
23
  }
24
24
 
25
- export function buildDomainEventChannelName(
25
+ export function buildEventChannelName(
26
26
  input: DomainEventChannelName,
27
27
  id?: string | number,
28
28
  ) : string {
@@ -9,6 +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';
12
13
  export * from './logger';
13
14
  export * from './redis';
14
15
  export * from './vault';
@@ -1,5 +0,0 @@
1
- export * from './publish';
2
- export * from './redis';
3
- export * from './socket';
4
- export * from './type';
5
- //# sourceMappingURL=index.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/domain-event/index.ts"],"names":[],"mappings":"AAOA,cAAc,WAAW,CAAC;AAC1B,cAAc,SAAS,CAAC;AACxB,cAAc,UAAU,CAAC;AACzB,cAAc,QAAQ,CAAC"}
@@ -1,5 +0,0 @@
1
- import type { DomainsEvents } from '@privateaim/core-kit';
2
- import type { Client } from 'redis-extension';
3
- import type { DomainEventDestinations } from './type';
4
- export declare function publishDomainEvent(client: Client, context: DomainsEvents, destinations: DomainEventDestinations): Promise<void>;
5
- //# sourceMappingURL=publish.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"publish.d.ts","sourceRoot":"","sources":["../../src/domain-event/publish.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAC;AAC1D,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAC;AAG9C,OAAO,KAAK,EAAE,uBAAuB,EAAE,MAAM,QAAQ,CAAC;AAEtD,wBAAsB,kBAAkB,CACpC,MAAM,EAAE,MAAM,EACd,OAAO,EAAE,aAAa,EACtB,YAAY,EAAE,uBAAuB,iBAIxC"}
@@ -1,2 +0,0 @@
1
- export * from './publish';
2
- //# sourceMappingURL=index.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/domain-event/redis/index.ts"],"names":[],"mappings":"AAOA,cAAc,WAAW,CAAC"}
@@ -1,5 +0,0 @@
1
- import type { DomainsEvents } from '@privateaim/core-kit';
2
- import type { Client } from 'redis-extension';
3
- import type { DomainEventDestinations } from '../type';
4
- export declare function publishDomainRedisEvent(client: Client, context: DomainsEvents, destinations: DomainEventDestinations): Promise<any>;
5
- //# sourceMappingURL=publish.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"publish.d.ts","sourceRoot":"","sources":["../../../src/domain-event/redis/publish.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAC;AAC1D,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAC;AAC9C,OAAO,KAAK,EAAE,uBAAuB,EAAE,MAAM,SAAS,CAAC;AAGvD,wBAAsB,uBAAuB,CACzC,MAAM,EAAE,MAAM,EACd,OAAO,EAAE,aAAa,EACtB,YAAY,EAAE,uBAAuB,GACrC,OAAO,CAAC,GAAG,CAAC,CAuBf"}
@@ -1,2 +0,0 @@
1
- export * from './publish';
2
- //# sourceMappingURL=index.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/domain-event/socket/index.ts"],"names":[],"mappings":"AAOA,cAAc,WAAW,CAAC"}
@@ -1,5 +0,0 @@
1
- import type { DomainsEvents } from '@privateaim/core-kit';
2
- import type { Client } from 'redis-extension';
3
- import type { DomainEventDestinations } from '../type';
4
- export declare function publishDomainSocketEvent(client: Client, context: DomainsEvents, destinations: DomainEventDestinations): void;
5
- //# sourceMappingURL=publish.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"publish.d.ts","sourceRoot":"","sources":["../../../src/domain-event/socket/publish.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAC;AAG1D,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAC;AAC9C,OAAO,KAAK,EAAE,uBAAuB,EAAE,MAAM,SAAS,CAAC;AAGvD,wBAAgB,wBAAwB,CACpC,MAAM,EAAE,MAAM,EACd,OAAO,EAAE,aAAa,EACtB,YAAY,EAAE,uBAAuB,QA0CxC"}
@@ -1,7 +0,0 @@
1
- export type DomainEventChannelName = string | ((id?: string | number) => string);
2
- export type DomainEventDestination = {
3
- namespace?: string;
4
- channel: DomainEventChannelName;
5
- };
6
- export type DomainEventDestinations = DomainEventDestination[];
7
- //# sourceMappingURL=type.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"type.d.ts","sourceRoot":"","sources":["../../src/domain-event/type.ts"],"names":[],"mappings":"AAOA,MAAM,MAAM,sBAAsB,GAAG,MAAM,GAAG,CAAC,CAAC,EAAE,CAAC,EAAE,MAAM,GAAG,MAAM,KAAK,MAAM,CAAC,CAAC;AACjF,MAAM,MAAM,sBAAsB,GAAG;IACjC,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,OAAO,EAAE,sBAAsB,CAAA;CAClC,CAAC;AAEF,MAAM,MAAM,uBAAuB,GAAG,sBAAsB,EAAE,CAAC"}
@@ -1,4 +0,0 @@
1
- import type { DomainEventChannelName } from './type';
2
- export declare function transformDomainEventData<T>(input: T): T;
3
- export declare function buildDomainEventChannelName(input: DomainEventChannelName, id?: string | number): string;
4
- //# sourceMappingURL=utils.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../src/domain-event/utils.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,QAAQ,CAAC;AAErD,wBAAgB,wBAAwB,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,GAAI,CAAC,CAYxD;AAED,wBAAgB,2BAA2B,CACvC,KAAK,EAAE,sBAAsB,EAC7B,EAAE,CAAC,EAAE,MAAM,GAAG,MAAM,GACpB,MAAM,CAMT"}
@@ -1,21 +0,0 @@
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 { DomainsEvents } from '@privateaim/core-kit';
9
- import type { Client } from 'redis-extension';
10
- import { publishDomainRedisEvent } from './redis';
11
- import { publishDomainSocketEvent } from './socket';
12
- import type { DomainEventDestinations } from './type';
13
-
14
- export async function publishDomainEvent(
15
- client: Client,
16
- context: DomainsEvents,
17
- destinations: DomainEventDestinations,
18
- ) {
19
- await publishDomainRedisEvent(client, context, destinations);
20
- publishDomainSocketEvent(client, context, destinations);
21
- }
@@ -1,41 +0,0 @@
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 { DomainEventName } from '@privateaim/core-kit';
9
- import type { DomainsEvents } from '@privateaim/core-kit';
10
- import type { Client } from 'redis-extension';
11
- import type { DomainEventDestinations } from '../type';
12
- import { buildDomainEventChannelName, transformDomainEventData } from '../utils';
13
-
14
- export async function publishDomainRedisEvent(
15
- client: Client,
16
- context: DomainsEvents,
17
- destinations: DomainEventDestinations,
18
- ) : Promise<any> {
19
- context = transformDomainEventData(context);
20
-
21
- const json = JSON.stringify(context);
22
-
23
- const pipeline = client.pipeline();
24
- for (let i = 0; i < destinations.length; i++) {
25
- const { namespace } = destinations[i];
26
- const keyPrefix = (namespace ? `${namespace}:` : '');
27
-
28
- let key = keyPrefix + buildDomainEventChannelName(destinations[i].channel);
29
- pipeline.publish(key, json);
30
-
31
- if (
32
- context.event !== DomainEventName.CREATED &&
33
- typeof destinations[i].channel === 'function'
34
- ) {
35
- key = keyPrefix + buildDomainEventChannelName(destinations[i].channel, context.data.id);
36
- pipeline.publish(key, json);
37
- }
38
- }
39
-
40
- return pipeline.exec();
41
- }
@@ -1,60 +0,0 @@
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 { DomainsEvents } from '@privateaim/core-kit';
9
- import { DomainEventName, buildDomainEventFullName } from '@privateaim/core-kit';
10
- import { Emitter } from '@socket.io/redis-emitter';
11
- import type { Client } from 'redis-extension';
12
- import type { DomainEventDestinations } from '../type';
13
- import { buildDomainEventChannelName, transformDomainEventData } from '../utils';
14
-
15
- export function publishDomainSocketEvent(
16
- client: Client,
17
- context: DomainsEvents,
18
- destinations: DomainEventDestinations,
19
- ) {
20
- context = transformDomainEventData(context);
21
-
22
- for (let i = 0; i < destinations.length; i++) {
23
- let emitter = new Emitter(client);
24
- if (destinations[i].namespace) {
25
- emitter = emitter.of(destinations[i].namespace);
26
- }
27
-
28
- let roomName = buildDomainEventChannelName(destinations[i].channel);
29
-
30
- const fullEventName = buildDomainEventFullName(
31
- context.type,
32
- context.event,
33
- );
34
-
35
- emitter
36
- .in(roomName)
37
- .emit(fullEventName, {
38
- ...context,
39
- meta: {
40
- roomName,
41
- },
42
- });
43
-
44
- if (
45
- context.event !== DomainEventName.CREATED &&
46
- typeof destinations[i].channel === 'function'
47
- ) {
48
- roomName = buildDomainEventChannelName(destinations[i].channel, context.data.id);
49
- emitter
50
- .in(roomName)
51
- .emit(fullEventName, {
52
- ...context,
53
- meta: {
54
- roomName,
55
- roomId: context.data.id,
56
- },
57
- });
58
- }
59
- }
60
- }