@baeta/subscriptions-pubsub 2.0.0-next.1 → 2.0.0-next.2
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.
- package/CHANGELOG.md +6 -0
- package/dist/index.d.ts +68 -0
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
type Any$1 = any;
|
|
2
|
+
interface PubSubEngine {
|
|
3
|
+
publish: (triggerName: string, payload: Any$1, ...rest: Any$1[]) => Promise<void>;
|
|
4
|
+
subscribe: (triggerName: string, onMessage: (message: Any$1) => Promise<void> | void, ...rest: Any$1[]) => Promise<number>;
|
|
5
|
+
unsubscribe: (subId: number, ...rest: Any$1[]) => void;
|
|
6
|
+
asyncIterableIterator: <T>(triggers: string | readonly string[], ...rest: Any$1[]) => AsyncIterableIterator<T>;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Configuration options for TypedPubSub
|
|
11
|
+
*/
|
|
12
|
+
interface TypedPubSubOptions {
|
|
13
|
+
/** Optional prefix for channel names */
|
|
14
|
+
prefix?: string;
|
|
15
|
+
}
|
|
16
|
+
type Any = any;
|
|
17
|
+
type PubSubMap = Record<string, Any>;
|
|
18
|
+
type OnMessage<Payload> = (message: Payload) => Promise<void> | void;
|
|
19
|
+
type RestPayloadFnArgs<Fn> = Fn extends (triggerName: string, payload: Any, ...rest: infer Rest) => Any ? Rest : never;
|
|
20
|
+
type RestSubscribeFnArgs<Fn> = Fn extends (triggerName: string, onMessage: OnMessage<Any>, ...rest: infer Rest) => Any ? Rest : never;
|
|
21
|
+
type RestUnsubscribeFnArgs<Fn> = Fn extends (subId: number, ...rest: infer Rest) => Any ? Rest : never;
|
|
22
|
+
type RestAsyncIterableIteratorFnArgs<Fn> = Fn extends (triggers: string | readonly string[], ...rest: infer Rest) => Any ? Rest : never;
|
|
23
|
+
/**
|
|
24
|
+
* Creates a type-safe wrapper around a PubSub implementation.
|
|
25
|
+
*
|
|
26
|
+
* This utility ensures that your subscription channels and their payloads
|
|
27
|
+
* are properly typed, helping catch potential errors at compile time.
|
|
28
|
+
*
|
|
29
|
+
* @param pubsub - The PubSub engine instance (e.g., PubSub, RedisPubSub)
|
|
30
|
+
* @param options - Configuration options
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* ```typescript
|
|
34
|
+
* // Define your event map
|
|
35
|
+
* type PubSubMap = {
|
|
36
|
+
* "user-updated": User;
|
|
37
|
+
* [c: `user-updated-${string}`]: User;
|
|
38
|
+
* };
|
|
39
|
+
*
|
|
40
|
+
* // Create typed PubSub instance
|
|
41
|
+
* const pubsub = new TypedPubSub<PubSub, PubSubMap>(new PubSub());
|
|
42
|
+
*
|
|
43
|
+
* // Usage with Redis
|
|
44
|
+
* const pubsub = new TypedPubSub<RedisPubSub, PubSubMap>(
|
|
45
|
+
* new RedisPubSub({
|
|
46
|
+
* publisher: new Redis(options),
|
|
47
|
+
* subscriber: new Redis(options),
|
|
48
|
+
* }),
|
|
49
|
+
* {
|
|
50
|
+
* prefix: "feature-1:",
|
|
51
|
+
* }
|
|
52
|
+
* );
|
|
53
|
+
* ```
|
|
54
|
+
*/
|
|
55
|
+
declare class TypedPubSub<Engine extends PubSubEngine, Map extends PubSubMap> {
|
|
56
|
+
protected channelPrefix: string;
|
|
57
|
+
protected pubsub: Engine;
|
|
58
|
+
protected options?: TypedPubSubOptions;
|
|
59
|
+
constructor(pubsub: Engine, options?: TypedPubSubOptions);
|
|
60
|
+
publish: <C extends keyof Map>(channel: C, message: Map[C], ...rest: RestPayloadFnArgs<Engine["publish"]>) => Promise<void>;
|
|
61
|
+
subscribe: <C extends keyof Map>(channel: C, onMessage: OnMessage<Map[C]>, ...rest: RestSubscribeFnArgs<Engine["subscribe"]>) => Promise<number>;
|
|
62
|
+
unsubscribe: (subId: number, ...rest: RestUnsubscribeFnArgs<Engine["unsubscribe"]>) => void;
|
|
63
|
+
asyncIterableIterator: <C extends keyof Map>(triggers: C | C[], ...rest: RestAsyncIterableIteratorFnArgs<Engine["asyncIterableIterator"]>) => AsyncIterableIterator<Map[C]>;
|
|
64
|
+
protected mapChannel: <C extends keyof Map>(channel: C) => string;
|
|
65
|
+
protected mapTriggers: <C extends keyof Map>(triggers: C | C[]) => string | string[];
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export { TypedPubSub, type TypedPubSubOptions };
|