@orpc/server 0.0.0-next.9b3a030 → 0.0.0-next.9fe2a8b

Sign up to get free protection for your applications and to get access to all the features.
Files changed (46) hide show
  1. package/dist/{chunk-V3I7RIRY.js → chunk-ESTRJAOX.js} +8 -11
  2. package/dist/chunk-KK4SDLC7.js +320 -0
  3. package/dist/fetch.js +4 -4
  4. package/dist/hono.js +5 -5
  5. package/dist/index.js +248 -402
  6. package/dist/next.js +5 -5
  7. package/dist/node.js +10 -10
  8. package/dist/src/adapters/fetch/orpc-handler.d.ts +4 -4
  9. package/dist/src/adapters/fetch/orpc-procedure-matcher.d.ts +4 -4
  10. package/dist/src/adapters/fetch/types.d.ts +4 -4
  11. package/dist/src/adapters/hono/middleware.d.ts +3 -3
  12. package/dist/src/adapters/next/serve.d.ts +8 -8
  13. package/dist/src/adapters/node/orpc-handler.d.ts +5 -5
  14. package/dist/src/adapters/node/types.d.ts +5 -5
  15. package/dist/src/builder-variants.d.ts +74 -0
  16. package/dist/src/builder.d.ts +52 -31
  17. package/dist/src/config.d.ts +6 -0
  18. package/dist/src/context.d.ts +9 -0
  19. package/dist/src/hidden.d.ts +6 -4
  20. package/dist/src/implementer-procedure.d.ts +30 -0
  21. package/dist/src/implementer-variants.d.ts +17 -0
  22. package/dist/src/implementer.d.ts +28 -0
  23. package/dist/src/index.d.ts +10 -13
  24. package/dist/src/lazy-utils.d.ts +4 -2
  25. package/dist/src/lazy.d.ts +9 -5
  26. package/dist/src/middleware-decorated.d.ts +7 -6
  27. package/dist/src/middleware-utils.d.ts +5 -0
  28. package/dist/src/middleware.d.ts +22 -20
  29. package/dist/src/procedure-client.d.ts +7 -21
  30. package/dist/src/procedure-decorated.d.ts +18 -12
  31. package/dist/src/procedure-utils.d.ts +17 -0
  32. package/dist/src/procedure.d.ts +19 -33
  33. package/dist/src/router-accessible-lazy.d.ts +8 -0
  34. package/dist/src/router-client.d.ts +7 -10
  35. package/dist/src/router.d.ts +25 -12
  36. package/package.json +3 -3
  37. package/dist/chunk-DNG2IB3R.js +0 -227
  38. package/dist/src/error.d.ts +0 -10
  39. package/dist/src/implementer-chainable.d.ts +0 -10
  40. package/dist/src/lazy-decorated.d.ts +0 -10
  41. package/dist/src/procedure-builder.d.ts +0 -24
  42. package/dist/src/procedure-implementer.d.ts +0 -19
  43. package/dist/src/router-builder.d.ts +0 -29
  44. package/dist/src/router-implementer.d.ts +0 -21
  45. package/dist/src/types.d.ts +0 -14
  46. package/dist/src/utils.d.ts +0 -3
package/dist/index.js CHANGED
@@ -1,23 +1,38 @@
1
1
  import {
2
2
  LAZY_LOADER_SYMBOL,
3
3
  Procedure,
4
- createORPCErrorConstructorMap,
4
+ adaptRouter,
5
+ addMiddleware,
6
+ createAccessibleLazyRouter,
7
+ createLazyProcedureFormAnyLazy,
5
8
  createProcedureClient,
9
+ deepSetLazyRouterPrefix,
6
10
  flatLazy,
11
+ getLazyRouterPrefix,
7
12
  getRouterChild,
13
+ getRouterContract,
8
14
  isLazy,
9
15
  isProcedure,
10
16
  lazy,
11
- mergeContext,
17
+ middlewareOutputFn,
18
+ setRouterContract,
12
19
  unlazy
13
- } from "./chunk-DNG2IB3R.js";
20
+ } from "./chunk-KK4SDLC7.js";
14
21
 
15
22
  // src/builder.ts
