effect-orpc 0.0.5 → 0.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,422 @@
1
+ import type { ORPCError, ORPCErrorCode } from "@orpc/client";
2
+ import type {
3
+ AnySchema,
4
+ ErrorMap,
5
+ ErrorMapItem,
6
+ Meta,
7
+ Route,
8
+ Schema,
9
+ } from "@orpc/contract";
10
+ import type {
11
+ Builder,
12
+ BuilderConfig,
13
+ BuilderDef,
14
+ BuilderWithMiddlewares,
15
+ Context,
16
+ EnhanceRouterOptions,
17
+ ProcedureBuilder,
18
+ ProcedureBuilderWithInput,
19
+ ProcedureBuilderWithInputOutput,
20
+ ProcedureBuilderWithOutput,
21
+ ProcedureDef,
22
+ ProcedureHandlerOptions,
23
+ RouterBuilder,
24
+ } from "@orpc/server";
25
+ import type { Promisable } from "@orpc/shared";
26
+ import type { Effect, ManagedRuntime } from "effect";
27
+ import type { YieldWrap } from "effect/Utils";
28
+
29
+ import type {
30
+ EffectErrorConstructorMap,
31
+ EffectErrorMap,
32
+ EffectErrorMapToUnion,
33
+ ORPCTaggedErrorInstance,
34
+ } from "../tagged-error";
35
+
36
+ /**
37
+ * Extended builder definition that includes the Effect ManagedRuntime.
38
+ */
39
+ export interface EffectBuilderDef<
40
+ TInputSchema extends AnySchema,
41
+ TOutputSchema extends AnySchema,
42
+ TEffectErrorMap extends EffectErrorMap,
43
+ TMeta extends Meta,
44
+ TRequirementsProvided,
45
+ TRuntimeError,
46
+ > extends EnhanceRouterOptions<EffectErrorMapToErrorMap<TEffectErrorMap>> {
47
+ inputValidationIndex: number;
48
+ outputValidationIndex: number;
49
+ config: BuilderConfig;
50
+ meta: TMeta;
51
+ route: Route;
52
+ inputSchema?: TInputSchema;
53
+ outputSchema?: TOutputSchema;
54
+ runtime: ManagedRuntime.ManagedRuntime<TRequirementsProvided, TRuntimeError>;
55
+ /**
56
+ * Optional span configuration for Effect tracing.
57
+ */
58
+ spanConfig?: EffectSpanConfig;
59
+ /**
60
+ * Effect-extended error map that supports both traditional errors and tagged errors.
61
+ */
62
+ effectErrorMap: TEffectErrorMap;
63
+ }
64
+
65
+ export type NonEffectProcedureHandler<
66
+ TCurrentContext extends Context,
67
+ TInput,
68
+ THandlerOutput,
69
+ TEffectErrorMap extends EffectErrorMap,
70
+ TMeta extends Meta,
71
+ > = (
72
+ opt: ProcedureHandlerOptions<
73
+ TCurrentContext,
74
+ TInput,
75
+ EffectErrorConstructorMap<TEffectErrorMap>,
76
+ TMeta
77
+ >,
78
+ ) => Promisable<THandlerOutput>;
79
+
80
+ /**
81
+ * Extended procedure definition that includes the Effect ManagedRuntime.
82
+ */
83
+ export interface EffectProcedureDef<
84
+ TInitialContext extends Context,
85
+ TCurrentContext extends Context,
86
+ TInputSchema extends AnySchema,
87
+ TOutputSchema extends AnySchema,
88
+ TEffectErrorMap extends EffectErrorMap,
89
+ TMeta extends Meta,
90
+ TRequirementsProvided,
91
+ TRuntimeError,
92
+ > extends ProcedureDef<
93
+ TInitialContext,
94
+ TCurrentContext,
95
+ TInputSchema,
96
+ TOutputSchema,
97
+ EffectErrorMapToErrorMap<TEffectErrorMap>,
98
+ TMeta
99
+ > {
100
+ runtime: ManagedRuntime.ManagedRuntime<TRequirementsProvided, TRuntimeError>;
101
+ effectErrorMap: TEffectErrorMap;
102
+ }
103
+
104
+ /**
105
+ * Configuration for Effect span tracing.
106
+ */
107
+ export interface EffectSpanConfig {
108
+ /**
109
+ * The name of the span for telemetry.
110
+ */
111
+ name: string;
112
+ /**
113
+ * Function to lazily capture the stack trace at definition time.
114
+ */
115
+ captureStackTrace: () => string | undefined;
116
+ }
117
+
118
+ /**
119
+ * Handler type for Effect procedures.
120
+ * The handler receives procedure options and returns an Effect.
121
+ */
122
+ export type EffectProcedureHandler<
123
+ TCurrentContext extends Context,
124
+ TInput,
125
+ THandlerOutput,
126
+ TEffectErrorMap extends EffectErrorMap,
127
+ TRequirementsProvided,
128
+ TMeta extends Meta,
129
+ > = (
130
+ opt: ProcedureHandlerOptions<
131
+ TCurrentContext,
132
+ TInput,
133
+ EffectErrorConstructorMap<TEffectErrorMap>,
134
+ TMeta
135
+ >,
136
+ ) => Generator<
137
+ YieldWrap<
138
+ Effect.Effect<
139
+ any,
140
+ | EffectErrorMapToUnion<TEffectErrorMap>
141
+ | ORPCError<ORPCErrorCode, unknown>,
142
+ TRequirementsProvided
143
+ >
144
+ >,
145
+ THandlerOutput,
146
+ never
147
+ >;
148
+
149
+ export type EffectErrorMapToErrorMap<T extends EffectErrorMap> = {
150
+ [K in keyof T]: K extends ORPCErrorCode
151
+ ? T[K] extends ErrorMapItem<AnySchema>
152
+ ? T[K]
153
+ : T[K] extends {
154
+ new (
155
+ ...args: any[]
156
+ ): ORPCTaggedErrorInstance<any, any, infer TSchema>;
157
+ }
158
+ ? ErrorMapItem<TSchema>
159
+ : never
160
+ : never;
161
+ };
162
+
163
+ /**
164
+ * Any oRPC builder-like object that has the `~orpc` definition property.
165
+ * This includes Builder, BuilderWithMiddlewares, ProcedureBuilder, etc.
166
+ */
167
+ export interface AnyBuilderLike<
168
+ TInputSchema extends AnySchema = AnySchema,
169
+ TOutputSchema extends AnySchema = AnySchema,
170
+ TErrorMap extends ErrorMap = ErrorMap,
171
+ TMeta extends Meta = Meta,
172
+ > {
173
+ "~orpc": BuilderDef<TInputSchema, TOutputSchema, TErrorMap, TMeta>;
174
+ }
175
+
176
+ /**
177
+ * Infers the initial context from an oRPC builder type.
178
+ * Since context is a phantom type parameter not present in `~orpc`,
179
+ * we need to use conditional type inference on the known builder types.
180
+ */
181
+ export type InferBuilderInitialContext<T> =
182
+ T extends Builder<infer TInitial, any, any, any, any, any>
183
+ ? TInitial
184
+ : T extends BuilderWithMiddlewares<infer TInitial, any, any, any, any, any>
185
+ ? TInitial
186
+ : T extends ProcedureBuilder<infer TInitial, any, any, any, any, any>
187
+ ? TInitial
188
+ : T extends ProcedureBuilderWithInput<
189
+ infer TInitial,
190
+ any,
191
+ any,
192
+ any,
193
+ any,
194
+ any
195
+ >
196
+ ? TInitial
197
+ : T extends ProcedureBuilderWithOutput<
198
+ infer TInitial,
199
+ any,
200
+ any,
201
+ any,
202
+ any,
203
+ any
204
+ >
205
+ ? TInitial
206
+ : T extends ProcedureBuilderWithInputOutput<
207
+ infer TInitial,
208
+ any,
209
+ any,
210
+ any,
211
+ any,
212
+ any
213
+ >
214
+ ? TInitial
215
+ : T extends RouterBuilder<infer TInitial, any, any, any>
216
+ ? TInitial
217
+ : Context;
218
+
219
+ /**
220
+ * Infers the current context from an oRPC builder type.
221
+ * Since context is a phantom type parameter not present in `~orpc`,
222
+ * we need to use conditional type inference on the known builder types.
223
+ */
224
+ export type InferBuilderCurrentContext<T> =
225
+ T extends Builder<any, infer TCurrent, any, any, any, any>
226
+ ? TCurrent
227
+ : T extends BuilderWithMiddlewares<any, infer TCurrent, any, any, any, any>
228
+ ? TCurrent
229
+ : T extends ProcedureBuilder<any, infer TCurrent, any, any, any, any>
230
+ ? TCurrent
231
+ : T extends ProcedureBuilderWithInput<
232
+ any,
233
+ infer TCurrent,
234
+ any,
235
+ any,
236
+ any,
237
+ any
238
+ >
239
+ ? TCurrent
240
+ : T extends ProcedureBuilderWithOutput<
241
+ any,
242
+ infer TCurrent,
243
+ any,
244
+ any,
245
+ any,
246
+ any
247
+ >
248
+ ? TCurrent
249
+ : T extends ProcedureBuilderWithInputOutput<
250
+ any,
251
+ infer TCurrent,
252
+ any,
253
+ any,
254
+ any,
255
+ any
256
+ >
257
+ ? TCurrent
258
+ : T extends RouterBuilder<any, infer TCurrent, any, any>
259
+ ? TCurrent
260
+ : Context;
261
+
262
+ /**
263
+ * Infers the input schema from an oRPC builder type.
264
+ */
265
+ export type InferBuilderInputSchema<T> =
266
+ T extends Builder<any, any, infer TInput, any, any, any>
267
+ ? TInput
268
+ : T extends BuilderWithMiddlewares<any, any, infer TInput, any, any, any>
269
+ ? TInput
270
+ : T extends ProcedureBuilder<any, any, infer TInput, any, any, any>
271
+ ? TInput
272
+ : T extends ProcedureBuilderWithInput<
273
+ any,
274
+ any,
275
+ infer TInput,
276
+ any,
277
+ any,
278
+ any
279
+ >
280
+ ? TInput
281
+ : T extends ProcedureBuilderWithOutput<
282
+ any,
283
+ any,
284
+ infer TInput,
285
+ any,
286
+ any,
287
+ any
288
+ >
289
+ ? TInput
290
+ : T extends ProcedureBuilderWithInputOutput<
291
+ any,
292
+ any,
293
+ infer TInput,
294
+ any,
295
+ any,
296
+ any
297
+ >
298
+ ? TInput
299
+ : Schema<unknown, unknown>;
300
+
301
+ /**
302
+ * Infers the output schema from an oRPC builder type.
303
+ */
304
+ export type InferBuilderOutputSchema<T> =
305
+ T extends Builder<any, any, any, infer TOutput, any, any>
306
+ ? TOutput
307
+ : T extends BuilderWithMiddlewares<any, any, any, infer TOutput, any, any>
308
+ ? TOutput
309
+ : T extends ProcedureBuilder<any, any, any, infer TOutput, any, any>
310
+ ? TOutput
311
+ : T extends ProcedureBuilderWithInput<
312
+ any,
313
+ any,
314
+ any,
315
+ infer TOutput,
316
+ any,
317
+ any
318
+ >
319
+ ? TOutput
320
+ : T extends ProcedureBuilderWithOutput<
321
+ any,
322
+ any,
323
+ any,
324
+ infer TOutput,
325
+ any,
326
+ any
327
+ >
328
+ ? TOutput
329
+ : T extends ProcedureBuilderWithInputOutput<
330
+ any,
331
+ any,
332
+ any,
333
+ infer TOutput,
334
+ any,
335
+ any
336
+ >
337
+ ? TOutput
338
+ : Schema<unknown, unknown>;
339
+
340
+ /**
341
+ * Infers the error map from an oRPC builder type.
342
+ */
343
+ export type InferBuilderErrorMap<T> =
344
+ T extends Builder<any, any, any, any, infer TErrorMap, any>
345
+ ? TErrorMap
346
+ : T extends BuilderWithMiddlewares<any, any, any, any, infer TErrorMap, any>
347
+ ? TErrorMap
348
+ : T extends ProcedureBuilder<any, any, any, any, infer TErrorMap, any>
349
+ ? TErrorMap
350
+ : T extends ProcedureBuilderWithInput<
351
+ any,
352
+ any,
353
+ any,
354
+ any,
355
+ infer TErrorMap,
356
+ any
357
+ >
358
+ ? TErrorMap
359
+ : T extends ProcedureBuilderWithOutput<
360
+ any,
361
+ any,
362
+ any,
363
+ any,
364
+ infer TErrorMap,
365
+ any
366
+ >
367
+ ? TErrorMap
368
+ : T extends ProcedureBuilderWithInputOutput<
369
+ any,
370
+ any,
371
+ any,
372
+ any,
373
+ infer TErrorMap,
374
+ any
375
+ >
376
+ ? TErrorMap
377
+ : T extends RouterBuilder<any, any, infer TErrorMap, any>
378
+ ? TErrorMap
379
+ : ErrorMap;
380
+
381
+ /**
382
+ * Infers the meta from an oRPC builder type.
383
+ */
384
+ export type InferBuilderMeta<T> =
385
+ T extends Builder<any, any, any, any, any, infer TMeta>
386
+ ? TMeta
387
+ : T extends BuilderWithMiddlewares<any, any, any, any, any, infer TMeta>
388
+ ? TMeta
389
+ : T extends ProcedureBuilder<any, any, any, any, any, infer TMeta>
390
+ ? TMeta
391
+ : T extends ProcedureBuilderWithInput<
392
+ any,
393
+ any,
394
+ any,
395
+ any,
396
+ any,
397
+ infer TMeta
398
+ >
399
+ ? TMeta
400
+ : T extends ProcedureBuilderWithOutput<
401
+ any,
402
+ any,
403
+ any,
404
+ any,
405
+ any,
406
+ infer TMeta
407
+ >
408
+ ? TMeta
409
+ : T extends ProcedureBuilderWithInputOutput<
410
+ any,
411
+ any,
412
+ any,
413
+ any,
414
+ any,
415
+ infer TMeta
416
+ >
417
+ ? TMeta
418
+ : T extends RouterBuilder<any, any, any, infer TMeta>
419
+ ? TMeta
420
+ : Meta;
421
+
422
+ export * from "./variants";