@orpc/server 0.0.0-next.3f6c426 → 0.0.0-next.43c0c87

Sign up to get free protection for your applications and to get access to all the features.
Files changed (48) hide show
  1. package/dist/{chunk-6A7XHEBH.js → chunk-GK2Z6B6W.js} +85 -44
  2. package/dist/{chunk-B2EZJB7X.js → chunk-SXUFCJBY.js} +18 -20
  3. package/dist/chunk-WUOGVGWG.js +1 -0
  4. package/dist/fetch.js +5 -22
  5. package/dist/hono.js +30 -0
  6. package/dist/index.js +630 -165
  7. package/dist/next.js +36 -0
  8. package/dist/node.js +69 -27
  9. package/dist/src/adapters/fetch/index.d.ts +0 -1
  10. package/dist/src/adapters/fetch/orpc-handler.d.ts +9 -9
  11. package/dist/src/adapters/fetch/orpc-payload-codec.d.ts +1 -1
  12. package/dist/src/adapters/fetch/types.d.ts +11 -6
  13. package/dist/src/adapters/hono/index.d.ts +3 -0
  14. package/dist/src/adapters/hono/middleware.d.ts +12 -0
  15. package/dist/src/adapters/next/index.d.ts +3 -0
  16. package/dist/src/adapters/next/serve.d.ts +19 -0
  17. package/dist/src/adapters/node/index.d.ts +1 -1
  18. package/dist/src/adapters/node/orpc-handler.d.ts +7 -7
  19. package/dist/src/adapters/node/request-listener.d.ts +28 -0
  20. package/dist/src/adapters/node/types.d.ts +10 -9
  21. package/dist/src/builder-with-errors-middlewares.d.ts +50 -0
  22. package/dist/src/builder-with-errors.d.ts +49 -0
  23. package/dist/src/builder-with-middlewares.d.ts +47 -0
  24. package/dist/src/builder.d.ts +30 -22
  25. package/dist/src/context.d.ts +11 -0
  26. package/dist/src/error.d.ts +10 -0
  27. package/dist/src/hidden.d.ts +2 -2
  28. package/dist/src/implementer-chainable.d.ts +7 -3
  29. package/dist/src/index.d.ts +4 -3
  30. package/dist/src/lazy-decorated.d.ts +3 -6
  31. package/dist/src/middleware-decorated.d.ts +7 -5
  32. package/dist/src/middleware.d.ts +21 -8
  33. package/dist/src/procedure-builder-with-input.d.ts +34 -0
  34. package/dist/src/procedure-builder-with-output.d.ts +33 -0
  35. package/dist/src/procedure-builder.d.ts +22 -17
  36. package/dist/src/procedure-client.d.ts +8 -20
  37. package/dist/src/procedure-decorated.d.ts +22 -10
  38. package/dist/src/procedure-implementer.d.ts +16 -12
  39. package/dist/src/procedure-utils.d.ts +17 -0
  40. package/dist/src/procedure.d.ts +35 -13
  41. package/dist/src/router-builder.d.ts +19 -15
  42. package/dist/src/router-client.d.ts +7 -6
  43. package/dist/src/router-implementer.d.ts +7 -6
  44. package/dist/src/router.d.ts +5 -5
  45. package/dist/src/types.d.ts +2 -0
  46. package/package.json +18 -8
  47. package/dist/src/adapters/fetch/composite-handler.d.ts +0 -8
  48. package/dist/src/adapters/node/composite-handler.d.ts +0 -9
package/dist/index.js CHANGED
@@ -1,6 +1,7 @@
1
1
  import {
2
2
  LAZY_LOADER_SYMBOL,
3
3
  Procedure,
4
+ createORPCErrorConstructorMap,
4
5
  createProcedureClient,
5
6
  flatLazy,
6
7
  getRouterChild,
@@ -8,32 +9,41 @@ import {
8
9
  isProcedure,
9
10
  lazy,
10
11
  mergeContext,
12
+ middlewareOutputFn,
11
13
  unlazy
12
- } from "./chunk-6A7XHEBH.js";
14
+ } from "./chunk-GK2Z6B6W.js";
13
15
 
14
16
  // src/builder.ts
17
+ import { ContractProcedure as ContractProcedure4 } from "@orpc/contract";
18
+
19
+ // src/builder-with-errors.ts
20
+ import { ContractProcedure as ContractProcedure2 } from "@orpc/contract";
21
+
22
+ // src/builder-with-errors-middlewares.ts
15
23
  import { ContractProcedure } from "@orpc/contract";
16
24
 
17
- // src/implementer-chainable.ts
18
- import { isContractProcedure } from "@orpc/contract";
19
- import { createCallableObject } from "@orpc/shared";
25
+ // src/procedure-builder.ts
26
+ import { ContractProcedureBuilder, DecoratedContractProcedure as DecoratedContractProcedure4 } from "@orpc/contract";
27
+
28
+ // src/procedure-builder-with-input.ts
29
+ import { ContractProcedureBuilderWithInput, DecoratedContractProcedure as DecoratedContractProcedure2 } from "@orpc/contract";
20
30
 
21
31
  // src/middleware-decorated.ts
