@orpc/server 0.30.0 → 0.32.0

Sign up to get free protection for your applications and to get access to all the features.
package/dist/index.js CHANGED
@@ -11,14 +11,22 @@ import {
11
11
  mergeContext,
12
12
  middlewareOutputFn,
13
13
  unlazy
14
- } from "./chunk-SA7HGGVY.js";
14
+ } from "./chunk-GK2Z6B6W.js";
15
15
 
16
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
17
23
  import { ContractProcedure } from "@orpc/contract";
18
24
 
19
- // src/implementer-chainable.ts
20
- import { isContractProcedure } from "@orpc/contract";
21
- import { createCallableObject as createCallableObject2 } 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";
22
30
 
23
31
  // src/middleware-decorated.ts
24
32
  function decorateMiddleware(middleware) {
@@ -45,7 +53,6 @@ function decorateMiddleware(middleware) {
45
53
 
46
54
  // src/procedure-decorated.ts
47
55
  import { DecoratedContractProcedure } from "@orpc/contract";
48
- import { createCallableObject } from "@orpc/shared";
49
56
  var DecoratedProcedure = class _DecoratedProcedure extends Procedure {
50
57
  static decorate(procedure) {
51
58
  if (procedure instanceof _DecoratedProcedure) {
@@ -75,7 +82,7 @@ var DecoratedProcedure = class _DecoratedProcedure extends Procedure {
75
82
  const middleware_ = mapInput ? decorateMiddleware(middleware).mapInput(mapInput) : middleware;
76
83
  return new _DecoratedProcedure({
77
84
  ...this["~orpc"],
78
- postMiddlewares: [...this["~orpc"].postMiddlewares, middleware_]
85
+ middlewares: [...this["~orpc"].middlewares, middleware_]
79
86
  });
80
87
  }
81
88
  unshiftTag(...tags) {
@@ -86,32 +93,36 @@ var DecoratedProcedure = class _DecoratedProcedure extends Procedure {
86
93
  }
87
94
  unshiftMiddleware(...middlewares) {
88
95
  const castedMiddlewares = middlewares;
89
- if (this["~orpc"].preMiddlewares.length) {
96
+ if (this["~orpc"].middlewares.length) {
90
97
  let min = 0;
91
- for (let i = 0; i < this["~orpc"].preMiddlewares.length; i++) {
92
- const index = castedMiddlewares.indexOf(this["~orpc"].preMiddlewares[i], min);
98
+ for (let i = 0; i < this["~orpc"].middlewares.length; i++) {
99
+ const index = castedMiddlewares.indexOf(this["~orpc"].middlewares[i], min);
93
100
  if (index === -1) {
94
- castedMiddlewares.push(...this["~orpc"].preMiddlewares.slice(i));
101
+ castedMiddlewares.push(...this["~orpc"].middlewares.slice(i));
95
102
  break;
96
103
  }
97
104
  min = index + 1;
98
105
  }
99
106
  }
107
+ const numNewMiddlewares = castedMiddlewares.length - this["~orpc"].middlewares.length;
100
108
  return new _DecoratedProcedure({
101
109
  ...this["~orpc"],
102
- preMiddlewares: castedMiddlewares
110
+ inputValidationIndex: this["~orpc"].inputValidationIndex + numNewMiddlewares,
111
+ outputValidationIndex: this["~orpc"].outputValidationIndex + numNewMiddlewares,
112
+ middlewares: castedMiddlewares
103
113
  });
104
114
  }
105
115
  /**
106
116
  * Make this procedure callable (works like a function while still being a procedure).
107
- * **Note**: this only takes effect when this method is called at the end of the chain.
108
117
  */
109
118
  callable(...rest) {
110
- return createCallableObject(this, createProcedureClient(this, ...rest));
119
+ return Object.assign(createProcedureClient(this, ...rest), {
120
+ "~type": "Procedure",
121
+ "~orpc": this["~orpc"]
122
+ });
111
123
  }
112
124
  /**
113
125
  * Make this procedure compatible with server action (the same as .callable, but the type is compatible with server action).
114
- * **Note**: this only takes effect when this method is called at the end of the chain.
115
126
  */
116
127
  actionable(...rest) {
117
128
  return this.callable(...rest);
@@ -129,14 +140,141 @@ var ProcedureImplementer = class _ProcedureImplementer {
129
140
  const mappedMiddleware = mapInput ? decorateMiddleware(middleware).mapInput(mapInput) : middleware;
130
141
  return new _ProcedureImplementer({
131
142
  ...this["~orpc"],
132
- postMiddlewares: [...this["~orpc"].postMiddlewares, mappedMiddleware]
143
+ middlewares: [...this["~orpc"].middlewares, mappedMiddleware]
144
+ });
145
+ }
146
+ handler(handler) {
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
+ 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)
133
226
  });
134
227
  }
135
228
  handler(handler) {
136
229
  return new DecoratedProcedure({
137
- postMiddlewares: this["~orpc"].postMiddlewares,
138
- preMiddlewares: this["~orpc"].preMiddlewares,
139
- contract: this["~orpc"].contract,
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"],
140
278
  handler
141
279
  });
142
280
  }
@@ -277,6 +415,246 @@ function adapt(item, options) {
277
415
  return adapted;
278
416
  }
279
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
+ ...this["~orpc"].config.initialRoute,
449
+ ...route
450
+ },
451
+ InputSchema: void 0,
452
+ OutputSchema: void 0,
453
+ errorMap: this["~orpc"].errorMap
454
+ })
455
+ });
456
+ }
457
+ input(schema, example) {
458
+ return new ProcedureBuilderWithInput({
459
+ ...this["~orpc"],
460
+ contract: new ContractProcedure({
461
+ route: this["~orpc"].config.initialRoute,
462
+ OutputSchema: void 0,
463
+ InputSchema: schema,
464
+ inputExample: example,
465
+ errorMap: this["~orpc"].errorMap
466
+ })
467
+ });
468
+ }
469
+ output(schema, example) {
470
+ return new ProcedureBuilderWithOutput({
471
+ ...this["~orpc"],
472
+ contract: new ContractProcedure({
473
+ route: this["~orpc"].config.initialRoute,
474
+ InputSchema: void 0,
475
+ OutputSchema: schema,
476
+ outputExample: example,
477
+ errorMap: this["~orpc"].errorMap
478
+ })
479
+ });
480
+ }
481
+ handler(handler) {
482
+ return new DecoratedProcedure({
483
+ ...this["~orpc"],
484
+ contract: new ContractProcedure({
485
+ route: this["~orpc"].config.initialRoute,
486
+ InputSchema: void 0,
487
+ OutputSchema: void 0,
488
+ errorMap: this["~orpc"].errorMap
489
+ }),
490
+ handler
491
+ });
492
+ }
493
+ prefix(prefix) {
494
+ return new RouterBuilder({
495
+ ...this["~orpc"],
496
+ prefix
497
+ });
498
+ }
499
+ tag(...tags) {
500
+ return new RouterBuilder({
501
+ ...this["~orpc"],
502
+ tags
503
+ });
504
+ }
505
+ router(router) {
506
+ return new RouterBuilder(this["~orpc"]).router(router);
507
+ }
508
+ lazy(loader) {
509
+ return new RouterBuilder(this["~orpc"]).lazy(loader);
510
+ }
511
+ };
512
+
513
+ // src/config.ts
514
+ var DEFAULT_CONFIG = {
515
+ initialInputValidationIndex: 0,
516
+ initialOutputValidationIndex: 0
517
+ };
518
+ function fallbackConfig(key, value) {
519
+ if (value === void 0) {
520
+ return DEFAULT_CONFIG[key];
521
+ }
522
+ return value;
523
+ }
524
+
525
+ // src/builder-with-errors.ts
526
+ var BuilderWithErrors = class _BuilderWithErrors {
527
+ "~type" = "BuilderWithErrors";
528
+ "~orpc";
529
+ constructor(def) {
530
+ this["~orpc"] = def;
531
+ }
532
+ config(config) {
533
+ return new _BuilderWithErrors({
534
+ ...this["~orpc"],
535
+ config: {
536
+ ...this["~orpc"].config,
537
+ ...config
538
+ }
539
+ });
540
+ }
541
+ context() {
542
+ return this;
543
+ }
544
+ errors(errors) {
545
+ return new _BuilderWithErrors({
546
+ ...this["~orpc"],
547
+ errorMap: {
548
+ ...this["~orpc"].errorMap,
549
+ ...errors
550
+ }
551
+ });
552
+ }
553
+ middleware(middleware) {
554
+ return decorateMiddleware(middleware);
555
+ }
556
+ use(middleware) {
557
+ return new BuilderWithErrorsMiddlewares({
558
+ ...this["~orpc"],
559
+ inputValidationIndex: fallbackConfig("initialInputValidationIndex", this["~orpc"].config.initialInputValidationIndex) + 1,
560
+ outputValidationIndex: fallbackConfig("initialOutputValidationIndex", this["~orpc"].config.initialOutputValidationIndex) + 1,
561
+ middlewares: [middleware]
562
+ // FIXME: I believe we can remove `as any` here
563
+ });
564
+ }
565
+ route(route) {
566
+ return new ProcedureBuilder({
567
+ middlewares: [],
568
+ inputValidationIndex: fallbackConfig("initialInputValidationIndex", this["~orpc"].config.initialInputValidationIndex),
569
+ outputValidationIndex: fallbackConfig("initialOutputValidationIndex", this["~orpc"].config.initialOutputValidationIndex),
570
+ contract: new ContractProcedure2({
571
+ route: {
572
+ ...this["~orpc"].config.initialRoute,
573
+ ...route
574
+ },
575
+ InputSchema: void 0,
576
+ OutputSchema: void 0,
577
+ errorMap: this["~orpc"].errorMap
578
+ })
579
+ });
580
+ }
581
+ input(schema, example) {
582
+ return new ProcedureBuilderWithInput({
583
+ middlewares: [],
584
+ inputValidationIndex: fallbackConfig("initialInputValidationIndex", this["~orpc"].config.initialInputValidationIndex),
585
+ outputValidationIndex: fallbackConfig("initialOutputValidationIndex", this["~orpc"].config.initialOutputValidationIndex),
586
+ contract: new ContractProcedure2({
587
+ route: this["~orpc"].config.initialRoute,
588
+ OutputSchema: void 0,
589
+ InputSchema: schema,
590
+ inputExample: example,
591
+ errorMap: this["~orpc"].errorMap
592
+ })
593
+ });
594
+ }
595
+ output(schema, example) {
596
+ return new ProcedureBuilderWithOutput({
597
+ middlewares: [],
598
+ inputValidationIndex: fallbackConfig("initialInputValidationIndex", this["~orpc"].config.initialInputValidationIndex),
599
+ outputValidationIndex: fallbackConfig("initialOutputValidationIndex", this["~orpc"].config.initialOutputValidationIndex),
600
+ contract: new ContractProcedure2({
601
+ route: this["~orpc"].config.initialRoute,
602
+ InputSchema: void 0,
603
+ OutputSchema: schema,
604
+ outputExample: example,
605
+ errorMap: this["~orpc"].errorMap
606
+ })
607
+ });
608
+ }
609
+ handler(handler) {
610
+ return new DecoratedProcedure({
611
+ middlewares: [],
612
+ inputValidationIndex: fallbackConfig("initialInputValidationIndex", this["~orpc"].config.initialInputValidationIndex),
613
+ outputValidationIndex: fallbackConfig("initialOutputValidationIndex", this["~orpc"].config.initialOutputValidationIndex),
614
+ contract: new ContractProcedure2({
615
+ route: this["~orpc"].config.initialRoute,
616
+ InputSchema: void 0,
617
+ OutputSchema: void 0,
618
+ errorMap: this["~orpc"].errorMap
619
+ }),
620
+ handler
621
+ });
622
+ }
623
+ prefix(prefix) {
624
+ return new RouterBuilder({
625
+ middlewares: [],
626
+ errorMap: this["~orpc"].errorMap,
627
+ prefix
628
+ });
629
+ }
630
+ tag(...tags) {
631
+ return new RouterBuilder({
632
+ middlewares: [],
633
+ errorMap: this["~orpc"].errorMap,
634
+ tags
635
+ });
636
+ }
637
+ router(router) {
638
+ return new RouterBuilder({
639
+ middlewares: [],
640
+ ...this["~orpc"]
641
+ }).router(router);
642
+ }
643
+ lazy(loader) {
644
+ return new RouterBuilder({
645
+ middlewares: [],
646
+ ...this["~orpc"]
647
+ }).lazy(loader);
648
+ }
649
+ };
650
+
651
+ // src/builder-with-middlewares.ts
652
+ import { ContractProcedure as ContractProcedure3 } from "@orpc/contract";
653
+
654
+ // src/implementer-chainable.ts
655
+ import { isContractProcedure } from "@orpc/contract";
656
+ import { createCallableObject } from "@orpc/shared";
657
+
280
658
  // src/router-implementer.ts
