@baeta/subscriptions-pubsub 0.0.2 → 1.0.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.
- package/CHANGELOG.md +14 -0
- package/dist/index.d.ts +54 -15
- package/dist/index.js.map +1 -1
- package/package.json +15 -5
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,19 @@
|
|
|
1
1
|
# @baeta/subscriptions-pubsub
|
|
2
2
|
|
|
3
|
+
## 1.0.9
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- [`583014f`](https://github.com/andreisergiu98/baeta/commit/583014f0bac810b25d9a8226bda2df4c9039f5e3) Thanks [@andreisergiu98](https://github.com/andreisergiu98)! - Update dependencies
|
|
8
|
+
|
|
9
|
+
## 1.0.8
|
|
10
|
+
|
|
11
|
+
### Patch Changes
|
|
12
|
+
|
|
13
|
+
- [#189](https://github.com/andreisergiu98/baeta/pull/189) [`d500378`](https://github.com/andreisergiu98/baeta/commit/d500378198e0a9c48298c4242913bca8ad348228) Thanks [@andreisergiu98](https://github.com/andreisergiu98)! - add jsdocs
|
|
14
|
+
|
|
15
|
+
- [#165](https://github.com/andreisergiu98/baeta/pull/165) [`1334c2a`](https://github.com/andreisergiu98/baeta/commit/1334c2a866676c88f0f3d380b22133d81c4e98bc) Thanks [@andreisergiu98](https://github.com/andreisergiu98)! - mark as stable
|
|
16
|
+
|
|
3
17
|
## 0.0.2
|
|
4
18
|
|
|
5
19
|
### Patch Changes
|
package/dist/index.d.ts
CHANGED
|
@@ -1,26 +1,32 @@
|
|
|
1
|
+
interface PubSubEngineV2 {
|
|
2
|
+
publish: (triggerName: string, payload: any, ...rest: any[]) => Promise<void>;
|
|
3
|
+
subscribe: (triggerName: string, onMessage: (message: any) => Promise<void> | void, ...rest: any[]) => Promise<number>;
|
|
4
|
+
unsubscribe: (subId: number, ...rest: any[]) => void;
|
|
5
|
+
asyncIterator: <T>(triggers: string | string[], ...rest: any[]) => AsyncIterator<T>;
|
|
6
|
+
}
|
|
7
|
+
interface PubSubEngineV3 {
|
|
8
|
+
publish: (triggerName: string, payload: any, ...rest: any[]) => Promise<void>;
|
|
9
|
+
subscribe: (triggerName: string, onMessage: (message: any) => Promise<void> | void, ...rest: any[]) => Promise<number>;
|
|
10
|
+
unsubscribe: (subId: number, ...rest: any[]) => void;
|
|
11
|
+
asyncIterableIterator: <T>(triggers: string | readonly string[], ...rest: any[]) => AsyncIterableIterator<T>;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Configuration options for TypedPubSub
|
|
16
|
+
*/
|
|
1
17
|
interface TypedPubSubOptions {
|
|
18
|
+
/** Optional prefix for channel names */
|
|
2
19
|
prefix?: string;
|
|
3
20
|
}
|
|
4
21
|
type Any = any;
|
|
5
22
|
type PubSubMap = Record<string, Any>;
|
|
6
23
|
type OnMessage<Payload> = (message: Payload) => Promise<void> | void;
|
|
7
|
-
interface PubSubEngineBase {
|
|
8
|
-
publish: (triggerName: string, payload: Any, ...rest: Any[]) => Promise<void>;
|
|
9
|
-
subscribe: (triggerName: string, onMessage: OnMessage<Any>, ...rest: Any[]) => Promise<number>;
|
|
10
|
-
unsubscribe: (subId: number, ...rest: Any[]) => void;
|
|
11
|
-
}
|
|
12
|
-
interface PubSubEngineV2 extends PubSubEngineBase {
|
|
13
|
-
asyncIterator: <T>(triggers: string | string[], ...rest: Any[]) => AsyncIterator<T>;
|
|
14
|
-
}
|
|
15
|
-
interface PubSubEngineV3 extends PubSubEngineBase {
|
|
16
|
-
asyncIterableIterator: <T>(triggers: string | readonly string[], ...rest: Any[]) => AsyncIterableIterator<T>;
|
|
17
|
-
}
|
|
18
24
|
type RestPayloadFnArgs<Fn> = Fn extends (triggerName: string, payload: Any, ...rest: infer Rest) => Any ? Rest : never;
|
|
19
25
|
type RestSubscribeFnArgs<Fn> = Fn extends (triggerName: string, onMessage: OnMessage<Any>, ...rest: infer Rest) => Any ? Rest : never;
|
|
20
26
|
type RestUnsubscribeFnArgs<Fn> = Fn extends (subId: number, ...rest: infer Rest) => Any ? Rest : never;
|
|
21
27
|
type RestAsyncIteratorFnArgs<Fn> = Fn extends (triggers: string | string[], ...rest: infer Rest) => Any ? Rest : never;
|
|
22
28
|
type RestAsyncIterableIteratorFnArgs<Fn> = Fn extends (triggers: string | readonly string[], ...rest: infer Rest) => Any ? Rest : never;
|
|
23
|
-
declare class TypedPubSubBase<Engine extends
|
|
29
|
+
declare class TypedPubSubBase<Engine extends PubSubEngineV2 | PubSubEngineV3, Map extends PubSubMap> {
|
|
24
30
|
protected pubsub: Engine;
|
|
25
31
|
protected options?: TypedPubSubOptions | undefined;
|
|
26
32
|
protected channelPrefix: string;
|
|
@@ -34,10 +40,43 @@ declare class TypedPubSubBase<Engine extends PubSubEngineBase, Map extends PubSu
|
|
|
34
40
|
declare class TypedPubSubV2<Engine extends PubSubEngineV2, Map extends PubSubMap> extends TypedPubSubBase<Engine, Map> {
|
|
35
41
|
asyncIterator: <C extends keyof Map>(triggers: C | C[], ...rest: RestAsyncIteratorFnArgs<Engine["asyncIterator"]>) => AsyncIterator<Map[C], any, any>;
|
|
36
42
|
}
|
|
43
|
+
/** @hidden */
|
|
37
44
|
declare class TypedPubSubV3<Engine extends PubSubEngineV3, Map extends PubSubMap> extends TypedPubSubBase<Engine, Map> {
|
|
38
45
|
asyncIterableIterator: <C extends keyof Map>(triggers: C | C[], ...rest: RestAsyncIterableIteratorFnArgs<Engine["asyncIterableIterator"]>) => AsyncIterableIterator<Map[C]>;
|
|
39
46
|
}
|
|
40
|
-
type TypedPubSub<Engine extends PubSubEngineV2 | PubSubEngineV3, Map extends
|
|
41
|
-
|
|
47
|
+
type TypedPubSub<Engine extends PubSubEngineV2 | PubSubEngineV3, Map extends Record<string, any>> = Engine extends PubSubEngineV3 ? TypedPubSubV3<Engine, Map> : Engine extends PubSubEngineV2 ? TypedPubSubV2<Engine, Map> : never;
|
|
48
|
+
/**
|
|
49
|
+
* Creates a type-safe wrapper around a PubSub implementation.
|
|
50
|
+
*
|
|
51
|
+
* This utility ensures that your subscription channels and their payloads
|
|
52
|
+
* are properly typed, helping catch potential errors at compile time.
|
|
53
|
+
*
|
|
54
|
+
* @param pubsub - The PubSub engine instance (e.g., PubSub, RedisPubSub)
|
|
55
|
+
* @param options - Configuration options
|
|
56
|
+
*
|
|
57
|
+
* @example
|
|
58
|
+
* ```typescript
|
|
59
|
+
* // Define your event map
|
|
60
|
+
* type PubSubMap = {
|
|
61
|
+
* "user-updated": User;
|
|
62
|
+
* [c: `user-updated-${string}`]: User;
|
|
63
|
+
* };
|
|
64
|
+
*
|
|
65
|
+
* // Create typed PubSub instance
|
|
66
|
+
* const pubsub = createTypedPubSub<PubSub, PubSubMap>(new PubSub());
|
|
67
|
+
*
|
|
68
|
+
* // Usage with Redis
|
|
69
|
+
* const pubsub = createTypedPubSub<RedisPubSub, PubSubMap>(
|
|
70
|
+
* new RedisPubSub({
|
|
71
|
+
* publisher: new Redis(options),
|
|
72
|
+
* subscriber: new Redis(options),
|
|
73
|
+
* }),
|
|
74
|
+
* {
|
|
75
|
+
* prefix: "feature-1:",
|
|
76
|
+
* }
|
|
77
|
+
* );
|
|
78
|
+
* ```
|
|
79
|
+
*/
|
|
80
|
+
declare function createTypedPubSub<Engine extends PubSubEngineV2 | PubSubEngineV3, Map extends Record<string, any>>(pubsub: Engine, options?: TypedPubSubOptions): TypedPubSub<Engine, Map>;
|
|
42
81
|
|
|
43
|
-
export { type TypedPubSub, type TypedPubSubOptions, createTypedPubSub };
|
|
82
|
+
export { type PubSubEngineV2, type PubSubEngineV3, type TypedPubSub, type TypedPubSubOptions, createTypedPubSub };
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../lib/typed-pubsub.ts"],"sourcesContent":["
|
|
1
|
+
{"version":3,"sources":["../lib/typed-pubsub.ts"],"sourcesContent":["import type { PubSubEngineV2, PubSubEngineV3 } from './pubsub-engine.ts';\n\n/**\n * Configuration options for TypedPubSub\n */\nexport interface TypedPubSubOptions {\n\t/** Optional prefix for channel names */\n\tprefix?: string;\n}\n\n// biome-ignore lint/suspicious/noExplicitAny: we need to use `any` in this file to support dynamic typing\ntype Any = any;\n\ntype PubSubMap = Record<string, Any>;\ntype OnMessage<Payload> = (message: Payload) => Promise<void> | void;\n\ntype RestPayloadFnArgs<Fn> = Fn extends (\n\ttriggerName: string,\n\tpayload: Any,\n\t...rest: infer Rest\n) => Any\n\t? Rest\n\t: never;\n\ntype RestSubscribeFnArgs<Fn> = Fn extends (\n\ttriggerName: string,\n\tonMessage: OnMessage<Any>,\n\t...rest: infer Rest\n) => Any\n\t? Rest\n\t: never;\n\ntype RestUnsubscribeFnArgs<Fn> = Fn extends (subId: number, ...rest: infer Rest) => Any\n\t? Rest\n\t: never;\n\ntype RestAsyncIteratorFnArgs<Fn> = Fn extends (\n\ttriggers: string | string[],\n\t...rest: infer Rest\n) => Any\n\t? Rest\n\t: never;\n\ntype RestAsyncIterableIteratorFnArgs<Fn> = Fn extends (\n\ttriggers: string | readonly string[],\n\t...rest: infer Rest\n) => Any\n\t? Rest\n\t: never;\n\nclass TypedPubSubBase<Engine extends PubSubEngineV2 | PubSubEngineV3, Map extends PubSubMap> {\n\tprotected channelPrefix: string;\n\n\tconstructor(\n\t\tprotected pubsub: Engine,\n\t\tprotected options?: TypedPubSubOptions,\n\t) {\n\t\tthis.channelPrefix = options?.prefix ?? '';\n\t}\n\n\tpublish = <C extends keyof Map>(\n\t\tchannel: C,\n\t\tmessage: Map[C],\n\t\t...rest: RestPayloadFnArgs<Engine['publish']>\n\t) => {\n\t\treturn this.pubsub.publish(this.mapChannel(channel), message, ...rest);\n\t};\n\n\tsubscribe = <C extends keyof Map>(\n\t\tchannel: C,\n\t\tonMessage: OnMessage<Map[C]>,\n\t\t...rest: RestSubscribeFnArgs<Engine['subscribe']>\n\t) => {\n\t\treturn this.pubsub.subscribe(this.mapChannel(channel), onMessage, ...rest);\n\t};\n\n\tunsubscribe = (subId: number, ...rest: RestUnsubscribeFnArgs<Engine['unsubscribe']>) => {\n\t\treturn this.pubsub.unsubscribe(subId, ...rest);\n\t};\n\n\tprotected mapChannel = <C extends keyof Map>(channel: C): string => {\n\t\treturn `${this.channelPrefix}${channel.toString()}`;\n\t};\n\n\tprotected mapTriggers = <C extends keyof Map>(triggers: C | C[]): string | string[] => {\n\t\treturn Array.isArray(triggers) ? triggers.map(this.mapChannel) : this.mapChannel(triggers);\n\t};\n}\n\nclass TypedPubSubV2<Engine extends PubSubEngineV2, Map extends PubSubMap> extends TypedPubSubBase<\n\tEngine,\n\tMap\n> {\n\tasyncIterator = <C extends keyof Map>(\n\t\ttriggers: C | C[],\n\t\t...rest: RestAsyncIteratorFnArgs<Engine['asyncIterator']>\n\t) => {\n\t\treturn this.pubsub.asyncIterator<Map[C]>(this.mapTriggers(triggers), ...rest);\n\t};\n}\n\n/** @hidden */\nclass TypedPubSubV3<Engine extends PubSubEngineV3, Map extends PubSubMap> extends TypedPubSubBase<\n\tEngine,\n\tMap\n> {\n\tasyncIterableIterator = <C extends keyof Map>(\n\t\ttriggers: C | C[],\n\t\t...rest: RestAsyncIterableIteratorFnArgs<Engine['asyncIterableIterator']>\n\t) => {\n\t\treturn this.pubsub.asyncIterableIterator<Map[C]>(this.mapTriggers(triggers), ...rest);\n\t};\n}\n\nexport type TypedPubSub<\n\tEngine extends PubSubEngineV2 | PubSubEngineV3,\n\t// biome-ignore lint/suspicious/noExplicitAny: accept any for dynamic typing\n\tMap extends Record<string, any>,\n> = Engine extends PubSubEngineV3\n\t? TypedPubSubV3<Engine, Map>\n\t: Engine extends PubSubEngineV2\n\t\t? TypedPubSubV2<Engine, Map>\n\t\t: never;\n\n/**\n * Creates a type-safe wrapper around a PubSub implementation.\n *\n * This utility ensures that your subscription channels and their payloads\n * are properly typed, helping catch potential errors at compile time.\n *\n * @param pubsub - The PubSub engine instance (e.g., PubSub, RedisPubSub)\n * @param options - Configuration options\n *\n * @example\n * ```typescript\n * // Define your event map\n * type PubSubMap = {\n * \"user-updated\": User;\n * [c: `user-updated-${string}`]: User;\n * };\n *\n * // Create typed PubSub instance\n * const pubsub = createTypedPubSub<PubSub, PubSubMap>(new PubSub());\n *\n * // Usage with Redis\n * const pubsub = createTypedPubSub<RedisPubSub, PubSubMap>(\n * new RedisPubSub({\n * publisher: new Redis(options),\n * subscriber: new Redis(options),\n * }),\n * {\n * prefix: \"feature-1:\",\n * }\n * );\n * ```\n */\nexport function createTypedPubSub<\n\tEngine extends PubSubEngineV2 | PubSubEngineV3,\n\t// biome-ignore lint/suspicious/noExplicitAny: accept any for dynamic typing\n\tMap extends Record<string, any>,\n>(pubsub: Engine, options?: TypedPubSubOptions): TypedPubSub<Engine, Map> {\n\tif ('asyncIterator' in pubsub) {\n\t\treturn new TypedPubSubV2<PubSubEngineV2, Map>(pubsub, options) as TypedPubSub<Engine, Map>;\n\t}\n\n\tif ('asyncIterableIterator' in pubsub) {\n\t\treturn new TypedPubSubV3<PubSubEngineV3, PubSubMap>(pubsub, options) as TypedPubSub<\n\t\t\tEngine,\n\t\t\tMap\n\t\t>;\n\t}\n\n\tthrow new Error('Unsupported PubSub');\n}\n"],"mappings":";AAkDA,IAAM,kBAAN,MAA6F;AAAA,EAG5F,YACW,QACA,SACT;AAFS;AACA;AAEV,SAAK,gBAAgB,SAAS,UAAU;AAAA,EACzC;AAAA,EAPU;AAAA,EASV,UAAU,CACT,SACA,YACG,SACC;AACJ,WAAO,KAAK,OAAO,QAAQ,KAAK,WAAW,OAAO,GAAG,SAAS,GAAG,IAAI;AAAA,EACtE;AAAA,EAEA,YAAY,CACX,SACA,cACG,SACC;AACJ,WAAO,KAAK,OAAO,UAAU,KAAK,WAAW,OAAO,GAAG,WAAW,GAAG,IAAI;AAAA,EAC1E;AAAA,EAEA,cAAc,CAAC,UAAkB,SAAuD;AACvF,WAAO,KAAK,OAAO,YAAY,OAAO,GAAG,IAAI;AAAA,EAC9C;AAAA,EAEU,aAAa,CAAsB,YAAuB;AACnE,WAAO,GAAG,KAAK,aAAa,GAAG,QAAQ,SAAS,CAAC;AAAA,EAClD;AAAA,EAEU,cAAc,CAAsB,aAAyC;AACtF,WAAO,MAAM,QAAQ,QAAQ,IAAI,SAAS,IAAI,KAAK,UAAU,IAAI,KAAK,WAAW,QAAQ;AAAA,EAC1F;AACD;AAEA,IAAM,gBAAN,cAAkF,gBAGhF;AAAA,EACD,gBAAgB,CACf,aACG,SACC;AACJ,WAAO,KAAK,OAAO,cAAsB,KAAK,YAAY,QAAQ,GAAG,GAAG,IAAI;AAAA,EAC7E;AACD;AAGA,IAAM,gBAAN,cAAkF,gBAGhF;AAAA,EACD,wBAAwB,CACvB,aACG,SACC;AACJ,WAAO,KAAK,OAAO,sBAA8B,KAAK,YAAY,QAAQ,GAAG,GAAG,IAAI;AAAA,EACrF;AACD;AA4CO,SAAS,kBAId,QAAgB,SAAwD;AACzE,MAAI,mBAAmB,QAAQ;AAC9B,WAAO,IAAI,cAAmC,QAAQ,OAAO;AAAA,EAC9D;AAEA,MAAI,2BAA2B,QAAQ;AACtC,WAAO,IAAI,cAAyC,QAAQ,OAAO;AAAA,EAIpE;AAEA,QAAM,IAAI,MAAM,oBAAoB;AACrC;","names":[]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@baeta/subscriptions-pubsub",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "1.0.9",
|
|
4
4
|
"keywords": [
|
|
5
5
|
"baeta",
|
|
6
6
|
"graphql",
|
|
@@ -45,12 +45,12 @@
|
|
|
45
45
|
},
|
|
46
46
|
"devDependencies": {
|
|
47
47
|
"@baeta/builder": "^0.0.0",
|
|
48
|
+
"@baeta/testing": "^0.0.0",
|
|
48
49
|
"@baeta/tsconfig": "^0.0.0",
|
|
49
|
-
"
|
|
50
|
-
"graphql": "^16.9.0",
|
|
50
|
+
"graphql": "^16.10.0",
|
|
51
51
|
"graphql-subscriptions-v2": "npm:graphql-subscriptions@^2.0.0",
|
|
52
52
|
"graphql-subscriptions-v3": "npm:graphql-subscriptions@^3.0.0",
|
|
53
|
-
"typescript": "^5.
|
|
53
|
+
"typescript": "^5.8.2"
|
|
54
54
|
},
|
|
55
55
|
"engines": {
|
|
56
56
|
"node": ">=22.12.0"
|
|
@@ -78,6 +78,16 @@
|
|
|
78
78
|
"./index.ts"
|
|
79
79
|
],
|
|
80
80
|
"readme": "none",
|
|
81
|
-
"tsconfig": "./tsconfig.json"
|
|
81
|
+
"tsconfig": "./tsconfig.json",
|
|
82
|
+
"intentionallyNotExported": [
|
|
83
|
+
"TypedPubSubV2",
|
|
84
|
+
"TypedPubSubV3"
|
|
85
|
+
],
|
|
86
|
+
"sort": [
|
|
87
|
+
"kind",
|
|
88
|
+
"instance-first",
|
|
89
|
+
"required-first",
|
|
90
|
+
"alphabetical-ignoring-documents"
|
|
91
|
+
]
|
|
82
92
|
}
|
|
83
93
|
}
|