@baeta/subscriptions-pubsub 2.0.0-next.2 → 2.0.0-next.7
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 +8 -0
- package/README.md +42 -17
- package/dist/index.d.ts +27 -25
- package/dist/index.js +64 -0
- package/dist/index.js.map +1 -1
- package/package.json +12 -15
package/CHANGELOG.md
CHANGED
package/README.md
CHANGED
|
@@ -77,43 +77,68 @@ type Query {
|
|
|
77
77
|
#### 2. Implement your resolvers
|
|
78
78
|
|
|
79
79
|
```typescript
|
|
80
|
-
import {
|
|
80
|
+
import { UserModule } from "./typedef.ts";
|
|
81
81
|
|
|
82
|
-
const { Query } =
|
|
82
|
+
const { Query } = UserModule;
|
|
83
83
|
|
|
84
|
-
Query.user(({ args }) => {
|
|
84
|
+
const userQuery = Query.user.resolve(({ args }) => {
|
|
85
85
|
return dataSource.user.find(args.where);
|
|
86
86
|
});
|
|
87
87
|
|
|
88
|
-
Query.users(() => {
|
|
88
|
+
const usersQuery = Query.users.resolve(() => {
|
|
89
89
|
return dataSource.user.findMany();
|
|
90
90
|
});
|
|
91
|
+
|
|
92
|
+
Query.$fields({
|
|
93
|
+
user: userQuery,
|
|
94
|
+
users: usersQuery,
|
|
95
|
+
});
|
|
91
96
|
```
|
|
92
97
|
|
|
93
98
|
#### 3. Add authorization
|
|
94
99
|
|
|
95
100
|
```typescript
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
Query
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
101
|
+
import { UserModule } from "./typedef.ts";
|
|
102
|
+
|
|
103
|
+
const { Query } = UserModule;
|
|
104
|
+
|
|
105
|
+
const userQuery = Query.user
|
|
106
|
+
.auth({
|
|
107
|
+
$or: {
|
|
108
|
+
isPublic: true,
|
|
109
|
+
isLoggedIn: true,
|
|
110
|
+
},
|
|
111
|
+
})
|
|
112
|
+
.resolve(async ({ args }) => {
|
|
113
|
+
// ...
|
|
114
|
+
});
|
|
104
115
|
```
|
|
105
116
|
|
|
106
117
|
#### 4. Add caching
|
|
107
118
|
|
|
108
119
|
```typescript
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
const { User, Query } = getUserModule();
|
|
120
|
+
const { Query, Mutation, User } = UserModule;
|
|
112
121
|
|
|
113
122
|
export const userCache = User.$createCache();
|
|
114
123
|
|
|
115
|
-
Query.user
|
|
116
|
-
|
|
124
|
+
const userQuery = Query.user
|
|
125
|
+
.auth({
|
|
126
|
+
// ...
|
|
127
|
+
})
|
|
128
|
+
.useCache(userCache)
|
|
129
|
+
.resolve(async ({ args }) => {
|
|
130
|
+
// ...
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
const updateUserMutation = Mutation.updateUser
|
|
134
|
+
.$use(async (next) => {
|
|
135
|
+
const user = await next();
|
|
136
|
+
await userCache.save(user);
|
|
137
|
+
return user;
|
|
138
|
+
})
|
|
139
|
+
.resolve(async ({ args }) => {
|
|
140
|
+
// ...
|
|
141
|
+
});
|
|
117
142
|
```
|
|
118
143
|
|
|
119
144
|
## Compatibility
|
package/dist/index.d.ts
CHANGED
|
@@ -1,25 +1,26 @@
|
|
|
1
|
-
|
|
1
|
+
//#region lib/pubsub-engine.d.ts
|
|
2
|
+
/** biome-ignore-all lint/suspicious/noExplicitAny: allow any args*/
|
|
2
3
|
interface PubSubEngine {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
4
|
+
publish: (triggerName: string, payload: any, ...rest: any[]) => Promise<void>;
|
|
5
|
+
subscribe: (triggerName: string, onMessage: (message: any) => Promise<void> | void, ...rest: any[]) => Promise<number>;
|
|
6
|
+
unsubscribe: (subId: number, ...rest: any[]) => void;
|
|
7
|
+
asyncIterableIterator: <T>(triggers: string | readonly string[], ...rest: any[]) => AsyncIterableIterator<T>;
|
|
7
8
|
}
|
|
8
|
-
|
|
9
|
+
//#endregion
|
|
10
|
+
//#region lib/typed-pubsub.d.ts
|
|
9
11
|
/**
|
|
10
12
|
* Configuration options for TypedPubSub
|
|
11
13
|
*/
|
|
12
14
|
interface TypedPubSubOptions {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
+
/** Optional prefix for channel names */
|
|
16
|
+
prefix?: string;
|
|
15
17
|
}
|
|
16
|
-
type
|
|
17
|
-
type PubSubMap = Record<string, Any>;
|
|
18
|
+
type PubSubMap = Record<string, any>;
|
|
18
19
|
type OnMessage<Payload> = (message: Payload) => Promise<void> | void;
|
|
19
|
-
type RestPayloadFnArgs<Fn> = Fn extends (triggerName: string, payload:
|
|
20
|
-
type RestSubscribeFnArgs<Fn> = Fn extends (triggerName: string, onMessage: OnMessage<
|
|
21
|
-
type RestUnsubscribeFnArgs<Fn> = Fn extends (subId: number, ...rest: infer Rest) =>
|
|
22
|
-
type RestAsyncIterableIteratorFnArgs<Fn> = Fn extends (triggers: string | readonly string[], ...rest: infer Rest) =>
|
|
20
|
+
type RestPayloadFnArgs<Fn> = Fn extends ((triggerName: string, payload: any, ...rest: infer Rest) => any) ? Rest : never;
|
|
21
|
+
type RestSubscribeFnArgs<Fn> = Fn extends ((triggerName: string, onMessage: OnMessage<any>, ...rest: infer Rest) => any) ? Rest : never;
|
|
22
|
+
type RestUnsubscribeFnArgs<Fn> = Fn extends ((subId: number, ...rest: infer Rest) => any) ? Rest : never;
|
|
23
|
+
type RestAsyncIterableIteratorFnArgs<Fn> = Fn extends ((triggers: string | readonly string[], ...rest: infer Rest) => any) ? Rest : never;
|
|
23
24
|
/**
|
|
24
25
|
* Creates a type-safe wrapper around a PubSub implementation.
|
|
25
26
|
*
|
|
@@ -53,16 +54,17 @@ type RestAsyncIterableIteratorFnArgs<Fn> = Fn extends (triggers: string | readon
|
|
|
53
54
|
* ```
|
|
54
55
|
*/
|
|
55
56
|
declare class TypedPubSub<Engine extends PubSubEngine, Map extends PubSubMap> {
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
57
|
+
protected channelPrefix: string;
|
|
58
|
+
protected pubsub: Engine;
|
|
59
|
+
protected options?: TypedPubSubOptions;
|
|
60
|
+
constructor(pubsub: Engine, options?: TypedPubSubOptions);
|
|
61
|
+
publish: <C extends keyof Map>(channel: C, message: Map[C], ...rest: RestPayloadFnArgs<Engine["publish"]>) => Promise<void>;
|
|
62
|
+
subscribe: <C extends keyof Map>(channel: C, onMessage: OnMessage<Map[C]>, ...rest: RestSubscribeFnArgs<Engine["subscribe"]>) => Promise<number>;
|
|
63
|
+
unsubscribe: (subId: number, ...rest: RestUnsubscribeFnArgs<Engine["unsubscribe"]>) => void;
|
|
64
|
+
asyncIterableIterator: <C extends keyof Map>(triggers: C | C[], ...rest: RestAsyncIterableIteratorFnArgs<Engine["asyncIterableIterator"]>) => AsyncIterableIterator<Map[C]>;
|
|
65
|
+
protected mapChannel: <C extends keyof Map>(channel: C) => string;
|
|
66
|
+
protected mapTriggers: <C extends keyof Map>(triggers: C | C[]) => string | string[];
|
|
66
67
|
}
|
|
67
|
-
|
|
68
|
+
//#endregion
|
|
68
69
|
export { TypedPubSub, type TypedPubSubOptions };
|
|
70
|
+
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.js
CHANGED
|
@@ -1 +1,65 @@
|
|
|
1
|
+
//#region lib/typed-pubsub.ts
|
|
2
|
+
/**
|
|
3
|
+
* Creates a type-safe wrapper around a PubSub implementation.
|
|
4
|
+
*
|
|
5
|
+
* This utility ensures that your subscription channels and their payloads
|
|
6
|
+
* are properly typed, helping catch potential errors at compile time.
|
|
7
|
+
*
|
|
8
|
+
* @param pubsub - The PubSub engine instance (e.g., PubSub, RedisPubSub)
|
|
9
|
+
* @param options - Configuration options
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```typescript
|
|
13
|
+
* // Define your event map
|
|
14
|
+
* type PubSubMap = {
|
|
15
|
+
* "user-updated": User;
|
|
16
|
+
* [c: `user-updated-${string}`]: User;
|
|
17
|
+
* };
|
|
18
|
+
*
|
|
19
|
+
* // Create typed PubSub instance
|
|
20
|
+
* const pubsub = new TypedPubSub<PubSub, PubSubMap>(new PubSub());
|
|
21
|
+
*
|
|
22
|
+
* // Usage with Redis
|
|
23
|
+
* const pubsub = new TypedPubSub<RedisPubSub, PubSubMap>(
|
|
24
|
+
* new RedisPubSub({
|
|
25
|
+
* publisher: new Redis(options),
|
|
26
|
+
* subscriber: new Redis(options),
|
|
27
|
+
* }),
|
|
28
|
+
* {
|
|
29
|
+
* prefix: "feature-1:",
|
|
30
|
+
* }
|
|
31
|
+
* );
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
var TypedPubSub = class {
|
|
35
|
+
channelPrefix;
|
|
36
|
+
pubsub;
|
|
37
|
+
options;
|
|
38
|
+
constructor(pubsub, options) {
|
|
39
|
+
this.channelPrefix = options?.prefix ?? "";
|
|
40
|
+
this.pubsub = pubsub;
|
|
41
|
+
this.options = options;
|
|
42
|
+
}
|
|
43
|
+
publish = (channel, message, ...rest) => {
|
|
44
|
+
return this.pubsub.publish(this.mapChannel(channel), message, ...rest);
|
|
45
|
+
};
|
|
46
|
+
subscribe = (channel, onMessage, ...rest) => {
|
|
47
|
+
return this.pubsub.subscribe(this.mapChannel(channel), onMessage, ...rest);
|
|
48
|
+
};
|
|
49
|
+
unsubscribe = (subId, ...rest) => {
|
|
50
|
+
return this.pubsub.unsubscribe(subId, ...rest);
|
|
51
|
+
};
|
|
52
|
+
asyncIterableIterator = (triggers, ...rest) => {
|
|
53
|
+
return this.pubsub.asyncIterableIterator(this.mapTriggers(triggers), ...rest);
|
|
54
|
+
};
|
|
55
|
+
mapChannel = (channel) => {
|
|
56
|
+
return `${this.channelPrefix}${channel.toString()}`;
|
|
57
|
+
};
|
|
58
|
+
mapTriggers = (triggers) => {
|
|
59
|
+
return Array.isArray(triggers) ? triggers.map(this.mapChannel) : this.mapChannel(triggers);
|
|
60
|
+
};
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
//#endregion
|
|
64
|
+
export { TypedPubSub };
|
|
1
65
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":[],"sourcesContent":[],"
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../lib/typed-pubsub.ts"],"sourcesContent":["/** biome-ignore-all lint/suspicious/noExplicitAny: allow any args */\n\nimport type { PubSubEngine } 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\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 RestAsyncIterableIteratorFnArgs<Fn> = Fn extends (\n\ttriggers: string | readonly string[],\n\t...rest: infer Rest\n) => any\n\t? Rest\n\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 = new TypedPubSub<PubSub, PubSubMap>(new PubSub());\n *\n * // Usage with Redis\n * const pubsub = new TypedPubSub<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 class TypedPubSub<Engine extends PubSubEngine, Map extends PubSubMap> {\n\tprotected channelPrefix: string;\n\tprotected pubsub: Engine;\n\tprotected options?: TypedPubSubOptions;\n\n\tconstructor(pubsub: Engine, options?: TypedPubSubOptions) {\n\t\tthis.channelPrefix = options?.prefix ?? '';\n\t\tthis.pubsub = pubsub;\n\t\tthis.options = options;\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\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\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"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA0EA,IAAa,cAAb,MAA6E;CAC5E,AAAU;CACV,AAAU;CACV,AAAU;CAEV,YAAY,QAAgB,SAA8B;AACzD,OAAK,gBAAgB,SAAS,UAAU;AACxC,OAAK,SAAS;AACd,OAAK,UAAU;;CAGhB,WACC,SACA,SACA,GAAG,SACC;AACJ,SAAO,KAAK,OAAO,QAAQ,KAAK,WAAW,QAAQ,EAAE,SAAS,GAAG,KAAK;;CAGvE,aACC,SACA,WACA,GAAG,SACC;AACJ,SAAO,KAAK,OAAO,UAAU,KAAK,WAAW,QAAQ,EAAE,WAAW,GAAG,KAAK;;CAG3E,eAAe,OAAe,GAAG,SAAuD;AACvF,SAAO,KAAK,OAAO,YAAY,OAAO,GAAG,KAAK;;CAG/C,yBACC,UACA,GAAG,SACC;AACJ,SAAO,KAAK,OAAO,sBAA8B,KAAK,YAAY,SAAS,EAAE,GAAG,KAAK;;CAGtF,AAAU,cAAmC,YAAuB;AACnE,SAAO,GAAG,KAAK,gBAAgB,QAAQ,UAAU;;CAGlD,AAAU,eAAoC,aAAyC;AACtF,SAAO,MAAM,QAAQ,SAAS,GAAG,SAAS,IAAI,KAAK,WAAW,GAAG,KAAK,WAAW,SAAS"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@baeta/subscriptions-pubsub",
|
|
3
|
-
"version": "2.0.0-next.
|
|
3
|
+
"version": "2.0.0-next.7",
|
|
4
4
|
"keywords": [
|
|
5
5
|
"baeta",
|
|
6
6
|
"graphql",
|
|
@@ -37,17 +37,23 @@
|
|
|
37
37
|
"package.json"
|
|
38
38
|
],
|
|
39
39
|
"scripts": {
|
|
40
|
-
"build": "
|
|
41
|
-
"prepack": "
|
|
42
|
-
"postpack": "
|
|
43
|
-
"test": "
|
|
40
|
+
"build": "builder build",
|
|
41
|
+
"prepack": "builder prepare",
|
|
42
|
+
"postpack": "builder prepare --clean",
|
|
43
|
+
"test": "builder test",
|
|
44
|
+
"test:circular": "builder test-circular",
|
|
44
45
|
"types": "tsc --noEmit"
|
|
45
46
|
},
|
|
47
|
+
"ava": {
|
|
48
|
+
"extensions": {
|
|
49
|
+
"ts": "module"
|
|
50
|
+
}
|
|
51
|
+
},
|
|
46
52
|
"devDependencies": {
|
|
47
53
|
"@baeta/builder": "^0.0.0",
|
|
48
54
|
"@baeta/testing": "^0.0.0",
|
|
49
55
|
"@baeta/tsconfig": "^0.0.0",
|
|
50
|
-
"graphql": "^16.
|
|
56
|
+
"graphql": "^16.12.0",
|
|
51
57
|
"graphql-subscriptions": "^3.0.0",
|
|
52
58
|
"typescript": "^5.9.3"
|
|
53
59
|
},
|
|
@@ -63,15 +69,6 @@
|
|
|
63
69
|
}
|
|
64
70
|
}
|
|
65
71
|
},
|
|
66
|
-
"ava": {
|
|
67
|
-
"extensions": {
|
|
68
|
-
"ts": "module"
|
|
69
|
-
},
|
|
70
|
-
"nodeArguments": [
|
|
71
|
-
"--no-warnings",
|
|
72
|
-
"--experimental-transform-types"
|
|
73
|
-
]
|
|
74
|
-
},
|
|
75
72
|
"typedocOptions": {
|
|
76
73
|
"entryPoints": [
|
|
77
74
|
"./index.ts"
|