@daiso-tech/core 0.8.0 → 0.9.0

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/dist/cjs/collection/implementations/async-iterable-collection/_shared/async-update-iterable.js.map +1 -1
  2. package/dist/cjs/collection/implementations/async-iterable-collection/async-iterable-collection.js +19 -0
  3. package/dist/cjs/collection/implementations/async-iterable-collection/async-iterable-collection.js.map +1 -1
  4. package/dist/cjs/collection/implementations/iterable-collection/_shared/update-iterable.js.map +1 -1
  5. package/dist/cjs/collection/implementations/iterable-collection/iterable-collection.js +19 -0
  6. package/dist/cjs/collection/implementations/iterable-collection/iterable-collection.js.map +1 -1
  7. package/dist/cjs/collection/implementations/list-collection/list-collection.js +31 -2
  8. package/dist/cjs/collection/implementations/list-collection/list-collection.js.map +1 -1
  9. package/dist/cjs/event-bus/contracts/_shared.js +8 -1
  10. package/dist/cjs/event-bus/contracts/_shared.js.map +1 -1
  11. package/dist/cjs/event-bus/implementations/_shared/test-utilities/event-bus-adapter.test-suite.js +2 -2
  12. package/dist/cjs/event-bus/implementations/_shared/test-utilities/event-bus-adapter.test-suite.js.map +1 -1
  13. package/dist/cjs/event-bus/implementations/event-bus/event-bus-manager.js +14 -7
  14. package/dist/cjs/event-bus/implementations/event-bus/event-bus-manager.js.map +1 -1
  15. package/dist/cjs/storage/implementations/_shared/test-utilities/storage-adapter.test-suite.js +2 -2
  16. package/dist/cjs/storage/implementations/_shared/test-utilities/storage-adapter.test-suite.js.map +1 -1
  17. package/dist/cjs/storage/implementations/storage/storage-manager.js +17 -7
  18. package/dist/cjs/storage/implementations/storage/storage-manager.js.map +1 -1
  19. package/dist/cjs/storage/implementations/storage/with-event-storage-adapter.js +8 -7
  20. package/dist/cjs/storage/implementations/storage/with-event-storage-adapter.js.map +1 -1
  21. package/dist/esm/collection/implementations/async-iterable-collection/_shared/async-update-iterable.js.map +1 -1
  22. package/dist/esm/collection/implementations/async-iterable-collection/async-iterable-collection.js +19 -0
  23. package/dist/esm/collection/implementations/async-iterable-collection/async-iterable-collection.js.map +1 -1
  24. package/dist/esm/collection/implementations/iterable-collection/_shared/update-iterable.js.map +1 -1
  25. package/dist/esm/collection/implementations/iterable-collection/iterable-collection.js +19 -0
  26. package/dist/esm/collection/implementations/iterable-collection/iterable-collection.js.map +1 -1
  27. package/dist/esm/collection/implementations/list-collection/list-collection.js +31 -2
  28. package/dist/esm/collection/implementations/list-collection/list-collection.js.map +1 -1
  29. package/dist/esm/event-bus/contracts/_shared.js +6 -0
  30. package/dist/esm/event-bus/contracts/_shared.js.map +1 -1
  31. package/dist/esm/event-bus/implementations/_shared/test-utilities/event-bus-adapter.test-suite.js +1 -1
  32. package/dist/esm/event-bus/implementations/_shared/test-utilities/event-bus-adapter.test-suite.js.map +1 -1
  33. package/dist/esm/event-bus/implementations/event-bus/event-bus-manager.js +15 -8
  34. package/dist/esm/event-bus/implementations/event-bus/event-bus-manager.js.map +1 -1
  35. package/dist/esm/storage/implementations/_shared/test-utilities/storage-adapter.test-suite.js +1 -1
  36. package/dist/esm/storage/implementations/_shared/test-utilities/storage-adapter.test-suite.js.map +1 -1
  37. package/dist/esm/storage/implementations/storage/storage-manager.js +18 -8
  38. package/dist/esm/storage/implementations/storage/storage-manager.js.map +1 -1
  39. package/dist/esm/storage/implementations/storage/with-event-storage-adapter.js +8 -7
  40. package/dist/esm/storage/implementations/storage/with-event-storage-adapter.js.map +1 -1
  41. package/dist/types/collection/contracts/_shared.d.ts +0 -1
  42. package/dist/types/collection/contracts/async-collection.contract.d.ts +283 -37
  43. package/dist/types/collection/contracts/collection.contract.d.ts +277 -37
  44. package/dist/types/collection/implementations/async-iterable-collection/_shared/async-update-iterable.d.ts +3 -3
  45. package/dist/types/collection/implementations/async-iterable-collection/async-iterable-collection.d.ts +5 -2
  46. package/dist/types/collection/implementations/iterable-collection/_shared/update-iterable.d.ts +3 -3
  47. package/dist/types/collection/implementations/iterable-collection/iterable-collection.d.ts +5 -2
  48. package/dist/types/collection/implementations/list-collection/list-collection.d.ts +5 -2
  49. package/dist/types/event-bus/contracts/_shared.d.ts +6 -0
  50. package/dist/types/event-bus/contracts/event-bus-manager.contract.d.ts +107 -7
  51. package/dist/types/event-bus/contracts/event-bus.contract.d.ts +50 -22
  52. package/dist/types/event-bus/implementations/_shared/test-utilities/event-bus-adapter.test-suite.d.ts +17 -1
  53. package/dist/types/event-bus/implementations/event-bus/event-bus-manager.d.ts +9 -6
  54. package/dist/types/serializer/contracts/serializer.contract.d.ts +0 -3
  55. package/dist/types/storage/contracts/storage-adapter.contract.d.ts +1 -18
  56. package/dist/types/storage/contracts/storage-events.contract.d.ts +1 -0
  57. package/dist/types/storage/contracts/storage-manager.contract.d.ts +87 -6
  58. package/dist/types/storage/contracts/storage.contract.d.ts +41 -33
  59. package/dist/types/storage/implementations/_shared/test-utilities/storage-adapter.test-suite.d.ts +18 -2
  60. package/dist/types/storage/implementations/storage/storage-manager.d.ts +12 -6
  61. package/dist/types/storage/implementations/storage/with-event-storage-adapter.d.ts +1 -1
  62. package/package.json +1 -1
