@orpc/server 0.0.0-next.4220427 → 0.0.0-next.430a0fe

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