@replit/river 0.23.3 → 0.23.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/README.md +48 -0
- package/dist/{chunk-3U43VMW2.js → chunk-42ON4GTR.js} +2 -2
- package/dist/{chunk-XZV3HOQW.js → chunk-6GPJFN7T.js} +2 -2
- package/dist/chunk-6GPJFN7T.js.map +1 -0
- package/dist/{chunk-R3EG3FOP.js → chunk-DZLXTSL5.js} +2 -2
- package/dist/{chunk-JBRQNEBV.js → chunk-JJWZ5EHY.js} +13 -3
- package/dist/chunk-JJWZ5EHY.js.map +1 -0
- package/dist/{chunk-IKT6A32W.js → chunk-N7OICPYR.js} +2 -2
- package/dist/chunk-N7OICPYR.js.map +1 -0
- package/dist/{connection-89a66019.d.ts → connection-5331d77f.d.ts} +1 -1
- package/dist/{connection-4abad643.d.ts → connection-e257f6b7.d.ts} +1 -1
- package/dist/index-926aea33.d.ts +133 -0
- package/dist/logging/index.d.cts +1 -2
- package/dist/logging/index.d.ts +1 -2
- package/dist/router/index.cjs +13 -1
- package/dist/router/index.cjs.map +1 -1
- package/dist/router/index.d.cts +7 -7
- package/dist/router/index.d.ts +7 -7
- package/dist/router/index.js +6 -2
- package/dist/{services-ef89f5b0.d.ts → services-3f99f5d2.d.ts} +2 -2
- package/dist/transport/impls/uds/client.cjs +1 -1
- package/dist/transport/impls/uds/client.cjs.map +1 -1
- package/dist/transport/impls/uds/client.d.cts +4 -4
- package/dist/transport/impls/uds/client.d.ts +4 -4
- package/dist/transport/impls/uds/client.js +3 -3
- package/dist/transport/impls/uds/server.cjs +1 -1
- package/dist/transport/impls/uds/server.cjs.map +1 -1
- package/dist/transport/impls/uds/server.d.cts +4 -4
- package/dist/transport/impls/uds/server.d.ts +4 -4
- package/dist/transport/impls/uds/server.js +3 -3
- package/dist/transport/impls/ws/client.cjs +1 -1
- package/dist/transport/impls/ws/client.cjs.map +1 -1
- package/dist/transport/impls/ws/client.d.cts +4 -4
- package/dist/transport/impls/ws/client.d.ts +4 -4
- package/dist/transport/impls/ws/client.js +3 -3
- package/dist/transport/impls/ws/server.cjs +1 -1
- package/dist/transport/impls/ws/server.cjs.map +1 -1
- package/dist/transport/impls/ws/server.d.cts +3 -3
- package/dist/transport/impls/ws/server.d.ts +3 -3
- package/dist/transport/impls/ws/server.js +3 -3
- package/dist/transport/index.cjs +1 -1
- package/dist/transport/index.cjs.map +1 -1
- package/dist/transport/index.d.cts +4 -307
- package/dist/transport/index.d.ts +4 -307
- package/dist/transport/index.js +2 -2
- package/dist/transport-cbe70659.d.ts +537 -0
- package/dist/util/testHelpers.cjs +1 -1
- package/dist/util/testHelpers.cjs.map +1 -1
- package/dist/util/testHelpers.d.cts +3 -3
- package/dist/util/testHelpers.d.ts +3 -3
- package/dist/util/testHelpers.js +3 -3
- package/package.json +1 -1
- package/dist/chunk-IKT6A32W.js.map +0 -1
- package/dist/chunk-JBRQNEBV.js.map +0 -1
- package/dist/chunk-XZV3HOQW.js.map +0 -1
- package/dist/index-a009d7f6.d.ts +0 -360
- package/dist/services-edc3b16d.d.ts +0 -707
- /package/dist/{chunk-3U43VMW2.js.map → chunk-42ON4GTR.js.map} +0 -0
- /package/dist/{chunk-R3EG3FOP.js.map → chunk-DZLXTSL5.js.map} +0 -0
|
@@ -1,707 +0,0 @@
|
|
|
1
|
-
import { Static, TObject, TUnion, TString, TSchema, TNever, TLiteral } from '@sinclair/typebox';
|
|
2
|
-
import { ClientTransport } from './transport/index.cjs';
|
|
3
|
-
import { Pushable } from 'it-pushable';
|
|
4
|
-
import { C as Connection, T as TransportClientId, g as ClientHandshakeOptions, f as ServiceContextWithTransportInfo } from './index-a009d7f6.js';
|
|
5
|
-
|
|
6
|
-
type AsyncIter<T> = AsyncGenerator<T, T>;
|
|
7
|
-
/**
|
|
8
|
-
* A helper type to transform an actual service type into a type
|
|
9
|
-
* we can case to in the proxy.
|
|
10
|
-
* @template Router - The type of the Router.
|
|
11
|
-
*/
|
|
12
|
-
type ServiceClient<Router extends AnyService> = {
|
|
13
|
-
[ProcName in keyof Router['procedures']]: ProcType<Router, ProcName> extends 'rpc' ? {
|
|
14
|
-
rpc: (input: Static<ProcInput<Router, ProcName>>) => Promise<Result<Static<ProcOutput<Router, ProcName>>, Static<ProcErrors<Router, ProcName>>>>;
|
|
15
|
-
} : ProcType<Router, ProcName> extends 'upload' ? ProcHasInit<Router, ProcName> extends true ? {
|
|
16
|
-
upload: (init: Static<ProcInit<Router, ProcName>>) => Promise<[
|
|
17
|
-
Pushable<Static<ProcInput<Router, ProcName>>>,
|
|
18
|
-
Promise<Result<Static<ProcOutput<Router, ProcName>>, Static<ProcErrors<Router, ProcName>>>>
|
|
19
|
-
]>;
|
|
20
|
-
} : {
|
|
21
|
-
upload: () => Promise<[
|
|
22
|
-
Pushable<Static<ProcInput<Router, ProcName>>>,
|
|
23
|
-
Promise<Result<Static<ProcOutput<Router, ProcName>>, Static<ProcErrors<Router, ProcName>>>>
|
|
24
|
-
]>;
|
|
25
|
-
} : ProcType<Router, ProcName> extends 'stream' ? ProcHasInit<Router, ProcName> extends true ? {
|
|
26
|
-
stream: (init: Static<ProcInit<Router, ProcName>>) => Promise<[
|
|
27
|
-
Pushable<Static<ProcInput<Router, ProcName>>>,
|
|
28
|
-
AsyncIter<Result<Static<ProcOutput<Router, ProcName>>, Static<ProcErrors<Router, ProcName>>>>,
|
|
29
|
-
() => void
|
|
30
|
-
]>;
|
|
31
|
-
} : {
|
|
32
|
-
stream: () => Promise<[
|
|
33
|
-
Pushable<Static<ProcInput<Router, ProcName>>>,
|
|
34
|
-
AsyncIter<Result<Static<ProcOutput<Router, ProcName>>, Static<ProcErrors<Router, ProcName>>>>,
|
|
35
|
-
() => void
|
|
36
|
-
]>;
|
|
37
|
-
} : ProcType<Router, ProcName> extends 'subscription' ? {
|
|
38
|
-
subscribe: (input: Static<ProcInput<Router, ProcName>>) => Promise<[
|
|
39
|
-
AsyncIter<Result<Static<ProcOutput<Router, ProcName>>, Static<ProcErrors<Router, ProcName>>>>,
|
|
40
|
-
() => void
|
|
41
|
-
]>;
|
|
42
|
-
} : never;
|
|
43
|
-
};
|
|
44
|
-
/**
|
|
45
|
-
* Defines a type that represents a client for a server with a set of services.
|
|
46
|
-
* @template Srv - The type of the server.
|
|
47
|
-
*/
|
|
48
|
-
type Client<Services extends AnyServiceSchemaMap, IS extends InstantiatedServiceSchemaMap<Services> = InstantiatedServiceSchemaMap<Services>> = {
|
|
49
|
-
[SvcName in keyof IS]: ServiceClient<IS[SvcName]>;
|
|
50
|
-
};
|
|
51
|
-
interface ClientOptions {
|
|
52
|
-
connectOnInvoke: boolean;
|
|
53
|
-
eagerlyConnect: boolean;
|
|
54
|
-
}
|
|
55
|
-
/**
|
|
56
|
-
* Creates a client for a given server using the provided transport.
|
|
57
|
-
* Note that the client only needs the type of the server, not the actual
|
|
58
|
-
* server definition itself.
|
|
59
|
-
*
|
|
60
|
-
* This relies on a proxy to dynamically create the client, so the client
|
|
61
|
-
* will be typed as if it were the actual server with the appropriate services
|
|
62
|
-
* and procedures.
|
|
63
|
-
*
|
|
64
|
-
* @template Srv - The type of the server.
|
|
65
|
-
* @param {Transport} transport - The transport to use for communication.
|
|
66
|
-
* @param {TransportClientId} serverId - The ID of the server to connect to.
|
|
67
|
-
* @param {Partial<ClientOptions>} providedClientOptions - The options for the client.
|
|
68
|
-
* @returns The client for the server.
|
|
69
|
-
*/
|
|
70
|
-
declare function createClient<ServiceSchemaMap extends AnyServiceSchemaMap>(transport: ClientTransport<Connection>, serverId: TransportClientId, providedClientOptions?: Partial<ClientOptions & {
|
|
71
|
-
handshakeOptions: ClientHandshakeOptions;
|
|
72
|
-
}>): Client<ServiceSchemaMap>;
|
|
73
|
-
|
|
74
|
-
type TLiteralString = TLiteral<string>;
|
|
75
|
-
type RiverErrorSchema = TObject<{
|
|
76
|
-
code: TLiteralString | TUnion<Array<TLiteralString>>;
|
|
77
|
-
message: TLiteralString | TString;
|
|
78
|
-
}> | TObject<{
|
|
79
|
-
code: TLiteralString | TUnion<Array<TLiteralString>>;
|
|
80
|
-
message: TLiteralString | TString;
|
|
81
|
-
extras: TSchema;
|
|
82
|
-
}>;
|
|
83
|
-
type RiverError = TUnion<Array<RiverErrorSchema>> | RiverErrorSchema | TNever;
|
|
84
|
-
declare const UNCAUGHT_ERROR = "UNCAUGHT_ERROR";
|
|
85
|
-
declare const RiverUncaughtSchema: TObject<{
|
|
86
|
-
code: TUnion<[TLiteral<"UNCAUGHT_ERROR">, TLiteral<"UNEXPECTED_DISCONNECT">]>;
|
|
87
|
-
message: TString;
|
|
88
|
-
}>;
|
|
89
|
-
type Result<T, E> = {
|
|
90
|
-
ok: true;
|
|
91
|
-
payload: T;
|
|
92
|
-
} | {
|
|
93
|
-
ok: false;
|
|
94
|
-
payload: E;
|
|
95
|
-
};
|
|
96
|
-
declare function Ok<const T extends Array<unknown>, const E>(p: T): Result<T, E>;
|
|
97
|
-
declare function Ok<const T extends ReadonlyArray<unknown>, const E>(p: T): Result<T, E>;
|
|
98
|
-
declare function Ok<const T, const E>(payload: T): Result<T, E>;
|
|
99
|
-
declare function Err<const T, const E>(error: E): Result<T, E>;
|
|
100
|
-
/**
|
|
101
|
-
* Refine a {@link Result} type to its returned payload.
|
|
102
|
-
*/
|
|
103
|
-
type ResultUnwrapOk<R> = R extends Result<infer T, infer __E> ? T : never;
|
|
104
|
-
/**
|
|
105
|
-
* Refine a {@link Result} type to its error payload.
|
|
106
|
-
*/
|
|
107
|
-
type ResultUnwrapErr<R> = R extends Result<infer __T, infer E> ? E : never;
|
|
108
|
-
/**
|
|
109
|
-
* Retrieve the output type for a procedure, represented as a {@link Result}
|
|
110
|
-
* type.
|
|
111
|
-
* Example:
|
|
112
|
-
* ```
|
|
113
|
-
* type Message = Output<typeof client, 'serviceName', 'procedureName'>
|
|
114
|
-
* ```
|
|
115
|
-
*/
|
|
116
|
-
type Output<RiverClient, ServiceName extends keyof RiverClient, ProcedureName extends keyof RiverClient[ServiceName], Procedure = RiverClient[ServiceName][ProcedureName], Fn extends (...args: never) => unknown = (...args: never) => unknown> = RiverClient extends Client<infer __ServiceSchemaMap> ? Procedure extends object ? Procedure extends object & {
|
|
117
|
-
rpc: infer RpcHandler extends Fn;
|
|
118
|
-
} ? Awaited<ReturnType<RpcHandler>> : Procedure extends object & {
|
|
119
|
-
upload: infer UploadHandler extends Fn;
|
|
120
|
-
} ? Awaited<ReturnType<UploadHandler>> extends [
|
|
121
|
-
infer __UploadInputMessage,
|
|
122
|
-
infer UploadOutputMessage
|
|
123
|
-
] ? Awaited<UploadOutputMessage> : never : Procedure extends object & {
|
|
124
|
-
stream: infer StreamHandler extends Fn;
|
|
125
|
-
} ? Awaited<ReturnType<StreamHandler>> extends [
|
|
126
|
-
infer __StreamInputMessage,
|
|
127
|
-
AsyncGenerator<infer StreamOutputMessage>,
|
|
128
|
-
infer __StreamCloseHandle
|
|
129
|
-
] ? StreamOutputMessage : never : Procedure extends object & {
|
|
130
|
-
subscribe: infer SubscriptionHandler extends Fn;
|
|
131
|
-
} ? Awaited<ReturnType<SubscriptionHandler>> extends [
|
|
132
|
-
AsyncGenerator<infer SubscriptionOutputMessage>,
|
|
133
|
-
infer __SubscriptionCloseHandle
|
|
134
|
-
] ? SubscriptionOutputMessage : never : never : never : never;
|
|
135
|
-
|
|
136
|
-
/**
|
|
137
|
-
* Brands a type to prevent it from being directly constructed.
|
|
138
|
-
*/
|
|
139
|
-
type Branded<T> = T & {
|
|
140
|
-
readonly __BRAND_DO_NOT_USE: unique symbol;
|
|
141
|
-
};
|
|
142
|
-
/**
|
|
143
|
-
* Unbrands a {@link Branded} type.
|
|
144
|
-
*/
|
|
145
|
-
type Unbranded<T> = T extends Branded<infer U> ? U : never;
|
|
146
|
-
/**
|
|
147
|
-
* The valid {@link Procedure} types. The `stream` and `upload` types can optionally have a
|
|
148
|
-
* different type for the very first initialization message. The suffixless types correspond to
|
|
149
|
-
* gRPC's four combinations of stream / non-stream in each direction.
|
|
150
|
-
*/
|
|
151
|
-
type ValidProcType = 'rpc' | 'upload' | 'subscription' | 'stream';
|
|
152
|
-
/**
|
|
153
|
-
* Represents the payload type for {@link Procedure}s.
|
|
154
|
-
*/
|
|
155
|
-
type PayloadType = TSchema;
|
|
156
|
-
/**
|
|
157
|
-
* Represents results from a {@link Procedure}. Might come from inside a stream or
|
|
158
|
-
* from a single message.
|
|
159
|
-
*/
|
|
160
|
-
type ProcedureResult<O extends PayloadType, E extends RiverError> = Result<Static<O>, Static<E> | Static<typeof RiverUncaughtSchema>>;
|
|
161
|
-
/**
|
|
162
|
-
* Procedure for a single message in both directions (1:1).
|
|
163
|
-
*
|
|
164
|
-
* @template State - The context state object.
|
|
165
|
-
* @template I - The TypeBox schema of the input object.
|
|
166
|
-
* @template O - The TypeBox schema of the output object.
|
|
167
|
-
* @template E - The TypeBox schema of the error object.
|
|
168
|
-
*/
|
|
169
|
-
interface RpcProcedure<State, I extends PayloadType, O extends PayloadType, E extends RiverError> {
|
|
170
|
-
type: 'rpc';
|
|
171
|
-
input: I;
|
|
172
|
-
output: O;
|
|
173
|
-
errors: E;
|
|
174
|
-
description?: string;
|
|
175
|
-
handler(context: ServiceContextWithTransportInfo<State>, input: Static<I>): Promise<ProcedureResult<O, E>>;
|
|
176
|
-
}
|
|
177
|
-
/**
|
|
178
|
-
* Procedure for a client-stream (potentially preceded by an initialization message),
|
|
179
|
-
* single message from server (n:1).
|
|
180
|
-
*
|
|
181
|
-
* @template State - The context state object.
|
|
182
|
-
* @template I - The TypeBox schema of the input object.
|
|
183
|
-
* @template O - The TypeBox schema of the output object.
|
|
184
|
-
* @template E - The TypeBox schema of the error object.
|
|
185
|
-
* @template Init - The TypeBox schema of the input initialization object, if any.
|
|
186
|
-
*/
|
|
187
|
-
type UploadProcedure<State, I extends PayloadType, O extends PayloadType, E extends RiverError, Init extends PayloadType | null = null> = Init extends PayloadType ? {
|
|
188
|
-
type: 'upload';
|
|
189
|
-
init: Init;
|
|
190
|
-
input: I;
|
|
191
|
-
output: O;
|
|
192
|
-
errors: E;
|
|
193
|
-
description?: string;
|
|
194
|
-
handler(context: ServiceContextWithTransportInfo<State>, init: Static<Init>, input: AsyncIterableIterator<Static<I>>): Promise<ProcedureResult<O, E>>;
|
|
195
|
-
} : {
|
|
196
|
-
type: 'upload';
|
|
197
|
-
input: I;
|
|
198
|
-
output: O;
|
|
199
|
-
errors: E;
|
|
200
|
-
description?: string;
|
|
201
|
-
handler(context: ServiceContextWithTransportInfo<State>, input: AsyncIterableIterator<Static<I>>): Promise<ProcedureResult<O, E>>;
|
|
202
|
-
};
|
|
203
|
-
/**
|
|
204
|
-
* Procedure for a single message from client, stream from server (1:n).
|
|
205
|
-
*
|
|
206
|
-
* @template State - The context state object.
|
|
207
|
-
* @template I - The TypeBox schema of the input object.
|
|
208
|
-
* @template O - The TypeBox schema of the output object.
|
|
209
|
-
* @template E - The TypeBox schema of the error object.
|
|
210
|
-
*/
|
|
211
|
-
interface SubscriptionProcedure<State, I extends PayloadType, O extends PayloadType, E extends RiverError> {
|
|
212
|
-
type: 'subscription';
|
|
213
|
-
input: I;
|
|
214
|
-
output: O;
|
|
215
|
-
errors: E;
|
|
216
|
-
description?: string;
|
|
217
|
-
handler(context: ServiceContextWithTransportInfo<State>, input: Static<I>, output: Pushable<ProcedureResult<O, E>>): Promise<(() => void) | void>;
|
|
218
|
-
}
|
|
219
|
-
/**
|
|
220
|
-
* Procedure for a bidirectional stream (potentially preceded by an initialization message),
|
|
221
|
-
* (n:n).
|
|
222
|
-
*
|
|
223
|
-
* @template State - The context state object.
|
|
224
|
-
* @template I - The TypeBox schema of the input object.
|
|
225
|
-
* @template O - The TypeBox schema of the output object.
|
|
226
|
-
* @template E - The TypeBox schema of the error object.
|
|
227
|
-
* @template Init - The TypeBox schema of the input initialization object, if any.
|
|
228
|
-
*/
|
|
229
|
-
type StreamProcedure<State, I extends PayloadType, O extends PayloadType, E extends RiverError, Init extends PayloadType | null = null> = Init extends PayloadType ? {
|
|
230
|
-
type: 'stream';
|
|
231
|
-
init: Init;
|
|
232
|
-
input: I;
|
|
233
|
-
output: O;
|
|
234
|
-
errors: E;
|
|
235
|
-
description?: string;
|
|
236
|
-
handler(context: ServiceContextWithTransportInfo<State>, init: Static<Init>, input: AsyncIterableIterator<Static<I>>, output: Pushable<ProcedureResult<O, E>>): Promise<(() => void) | void>;
|
|
237
|
-
} : {
|
|
238
|
-
type: 'stream';
|
|
239
|
-
input: I;
|
|
240
|
-
output: O;
|
|
241
|
-
errors: E;
|
|
242
|
-
description?: string;
|
|
243
|
-
handler(context: ServiceContextWithTransportInfo<State>, input: AsyncIterableIterator<Static<I>>, output: Pushable<ProcedureResult<O, E>>): Promise<(() => void) | void>;
|
|
244
|
-
};
|
|
245
|
-
/**
|
|
246
|
-
* Represents any {@link Procedure} type.
|
|
247
|
-
*
|
|
248
|
-
* @template State - The context state object. You can provide this to constrain
|
|
249
|
-
* the type of procedures.
|
|
250
|
-
*/
|
|
251
|
-
type AnyProcedure<State = object> = Procedure<State, ValidProcType, PayloadType, PayloadType, RiverError, PayloadType | null>;
|
|
252
|
-
/**
|
|
253
|
-
* Represents a map of {@link Procedure}s.
|
|
254
|
-
*
|
|
255
|
-
* @template State - The context state object. You can provide this to constrain
|
|
256
|
-
* the type of procedures.
|
|
257
|
-
*/
|
|
258
|
-
type ProcedureMap<State = object> = Record<string, AnyProcedure<State>>;
|
|
259
|
-
/**
|
|
260
|
-
* Creates an {@link RpcProcedure}.
|
|
261
|
-
*/
|
|
262
|
-
declare function rpc<State, I extends PayloadType, O extends PayloadType>(def: {
|
|
263
|
-
input: I;
|
|
264
|
-
output: O;
|
|
265
|
-
errors?: never;
|
|
266
|
-
description?: string;
|
|
267
|
-
handler: RpcProcedure<State, I, O, TNever>['handler'];
|
|
268
|
-
}): Branded<RpcProcedure<State, I, O, TNever>>;
|
|
269
|
-
declare function rpc<State, I extends PayloadType, O extends PayloadType, E extends RiverError>(def: {
|
|
270
|
-
input: I;
|
|
271
|
-
output: O;
|
|
272
|
-
errors: E;
|
|
273
|
-
description?: string;
|
|
274
|
-
handler: RpcProcedure<State, I, O, E>['handler'];
|
|
275
|
-
}): Branded<RpcProcedure<State, I, O, E>>;
|
|
276
|
-
/**
|
|
277
|
-
* Creates an {@link UploadProcedure}, optionally with an initialization message.
|
|
278
|
-
*/
|
|
279
|
-
declare function upload<State, I extends PayloadType, O extends PayloadType, Init extends PayloadType>(def: {
|
|
280
|
-
init: Init;
|
|
281
|
-
input: I;
|
|
282
|
-
output: O;
|
|
283
|
-
errors?: never;
|
|
284
|
-
description?: string;
|
|
285
|
-
handler: UploadProcedure<State, I, O, TNever, Init>['handler'];
|
|
286
|
-
}): Branded<UploadProcedure<State, I, O, TNever, Init>>;
|
|
287
|
-
declare function upload<State, I extends PayloadType, O extends PayloadType, E extends RiverError, Init extends PayloadType>(def: {
|
|
288
|
-
init: Init;
|
|
289
|
-
input: I;
|
|
290
|
-
output: O;
|
|
291
|
-
errors: E;
|
|
292
|
-
description?: string;
|
|
293
|
-
handler: UploadProcedure<State, I, O, E, Init>['handler'];
|
|
294
|
-
}): Branded<UploadProcedure<State, I, O, E, Init>>;
|
|
295
|
-
declare function upload<State, I extends PayloadType, O extends PayloadType>(def: {
|
|
296
|
-
init?: never;
|
|
297
|
-
input: I;
|
|
298
|
-
output: O;
|
|
299
|
-
errors?: never;
|
|
300
|
-
description?: string;
|
|
301
|
-
handler: UploadProcedure<State, I, O, TNever>['handler'];
|
|
302
|
-
}): Branded<UploadProcedure<State, I, O, TNever>>;
|
|
303
|
-
declare function upload<State, I extends PayloadType, O extends PayloadType, E extends RiverError>(def: {
|
|
304
|
-
init?: never;
|
|
305
|
-
input: I;
|
|
306
|
-
output: O;
|
|
307
|
-
errors: E;
|
|
308
|
-
description?: string;
|
|
309
|
-
handler: UploadProcedure<State, I, O, E>['handler'];
|
|
310
|
-
}): Branded<UploadProcedure<State, I, O, E>>;
|
|
311
|
-
/**
|
|
312
|
-
* Creates a {@link SubscriptionProcedure}.
|
|
313
|
-
*/
|
|
314
|
-
declare function subscription<State, I extends PayloadType, O extends PayloadType>(def: {
|
|
315
|
-
input: I;
|
|
316
|
-
output: O;
|
|
317
|
-
errors?: never;
|
|
318
|
-
description?: string;
|
|
319
|
-
handler: SubscriptionProcedure<State, I, O, TNever>['handler'];
|
|
320
|
-
}): Branded<SubscriptionProcedure<State, I, O, TNever>>;
|
|
321
|
-
declare function subscription<State, I extends PayloadType, O extends PayloadType, E extends RiverError>(def: {
|
|
322
|
-
input: I;
|
|
323
|
-
output: O;
|
|
324
|
-
errors: E;
|
|
325
|
-
description?: string;
|
|
326
|
-
handler: SubscriptionProcedure<State, I, O, E>['handler'];
|
|
327
|
-
}): Branded<SubscriptionProcedure<State, I, O, E>>;
|
|
328
|
-
/**
|
|
329
|
-
* Creates a {@link StreamProcedure}, optionally with an initialization message.
|
|
330
|
-
*/
|
|
331
|
-
declare function stream<State, I extends PayloadType, O extends PayloadType, Init extends PayloadType>(def: {
|
|
332
|
-
init: Init;
|
|
333
|
-
input: I;
|
|
334
|
-
output: O;
|
|
335
|
-
errors?: never;
|
|
336
|
-
description?: string;
|
|
337
|
-
handler: StreamProcedure<State, I, O, TNever, Init>['handler'];
|
|
338
|
-
}): Branded<StreamProcedure<State, I, O, TNever, Init>>;
|
|
339
|
-
declare function stream<State, I extends PayloadType, O extends PayloadType, E extends RiverError, Init extends PayloadType>(def: {
|
|
340
|
-
init: Init;
|
|
341
|
-
input: I;
|
|
342
|
-
output: O;
|
|
343
|
-
errors: E;
|
|
344
|
-
description?: string;
|
|
345
|
-
handler: StreamProcedure<State, I, O, E, Init>['handler'];
|
|
346
|
-
}): Branded<StreamProcedure<State, I, O, E, Init>>;
|
|
347
|
-
declare function stream<State, I extends PayloadType, O extends PayloadType>(def: {
|
|
348
|
-
init?: never;
|
|
349
|
-
input: I;
|
|
350
|
-
output: O;
|
|
351
|
-
errors?: never;
|
|
352
|
-
description?: string;
|
|
353
|
-
handler: StreamProcedure<State, I, O, TNever>['handler'];
|
|
354
|
-
}): Branded<StreamProcedure<State, I, O, TNever>>;
|
|
355
|
-
declare function stream<State, I extends PayloadType, O extends PayloadType, E extends RiverError>(def: {
|
|
356
|
-
init?: never;
|
|
357
|
-
input: I;
|
|
358
|
-
output: O;
|
|
359
|
-
errors: E;
|
|
360
|
-
description?: string;
|
|
361
|
-
handler: StreamProcedure<State, I, O, E>['handler'];
|
|
362
|
-
}): Branded<StreamProcedure<State, I, O, E>>;
|
|
363
|
-
/**
|
|
364
|
-
* Defines a Procedure type that can be a:
|
|
365
|
-
* - {@link RpcProcedure} for a single message in both directions (1:1)
|
|
366
|
-
* - {@link UploadProcedure} for a client-stream (potentially preceded by an
|
|
367
|
-
* initialization message)
|
|
368
|
-
* - {@link SubscriptionProcedure} for a single message from client, stream from server (1:n)
|
|
369
|
-
* - {@link StreamProcedure} for a bidirectional stream (potentially preceded by an
|
|
370
|
-
* initialization message)
|
|
371
|
-
*
|
|
372
|
-
* @template State - The TypeBox schema of the state object.
|
|
373
|
-
* @template Ty - The type of the procedure.
|
|
374
|
-
* @template I - The TypeBox schema of the input object.
|
|
375
|
-
* @template O - The TypeBox schema of the output object.
|
|
376
|
-
* @template Init - The TypeBox schema of the input initialization object, if any.
|
|
377
|
-
*/
|
|
378
|
-
type Procedure<State, Ty extends ValidProcType, I extends PayloadType, O extends PayloadType, E extends RiverError, Init extends PayloadType | null = null> = {
|
|
379
|
-
type: Ty;
|
|
380
|
-
} & (Init extends PayloadType ? Ty extends 'upload' ? UploadProcedure<State, I, O, E, Init> : Ty extends 'stream' ? StreamProcedure<State, I, O, E, Init> : never : Ty extends 'rpc' ? RpcProcedure<State, I, O, E> : Ty extends 'upload' ? UploadProcedure<State, I, O, E> : Ty extends 'subscription' ? SubscriptionProcedure<State, I, O, E> : Ty extends 'stream' ? StreamProcedure<State, I, O, E> : never);
|
|
381
|
-
/**
|
|
382
|
-
* Holds the {@link Procedure} creation functions. Use these to create
|
|
383
|
-
* procedures for services. You aren't allowed to create procedures directly.
|
|
384
|
-
*/
|
|
385
|
-
declare const Procedure: {
|
|
386
|
-
rpc: typeof rpc;
|
|
387
|
-
upload: typeof upload;
|
|
388
|
-
subscription: typeof subscription;
|
|
389
|
-
stream: typeof stream;
|
|
390
|
-
};
|
|
391
|
-
|
|
392
|
-
/**
|
|
393
|
-
* An instantiated service, probably from a {@link ServiceSchema}.
|
|
394
|
-
*
|
|
395
|
-
* You shouldn't construct these directly, use {@link ServiceSchema} instead.
|
|
396
|
-
*/
|
|
397
|
-
interface Service<State extends object, Procs extends ProcedureMap<State>> {
|
|
398
|
-
readonly state: State;
|
|
399
|
-
readonly procedures: Procs;
|
|
400
|
-
}
|
|
401
|
-
/**
|
|
402
|
-
* Represents any {@link Service} object.
|
|
403
|
-
*/
|
|
404
|
-
type AnyService = Service<object, ProcedureMap>;
|
|
405
|
-
/**
|
|
406
|
-
* Represents any {@link ServiceSchema} object.
|
|
407
|
-
*/
|
|
408
|
-
type AnyServiceSchema = ServiceSchema<object, ProcedureMap>;
|
|
409
|
-
/**
|
|
410
|
-
* A dictionary of {@link ServiceSchema}s, where the key is the service name.
|
|
411
|
-
*/
|
|
412
|
-
type AnyServiceSchemaMap = Record<string, AnyServiceSchema>;
|
|
413
|
-
/**
|
|
414
|
-
* Takes a {@link AnyServiceSchemaMap} and returns a dictionary of instantiated
|
|
415
|
-
* services.
|
|
416
|
-
*/
|
|
417
|
-
type InstantiatedServiceSchemaMap<T extends AnyServiceSchemaMap> = {
|
|
418
|
-
[K in keyof T]: T[K] extends ServiceSchema<infer S, infer P> ? Service<S, P> : never;
|
|
419
|
-
};
|
|
420
|
-
/**
|
|
421
|
-
* Helper to get the type definition for a specific handler of a procedure in a service.
|
|
422
|
-
* @template S - The service.
|
|
423
|
-
* @template ProcName - The name of the procedure.
|
|
424
|
-
*/
|
|
425
|
-
type ProcHandler<S extends AnyService, ProcName extends keyof S['procedures']> = S['procedures'][ProcName]['handler'];
|
|
426
|
-
/**
|
|
427
|
-
* Helper to get whether the type definition for the procedure contains an init type.
|
|
428
|
-
* @template S - The service.
|
|
429
|
-
* @template ProcName - The name of the procedure.
|
|
430
|
-
*/
|
|
431
|
-
type ProcHasInit<S extends AnyService, ProcName extends keyof S['procedures']> = S['procedures'][ProcName] extends {
|
|
432
|
-
init: PayloadType;
|
|
433
|
-
} ? true : false;
|
|
434
|
-
/**
|
|
435
|
-
* Helper to get the type definition for the procedure init type of a service.
|
|
436
|
-
* @template S - The service.
|
|
437
|
-
* @template ProcName - The name of the procedure.
|
|
438
|
-
*/
|
|
439
|
-
type ProcInit<S extends AnyService, ProcName extends keyof S['procedures']> = S['procedures'][ProcName] extends {
|
|
440
|
-
init: PayloadType;
|
|
441
|
-
} ? S['procedures'][ProcName]['init'] : never;
|
|
442
|
-
/**
|
|
443
|
-
* Helper to get the type definition for the procedure input of a service.
|
|
444
|
-
* @template S - The service.
|
|
445
|
-
* @template ProcName - The name of the procedure.
|
|
446
|
-
*/
|
|
447
|
-
type ProcInput<S extends AnyService, ProcName extends keyof S['procedures']> = S['procedures'][ProcName]['input'];
|
|
448
|
-
/**
|
|
449
|
-
* Helper to get the type definition for the procedure output of a service.
|
|
450
|
-
* @template S - The service.
|
|
451
|
-
* @template ProcName - The name of the procedure.
|
|
452
|
-
*/
|
|
453
|
-
type ProcOutput<S extends AnyService, ProcName extends keyof S['procedures']> = S['procedures'][ProcName]['output'];
|
|
454
|
-
/**
|
|
455
|
-
* Helper to get the type definition for the procedure errors of a service.
|
|
456
|
-
* @template S - The service.
|
|
457
|
-
* @template ProcName - The name of the procedure.
|
|
458
|
-
*/
|
|
459
|
-
type ProcErrors<S extends AnyService, ProcName extends keyof S['procedures']> = TUnion<[S['procedures'][ProcName]['errors'], typeof RiverUncaughtSchema]>;
|
|
460
|
-
/**
|
|
461
|
-
* Helper to get the type of procedure in a service.
|
|
462
|
-
* @template S - The service.
|
|
463
|
-
* @template ProcName - The name of the procedure.
|
|
464
|
-
*/
|
|
465
|
-
type ProcType<S extends AnyService, ProcName extends keyof S['procedures']> = S['procedures'][ProcName]['type'];
|
|
466
|
-
/**
|
|
467
|
-
* A list of procedures where every procedure is "branded", as-in the procedure
|
|
468
|
-
* was created via the {@link Procedure} constructors.
|
|
469
|
-
*/
|
|
470
|
-
type BrandedProcedureMap<State> = Record<string, Branded<AnyProcedure<State>>>;
|
|
471
|
-
/**
|
|
472
|
-
* The configuration for a service.
|
|
473
|
-
*/
|
|
474
|
-
interface ServiceConfiguration<State extends object> {
|
|
475
|
-
/**
|
|
476
|
-
* A factory function for creating a fresh state.
|
|
477
|
-
*/
|
|
478
|
-
initializeState: () => State;
|
|
479
|
-
}
|
|
480
|
-
interface SerializedServiceSchema {
|
|
481
|
-
procedures: Record<string, {
|
|
482
|
-
input: PayloadType;
|
|
483
|
-
output: PayloadType;
|
|
484
|
-
errors?: RiverError;
|
|
485
|
-
type: 'rpc' | 'subscription' | 'upload' | 'stream';
|
|
486
|
-
init?: PayloadType;
|
|
487
|
-
}>;
|
|
488
|
-
}
|
|
489
|
-
interface SerializedServerSchema {
|
|
490
|
-
handshakeSchema?: TSchema;
|
|
491
|
-
services: Record<string, SerializedServiceSchema>;
|
|
492
|
-
}
|
|
493
|
-
declare function serializeSchema(services: AnyServiceSchemaMap, handshakeSchema?: TSchema): SerializedServerSchema;
|
|
494
|
-
/**
|
|
495
|
-
* The schema for a {@link Service}. This is used to define a service, specifically
|
|
496
|
-
* its initial state and procedures.
|
|
497
|
-
*
|
|
498
|
-
* There are two ways to define a service:
|
|
499
|
-
* 1. the {@link ServiceSchema.define} static method, which takes a configuration and
|
|
500
|
-
* a list of procedures directly. Use this to ergonomically define a service schema
|
|
501
|
-
* in one go. Good for smaller services, especially if they're stateless.
|
|
502
|
-
* 2. the {@link ServiceSchema.scaffold} static method, which creates a scaffold that
|
|
503
|
-
* can be used to define procedures separately from the configuration. Use this to
|
|
504
|
-
* better organize your service's definition, especially if it's a large service.
|
|
505
|
-
* You can also use it in a builder pattern to define the service in a more
|
|
506
|
-
* fluent way.
|
|
507
|
-
*
|
|
508
|
-
* See the static methods for more information and examples.
|
|
509
|
-
*
|
|
510
|
-
* When defining procedures, use the {@link Procedure} constructors to create them.
|
|
511
|
-
*/
|
|
512
|
-
declare class ServiceSchema<State extends object, Procedures extends ProcedureMap<State>> {
|
|
513
|
-
/**
|
|
514
|
-
* Factory function for creating a fresh state.
|
|
515
|
-
*/
|
|
516
|
-
protected readonly initializeState: () => State;
|
|
517
|
-
/**
|
|
518
|
-
* The procedures for this service.
|
|
519
|
-
*/
|
|
520
|
-
readonly procedures: Procedures;
|
|
521
|
-
/**
|
|
522
|
-
* @param config - The configuration for this service.
|
|
523
|
-
* @param procedures - The procedures for this service.
|
|
524
|
-
*/
|
|
525
|
-
protected constructor(config: ServiceConfiguration<State>, procedures: Procedures);
|
|
526
|
-
/**
|
|
527
|
-
* Creates a {@link ServiceScaffold}, which can be used to define procedures
|
|
528
|
-
* that can then be merged into a {@link ServiceSchema}, via the scaffold's
|
|
529
|
-
* `finalize` method.
|
|
530
|
-
*
|
|
531
|
-
* There are two patterns that work well with this method. The first is using
|
|
532
|
-
* it to separate the definition of procedures from the definition of the
|
|
533
|
-
* service's configuration:
|
|
534
|
-
* ```ts
|
|
535
|
-
* const MyServiceScaffold = ServiceSchema.scaffold({
|
|
536
|
-
* initializeState: () => ({ count: 0 }),
|
|
537
|
-
* });
|
|
538
|
-
*
|
|
539
|
-
* const incrementProcedures = MyServiceScaffold.procedures({
|
|
540
|
-
* increment: Procedure.rpc({
|
|
541
|
-
* input: Type.Object({ amount: Type.Number() }),
|
|
542
|
-
* output: Type.Object({ current: Type.Number() }),
|
|
543
|
-
* async handler(ctx, input) {
|
|
544
|
-
* ctx.state.count += input.amount;
|
|
545
|
-
* return Ok({ current: ctx.state.count });
|
|
546
|
-
* }
|
|
547
|
-
* }),
|
|
548
|
-
* })
|
|
549
|
-
*
|
|
550
|
-
* const MyService = MyServiceScaffold.finalize({
|
|
551
|
-
* ...incrementProcedures,
|
|
552
|
-
* // you can also directly define procedures here
|
|
553
|
-
* });
|
|
554
|
-
* ```
|
|
555
|
-
* This might be really handy if you have a very large service and you're
|
|
556
|
-
* wanting to split it over multiple files. You can define the scaffold
|
|
557
|
-
* in one file, and then import that scaffold in other files where you
|
|
558
|
-
* define procedures - and then finally import the scaffolds and your
|
|
559
|
-
* procedure objects in a final file where you finalize the scaffold into
|
|
560
|
-
* a service schema.
|
|
561
|
-
*
|
|
562
|
-
* The other way is to use it like in a builder pattern:
|
|
563
|
-
* ```ts
|
|
564
|
-
* const MyService = ServiceSchema
|
|
565
|
-
* .scaffold({ initializeState: () => ({ count: 0 }) })
|
|
566
|
-
* .finalize({
|
|
567
|
-
* increment: Procedure.rpc({
|
|
568
|
-
* input: Type.Object({ amount: Type.Number() }),
|
|
569
|
-
* output: Type.Object({ current: Type.Number() }),
|
|
570
|
-
* async handler(ctx, input) {
|
|
571
|
-
* ctx.state.count += input.amount;
|
|
572
|
-
* return Ok({ current: ctx.state.count });
|
|
573
|
-
* }
|
|
574
|
-
* }),
|
|
575
|
-
* })
|
|
576
|
-
* ```
|
|
577
|
-
* Depending on your preferences, this may be a more appealing way to define
|
|
578
|
-
* a schema versus using the {@link ServiceSchema.define} method.
|
|
579
|
-
*/
|
|
580
|
-
static scaffold<State extends object>(config: ServiceConfiguration<State>): ServiceScaffold<State>;
|
|
581
|
-
/**
|
|
582
|
-
* Creates a new {@link ServiceSchema} with the given configuration and procedures.
|
|
583
|
-
*
|
|
584
|
-
* All procedures must be created with the {@link Procedure} constructors.
|
|
585
|
-
*
|
|
586
|
-
* NOTE: There is an overload that lets you just provide the procedures alone if your
|
|
587
|
-
* service has no state.
|
|
588
|
-
*
|
|
589
|
-
* @param config - The configuration for this service.
|
|
590
|
-
* @param procedures - The procedures for this service.
|
|
591
|
-
*
|
|
592
|
-
* @example
|
|
593
|
-
* ```
|
|
594
|
-
* const service = ServiceSchema.define(
|
|
595
|
-
* { initializeState: () => ({ count: 0 }) },
|
|
596
|
-
* {
|
|
597
|
-
* increment: Procedure.rpc({
|
|
598
|
-
* input: Type.Object({ amount: Type.Number() }),
|
|
599
|
-
* output: Type.Object({ current: Type.Number() }),
|
|
600
|
-
* async handler(ctx, input) {
|
|
601
|
-
* ctx.state.count += input.amount;
|
|
602
|
-
* return Ok({ current: ctx.state.count });
|
|
603
|
-
* }
|
|
604
|
-
* }),
|
|
605
|
-
* },
|
|
606
|
-
* );
|
|
607
|
-
* ```
|
|
608
|
-
*/
|
|
609
|
-
static define<State extends object, Procedures extends BrandedProcedureMap<State>>(config: ServiceConfiguration<State>, procedures: Procedures): ServiceSchema<State, {
|
|
610
|
-
[K in keyof Procedures]: Unbranded<Procedures[K]>;
|
|
611
|
-
}>;
|
|
612
|
-
/**
|
|
613
|
-
* Creates a new {@link ServiceSchema} with the given procedures.
|
|
614
|
-
*
|
|
615
|
-
* All procedures must be created with the {@link Procedure} constructors.
|
|
616
|
-
*
|
|
617
|
-
* NOTE: There is an overload that lets you provide configuration as well,
|
|
618
|
-
* if your service has extra configuration like a state.
|
|
619
|
-
*
|
|
620
|
-
* @param procedures - The procedures for this service.
|
|
621
|
-
*
|
|
622
|
-
* @example
|
|
623
|
-
* ```
|
|
624
|
-
* const service = ServiceSchema.define({
|
|
625
|
-
* add: Procedure.rpc({
|
|
626
|
-
* input: Type.Object({ a: Type.Number(), b: Type.Number() }),
|
|
627
|
-
* output: Type.Object({ result: Type.Number() }),
|
|
628
|
-
* async handler(ctx, input) {
|
|
629
|
-
* return Ok({ result: input.a + input.b });
|
|
630
|
-
* }
|
|
631
|
-
* }),
|
|
632
|
-
* });
|
|
633
|
-
*/
|
|
634
|
-
static define<Procedures extends BrandedProcedureMap<Record<string, never>>>(procedures: Procedures): ServiceSchema<Record<string, never>, {
|
|
635
|
-
[K in keyof Procedures]: Unbranded<Procedures[K]>;
|
|
636
|
-
}>;
|
|
637
|
-
/**
|
|
638
|
-
* Serializes this schema's procedures into a plain object that is JSON compatible.
|
|
639
|
-
*/
|
|
640
|
-
serialize(): SerializedServiceSchema;
|
|
641
|
-
/**
|
|
642
|
-
* Instantiates this schema into a {@link Service} object.
|
|
643
|
-
*
|
|
644
|
-
* You probably don't need this, usually the River server will handle this
|
|
645
|
-
* for you.
|
|
646
|
-
*/
|
|
647
|
-
instantiate(): Service<State, Procedures>;
|
|
648
|
-
}
|
|
649
|
-
/**
|
|
650
|
-
* A scaffold for defining a service's procedures.
|
|
651
|
-
*
|
|
652
|
-
* @see {@link ServiceSchema.scaffold}
|
|
653
|
-
*/
|
|
654
|
-
declare class ServiceScaffold<State extends object> {
|
|
655
|
-
/**
|
|
656
|
-
* The configuration for this service.
|
|
657
|
-
*/
|
|
658
|
-
protected readonly config: ServiceConfiguration<State>;
|
|
659
|
-
/**
|
|
660
|
-
* @param config - The configuration for this service.
|
|
661
|
-
*/
|
|
662
|
-
constructor(config: ServiceConfiguration<State>);
|
|
663
|
-
/**
|
|
664
|
-
* Define procedures for this service. Use the {@link Procedure} constructors
|
|
665
|
-
* to create them. This returns the procedures object, which can then be
|
|
666
|
-
* passed to {@link ServiceSchema.finalize} to create a {@link ServiceSchema}.
|
|
667
|
-
*
|
|
668
|
-
* @example
|
|
669
|
-
* ```
|
|
670
|
-
* const myProcedures = MyServiceScaffold.procedures({
|
|
671
|
-
* myRPC: Procedure.rpc({
|
|
672
|
-
* // ...
|
|
673
|
-
* }),
|
|
674
|
-
* });
|
|
675
|
-
*
|
|
676
|
-
* const MyService = MyServiceScaffold.finalize({
|
|
677
|
-
* ...myProcedures,
|
|
678
|
-
* });
|
|
679
|
-
* ```
|
|
680
|
-
*
|
|
681
|
-
* @param procedures - The procedures for this service.
|
|
682
|
-
*/
|
|
683
|
-
procedures<T extends BrandedProcedureMap<State>>(procedures: T): T;
|
|
684
|
-
/**
|
|
685
|
-
* Finalizes the scaffold into a {@link ServiceSchema}. This is where you
|
|
686
|
-
* provide the service's procedures and get a {@link ServiceSchema} in return.
|
|
687
|
-
*
|
|
688
|
-
* You can directly define procedures here, or you can define them separately
|
|
689
|
-
* with the {@link ServiceScaffold.procedures} method, and then pass them here.
|
|
690
|
-
*
|
|
691
|
-
* @example
|
|
692
|
-
* ```
|
|
693
|
-
* const MyService = MyServiceScaffold.finalize({
|
|
694
|
-
* myRPC: Procedure.rpc({
|
|
695
|
-
* // ...
|
|
696
|
-
* }),
|
|
697
|
-
* // e.g. from the procedures method
|
|
698
|
-
* ...myOtherProcedures,
|
|
699
|
-
* });
|
|
700
|
-
* ```
|
|
701
|
-
*/
|
|
702
|
-
finalize<T extends BrandedProcedureMap<State>>(procedures: T): ServiceSchema<State, {
|
|
703
|
-
[K in keyof T]: Unbranded<T[K]>;
|
|
704
|
-
}>;
|
|
705
|
-
}
|
|
706
|
-
|
|
707
|
-
export { AnyServiceSchemaMap as A, Client as C, Err as E, InstantiatedServiceSchemaMap as I, Ok as O, PayloadType as P, RiverError as R, Service as S, UploadProcedure as U, ValidProcType as V, Procedure as a, Result as b, RiverUncaughtSchema as c, ProcedureResult as d, ServiceConfiguration as e, ProcHandler as f, ProcInit as g, ProcInput as h, ProcOutput as i, ProcErrors as j, ProcType as k, ServiceSchema as l, SerializedServerSchema as m, ProcedureMap as n, RpcProcedure as o, SubscriptionProcedure as p, StreamProcedure as q, createClient as r, serializeSchema as s, UNCAUGHT_ERROR as t, RiverErrorSchema as u, ResultUnwrapOk as v, ResultUnwrapErr as w, Output as x };
|