@orpc/server 0.0.0-next.aa72097 → 0.0.0-next.ad0709a

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