effect-orpc 0.0.1
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/.cursor/hooks.json +10 -0
- package/.cursor/rules/use-bun-instead-of-node-vite-npm-pnpm.mdc +111 -0
- package/.github/workflows/publish.yml +45 -0
- package/.oxfmtrc.jsonc +23 -0
- package/.oxlintrc.json +4 -0
- package/.vscode/settings.json +59 -0
- package/LICENSE +21 -0
- package/README.md +538 -0
- package/bun.lock +403 -0
- package/package.json +55 -0
- package/src/effect-builder.ts +680 -0
- package/src/effect-procedure.ts +354 -0
- package/src/index.ts +36 -0
- package/src/tagged-error.ts +515 -0
- package/src/tests/effect-builder.test.ts +488 -0
- package/src/tests/effect-error-map.test.ts +313 -0
- package/src/tests/effect-procedure.test.ts +213 -0
- package/src/tests/shared.ts +79 -0
- package/src/tests/tagged-error.test.ts +311 -0
- package/tsconfig.json +30 -0
- package/tsup.config.ts +8 -0
|
@@ -0,0 +1,354 @@
|
|
|
1
|
+
import type { ClientContext } from "@orpc/client";
|
|
2
|
+
import type {
|
|
3
|
+
AnySchema,
|
|
4
|
+
ErrorMap,
|
|
5
|
+
InferSchemaInput,
|
|
6
|
+
InferSchemaOutput,
|
|
7
|
+
Meta,
|
|
8
|
+
Route,
|
|
9
|
+
} from "@orpc/contract";
|
|
10
|
+
import type {
|
|
11
|
+
AnyMiddleware,
|
|
12
|
+
Context,
|
|
13
|
+
CreateProcedureClientOptions,
|
|
14
|
+
MapInputMiddleware,
|
|
15
|
+
MergedCurrentContext,
|
|
16
|
+
MergedInitialContext,
|
|
17
|
+
Middleware,
|
|
18
|
+
ORPCErrorConstructorMap,
|
|
19
|
+
ProcedureActionableClient,
|
|
20
|
+
ProcedureClient,
|
|
21
|
+
ProcedureDef,
|
|
22
|
+
} from "@orpc/server";
|
|
23
|
+
import type { IntersectPick, MaybeOptionalOptions } from "@orpc/shared";
|
|
24
|
+
import type { ManagedRuntime } from "effect";
|
|
25
|
+
|
|
26
|
+
import { mergeMeta, mergeRoute } from "@orpc/contract";
|
|
27
|
+
import {
|
|
28
|
+
addMiddleware,
|
|
29
|
+
createActionableClient,
|
|
30
|
+
createProcedureClient,
|
|
31
|
+
decorateMiddleware,
|
|
32
|
+
Procedure,
|
|
33
|
+
} from "@orpc/server";
|
|
34
|
+
|
|
35
|
+
import type { EffectErrorMap, MergedEffectErrorMap } from "./tagged-error";
|
|
36
|
+
|
|
37
|
+
import { effectErrorMapToErrorMap } from "./tagged-error";
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Extended procedure definition that includes the Effect ManagedRuntime.
|
|
41
|
+
*/
|
|
42
|
+
export interface EffectProcedureDef<
|
|
43
|
+
TInitialContext extends Context,
|
|
44
|
+
TCurrentContext extends Context,
|
|
45
|
+
TInputSchema extends AnySchema,
|
|
46
|
+
TOutputSchema extends AnySchema,
|
|
47
|
+
TEffectErrorMap extends EffectErrorMap,
|
|
48
|
+
TMeta extends Meta,
|
|
49
|
+
TRequirementsProvided,
|
|
50
|
+
TRuntimeError,
|
|
51
|
+
> extends ProcedureDef<
|
|
52
|
+
TInitialContext,
|
|
53
|
+
TCurrentContext,
|
|
54
|
+
TInputSchema,
|
|
55
|
+
TOutputSchema,
|
|
56
|
+
ErrorMap,
|
|
57
|
+
TMeta
|
|
58
|
+
> {
|
|
59
|
+
runtime: ManagedRuntime.ManagedRuntime<TRequirementsProvided, TRuntimeError>;
|
|
60
|
+
effectErrorMap: TEffectErrorMap;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* An Effect-native decorated procedure that preserves Effect error and requirements types.
|
|
65
|
+
*
|
|
66
|
+
* This class extends Procedure with additional type parameters for Effect-specific
|
|
67
|
+
* type information, allowing full type inference of Effect errors and requirements.
|
|
68
|
+
*/
|
|
69
|
+
export class EffectDecoratedProcedure<
|
|
70
|
+
TInitialContext extends Context,
|
|
71
|
+
TCurrentContext extends Context,
|
|
72
|
+
TInputSchema extends AnySchema,
|
|
73
|
+
TOutputSchema extends AnySchema,
|
|
74
|
+
TEffectErrorMap extends EffectErrorMap,
|
|
75
|
+
TMeta extends Meta,
|
|
76
|
+
TRequirementsProvided,
|
|
77
|
+
TRuntimeError,
|
|
78
|
+
> extends Procedure<
|
|
79
|
+
TInitialContext,
|
|
80
|
+
TCurrentContext,
|
|
81
|
+
TInputSchema,
|
|
82
|
+
TOutputSchema,
|
|
83
|
+
ErrorMap,
|
|
84
|
+
TMeta
|
|
85
|
+
> {
|
|
86
|
+
declare "~orpc": EffectProcedureDef<
|
|
87
|
+
TInitialContext,
|
|
88
|
+
TCurrentContext,
|
|
89
|
+
TInputSchema,
|
|
90
|
+
TOutputSchema,
|
|
91
|
+
TEffectErrorMap,
|
|
92
|
+
TMeta,
|
|
93
|
+
TRequirementsProvided,
|
|
94
|
+
TRuntimeError
|
|
95
|
+
>;
|
|
96
|
+
|
|
97
|
+
constructor(
|
|
98
|
+
def: EffectProcedureDef<
|
|
99
|
+
TInitialContext,
|
|
100
|
+
TCurrentContext,
|
|
101
|
+
TInputSchema,
|
|
102
|
+
TOutputSchema,
|
|
103
|
+
TEffectErrorMap,
|
|
104
|
+
TMeta,
|
|
105
|
+
TRequirementsProvided,
|
|
106
|
+
TRuntimeError
|
|
107
|
+
>,
|
|
108
|
+
) {
|
|
109
|
+
super(def);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Adds type-safe custom errors.
|
|
114
|
+
* Supports both traditional oRPC error definitions and ORPCTaggedError classes.
|
|
115
|
+
*
|
|
116
|
+
* @see {@link https://orpc.dev/docs/error-handling#type%E2%80%90safe-error-handling Type-Safe Error Handling Docs}
|
|
117
|
+
*/
|
|
118
|
+
errors<U extends EffectErrorMap>(
|
|
119
|
+
errors: U,
|
|
120
|
+
): EffectDecoratedProcedure<
|
|
121
|
+
TInitialContext,
|
|
122
|
+
TCurrentContext,
|
|
123
|
+
TInputSchema,
|
|
124
|
+
TOutputSchema,
|
|
125
|
+
MergedEffectErrorMap<TEffectErrorMap, U>,
|
|
126
|
+
TMeta,
|
|
127
|
+
TRequirementsProvided,
|
|
128
|
+
TRuntimeError
|
|
129
|
+
> {
|
|
130
|
+
const newEffectErrorMap = { ...this["~orpc"].effectErrorMap, ...errors };
|
|
131
|
+
return new EffectDecoratedProcedure({
|
|
132
|
+
...this["~orpc"],
|
|
133
|
+
effectErrorMap: newEffectErrorMap,
|
|
134
|
+
errorMap: effectErrorMapToErrorMap(newEffectErrorMap),
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Sets or updates the metadata.
|
|
140
|
+
* The provided metadata is spared-merged with any existing metadata.
|
|
141
|
+
*
|
|
142
|
+
* @see {@link https://orpc.dev/docs/metadata Metadata Docs}
|
|
143
|
+
*/
|
|
144
|
+
meta(
|
|
145
|
+
meta: TMeta,
|
|
146
|
+
): EffectDecoratedProcedure<
|
|
147
|
+
TInitialContext,
|
|
148
|
+
TCurrentContext,
|
|
149
|
+
TInputSchema,
|
|
150
|
+
TOutputSchema,
|
|
151
|
+
TEffectErrorMap,
|
|
152
|
+
TMeta,
|
|
153
|
+
TRequirementsProvided,
|
|
154
|
+
TRuntimeError
|
|
155
|
+
> {
|
|
156
|
+
return new EffectDecoratedProcedure({
|
|
157
|
+
...this["~orpc"],
|
|
158
|
+
meta: mergeMeta(this["~orpc"].meta, meta),
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Sets or updates the route definition.
|
|
164
|
+
* The provided route is spared-merged with any existing route.
|
|
165
|
+
* This option is typically relevant when integrating with OpenAPI.
|
|
166
|
+
*
|
|
167
|
+
* @see {@link https://orpc.dev/docs/openapi/routing OpenAPI Routing Docs}
|
|
168
|
+
* @see {@link https://orpc.dev/docs/openapi/input-output-structure OpenAPI Input/Output Structure Docs}
|
|
169
|
+
*/
|
|
170
|
+
route(
|
|
171
|
+
route: Route,
|
|
172
|
+
): EffectDecoratedProcedure<
|
|
173
|
+
TInitialContext,
|
|
174
|
+
TCurrentContext,
|
|
175
|
+
TInputSchema,
|
|
176
|
+
TOutputSchema,
|
|
177
|
+
TEffectErrorMap,
|
|
178
|
+
TMeta,
|
|
179
|
+
TRequirementsProvided,
|
|
180
|
+
TRuntimeError
|
|
181
|
+
> {
|
|
182
|
+
return new EffectDecoratedProcedure({
|
|
183
|
+
...this["~orpc"],
|
|
184
|
+
route: mergeRoute(this["~orpc"].route, route),
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* Uses a middleware to modify the context or improve the pipeline.
|
|
190
|
+
*
|
|
191
|
+
* @info Supports both normal middleware and inline middleware implementations.
|
|
192
|
+
* @info Pass second argument to map the input.
|
|
193
|
+
* @note The current context must be satisfy middleware dependent-context
|
|
194
|
+
* @see {@link https://orpc.dev/docs/middleware Middleware Docs}
|
|
195
|
+
*/
|
|
196
|
+
use<
|
|
197
|
+
UOutContext extends IntersectPick<TCurrentContext, UOutContext>,
|
|
198
|
+
UInContext extends Context = TCurrentContext,
|
|
199
|
+
>(
|
|
200
|
+
middleware: Middleware<
|
|
201
|
+
UInContext | TCurrentContext,
|
|
202
|
+
UOutContext,
|
|
203
|
+
InferSchemaOutput<TInputSchema>,
|
|
204
|
+
InferSchemaInput<TOutputSchema>,
|
|
205
|
+
ORPCErrorConstructorMap<ErrorMap>,
|
|
206
|
+
TMeta
|
|
207
|
+
>,
|
|
208
|
+
): EffectDecoratedProcedure<
|
|
209
|
+
MergedInitialContext<TInitialContext, UInContext, TCurrentContext>,
|
|
210
|
+
MergedCurrentContext<TCurrentContext, UOutContext>,
|
|
211
|
+
TInputSchema,
|
|
212
|
+
TOutputSchema,
|
|
213
|
+
TEffectErrorMap,
|
|
214
|
+
TMeta,
|
|
215
|
+
TRequirementsProvided,
|
|
216
|
+
TRuntimeError
|
|
217
|
+
>;
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* Uses a middleware to modify the context or improve the pipeline.
|
|
221
|
+
*
|
|
222
|
+
* @info Supports both normal middleware and inline middleware implementations.
|
|
223
|
+
* @info Pass second argument to map the input.
|
|
224
|
+
* @note The current context must be satisfy middleware dependent-context
|
|
225
|
+
* @see {@link https://orpc.dev/docs/middleware Middleware Docs}
|
|
226
|
+
*/
|
|
227
|
+
use<
|
|
228
|
+
UOutContext extends IntersectPick<TCurrentContext, UOutContext>,
|
|
229
|
+
UInput,
|
|
230
|
+
UInContext extends Context = TCurrentContext,
|
|
231
|
+
>(
|
|
232
|
+
middleware: Middleware<
|
|
233
|
+
UInContext | TCurrentContext,
|
|
234
|
+
UOutContext,
|
|
235
|
+
UInput,
|
|
236
|
+
InferSchemaInput<TOutputSchema>,
|
|
237
|
+
ORPCErrorConstructorMap<ErrorMap>,
|
|
238
|
+
TMeta
|
|
239
|
+
>,
|
|
240
|
+
mapInput: MapInputMiddleware<InferSchemaOutput<TInputSchema>, UInput>,
|
|
241
|
+
): EffectDecoratedProcedure<
|
|
242
|
+
MergedInitialContext<TInitialContext, UInContext, TCurrentContext>,
|
|
243
|
+
MergedCurrentContext<TCurrentContext, UOutContext>,
|
|
244
|
+
TInputSchema,
|
|
245
|
+
TOutputSchema,
|
|
246
|
+
TEffectErrorMap,
|
|
247
|
+
TMeta,
|
|
248
|
+
TRequirementsProvided,
|
|
249
|
+
TRuntimeError
|
|
250
|
+
>;
|
|
251
|
+
|
|
252
|
+
use(
|
|
253
|
+
middleware: AnyMiddleware,
|
|
254
|
+
mapInput?: MapInputMiddleware<any, any>,
|
|
255
|
+
): EffectDecoratedProcedure<any, any, any, any, any, any, any, any> {
|
|
256
|
+
const mapped = mapInput
|
|
257
|
+
? decorateMiddleware(middleware).mapInput(mapInput)
|
|
258
|
+
: middleware;
|
|
259
|
+
|
|
260
|
+
return new EffectDecoratedProcedure({
|
|
261
|
+
...this["~orpc"],
|
|
262
|
+
middlewares: addMiddleware(this["~orpc"].middlewares, mapped),
|
|
263
|
+
});
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
/**
|
|
267
|
+
* Make this procedure callable (works like a function while still being a procedure).
|
|
268
|
+
*
|
|
269
|
+
* @see {@link https://orpc.dev/docs/client/server-side Server-side Client Docs}
|
|
270
|
+
*/
|
|
271
|
+
callable<TClientContext extends ClientContext>(
|
|
272
|
+
...rest: MaybeOptionalOptions<
|
|
273
|
+
CreateProcedureClientOptions<
|
|
274
|
+
TInitialContext,
|
|
275
|
+
TOutputSchema,
|
|
276
|
+
ErrorMap,
|
|
277
|
+
TMeta,
|
|
278
|
+
TClientContext
|
|
279
|
+
>
|
|
280
|
+
>
|
|
281
|
+
): EffectDecoratedProcedure<
|
|
282
|
+
TInitialContext,
|
|
283
|
+
TCurrentContext,
|
|
284
|
+
TInputSchema,
|
|
285
|
+
TOutputSchema,
|
|
286
|
+
TEffectErrorMap,
|
|
287
|
+
TMeta,
|
|
288
|
+
TRequirementsProvided,
|
|
289
|
+
TRuntimeError
|
|
290
|
+
> &
|
|
291
|
+
ProcedureClient<TClientContext, TInputSchema, TOutputSchema, ErrorMap> {
|
|
292
|
+
const client: ProcedureClient<
|
|
293
|
+
TClientContext,
|
|
294
|
+
TInputSchema,
|
|
295
|
+
TOutputSchema,
|
|
296
|
+
ErrorMap
|
|
297
|
+
> = createProcedureClient(this, ...rest);
|
|
298
|
+
|
|
299
|
+
return new Proxy(client, {
|
|
300
|
+
get: (target, key) => {
|
|
301
|
+
return Reflect.has(this, key)
|
|
302
|
+
? Reflect.get(this, key)
|
|
303
|
+
: Reflect.get(target, key);
|
|
304
|
+
},
|
|
305
|
+
has: (target, key) => {
|
|
306
|
+
return Reflect.has(this, key) || Reflect.has(target, key);
|
|
307
|
+
},
|
|
308
|
+
}) as any;
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
/**
|
|
312
|
+
* Make this procedure compatible with server action.
|
|
313
|
+
*
|
|
314
|
+
* @see {@link https://orpc.dev/docs/server-action Server Action Docs}
|
|
315
|
+
*/
|
|
316
|
+
actionable(
|
|
317
|
+
...rest: MaybeOptionalOptions<
|
|
318
|
+
CreateProcedureClientOptions<
|
|
319
|
+
TInitialContext,
|
|
320
|
+
TOutputSchema,
|
|
321
|
+
ErrorMap,
|
|
322
|
+
TMeta,
|
|
323
|
+
Record<never, never>
|
|
324
|
+
>
|
|
325
|
+
>
|
|
326
|
+
): EffectDecoratedProcedure<
|
|
327
|
+
TInitialContext,
|
|
328
|
+
TCurrentContext,
|
|
329
|
+
TInputSchema,
|
|
330
|
+
TOutputSchema,
|
|
331
|
+
TEffectErrorMap,
|
|
332
|
+
TMeta,
|
|
333
|
+
TRequirementsProvided,
|
|
334
|
+
TRuntimeError
|
|
335
|
+
> &
|
|
336
|
+
ProcedureActionableClient<TInputSchema, TOutputSchema, ErrorMap> {
|
|
337
|
+
const action: ProcedureActionableClient<
|
|
338
|
+
TInputSchema,
|
|
339
|
+
TOutputSchema,
|
|
340
|
+
ErrorMap
|
|
341
|
+
> = createActionableClient(createProcedureClient(this, ...rest));
|
|
342
|
+
|
|
343
|
+
return new Proxy(action, {
|
|
344
|
+
get: (target, key) => {
|
|
345
|
+
return Reflect.has(this, key)
|
|
346
|
+
? Reflect.get(this, key)
|
|
347
|
+
: Reflect.get(target, key);
|
|
348
|
+
},
|
|
349
|
+
has: (target, key) => {
|
|
350
|
+
return Reflect.has(this, key) || Reflect.has(target, key);
|
|
351
|
+
},
|
|
352
|
+
}) as any;
|
|
353
|
+
}
|
|
354
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
export {
|
|
2
|
+
addSpanStackTrace,
|
|
3
|
+
EffectBuilder,
|
|
4
|
+
makeEffectORPC,
|
|
5
|
+
} from "./effect-builder";
|
|
6
|
+
export type {
|
|
7
|
+
AnyBuilderLike,
|
|
8
|
+
EffectBuilderDef,
|
|
9
|
+
EffectProcedureHandler,
|
|
10
|
+
EffectSpanConfig,
|
|
11
|
+
} from "./effect-builder";
|
|
12
|
+
export { EffectDecoratedProcedure } from "./effect-procedure";
|
|
13
|
+
export type { EffectProcedureDef } from "./effect-procedure";
|
|
14
|
+
export {
|
|
15
|
+
createEffectErrorConstructorMap,
|
|
16
|
+
effectErrorMapToErrorMap,
|
|
17
|
+
isORPCTaggedError,
|
|
18
|
+
isORPCTaggedErrorClass,
|
|
19
|
+
ORPCErrorSymbol,
|
|
20
|
+
ORPCTaggedError,
|
|
21
|
+
toORPCError,
|
|
22
|
+
} from "./tagged-error";
|
|
23
|
+
export type {
|
|
24
|
+
AnyORPCTaggedErrorClass,
|
|
25
|
+
EffectErrorConstructorMap,
|
|
26
|
+
EffectErrorMap,
|
|
27
|
+
EffectErrorMapItem,
|
|
28
|
+
EffectErrorMapItemToInstance,
|
|
29
|
+
EffectErrorMapToUnion,
|
|
30
|
+
InferORPCError,
|
|
31
|
+
MergedEffectErrorMap,
|
|
32
|
+
ORPCTaggedErrorClass,
|
|
33
|
+
ORPCTaggedErrorInstance,
|
|
34
|
+
ORPCTaggedErrorOptions,
|
|
35
|
+
TagToCode,
|
|
36
|
+
} from "./tagged-error";
|