@orpc/server 0.0.0-next.fd1db03 → 0.0.0-next.fea68c1

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 (71) hide show
  1. package/README.md +118 -0
  2. package/dist/chunk-MBMXGUNI.js +32 -0
  3. package/dist/chunk-MHVECKBC.js +421 -0
  4. package/dist/chunk-NTHCS5CK.js +182 -0
  5. package/dist/chunk-TXHKQO7N.js +120 -0
  6. package/dist/fetch.js +6 -320
  7. package/dist/hono.js +34 -0
  8. package/dist/index.js +280 -385
  9. package/dist/next.js +31 -0
  10. package/dist/node.js +31 -0
  11. package/dist/plugins.js +11 -0
  12. package/dist/src/adapters/fetch/index.d.ts +3 -0
  13. package/dist/src/adapters/fetch/rpc-handler.d.ts +11 -0
  14. package/dist/src/adapters/fetch/types.d.ts +14 -0
  15. package/dist/src/adapters/hono/index.d.ts +3 -0
  16. package/dist/src/adapters/hono/middleware.d.ts +12 -0
  17. package/dist/src/adapters/next/index.d.ts +3 -0
  18. package/dist/src/adapters/next/serve.d.ts +19 -0
  19. package/dist/src/adapters/node/index.d.ts +3 -0
  20. package/dist/src/adapters/node/rpc-handler.d.ts +11 -0
  21. package/dist/src/adapters/node/types.d.ts +22 -0
  22. package/dist/src/adapters/standard/handler.d.ts +53 -0
  23. package/dist/src/adapters/standard/index.d.ts +6 -0
  24. package/dist/src/adapters/standard/rpc-codec.d.ts +16 -0
  25. package/dist/src/adapters/standard/rpc-handler.d.ts +8 -0
  26. package/dist/src/adapters/standard/rpc-matcher.d.ts +10 -0
  27. package/dist/src/adapters/standard/types.d.ts +21 -0
  28. package/dist/src/builder-variants.d.ts +75 -0
  29. package/dist/src/builder.d.ts +53 -30
  30. package/dist/src/config.d.ts +6 -0
  31. package/dist/src/context.d.ts +8 -0
  32. package/dist/src/error.d.ts +12 -0
  33. package/dist/src/hidden.d.ts +6 -4
  34. package/dist/src/implementer-procedure.d.ts +33 -0
  35. package/dist/src/implementer-variants.d.ts +18 -0
  36. package/dist/src/implementer.d.ts +29 -0
  37. package/dist/src/index.d.ts +13 -11
  38. package/dist/src/lazy-utils.d.ts +4 -2
  39. package/dist/src/lazy.d.ts +9 -5
  40. package/dist/src/middleware-decorated.d.ts +8 -5
  41. package/dist/src/middleware-utils.d.ts +5 -0
  42. package/dist/src/middleware.d.ts +30 -15
  43. package/dist/src/plugins/base.d.ts +11 -0
  44. package/dist/src/plugins/cors.d.ts +19 -0
  45. package/dist/src/plugins/index.d.ts +4 -0
  46. package/dist/src/plugins/response-headers.d.ts +10 -0
  47. package/dist/src/procedure-client.d.ts +22 -23
  48. package/dist/src/procedure-decorated.d.ts +21 -11
  49. package/dist/src/procedure-utils.d.ts +19 -0
  50. package/dist/src/procedure.d.ts +26 -18
  51. package/dist/src/router-accessible-lazy.d.ts +8 -0
  52. package/dist/src/router-client.d.ts +8 -21
  53. package/dist/src/router.d.ts +26 -12
  54. package/dist/src/utils.d.ts +23 -2
  55. package/dist/standard.js +13 -0
  56. package/package.json +39 -6
  57. package/dist/chunk-6A7XHEBH.js +0 -189
  58. package/dist/src/fetch/composite-handler.d.ts +0 -8
  59. package/dist/src/fetch/index.d.ts +0 -7
  60. package/dist/src/fetch/orpc-handler.d.ts +0 -20
  61. package/dist/src/fetch/orpc-payload-codec.d.ts +0 -16
  62. package/dist/src/fetch/orpc-procedure-matcher.d.ts +0 -12
  63. package/dist/src/fetch/super-json.d.ts +0 -12
  64. package/dist/src/fetch/types.d.ts +0 -16
  65. package/dist/src/implementer-chainable.d.ts +0 -10
  66. package/dist/src/lazy-decorated.d.ts +0 -10
  67. package/dist/src/procedure-builder.d.ts +0 -22
  68. package/dist/src/procedure-implementer.d.ts +0 -18
  69. package/dist/src/router-builder.d.ts +0 -29
  70. package/dist/src/router-implementer.d.ts +0 -21
  71. package/dist/src/types.d.ts +0 -12