22
32
  function decorateMiddleware(middleware) {
23
33
  const decorated = middleware;
24
34
  decorated.mapInput = (mapInput) => {
25
35
  const mapped = decorateMiddleware(
26
- (input, ...rest) => middleware(mapInput(input), ...rest)
36
+ (options, input, ...rest) => middleware(options, mapInput(input), ...rest)
27
37
  );
28
38
  return mapped;
29
39
  };
30
40
  decorated.concat = (concatMiddleware, mapInput) => {
31
41
  const mapped = mapInput ? decorateMiddleware(concatMiddleware).mapInput(mapInput) : concatMiddleware;
32
- const concatted = decorateMiddleware((input, context, meta, ...rest) => {
33
- const next = async (options) => {
34
- return mapped(input, mergeContext(context, options.context), meta, ...rest);
42
+ const concatted = decorateMiddleware((options, input, output, ...rest) => {
43
+ const next = async (nextOptions) => {
44
+ return mapped({ ...options, context: mergeContext(nextOptions.context, options.context) }, input, output, ...rest);
35
45
  };
36
- const merged = middleware(input, context, { ...meta, next }, ...rest);
46
+ const merged = middleware({ ...options, next }, input, output, ...rest);
37
47
  return merged;
38
48
  });
39
49
  return concatted;
@@ -43,58 +53,81 @@ function decorateMiddleware(middleware) {
43
53
 
44
54
  // src/procedure-decorated.ts
45
55
  import { DecoratedContractProcedure } from "@orpc/contract";
46
- function decorateProcedure(procedure) {
47
- const caller = createProcedureClient({
48
- procedure,
49
- context: void 0
50
- });
51
- const decorated = caller;
52
- decorated["~type"] = procedure["~type"];
53
- decorated["~orpc"] = procedure["~orpc"];
54
- decorated.prefix = (prefix) => {
55
- return decorateProcedure(new Procedure({
56
- ...procedure["~orpc"],
57
- contract: DecoratedContractProcedure.decorate(procedure["~orpc"].contract).prefix(prefix)
58
- }));
59
- };
60
- decorated.route = (route) => {
61
- return decorateProcedure(new Procedure({
62
- ...procedure["~orpc"],
63
- contract: DecoratedContractProcedure.decorate(procedure["~orpc"].contract).route(route)
64
- }));
65
- };
66
- decorated.use = (middleware, mapInput) => {
56
+ var DecoratedProcedure = class _DecoratedProcedure extends Procedure {
57
+ static decorate(procedure) {
58
+ if (procedure instanceof _DecoratedProcedure) {
59
+ return procedure;
60
+ }
61
+ return new _DecoratedProcedure(procedure["~orpc"]);
62
+ }
63
+ prefix(prefix) {
64
+ return new _DecoratedProcedure({
65
+ ...this["~orpc"],
66
+ contract: DecoratedContractProcedure.decorate(this["~orpc"].contract).prefix(prefix)
67
+ });
68
+ }
69
+ route(route) {
70
+ return new _DecoratedProcedure({
71
+ ...this["~orpc"],
72
+ contract: DecoratedContractProcedure.decorate(this["~orpc"].contract).route(route)
73
+ });
74
+ }
75
+ errors(errors) {
76
+ return new _DecoratedProcedure({
77
+ ...this["~orpc"],
78
+ contract: DecoratedContractProcedure.decorate(this["~orpc"].contract).errors(errors)
79
+ });
80
+ }
81
+ use(middleware, mapInput) {
67
82
  const middleware_ = mapInput ? decorateMiddleware(middleware).mapInput(mapInput) : middleware;
68
- return decorateProcedure(new Procedure({
69
- ...procedure["~orpc"],
70
- middlewares: [...procedure["~orpc"].middlewares ?? [], middleware_]
71
- }));
72
- };
73
- decorated.unshiftTag = (...tags) => {
74
- return decorateProcedure(new Procedure({
75
- ...procedure["~orpc"],
76
- contract: DecoratedContractProcedure.decorate(procedure["~orpc"].contract).unshiftTag(...tags)
77
- }));
78
- };
79
- decorated.unshiftMiddleware = (...middlewares) => {
80
- if (procedure["~orpc"].middlewares?.length) {
83
+ return new _DecoratedProcedure({
84
+ ...this["~orpc"],
85
+ middlewares: [...this["~orpc"].middlewares, middleware_]
86
+ });
87
+ }
88
+ unshiftTag(...tags) {
89
+ return new _DecoratedProcedure({
90
+ ...this["~orpc"],
91
+ contract: DecoratedContractProcedure.decorate(this["~orpc"].contract).unshiftTag(...tags)
92
+ });
93
+ }
94
+ unshiftMiddleware(...middlewares) {
95
+ const castedMiddlewares = middlewares;
96
+ if (this["~orpc"].middlewares.length) {
81
97
  let min = 0;
82
- for (let i = 0; i < procedure["~orpc"].middlewares.length; i++) {
83
- const index = middlewares.indexOf(procedure["~orpc"].middlewares[i], min);
98
+ for (let i = 0; i < this["~orpc"].middlewares.length; i++) {
99
+ const index = castedMiddlewares.indexOf(this["~orpc"].middlewares[i], min);
84
100
  if (index === -1) {
85
- middlewares.push(...procedure["~orpc"].middlewares.slice(i));
101
+ castedMiddlewares.push(...this["~orpc"].middlewares.slice(i));
86
102
  break;
87
103
  }
88
104
  min = index + 1;
89
105
  }
90
106
  }
91
- return decorateProcedure(new Procedure({
92
- ...procedure["~orpc"],
93
- middlewares
94
- }));
95
- };
96
- return decorated;
97
- }
107
+ const numNewMiddlewares = castedMiddlewares.length - this["~orpc"].middlewares.length;
108
+ return new _DecoratedProcedure({
109
+ ...this["~orpc"],
110
+ inputValidationIndex: this["~orpc"].inputValidationIndex + numNewMiddlewares,
111
+ outputValidationIndex: this["~orpc"].outputValidationIndex + numNewMiddlewares,
112
+ middlewares: castedMiddlewares
113
+ });
114
+ }
115
+ /**
116
+ * Make this procedure callable (works like a function while still being a procedure).
117
+ */
118
+ callable(...rest) {
119
+ return Object.assign(createProcedureClient(this, ...rest), {
120
+ "~type": "Procedure",
121
+ "~orpc": this["~orpc"]
122
+ });
123
+ }
124
+ /**
125
+ * Make this procedure compatible with server action (the same as .callable, but the type is compatible with server action).
126
+ */
127
+ actionable(...rest) {
128
+ return this.callable(...rest);
129
+ }
130
+ };
98
131
 
99
132
  // src/procedure-implementer.ts
100
133
  var ProcedureImplementer = class _ProcedureImplementer {
@@ -107,15 +140,143 @@ var ProcedureImplementer = class _ProcedureImplementer {
107
140
  const mappedMiddleware = mapInput ? decorateMiddleware(middleware).mapInput(mapInput) : middleware;
108
141
  return new _ProcedureImplementer({
109
142
  ...this["~orpc"],
110
- middlewares: [...this["~orpc"].middlewares ?? [], mappedMiddleware]
143
+ middlewares: [...this["~orpc"].middlewares, mappedMiddleware]
111
144
  });
112
145
  }
113
146
  handler(handler) {
114
- return decorateProcedure(new Procedure({
115
- middlewares: this["~orpc"].middlewares,
116
- contract: this["~orpc"].contract,
147
+ return new DecoratedProcedure({
148
+ ...this["~orpc"],
117
149
  handler
118
- }));
150
+ });
151
+ }
152
+ };
153
+
154
+ // src/procedure-builder-with-input.ts
155
+ var ProcedureBuilderWithInput = class _ProcedureBuilderWithInput {
156
+ "~type" = "ProcedureBuilderWithInput";
157
+ "~orpc";
158
+ constructor(def) {
159
+ this["~orpc"] = def;
160
+ }
161
+ errors(errors) {
162
+ return new _ProcedureBuilderWithInput({
163
+ ...this["~orpc"],
164
+ contract: DecoratedContractProcedure2.decorate(this["~orpc"].contract).errors(errors)
165
+ });
166
+ }
167
+ route(route) {
168
+ return new _ProcedureBuilderWithInput({
169
+ ...this["~orpc"],
170
+ contract: DecoratedContractProcedure2.decorate(this["~orpc"].contract).route(route)
171
+ });
172
+ }
173
+ use(middleware, mapInput) {
174
+ const maybeWithMapInput = mapInput ? decorateMiddleware(middleware).mapInput(mapInput) : middleware;
175
+ return new _ProcedureBuilderWithInput({
176
+ ...this["~orpc"],
177
+ outputValidationIndex: this["~orpc"].outputValidationIndex + 1,
178
+ middlewares: [...this["~orpc"].middlewares, maybeWithMapInput]
179
+ });
180
+ }
181
+ output(schema, example) {
182
+ return new ProcedureImplementer({
183
+ ...this["~orpc"],
184
+ contract: new ContractProcedureBuilderWithInput(this["~orpc"].contract["~orpc"]).output(schema, example)
185
+ });
186
+ }
187
+ handler(handler) {
188
+ return new DecoratedProcedure({
189
+ ...this["~orpc"],
190
+ handler
191
+ });
192
+ }
193
+ };
194
+
195
+ // src/procedure-builder-with-output.ts
196
+ import { ContractProcedureBuilderWithOutput, DecoratedContractProcedure as DecoratedContractProcedure3 } from "@orpc/contract";
197
+ var ProcedureBuilderWithOutput = class _ProcedureBuilderWithOutput {
198
+ "~type" = "ProcedureBuilderWithOutput";
199
+ "~orpc";
200
+ constructor(def) {
201
+ this["~orpc"] = def;
202
+ }
203
+ errors(errors) {
204
+ return new _ProcedureBuilderWithOutput({
205
+ ...this["~orpc"],
206
+ contract: DecoratedContractProcedure3.decorate(this["~orpc"].contract).errors(errors)
207
+ });
208
+ }
209
+ route(route) {
210
+ return new _ProcedureBuilderWithOutput({
211
+ ...this["~orpc"],
212
+ contract: DecoratedContractProcedure3.decorate(this["~orpc"].contract).route(route)
213
+ });
214
+ }
215
+ use(middleware) {
216
+ return new _ProcedureBuilderWithOutput({
217
+ ...this["~orpc"],
218
+ inputValidationIndex: this["~orpc"].inputValidationIndex + 1,
219
+ middlewares: [...this["~orpc"].middlewares, middleware]
220
+ });
221
+ }
222
+ input(schema, example) {
223
+ return new ProcedureImplementer({
224
+ ...this["~orpc"],
225
+ contract: new ContractProcedureBuilderWithOutput(this["~orpc"].contract["~orpc"]).input(schema, example)
226
+ });
227
+ }
228
+ handler(handler) {
229
+ return new DecoratedProcedure({
230
+ ...this["~orpc"],
231
+ handler
232
+ });
233
+ }
234
+ };
235
+
236
+ // src/procedure-builder.ts
237
+ var ProcedureBuilder = class _ProcedureBuilder {
238
+ "~type" = "ProcedureBuilder";
239
+ "~orpc";
240
+ constructor(def) {
241
+ this["~orpc"] = def;
242
+ }
243
+ errors(errors) {
244
+ return new _ProcedureBuilder({
245
+ ...this["~orpc"],
246
+ contract: DecoratedContractProcedure4.decorate(this["~orpc"].contract).errors(errors)
247
+ });
248
+ }
249
+ route(route) {
250
+ return new _ProcedureBuilder({
251
+ ...this["~orpc"],
252
+ contract: DecoratedContractProcedure4.decorate(this["~orpc"].contract).route(route)
253
+ });
254
+ }
255
+ use(middleware) {
256
+ return new _ProcedureBuilder({
257
+ ...this["~orpc"],
258
+ inputValidationIndex: this["~orpc"].inputValidationIndex + 1,
259
+ outputValidationIndex: this["~orpc"].outputValidationIndex + 1,
260
+ middlewares: [...this["~orpc"].middlewares, middleware]
261
+ });
262
+ }
263
+ input(schema, example) {
264
+ return new ProcedureBuilderWithInput({
265
+ ...this["~orpc"],
266
+ contract: new ContractProcedureBuilder(this["~orpc"].contract["~orpc"]).input(schema, example)
267
+ });
268
+ }
269
+ output(schema, example) {
270
+ return new ProcedureBuilderWithOutput({
271
+ ...this["~orpc"],
272
+ contract: new ContractProcedureBuilder(this["~orpc"].contract["~orpc"]).output(schema, example)
273
+ });
274
+ }
275
+ handler(handler) {
276
+ return new DecoratedProcedure({
277
+ ...this["~orpc"],
278
+ handler
279
+ });
119
280
  }
120
281
  };
121
282
 
@@ -153,31 +314,10 @@ function getLazyRouterPrefix(obj) {
153
314
  return obj[LAZY_ROUTER_PREFIX_SYMBOL];
154
315
  }
155
316
 
156
- // src/lazy-utils.ts
157
- function createLazyProcedureFormAnyLazy(lazied) {
158
- const lazyProcedure = lazy(async () => {
159
- const { default: maybeProcedure } = await unlazy(flatLazy(lazied));
160
- if (!isProcedure(maybeProcedure)) {
161
- throw new Error(`
162
- Expected a lazy<procedure> but got lazy<unknown>.
163
- This should be caught by TypeScript compilation.
164
- Please report this issue if this makes you feel uncomfortable.
165
- `);
166
- }
167
- return { default: maybeProcedure };
168
- });
169
- return lazyProcedure;
170
- }
171
-
172
317
  // src/lazy-decorated.ts
173
318
  function decorateLazy(lazied) {
174
319
  const flattenLazy = flatLazy(lazied);
175
- const procedureProcedureClient = createProcedureClient({
176
- procedure: createLazyProcedureFormAnyLazy(flattenLazy),
177
- context: void 0
178
- });
179
- Object.assign(procedureProcedureClient, flattenLazy);
180
- const recursive = new Proxy(procedureProcedureClient, {
320
+ const recursive = new Proxy(flattenLazy, {
181
321
  get(target, key) {
182
322
  if (typeof key !== "string") {
183
323
  return Reflect.get(target, key);
@@ -214,10 +354,19 @@ var RouterBuilder = class _RouterBuilder {
214
354
  tags: [...this["~orpc"].tags ?? [], ...tags]
215
355
  });
216
356
  }
357
+ errors(errors) {
358
+ return new _RouterBuilder({
359
+ ...this["~orpc"],
360
+ errorMap: {
361
+ ...this["~orpc"].errorMap,
362
+ ...errors
363
+ }
364
+ });
365
+ }
217
366
  use(middleware) {
218
367
  return new _RouterBuilder({
219
368
  ...this["~orpc"],
220
- middlewares: [...this["~orpc"].middlewares ?? [], middleware]
369
+ middlewares: [...this["~orpc"].middlewares, middleware]
221
370
  });
222
371
  }
223
372
  router(router) {
@@ -244,7 +393,7 @@ function adapt(item, options) {
244
393
  return adaptedLazy;
245
394
  }
246
395
  if (isProcedure(item)) {
247
- let decorated = decorateProcedure(item);
396
+ let decorated = DecoratedProcedure.decorate(item);
248
397
  if (options.tags?.length) {
249
398
  decorated = decorated.unshiftTag(...options.tags);
250
399
  }
@@ -254,6 +403,9 @@ function adapt(item, options) {
254
403
  if (options.middlewares?.length) {
255
404
  decorated = decorated.unshiftMiddleware(...options.middlewares);
256
405
  }
406
+ if (Object.keys(options.errorMap).length) {
407
+ decorated = decorated.errors(options.errorMap);
408
+ }
257
409
  return decorated;
258
410
  }
259
411
  const adapted = {};
@@ -263,6 +415,213 @@ function adapt(item, options) {
263
415
  return adapted;
264
416
  }
265
417
 
418
+ // src/builder-with-errors-middlewares.ts
419
+ var BuilderWithErrorsMiddlewares = class _BuilderWithErrorsMiddlewares {
420
+ "~type" = "BuilderWithErrorsMiddlewares";
421
+ "~orpc";
422
+ constructor(def) {
423
+ this["~orpc"] = def;
424
+ }
425
+ errors(errors) {
426
+ return new _BuilderWithErrorsMiddlewares({
427
+ ...this["~orpc"],
428
+ errorMap: {
429
+ ...this["~orpc"].errorMap,
430
+ ...errors
431
+ }
432
+ });
433
+ }
434
+ use(middleware) {
435
+ return new _BuilderWithErrorsMiddlewares({
436
+ ...this["~orpc"],
437
+ inputValidationIndex: this["~orpc"].inputValidationIndex + 1,
438
+ outputValidationIndex: this["~orpc"].outputValidationIndex + 1,
439
+ middlewares: [...this["~orpc"].middlewares, middleware]
440
+ // FIXME: I believe we can remove `as any` here
441
+ });
442
+ }
443
+ route(route) {
444
+ return new ProcedureBuilder({
445
+ ...this["~orpc"],
446
+ contract: new ContractProcedure({
447
+ route,
448
+ InputSchema: void 0,
449
+ OutputSchema: void 0,
450
+ errorMap: this["~orpc"].errorMap
451
+ })
452
+ });
453
+ }
454
+ input(schema, example) {
455
+ return new ProcedureBuilderWithInput({
456
+ ...this["~orpc"],
457
+ contract: new ContractProcedure({
458
+ OutputSchema: void 0,
459
+ InputSchema: schema,
460
+ inputExample: example,
461
+ errorMap: this["~orpc"].errorMap
462
+ })
463
+ });
464
+ }
465
+ output(schema, example) {
466
+ return new ProcedureBuilderWithOutput({
467
+ ...this["~orpc"],
468
+ contract: new ContractProcedure({
469
+ InputSchema: void 0,
470
+ OutputSchema: schema,
471
+ outputExample: example,
472
+ errorMap: this["~orpc"].errorMap
473
+ })
474
+ });
475
+ }
476
+ handler(handler) {
477
+ return new DecoratedProcedure({
478
+ ...this["~orpc"],
479
+ contract: new ContractProcedure({
480
+ InputSchema: void 0,
481
+ OutputSchema: void 0,
482
+ errorMap: this["~orpc"].errorMap
483
+ }),
484
+ handler
485
+ });
486
+ }
487
+ prefix(prefix) {
488
+ return new RouterBuilder({
489
+ ...this["~orpc"],
490
+ prefix
491
+ });
492
+ }
493
+ tag(...tags) {
494
+ return new RouterBuilder({
495
+ ...this["~orpc"],
496
+ tags
497
+ });
498
+ }
499
+ router(router) {
500
+ return new RouterBuilder(this["~orpc"]).router(router);
501
+ }
502
+ lazy(loader) {
503
+ return new RouterBuilder(this["~orpc"]).lazy(loader);
504
+ }
505
+ };
506
+
507
+ // src/builder-with-errors.ts
508
+ var BuilderWithErrors = class _BuilderWithErrors {
509
+ "~type" = "BuilderWithErrors";
510
+ "~orpc";
511
+ constructor(def) {
512
+ this["~orpc"] = def;
513
+ }
514
+ context() {
515
+ return this;
516
+ }
517
+ errors(errors) {
518
+ return new _BuilderWithErrors({
519
+ ...this["~orpc"],
520
+ errorMap: {
521
+ ...this["~orpc"].errorMap,
522
+ ...errors
523
+ }
524
+ });
525
+ }
526
+ middleware(middleware) {
527
+ return decorateMiddleware(middleware);
528
+ }
529
+ use(middleware) {
530
+ return new BuilderWithErrorsMiddlewares({
531
+ ...this["~orpc"],
532
+ inputValidationIndex: 1,
533
+ outputValidationIndex: 1,
534
+ middlewares: [middleware]
535
+ // FIXME: I believe we can remove `as any` here
536
+ });
537
+ }
538
+ route(route) {
539
+ return new ProcedureBuilder({
540
+ middlewares: [],
541
+ inputValidationIndex: 0,
542
+ outputValidationIndex: 0,
543
+ contract: new ContractProcedure2({
544
+ route,
545
+ InputSchema: void 0,
546
+ OutputSchema: void 0,
547
+ errorMap: this["~orpc"].errorMap
548
+ })
549
+ });
550
+ }
551
+ input(schema, example) {
552
+ return new ProcedureBuilderWithInput({
553
+ middlewares: [],
554
+ inputValidationIndex: 0,
555
+ outputValidationIndex: 0,
556
+ contract: new ContractProcedure2({
557
+ OutputSchema: void 0,
558
+ InputSchema: schema,
559
+ inputExample: example,
560
+ errorMap: this["~orpc"].errorMap
561
+ })
562
+ });
563
+ }
564
+ output(schema, example) {
565
+ return new ProcedureBuilderWithOutput({
566
+ middlewares: [],
567
+ inputValidationIndex: 0,
568
+ outputValidationIndex: 0,
569
+ contract: new ContractProcedure2({
570
+ InputSchema: void 0,
571
+ OutputSchema: schema,
572
+ outputExample: example,
573
+ errorMap: this["~orpc"].errorMap
574
+ })
575
+ });
576
+ }
577
+ handler(handler) {
578
+ return new DecoratedProcedure({
579
+ middlewares: [],
580
+ inputValidationIndex: 0,
581
+ outputValidationIndex: 0,
582
+ contract: new ContractProcedure2({
583
+ InputSchema: void 0,
584
+ OutputSchema: void 0,
585
+ errorMap: this["~orpc"].errorMap
586
+ }),
587
+ handler
588
+ });
589
+ }
590
+ prefix(prefix) {
591
+ return new RouterBuilder({
592
+ middlewares: [],
593
+ errorMap: this["~orpc"].errorMap,
594
+ prefix
595
+ });
596
+ }
597
+ tag(...tags) {
598
+ return new RouterBuilder({
599
+ middlewares: [],
600
+ errorMap: this["~orpc"].errorMap,
601
+ tags
602
+ });
603
+ }
604
+ router(router) {
605
+ return new RouterBuilder({
606
+ middlewares: [],
607
+ ...this["~orpc"]
608
+ }).router(router);
609
+ }
610
+ lazy(loader) {
611
+ return new RouterBuilder({
612
+ middlewares: [],
613
+ ...this["~orpc"]
614
+ }).lazy(loader);
615
+ }
616
+ };
617
+
618
+ // src/builder-with-middlewares.ts
619
+ import { ContractProcedure as ContractProcedure3 } from "@orpc/contract";
620
+
621
+ // src/implementer-chainable.ts
622
+ import { isContractProcedure } from "@orpc/contract";
623
+ import { createCallableObject } from "@orpc/shared";
624
+
266
625
  // src/router-implementer.ts
267
626
  var RouterImplementer = class _RouterImplementer {
268
627
  "~type" = "RouterImplementer";
@@ -277,31 +636,42 @@ var RouterImplementer = class _RouterImplementer {
277
636
  });
278
637
  }
279
638
  router(router) {
280
- const adapted = new RouterBuilder(this["~orpc"]).router(router);
639
+ const adapted = new RouterBuilder({
640
+ ...this["~orpc"],
641
+ errorMap: {}
642
+ }).router(router);
281
643
  const contracted = setRouterContract(adapted, this["~orpc"].contract);
282
644
  return contracted;
283
645
  }
284
646
  lazy(loader) {
285
- const adapted = new RouterBuilder(this["~orpc"]).lazy(loader);
647
+ const adapted = new RouterBuilder({
648
+ ...this["~orpc"],
649
+ errorMap: {}
650
+ }).lazy(loader);
286
651
  const contracted = setRouterContract(adapted, this["~orpc"].contract);
287
652
  return contracted;
288
653
  }
289
654
  };
290
655
 
291
656
  // src/implementer-chainable.ts
292
- function createChainableImplementer(contract, middlewares) {
657
+ function createChainableImplementer(contract, options) {
293
658
  if (isContractProcedure(contract)) {
294
659
  const implementer = new ProcedureImplementer({
295
660
  contract,
296
- middlewares
661
+ middlewares: options.middlewares,
662
+ inputValidationIndex: options.inputValidationIndex,
663
+ outputValidationIndex: options.outputValidationIndex
297
664
  });
298
665
  return implementer;
299
666
  }
300
667
  const chainable = {};
301
668
  for (const key in contract) {
302
- chainable[key] = createChainableImplementer(contract[key], middlewares);
669
+ chainable[key] = createChainableImplementer(contract[key], options);
303
670
  }
304
- const routerImplementer = new RouterImplementer({ contract, middlewares });
671
+ const routerImplementer = new RouterImplementer({
672
+ contract,
673
+ middlewares: options.middlewares
674
+ });
305
675
  const merged = new Proxy(chainable, {
306
676
  get(target, key) {
307
677
  const next = Reflect.get(target, key);
@@ -318,167 +688,257 @@ function createChainableImplementer(contract, middlewares) {
318
688
  return merged;
319
689
  }
320
690
 
321
- // src/procedure-builder.ts
322
- import {
323
- DecoratedContractProcedure as DecoratedContractProcedure2
324
- } from "@orpc/contract";
325
- var ProcedureBuilder = class _ProcedureBuilder {
326
- "~type" = "ProcedureBuilder";
691
+ // src/builder-with-middlewares.ts
692
+ var BuilderWithMiddlewares = class _BuilderWithMiddlewares {
693
+ "~type" = "BuilderHasMiddlewares";
327
694
  "~orpc";
328
695
  constructor(def) {
329
696
  this["~orpc"] = def;
330
697
  }
698
+ use(middleware) {
699
+ return new _BuilderWithMiddlewares({
700
+ ...this["~orpc"],
701
+ inputValidationIndex: this["~orpc"].inputValidationIndex + 1,
702
+ outputValidationIndex: this["~orpc"].outputValidationIndex + 1,
703
+ middlewares: [...this["~orpc"].middlewares, middleware]
704
+ });
705
+ }
706
+ errors(errors) {
707
+ return new BuilderWithErrorsMiddlewares({
708
+ ...this["~orpc"],
709
+ errorMap: errors
710
+ });
711
+ }
331
712
  route(route) {
332
- return new _ProcedureBuilder({
713
+ return new ProcedureBuilder({
333
714
  ...this["~orpc"],
334
- contract: DecoratedContractProcedure2.decorate(this["~orpc"].contract).route(route)
715
+ contract: new ContractProcedure3({
716
+ route,
717
+ InputSchema: void 0,
718
+ OutputSchema: void 0,
719
+ errorMap: {}
720
+ })
335
721
  });
336
722
  }
337
723
  input(schema, example) {
338
- return new _ProcedureBuilder({
724
+ return new ProcedureBuilderWithInput({
339
725
  ...this["~orpc"],
340
- contract: DecoratedContractProcedure2.decorate(this["~orpc"].contract).input(schema, example)
726
+ contract: new ContractProcedure3({
727
+ OutputSchema: void 0,
728
+ InputSchema: schema,
729
+ inputExample: example,
730
+ errorMap: {}
731
+ })
341
732
  });
342
733
  }
343
734
  output(schema, example) {
344
- return new _ProcedureBuilder({
735
+ return new ProcedureBuilderWithOutput({
345
736
  ...this["~orpc"],
346
- contract: DecoratedContractProcedure2.decorate(this["~orpc"].contract).output(schema, example)
737
+ contract: new ContractProcedure3({
738
+ InputSchema: void 0,
739
+ OutputSchema: schema,
740
+ outputExample: example,
741
+ errorMap: {}
742
+ })
347
743
  });
348
744
  }
349
- use(middleware, mapInput) {
350
- if (!mapInput) {
351
- return new ProcedureImplementer({
352
- contract: this["~orpc"].contract,
353
- middlewares: this["~orpc"].middlewares
354
- }).use(middleware);
355
- }
356
- return new ProcedureImplementer({
357
- contract: this["~orpc"].contract,
358
- middlewares: this["~orpc"].middlewares
359
- }).use(middleware, mapInput);
360
- }
361
745
  handler(handler) {
362
- return decorateProcedure(new Procedure({
363
- middlewares: this["~orpc"].middlewares,
364
- contract: this["~orpc"].contract,
746
+ return new DecoratedProcedure({
747
+ ...this["~orpc"],
748
+ contract: new ContractProcedure3({
749
+ InputSchema: void 0,
750
+ OutputSchema: void 0,
751
+ errorMap: {}
752
+ }),
365
753
  handler
366
- }));
754
+ });
755
+ }
756
+ prefix(prefix) {
757
+ return new RouterBuilder({
758
+ middlewares: this["~orpc"].middlewares,
759
+ errorMap: {},
760
+ prefix
761
+ });
762
+ }
763
+ tag(...tags) {
764
+ return new RouterBuilder({
765
+ middlewares: this["~orpc"].middlewares,
766
+ errorMap: {},
767
+ tags
768
+ });
769
+ }
770
+ router(router) {
771
+ return new RouterBuilder({
772
+ errorMap: {},
773
+ ...this["~orpc"]
774
+ }).router(router);
775
+ }
776
+ lazy(loader) {
777
+ return new RouterBuilder({
778
+ errorMap: {},
779
+ ...this["~orpc"]
780
+ }).lazy(loader);
781
+ }
782
+ contract(contract) {
783
+ return createChainableImplementer(contract, this["~orpc"]);
367
784
  }
368
785
  };
369
786
 
370
787
  // src/builder.ts
371
- var Builder = class _Builder {
788
+ var Builder = class {
372
789
  "~type" = "Builder";
373
790
  "~orpc";
374
791
  constructor(def) {
375
792
  this["~orpc"] = def;
376
793
  }
377
794
  context() {
378
- return new _Builder({});
795
+ return this;
796
+ }
797
+ middleware(middleware) {
798
+ return decorateMiddleware(middleware);
799
+ }
800
+ errors(errors) {
801
+ return new BuilderWithErrors({
802
+ errorMap: errors
803
+ });
379
804
  }
380
805
  use(middleware) {
381
- return new _Builder({
806
+ return new BuilderWithMiddlewares({
382
807
  ...this["~orpc"],
383
- middlewares: [...this["~orpc"].middlewares ?? [], middleware]
808
+ inputValidationIndex: 1,
809
+ outputValidationIndex: 1,
810
+ middlewares: [middleware]
811
+ // FIXME: I believe we can remove `as any` here
384
812
  });
385
813
  }
386
- middleware(middleware) {
387
- return decorateMiddleware(middleware);
388
- }
389
814
  route(route) {
390
815
  return new ProcedureBuilder({
391
- middlewares: this["~orpc"].middlewares,
392
- contract: new ContractProcedure({
816
+ middlewares: [],
817
+ inputValidationIndex: 0,
818
+ outputValidationIndex: 0,
819
+ contract: new ContractProcedure4({
393
820
  route,
394
821
  InputSchema: void 0,
395
- OutputSchema: void 0
822
+ OutputSchema: void 0,
823
+ errorMap: {}
396
824
  })
397
825
  });
398
826
  }
399
827
  input(schema, example) {
400
- return new ProcedureBuilder({
401
- middlewares: this["~orpc"].middlewares,
402
- contract: new ContractProcedure({
828
+ return new ProcedureBuilderWithInput({
829
+ middlewares: [],
830
+ inputValidationIndex: 0,
831
+ outputValidationIndex: 0,
832
+ contract: new ContractProcedure4({
403
833
  OutputSchema: void 0,
404
834
  InputSchema: schema,
405
- inputExample: example
835
+ inputExample: example,
836
+ errorMap: {}
406
837
  })
407
838
  });
408
839
  }
409
840
  output(schema, example) {
410
- return new ProcedureBuilder({
411
- middlewares: this["~orpc"].middlewares,
412
- contract: new ContractProcedure({
841
+ return new ProcedureBuilderWithOutput({
842
+ middlewares: [],
843
+ inputValidationIndex: 0,
844
+ outputValidationIndex: 0,
845
+ contract: new ContractProcedure4({
413
846
  InputSchema: void 0,
414
847
  OutputSchema: schema,
415
- outputExample: example
848
+ outputExample: example,
849
+ errorMap: {}
416
850
  })
417
851
  });
418
852
  }
419
853
  handler(handler) {
420
- return decorateProcedure(new Procedure({
421
- middlewares: this["~orpc"].middlewares,
422
- contract: new ContractProcedure({
854
+ return new DecoratedProcedure({
855
+ middlewares: [],
856
+ inputValidationIndex: 0,
857
+ outputValidationIndex: 0,
858
+ contract: new ContractProcedure4({
423
859
  InputSchema: void 0,
424
- OutputSchema: void 0
860
+ OutputSchema: void 0,
861
+ errorMap: {}
425
862
  }),
426
863
  handler
427
- }));
864
+ });
428
865
  }
429
866
  prefix(prefix) {
430
867
  return new RouterBuilder({
431
- middlewares: this["~orpc"].middlewares,
868
+ middlewares: [],
869
+ errorMap: {},
432
870
  prefix
433
871
  });
434
872
  }
435
873
  tag(...tags) {
436
874
  return new RouterBuilder({
437
- middlewares: this["~orpc"].middlewares,
875
+ middlewares: [],
876
+ errorMap: {},
438
877
  tags
439
878
  });
440
879
  }
441
880
  router(router) {
442
- return new RouterBuilder(this["~orpc"]).router(router);
881
+ return new RouterBuilder({
882
+ middlewares: [],
883
+ errorMap: []
884
+ }).router(router);
443
885
  }
444
886
  lazy(loader) {
445
- return new RouterBuilder(this["~orpc"]).lazy(loader);
887
+ return new RouterBuilder({
888
+ middlewares: [],
889
+ errorMap: {}
890
+ }).lazy(loader);
446
891
  }
447
892
  contract(contract) {
448
- return createChainableImplementer(contract, this["~orpc"].middlewares);
893
+ return createChainableImplementer(contract, {
894
+ middlewares: [],
895
+ inputValidationIndex: 0,
896
+ outputValidationIndex: 0
897
+ });
449
898
  }
450
899
  };
451
900
 
901
+ // src/procedure-utils.ts
902
+ function call(procedure, input, ...rest) {
903
+ return createProcedureClient(procedure, ...rest)(input);
904
+ }
905
+
906
+ // src/lazy-utils.ts
907
+ function createLazyProcedureFormAnyLazy(lazied) {
908
+ const lazyProcedure = lazy(async () => {
909
+ const { default: maybeProcedure } = await unlazy(flatLazy(lazied));
910
+ if (!isProcedure(maybeProcedure)) {
911
+ throw new Error(`
912
+ Expected a lazy<procedure> but got lazy<unknown>.
913
+ This should be caught by TypeScript compilation.
914
+ Please report this issue if this makes you feel uncomfortable.
915
+ `);
916
+ }
917
+ return { default: maybeProcedure };
918
+ });
919
+ return lazyProcedure;
920
+ }
921
+
452
922
  // src/router-client.ts
453
- function createRouterClient(options) {
454
- if (isProcedure(options.router)) {
455
- const caller = createProcedureClient({
456
- ...options,
457
- procedure: options.router,
458
- context: options.context,
459
- path: options.path
460
- });
923
+ function createRouterClient(router, ...rest) {
924
+ if (isProcedure(router)) {
925
+ const caller = createProcedureClient(router, ...rest);
461
926
  return caller;
462
927
  }
463
- const procedureCaller = isLazy(options.router) ? createProcedureClient({
464
- ...options,
465
- procedure: createLazyProcedureFormAnyLazy(options.router),
466
- context: options.context,
467
- path: options.path
468
- }) : {};
928
+ const procedureCaller = isLazy(router) ? createProcedureClient(createLazyProcedureFormAnyLazy(router), ...rest) : {};
469
929
  const recursive = new Proxy(procedureCaller, {
470
930
  get(target, key) {
471
931
  if (typeof key !== "string") {
472
932
  return Reflect.get(target, key);
473
933
  }
474
- const next = getRouterChild(options.router, key);
934
+ const next = getRouterChild(router, key);
475
935
  if (!next) {
476
936
  return Reflect.get(target, key);
477
937
  }
478
- return createRouterClient({
938
+ const [options] = rest;
939
+ return createRouterClient(next, {
479
940
  ...options,
480
- router: next,
481
- path: [...options.path ?? [], key]
941
+ path: [...options?.path ?? [], key]
482
942
  });
483
943
  }
484
944
  });
@@ -486,35 +946,40 @@ function createRouterClient(options) {
486
946
  }
487
947
 
488
948
  // src/index.ts
489
- import { configGlobal, fallbackToGlobalConfig } from "@orpc/contract";
490
- export * from "@orpc/shared/error";
949
+ import { configGlobal, fallbackToGlobalConfig, isDefinedError, ORPCError, safe } from "@orpc/contract";
491
950
  var os = new Builder({});
492
951
  export {
493
952
  Builder,
953
+ DecoratedProcedure,
494
954
  LAZY_LOADER_SYMBOL,
955
+ ORPCError,
495
956
  Procedure,
496
957
  ProcedureBuilder,
497
958
  ProcedureImplementer,
498
959
  RouterBuilder,
499
960
  RouterImplementer,
961
+ call,
500
962
  configGlobal,
501
963
  createChainableImplementer,
964
+ createORPCErrorConstructorMap,
502
965
  createProcedureClient,
503
966
  createRouterClient,
504
967
  decorateLazy,
505
968
  decorateMiddleware,
506
- decorateProcedure,
507
969
  deepSetLazyRouterPrefix,
508
970
  fallbackToGlobalConfig,
509
971
  flatLazy,
510
972
  getLazyRouterPrefix,
511
973
  getRouterChild,
512
974
  getRouterContract,
975
+ isDefinedError,
513
976
  isLazy,
514
977
  isProcedure,
515
978
  lazy,
516
979
  mergeContext,
980
+ middlewareOutputFn,
517
981
  os,
982
+ safe,
518
983
  setRouterContract,
519
984
  unlazy
520
985
  };