@orpc/server 0.0.0-next.3f6c426 → 0.0.0-next.43889a7

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