@baeta/subscriptions-pubsub 1.0.9 → 2.0.0-next.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.
package/dist/index.js CHANGED
@@ -1,47 +1 @@
1
- // lib/typed-pubsub.ts
2
- var TypedPubSubBase = class {
3
- constructor(pubsub, options) {
4
- this.pubsub = pubsub;
5
- this.options = options;
6
- this.channelPrefix = options?.prefix ?? "";
7
- }
8
- channelPrefix;
9
- publish = (channel, message, ...rest) => {
10
- return this.pubsub.publish(this.mapChannel(channel), message, ...rest);
11
- };
12
- subscribe = (channel, onMessage, ...rest) => {
13
- return this.pubsub.subscribe(this.mapChannel(channel), onMessage, ...rest);
14
- };
15
- unsubscribe = (subId, ...rest) => {
16
- return this.pubsub.unsubscribe(subId, ...rest);
17
- };
18
- mapChannel = (channel) => {
19
- return `${this.channelPrefix}${channel.toString()}`;
20
- };
21
- mapTriggers = (triggers) => {
22
- return Array.isArray(triggers) ? triggers.map(this.mapChannel) : this.mapChannel(triggers);
23
- };
24
- };
25
- var TypedPubSubV2 = class extends TypedPubSubBase {
26
- asyncIterator = (triggers, ...rest) => {
27
- return this.pubsub.asyncIterator(this.mapTriggers(triggers), ...rest);
28
- };
29
- };
30
- var TypedPubSubV3 = class extends TypedPubSubBase {
31
- asyncIterableIterator = (triggers, ...rest) => {
32
- return this.pubsub.asyncIterableIterator(this.mapTriggers(triggers), ...rest);
33
- };
34
- };
35
- function createTypedPubSub(pubsub, options) {
36
- if ("asyncIterator" in pubsub) {
37
- return new TypedPubSubV2(pubsub, options);
38
- }
39
- if ("asyncIterableIterator" in pubsub) {
40
- return new TypedPubSubV3(pubsub, options);
41
- }
42
- throw new Error("Unsupported PubSub");
43
- }
44
- export {
45
- createTypedPubSub
46
- };
47
1
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
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":[]}
1
+ {"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@baeta/subscriptions-pubsub",
3
- "version": "1.0.9",
3
+ "version": "2.0.0-next.0",
4
4
  "keywords": [
5
5
  "baeta",
6
6
  "graphql",
@@ -27,8 +27,8 @@
27
27
  "type": "module",
28
28
  "exports": {
29
29
  ".": {
30
- "types": "./dist/index.d.ts",
31
- "default": "./dist/index.js"
30
+ "types": "./index.ts",
31
+ "default": "./index.ts"
32
32
  }
33
33
  },
34
34
  "types": "dist/index.d.ts",
@@ -44,16 +44,15 @@
44
44
  "types": "tsc --noEmit"
45
45
  },
46
46
  "devDependencies": {
47
- "@baeta/builder": "^0.0.0",
48
- "@baeta/testing": "^0.0.0",
49
- "@baeta/tsconfig": "^0.0.0",
50
- "graphql": "^16.10.0",
51
- "graphql-subscriptions-v2": "npm:graphql-subscriptions@^2.0.0",
52
- "graphql-subscriptions-v3": "npm:graphql-subscriptions@^3.0.0",
53
- "typescript": "^5.8.2"
47
+ "@baeta/builder": "workspace:^",
48
+ "@baeta/testing": "workspace:^",
49
+ "@baeta/tsconfig": "workspace:^",
50
+ "graphql": "^16.11.0",
51
+ "graphql-subscriptions": "^3.0.0",
52
+ "typescript": "^5.9.3"
54
53
  },
55
54
  "engines": {
56
- "node": ">=22.12.0"
55
+ "node": ">=22.20.0"
57
56
  },
58
57
  "publishConfig": {
59
58
  "access": "public",
@@ -90,4 +89,4 @@
90
89
  "alphabetical-ignoring-documents"
91
90
  ]
92
91
  }
