@baeta/subscriptions-pubsub 2.0.0-next.14 → 2.0.0-next.15
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/README.md +29 -22
- package/dist/index.js.map +1 -1
- package/package.json +8 -7
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
# @baeta/subscriptions-pubsub
|
|
2
2
|
|
|
3
|
+
## 2.0.0-next.15
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- Drop Node v23 and v25 by [@andreisergiu98](https://github.com/andreisergiu98) in [#474](https://github.com/andreisergiu98/baeta/pull/474)
|
|
8
|
+
|
|
3
9
|
## 2.0.0-next.14
|
|
4
10
|
|
|
5
11
|
### Patch Changes
|
package/README.md
CHANGED
|
@@ -35,10 +35,11 @@ Building GraphQL APIs shouldn't be complicated. **Baeta** is a modern, modular,
|
|
|
35
35
|
- **Middleware & Directives**: Easy integration of custom behaviors
|
|
36
36
|
- **High Performance**: Built for scalability and efficiency
|
|
37
37
|
|
|
38
|
-
#### And optional
|
|
38
|
+
#### And optional app plugins and libraries
|
|
39
39
|
|
|
40
|
-
- **@baeta/
|
|
41
|
-
- **@baeta/
|
|
40
|
+
- **@baeta/auth**: Add powerful scope-based authorization
|
|
41
|
+
- **@baeta/complexity**: Reject resource-exhausting queries with depth, breadth and complexity limits
|
|
42
|
+
- **@baeta/cache**: Type-safe caching with declarative queries and automatic reconciliation
|
|
42
43
|
- ... and more!
|
|
43
44
|
|
|
44
45
|
## Why use Baeta?
|
|
@@ -89,7 +90,7 @@ const usersQuery = Query.users.resolve(() => {
|
|
|
89
90
|
return dataSource.user.findMany();
|
|
90
91
|
});
|
|
91
92
|
|
|
92
|
-
Query.$fields({
|
|
93
|
+
export default Query.$fields({
|
|
93
94
|
user: userQuery,
|
|
94
95
|
users: usersQuery,
|
|
95
96
|
});
|
|
@@ -98,17 +99,13 @@ Query.$fields({
|
|
|
98
99
|
#### 3. Add authorization
|
|
99
100
|
|
|
100
101
|
```typescript
|
|
102
|
+
import { auth, rule, scope } from "./lib/auth.ts";
|
|
101
103
|
import { UserModule } from "./typedef.ts";
|
|
102
104
|
|
|
103
105
|
const { Query } = UserModule;
|
|
104
106
|
|
|
105
107
|
const userQuery = Query.user
|
|
106
|
-
.$auth(
|
|
107
|
-
$or: {
|
|
108
|
-
isPublic: true,
|
|
109
|
-
isLoggedIn: true,
|
|
110
|
-
},
|
|
111
|
-
})
|
|
108
|
+
.$use(auth(rule.or(scope.isPublic, scope.isLoggedIn)))
|
|
112
109
|
.resolve(async ({ args }) => {
|
|
113
110
|
// ...
|
|
114
111
|
});
|
|
@@ -117,23 +114,33 @@ const userQuery = Query.user
|
|
|
117
114
|
#### 4. Add caching
|
|
118
115
|
|
|
119
116
|
```typescript
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
117
|
+
import { createCache, defineQuery } from "@baeta/cache";
|
|
118
|
+
import { redisClient } from "./lib/redis.ts";
|
|
119
|
+
|
|
120
|
+
const { Query, Mutation } = UserModule;
|
|
121
|
+
|
|
122
|
+
export const userCache = createCache(redisClient, {
|
|
123
|
+
name: "UserCache",
|
|
124
|
+
parse: JSON.parse,
|
|
125
|
+
serialize: JSON.stringify,
|
|
126
|
+
})
|
|
127
|
+
.withQueries({
|
|
128
|
+
findUser: defineQuery({
|
|
129
|
+
resolve: async (args: { id: string }) => {
|
|
130
|
+
return dataSource.user.findUnique({ where: args });
|
|
131
|
+
},
|
|
132
|
+
}),
|
|
127
133
|
})
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
})
|
|
134
|
+
.build();
|
|
135
|
+
|
|
136
|
+
const userQuery = Query.user.map(({ args }) =>
|
|
137
|
+
userCache.queries.findUser({ id: args.where.id }),
|
|
138
|
+
);
|
|
132
139
|
|
|
133
140
|
const updateUserMutation = Mutation.updateUser
|
|
134
141
|
.$use(async (next) => {
|
|
135
142
|
const user = await next();
|
|
136
|
-
await userCache.
|
|
143
|
+
if (user) await userCache.update(user);
|
|
137
144
|
return user;
|
|
138
145
|
})
|
|
139
146
|
.resolve(async ({ args }) => {
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
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;CACA;CACA;CAEA,YAAY,QAAgB,SAA8B;
|
|
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;CACA;CACA;CAEA,YAAY,QAAgB,SAA8B;EACzD,KAAK,gBAAgB,SAAS,UAAU;EACxC,KAAK,SAAS;EACd,KAAK,UAAU;CAChB;CAEA,WACC,SACA,SACA,GAAG,SACC;EACJ,OAAO,KAAK,OAAO,QAAQ,KAAK,WAAW,OAAO,GAAG,SAAS,GAAG,IAAI;CACtE;CAEA,aACC,SACA,WACA,GAAG,SACC;EACJ,OAAO,KAAK,OAAO,UAAU,KAAK,WAAW,OAAO,GAAG,WAAW,GAAG,IAAI;CAC1E;CAEA,eAAe,OAAe,GAAG,SAAuD;EACvF,OAAO,KAAK,OAAO,YAAY,OAAO,GAAG,IAAI;CAC9C;CAEA,yBACC,UACA,GAAG,SACC;EACJ,OAAO,KAAK,OAAO,sBAA8B,KAAK,YAAY,QAAQ,GAAG,GAAG,IAAI;CACrF;CAEA,cAA6C,YAAuB;EACnE,OAAO,GAAG,KAAK,gBAAgB,QAAQ,SAAS;CACjD;CAEA,eAA8C,aAAyC;EACtF,OAAO,MAAM,QAAQ,QAAQ,IAAI,SAAS,IAAI,KAAK,UAAU,IAAI,KAAK,WAAW,QAAQ;CAC1F;AACD"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@baeta/subscriptions-pubsub",
|
|
3
|
-
"version": "2.0.0-next.
|
|
3
|
+
"version": "2.0.0-next.15",
|
|
4
|
+
"description": "Typed PubSub helpers for Baeta subscriptions.",
|
|
4
5
|
"keywords": [
|
|
5
6
|
"baeta",
|
|
6
7
|
"builder",
|
|
@@ -21,14 +22,14 @@
|
|
|
21
22
|
},
|
|
22
23
|
"repository": {
|
|
23
24
|
"type": "git",
|
|
24
|
-
"url": "https://github.com/andreisergiu98/baeta.git",
|
|
25
|
+
"url": "git+https://github.com/andreisergiu98/baeta.git",
|
|
25
26
|
"directory": "packages/subscriptions-pubsub"
|
|
26
27
|
},
|
|
27
28
|
"files": [
|
|
28
|
-
"dist"
|
|
29
|
-
"package.json"
|
|
29
|
+
"dist"
|
|
30
30
|
],
|
|
31
31
|
"type": "module",
|
|
32
|
+
"sideEffects": false,
|
|
32
33
|
"types": "dist/index.d.ts",
|
|
33
34
|
"exports": {
|
|
34
35
|
".": {
|
|
@@ -37,13 +38,13 @@
|
|
|
37
38
|
}
|
|
38
39
|
},
|
|
39
40
|
"publishConfig": {
|
|
40
|
-
"access": "public",
|
|
41
41
|
"exports": {
|
|
42
42
|
".": {
|
|
43
43
|
"types": "./dist/index.d.ts",
|
|
44
44
|
"default": "./dist/index.js"
|
|
45
45
|
}
|
|
46
|
-
}
|
|
46
|
+
},
|
|
47
|
+
"access": "public"
|
|
47
48
|
},
|
|
48
49
|
"scripts": {
|
|
49
50
|
"build": "builder build",
|
|
@@ -67,7 +68,7 @@
|
|
|
67
68
|
]
|
|
68
69
|
},
|
|
69
70
|
"engines": {
|
|
70
|
-
"node": "
|
|
71
|
+
"node": "^22.20.0 || ^24.0.0 || >=26.0.0"
|
|
71
72
|
},
|
|
72
73
|
"typedocOptions": {
|
|
73
74
|
"entryPoints": [
|