package/dist/index.js CHANGED
@@ -1,39 +1,63 @@
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-6A7XHEBH.js";
24
+ } from "./chunk-MHVECKBC.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) {
23
43
  const decorated = middleware;
24
44
  decorated.mapInput = (mapInput) => {
25
45
  const mapped = decorateMiddleware(
26
- (input, ...rest) => middleware(mapInput(input), ...rest)
46
+ (options, input, ...rest) => middleware(options, mapInput(input), ...rest)
27
47
  );
28
48
  return mapped;
29
49
  };
30
50
  decorated.concat = (concatMiddleware, mapInput) => {
31
51
  const mapped = mapInput ? decorateMiddleware(concatMiddleware).mapInput(mapInput) : concatMiddleware;
32
- const concatted = decorateMiddleware((input, context, meta, ...rest) => {
33
- const next = async (options) => {
34
- return mapped(input, mergeContext(context, options.context), meta, ...rest);
35
- };
36
- const merged = middleware(input, context, { ...meta, next }, ...rest);
52
+ const concatted = decorateMiddleware((options, 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);
37
61
  return merged;
38
62
  });
39
63
  return concatted;
@@ -42,443 +66,294 @@ function decorateMiddleware(middleware) {
42
66
  }
43
67
 
44
68
  // 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({
69
+ import { mergeErrorMap, mergeMeta, mergeRoute } from "@orpc/contract";
70
+ var DecoratedProcedure = class _DecoratedProcedure extends Procedure {
71
+ errors(errors) {
72
+ return new _DecoratedProcedure({
109
73
  ...this["~orpc"],
110
- middlewares: [...this["~orpc"].middlewares ?? [], mappedMiddleware]
74
+ errorMap: mergeErrorMap(this["~orpc"].errorMap, errors)
111
75
  });
112
76
  }
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({
77
+ meta(meta) {
78
+ return new _DecoratedProcedure({
207
79
  ...this["~orpc"],
208
- prefix: `${this["~orpc"].prefix ?? ""}${prefix}`
80
+ meta: mergeMeta(this["~orpc"].meta, meta)
209
81
  });
210
82
  }
211
- tag(...tags) {
212
- return new _RouterBuilder({
83
+ route(route) {
84
+ return new _DecoratedProcedure({
213
85
  ...this["~orpc"],
214
- tags: [...this["~orpc"].tags ?? [], ...tags]
86
+ route: mergeRoute(this["~orpc"].route, route)
215
87
  });
216
88
  }
217
- use(middleware) {
218
- return new _RouterBuilder({
89
+ use(middleware, mapInput) {
90
+ const mapped = mapInput ? decorateMiddleware(middleware).mapInput(mapInput) : middleware;
91
+ return new _DecoratedProcedure({
219
92
  ...this["~orpc"],
220
- middlewares: [...this["~orpc"].middlewares ?? [], middleware]
93
+ middlewares: addMiddleware(this["~orpc"].middlewares, mapped)
221
94
  });
222
95
  }
223
- router(router) {
224
- const adapted = adapt(router, this["~orpc"]);
225
- 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
+ });
226
104
  }
227
- lazy(loader) {
228
- const adapted = adapt(flatLazy(lazy(loader)), this["~orpc"]);
229
- 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);
230
110
  }
231
111
  };
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
112
 
266
- // src/router-implementer.ts
267
- var RouterImplementer = class _RouterImplementer {
268
- "~type" = "RouterImplementer";
113
+ // src/builder.ts
114
+ var Builder = class _Builder {
269
115
  "~orpc";
270
116
  constructor(def) {
271
117
  this["~orpc"] = def;
272
118
  }
273
- use(middleware) {
274
- 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({
275
126
  ...this["~orpc"],
276
- middlewares: [...this["~orpc"].middlewares ?? [], middleware]
127
+ config,
128
+ inputValidationIndex: fallbackConfig("initialInputValidationIndex", config.initialInputValidationIndex) + inputValidationCount,
129
+ outputValidationIndex: fallbackConfig("initialOutputValidationIndex", config.initialOutputValidationIndex) + outputValidationCount
277
130
  });
278
131
  }
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
132
+ /**
133
+ * Reset initial context
134
+ */
135
+ $context() {
136
+ return new _Builder({
137
+ ...this["~orpc"],
138
+ middlewares: [],
139
+ inputValidationIndex: fallbackConfig("initialInputValidationIndex", this["~orpc"].config.initialInputValidationIndex),
140
+ outputValidationIndex: fallbackConfig("initialOutputValidationIndex", this["~orpc"].config.initialOutputValidationIndex)
297
141
  });
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
142
  }
331
- route(route) {
332
- return new _ProcedureBuilder({
143
+ /**
144
+ * Reset initial meta
145
+ */
146
+ $meta(initialMeta) {
147
+ return new _Builder({
333
148
  ...this["~orpc"],
334
- contract: DecoratedContractProcedure2.decorate(this["~orpc"].contract).route(route)
149
+ meta: initialMeta
335
150
  });
336
151
  }
337
- input(schema, example) {
338
- return new _ProcedureBuilder({
152
+ /**
153
+ * Reset initial route
154
+ */
155
+ $route(initialRoute) {
156
+ return new _Builder({
339
157
  ...this["~orpc"],
340
- contract: DecoratedContractProcedure2.decorate(this["~orpc"].contract).input(schema, example)
158
+ route: initialRoute
341
159
  });
342
160
  }
343
- output(schema, example) {
344
- return new _ProcedureBuilder({
161
+ middleware(middleware) {
162
+ return decorateMiddleware(middleware);
163
+ }
164
+ errors(errors) {
165
+ return new _Builder({
345
166
  ...this["~orpc"],
346
- contract: DecoratedContractProcedure2.decorate(this["~orpc"].contract).output(schema, example)
167
+ errorMap: mergeErrorMap2(this["~orpc"].errorMap, errors)
347
168
  });
348
169
  }
349
170
  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) {
171
+ const mapped = mapInput ? decorateMiddleware(middleware).mapInput(mapInput) : middleware;
381
172
  return new _Builder({
382
173
  ...this["~orpc"],
383
- middlewares: [...this["~orpc"].middlewares ?? [], middleware]
174
+ middlewares: addMiddleware(this["~orpc"].middlewares, mapped)
384
175
  });
385
176
  }
386
- middleware(middleware) {
387
- return decorateMiddleware(middleware);
177
+ meta(meta) {
178
+ return new _Builder({
179
+ ...this["~orpc"],
180
+ meta: mergeMeta2(this["~orpc"].meta, meta)
181
+ });
388
182
  }
389
183
  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
- })
184
+ return new _Builder({
185
+ ...this["~orpc"],
186
+ route: mergeRoute2(this["~orpc"].route, route)
397
187
  });
398
188
  }
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
- })
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
407
194
  });
408
195
  }
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
- })
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
417
201
  });
418
202
  }
419
203
  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
- }),
204
+ return new DecoratedProcedure({
205
+ ...this["~orpc"],
426
206
  handler
427
- }));
207
+ });
428
208
  }