93
- }
92
+ }
package/CHANGELOG.md DELETED
@@ -1,29 +0,0 @@
1
- # @baeta/subscriptions-pubsub
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
-
17
- ## 0.0.2
18
-
19
- ### Patch Changes
20
-
21
- - [`b59db50`](https://github.com/andreisergiu98/baeta/commit/b59db501a83275ab2d964933080e688a3a5d8820) Thanks [@andreisergiu98](https://github.com/andreisergiu98)! - add readme
22
-
23
- ## 0.0.1
24
-
25
- ### Patch Changes
26
-
27
- - [#180](https://github.com/andreisergiu98/baeta/pull/180) [`483c709`](https://github.com/andreisergiu98/baeta/commit/483c70932f815fd114732c00b74f9488d7924c72) Thanks [@andreisergiu98](https://github.com/andreisergiu98)! - Raise minimum required NodeJS version to 22.12.0. Drop CommonJS builds in favor of the require_esm feature from NodeJS 22.12.0 onwards.
28
-
29
- - [`de6e89c`](https://github.com/andreisergiu98/baeta/commit/de6e89c1b592e280967c73a4137d24ee56ef1857) Thanks [@andreisergiu98](https://github.com/andreisergiu98)! - raise es target to 2024
package/dist/index.d.ts DELETED
@@ -1,82 +0,0 @@
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
- */
17
- interface TypedPubSubOptions {
18
- /** Optional prefix for channel names */
19
- prefix?: string;
20
- }
21
- type Any = any;
22
- type PubSubMap = Record<string, Any>;
23
- type OnMessage<Payload> = (message: Payload) => Promise<void> | void;
24
- type RestPayloadFnArgs<Fn> = Fn extends (triggerName: string, payload: Any, ...rest: infer Rest) => Any ? Rest : never;
25
- type RestSubscribeFnArgs<Fn> = Fn extends (triggerName: string, onMessage: OnMessage<Any>, ...rest: infer Rest) => Any ? Rest : never;
26
- type RestUnsubscribeFnArgs<Fn> = Fn extends (subId: number, ...rest: infer Rest) => Any ? Rest : never;
27
- type RestAsyncIteratorFnArgs<Fn> = Fn extends (triggers: string | string[], ...rest: infer Rest) => Any ? Rest : never;
28
- type RestAsyncIterableIteratorFnArgs<Fn> = Fn extends (triggers: string | readonly string[], ...rest: infer Rest) => Any ? Rest : never;
29
- declare class TypedPubSubBase<Engine extends PubSubEngineV2 | PubSubEngineV3, Map extends PubSubMap> {
30
- protected pubsub: Engine;
31
- protected options?: TypedPubSubOptions | undefined;
32
- protected channelPrefix: string;
33
- constructor(pubsub: Engine, options?: TypedPubSubOptions | undefined);
34
- publish: <C extends keyof Map>(channel: C, message: Map[C], ...rest: RestPayloadFnArgs<Engine["publish"]>) => Promise<void>;
35
- subscribe: <C extends keyof Map>(channel: C, onMessage: OnMessage<Map[C]>, ...rest: RestSubscribeFnArgs<Engine["subscribe"]>) => Promise<number>;
36
- unsubscribe: (subId: number, ...rest: RestUnsubscribeFnArgs<Engine["unsubscribe"]>) => void;
37
- protected mapChannel: <C extends keyof Map>(channel: C) => string;
38
- protected mapTriggers: <C extends keyof Map>(triggers: C | C[]) => string | string[];
39
- }
40
- declare class TypedPubSubV2<Engine extends PubSubEngineV2, Map extends PubSubMap> extends TypedPubSubBase<Engine, Map> {
41
- asyncIterator: <C extends keyof Map>(triggers: C | C[], ...rest: RestAsyncIteratorFnArgs<Engine["asyncIterator"]>) => AsyncIterator<Map[C], any, any>;
42
- }
43
- /** @hidden */
44
- declare class TypedPubSubV3<Engine extends PubSubEngineV3, Map extends PubSubMap> extends TypedPubSubBase<Engine, Map> {
45
- asyncIterableIterator: <C extends keyof Map>(triggers: C | C[], ...rest: RestAsyncIterableIteratorFnArgs<Engine["asyncIterableIterator"]>) => AsyncIterableIterator<Map[C]>;
46
- }
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>;
81
-
82
- export { type PubSubEngineV2, type PubSubEngineV3, type TypedPubSub, type TypedPubSubOptions, createTypedPubSub };