@bunworks/inngest-realtime 0.1.0 → 0.1.5
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 -6
- package/README.md +1 -1
- package/_virtual/_rolldown/runtime.cjs +29 -0
- package/api.cjs +45 -0
- package/api.cjs.map +1 -0
- package/api.mjs +44 -0
- package/api.mjs.map +1 -0
- package/channel.cjs +67 -0
- package/channel.cjs.map +1 -0
- package/channel.d.cts +19 -0
- package/channel.d.cts.map +1 -0
- package/channel.d.mts +19 -0
- package/channel.d.mts.map +1 -0
- package/channel.mjs +66 -0
- package/channel.mjs.map +1 -0
- package/env.cjs +72 -0
- package/env.cjs.map +1 -0
- package/env.mjs +71 -0
- package/env.mjs.map +1 -0
- package/hooks.cjs +147 -0
- package/hooks.cjs.map +1 -0
- package/hooks.d.cts +70 -0
- package/hooks.d.cts.map +1 -0
- package/hooks.d.mts +70 -0
- package/hooks.d.mts.map +1 -0
- package/hooks.mjs +144 -0
- package/hooks.mjs.map +1 -0
- package/index.cjs +19 -0
- package/index.d.cts +5 -0
- package/index.d.mts +6 -0
- package/index.mjs +7 -0
- package/middleware.cjs +41 -0
- package/middleware.cjs.map +1 -0
- package/middleware.d.cts +29 -0
- package/middleware.d.cts.map +1 -0
- package/middleware.d.mts +29 -0
- package/middleware.d.mts.map +1 -0
- package/middleware.mjs +34 -0
- package/middleware.mjs.map +1 -0
- package/package.json +6 -5
- package/subscribe/StreamFanout.cjs +47 -0
- package/subscribe/StreamFanout.cjs.map +1 -0
- package/subscribe/StreamFanout.mjs +46 -0
- package/subscribe/StreamFanout.mjs.map +1 -0
- package/subscribe/TokenSubscription.cjs +438 -0
- package/subscribe/TokenSubscription.cjs.map +1 -0
- package/subscribe/TokenSubscription.mjs +436 -0
- package/subscribe/TokenSubscription.mjs.map +1 -0
- package/subscribe/helpers.cjs +41 -0
- package/subscribe/helpers.cjs.map +1 -0
- package/subscribe/helpers.d.cts +64 -0
- package/subscribe/helpers.d.cts.map +1 -0
- package/subscribe/helpers.d.mts +64 -0
- package/subscribe/helpers.d.mts.map +1 -0
- package/subscribe/helpers.mjs +40 -0
- package/subscribe/helpers.mjs.map +1 -0
- package/subscribe/index.cjs +1 -0
- package/subscribe/index.d.mts +1 -0
- package/subscribe/index.mjs +3 -0
- package/topic.cjs +30 -0
- package/topic.cjs.map +1 -0
- package/topic.d.cts +19 -0
- package/topic.d.cts.map +1 -0
- package/topic.d.mts +19 -0
- package/topic.d.mts.map +1 -0
- package/topic.mjs +28 -0
- package/topic.mjs.map +1 -0
- package/types.cjs +45 -0
- package/types.cjs.map +1 -0
- package/types.d.cts +241 -0
- package/types.d.cts.map +1 -0
- package/types.d.mts +241 -0
- package/types.d.mts.map +1 -0
- package/types.mjs +39 -0
- package/types.mjs.map +1 -0
- package/util.cjs +72 -0
- package/util.cjs.map +1 -0
- package/util.mjs +69 -0
- package/util.mjs.map +1 -0
- package/CLAUDE.md +0 -69
- package/bun.lock +0 -527
- package/eslint.config.mjs +0 -19
- package/src/api.ts +0 -83
- package/src/channel.ts +0 -122
- package/src/env.d.ts +0 -15
- package/src/env.ts +0 -152
- package/src/hooks.ts +0 -256
- package/src/index.test.ts +0 -840
- package/src/index.ts +0 -4
- package/src/middleware.ts +0 -68
- package/src/subscribe/StreamFanout.ts +0 -82
- package/src/subscribe/TokenSubscription.ts +0 -524
- package/src/subscribe/helpers.ts +0 -153
- package/src/subscribe/index.ts +0 -1
- package/src/topic.ts +0 -51
- package/src/types.ts +0 -499
- package/src/util.ts +0 -104
- package/tsconfig.build.json +0 -7
- package/tsconfig.json +0 -114
- package/tsdown.config.ts +0 -18
- package/vitest.config.ts +0 -22
package/types.d.cts
ADDED
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
import { StandardSchemaV1 } from "@standard-schema/spec";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
|
|
4
|
+
//#region src/types.d.ts
|
|
5
|
+
declare namespace Realtime {
|
|
6
|
+
type PublishFn = <TMessage extends MaybePromise<Realtime.Message.Input>>(message: TMessage) => Promise<Awaited<TMessage>["data"]>;
|
|
7
|
+
type Token<TChannel extends Channel | Channel.Definition, TTopics extends (keyof Channel.InferTopics<Channel.Definition.AsChannel<TChannel>> & string)[] = (keyof Channel.InferTopics<Channel.Definition.AsChannel<TChannel>> & string)[]> = TChannel extends Channel.Definition ? Subscribe.Token<Channel.Definition.AsChannel<TChannel>, TTopics> : TChannel extends Channel ? Subscribe.Token<TChannel, TTopics> : never;
|
|
8
|
+
namespace Subscribe {
|
|
9
|
+
type InferChannelInput<T> = T extends Realtime.Channel.Definition ? Realtime.Channel.Definition.InferId<T> : T;
|
|
10
|
+
type StreamSubscription<TSubscribeToken extends Token = Token, TData extends Simplify<Token.InferMessage<TSubscribeToken>> = Simplify<Token.InferMessage<TSubscribeToken>>> = ReadableStream<TData> & {
|
|
11
|
+
/**
|
|
12
|
+
* Get a new readable stream from the subscription that delivers JSON chunks.
|
|
13
|
+
*
|
|
14
|
+
* The stream starts when this function is called and will not contain any
|
|
15
|
+
* messages that were sent before this function was called.
|
|
16
|
+
*/
|
|
17
|
+
getJsonStream(): ReadableStream<TData>;
|
|
18
|
+
/**
|
|
19
|
+
* Get a new readable stream from the subscription that delivers
|
|
20
|
+
* SSE-compatible chunks, which are compatible with the `EventSource` API
|
|
21
|
+
* and generally used for streaming data from a server to the browser.
|
|
22
|
+
*
|
|
23
|
+
* The stream starts when this function is called and will not contain any
|
|
24
|
+
* messages that were sent before this function was called.
|
|
25
|
+
*/
|
|
26
|
+
getEncodedStream(): ReadableStream<Uint8Array>;
|
|
27
|
+
};
|
|
28
|
+
type Callback<TSubscribeToken extends Subscribe.Token = Subscribe.Token> = (message: Token.InferMessage<TSubscribeToken>) => void;
|
|
29
|
+
interface Token<TChannel extends Channel | Channel.Definition = Channel, TTopics extends (keyof Channel.InferTopics<TChannel>)[] = (keyof Channel.InferTopics<TChannel>)[]> {
|
|
30
|
+
key?: string | undefined;
|
|
31
|
+
channel: Realtime.Channel.Definition.AsChannel<TChannel>;
|
|
32
|
+
topics: TTopics;
|
|
33
|
+
}
|
|
34
|
+
namespace Token {
|
|
35
|
+
type InferChannel<TToken extends Token> = TToken extends Token<infer IChannel, any> ? IChannel : Channel;
|
|
36
|
+
type InferTopicData<TToken extends Token, TChannelTopics extends Record<string, Topic.Definition> = Channel.InferTopics<Token.InferChannel<TToken>>> = TToken extends Token<any, infer ITopics> ? { [K in ITopics[number]]: TChannelTopics[K] } : never;
|
|
37
|
+
type InferMessage<TToken extends Token> = Simplify<Realtime.Message<Channel.InferId<Token.InferChannel<TToken>>, Token.InferTopicData<TToken>>>;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
const messageSchema: z.ZodPipe<z.ZodObject<{
|
|
41
|
+
channel: z.ZodOptional<z.ZodString>;
|
|
42
|
+
topic: z.ZodOptional<z.ZodString>;
|
|
43
|
+
data: z.ZodOptional<z.ZodAny>;
|
|
44
|
+
run_id: z.ZodOptional<z.ZodString>;
|
|
45
|
+
fn_id: z.ZodOptional<z.ZodString>;
|
|
46
|
+
created_at: z.ZodPipe<z.ZodOptional<z.ZodString>, z.ZodTransform<Date | undefined, string | undefined>>;
|
|
47
|
+
env_id: z.ZodOptional<z.ZodString>;
|
|
48
|
+
stream_id: z.ZodOptional<z.ZodString>;
|
|
49
|
+
kind: z.ZodEnum<{
|
|
50
|
+
data: "data";
|
|
51
|
+
step: "step";
|
|
52
|
+
run: "run";
|
|
53
|
+
ping: "ping";
|
|
54
|
+
pong: "pong";
|
|
55
|
+
closing: "closing";
|
|
56
|
+
event: "event";
|
|
57
|
+
sub: "sub";
|
|
58
|
+
unsub: "unsub";
|
|
59
|
+
"datastream-start": "datastream-start";
|
|
60
|
+
"datastream-end": "datastream-end";
|
|
61
|
+
chunk: "chunk";
|
|
62
|
+
}>;
|
|
63
|
+
}, z.core.$strip>, z.ZodTransform<{
|
|
64
|
+
data: any;
|
|
65
|
+
created_at: Date | undefined;
|
|
66
|
+
kind: "data" | "step" | "run" | "ping" | "pong" | "closing" | "event" | "sub" | "unsub" | "datastream-start" | "datastream-end" | "chunk";
|
|
67
|
+
channel?: string | undefined;
|
|
68
|
+
topic?: string | undefined;
|
|
69
|
+
run_id?: string | undefined;
|
|
70
|
+
fn_id?: string | undefined;
|
|
71
|
+
env_id?: string | undefined;
|
|
72
|
+
stream_id?: string | undefined;
|
|
73
|
+
}, {
|
|
74
|
+
created_at: Date | undefined;
|
|
75
|
+
kind: "data" | "step" | "run" | "ping" | "pong" | "closing" | "event" | "sub" | "unsub" | "datastream-start" | "datastream-end" | "chunk";
|
|
76
|
+
channel?: string | undefined;
|
|
77
|
+
topic?: string | undefined;
|
|
78
|
+
data?: any;
|
|
79
|
+
run_id?: string | undefined;
|
|
80
|
+
fn_id?: string | undefined;
|
|
81
|
+
env_id?: string | undefined;
|
|
82
|
+
stream_id?: string | undefined;
|
|
83
|
+
}>>;
|
|
84
|
+
type Message<TChannelId extends string = string, TTopics extends Record<string, Realtime.Topic.Definition> = Record<string, Realtime.Topic.Definition>> = { [K in keyof TTopics]: {
|
|
85
|
+
topic: K;
|
|
86
|
+
channel: TChannelId;
|
|
87
|
+
data: Realtime.Topic.InferSubscribe<TTopics[K]>;
|
|
88
|
+
runId?: string;
|
|
89
|
+
fnId?: string;
|
|
90
|
+
createdAt: Date;
|
|
91
|
+
envId?: string;
|
|
92
|
+
kind: "data";
|
|
93
|
+
} | {
|
|
94
|
+
topic: K;
|
|
95
|
+
channel: TChannelId;
|
|
96
|
+
data: Realtime.Topic.InferSubscribe<TTopics[K]>;
|
|
97
|
+
runId?: string;
|
|
98
|
+
fnId?: string;
|
|
99
|
+
kind: "datastream-start" | "datastream-end" | "chunk";
|
|
100
|
+
streamId: string;
|
|
101
|
+
stream: ReadableStream<Realtime.Topic.InferSubscribe<TTopics[K]>>;
|
|
102
|
+
} }[keyof TTopics];
|
|
103
|
+
namespace Message {
|
|
104
|
+
type Input<TChannelId extends string = string, TTopicId extends string = string, TData = any> = {
|
|
105
|
+
channel: TChannelId;
|
|
106
|
+
topic: TTopicId;
|
|
107
|
+
data: TData;
|
|
108
|
+
};
|
|
109
|
+
type Raw<TChannelId extends string = string, TTopics extends Record<string, Realtime.Topic.Definition> = Record<string, Realtime.Topic.Definition>> = { [K in keyof TTopics]: {
|
|
110
|
+
topic?: K;
|
|
111
|
+
stream_id?: string;
|
|
112
|
+
data: Realtime.Topic.InferSubscribe<TTopics[K]>;
|
|
113
|
+
channel?: TChannelId;
|
|
114
|
+
run_id?: string;
|
|
115
|
+
fn_id?: string;
|
|
116
|
+
created_at?: Date;
|
|
117
|
+
env_id?: string;
|
|
118
|
+
kind: "step" | "run" | "data" | "datastream-start" | "datastream-end" | "ping" | "pong" | "closing" | "event" | "sub" | "unsub" | "chunk";
|
|
119
|
+
} }[keyof TTopics];
|
|
120
|
+
}
|
|
121
|
+
type Channel<TChannelId extends string = string, TTopics extends Record<string, Realtime.Topic.Definition> = Record<string, Realtime.Topic.Definition>> = { [K in IsLiteral<keyof TTopics, keyof TTopics, never> | "name" | "topics"]: K extends "name" ? string : K extends "topics" ? TTopics : Realtime.Topic<TChannelId, TTopics[K]> };
|
|
122
|
+
namespace Channel {
|
|
123
|
+
type Like = {
|
|
124
|
+
channel: string;
|
|
125
|
+
topics: string[];
|
|
126
|
+
};
|
|
127
|
+
type InferId<TChannel extends Channel> = TChannel extends Channel<infer IId, any> ? IId : string;
|
|
128
|
+
type AsChannel<T extends Channel | string> = T extends Channel ? T : T extends string ? Realtime.Channel<T> : never;
|
|
129
|
+
type InferTopics<TChannel extends Channel | Channel.Definition> = TChannel extends Channel.Definition<any, infer ITopics> ? ITopics : TChannel extends Channel<any, infer ITopics> ? ITopics : Record<string, Realtime.Topic.Definition>;
|
|
130
|
+
interface Definition<TChannelBuilderFn extends BuilderFn = (...args: any[]) => string, TTopics extends Record<string, Topic.Definition> = Record<string, Topic.Definition>> {
|
|
131
|
+
(...args: Parameters<TChannelBuilderFn>): Channel<ReturnType<TChannelBuilderFn>, TTopics>;
|
|
132
|
+
addTopic<UTopic extends Topic.Definition>(topic: UTopic): Definition<TChannelBuilderFn, AddTopic<TTopics, UTopic>>;
|
|
133
|
+
topics: TTopics;
|
|
134
|
+
}
|
|
135
|
+
namespace Definition {
|
|
136
|
+
type InferId<TChannel extends Definition> = TChannel extends Definition<infer IBuilder, any> ? ReturnType<IBuilder> : string;
|
|
137
|
+
type InferTopics<TChannel extends Definition> = TChannel extends Definition<any, infer ITopics> ? ITopics : Record<string, Topic.Definition>;
|
|
138
|
+
type AsChannel<T extends Definition | Channel> = T extends Definition ? Channel<InferId<T>, InferTopics<T>> : T extends Channel ? T : never;
|
|
139
|
+
}
|
|
140
|
+
type AddTopic<TCurr extends Record<string, Topic.Definition>, TInc extends Topic.Definition, TIncWrapped extends Record<TInc["name"], TInc> = Record<TInc["name"], TInc>> = IsStringLiteral<keyof TCurr & string> extends true ? Simplify<Omit<TCurr, TInc["name"]> & TIncWrapped> : TIncWrapped;
|
|
141
|
+
type BuilderFn<TChannelId extends string = string> = (...args: any[]) => TChannelId;
|
|
142
|
+
type Builder = <const TChannelId extends string, const TIdInput extends TChannelId | BuilderFn<TChannelId>>(id: TIdInput) => TIdInput extends TChannelId ? Channel.Definition<() => TIdInput> : TIdInput extends BuilderFn<TChannelId> ? Channel.Definition<TIdInput> : never;
|
|
143
|
+
}
|
|
144
|
+
type Topic<TChannelId extends string = string, TTopic extends Topic.Definition = Topic.Definition> = (data: Topic.InferPublish<TTopic>) => Promise<Realtime.Message.Input<TChannelId, Topic.InferId<TTopic>, Topic.InferPublish<TTopic>>>;
|
|
145
|
+
namespace Topic {
|
|
146
|
+
type Like = {
|
|
147
|
+
name: string;
|
|
148
|
+
};
|
|
149
|
+
interface Definition<TTopicId extends string = string, TPublish = any, _TSubscribe = TPublish> {
|
|
150
|
+
name: TTopicId;
|
|
151
|
+
type<const UPublish>(): Definition<TTopicId, UPublish>;
|
|
152
|
+
schema<const TSchema extends StandardSchemaV1>(schema: TSchema): Definition<TTopicId, StandardSchemaV1.InferInput<TSchema>, StandardSchemaV1.InferOutput<TSchema>>;
|
|
153
|
+
getSchema(): StandardSchemaV1 | undefined;
|
|
154
|
+
}
|
|
155
|
+
type InferId<TTopic extends Topic.Definition> = TTopic extends Topic.Definition<infer IId, any, any> ? IId : string;
|
|
156
|
+
type InferPublish<TTopic extends Topic.Definition> = TTopic extends Topic.Definition<any, infer IPublish, any> ? IPublish : any;
|
|
157
|
+
type InferSubscribe<TTopic extends Topic.Definition> = TTopic extends Topic.Definition<any, any, infer ISubscribe> ? ISubscribe : any;
|
|
158
|
+
type Builder = <const TTopicId extends string>(id: TTopicId) => Topic.Definition<TTopicId>;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* Expects that a value resolves to `true`, useful for asserting type checks.
|
|
163
|
+
*/
|
|
164
|
+
type Expect<T extends true> = T;
|
|
165
|
+
/**
|
|
166
|
+
Returns a boolean for whether the two given types are equal.
|
|
167
|
+
|
|
168
|
+
{@link https://github.com/microsoft/TypeScript/issues/27024#issuecomment-421529650}
|
|
169
|
+
{@link https://stackoverflow.com/questions/68961864/how-does-the-equals-work-in-typescript/68963796#68963796}
|
|
170
|
+
|
|
171
|
+
Use-cases:
|
|
172
|
+
- If you want to make a conditional branch based on the result of a comparison of two types.
|
|
173
|
+
|
|
174
|
+
@example
|
|
175
|
+
```
|
|
176
|
+
import type {IsEqual} from 'type-fest';
|
|
177
|
+
|
|
178
|
+
// This type returns a boolean for whether the given array includes the given item.
|
|
179
|
+
// `IsEqual` is used to compare the given array at position 0 and the given item and then return true if they are equal.
|
|
180
|
+
type Includes<Value extends readonly any[], Item> =
|
|
181
|
+
Value extends readonly [Value[0], ...infer rest]
|
|
182
|
+
? IsEqual<Value[0], Item> extends true
|
|
183
|
+
? true
|
|
184
|
+
: Includes<rest, Item>
|
|
185
|
+
: false;
|
|
186
|
+
```
|
|
187
|
+
*/
|
|
188
|
+
type IsEqual<A, B> = (<G>() => G extends A ? 1 : 2) extends (<G>() => G extends B ? 1 : 2) ? true : false;
|
|
189
|
+
/**
|
|
190
|
+
* Given a type `T`, return `Then` if `T` is a string, number, or symbol
|
|
191
|
+
* literal, else `Else`.
|
|
192
|
+
*
|
|
193
|
+
* `Then` defaults to `true` and `Else` defaults to `false`.
|
|
194
|
+
*
|
|
195
|
+
* Useful for determining if an object is a generic type or has known keys.
|
|
196
|
+
*
|
|
197
|
+
* @example
|
|
198
|
+
* ```ts
|
|
199
|
+
* type IsLiteralType = IsLiteral<"foo">; // true
|
|
200
|
+
* type IsLiteralType = IsLiteral<string>; // false
|
|
201
|
+
*
|
|
202
|
+
* type IsLiteralType = IsLiteral<1>; // true
|
|
203
|
+
* type IsLiteralType = IsLiteral<number>; // false
|
|
204
|
+
*
|
|
205
|
+
* type IsLiteralType = IsLiteral<symbol>; // true
|
|
206
|
+
* type IsLiteralType = IsLiteral<typeof Symbol.iterator>; // false
|
|
207
|
+
*
|
|
208
|
+
* type T0 = { foo: string };
|
|
209
|
+
* type HasAllKnownKeys = IsLiteral<keyof T0>; // true
|
|
210
|
+
*
|
|
211
|
+
* type T1 = { [x: string]: any; foo: boolean };
|
|
212
|
+
* type HasAllKnownKeys = IsLiteral<keyof T1>; // false
|
|
213
|
+
* ```
|
|
214
|
+
*/
|
|
215
|
+
type IsLiteral<T, Then = true, Else = false> = string extends T ? Else : number extends T ? Else : symbol extends T ? Else : Then;
|
|
216
|
+
/**
|
|
217
|
+
* Returns `true` if the given generic `T` is a string literal, e.g. `"foo"`, or
|
|
218
|
+
* `false` if it is a string type, e.g. `string`.
|
|
219
|
+
*
|
|
220
|
+
* Useful for checking whether the keys of an object are known or not.
|
|
221
|
+
*
|
|
222
|
+
* @example
|
|
223
|
+
* ```ts
|
|
224
|
+
* // false
|
|
225
|
+
* type ObjIsGeneric = IsStringLiteral<keyof Record<string, boolean>>;
|
|
226
|
+
*
|
|
227
|
+
* // true
|
|
228
|
+
* type ObjIsKnown = IsStringLiteral<keyof { foo: boolean; }>; // true
|
|
229
|
+
* ```
|
|
230
|
+
*
|
|
231
|
+
* @internal
|
|
232
|
+
*/
|
|
233
|
+
type IsStringLiteral<T extends string> = string extends T ? false : true;
|
|
234
|
+
/**
|
|
235
|
+
* Returns the given generic as either itself or a promise of itself.
|
|
236
|
+
*/
|
|
237
|
+
type MaybePromise<T> = T | Promise<T>;
|
|
238
|
+
type Simplify<T> = { [KeyType in keyof T]: T[KeyType] } & {};
|
|
239
|
+
//#endregion
|
|
240
|
+
export { Expect, IsEqual, IsLiteral, IsStringLiteral, MaybePromise, Realtime, Simplify };
|
|
241
|
+
//# sourceMappingURL=types.d.cts.map
|
package/types.d.cts.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.cts","names":[],"sources":["../src/types.ts"],"mappings":";;;;kBAGiB,QAAA;EAAA,KACH,SAAA,qBACO,YAAA,CAAa,QAAA,CAAS,OAAA,CAAQ,KAAA,GAE/C,OAAA,EAAS,QAAA,KACN,OAAA,CAAQ,OAAA,CAAQ,QAAA;EAAA,KAET,KAAA,kBACO,OAAA,GAAU,OAAA,CAAQ,UAAA,yBACZ,OAAA,CAAQ,WAAA,CAC7B,OAAA,CAAQ,UAAA,CAAW,SAAA,CAAU,QAAA,wBAEV,OAAA,CAAQ,WAAA,CAC3B,OAAA,CAAQ,UAAA,CAAW,SAAA,CAAU,QAAA,kBAG7B,QAAA,SAAiB,OAAA,CAAQ,UAAA,GACzB,SAAA,CAAU,KAAA,CAAM,OAAA,CAAQ,UAAA,CAAW,SAAA,CAAU,QAAA,GAAW,OAAA,IACxD,QAAA,SAAiB,OAAA,GACf,SAAA,CAAU,KAAA,CAAM,QAAA,EAAU,OAAA;EAAA,UAGf,SAAA;IAAA,KACH,iBAAA,MAAuB,CAAA,SAAU,QAAA,CAAS,OAAA,CAAQ,UAAA,GAC1D,QAAA,CAAS,OAAA,CAAQ,UAAA,CAAW,OAAA,CA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A,IAEvC,MAAA,SAAe,KAAA,CAAM,UAAA,wBAAkC,GAAA;IAAA,KAE7C,YAAA,gBAA4B,KAAA,CAAM,UAAA,IAE5C,MAAA,SAAe,KAAA,CAAM,UAAA,6BACjB,QAAA;IAAA,KAIM,cAAA,gBAA8B,KAAA,CAAM,UAAA,IAE9C,MAAA,SAAe,KAAA,CAAM,UAAA,+BACjB,UAAA;IAAA,KAIM,OAAA,mCACV,EAAA,EAAI,QAAA,KACD,KAAA,CAAM,UAAA,CAAW,QAAA;EAAA;AAAA;;;;KAOd,MAAA,mBAAyB,CAAA;;;;;;;;;;;;;;;;;;;;;;;;KAyBzB,OAAA,mBACA,CAAA,SAAU,CAAA,4BAA4B,CAAA,SAAU,CAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;KA8BhD,SAAA,gDAAyD,CAAA,GACjE,IAAA,kBACe,CAAA,GACb,IAAA,kBACe,CAAA,GACb,IAAA,GACA,IAAA;;;;;;;;;;;;;;;;;;KAmBI,eAAA,oCAAmD,CAAA;;;;KAKnD,YAAA,MAAkB,CAAA,GAAI,OAAA,CAAQ,CAAA;AAAA,KAE9B,QAAA,0BAAkC,CAAA,GAAI,CAAA,CAAE,OAAA"}
|
package/types.d.mts
ADDED
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { StandardSchemaV1 } from "@standard-schema/spec";
|
|
3
|
+
|
|
4
|
+
//#region src/types.d.ts
|
|
5
|
+
declare namespace Realtime {
|
|
6
|
+
type PublishFn = <TMessage extends MaybePromise<Realtime.Message.Input>>(message: TMessage) => Promise<Awaited<TMessage>["data"]>;
|
|
7
|
+
type Token<TChannel extends Channel | Channel.Definition, TTopics extends (keyof Channel.InferTopics<Channel.Definition.AsChannel<TChannel>> & string)[] = (keyof Channel.InferTopics<Channel.Definition.AsChannel<TChannel>> & string)[]> = TChannel extends Channel.Definition ? Subscribe.Token<Channel.Definition.AsChannel<TChannel>, TTopics> : TChannel extends Channel ? Subscribe.Token<TChannel, TTopics> : never;
|
|
8
|
+
namespace Subscribe {
|
|
9
|
+
type InferChannelInput<T> = T extends Realtime.Channel.Definition ? Realtime.Channel.Definition.InferId<T> : T;
|
|
10
|
+
type StreamSubscription<TSubscribeToken extends Token = Token, TData extends Simplify<Token.InferMessage<TSubscribeToken>> = Simplify<Token.InferMessage<TSubscribeToken>>> = ReadableStream<TData> & {
|
|
11
|
+
/**
|
|
12
|
+
* Get a new readable stream from the subscription that delivers JSON chunks.
|
|
13
|
+
*
|
|
14
|
+
* The stream starts when this function is called and will not contain any
|
|
15
|
+
* messages that were sent before this function was called.
|
|
16
|
+
*/
|
|
17
|
+
getJsonStream(): ReadableStream<TData>;
|
|
18
|
+
/**
|
|
19
|
+
* Get a new readable stream from the subscription that delivers
|
|
20
|
+
* SSE-compatible chunks, which are compatible with the `EventSource` API
|
|
21
|
+
* and generally used for streaming data from a server to the browser.
|
|
22
|
+
*
|
|
23
|
+
* The stream starts when this function is called and will not contain any
|
|
24
|
+
* messages that were sent before this function was called.
|
|
25
|
+
*/
|
|
26
|
+
getEncodedStream(): ReadableStream<Uint8Array>;
|
|
27
|
+
};
|
|
28
|
+
type Callback<TSubscribeToken extends Subscribe.Token = Subscribe.Token> = (message: Token.InferMessage<TSubscribeToken>) => void;
|
|
29
|
+
interface Token<TChannel extends Channel | Channel.Definition = Channel, TTopics extends (keyof Channel.InferTopics<TChannel>)[] = (keyof Channel.InferTopics<TChannel>)[]> {
|
|
30
|
+
key?: string | undefined;
|
|
31
|
+
channel: Realtime.Channel.Definition.AsChannel<TChannel>;
|
|
32
|
+
topics: TTopics;
|
|
33
|
+
}
|
|
34
|
+
namespace Token {
|
|
35
|
+
type InferChannel<TToken extends Token> = TToken extends Token<infer IChannel, any> ? IChannel : Channel;
|
|
36
|
+
type InferTopicData<TToken extends Token, TChannelTopics extends Record<string, Topic.Definition> = Channel.InferTopics<Token.InferChannel<TToken>>> = TToken extends Token<any, infer ITopics> ? { [K in ITopics[number]]: TChannelTopics[K] } : never;
|
|
37
|
+
type InferMessage<TToken extends Token> = Simplify<Realtime.Message<Channel.InferId<Token.InferChannel<TToken>>, Token.InferTopicData<TToken>>>;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
const messageSchema: z.ZodPipe<z.ZodObject<{
|
|
41
|
+
channel: z.ZodOptional<z.ZodString>;
|
|
42
|
+
topic: z.ZodOptional<z.ZodString>;
|
|
43
|
+
data: z.ZodOptional<z.ZodAny>;
|
|
44
|
+
run_id: z.ZodOptional<z.ZodString>;
|
|
45
|
+
fn_id: z.ZodOptional<z.ZodString>;
|
|
46
|
+
created_at: z.ZodPipe<z.ZodOptional<z.ZodString>, z.ZodTransform<Date | undefined, string | undefined>>;
|
|
47
|
+
env_id: z.ZodOptional<z.ZodString>;
|
|
48
|
+
stream_id: z.ZodOptional<z.ZodString>;
|
|
49
|
+
kind: z.ZodEnum<{
|
|
50
|
+
data: "data";
|
|
51
|
+
step: "step";
|
|
52
|
+
run: "run";
|
|
53
|
+
ping: "ping";
|
|
54
|
+
pong: "pong";
|
|
55
|
+
closing: "closing";
|
|
56
|
+
event: "event";
|
|
57
|
+
sub: "sub";
|
|
58
|
+
unsub: "unsub";
|
|
59
|
+
"datastream-start": "datastream-start";
|
|
60
|
+
"datastream-end": "datastream-end";
|
|
61
|
+
chunk: "chunk";
|
|
62
|
+
}>;
|
|
63
|
+
}, z.core.$strip>, z.ZodTransform<{
|
|
64
|
+
data: any;
|
|
65
|
+
created_at: Date | undefined;
|
|
66
|
+
kind: "data" | "step" | "run" | "ping" | "pong" | "closing" | "event" | "sub" | "unsub" | "datastream-start" | "datastream-end" | "chunk";
|
|
67
|
+
channel?: string | undefined;
|
|
68
|
+
topic?: string | undefined;
|
|
69
|
+
run_id?: string | undefined;
|
|
70
|
+
fn_id?: string | undefined;
|
|
71
|
+
env_id?: string | undefined;
|
|
72
|
+
stream_id?: string | undefined;
|
|
73
|
+
}, {
|
|
74
|
+
created_at: Date | undefined;
|
|
75
|
+
kind: "data" | "step" | "run" | "ping" | "pong" | "closing" | "event" | "sub" | "unsub" | "datastream-start" | "datastream-end" | "chunk";
|
|
76
|
+
channel?: string | undefined;
|
|
77
|
+
topic?: string | undefined;
|
|
78
|
+
data?: any;
|
|
79
|
+
run_id?: string | undefined;
|
|
80
|
+
fn_id?: string | undefined;
|
|
81
|
+
env_id?: string | undefined;
|
|
82
|
+
stream_id?: string | undefined;
|
|
83
|
+
}>>;
|
|
84
|
+
type Message<TChannelId extends string = string, TTopics extends Record<string, Realtime.Topic.Definition> = Record<string, Realtime.Topic.Definition>> = { [K in keyof TTopics]: {
|
|
85
|
+
topic: K;
|
|
86
|
+
channel: TChannelId;
|
|
87
|
+
data: Realtime.Topic.InferSubscribe<TTopics[K]>;
|
|
88
|
+
runId?: string;
|
|
89
|
+
fnId?: string;
|
|
90
|
+
createdAt: Date;
|
|
91
|
+
envId?: string;
|
|
92
|
+
kind: "data";
|
|
93
|
+
} | {
|
|
94
|
+
topic: K;
|
|
95
|
+
channel: TChannelId;
|
|
96
|
+
data: Realtime.Topic.InferSubscribe<TTopics[K]>;
|
|
97
|
+
runId?: string;
|
|
98
|
+
fnId?: string;
|
|
99
|
+
kind: "datastream-start" | "datastream-end" | "chunk";
|
|
100
|
+
streamId: string;
|
|
101
|
+
stream: ReadableStream<Realtime.Topic.InferSubscribe<TTopics[K]>>;
|
|
102
|
+
} }[keyof TTopics];
|
|
103
|
+
namespace Message {
|
|
104
|
+
type Input<TChannelId extends string = string, TTopicId extends string = string, TData = any> = {
|
|
105
|
+
channel: TChannelId;
|
|
106
|
+
topic: TTopicId;
|
|
107
|
+
data: TData;
|
|
108
|
+
};
|
|
109
|
+
type Raw<TChannelId extends string = string, TTopics extends Record<string, Realtime.Topic.Definition> = Record<string, Realtime.Topic.Definition>> = { [K in keyof TTopics]: {
|
|
110
|
+
topic?: K;
|
|
111
|
+
stream_id?: string;
|
|
112
|
+
data: Realtime.Topic.InferSubscribe<TTopics[K]>;
|
|
113
|
+
channel?: TChannelId;
|
|
114
|
+
run_id?: string;
|
|
115
|
+
fn_id?: string;
|
|
116
|
+
created_at?: Date;
|
|
117
|
+
env_id?: string;
|
|
118
|
+
kind: "step" | "run" | "data" | "datastream-start" | "datastream-end" | "ping" | "pong" | "closing" | "event" | "sub" | "unsub" | "chunk";
|
|
119
|
+
} }[keyof TTopics];
|
|
120
|
+
}
|
|
121
|
+
type Channel<TChannelId extends string = string, TTopics extends Record<string, Realtime.Topic.Definition> = Record<string, Realtime.Topic.Definition>> = { [K in IsLiteral<keyof TTopics, keyof TTopics, never> | "name" | "topics"]: K extends "name" ? string : K extends "topics" ? TTopics : Realtime.Topic<TChannelId, TTopics[K]> };
|
|
122
|
+
namespace Channel {
|
|
123
|
+
type Like = {
|
|
124
|
+
channel: string;
|
|
125
|
+
topics: string[];
|
|
126
|
+
};
|
|
127
|
+
type InferId<TChannel extends Channel> = TChannel extends Channel<infer IId, any> ? IId : string;
|
|
128
|
+
type AsChannel<T extends Channel | string> = T extends Channel ? T : T extends string ? Realtime.Channel<T> : never;
|
|
129
|
+
type InferTopics<TChannel extends Channel | Channel.Definition> = TChannel extends Channel.Definition<any, infer ITopics> ? ITopics : TChannel extends Channel<any, infer ITopics> ? ITopics : Record<string, Realtime.Topic.Definition>;
|
|
130
|
+
interface Definition<TChannelBuilderFn extends BuilderFn = (...args: any[]) => string, TTopics extends Record<string, Topic.Definition> = Record<string, Topic.Definition>> {
|
|
131
|
+
(...args: Parameters<TChannelBuilderFn>): Channel<ReturnType<TChannelBuilderFn>, TTopics>;
|
|
132
|
+
addTopic<UTopic extends Topic.Definition>(topic: UTopic): Definition<TChannelBuilderFn, AddTopic<TTopics, UTopic>>;
|
|
133
|
+
topics: TTopics;
|
|
134
|
+
}
|
|
135
|
+
namespace Definition {
|
|
136
|
+
type InferId<TChannel extends Definition> = TChannel extends Definition<infer IBuilder, any> ? ReturnType<IBuilder> : string;
|
|
137
|
+
type InferTopics<TChannel extends Definition> = TChannel extends Definition<any, infer ITopics> ? ITopics : Record<string, Topic.Definition>;
|
|
138
|
+
type AsChannel<T extends Definition | Channel> = T extends Definition ? Channel<InferId<T>, InferTopics<T>> : T extends Channel ? T : never;
|
|
139
|
+
}
|
|
140
|
+
type AddTopic<TCurr extends Record<string, Topic.Definition>, TInc extends Topic.Definition, TIncWrapped extends Record<TInc["name"], TInc> = Record<TInc["name"], TInc>> = IsStringLiteral<keyof TCurr & string> extends true ? Simplify<Omit<TCurr, TInc["name"]> & TIncWrapped> : TIncWrapped;
|
|
141
|
+
type BuilderFn<TChannelId extends string = string> = (...args: any[]) => TChannelId;
|
|
142
|
+
type Builder = <const TChannelId extends string, const TIdInput extends TChannelId | BuilderFn<TChannelId>>(id: TIdInput) => TIdInput extends TChannelId ? Channel.Definition<() => TIdInput> : TIdInput extends BuilderFn<TChannelId> ? Channel.Definition<TIdInput> : never;
|
|
143
|
+
}
|
|
144
|
+
type Topic<TChannelId extends string = string, TTopic extends Topic.Definition = Topic.Definition> = (data: Topic.InferPublish<TTopic>) => Promise<Realtime.Message.Input<TChannelId, Topic.InferId<TTopic>, Topic.InferPublish<TTopic>>>;
|
|
145
|
+
namespace Topic {
|
|
146
|
+
type Like = {
|
|
147
|
+
name: string;
|
|
148
|
+
};
|
|
149
|
+
interface Definition<TTopicId extends string = string, TPublish = any, _TSubscribe = TPublish> {
|
|
150
|
+
name: TTopicId;
|
|
151
|
+
type<const UPublish>(): Definition<TTopicId, UPublish>;
|
|
152
|
+
schema<const TSchema extends StandardSchemaV1>(schema: TSchema): Definition<TTopicId, StandardSchemaV1.InferInput<TSchema>, StandardSchemaV1.InferOutput<TSchema>>;
|
|
153
|
+
getSchema(): StandardSchemaV1 | undefined;
|
|
154
|
+
}
|
|
155
|
+
type InferId<TTopic extends Topic.Definition> = TTopic extends Topic.Definition<infer IId, any, any> ? IId : string;
|
|
156
|
+
type InferPublish<TTopic extends Topic.Definition> = TTopic extends Topic.Definition<any, infer IPublish, any> ? IPublish : any;
|
|
157
|
+
type InferSubscribe<TTopic extends Topic.Definition> = TTopic extends Topic.Definition<any, any, infer ISubscribe> ? ISubscribe : any;
|
|
158
|
+
type Builder = <const TTopicId extends string>(id: TTopicId) => Topic.Definition<TTopicId>;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* Expects that a value resolves to `true`, useful for asserting type checks.
|
|
163
|
+
*/
|
|
164
|
+
type Expect<T extends true> = T;
|
|
165
|
+
/**
|
|
166
|
+
Returns a boolean for whether the two given types are equal.
|
|
167
|
+
|
|
168
|
+
{@link https://github.com/microsoft/TypeScript/issues/27024#issuecomment-421529650}
|
|
169
|
+
{@link https://stackoverflow.com/questions/68961864/how-does-the-equals-work-in-typescript/68963796#68963796}
|
|
170
|
+
|
|
171
|
+
Use-cases:
|
|
172
|
+
- If you want to make a conditional branch based on the result of a comparison of two types.
|
|
173
|
+
|
|
174
|
+
@example
|
|
175
|
+
```
|
|
176
|
+
import type {IsEqual} from 'type-fest';
|
|
177
|
+
|
|
178
|
+
// This type returns a boolean for whether the given array includes the given item.
|
|
179
|
+
// `IsEqual` is used to compare the given array at position 0 and the given item and then return true if they are equal.
|
|
180
|
+
type Includes<Value extends readonly any[], Item> =
|
|
181
|
+
Value extends readonly [Value[0], ...infer rest]
|
|
182
|
+
? IsEqual<Value[0], Item> extends true
|
|
183
|
+
? true
|
|
184
|
+
: Includes<rest, Item>
|
|
185
|
+
: false;
|
|
186
|
+
```
|
|
187
|
+
*/
|
|
188
|
+
type IsEqual<A, B> = (<G>() => G extends A ? 1 : 2) extends (<G>() => G extends B ? 1 : 2) ? true : false;
|
|
189
|
+
/**
|
|
190
|
+
* Given a type `T`, return `Then` if `T` is a string, number, or symbol
|
|
191
|
+
* literal, else `Else`.
|
|
192
|
+
*
|
|
193
|
+
* `Then` defaults to `true` and `Else` defaults to `false`.
|
|
194
|
+
*
|
|
195
|
+
* Useful for determining if an object is a generic type or has known keys.
|
|
196
|
+
*
|
|
197
|
+
* @example
|
|
198
|
+
* ```ts
|
|
199
|
+
* type IsLiteralType = IsLiteral<"foo">; // true
|
|
200
|
+
* type IsLiteralType = IsLiteral<string>; // false
|
|
201
|
+
*
|
|
202
|
+
* type IsLiteralType = IsLiteral<1>; // true
|
|
203
|
+
* type IsLiteralType = IsLiteral<number>; // false
|
|
204
|
+
*
|
|
205
|
+
* type IsLiteralType = IsLiteral<symbol>; // true
|
|
206
|
+
* type IsLiteralType = IsLiteral<typeof Symbol.iterator>; // false
|
|
207
|
+
*
|
|
208
|
+
* type T0 = { foo: string };
|
|
209
|
+
* type HasAllKnownKeys = IsLiteral<keyof T0>; // true
|
|
210
|
+
*
|
|
211
|
+
* type T1 = { [x: string]: any; foo: boolean };
|
|
212
|
+
* type HasAllKnownKeys = IsLiteral<keyof T1>; // false
|
|
213
|
+
* ```
|
|
214
|
+
*/
|
|
215
|
+
type IsLiteral<T, Then = true, Else = false> = string extends T ? Else : number extends T ? Else : symbol extends T ? Else : Then;
|
|
216
|
+
/**
|
|
217
|
+
* Returns `true` if the given generic `T` is a string literal, e.g. `"foo"`, or
|
|
218
|
+
* `false` if it is a string type, e.g. `string`.
|
|
219
|
+
*
|
|
220
|
+
* Useful for checking whether the keys of an object are known or not.
|
|
221
|
+
*
|
|
222
|
+
* @example
|
|
223
|
+
* ```ts
|
|
224
|
+
* // false
|
|
225
|
+
* type ObjIsGeneric = IsStringLiteral<keyof Record<string, boolean>>;
|
|
226
|
+
*
|
|
227
|
+
* // true
|
|
228
|
+
* type ObjIsKnown = IsStringLiteral<keyof { foo: boolean; }>; // true
|
|
229
|
+
* ```
|
|
230
|
+
*
|
|
231
|
+
* @internal
|
|
232
|
+
*/
|
|
233
|
+
type IsStringLiteral<T extends string> = string extends T ? false : true;
|
|
234
|
+
/**
|
|
235
|
+
* Returns the given generic as either itself or a promise of itself.
|
|
236
|
+
*/
|
|
237
|
+
type MaybePromise<T> = T | Promise<T>;
|
|
238
|
+
type Simplify<T> = { [KeyType in keyof T]: T[KeyType] } & {};
|
|
239
|
+
//#endregion
|
|
240
|
+
export { Expect, IsEqual, IsLiteral, IsStringLiteral, MaybePromise, Realtime, Simplify };
|
|
241
|
+
//# sourceMappingURL=types.d.mts.map
|
package/types.d.mts.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.mts","names":[],"sources":["../src/types.ts"],"mappings":";;;;kBAGiB,QAAA;EAAA,KACH,SAAA,qBACO,YAAA,CAAa,QAAA,CAAS,OAAA,CAAQ,KAAA,GAE/C,OAAA,EAAS,QAAA,KACN,OAAA,CAAQ,OAAA,CAAQ,QAAA;EAAA,KAET,KAAA,kBACO,OAAA,GAAU,OAAA,CAAQ,UAAA,yBACZ,OAAA,CAAQ,WAAA,CAC7B,OAAA,CAAQ,UAAA,CAAW,SAAA,CAAU,QAAA,wBAEV,OAAA,CAAQ,WAAA,CAC3B,OAAA,CAAQ,UAAA,CAAW,SAAA,CAAU,QAAA,kBAG7B,QAAA,SAAiB,OAAA,CAAQ,UAAA,GACzB,SAAA,CAAU,KAAA,CAAM,OAAA,CAAQ,UAAA,CAAW,SAAA,CAAU,QAAA,GAAW,OAAA,IACxD,QAAA,SAAiB,OAAA,GACf,SAAA,CAAU,KAAA,CAAM,QAAA,EAAU,OAAA;EAAA,UAGf,SAAA;IAAA,KACH,iBAAA,MAAuB,CAAA,SAAU,QAAA,CAAS,OAAA,CAAQ,UAAA,GAC1D,QAAA,CAAS,OAAA,CAAQ,UAAA,CAAW,OAAA,CAAQ,CAAA,IACpC,CAAA;IAAA,KAEQ,kBAAA,yBACc,KAAA,GAAQ,KAAA,gBAClB,QAAA,CAAS,KAAA,CAAM,YAAA,CAAa,eAAA,KAAoB,QAAA,CAC5D,KAAA,CAAM,YAAA,CAAa,eAAA,MAEnB,cAAA,CAAe,KAAA;MA9BF;;;;;;MAqCf,aAAA,IAAiB,cAAA,CAAe,KAAA;MA7BH;;;;;;;;MAuC7B,gBAAA,IAAoB,cAAA,CAAe,UAAA;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M,UAAA,CAAW,QAAA;EAAA;AAAA;;;;KAOd,MAAA,mBAAyB,CAAA;;;;;;;;;;;;;;;;;;;;;;;;KAyBzB,OAAA,mBACA,CAAA,SAAU,CAAA,4BAA4B,CAAA,SAAU,CAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;KA8BhD,SAAA,gDAAyD,CAAA,GACjE,IAAA,kBACe,CAAA,GACb,IAAA,kBACe,CAAA,GACb,IAAA,GACA,IAAA;;;;;;;;;;;;;;;;;;KAmBI,eAAA,oCAAmD,CAAA;;;;KAKnD,YAAA,MAAkB,CAAA,GAAI,OAAA,CAAQ,CAAA;AAAA,KAE9B,QAAA,0BAAkC,CAAA,GAAI,CAAA,CAAE,OAAA"}
|
package/types.mjs
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
|
|
3
|
+
//#region src/types.ts
|
|
4
|
+
let Realtime;
|
|
5
|
+
(function(_Realtime) {
|
|
6
|
+
_Realtime.messageSchema = z.object({
|
|
7
|
+
channel: z.string().optional(),
|
|
8
|
+
topic: z.string().optional(),
|
|
9
|
+
data: z.any().optional(),
|
|
10
|
+
run_id: z.string().optional(),
|
|
11
|
+
fn_id: z.string().optional(),
|
|
12
|
+
created_at: z.string().optional().transform((v) => v ? new Date(v) : void 0),
|
|
13
|
+
env_id: z.string().optional(),
|
|
14
|
+
stream_id: z.string().optional(),
|
|
15
|
+
kind: z.enum([
|
|
16
|
+
"step",
|
|
17
|
+
"run",
|
|
18
|
+
"data",
|
|
19
|
+
"ping",
|
|
20
|
+
"pong",
|
|
21
|
+
"closing",
|
|
22
|
+
"event",
|
|
23
|
+
"sub",
|
|
24
|
+
"unsub",
|
|
25
|
+
"datastream-start",
|
|
26
|
+
"datastream-end",
|
|
27
|
+
"chunk"
|
|
28
|
+
])
|
|
29
|
+
}).transform(({ data, ...rest }) => {
|
|
30
|
+
return {
|
|
31
|
+
...rest,
|
|
32
|
+
data: data ?? void 0
|
|
33
|
+
};
|
|
34
|
+
});
|
|
35
|
+
})(Realtime || (Realtime = {}));
|
|
36
|
+
|
|
37
|
+
//#endregion
|
|
38
|
+
export { Realtime };
|
|
39
|
+
//# sourceMappingURL=types.mjs.map
|
package/types.mjs.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.mjs","names":[],"sources":["../src/types.ts"],"sourcesContent":["import { type StandardSchemaV1 } from \"@standard-schema/spec\";\r\nimport { z } from \"zod\";\r\n\r\nexport namespace Realtime {\r\n export type PublishFn = <\r\n TMessage extends MaybePromise<Realtime.Message.Input>,\r\n >(\r\n message: TMessage,\r\n ) => Promise<Awaited<TMessage>[\"data\"]>;\r\n\r\n export type Token<\r\n TChannel extends Channel | Channel.Definition,\r\n TTopics extends (keyof Channel.InferTopics<\r\n Channel.Definition.AsChannel<TChannel>\r\n > &\r\n string)[] = (keyof Channel.InferTopics<\r\n Channel.Definition.AsChannel<TChannel>\r\n > &\r\n string)[],\r\n > = TChannel extends Channel.Definition\r\n ? Subscribe.Token<Channel.Definition.AsChannel<TChannel>, TTopics>\r\n : TChannel extends Channel\r\n ? Subscribe.Token<TChannel, TTopics>\r\n : never;\r\n\r\n export namespace Subscribe {\r\n export type InferChannelInput<T> = T extends Realtime.Channel.Definition\r\n ? Realtime.Channel.Definition.InferId<T>\r\n : T;\r\n\r\n export type StreamSubscription<\r\n TSubscribeToken extends Token = Token,\r\n TData extends Simplify<Token.InferMessage<TSubscribeToken>> = Simplify<\r\n Token.InferMessage<TSubscribeToken>\r\n >,\r\n > = ReadableStream<TData> & {\r\n /**\r\n * Get a new readable stream from the subscription that delivers JSON chunks.\r\n *\r\n * The stream starts when this function is called and will not contain any\r\n * messages that were sent before this function was called.\r\n */\r\n getJsonStream(): ReadableStream<TData>;\r\n\r\n /**\r\n * Get a new readable stream from the subscription that delivers\r\n * SSE-compatible chunks, which are compatible with the `EventSource` API\r\n * and generally used for streaming data from a server to the browser.\r\n *\r\n * The stream starts when this function is called and will not contain any\r\n * messages that were sent before this function was called.\r\n */\r\n getEncodedStream(): ReadableStream<Uint8Array>;\r\n };\r\n\r\n export type Callback<\r\n TSubscribeToken extends Subscribe.Token = Subscribe.Token,\r\n > = (message: Token.InferMessage<TSubscribeToken>) => void;\r\n\r\n export interface Token<\r\n TChannel extends Channel | Channel.Definition = Channel,\r\n TTopics extends (keyof Channel.InferTopics<TChannel>)[] =\r\n (keyof Channel.InferTopics<TChannel>)[],\r\n > {\r\n // key used to auth - could be undefined as then we can do a cold subscribe\r\n key?: string | undefined;\r\n channel: Realtime.Channel.Definition.AsChannel<TChannel>;\r\n topics: TTopics;\r\n }\r\n\r\n export namespace Token {\r\n export type InferChannel<TToken extends Token> =\r\n TToken extends Token<\r\n infer IChannel,\r\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\r\n any\r\n >\r\n ? IChannel\r\n : Channel;\r\n\r\n export type InferTopicData<\r\n TToken extends Token,\r\n TChannelTopics extends Record<string, Topic.Definition> =\r\n Channel.InferTopics<Token.InferChannel<TToken>>,\r\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\r\n > =\r\n TToken extends Token<any, infer ITopics>\r\n ? { [K in ITopics[number]]: TChannelTopics[K] }\r\n : never;\r\n\r\n export type InferMessage<TToken extends Token> = Simplify<\r\n Realtime.Message<\r\n Channel.InferId<Token.InferChannel<TToken>>,\r\n Token.InferTopicData<TToken>\r\n >\r\n >;\r\n }\r\n }\r\n\r\n // We need to use a `Message` type so that we can appropriately type incoming\r\n // and outgoing messages with generics, but we also need to validate these at\r\n // runtime.\r\n //\r\n // Ideally in the future we use protobuf for this, but for now we use Zod.\r\n // This type is used to assert that the Zod schema matches the generic type.\r\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\r\n // type _AssertMessageSchemaMatchesGeneric = Expect<\r\n // IsEqual<z.output<typeof messageSchema>, Message.Raw>\r\n // >;\r\n\r\n export const messageSchema = z\r\n .object({\r\n channel: z.string().optional(),\r\n topic: z.string().optional(),\r\n data: z.any().optional(),\r\n run_id: z.string().optional(),\r\n fn_id: z.string().optional(),\r\n created_at: z\r\n .string()\r\n .optional()\r\n .transform((v) => (v ? new Date(v) : undefined)),\r\n env_id: z.string().optional(),\r\n stream_id: z.string().optional(),\r\n kind: z.enum([\r\n \"step\",\r\n \"run\",\r\n \"data\",\r\n \"ping\",\r\n \"pong\",\r\n \"closing\",\r\n \"event\",\r\n \"sub\",\r\n \"unsub\",\r\n \"datastream-start\",\r\n \"datastream-end\",\r\n \"chunk\",\r\n ]),\r\n })\r\n .transform(({ data, ...rest }) => {\r\n return {\r\n ...rest,\r\n data: data ?? undefined,\r\n };\r\n });\r\n\r\n // Subscribe (output) msg\r\n export type Message<\r\n TChannelId extends string = string,\r\n TTopics extends Record<string, Realtime.Topic.Definition> = Record<\r\n string,\r\n Realtime.Topic.Definition\r\n >,\r\n > = {\r\n [K in keyof TTopics]:\r\n | {\r\n topic: K;\r\n channel: TChannelId;\r\n data: Realtime.Topic.InferSubscribe<TTopics[K]>;\r\n runId?: string;\r\n fnId?: string;\r\n createdAt: Date;\r\n envId?: string;\r\n kind: \"data\";\r\n }\r\n | {\r\n topic: K;\r\n channel: TChannelId;\r\n data: Realtime.Topic.InferSubscribe<TTopics[K]>;\r\n runId?: string;\r\n fnId?: string;\r\n kind: \"datastream-start\" | \"datastream-end\" | \"chunk\";\r\n streamId: string;\r\n stream: ReadableStream<Realtime.Topic.InferSubscribe<TTopics[K]>>;\r\n };\r\n }[keyof TTopics];\r\n\r\n export namespace Message {\r\n // Publish (input) msg\r\n export type Input<\r\n TChannelId extends string = string,\r\n TTopicId extends string = string,\r\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\r\n TData = any,\r\n > = {\r\n channel: TChannelId;\r\n topic: TTopicId;\r\n data: TData;\r\n };\r\n\r\n export type Raw<\r\n TChannelId extends string = string,\r\n TTopics extends Record<string, Realtime.Topic.Definition> = Record<\r\n string,\r\n Realtime.Topic.Definition\r\n >,\r\n > = {\r\n [K in keyof TTopics]: {\r\n topic?: K;\r\n stream_id?: string;\r\n data: Realtime.Topic.InferSubscribe<TTopics[K]>;\r\n channel?: TChannelId;\r\n run_id?: string;\r\n fn_id?: string;\r\n created_at?: Date;\r\n env_id?: string;\r\n kind:\r\n | \"step\" // step data\r\n | \"run\" // run results\r\n | \"data\" // misc stream data from `ctx.publish()`\r\n | \"datastream-start\"\r\n | \"datastream-end\"\r\n | \"ping\" // keepalive server -> client\r\n | \"pong\" // keepalive client -> server\r\n | \"closing\" // server is closing connection, client should reconnect\r\n | \"event\" // event sent to inngest\r\n | \"sub\"\r\n | \"unsub\"\r\n | \"chunk\";\r\n };\r\n }[keyof TTopics];\r\n }\r\n\r\n export type Channel<\r\n TChannelId extends string = string,\r\n TTopics extends Record<string, Realtime.Topic.Definition> = Record<\r\n string,\r\n Realtime.Topic.Definition\r\n >,\r\n > = {\r\n [K in\r\n | IsLiteral<keyof TTopics, keyof TTopics, never>\r\n | \"name\"\r\n | \"topics\"]: K extends \"name\"\r\n ? string\r\n : K extends \"topics\"\r\n ? TTopics\r\n : Realtime.Topic<TChannelId, TTopics[K]>;\r\n };\r\n\r\n export namespace Channel {\r\n export type Like = {\r\n channel: string;\r\n topics: string[];\r\n };\r\n\r\n export type InferId<TChannel extends Channel> =\r\n TChannel extends Channel<\r\n infer IId,\r\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\r\n any\r\n >\r\n ? IId\r\n : string;\r\n\r\n export type AsChannel<T extends Channel | string> = T extends Channel\r\n ? T\r\n : T extends string\r\n ? Realtime.Channel<T>\r\n : never;\r\n\r\n export type InferTopics<\r\n TChannel extends Channel | Channel.Definition,\r\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\r\n > =\r\n TChannel extends Channel.Definition<any, infer ITopics>\r\n ? ITopics\r\n : // eslint-disable-next-line @typescript-eslint/no-explicit-any\r\n TChannel extends Channel<any, infer ITopics>\r\n ? ITopics\r\n : Record<string, Realtime.Topic.Definition>;\r\n\r\n export interface Definition<\r\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\r\n TChannelBuilderFn extends BuilderFn = (...args: any[]) => string,\r\n TTopics extends Record<string, Topic.Definition> = Record<\r\n string,\r\n Topic.Definition\r\n >,\r\n > {\r\n (\r\n ...args: Parameters<TChannelBuilderFn>\r\n ): Channel<ReturnType<TChannelBuilderFn>, TTopics>;\r\n\r\n addTopic<UTopic extends Topic.Definition>(\r\n topic: UTopic,\r\n ): Definition<TChannelBuilderFn, AddTopic<TTopics, UTopic>>;\r\n\r\n topics: TTopics;\r\n }\r\n\r\n export namespace Definition {\r\n export type InferId<TChannel extends Definition> =\r\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\r\n TChannel extends Definition<infer IBuilder, any>\r\n ? ReturnType<IBuilder>\r\n : string;\r\n\r\n export type InferTopics<TChannel extends Definition> =\r\n TChannel extends Definition<\r\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\r\n any,\r\n infer ITopics\r\n >\r\n ? ITopics\r\n : Record<string, Topic.Definition>;\r\n\r\n export type AsChannel<T extends Definition | Channel> =\r\n T extends Definition\r\n ? Channel<InferId<T>, InferTopics<T>>\r\n : T extends Channel\r\n ? T\r\n : never;\r\n }\r\n\r\n export type AddTopic<\r\n TCurr extends Record<string, Topic.Definition>,\r\n TInc extends Topic.Definition,\r\n TIncWrapped extends Record<TInc[\"name\"], TInc> = Record<\r\n TInc[\"name\"],\r\n TInc\r\n >,\r\n > =\r\n IsStringLiteral<keyof TCurr & string> extends true\r\n ? Simplify<Omit<TCurr, TInc[\"name\"]> & TIncWrapped>\r\n : TIncWrapped;\r\n\r\n export type BuilderFn<TChannelId extends string = string> = (\r\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\r\n ...args: any[]\r\n ) => TChannelId;\r\n\r\n export type Builder = <\r\n const TChannelId extends string,\r\n const TIdInput extends TChannelId | BuilderFn<TChannelId>,\r\n >(\r\n id: TIdInput,\r\n ) => TIdInput extends TChannelId\r\n ? Channel.Definition<() => TIdInput>\r\n : TIdInput extends BuilderFn<TChannelId>\r\n ? Channel.Definition<TIdInput>\r\n : never;\r\n }\r\n\r\n export type Topic<\r\n TChannelId extends string = string,\r\n TTopic extends Topic.Definition = Topic.Definition,\r\n > = (\r\n data: Topic.InferPublish<TTopic>,\r\n ) => Promise<\r\n Realtime.Message.Input<\r\n TChannelId,\r\n Topic.InferId<TTopic>,\r\n Topic.InferPublish<TTopic>\r\n >\r\n >;\r\n\r\n export namespace Topic {\r\n export type Like = {\r\n name: string;\r\n };\r\n\r\n export interface Definition<\r\n TTopicId extends string = string,\r\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\r\n TPublish = any,\r\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\r\n _TSubscribe = TPublish,\r\n > {\r\n name: TTopicId;\r\n\r\n // Deliberately doesn't include `USubscribe` typing, as there's no schema\r\n // to perform transformations.\r\n type<const UPublish>(): Definition<TTopicId, UPublish>;\r\n\r\n schema<const TSchema extends StandardSchemaV1>(\r\n schema: TSchema,\r\n ): Definition<\r\n TTopicId,\r\n StandardSchemaV1.InferInput<TSchema>,\r\n StandardSchemaV1.InferOutput<TSchema>\r\n >;\r\n\r\n getSchema(): StandardSchemaV1 | undefined;\r\n }\r\n\r\n export type InferId<TTopic extends Topic.Definition> =\r\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\r\n TTopic extends Topic.Definition<infer IId, any, any> ? IId : string;\r\n\r\n export type InferPublish<TTopic extends Topic.Definition> =\r\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\r\n TTopic extends Topic.Definition<any, infer IPublish, any>\r\n ? IPublish\r\n : // eslint-disable-next-line @typescript-eslint/no-explicit-any\r\n any;\r\n\r\n export type InferSubscribe<TTopic extends Topic.Definition> =\r\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\r\n TTopic extends Topic.Definition<any, any, infer ISubscribe>\r\n ? ISubscribe\r\n : // eslint-disable-next-line @typescript-eslint/no-explicit-any\r\n any;\r\n\r\n export type Builder = <const TTopicId extends string>(\r\n id: TTopicId,\r\n ) => Topic.Definition<TTopicId>;\r\n }\r\n}\r\n\r\n/**\r\n * Expects that a value resolves to `true`, useful for asserting type checks.\r\n */\r\nexport type Expect<T extends true> = T;\r\n\r\n/**\r\nReturns a boolean for whether the two given types are equal.\r\n\r\n{@link https://github.com/microsoft/TypeScript/issues/27024#issuecomment-421529650}\r\n{@link https://stackoverflow.com/questions/68961864/how-does-the-equals-work-in-typescript/68963796#68963796}\r\n\r\nUse-cases:\r\n- If you want to make a conditional branch based on the result of a comparison of two types.\r\n\r\n@example\r\n```\r\nimport type {IsEqual} from 'type-fest';\r\n\r\n// This type returns a boolean for whether the given array includes the given item.\r\n// `IsEqual` is used to compare the given array at position 0 and the given item and then return true if they are equal.\r\ntype Includes<Value extends readonly any[], Item> =\r\n\tValue extends readonly [Value[0], ...infer rest]\r\n\t\t? IsEqual<Value[0], Item> extends true\r\n\t\t\t? true\r\n\t\t\t: Includes<rest, Item>\r\n\t\t: false;\r\n```\r\n*/\r\nexport type IsEqual<A, B> =\r\n (<G>() => G extends A ? 1 : 2) extends <G>() => G extends B ? 1 : 2\r\n ? true\r\n : false;\r\n\r\n/**\r\n * Given a type `T`, return `Then` if `T` is a string, number, or symbol\r\n * literal, else `Else`.\r\n *\r\n * `Then` defaults to `true` and `Else` defaults to `false`.\r\n *\r\n * Useful for determining if an object is a generic type or has known keys.\r\n *\r\n * @example\r\n * ```ts\r\n * type IsLiteralType = IsLiteral<\"foo\">; // true\r\n * type IsLiteralType = IsLiteral<string>; // false\r\n *\r\n * type IsLiteralType = IsLiteral<1>; // true\r\n * type IsLiteralType = IsLiteral<number>; // false\r\n *\r\n * type IsLiteralType = IsLiteral<symbol>; // true\r\n * type IsLiteralType = IsLiteral<typeof Symbol.iterator>; // false\r\n *\r\n * type T0 = { foo: string };\r\n * type HasAllKnownKeys = IsLiteral<keyof T0>; // true\r\n *\r\n * type T1 = { [x: string]: any; foo: boolean };\r\n * type HasAllKnownKeys = IsLiteral<keyof T1>; // false\r\n * ```\r\n */\r\nexport type IsLiteral<T, Then = true, Else = false> = string extends T\r\n ? Else\r\n : number extends T\r\n ? Else\r\n : symbol extends T\r\n ? Else\r\n : Then;\r\n\r\n/**\r\n * Returns `true` if the given generic `T` is a string literal, e.g. `\"foo\"`, or\r\n * `false` if it is a string type, e.g. `string`.\r\n *\r\n * Useful for checking whether the keys of an object are known or not.\r\n *\r\n * @example\r\n * ```ts\r\n * // false\r\n * type ObjIsGeneric = IsStringLiteral<keyof Record<string, boolean>>;\r\n *\r\n * // true\r\n * type ObjIsKnown = IsStringLiteral<keyof { foo: boolean; }>; // true\r\n * ```\r\n *\r\n * @internal\r\n */\r\nexport type IsStringLiteral<T extends string> = string extends T ? false : true;\r\n\r\n/**\r\n * Returns the given generic as either itself or a promise of itself.\r\n */\r\nexport type MaybePromise<T> = T | Promise<T>;\r\n\r\nexport type Simplify<T> = { [KeyType in keyof T]: T[KeyType] } & {};\r\n"],"mappings":";;;;;2BA8G+B,EAC1B,OAAO;EACN,SAAS,EAAE,QAAQ,CAAC,UAAU;EAC9B,OAAO,EAAE,QAAQ,CAAC,UAAU;EAC5B,MAAM,EAAE,KAAK,CAAC,UAAU;EACxB,QAAQ,EAAE,QAAQ,CAAC,UAAU;EAC7B,OAAO,EAAE,QAAQ,CAAC,UAAU;EAC5B,YAAY,EACT,QAAQ,CACR,UAAU,CACV,WAAW,MAAO,IAAI,IAAI,KAAK,EAAE,GAAG,OAAW;EAClD,QAAQ,EAAE,QAAQ,CAAC,UAAU;EAC7B,WAAW,EAAE,QAAQ,CAAC,UAAU;EAChC,MAAM,EAAE,KAAK;GACX;GACA;GACA;GACA;GACA;GACA;GACA;GACA;GACA;GACA;GACA;GACA;GACD,CAAC;EACH,CAAC,CACD,WAAW,EAAE,MAAM,GAAG,WAAW;AAChC,SAAO;GACL,GAAG;GACH,MAAM,QAAQ;GACf;GACD"}
|