281
659
  var RouterImplementer = class _RouterImplementer {
282
660
  "~type" = "RouterImplementer";
@@ -309,20 +687,24 @@ var RouterImplementer = class _RouterImplementer {
309
687
  };
310
688
 
311
689
  // src/implementer-chainable.ts
312
- function createChainableImplementer(contract, middlewares = []) {
690
+ function createChainableImplementer(contract, options) {
313
691
  if (isContractProcedure(contract)) {
314
692
  const implementer = new ProcedureImplementer({
315
693
  contract,
316
- preMiddlewares: middlewares,
317
- postMiddlewares: []
694
+ middlewares: options.middlewares,
695
+ inputValidationIndex: options.inputValidationIndex,
696
+ outputValidationIndex: options.outputValidationIndex
318
697
  });
319
698
  return implementer;
320
699
  }
321
700
  const chainable = {};
322
701
  for (const key in contract) {
323
- chainable[key] = createChainableImplementer(contract[key], middlewares);
702
+ chainable[key] = createChainableImplementer(contract[key], options);
324
703
  }
325
- const routerImplementer = new RouterImplementer({ contract, middlewares });
704
+ const routerImplementer = new RouterImplementer({
705
+ contract,
706
+ middlewares: options.middlewares
707
+ });
326
708
  const merged = new Proxy(chainable, {
327
709
  get(target, key) {
328
710
  const next = Reflect.get(target, key);
@@ -333,68 +715,112 @@ function createChainableImplementer(contract, middlewares = []) {
333
715
  if (!next) {
334
716
  return method.bind(routerImplementer);
335
717
  }
336
- return createCallableObject2(next, method.bind(routerImplementer));
718
+ return createCallableObject(next, method.bind(routerImplementer));
337
719
  }
338
720
  });
339
721
  return merged;
340
722
  }
341
723
 
342
- // src/procedure-builder.ts
343
- import {
344
- DecoratedContractProcedure as DecoratedContractProcedure2
345
- } from "@orpc/contract";
346
- var ProcedureBuilder = class _ProcedureBuilder {
347
- "~type" = "ProcedureBuilder";
724
+ // src/builder-with-middlewares.ts
725
+ var BuilderWithMiddlewares = class _BuilderWithMiddlewares {
726
+ "~type" = "BuilderHasMiddlewares";
348
727
  "~orpc";
349
728
  constructor(def) {
350
729
  this["~orpc"] = def;
351
730
  }
352
- route(route) {
353
- return new _ProcedureBuilder({
731
+ use(middleware) {
732
+ return new _BuilderWithMiddlewares({
354
733
  ...this["~orpc"],
355
- contract: DecoratedContractProcedure2.decorate(this["~orpc"].contract).route(route)
734
+ inputValidationIndex: this["~orpc"].inputValidationIndex + 1,
735
+ outputValidationIndex: this["~orpc"].outputValidationIndex + 1,
736
+ middlewares: [...this["~orpc"].middlewares, middleware]
356
737
  });
357
738
  }
358
- input(schema, example) {
359
- return new _ProcedureBuilder({
739
+ errors(errors) {
740
+ return new BuilderWithErrorsMiddlewares({
360
741
  ...this["~orpc"],
361
- contract: DecoratedContractProcedure2.decorate(this["~orpc"].contract).input(schema, example)
742
+ errorMap: errors
362
743
  });
363
744
  }
364
- output(schema, example) {
365
- return new _ProcedureBuilder({
745
+ route(route) {
746
+ return new ProcedureBuilder({
366
747
  ...this["~orpc"],
367
- contract: DecoratedContractProcedure2.decorate(this["~orpc"].contract).output(schema, example)
748
+ contract: new ContractProcedure3({
749
+ route: {
750
+ ...this["~orpc"].config.initialRoute,
751
+ ...route
752
+ },
753
+ InputSchema: void 0,
754
+ OutputSchema: void 0,
755
+ errorMap: {}
756
+ })
368
757
  });
369
758
  }
370
- errors(errors) {
371
- return new _ProcedureBuilder({
759
+ input(schema, example) {
760
+ return new ProcedureBuilderWithInput({
372
761
  ...this["~orpc"],
373
- contract: DecoratedContractProcedure2.decorate(this["~orpc"].contract).errors(errors)
762
+ contract: new ContractProcedure3({
763
+ route: this["~orpc"].config.initialRoute,
764
+ OutputSchema: void 0,
765
+ InputSchema: schema,
766
+ inputExample: example,
767
+ errorMap: {}
768
+ })
374
769
  });
375
770
  }
376
- use(middleware, mapInput) {
377
- if (!mapInput) {
378
- return new ProcedureImplementer({
379
- contract: this["~orpc"].contract,
380
- preMiddlewares: this["~orpc"].middlewares,
381
- postMiddlewares: []
382
- }).use(middleware);
383
- }
384
- return new ProcedureImplementer({
385
- contract: this["~orpc"].contract,
386
- preMiddlewares: this["~orpc"].middlewares,
387
- postMiddlewares: []
388
- }).use(middleware, mapInput);
771
+ output(schema, example) {
772
+ return new ProcedureBuilderWithOutput({
773
+ ...this["~orpc"],
774
+ contract: new ContractProcedure3({
775
+ route: this["~orpc"].config.initialRoute,
776
+ InputSchema: void 0,
777
+ OutputSchema: schema,
778
+ outputExample: example,
779
+ errorMap: {}
780
+ })
781
+ });
389
782
  }
390
783
  handler(handler) {
391
784
  return new DecoratedProcedure({
392
- preMiddlewares: this["~orpc"].middlewares,
393
- postMiddlewares: [],
394
- contract: this["~orpc"].contract,
785
+ ...this["~orpc"],
786
+ contract: new ContractProcedure3({
787
+ route: this["~orpc"].config.initialRoute,
788
+ InputSchema: void 0,
789
+ OutputSchema: void 0,
790
+ errorMap: {}
791
+ }),
395
792
  handler
396
793
  });
397
794
  }
795
+ prefix(prefix) {
796
+ return new RouterBuilder({
797
+ middlewares: this["~orpc"].middlewares,
798
+ errorMap: {},
799
+ prefix
800
+ });
801
+ }
802
+ tag(...tags) {
803
+ return new RouterBuilder({
804
+ middlewares: this["~orpc"].middlewares,
805
+ errorMap: {},
806
+ tags
807
+ });
808
+ }
809
+ router(router) {
810
+ return new RouterBuilder({
811
+ errorMap: {},
812
+ ...this["~orpc"]
813
+ }).router(router);
814
+ }
815
+ lazy(loader) {
816
+ return new RouterBuilder({
817
+ errorMap: {},
818
+ ...this["~orpc"]
819
+ }).lazy(loader);
820
+ }
821
+ contract(contract) {
822
+ return createChainableImplementer(contract, this["~orpc"]);
823
+ }
398
824
  };
399
825
 
400
826
  // src/builder.ts
@@ -404,99 +830,126 @@ var Builder = class _Builder {
404
830
  constructor(def) {
405
831
  this["~orpc"] = def;
406
832
  }
407
- // TODO: separate it
408
- context() {
409
- return new _Builder({
410
- middlewares: [],
411
- errorMap: {}
412
- });
413
- }
414
- use(middleware) {
833
+ config(config) {
415
834
  return new _Builder({
416
835
  ...this["~orpc"],
417
- middlewares: [...this["~orpc"].middlewares, middleware]
836
+ config: {
837
+ ...this["~orpc"].config,
838
+ ...config
839
+ }
418
840
  });
419
841
  }
842
+ context() {
843
+ return this;
844
+ }
845
+ middleware(middleware) {
846
+ return decorateMiddleware(middleware);
847
+ }
420
848
  errors(errors) {
421
- return new _Builder({
849
+ return new BuilderWithErrors({
422
850
  ...this["~orpc"],
423
- errorMap: {
424
- ...this["~orpc"].errorMap,
425
- ...errors
426
- }
851
+ errorMap: errors
427
852
  });
428
853
  }
429
- // TODO: not allow define middleware after has context, or anything else
430
- middleware(middleware) {
431
- return decorateMiddleware(middleware);
854
+ use(middleware) {
855
+ return new BuilderWithMiddlewares({
856
+ ...this["~orpc"],
857
+ inputValidationIndex: fallbackConfig("initialInputValidationIndex", this["~orpc"].config.initialInputValidationIndex) + 1,
858
+ outputValidationIndex: fallbackConfig("initialOutputValidationIndex", this["~orpc"].config.initialOutputValidationIndex) + 1,
859
+ middlewares: [middleware]
860
+ // FIXME: I believe we can remove `as any` here
861
+ });
432
862
  }
433
863
  route(route) {
434
864
  return new ProcedureBuilder({
435
- middlewares: this["~orpc"].middlewares,
436
- contract: new ContractProcedure({
437
- route,
865
+ middlewares: [],
866
+ inputValidationIndex: fallbackConfig("initialInputValidationIndex", this["~orpc"].config.initialInputValidationIndex),
867
+ outputValidationIndex: fallbackConfig("initialOutputValidationIndex", this["~orpc"].config.initialOutputValidationIndex),
868
+ contract: new ContractProcedure4({
869
+ route: {
870
+ ...this["~orpc"].config.initialRoute,
871
+ ...route
872
+ },
438
873
  InputSchema: void 0,
439
874
  OutputSchema: void 0,
440
- errorMap: this["~orpc"].errorMap
875
+ errorMap: {}
441
876
  })
442
877
  });
443
878
  }
444
879
  input(schema, example) {
445
- return new ProcedureBuilder({
446
- middlewares: this["~orpc"].middlewares,
447
- contract: new ContractProcedure({
880
+ return new ProcedureBuilderWithInput({
881
+ middlewares: [],
882
+ inputValidationIndex: fallbackConfig("initialInputValidationIndex", this["~orpc"].config.initialInputValidationIndex),
883
+ outputValidationIndex: fallbackConfig("initialOutputValidationIndex", this["~orpc"].config.initialOutputValidationIndex),
884
+ contract: new ContractProcedure4({
885
+ route: this["~orpc"].config.initialRoute,
448
886
  OutputSchema: void 0,
449
887
  InputSchema: schema,
450
888
  inputExample: example,
451
- errorMap: this["~orpc"].errorMap
889
+ errorMap: {}
452
890
  })
453
891
  });
454
892
  }
455
893
  output(schema, example) {
456
- return new ProcedureBuilder({
457
- middlewares: this["~orpc"].middlewares,
458
- contract: new ContractProcedure({
894
+ return new ProcedureBuilderWithOutput({
895
+ middlewares: [],
896
+ inputValidationIndex: fallbackConfig("initialInputValidationIndex", this["~orpc"].config.initialInputValidationIndex),
897
+ outputValidationIndex: fallbackConfig("initialOutputValidationIndex", this["~orpc"].config.initialOutputValidationIndex),
898
+ contract: new ContractProcedure4({
899
+ route: this["~orpc"].config.initialRoute,
459
900
  InputSchema: void 0,
460
901
  OutputSchema: schema,
461
902
  outputExample: example,
462
- errorMap: this["~orpc"].errorMap
903
+ errorMap: {}
463
904
  })
464
905
  });
465
906
  }
466
907
  handler(handler) {
467
908
  return new DecoratedProcedure({
468
- preMiddlewares: this["~orpc"].middlewares,
469
- postMiddlewares: [],
470
- contract: new ContractProcedure({
909
+ middlewares: [],
910
+ inputValidationIndex: fallbackConfig("initialInputValidationIndex", this["~orpc"].config.initialInputValidationIndex),
911
+ outputValidationIndex: fallbackConfig("initialOutputValidationIndex", this["~orpc"].config.initialOutputValidationIndex),
912
+ contract: new ContractProcedure4({
913
+ route: this["~orpc"].config.initialRoute,
471
914
  InputSchema: void 0,
472
915
  OutputSchema: void 0,
473
- errorMap: this["~orpc"].errorMap
916
+ errorMap: {}
474
917
  }),
475
918
  handler
476
919
  });
477
920
  }
478
921
  prefix(prefix) {
479
922
  return new RouterBuilder({
480
- middlewares: this["~orpc"].middlewares,
481
- errorMap: this["~orpc"].errorMap,
923
+ middlewares: [],
924
+ errorMap: {},
482
925
  prefix
483
926
  });
484
927
  }
485
928
  tag(...tags) {
486
929
  return new RouterBuilder({
487
- middlewares: this["~orpc"].middlewares,
488
- errorMap: this["~orpc"].errorMap,
930
+ middlewares: [],
931
+ errorMap: {},
489
932
  tags
490
933
  });
491
934
  }
492
935
  router(router) {
493
- return new RouterBuilder(this["~orpc"]).router(router);
936
+ return new RouterBuilder({
937
+ middlewares: [],
938
+ errorMap: []
939
+ }).router(router);
494
940
  }
495
941
  lazy(loader) {
496
- return new RouterBuilder(this["~orpc"]).lazy(loader);
942
+ return new RouterBuilder({
943
+ middlewares: [],
944
+ errorMap: {}
945
+ }).lazy(loader);
497
946
  }
498
947
  contract(contract) {
499
- return createChainableImplementer(contract, this["~orpc"].middlewares);
948
+ return createChainableImplementer(contract, {
949
+ middlewares: [],
950
+ inputValidationIndex: 0,
951
+ outputValidationIndex: 0
952
+ });
500
953
  }
501
954
  };
502
955
 
@@ -548,10 +1001,9 @@ function createRouterClient(router, ...rest) {
548
1001
  }
549
1002
 
550
1003
  // src/index.ts
551
- import { configGlobal, fallbackToGlobalConfig, isDefinedError, ORPCError, safe } from "@orpc/contract";
1004
+ import { isDefinedError, ORPCError, safe, type } from "@orpc/contract";
552
1005
  var os = new Builder({
553
- middlewares: [],
554
- errorMap: {}
1006
+ config: {}
555
1007
  });
556
1008
  export {
557
1009
  Builder,
@@ -564,7 +1016,6 @@ export {
564
1016
  RouterBuilder,
565
1017
  RouterImplementer,
566
1018
  call,
567
- configGlobal,
568
1019
  createChainableImplementer,
569
1020
  createORPCErrorConstructorMap,
570
1021
  createProcedureClient,
@@ -572,7 +1023,7 @@ export {
572
1023
  decorateLazy,
573
1024
  decorateMiddleware,
574
1025
  deepSetLazyRouterPrefix,
575
- fallbackToGlobalConfig,
1026
+ fallbackConfig,
576
1027
  flatLazy,
577
1028
  getLazyRouterPrefix,
578
1029
  getRouterChild,
@@ -586,6 +1037,7 @@ export {
586
1037
  os,
587
1038
  safe,
588
1039
  setRouterContract,
1040
+ type,
589
1041
  unlazy
590
1042
  };
591
1043
  //# sourceMappingURL=index.js.map