16
- import { ContractProcedure } from "@orpc/contract";
23
+ import { mergeErrorMap as mergeErrorMap2, mergeMeta as mergeMeta2, mergePrefix, mergeRoute as mergeRoute2, mergeTags } from "@orpc/contract";
17
24
 
18
- // src/implementer-chainable.ts
19
- import { isContractProcedure } from "@orpc/contract";
20
- import { createCallableObject } from "@orpc/shared";
25
+ // src/config.ts
26
+ var DEFAULT_CONFIG = {
27
+ initialInputValidationIndex: 0,
28
+ initialOutputValidationIndex: 0
29
+ };
30
+ function fallbackConfig(key, value) {
31
+ if (value === void 0) {
32
+ return DEFAULT_CONFIG[key];
33
+ }
34
+ return value;
35
+ }
21
36
 
22
37
  // src/middleware-decorated.ts
23
38
  function decorateMiddleware(middleware) {
@@ -31,8 +46,8 @@ function decorateMiddleware(middleware) {
31
46
  decorated.concat = (concatMiddleware, mapInput) => {
32
47
  const mapped = mapInput ? decorateMiddleware(concatMiddleware).mapInput(mapInput) : concatMiddleware;
33
48
  const concatted = decorateMiddleware((options, input, output, ...rest) => {
34
- const next = async (nextOptions) => {
35
- return mapped({ ...options, context: mergeContext(nextOptions.context, options.context) }, input, output, ...rest);
49
+ const next = async (...[nextOptions]) => {
50
+ return mapped({ ...options, context: { ...nextOptions?.context, ...options.context } }, input, output, ...rest);
36
51
  };
37
52
  const merged = middleware({ ...options, next }, input, output, ...rest);
38
53
  return merged;
@@ -43,463 +58,294 @@ function decorateMiddleware(middleware) {
43
58
  }
44
59
 
45
60
  // src/procedure-decorated.ts
46
- import { DecoratedContractProcedure } from "@orpc/contract";
47
- function decorateProcedure(procedure) {
48
- const caller = createProcedureClient({
49
- procedure,
50
- context: void 0
51
- });
52
- const decorated = caller;
53
- decorated["~type"] = procedure["~type"];
54
- decorated["~orpc"] = procedure["~orpc"];
55
- decorated.prefix = (prefix) => {
56
- return decorateProcedure(new Procedure({
57
- ...procedure["~orpc"],
58
- contract: DecoratedContractProcedure.decorate(procedure["~orpc"].contract).prefix(prefix)
59
- }));
60
- };
61
- decorated.route = (route) => {
62
- return decorateProcedure(new Procedure({
63
- ...procedure["~orpc"],
64
- contract: DecoratedContractProcedure.decorate(procedure["~orpc"].contract).route(route)
65
- }));
66
- };
67
- decorated.use = (middleware, mapInput) => {
68
- const middleware_ = mapInput ? decorateMiddleware(middleware).mapInput(mapInput) : middleware;
69
- return decorateProcedure(new Procedure({
70
- ...procedure["~orpc"],
71
- middlewares: [...procedure["~orpc"].middlewares ?? [], middleware_]
72
- }));
73
- };
74
- decorated.unshiftTag = (...tags) => {
75
- return decorateProcedure(new Procedure({
76
- ...procedure["~orpc"],
77
- contract: DecoratedContractProcedure.decorate(procedure["~orpc"].contract).unshiftTag(...tags)
78
- }));
79
- };
80
- decorated.unshiftMiddleware = (...middlewares) => {
81
- if (procedure["~orpc"].middlewares?.length) {
82
- let min = 0;
83
- for (let i = 0; i < procedure["~orpc"].middlewares.length; i++) {
84
- const index = middlewares.indexOf(procedure["~orpc"].middlewares[i], min);
85
- if (index === -1) {
86
- middlewares.push(...procedure["~orpc"].middlewares.slice(i));
87
- break;
88
- }
89
- min = index + 1;
90
- }
91
- }
92
- return decorateProcedure(new Procedure({
93
- ...procedure["~orpc"],
94
- middlewares
95
- }));
96
- };
97
- return decorated;
98
- }
99
-
100
- // src/procedure-implementer.ts
101
- var ProcedureImplementer = class _ProcedureImplementer {
102
- "~type" = "ProcedureImplementer";
103
- "~orpc";
104
- constructor(def) {
105
- this["~orpc"] = def;
106
- }
107
- use(middleware, mapInput) {
108
- const mappedMiddleware = mapInput ? decorateMiddleware(middleware).mapInput(mapInput) : middleware;
109
- return new _ProcedureImplementer({
61
+ import { mergeErrorMap, mergeMeta, mergeRoute } from "@orpc/contract";
62
+ var DecoratedProcedure = class _DecoratedProcedure extends Procedure {
63
+ errors(errors) {
64
+ return new _DecoratedProcedure({
110
65
  ...this["~orpc"],
111
- middlewares: [...this["~orpc"].middlewares ?? [], mappedMiddleware]
66
+ errorMap: mergeErrorMap(this["~orpc"].errorMap, errors)
112
67
  });
113
68
  }
114
- handler(handler) {
115
- return decorateProcedure(new Procedure({
116
- middlewares: this["~orpc"].middlewares,
117
- contract: this["~orpc"].contract,
118
- handler
119
- }));
120
- }
121
- };
122
-
123
- // src/hidden.ts
124
- var ROUTER_CONTRACT_SYMBOL = Symbol("ORPC_ROUTER_CONTRACT");
125
- function setRouterContract(obj, contract) {
126
- return new Proxy(obj, {
127
- get(target, key) {
128
- if (key === ROUTER_CONTRACT_SYMBOL) {
129
- return contract;
130
- }
131
- return Reflect.get(target, key);
132
- }
133
- });
134
- }
135
- function getRouterContract(obj) {
136
- return obj[ROUTER_CONTRACT_SYMBOL];
137
- }
138
- var LAZY_ROUTER_PREFIX_SYMBOL = Symbol("ORPC_LAZY_ROUTER_PREFIX");
139
- function deepSetLazyRouterPrefix(router, prefix) {
140
- return new Proxy(router, {
141
- get(target, key) {
142
- if (key !== LAZY_ROUTER_PREFIX_SYMBOL) {
143
- const val = Reflect.get(target, key);
144
- if (val && (typeof val === "object" || typeof val === "function")) {
145
- return deepSetLazyRouterPrefix(val, prefix);
146
- }
147
- return val;
148
- }
149
- return prefix;
150
- }
151
- });
152
- }
153
- function getLazyRouterPrefix(obj) {
154
- return obj[LAZY_ROUTER_PREFIX_SYMBOL];
155
- }
156
-
157
- // src/lazy-utils.ts
158
- function createLazyProcedureFormAnyLazy(lazied) {
159
- const lazyProcedure = lazy(async () => {
160
- const { default: maybeProcedure } = await unlazy(flatLazy(lazied));
161
- if (!isProcedure(maybeProcedure)) {
162
- throw new Error(`
163
- Expected a lazy<procedure> but got lazy<unknown>.
164
- This should be caught by TypeScript compilation.
165
- Please report this issue if this makes you feel uncomfortable.
166
- `);
167
- }
168
- return { default: maybeProcedure };
169
- });
170
- return lazyProcedure;
171
- }
172
-
173
- // src/lazy-decorated.ts
174
- function decorateLazy(lazied) {
175
- const flattenLazy = flatLazy(lazied);
176
- const procedureProcedureClient = createProcedureClient({
177
- procedure: createLazyProcedureFormAnyLazy(flattenLazy),
178
- context: void 0
179
- });
180
- Object.assign(procedureProcedureClient, flattenLazy);
181
- const recursive = new Proxy(procedureProcedureClient, {
182
- get(target, key) {
183
- if (typeof key !== "string") {
184
- return Reflect.get(target, key);
185
- }
186
- const next = getRouterChild(flattenLazy, key);
187
- return decorateLazy(next);
188
- }
189
- });
190
- return recursive;
191
- }
192
-
193
- // src/router-builder.ts
194
- var RouterBuilder = class _RouterBuilder {
195
- "~type" = "RouterBuilder";
196
- "~orpc";
197
- constructor(def) {
198
- this["~orpc"] = def;
199
- if (def.prefix && def.prefix.includes("{")) {
200
- throw new Error(`
201
- Dynamic routing in prefix not supported yet.
202
- Please remove "{" from "${def.prefix}".
203
- `);
204
- }
205
- }
206
- prefix(prefix) {
207
- return new _RouterBuilder({
69
+ meta(meta) {
70
+ return new _DecoratedProcedure({
208
71
  ...this["~orpc"],
209
- prefix: `${this["~orpc"].prefix ?? ""}${prefix}`
72
+ meta: mergeMeta(this["~orpc"].meta, meta)
210
73
  });
211
74
  }
212
- tag(...tags) {
213
- return new _RouterBuilder({
75
+ route(route) {
76
+ return new _DecoratedProcedure({
214
77
  ...this["~orpc"],
215
- tags: [...this["~orpc"].tags ?? [], ...tags]
78
+ route: mergeRoute(this["~orpc"].route, route)
216
79
  });
217
80
  }
218
- use(middleware) {
219
- return new _RouterBuilder({
81
+ use(middleware, mapInput) {
82
+ const mapped = mapInput ? decorateMiddleware(middleware).mapInput(mapInput) : middleware;
83
+ return new _DecoratedProcedure({
220
84
  ...this["~orpc"],
221
- middlewares: [...this["~orpc"].middlewares ?? [], middleware]
85
+ middlewares: addMiddleware(this["~orpc"].middlewares, mapped)
222
86
  });
223
87
  }
224
- router(router) {
225
- const adapted = adapt(router, this["~orpc"]);
226
- return adapted;
88
+ /**
89
+ * Make this procedure callable (works like a function while still being a procedure).
90
+ */
91
+ callable(...rest) {
92
+ return Object.assign(createProcedureClient(this, ...rest), {
93
+ "~type": "Procedure",
94
+ "~orpc": this["~orpc"]
95
+ });
227
96
  }
228
- lazy(loader) {
229
- const adapted = adapt(flatLazy(lazy(loader)), this["~orpc"]);
230
- return adapted;
97
+ /**
98
+ * Make this procedure compatible with server action (the same as .callable, but the type is compatible with server action).
99
+ */
100
+ actionable(...rest) {
101
+ return this.callable(...rest);
231
102
  }
232
103
  };
233
- function adapt(item, options) {
234
- if (isLazy(item)) {
235
- const adaptedLazy = decorateLazy(lazy(async () => {
236
- const routerOrProcedure = (await unlazy(item)).default;
237
- const adapted2 = adapt(routerOrProcedure, options);
238
- return { default: adapted2 };
239
- }));
240
- const lazyPrefix = getLazyRouterPrefix(item);
241
- if (options.prefix || lazyPrefix) {
242
- const prefixed = deepSetLazyRouterPrefix(adaptedLazy, `${options.prefix ?? ""}${lazyPrefix ?? ""}`);
243
- return prefixed;
244
- }
245
- return adaptedLazy;
246
- }
247
- if (isProcedure(item)) {
248
- let decorated = decorateProcedure(item);
249
- if (options.tags?.length) {
250
- decorated = decorated.unshiftTag(...options.tags);
251
- }
252
- if (options.prefix) {
253
- decorated = decorated.prefix(options.prefix);
254
- }
255
- if (options.middlewares?.length) {
256
- decorated = decorated.unshiftMiddleware(...options.middlewares);
257
- }
258
- return decorated;
259
- }
260
- const adapted = {};
261
- for (const key in item) {
262
- adapted[key] = adapt(item[key], options);
263
- }
264
- return adapted;
265
- }
266
104
 
267
- // src/router-implementer.ts
268
- var RouterImplementer = class _RouterImplementer {
269
- "~type" = "RouterImplementer";
105
+ // src/builder.ts
106
+ var Builder = class _Builder {
270
107
  "~orpc";
271
108
  constructor(def) {
272
109
  this["~orpc"] = def;
273
110
  }
274
- use(middleware) {
275
- return new _RouterImplementer({
111
+ /**
112
+ * Reset config
113
+ */
114
+ $config(config) {
115
+ const inputValidationCount = this["~orpc"].inputValidationIndex - fallbackConfig("initialInputValidationIndex", this["~orpc"].config.initialInputValidationIndex);
116
+ const outputValidationCount = this["~orpc"].outputValidationIndex - fallbackConfig("initialOutputValidationIndex", this["~orpc"].config.initialOutputValidationIndex);
117
+ return new _Builder({
276
118
  ...this["~orpc"],
277
- middlewares: [...this["~orpc"].middlewares ?? [], middleware]
119
+ config,
120
+ inputValidationIndex: fallbackConfig("initialInputValidationIndex", config.initialInputValidationIndex) + inputValidationCount,
121
+ outputValidationIndex: fallbackConfig("initialOutputValidationIndex", config.initialOutputValidationIndex) + outputValidationCount
278
122
  });
279
123
  }
280
- router(router) {
281
- const adapted = new RouterBuilder(this["~orpc"]).router(router);
282
- const contracted = setRouterContract(adapted, this["~orpc"].contract);
283
- return contracted;
284
- }
285
- lazy(loader) {
286
- const adapted = new RouterBuilder(this["~orpc"]).lazy(loader);
287
- const contracted = setRouterContract(adapted, this["~orpc"].contract);
288
- return contracted;
289
- }
290
- };
291
-
292
- // src/implementer-chainable.ts
293
- function createChainableImplementer(contract, middlewares) {
294
- if (isContractProcedure(contract)) {
295
- const implementer = new ProcedureImplementer({
296
- contract,
297
- middlewares
298
- });
299
- return implementer;
300
- }
301
- const chainable = {};
302
- for (const key in contract) {
303
- chainable[key] = createChainableImplementer(contract[key], middlewares);
304
- }
305
- const routerImplementer = new RouterImplementer({ contract, middlewares });
306
- const merged = new Proxy(chainable, {
307
- get(target, key) {
308
- const next = Reflect.get(target, key);
309
- const method = Reflect.get(routerImplementer, key);
310
- if (typeof key !== "string" || typeof method !== "function") {
311
- return next;
312
- }
313
- if (!next) {
314
- return method.bind(routerImplementer);
315
- }
316
- return createCallableObject(next, method.bind(routerImplementer));
317
- }
318
- });
319
- return merged;
320
- }
321
-
322
- // src/procedure-builder.ts
323
- import {
324
- DecoratedContractProcedure as DecoratedContractProcedure2
325
- } from "@orpc/contract";
326
- var ProcedureBuilder = class _ProcedureBuilder {
327
- "~type" = "ProcedureBuilder";
328
- "~orpc";
329
- constructor(def) {
330
- this["~orpc"] = def;
331
- }
332
- route(route) {
333
- return new _ProcedureBuilder({
124
+ /**
125
+ * Reset initial context
126
+ */
127
+ $context() {
128
+ return new _Builder({
334
129
  ...this["~orpc"],
335
- contract: DecoratedContractProcedure2.decorate(this["~orpc"].contract).route(route)
130
+ middlewares: [],
131
+ inputValidationIndex: fallbackConfig("initialInputValidationIndex", this["~orpc"].config.initialInputValidationIndex),
132
+ outputValidationIndex: fallbackConfig("initialOutputValidationIndex", this["~orpc"].config.initialOutputValidationIndex)
336
133
  });
337
134
  }
338
- input(schema, example) {
339
- return new _ProcedureBuilder({
135
+ /**
136
+ * Reset initial meta
137
+ */
138
+ $meta(initialMeta) {
139
+ return new _Builder({
340
140
  ...this["~orpc"],
341
- contract: DecoratedContractProcedure2.decorate(this["~orpc"].contract).input(schema, example)
141
+ meta: initialMeta
342
142
  });
343
143
  }
344
- output(schema, example) {
345
- return new _ProcedureBuilder({
144
+ /**
145
+ * Reset initial route
146
+ */
147
+ $route(initialRoute) {
148
+ return new _Builder({
346
149
  ...this["~orpc"],
347
- contract: DecoratedContractProcedure2.decorate(this["~orpc"].contract).output(schema, example)
150
+ route: initialRoute
348
151
  });
349
152
  }
153
+ middleware(middleware) {
154
+ return decorateMiddleware(middleware);
155
+ }
350
156
  errors(errors) {
351
- return new _ProcedureBuilder({
157
+ return new _Builder({
352
158
  ...this["~orpc"],
353
- contract: DecoratedContractProcedure2.decorate(this["~orpc"].contract).errors(errors)
159
+ errorMap: mergeErrorMap2(this["~orpc"].errorMap, errors)
354
160
  });
355
161
  }
356
162
  use(middleware, mapInput) {
357
- if (!mapInput) {
358
- return new ProcedureImplementer({
359
- contract: this["~orpc"].contract,
360
- middlewares: this["~orpc"].middlewares
361
- }).use(middleware);
362
- }
363
- return new ProcedureImplementer({
364
- contract: this["~orpc"].contract,
365
- middlewares: this["~orpc"].middlewares
366
- }).use(middleware, mapInput);
367
- }
368
- handler(handler) {
369
- return decorateProcedure(new Procedure({
370
- middlewares: this["~orpc"].middlewares,
371
- contract: this["~orpc"].contract,
372
- handler
373
- }));
374
- }
375
- };
376
-
377
- // src/builder.ts
378
- var Builder = class _Builder {
379
- "~type" = "Builder";
380
- "~orpc";
381
- constructor(def) {
382
- this["~orpc"] = def;
383
- }
384
- context() {
385
- return new _Builder({});
386
- }
387
- use(middleware) {
163
+ const mapped = mapInput ? decorateMiddleware(middleware).mapInput(mapInput) : middleware;
388
164
  return new _Builder({
389
165
  ...this["~orpc"],
390
- middlewares: [...this["~orpc"].middlewares ?? [], middleware]
166
+ middlewares: addMiddleware(this["~orpc"].middlewares, mapped)
391
167
  });
392
168
  }
393
- middleware(middleware) {
394
- return decorateMiddleware(middleware);
395
- }
396
- route(route) {
397
- return new ProcedureBuilder({
398
- middlewares: this["~orpc"].middlewares,
399
- contract: new ContractProcedure({
400
- route,
401
- InputSchema: void 0,
402
- OutputSchema: void 0,
403
- errorMap: void 0
404
- })
169
+ meta(meta) {
170
+ return new _Builder({
171
+ ...this["~orpc"],
172
+ meta: mergeMeta2(this["~orpc"].meta, meta)
405
173
  });
406
174
  }
407
- input(schema, example) {
408
- return new ProcedureBuilder({
409
- middlewares: this["~orpc"].middlewares,
410
- contract: new ContractProcedure({
411
- OutputSchema: void 0,
412
- InputSchema: schema,
413
- inputExample: example,
414
- errorMap: void 0
415
- })
175
+ route(route) {
176
+ return new _Builder({
177
+ ...this["~orpc"],
178
+ route: mergeRoute2(this["~orpc"].route, route)
416
179
  });
417
180
  }
418
- output(schema, example) {
419
- return new ProcedureBuilder({
420
- middlewares: this["~orpc"].middlewares,
421
- contract: new ContractProcedure({
422
- InputSchema: void 0,
423
- OutputSchema: schema,
424
- outputExample: example,
425
- errorMap: void 0
426
- })
181
+ input(schema) {
182
+ return new _Builder({
183
+ ...this["~orpc"],
184
+ inputSchema: schema,
185
+ inputValidationIndex: fallbackConfig("initialInputValidationIndex", this["~orpc"].config.initialInputValidationIndex) + this["~orpc"].middlewares.length
427
186
  });
428
187
  }
429
- errors(errors) {
430
- return new ProcedureBuilder({
431
- middlewares: this["~orpc"].middlewares,
432
- contract: new ContractProcedure({
433
- InputSchema: void 0,
434
- OutputSchema: void 0,
435
- errorMap: errors
436
- })
188
+ output(schema) {
189
+ return new _Builder({
190
+ ...this["~orpc"],
191
+ outputSchema: schema,
192
+ outputValidationIndex: fallbackConfig("initialOutputValidationIndex", this["~orpc"].config.initialOutputValidationIndex) + this["~orpc"].middlewares.length
437
193
  });
438
194
  }
439
195
  handler(handler) {
440
- return decorateProcedure(new Procedure({
441
- middlewares: this["~orpc"].middlewares,
442
- contract: new ContractProcedure({
443
- InputSchema: void 0,
444
- OutputSchema: void 0,
445
- errorMap: void 0
446
- }),
196
+ return new DecoratedProcedure({
197
+ ...this["~orpc"],
447
198
  handler
448
- }));
199
+ });
449
200
  }
450
201
  prefix(prefix) {
451
- return new RouterBuilder({
452
- middlewares: this["~orpc"].middlewares,
453
- prefix
202
+ return new _Builder({
203
+ ...this["~orpc"],
204
+ prefix: mergePrefix(this["~orpc"].prefix, prefix)
454
205
  });
455
206
  }
456
207
  tag(...tags) {
457
- return new RouterBuilder({
458
- middlewares: this["~orpc"].middlewares,
459
- tags
208
+ return new _Builder({
209
+ ...this["~orpc"],
210
+ tags: mergeTags(this["~orpc"].tags, tags)
460
211
  });
461
212
  }
462
213
  router(router) {
463
- return new RouterBuilder(this["~orpc"]).router(router);
214
+ return adaptRouter(router, this["~orpc"]);
464
215
  }
465
216
  lazy(loader) {
466
- return new RouterBuilder(this["~orpc"]).lazy(loader);
467
- }
468
- contract(contract) {
469
- return createChainableImplementer(contract, this["~orpc"].middlewares);
217
+ return adaptRouter(flatLazy(lazy(loader)), this["~orpc"]);
470
218
  }
471
219
  };
220
+ var os = new Builder({
221
+ config: {},
222
+ route: {},
223
+ meta: {},
224
+ errorMap: {},
225
+ inputSchema: void 0,
226
+ outputSchema: void 0,
227
+ inputValidationIndex: fallbackConfig("initialInputValidationIndex"),
228
+ outputValidationIndex: fallbackConfig("initialOutputValidationIndex"),
229
+ middlewares: []
230
+ });
472
231
 
473
- // src/router-client.ts
474
- function createRouterClient(options) {
475
- if (isProcedure(options.router)) {
476
- const caller = createProcedureClient({
477
- ...options,
478
- procedure: options.router,
479
- context: options.context,
480
- path: options.path
232
+ // src/context.ts
233
+ function mergeContext(context, other) {
234
+ return { ...context, ...other };
235
+ }
236
+
237
+ // src/implementer.ts
238
+ import { isContractProcedure } from "@orpc/contract";
239
+ function implementerInternal(contract, config, middlewares) {
240
+ if (isContractProcedure(contract)) {
241
+ const impl2 = new Builder({
242
+ ...contract["~orpc"],
243
+ config,
244
+ middlewares,
245
+ inputValidationIndex: fallbackConfig("initialInputValidationIndex", config?.initialInputValidationIndex) + middlewares.length,
246
+ outputValidationIndex: fallbackConfig("initialOutputValidationIndex", config?.initialOutputValidationIndex) + middlewares.length
481
247
  });
248
+ return impl2;
249
+ }
250
+ const impl = new Proxy(contract, {
251
+ get: (target, key) => {
252
+ let method;
253
+ if (key === "middleware") {
254
+ method = (mid) => decorateMiddleware(mid);
255
+ } else if (key === "use") {
256
+ method = (mid) => {
257
+ return implementerInternal(
258
+ contract,
259
+ config,
260
+ addMiddleware(middlewares, mid)
261
+ );
262
+ };
263
+ } else if (key === "router") {
264
+ method = (router) => {
265
+ const adapted = adaptRouter(router, {
266
+ middlewares,
267
+ errorMap: {}
268
+ });
269
+ return setRouterContract(adapted, contract);
270
+ };
271
+ } else if (key === "lazy") {
272
+ method = (loader) => {
273
+ const adapted = adaptRouter(flatLazy(lazy(loader)), {
274
+ middlewares,
275
+ errorMap: {}
276
+ });
277
+ return setRouterContract(adapted, contract);
278
+ };
279
+ }
280
+ const next = Reflect.get(target, key);
281
+ if (!next || typeof next !== "function" && typeof next !== "object") {
282
+ return method ?? next;
283
+ }
284
+ const nextImpl = implementerInternal(next, config, middlewares);
285
+ if (method) {
286
+ return new Proxy(method, {
287
+ get(_, key2) {
288
+ return Reflect.get(nextImpl, key2);
289
+ }
290
+ });
291
+ }
292
+ return nextImpl;
293
+ }
294
+ });
295
+ return impl;
296
+ }
297
+ function implement(contract, config = {}) {
298
+ const implInternal = implementerInternal(contract, config, []);
299
+ const impl = new Proxy(implInternal, {
300
+ get: (target, key) => {
301
+ let method;
302
+ if (key === "$context") {
303
+ method = () => impl;
304
+ } else if (key === "$config") {
305
+ method = (config2) => implement(contract, config2);
306
+ }
307
+ const next = Reflect.get(target, key);
308
+ if (!next || typeof next !== "function" && typeof next !== "object") {
309
+ return method ?? next;
310
+ }
311
+ if (method) {
312
+ return new Proxy(method, {
313
+ get(_, key2) {
314
+ return Reflect.get(next, key2);
315
+ }
316
+ });
317
+ }
318
+ return next;
319
+ }
320
+ });
321
+ return impl;
322
+ }
323
+
324
+ // src/procedure-utils.ts
325
+ function call(procedure, input, ...rest) {
326
+ return createProcedureClient(procedure, ...rest)(input);
327
+ }
328
+
329
+ // src/router-client.ts
330
+ function createRouterClient(router, ...rest) {
331
+ if (isProcedure(router)) {
332
+ const caller = createProcedureClient(router, ...rest);
482
333
  return caller;
483
334
  }
484
- const procedureCaller = isLazy(options.router) ? createProcedureClient({
485
- ...options,
486
- procedure: createLazyProcedureFormAnyLazy(options.router),
487
- context: options.context,
488
- path: options.path
489
- }) : {};
335
+ const procedureCaller = isLazy(router) ? createProcedureClient(createLazyProcedureFormAnyLazy(router), ...rest) : {};
490
336
  const recursive = new Proxy(procedureCaller, {
491
337
  get(target, key) {
492
338
  if (typeof key !== "string") {
493
339
  return Reflect.get(target, key);
494
340
  }
495
- const next = getRouterChild(options.router, key);
341
+ const next = getRouterChild(router, key);
496
342
  if (!next) {
497
343
  return Reflect.get(target, key);
498
344
  }
499
- return createRouterClient({
345
+ const [options] = rest;
346
+ return createRouterClient(next, {
500
347
  ...options,
501
- router: next,
502
- path: [...options.path ?? [], key]
348
+ path: [...options?.path ?? [], key]
503
349
  });
504
350
  }
505
351
  });
@@ -507,39 +353,39 @@ function createRouterClient(options) {
507
353
  }
508
354
 
509
355
  // src/index.ts
510
- import { configGlobal, fallbackToGlobalConfig, isDefinedError, ORPCError, safe } from "@orpc/contract";
511
- var os = new Builder({});
356
+ import { isDefinedError, ORPCError, safe, type, ValidationError } from "@orpc/contract";
512
357
  export {
513
358
  Builder,
359
+ DecoratedProcedure,
514
360
  LAZY_LOADER_SYMBOL,
515
361
  ORPCError,
516
362
  Procedure,
517
- ProcedureBuilder,
518
- ProcedureImplementer,
519
- RouterBuilder,
520
- RouterImplementer,
521
- configGlobal,
522
- createChainableImplementer,
523
- createORPCErrorConstructorMap,
363
+ ValidationError,
364
+ adaptRouter,
365
+ call,
366
+ createAccessibleLazyRouter,
367
+ createLazyProcedureFormAnyLazy,
524
368
  createProcedureClient,
525
369
  createRouterClient,
526
- decorateLazy,
527
370
  decorateMiddleware,
528
- decorateProcedure,
529
371
  deepSetLazyRouterPrefix,
530
- fallbackToGlobalConfig,
372
+ fallbackConfig,
531
373
  flatLazy,
532
374
  getLazyRouterPrefix,
533
375
  getRouterChild,
534
376
  getRouterContract,
377
+ implement,
378
+ implementerInternal,
535
379
  isDefinedError,
536
380
  isLazy,
537
381
  isProcedure,
538
382
  lazy,
539
383
  mergeContext,
384
+ middlewareOutputFn,
540
385
  os,
541
386
  safe,
542
387
  setRouterContract,
388
+ type,
543
389
  unlazy
544
390
  };
545
391
  //# sourceMappingURL=index.js.map