@orpc/server 0.0.0-next.4220427 → 0.0.0-next.435356b

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