effect-orpc 0.0.6 → 0.0.8
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 +176 -475
- package/dist/index.js +271 -46
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/effect-builder.ts +367 -178
- package/src/effect-enhance-router.ts +114 -0
- package/src/effect-procedure.ts +95 -32
- package/src/index.ts +20 -7
- package/src/tagged-error.ts +148 -98
- package/src/tests/effect-builder.test.ts +52 -39
- package/src/tests/effect-error-map.test.ts +32 -30
- package/src/tests/effect-procedure.test.ts +12 -12
- package/src/tests/tagged-error.test.ts +84 -27
- package/src/types/index.ts +422 -0
- package/src/types/variants.ts +1327 -0
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import type { ManagedRuntime } from "effect/ManagedRuntime";
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
enhanceRoute,
|
|
5
|
+
mergePrefix,
|
|
6
|
+
type EnhanceRouteOptions,
|
|
7
|
+
} from "@orpc/contract";
|
|
8
|
+
import {
|
|
9
|
+
createAccessibleLazyRouter,
|
|
10
|
+
getLazyMeta,
|
|
11
|
+
isLazy,
|
|
12
|
+
isProcedure,
|
|
13
|
+
lazy,
|
|
14
|
+
mergeMiddlewares,
|
|
15
|
+
unlazy,
|
|
16
|
+
type AnyMiddleware,
|
|
17
|
+
type AnyRouter,
|
|
18
|
+
type Context,
|
|
19
|
+
type Lazyable,
|
|
20
|
+
} from "@orpc/server";
|
|
21
|
+
|
|
22
|
+
import type { EffectErrorMapToErrorMap, EnhancedEffectRouter } from "./types";
|
|
23
|
+
|
|
24
|
+
import { EffectProcedure } from "./effect-procedure";
|
|
25
|
+
import { effectErrorMapToErrorMap, type EffectErrorMap } from "./tagged-error";
|
|
26
|
+
|
|
27
|
+
interface EnhanceEffectRouterOptions<
|
|
28
|
+
TEffectErrorMap extends EffectErrorMap,
|
|
29
|
+
TRequirementsProvided,
|
|
30
|
+
TRuntimeError,
|
|
31
|
+
> extends EnhanceRouteOptions {
|
|
32
|
+
middlewares: readonly AnyMiddleware[];
|
|
33
|
+
errorMap: TEffectErrorMap;
|
|
34
|
+
dedupeLeadingMiddlewares: boolean;
|
|
35
|
+
runtime: ManagedRuntime<TRequirementsProvided, TRuntimeError>;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export function enhanceEffectRouter<
|
|
39
|
+
T extends Lazyable<AnyRouter>,
|
|
40
|
+
TInitialContext extends Context,
|
|
41
|
+
TCurrentContext extends Context,
|
|
42
|
+
TEffectErrorMap extends EffectErrorMap,
|
|
43
|
+
TRequirementsProvided,
|
|
44
|
+
TRuntimeError,
|
|
45
|
+
>(
|
|
46
|
+
router: T,
|
|
47
|
+
options: EnhanceEffectRouterOptions<
|
|
48
|
+
TEffectErrorMap,
|
|
49
|
+
TRequirementsProvided,
|
|
50
|
+
TRuntimeError
|
|
51
|
+
>,
|
|
52
|
+
): EnhancedEffectRouter<T, TInitialContext, TCurrentContext, TEffectErrorMap> {
|
|
53
|
+
if (isLazy(router)) {
|
|
54
|
+
const laziedMeta = getLazyMeta(router);
|
|
55
|
+
const enhancedPrefix = laziedMeta?.prefix
|
|
56
|
+
? mergePrefix(options.prefix, laziedMeta?.prefix)
|
|
57
|
+
: options.prefix;
|
|
58
|
+
|
|
59
|
+
const enhanced = lazy(
|
|
60
|
+
async () => {
|
|
61
|
+
const { default: unlaziedRouter } = await unlazy(router);
|
|
62
|
+
const enhanced = enhanceEffectRouter(unlaziedRouter, options);
|
|
63
|
+
return unlazy(enhanced);
|
|
64
|
+
},
|
|
65
|
+
{
|
|
66
|
+
...laziedMeta,
|
|
67
|
+
prefix: enhancedPrefix,
|
|
68
|
+
},
|
|
69
|
+
);
|
|
70
|
+
|
|
71
|
+
const accessible = createAccessibleLazyRouter(enhanced);
|
|
72
|
+
|
|
73
|
+
return accessible as any;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
if (isProcedure(router)) {
|
|
77
|
+
const newMiddlewares = mergeMiddlewares(
|
|
78
|
+
options.middlewares,
|
|
79
|
+
router["~orpc"].middlewares,
|
|
80
|
+
{ dedupeLeading: options.dedupeLeadingMiddlewares },
|
|
81
|
+
);
|
|
82
|
+
const newMiddlewareAdded =
|
|
83
|
+
newMiddlewares.length - router["~orpc"].middlewares.length;
|
|
84
|
+
|
|
85
|
+
const effectErrorMap = {
|
|
86
|
+
...options.errorMap,
|
|
87
|
+
...router["~orpc"].errorMap,
|
|
88
|
+
};
|
|
89
|
+
const errorMap: EffectErrorMapToErrorMap<typeof effectErrorMap> =
|
|
90
|
+
effectErrorMapToErrorMap(effectErrorMap);
|
|
91
|
+
const enhanced = new EffectProcedure({
|
|
92
|
+
...router["~orpc"],
|
|
93
|
+
route: enhanceRoute(router["~orpc"].route, options),
|
|
94
|
+
effectErrorMap,
|
|
95
|
+
errorMap: errorMap as EffectErrorMapToErrorMap<typeof effectErrorMap>,
|
|
96
|
+
middlewares: newMiddlewares,
|
|
97
|
+
inputValidationIndex:
|
|
98
|
+
router["~orpc"].inputValidationIndex + newMiddlewareAdded,
|
|
99
|
+
outputValidationIndex:
|
|
100
|
+
router["~orpc"].outputValidationIndex + newMiddlewareAdded,
|
|
101
|
+
runtime: options.runtime,
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
return enhanced as any;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const enhanced = {} as Record<string, any>;
|
|
108
|
+
|
|
109
|
+
for (const key in router) {
|
|
110
|
+
enhanced[key] = enhanceEffectRouter(router[key]!, options);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
return enhanced as any;
|
|
114
|
+
}
|
package/src/effect-procedure.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import type { ClientContext } from "@orpc/client";
|
|
2
2
|
import type {
|
|
3
3
|
AnySchema,
|
|
4
|
-
ErrorMap,
|
|
5
4
|
InferSchemaInput,
|
|
6
5
|
InferSchemaOutput,
|
|
7
6
|
Meta,
|
|
@@ -15,13 +14,11 @@ import type {
|
|
|
15
14
|
MergedCurrentContext,
|
|
16
15
|
MergedInitialContext,
|
|
17
16
|
Middleware,
|
|
18
|
-
ORPCErrorConstructorMap,
|
|
19
17
|
ProcedureActionableClient,
|
|
20
18
|
ProcedureClient,
|
|
21
19
|
ProcedureDef,
|
|
22
20
|
} from "@orpc/server";
|
|
23
21
|
import type { IntersectPick, MaybeOptionalOptions } from "@orpc/shared";
|
|
24
|
-
import type { ManagedRuntime } from "effect";
|
|
25
22
|
|
|
26
23
|
import { mergeMeta, mergeRoute } from "@orpc/contract";
|
|
27
24
|
import {
|
|
@@ -32,14 +29,16 @@ import {
|
|
|
32
29
|
Procedure,
|
|
33
30
|
} from "@orpc/server";
|
|
34
31
|
|
|
35
|
-
import type {
|
|
32
|
+
import type {
|
|
33
|
+
EffectErrorConstructorMap,
|
|
34
|
+
EffectErrorMap,
|
|
35
|
+
MergedEffectErrorMap,
|
|
36
|
+
} from "./tagged-error";
|
|
37
|
+
import type { EffectErrorMapToErrorMap, EffectProcedureDef } from "./types";
|
|
36
38
|
|
|
37
39
|
import { effectErrorMapToErrorMap } from "./tagged-error";
|
|
38
40
|
|
|
39
|
-
|
|
40
|
-
* Extended procedure definition that includes the Effect ManagedRuntime.
|
|
41
|
-
*/
|
|
42
|
-
export interface EffectProcedureDef<
|
|
41
|
+
export class EffectProcedure<
|
|
43
42
|
TInitialContext extends Context,
|
|
44
43
|
TCurrentContext extends Context,
|
|
45
44
|
TInputSchema extends AnySchema,
|
|
@@ -48,16 +47,54 @@ export interface EffectProcedureDef<
|
|
|
48
47
|
TMeta extends Meta,
|
|
49
48
|
TRequirementsProvided,
|
|
50
49
|
TRuntimeError,
|
|
51
|
-
> extends
|
|
50
|
+
> extends Procedure<
|
|
52
51
|
TInitialContext,
|
|
53
52
|
TCurrentContext,
|
|
54
53
|
TInputSchema,
|
|
55
54
|
TOutputSchema,
|
|
56
|
-
|
|
55
|
+
EffectErrorMapToErrorMap<TEffectErrorMap>,
|
|
57
56
|
TMeta
|
|
58
57
|
> {
|
|
59
|
-
|
|
60
|
-
|
|
58
|
+
/**
|
|
59
|
+
* This property holds the defined options and the effect-specific properties.
|
|
60
|
+
*/
|
|
61
|
+
declare "~effect": EffectProcedureDef<
|
|
62
|
+
TInitialContext,
|
|
63
|
+
TCurrentContext,
|
|
64
|
+
TInputSchema,
|
|
65
|
+
TOutputSchema,
|
|
66
|
+
TEffectErrorMap,
|
|
67
|
+
TMeta,
|
|
68
|
+
TRequirementsProvided,
|
|
69
|
+
TRuntimeError
|
|
70
|
+
>;
|
|
71
|
+
/**
|
|
72
|
+
* This property holds the defined options.
|
|
73
|
+
*/
|
|
74
|
+
declare "~orpc": ProcedureDef<
|
|
75
|
+
TInitialContext,
|
|
76
|
+
TCurrentContext,
|
|
77
|
+
TInputSchema,
|
|
78
|
+
TOutputSchema,
|
|
79
|
+
EffectErrorMapToErrorMap<TEffectErrorMap>,
|
|
80
|
+
TMeta
|
|
81
|
+
>;
|
|
82
|
+
|
|
83
|
+
constructor(
|
|
84
|
+
def: EffectProcedureDef<
|
|
85
|
+
TInitialContext,
|
|
86
|
+
TCurrentContext,
|
|
87
|
+
TInputSchema,
|
|
88
|
+
TOutputSchema,
|
|
89
|
+
TEffectErrorMap,
|
|
90
|
+
TMeta,
|
|
91
|
+
TRequirementsProvided,
|
|
92
|
+
TRuntimeError
|
|
93
|
+
>,
|
|
94
|
+
) {
|
|
95
|
+
super(def);
|
|
96
|
+
this["~effect"] = def;
|
|
97
|
+
}
|
|
61
98
|
}
|
|
62
99
|
|
|
63
100
|
/**
|
|
@@ -75,15 +112,20 @@ export class EffectDecoratedProcedure<
|
|
|
75
112
|
TMeta extends Meta,
|
|
76
113
|
TRequirementsProvided,
|
|
77
114
|
TRuntimeError,
|
|
78
|
-
> extends
|
|
115
|
+
> extends EffectProcedure<
|
|
79
116
|
TInitialContext,
|
|
80
117
|
TCurrentContext,
|
|
81
118
|
TInputSchema,
|
|
82
119
|
TOutputSchema,
|
|
83
|
-
|
|
84
|
-
TMeta
|
|
120
|
+
TEffectErrorMap,
|
|
121
|
+
TMeta,
|
|
122
|
+
TRequirementsProvided,
|
|
123
|
+
TRuntimeError
|
|
85
124
|
> {
|
|
86
|
-
|
|
125
|
+
/**
|
|
126
|
+
* This property holds the defined options and the effect-specific properties.
|
|
127
|
+
*/
|
|
128
|
+
declare "~effect": EffectProcedureDef<
|
|
87
129
|
TInitialContext,
|
|
88
130
|
TCurrentContext,
|
|
89
131
|
TInputSchema,
|
|
@@ -93,6 +135,14 @@ export class EffectDecoratedProcedure<
|
|
|
93
135
|
TRequirementsProvided,
|
|
94
136
|
TRuntimeError
|
|
95
137
|
>;
|
|
138
|
+
declare "~orpc": ProcedureDef<
|
|
139
|
+
TInitialContext,
|
|
140
|
+
TCurrentContext,
|
|
141
|
+
TInputSchema,
|
|
142
|
+
TOutputSchema,
|
|
143
|
+
EffectErrorMapToErrorMap<TEffectErrorMap>,
|
|
144
|
+
TMeta
|
|
145
|
+
>;
|
|
96
146
|
|
|
97
147
|
constructor(
|
|
98
148
|
def: EffectProcedureDef<
|
|
@@ -107,6 +157,7 @@ export class EffectDecoratedProcedure<
|
|
|
107
157
|
>,
|
|
108
158
|
) {
|
|
109
159
|
super(def);
|
|
160
|
+
this["~effect"] = def;
|
|
110
161
|
}
|
|
111
162
|
|
|
112
163
|
/**
|
|
@@ -127,9 +178,12 @@ export class EffectDecoratedProcedure<
|
|
|
127
178
|
TRequirementsProvided,
|
|
128
179
|
TRuntimeError
|
|
129
180
|
> {
|
|
130
|
-
const newEffectErrorMap = {
|
|
181
|
+
const newEffectErrorMap: MergedEffectErrorMap<TEffectErrorMap, U> = {
|
|
182
|
+
...this["~effect"].effectErrorMap,
|
|
183
|
+
...errors,
|
|
184
|
+
};
|
|
131
185
|
return new EffectDecoratedProcedure({
|
|
132
|
-
...this["~
|
|
186
|
+
...this["~effect"],
|
|
133
187
|
effectErrorMap: newEffectErrorMap,
|
|
134
188
|
errorMap: effectErrorMapToErrorMap(newEffectErrorMap),
|
|
135
189
|
});
|
|
@@ -154,8 +208,8 @@ export class EffectDecoratedProcedure<
|
|
|
154
208
|
TRuntimeError
|
|
155
209
|
> {
|
|
156
210
|
return new EffectDecoratedProcedure({
|
|
157
|
-
...this["~
|
|
158
|
-
meta: mergeMeta(this["~
|
|
211
|
+
...this["~effect"],
|
|
212
|
+
meta: mergeMeta(this["~effect"].meta, meta),
|
|
159
213
|
});
|
|
160
214
|
}
|
|
161
215
|
|
|
@@ -180,8 +234,8 @@ export class EffectDecoratedProcedure<
|
|
|
180
234
|
TRuntimeError
|
|
181
235
|
> {
|
|
182
236
|
return new EffectDecoratedProcedure({
|
|
183
|
-
...this["~
|
|
184
|
-
route: mergeRoute(this["~
|
|
237
|
+
...this["~effect"],
|
|
238
|
+
route: mergeRoute(this["~effect"].route, route),
|
|
185
239
|
});
|
|
186
240
|
}
|
|
187
241
|
|
|
@@ -202,7 +256,7 @@ export class EffectDecoratedProcedure<
|
|
|
202
256
|
UOutContext,
|
|
203
257
|
InferSchemaOutput<TInputSchema>,
|
|
204
258
|
InferSchemaInput<TOutputSchema>,
|
|
205
|
-
|
|
259
|
+
EffectErrorConstructorMap<TEffectErrorMap>,
|
|
206
260
|
TMeta
|
|
207
261
|
>,
|
|
208
262
|
): EffectDecoratedProcedure<
|
|
@@ -234,7 +288,7 @@ export class EffectDecoratedProcedure<
|
|
|
234
288
|
UOutContext,
|
|
235
289
|
UInput,
|
|
236
290
|
InferSchemaInput<TOutputSchema>,
|
|
237
|
-
|
|
291
|
+
EffectErrorConstructorMap<TEffectErrorMap>,
|
|
238
292
|
TMeta
|
|
239
293
|
>,
|
|
240
294
|
mapInput: MapInputMiddleware<InferSchemaOutput<TInputSchema>, UInput>,
|
|
@@ -258,8 +312,8 @@ export class EffectDecoratedProcedure<
|
|
|
258
312
|
: middleware;
|
|
259
313
|
|
|
260
314
|
return new EffectDecoratedProcedure({
|
|
261
|
-
...this["~
|
|
262
|
-
middlewares: addMiddleware(this["~
|
|
315
|
+
...this["~effect"],
|
|
316
|
+
middlewares: addMiddleware(this["~effect"].middlewares, mapped),
|
|
263
317
|
});
|
|
264
318
|
}
|
|
265
319
|
|
|
@@ -273,7 +327,7 @@ export class EffectDecoratedProcedure<
|
|
|
273
327
|
CreateProcedureClientOptions<
|
|
274
328
|
TInitialContext,
|
|
275
329
|
TOutputSchema,
|
|
276
|
-
|
|
330
|
+
EffectErrorMapToErrorMap<TEffectErrorMap>,
|
|
277
331
|
TMeta,
|
|
278
332
|
TClientContext
|
|
279
333
|
>
|
|
@@ -288,12 +342,17 @@ export class EffectDecoratedProcedure<
|
|
|
288
342
|
TRequirementsProvided,
|
|
289
343
|
TRuntimeError
|
|
290
344
|
> &
|
|
291
|
-
ProcedureClient<
|
|
345
|
+
ProcedureClient<
|
|
346
|
+
TClientContext,
|
|
347
|
+
TInputSchema,
|
|
348
|
+
TOutputSchema,
|
|
349
|
+
EffectErrorMapToErrorMap<TEffectErrorMap>
|
|
350
|
+
> {
|
|
292
351
|
const client: ProcedureClient<
|
|
293
352
|
TClientContext,
|
|
294
353
|
TInputSchema,
|
|
295
354
|
TOutputSchema,
|
|
296
|
-
|
|
355
|
+
EffectErrorMapToErrorMap<TEffectErrorMap>
|
|
297
356
|
> = createProcedureClient(this, ...rest);
|
|
298
357
|
|
|
299
358
|
return new Proxy(client, {
|
|
@@ -318,7 +377,7 @@ export class EffectDecoratedProcedure<
|
|
|
318
377
|
CreateProcedureClientOptions<
|
|
319
378
|
TInitialContext,
|
|
320
379
|
TOutputSchema,
|
|
321
|
-
|
|
380
|
+
EffectErrorMapToErrorMap<TEffectErrorMap>,
|
|
322
381
|
TMeta,
|
|
323
382
|
Record<never, never>
|
|
324
383
|
>
|
|
@@ -333,11 +392,15 @@ export class EffectDecoratedProcedure<
|
|
|
333
392
|
TRequirementsProvided,
|
|
334
393
|
TRuntimeError
|
|
335
394
|
> &
|
|
336
|
-
ProcedureActionableClient<
|
|
395
|
+
ProcedureActionableClient<
|
|
396
|
+
TInputSchema,
|
|
397
|
+
TOutputSchema,
|
|
398
|
+
EffectErrorMapToErrorMap<TEffectErrorMap>
|
|
399
|
+
> {
|
|
337
400
|
const action: ProcedureActionableClient<
|
|
338
401
|
TInputSchema,
|
|
339
402
|
TOutputSchema,
|
|
340
|
-
|
|
403
|
+
EffectErrorMapToErrorMap<TEffectErrorMap>
|
|
341
404
|
> = createActionableClient(createProcedureClient(this, ...rest));
|
|
342
405
|
|
|
343
406
|
return new Proxy(action, {
|
package/src/index.ts
CHANGED
|
@@ -3,14 +3,7 @@ export {
|
|
|
3
3
|
EffectBuilder,
|
|
4
4
|
makeEffectORPC,
|
|
5
5
|
} from "./effect-builder";
|
|
6
|
-
export type {
|
|
7
|
-
AnyBuilderLike,
|
|
8
|
-
EffectBuilderDef,
|
|
9
|
-
EffectProcedureHandler,
|
|
10
|
-
EffectSpanConfig,
|
|
11
|
-
} from "./effect-builder";
|
|
12
6
|
export { EffectDecoratedProcedure } from "./effect-procedure";
|
|
13
|
-
export type { EffectProcedureDef } from "./effect-procedure";
|
|
14
7
|
export {
|
|
15
8
|
createEffectErrorConstructorMap,
|
|
16
9
|
effectErrorMapToErrorMap,
|
|
@@ -34,3 +27,23 @@ export type {
|
|
|
34
27
|
ORPCTaggedErrorOptions,
|
|
35
28
|
TagToCode,
|
|
36
29
|
} from "./tagged-error";
|
|
30
|
+
export type {
|
|
31
|
+
AnyBuilderLike,
|
|
32
|
+
EffectBuilderDef,
|
|
33
|
+
EffectBuilderWithMiddlewares,
|
|
34
|
+
EffectErrorMapToErrorMap,
|
|
35
|
+
EffectProcedureBuilder,
|
|
36
|
+
EffectProcedureBuilderWithInput,
|
|
37
|
+
EffectProcedureBuilderWithInputOutput,
|
|
38
|
+
EffectProcedureBuilderWithOutput,
|
|
39
|
+
EffectProcedureDef,
|
|
40
|
+
EffectProcedureHandler,
|
|
41
|
+
EffectRouterBuilder,
|
|
42
|
+
EffectSpanConfig,
|
|
43
|
+
InferBuilderInitialContext,
|
|
44
|
+
InferBuilderCurrentContext,
|
|
45
|
+
InferBuilderInputSchema,
|
|
46
|
+
InferBuilderOutputSchema,
|
|
47
|
+
InferBuilderErrorMap,
|
|
48
|
+
InferBuilderMeta,
|
|
49
|
+
} from "./types";
|