429
209
  prefix(prefix) {
430
- return new RouterBuilder({
431
- middlewares: this["~orpc"].middlewares,
432
- prefix
210
+ return new _Builder({
211
+ ...this["~orpc"],
212
+ prefix: mergePrefix(this["~orpc"].prefix, prefix)
433
213
  });
434
214
  }
435
215
  tag(...tags) {
436
- return new RouterBuilder({
437
- middlewares: this["~orpc"].middlewares,
438
- tags
216
+ return new _Builder({
217
+ ...this["~orpc"],
218
+ tags: mergeTags(this["~orpc"].tags, tags)
439
219
  });
440
220
  }
441
221
  router(router) {
442
- return new RouterBuilder(this["~orpc"]).router(router);
222
+ return adaptRouter(router, this["~orpc"]);
443
223
  }
444
224
  lazy(loader) {
445
- return new RouterBuilder(this["~orpc"]).lazy(loader);
446
- }
447
- contract(contract) {
448
- return createChainableImplementer(contract, this["~orpc"].middlewares);
225
+ return adaptRouter(flatLazy(lazy(loader)), this["~orpc"]);
449
226
  }
450
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
+ });
451
239
 
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
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
460
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);
461
341
  return caller;
462
342
  }
463
- const procedureCaller = isLazy(options.router) ? createProcedureClient({
464
- ...options,
465
- procedure: createLazyProcedureFormAnyLazy(options.router),
466
- context: options.context,
467
- path: options.path
468
- }) : {};
343
+ const procedureCaller = isLazy(router) ? createProcedureClient(createLazyProcedureFormAnyLazy(router), ...rest) : {};
469
344
  const recursive = new Proxy(procedureCaller, {
470
345
  get(target, key) {
471
346
  if (typeof key !== "string") {
472
347
  return Reflect.get(target, key);
473
348
  }
474
- const next = getRouterChild(options.router, key);
349
+ const next = getRouterChild(router, key);
475
350
  if (!next) {
476
351
  return Reflect.get(target, key);
477
352
  }
478
- return createRouterClient({
353
+ const [options] = rest;
354
+ return createRouterClient(next, {
479
355
  ...options,
480
- router: next,
481
- path: [...options.path ?? [], key]
356
+ path: [...options?.path ?? [], key]
482
357
  });
483
358
  }
484
359
  });
@@ -486,33 +361,53 @@ function createRouterClient(options) {
486
361
  }
487
362
 
488
363
  // src/index.ts
489
- export * from "@orpc/shared/error";
490
- var os = new Builder({});
364
+ import { isDefinedError, ORPCError, safe } from "@orpc/client";
365
+ import { eventIterator, type, ValidationError } from "@orpc/contract";
366
+ import { onError, onFinish, onStart, onSuccess } from "@orpc/shared";
367
+ import { getEventMeta, withEventMeta } from "@orpc/standard-server";
491
368
  export {
492
369
  Builder,
370
+ DecoratedProcedure,
493
371
  LAZY_LOADER_SYMBOL,
372
+ ORPCError,
494
373
  Procedure,
495
- ProcedureBuilder,
496
- ProcedureImplementer,
497
- RouterBuilder,
498
- RouterImplementer,
499
- createChainableImplementer,
374
+ ValidationError,
375
+ adaptRouter,
376
+ call,
377
+ convertPathToHttpPath,
378
+ createAccessibleLazyRouter,
379
+ createContractedProcedure,
380
+ createLazyProcedureFormAnyLazy,
500
381
  createProcedureClient,
501
382
  createRouterClient,
502
- decorateLazy,
503
383
  decorateMiddleware,
504
- decorateProcedure,
505
384
  deepSetLazyRouterPrefix,
385
+ eachAllContractProcedure,
386
+ eachContractProcedure,
387
+ eventIterator,
388
+ fallbackConfig,
506
389
  flatLazy,
390
+ getEventMeta,
507
391
  getLazyRouterPrefix,
508
392
  getRouterChild,
509
393
  getRouterContract,
394
+ implement,
395
+ implementerInternal,
396
+ isDefinedError,
510
397
  isLazy,
511
398
  isProcedure,
512
399
  lazy,
513
400
  mergeContext,
401
+ middlewareOutputFn,
402
+ onError,
403
+ onFinish,
404
+ onStart,
405
+ onSuccess,
514
406
  os,
407
+ safe,
515
408
  setRouterContract,
516
- unlazy
409
+ type,
410
+ unlazy,
411
+ withEventMeta
517
412
  };
518
413
  //# sourceMappingURL=index.js.map