@@ -1,14 +1,14 @@
1
1
  /**
2
2
  * @module Collection
3
3
  */
4
- import { type AsyncPredicate, type AsyncMap, type IAsyncCollection, type ChangendItem } from "../../../../collection/contracts/_module";
4
+ import { type AsyncPredicate, type AsyncMap, type IAsyncCollection } from "../../../../collection/contracts/_module";
5
5
  /**
6
6
  * @internal
7
7
  */
8
- export declare class AsyncUpdateIterable<TInput, TFilterOutput extends TInput, TMapOutput> implements AsyncIterable<ChangendItem<TInput, TFilterOutput, TMapOutput>> {
8
+ export declare class AsyncUpdateIterable<TInput, TFilterOutput extends TInput, TMapOutput> implements AsyncIterable<TInput | TFilterOutput | TMapOutput> {
9
9
  private collection;
10
10
  private predicateFn;
11
11
  private mapFn;
12
12
  constructor(collection: IAsyncCollection<TInput>, predicateFn: AsyncPredicate<TInput, IAsyncCollection<TInput>, TFilterOutput>, mapFn: AsyncMap<TFilterOutput, IAsyncCollection<TInput>, TMapOutput>);
13
- [Symbol.asyncIterator](): AsyncIterator<ChangendItem<TInput, TFilterOutput, TMapOutput>>;
13
+ [Symbol.asyncIterator](): AsyncIterator<TInput | TFilterOutput | TMapOutput>;
14
14
  }
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * @module Collection
3
3
  */
4
- import { type AsyncCollapse, type AsyncPredicate, type AsyncForEach, type AsyncMap, type AsyncModifier, type AsyncTap, type AsyncTransform, type Comparator, type IAsyncCollection, type ChangendItem, type AsyncReduce, type CrossJoinResult } from "../../../collection/contracts/_module";
4
+ import { type AsyncCollapse, type AsyncPredicate, type AsyncForEach, type AsyncMap, type AsyncModifier, type AsyncTap, type AsyncTransform, type Comparator, type IAsyncCollection, type AsyncReduce, type CrossJoinResult } from "../../../collection/contracts/_module";
5
5
  import { type AsyncIterableValue, type AsyncLazyable } from "../../../_shared/types";
6
6
  import { type RecordItem } from "../../../_shared/types";
7
7
  import type { TimeSpan } from "../../../utilities/_module";
@@ -34,7 +34,10 @@ export declare class AsyncIterableCollection<TInput> implements IAsyncCollection
34
34
  join(separator?: string): LazyPromise<Extract<TInput, string>>;
35
35
  collapse(): IAsyncCollection<AsyncCollapse<TInput>>;
36
36
  flatMap<TOutput>(mapFn: AsyncMap<TInput, IAsyncCollection<TInput>, Iterable<TOutput>>): IAsyncCollection<TOutput>;
37
- change<TFilterOutput extends TInput, TMapOutput>(predicateFn: AsyncPredicate<TInput, IAsyncCollection<TInput>, TFilterOutput>, mapFn: AsyncMap<TFilterOutput, IAsyncCollection<TInput>, TMapOutput>): IAsyncCollection<ChangendItem<TInput, TFilterOutput, TMapOutput>>;
37
+ change<TFilterOutput extends TInput, TMapOutput>(predicateFn: AsyncPredicate<TInput, IAsyncCollection<TInput>, TFilterOutput>, mapFn: AsyncMap<TFilterOutput, IAsyncCollection<TInput>, TMapOutput>): IAsyncCollection<TInput | TFilterOutput | TMapOutput>;
38
+ set(index: number, value: TInput | AsyncMap<TInput, IAsyncCollection<TInput>, TInput>): IAsyncCollection<TInput>;
39
+ get(index: number): LazyPromise<TInput | null>;
40
+ getOrFail(index: number): LazyPromise<TInput>;
38
41
  page(page: number, pageSize: number): IAsyncCollection<TInput>;
39
42
  sum(): LazyPromise<Extract<TInput, number>>;
40
43
  average(): LazyPromise<Extract<TInput, number>>;
@@ -1,14 +1,14 @@
1
1
  /**
2
2
  * @module Collection
3
3
  */
4
- import { type Predicate, type ICollection, type Map, type ChangendItem } from "../../../../collection/contracts/_module";
4
+ import { type Predicate, type ICollection, type Map } from "../../../../collection/contracts/_module";
5
5
  /**
6
6
  * @internal
7
7
  */
8
- export declare class UpdateIterable<TInput, TFilterOutput extends TInput, TMapOutput> implements Iterable<ChangendItem<TInput, TFilterOutput, TMapOutput>> {
8
+ export declare class UpdateIterable<TInput, TFilterOutput extends TInput, TMapOutput> implements Iterable<TInput | TFilterOutput | TMapOutput> {
9
9
  private collection;
10
10
  private predicateFn;
11
11
  private mapFn;
12
12
  constructor(collection: ICollection<TInput>, predicateFn: Predicate<TInput, ICollection<TInput>, TFilterOutput>, mapFn: Map<TFilterOutput, ICollection<TInput>, TMapOutput>);
13
- [Symbol.iterator](): Iterator<ChangendItem<TInput, TFilterOutput, TMapOutput>>;
13
+ [Symbol.iterator](): Iterator<TInput | TFilterOutput | TMapOutput>;
14
14
  }
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * @module Collection
3
3
  */
4
- import { type Collapse, type Comparator, type Predicate, type ForEach, type ICollection, type Map, type Modifier, type Tap, type Transform, type ChangendItem, type Reduce, type CrossJoinResult } from "../../../collection/contracts/_module";
4
+ import { type Collapse, type Comparator, type Predicate, type ForEach, type ICollection, type Map, type Modifier, type Tap, type Transform, type Reduce, type CrossJoinResult } from "../../../collection/contracts/_module";
5
5
  import { type Lazyable } from "../../../_shared/types";
6
6
  import { type RecordItem } from "../../../_shared/types";
7
7
  /**
@@ -31,7 +31,10 @@ export declare class IterableCollection<TInput> implements ICollection<TInput> {
31
31
  join(separator?: string): Extract<TInput, string>;
32
32
  collapse(): ICollection<Collapse<TInput>>;
33
33
  flatMap<TOutput>(mapFn: Map<TInput, ICollection<TInput>, Iterable<TOutput>>): ICollection<TOutput>;
34
- change<TFilterOutput extends TInput, TMapOutput>(predicateFn: Predicate<TInput, ICollection<TInput>, TFilterOutput>, mapFn: Map<TFilterOutput, ICollection<TInput>, TMapOutput>): ICollection<ChangendItem<TInput, TFilterOutput, TMapOutput>>;
34
+ change<TFilterOutput extends TInput, TMapOutput>(predicateFn: Predicate<TInput, ICollection<TInput>, TFilterOutput>, mapFn: Map<TFilterOutput, ICollection<TInput>, TMapOutput>): ICollection<TInput | TFilterOutput | TMapOutput>;
35
+ set(index: number, value: TInput | Map<TInput, ICollection<TInput>, TInput>): ICollection<TInput>;
36
+ get(index: number): TInput | null;
37
+ getOrFail(index: number): TInput;
35
38
  page(page: number, pageSize: number): ICollection<TInput>;
36
39
  sum(): Extract<TInput, number>;
37
40
  average(): Extract<TInput, number>;
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * @module Collection
3
3
  */
4
- import { type Collapse, type Comparator, type Predicate, type ICollection, type Map, type Modifier, type Tap, type Transform, type ChangendItem, type Reduce, type ForEach, type CrossJoinResult } from "../../../collection/contracts/_module";
4
+ import { type Collapse, type Comparator, type Predicate, type ICollection, type Map, type Modifier, type Tap, type Transform, type Reduce, type ForEach, type CrossJoinResult } from "../../../collection/contracts/_module";
5
5
  import { type Lazyable } from "../../../_shared/types";
6
6
  import { type RecordItem } from "../../../_shared/types";
7
7
  /**
@@ -28,7 +28,10 @@ export declare class ListCollection<TInput> implements ICollection<TInput> {
28
28
  join(separator?: string): Extract<TInput, string>;
29
29
  collapse(): ICollection<Collapse<TInput>>;
30
30
  flatMap<TOutput>(mapFn: Map<TInput, ICollection<TInput>, Iterable<TOutput>>): ICollection<TOutput>;
31
- change<TFilterOutput extends TInput, TMapOutput>(predicateFn: Predicate<TInput, ICollection<TInput>, TFilterOutput>, mapFn: Map<TFilterOutput, ICollection<TInput>, TMapOutput>): ICollection<ChangendItem<TInput, TFilterOutput, TMapOutput>>;
31
+ change<TFilterOutput extends TInput, TMapOutput>(predicateFn: Predicate<TInput, ICollection<TInput>, TFilterOutput>, mapFn: Map<TFilterOutput, ICollection<TInput>, TMapOutput>): ICollection<TInput | TFilterOutput | TMapOutput>;
32
+ set(index: number, value: TInput | Map<TInput, ICollection<TInput>, TInput>): ICollection<TInput>;
33
+ get(index: number): TInput | null;
34
+ getOrFail(index: number): TInput;
32
35
  page(page: number, pageSize: number): ICollection<TInput>;
33
36
  sum(): Extract<TInput, number>;
34
37
  average(): Extract<TInput, number>;
@@ -12,6 +12,12 @@ export type Listener<TEvent extends IBaseEvent> = (event: TEvent) => Promisable<
12
12
  export declare class EventBusError extends Error {
13
13
  constructor(message: string, cause?: unknown);
14
14
  }
15
+ /**
16
+ * @group Errors
17
+ */
18
+ export declare class UnexpectedEventBusError extends EventBusError {
19
+ constructor(message: string, cause?: unknown);
20
+ }
15
21
  /**
16
22
  * @group Errors
17
23
  */
@@ -1,20 +1,120 @@
1
1
  /**
2
2
  * @module EventBus
3
3
  */
4
- import type { Validator } from "../../utilities/_module";
4
+ import { type Validator } from "../../utilities/_module";
5
5
  import type { INamespacedEventBus } from "../../event-bus/contracts/event-bus.contract";
6
6
  import type { IBaseEvent } from "../../event-bus/contracts/_shared";
7
7
  /**
8
+ * The <i>IEventBusFactory</i> contract makes it easy to switch between different <i>{@link IEventBusAdapter | event adapters}</i> dynamically.
8
9
  * @group Contracts
9
10
  */
10
- export type EventBusManagerUseSettings<TType, TAdapters extends string = string> = {
11
- adapter?: TAdapters;
12
- validator?: Validator<TType>;
11
+ export type IEventBusFactory<TAdapters extends string = string, TEvents extends IBaseEvent = IBaseEvent> = {
12
+ /**
13
+ * @example
14
+ * ```ts
15
+ * import { EventBusManager, MemoryEventBusAdapter, RedisEventBusAdapter } from "@daiso-tech/core";
16
+ * import Redis from "ioredis";
17
+ *
18
+ * const eventBusManager = new EventBusManager({
19
+ * adapters: {
20
+ * memory: new MemoryEventBusAdapter(),
21
+ * redis: new RedisEventBusAdapter({
22
+ * dispatcherClient: new Redis();
23
+ * listenerClient: new Redis();
24
+ * }),
25
+ * },
26
+ * defaultAdapter: "memory",
27
+ * rootNamespace: "@storage/"
28
+ * });
29
+ *
30
+ * (async () => {
31
+ * // Will dispatch envent using the default adapter which is the memory addapter
32
+ * await eventBusManager
33
+ * .use()
34
+ * .dispatch({ type: "add", a: 1, b: 2 });
35
+ * // Will dispatch envent using the redis addapter
36
+ * await eventBusManager
37
+ * .use("redis")
38
+ * .dispatch({ type: "add", a: 1, b: 2 });
39
+ * })();
40
+ * ```
41
+ */
42
+ use(adapter?: TAdapters): INamespacedEventBus<TEvents>;
13
43
  };
14
44
  /**
15
- * The <i>IEventBusManager</i> contract makes it easy to switch between different <i>{@link IEventBusAdapter | event bus adapters}</i> dynamically.
45
+ * The <i>IEventBusManager</i> contract makes it easy to configure and switch between different <i>{@link IEventBusAdapter | event adapters}</i> dynamically.
16
46
  * @group Contracts
17
47
  */
18
- export type IEventBusManager<TAdapters extends string = string> = {
19
- use<TEvents extends IBaseEvent = IBaseEvent>(settings?: EventBusManagerUseSettings<TEvents, TAdapters>): INamespacedEventBus<TEvents>;
48
+ export type IEventBusManager<TAdapters extends string = string, TEvents extends IBaseEvent = IBaseEvent> = IEventBusFactory<TAdapters, TEvents> & {
49
+ /**
50
+ * The <i>withValidation</i> method is used to set a <i>validator</i>, which validates the event during runtime.
51
+ * The type is inferred from the provided <i>validator</i>.
52
+ * @example
53
+ * ```ts
54
+ * import { EventBusManager, MemoryEventBusAdapter, RedisEventBusAdapter, zodValidator } from "@daiso-tech/core";
55
+ * import Redis from "ioredis";
56
+ * import { z } from "zod"
57
+ *
58
+ * const eventBusManager = new EventBusManager({
59
+ * adapters: {
60
+ * memory: new MemoryEventBusAdapter(),
61
+ * redis: new RedisEventBusAdapter({
62
+ * dispatcherClient: new Redis();
63
+ * listenerClient: new Redis();
64
+ * }),
65
+ * },
66
+ * defaultAdapter: "memory",
67
+ * rootNamespace: "@storage/"
68
+ * });
69
+ *
70
+ * (async () => {
71
+ * const addEventSchema = z.zod({
72
+ * type: z.literal("add"),
73
+ * a: z.number(),
74
+ * b: z.number(),
75
+ * });
76
+ * await eventBusManager
77
+ * .withValidation(zodValidator(addEventSchema))
78
+ * .use()
79
+ * // You will se an typescript error and get runtime erorr
80
+ * .dispatch({ type: "add" });
81
+ * })();
82
+ * ```
83
+ */
84
+ withValidation<TOutput extends TEvents = TEvents>(validator: Validator<TOutput>): IEventBusFactory<TAdapters, TOutput>;
85
+ /**
86
+ * The <i>withTypes</i> method is used to set the event types of the <i>{@link IEventBus}</i>.
87
+ * @example
88
+ * ```ts
89
+ * import { EventBusManager, MemoryEventBusAdapter, RedisEventBusAdapter, zodValidator } from "@daiso-tech/core";
90
+ * import Redis from "ioredis";
91
+ * import { z } from "zod"
92
+ *
93
+ * const eventBusManager = new EventBusManager({
94
+ * adapters: {
95
+ * memory: new MemoryEventBusAdapter(),
96
+ * redis: new RedisEventBusAdapter({
97
+ * dispatcherClient: new Redis();
98
+ * listenerClient: new Redis();
99
+ * }),
100
+ * },
101
+ * defaultAdapter: "memory",
102
+ * rootNamespace: "@storage/"
103
+ * });
104
+ *
105
+ * (async () => {
106
+ * type AddEvent = {
107
+ * type: "add";
108
+ * a: number;
109
+ * b: number;
110
+ * };
111
+ * await eventBusManager
112
+ * .withTypes<AddEvent>()
113
+ * .use()
114
+ * // You will se an typescript error
115
+ * .dispatch({ type: "add" });
116
+ * })();
117
+ * ```
118
+ */
119
+ withType<TOutput extends TEvents = TEvents>(): IEventBusFactory<TAdapters, TOutput>;
20
120
  };
@@ -9,83 +9,111 @@ export type SelectEvent<TEvents extends IBaseEvent, TEventType extends TEvents["
9
9
  export type Unsubscribe = () => PromiseLike<void>;
10
10
  /**
11
11
  * The <i>IListenable</i> contract defines a way listening to events independent of underlying technology
12
- * @throws {EventBusError} {@link EventBusError}
13
- * @throws {AddListenerEventBusError} {@link AddListenerEventBusError}
14
- * @throws {RemoveListenerEventBusError} {@link RemoveListenerEventBusError}
15
12
  * @group Contracts
16
13
  */
17
14
  export type IListenable<TEvents extends IBaseEvent = IBaseEvent> = {
18
15
  /**
19
16
  * The <i>addListener</i> method is used for adding <i>{@link Listener | listener}</i> for certain <i>event</i>.
20
17
  * A listener can only be added once for a specific event. Adding the same listener multiple times will have no effect and nothing will occur.
21
- * @throws {EventBusError} {@link EventBusError}
22
18
  * @throws {AddListenerEventBusError} {@link AddListenerEventBusError}
23
19
  */
24
20
  addListener<TEventType extends TEvents["type"]>(event: TEventType, listener: Listener<SelectEvent<TEvents, TEventType>>): PromiseLike<void>;
25
21
  /**
26
22
  * The <i>addListenerMany</i> method is used for adding multiple <i>{@link Listener | listeners}</i> for certain <i>events</i>.
27
23
  * A listener can only be added once for a specific event. Adding the same listener multiple times will have no effect and nothing will occur.
28
- * @throws {EventBusError} {@link EventBusError}
29
24
  * @throws {AddListenerEventBusError} {@link AddListenerEventBusError}
30
25
  */
31
26
  addListenerMany<TEventType extends TEvents["type"]>(events: TEventType[], listener: Listener<SelectEvent<TEvents, TEventType>>): PromiseLike<void>;
32
27
  /**
33
28
  * The <i>removeListener</i> method is used for removing <i>{@link Listener | listener}</i> for certain <i>event</i>.
34
29
  * Removing unadded listener will have no effect and nothing will occur.
35
- * @throws {EventBusError} {@link EventBusError}
36
30
  * @throws {RemoveListenerEventBusError} {@link RemoveListenerEventBusError}
37
31
  */
38
32
  removeListener<TEventType extends TEvents["type"]>(event: TEventType, listener: Listener<SelectEvent<TEvents, TEventType>>): PromiseLike<void>;
39
33
  /**
40
34
  * The <i>removeListener</i> method is used for removing multiple <i>{@link Listener | listeners}</i> for certain <i>event</i>.
41
35
  * Removing unadded listener will have no effect and nothing will occur.
42
- * @throws {EventBusError} {@link EventBusError}
43
36
  * @throws {RemoveListenerEventBusError} {@link RemoveListenerEventBusError}
44
37
  */
45
38
  removeListenerMany<TEventType extends TEvents["type"]>(events: TEventType[], listener: Listener<SelectEvent<TEvents, TEventType>>): PromiseLike<void>;
46
39
  /**
47
40
  * The <i>subscribe</i> method is used for adding <i>{@link Listener | listener}</i> for certain <i>event</i>.
48
41
  * A listener can only be added once for a specific event. Adding the same listener multiple times will have no effect and nothing will occur.
49
- * @throws {EventBusError} {@link EventBusError}
50
- * @throws {RemoveListenerEventBusError} {@link RemoveListenerEventBusError}
51
42
  */
52
43
  subscribe<TEventType extends TEvents["type"]>(event: TEventType, listener: Listener<SelectEvent<TEvents, TEventType>>): PromiseLike<Unsubscribe>;
44
+ /**
45
+ * The <i>subscribeMany</i> method is used for adding <i>{@link Listener | listener}</i> for multiple <i>events</i>.
46
+ * A listener can only be added once for a specific event. Adding the same listener multiple times will have no effect and nothing will occur.
47
+ */
53
48
  subscribeMany<TEventType extends TEvents["type"]>(events: TEventType[], listener: Listener<SelectEvent<TEvents, TEventType>>): PromiseLike<Unsubscribe>;
54
49
  };
55
50
  /**
56
51
  * The <i>IEventBus</i> contract defines a way for dispatching and listening to events independent of underlying technology.
57
52
  * It commes with more convient methods compared to <i>IEventBusAdapter</i>.
58
- * @throws {EventBusError} {@link EventBusError}
59
- * @throws {DispatchEventBusError} {@link DispatchEventBusError}
60
- * @throws {AddListenerEventBusError} {@link AddListenerEventBusError}
61
- * @throws {RemoveListenerEventBusError} {@link RemoveListenerEventBusError}
62
53
  * @group Contracts
63
54
  */
64
55
  export type IEventBus<TEvents extends IBaseEvent = IBaseEvent> = IListenable<TEvents> & {
65
56
  /**
66
57
  * The <i>dispatch</i> method is used for dispatching one or multiple <i>events</i>.
67
- * @throws {EventBusError} {@link EventBusError}
58
+
68
59
  * @throws {DispatchEventBusError} {@link DispatchEventBusError}
69
60
  */
70
61
  dispatch(events: OneOrMore<TEvents>): PromiseLike<void>;
62
+ /**
63
+ * The <i>getNamespace</i> method return the complete namespace.
64
+ * @example
65
+ * ```ts
66
+ * const eventBus = new EventBus(new MemoryEventBusAdapter(), {
67
+ * rootNamespace: "@root/"
68
+ * });
69
+ *
70
+ * // Will be "@root/"
71
+ * console.log(eventBus.getNamespace())
72
+ *
73
+ * const eventBusA = eventBus.withNamespace("a/");
74
+ *
75
+ * // Will be "@root/a/"
76
+ * console.log(eventBusA.getNamespace())
77
+ * ```
78
+ */
79
+ getNamespace(): string;
71
80
  };
72
81
  /**
73
82
  * The <i>INamespacedEventBus</i> contract defines a way for dispatching and listening to events independent of underlying technology.
74
83
  * It commes with one extra method which is useful for multitennat applications compared to <i>IEventBus</i>.
75
- * @throws {EventBusError} {@link EventBusError}
76
- * @throws {DispatchEventBusError} {@link DispatchEventBusError}
77
- * @throws {AddListenerEventBusError} {@link AddListenerEventBusError}
78
- * @throws {RemoveListenerEventBusError} {@link RemoveListenerEventBusError}
79
84
  * @group Contracts
80
85
  */
81
86
  export type INamespacedEventBus<TEvents extends IBaseEvent = IBaseEvent> = IEventBus<TEvents> & {
82
87
  /**
83
88
  * The <i>withNamespace</i> method returns new instance of <i>{@link IEventBus}</i> where all the events names will be prefixed with a given <i>namespace</i>.
84
89
  * This useful for multitennat applications.
90
+ * @example
91
+ * ```ts
92
+ * import { EventBus, MemoryEventBusAdapter } from "@daiso-tech/core";
93
+ *
94
+ * (async () => {
95
+ * const eventBus = new EventBus(new MemoryEventBusAdapter());
96
+ *
97
+ * const eventBusA = eventBus.withNamespace("a");
98
+ * await eventBusA.subscribe("add", (event) => {
99
+ * // This will be logged
100
+ * console.log("eventBusA:", event);
101
+ * });
102
+ *
103
+ * const eventBusB = eventBus.withNamespace("b");
104
+ * await eventBusB.subscribe("add", (event) => {
105
+ * // This will never be logged
106
+ * console.log("eventBusB:", event);
107
+ * });
108
+ *
109
+ * await eventBusA.dispatch({
110
+ * type: "add",
111
+ * a: 1,
112
+ * b: 2
113
+ * })
114
+ * })();
115
+ *
116
+ * ```
85
117
  */
86
118
  withNamespace(namespace: string): IEventBus<TEvents>;
87
- /**
88
- * The <i>getNamespace</i> method return the complete namespace.
89
- */
90
- getNamespace(): string;
91
119
  };
@@ -15,6 +15,22 @@ export type EventBusTestSuiteSettings = {
15
15
  createAdapter: () => Promisable<IEventBusAdapter>;
16
16
  };
17
17
  /**
18
+ * The <i>eventBusAdapterTestSuite</i> function simplifies the process of testing your custom implementation of <i>{@link IEventBusAdapter}</i> with vitest.
19
+ * @example
20
+ * ```ts
21
+ * import { eventBusAdapterTestSuite, MemoryEventBusAdapter } from "@daiso-tech/core";
22
+ * import { expext, test, describe, beforeEach } from "vitest";
23
+ *
24
+ * describe("class: MemoryEventBusAdapter", () => {
25
+ * eventBusAdapterTestSuite({
26
+ * createAdapter: () => new MemoryEventBusAdapter(),
27
+ * test,
28
+ * beforeEach,
29
+ * expect,
30
+ * describe,
31
+ * });
32
+ * });
33
+ * ```
18
34
  * @group Utilities
19
35
  */
20
- export declare function eventBusTestSuite(settings: EventBusTestSuiteSettings): void;
36
+ export declare function eventBusAdapterTestSuite(settings: EventBusTestSuiteSettings): void;
@@ -1,23 +1,26 @@
1
1
  /**
2
2
  * @module EventBus
3
3
  */
4
- import type { INamespacedEventBus, IEventBusAdapter, IEventBusManager, EventBusManagerUseSettings, IBaseEvent } from "../../../event-bus/contracts/_module";
5
- import { type EventBusSettings } from "../../../event-bus/implementations/event-bus/event-bus";
4
+ import { type Validator } from "../../../utilities/_module";
5
+ import type { INamespacedEventBus, IEventBusAdapter, IEventBusManager, IBaseEvent, IEventBusFactory } from "../../../event-bus/contracts/_module";
6
6
  /**
7
7
  * @group Derivables
8
8
  */
9
9
  export type EventBusManagerSettings<TAdapters extends string = string> = {
10
10
  adapters: Record<TAdapters, IEventBusAdapter>;
11
11
  defaultAdapter: NoInfer<TAdapters>;
12
- adapterSettings?: Omit<EventBusSettings<any>, "validator">;
12
+ rootNamespace: string;
13
13
  };
14
14
  /**
15
15
  * @group Derivables
16
16
  */
17
- export declare class EventBusManager<TAdapters extends string = string> implements IEventBusManager<TAdapters> {
17
+ export declare class EventBusManager<TAdapters extends string = string, TEvent extends IBaseEvent = IBaseEvent> implements IEventBusManager<TAdapters> {
18
18
  private readonly adapters;
19
19
  private readonly defaultAdapter;
20
- private readonly adapterSettings;
20
+ private readonly rootNamespace;
21
+ private validator;
21
22
  constructor(settings: EventBusManagerSettings<TAdapters>);
22
- use<TEvents extends IBaseEvent = IBaseEvent>(adapterSettings: EventBusManagerUseSettings<TEvents, TAdapters>): INamespacedEventBus<TEvents>;
23
+ use(adapter?: TAdapters): INamespacedEventBus<TEvent>;
24
+ withValidation<TOutput extends IBaseEvent = IBaseEvent>(validator: Validator<TOutput>): IEventBusFactory<TAdapters, TOutput>;
25
+ withType<TOutput extends IBaseEvent = IBaseEvent>(): IEventBusFactory<TAdapters, TOutput>;
23
26
  }
@@ -21,9 +21,6 @@ export declare class DeserializationError extends SerializerError {
21
21
  }
22
22
  /**
23
23
  * @group Contracts
24
- * @throws {SerializerError} {@link SerializerError}
25
- * @throws {SerializationError} {@link SerializationError}
26
- * @throws {DeserializationError} {@link DeserializationError}
27
24
  */
28
25
  export type ISerializer<TSerialized = unknown> = {
29
26
  /**
@@ -4,55 +4,38 @@
4
4
  /**
5
5
  * The <i>IStorageAdapter</i> contract defines a way for storing data as key-value pairs independent of storage.
6
6
  * This interface is not meant to be used directly, instead you should use <i>IStorage</i>
7
- * @throws {StorageError} {@link StorageError}
8
- * @throws {UnexpectedStorageError} {@link UnexpectedStorageError}
9
- * @throws {TypeStorageError} {@link TypeStorageError}
10
7
  * @group Contracts
11
8
  */
12
- export type IStorageAdapter<TType> = {
9
+ export type IStorageAdapter<TType = unknown> = {
13
10
  /**
14
11
  * The <i>getMany</i> returns the value for the <i>keys</i> that are found otherwise null will be returned.
15
- * @throws {StorageError} {@link StorageError}
16
- * @throws {UnexpectedStorageError} {@link UnexpectedStorageError}
17
12
  */
18
13
  getMany<TKeys extends string>(keys: TKeys[]): PromiseLike<Record<TKeys, TType | null>>;
19
14
  /**
20
15
  * The <i>addMany</i> method adds the keys that doesn't exists. Returns true for the keys that doesn't exists otherwise false will be returned.
21
- * @throws {StorageError} {@link StorageError}
22
- * @throws {UnexpectedStorageError} {@link UnexpectedStorageError}
23
16
  */
24
17
  addMany<TKeys extends string>(values: Record<TKeys, TType>): PromiseLike<Record<TKeys, boolean>>;
25
18
  /**
26
19
  * The <i>updateMany</i> method updates the given keys. Returns true for the keys that exists otherwise false will be returned.
27
- * @throws {StorageError} {@link StorageError}
28
- * @throws {UnexpectedStorageError} {@link UnexpectedStorageError}
29
20
  */
30
21
  updateMany<TKeys extends string>(values: Record<TKeys, TType>): PromiseLike<Record<TKeys, boolean>>;
31
22
  /**
32
23
  * The <i>putMany</i> method replaces the keys that are found. Adds keys that are not found. Returns true for all the keys that are found otherwise false is returned.
33
- * @throws {StorageError} {@link StorageError}
34
- * @throws {UnexpectedStorageError} {@link UnexpectedStorageError}
35
24
  */
36
25
  putMany<TKeys extends string>(values: Record<TKeys, TType>): PromiseLike<Record<TKeys, boolean>>;
37
26
  /**
38
27
  * The <i>removesMany</i> method removes the keys that are found. Returns true for the keys that are found otherwise false is returned.
39
- * @throws {StorageError} {@link StorageError}
40
- * @throws {UnexpectedStorageError} {@link UnexpectedStorageError}
41
28
  */
42
29
  removeMany<TKeys extends string>(keys: TKeys[]): PromiseLike<Record<TKeys, boolean>>;
43
30
  /**
44
31
  * The <i>increment</i> method will increment the given <i>key</i> if found otherwise nonthing will occur.
45
32
  * Returns true if key exists otherwise false will be returned.
46
33
  * An error will thrown if the key is not a number.
47
- * @throws {StorageError} {@link StorageError}
48
- * @throws {UnexpectedStorageError} {@link UnexpectedStorageError}
49
34
  * @throws {TypeStorageError} {@link TypeStorageError}
50
35
  */
51
36
  increment(key: string, value: number): PromiseLike<boolean>;
52
37
  /**
53
38
  * The <i>clear</i> method removes all keys that starts <i>prefix</i>.
54
- * @throws {StorageError} {@link StorageError}
55
- * @throws {UnexpectedStorageError} {@link UnexpectedStorageError}
56
39
  */
57
40
  clear(prefix: string): PromiseLike<void>;
58
41
  };
@@ -79,6 +79,7 @@ export type KeysRemovedStorageEvent = StorageEvent & {
79
79
  keys: string[];
80
80
  };
81
81
  /**
82
+ * This event will be triggered when storage is cleared.
82
83
  * @group Events
83
84
  */
84
85
  export type KeysClearedStorageEvent = StorageEvent & {
@@ -4,16 +4,97 @@
4
4
  import type { Validator } from "../../utilities/_module";
5
5
  import type { INamespacedStorage } from "../../storage/contracts/storage.contract";
6
6
  /**
7
+ * The <i>IStorageFactory</i> contract makes it easy to switch between different <i>{@link IStorageAdapter | storage adapters}</i> dynamically.
7
8
  * @group Contracts
8
9
  */
9
- export type StorageManagerUseSettings<TType, TAdapters extends string = string> = {
10
- adapter?: TAdapters;
11
- validator?: Validator<TType>;
10
+ export type IStorageFactory<TAdapters extends string = string, TType = unknown> = {
11
+ /**
12
+ * @example
13
+ * ```ts
14
+ * import { StorageManager, MemoryStorageAdapter, RedisStorageAdapter } from "@daiso-tech/core";
15
+ * import Redis from "ioredis";
16
+ *
17
+ * const storageManager = new StorageManager({
18
+ * adapters: {
19
+ * memory: new MemoryStorageAdapter(),
20
+ * redis: new RedisStorageAdapter(new Redis()),
21
+ * },
22
+ * defaultAdapter: "memory",
23
+ * rootNamespace: "@storage/"
24
+ * });
25
+ *
26
+ * (async () => {
27
+ * // Will add key using the default adapter which is the memory addapter
28
+ * await storageManager
29
+ * .use()
30
+ * .add("a", 1);
31
+ * // Will add key using the redis addapter
32
+ * await storageManager
33
+ * .use("redis")
34
+ * .add("a", 1);
35
+ * })();
36
+ * ```
37
+ */
38
+ use(adapter?: TAdapters): INamespacedStorage<TType>;
12
39
  };
13
40
  /**
14
- * The <i>IStorageManager</i> contract makes it easy to switch between different <i>{@link IStorageAdapter | storage adapters}</i> dynamically.
41
+ * The <i>IStorageManager</i> contract makes it easy to configure and switch between different <i>{@link IStorageAdapter | storage adapters}</i> dynamically.
15
42
  * @group Contracts
16
43
  */
17
- export type IStorageManager<TAdapters extends string = string> = {
18
- use<TType>(settings?: StorageManagerUseSettings<TType, TAdapters>): INamespacedStorage<TType>;
44
+ export type IStorageManager<TAdapters extends string = string, TType = unknown> = IStorageFactory<TAdapters, TType> & {
45
+ /**
46
+ * The <i>withValidation</i> method is used to set a <i>validator</i>, which validates the storage values during runtime.
47
+ * The type is inferred from the provided <i>validator</i>.
48
+ * @example
49
+ * ```ts
50
+ * import { StorageManager, MemoryStorageAdapter, RedisStorageAdapter, zodValidator } from "@daiso-tech/core";
51
+ * import Redis from "ioredis";
52
+ * import { z } from "zod"
53
+ *
54
+ * const storageManager = new StorageManager({
55
+ * adapters: {
56
+ * memory: new MemoryStorageAdapter(),
57
+ * redis: new RedisStorageAdapter(new Redis()),
58
+ * },
59
+ * defaultAdapter: "memory",
60
+ * rootNamespace: "@storage/"
61
+ * });
62
+ *
63
+ * (async () => {
64
+ * await storageManager
65
+ * .withValidation(zodValidator(z.string()))
66
+ * .use()
67
+ * // You will se an typescript error and get runtime erorr
68
+ * .add("a", 1);
69
+ * })();
70
+ * ```
71
+ */
72
+ withValidation<TOutput extends TType = TType>(validator: Validator<TOutput>): IStorageFactory<TAdapters, TOutput>;
73
+ /**
74
+ * The <i>withTypes</i> method is used to set the value types of the storage.
75
+ * @example
76
+ * ```ts
77
+ * import { StorageManager, MemoryStorageAdapter, RedisStorageAdapter, zodValidator } from "@daiso-tech/core";
78
+ * import Redis from "ioredis";
79
+ * import { z } from "zod"
80
+ *
81
+ * const storageManager = new StorageManager({
82
+ * adapters: {
83
+ * memory: new MemoryStorageAdapter(),
84
+ * redis: new RedisStorageAdapter(new Redis()),
85
+ * },
86
+ * defaultAdapter: "memory",
87
+ * rootNamespace: "@storage/"
88
+ * });
89
+ *
90
+ * (async () => {
91
+ * await storageManager
92
+ * .withTypes<string>()
93
+ * .use()
94
+ * // You will se an typescript error
95
+ * .add("a", 1)
96
+ * })();
97
+ * ```
98
+ */
99
+ withType<TOutput extends TType = TType>(): IStorageFactory<TAdapters, TOutput>;
19
100
  };