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

Sign up to get free protection for your applications and to get access to all the features.
Files changed (63) 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 -103
  5. package/dist/hono.js +31 -0
  6. package/dist/index.js +313 -377
  7. package/dist/next.js +38 -0
  8. package/dist/node.js +173 -0
  9. package/dist/src/adapters/fetch/index.d.ts +4 -0
  10. package/dist/src/adapters/fetch/rpc-handler.d.ts +10 -0
  11. package/dist/src/adapters/fetch/types.d.ts +13 -0
  12. package/dist/src/adapters/fetch/utils.d.ts +6 -0
  13. package/dist/src/adapters/hono/index.d.ts +3 -0
  14. package/dist/src/adapters/hono/middleware.d.ts +13 -0
  15. package/dist/src/adapters/next/index.d.ts +3 -0
  16. package/dist/src/adapters/next/serve.d.ts +20 -0
  17. package/dist/src/adapters/node/index.d.ts +4 -0
  18. package/dist/src/adapters/node/rpc-handler.d.ts +10 -0
  19. package/dist/src/adapters/node/types.d.ts +21 -0
  20. package/dist/src/adapters/node/utils.d.ts +5 -0
  21. package/dist/src/adapters/standard/handler.d.ts +33 -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 +46 -42
  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 +8 -0
  33. package/dist/src/implementer-procedure.d.ts +30 -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 +16 -10
  37. package/dist/src/lazy-utils.d.ts +6 -0
  38. package/dist/src/lazy.d.ts +13 -14
  39. package/dist/src/middleware-decorated.d.ts +10 -0
  40. package/dist/src/middleware-utils.d.ts +5 -0
  41. package/dist/src/middleware.d.ts +28 -17
  42. package/dist/src/procedure-client.d.ts +20 -0
  43. package/dist/src/procedure-decorated.d.ts +21 -0
  44. package/dist/src/procedure-utils.d.ts +17 -0
  45. package/dist/src/procedure.d.ts +25 -28
  46. package/dist/src/router-accessible-lazy.d.ts +8 -0
  47. package/dist/src/router-client.d.ts +22 -0
  48. package/dist/src/router.d.ts +25 -17
  49. package/dist/src/utils.d.ts +23 -2
  50. package/dist/standard.js +16 -0
  51. package/package.json +29 -9
  52. package/dist/chunk-FL4ZAGNE.js +0 -267
  53. package/dist/src/fetch/handle.d.ts +0 -7
  54. package/dist/src/fetch/handler.d.ts +0 -3
  55. package/dist/src/fetch/index.d.ts +0 -4
  56. package/dist/src/fetch/types.d.ts +0 -35
  57. package/dist/src/procedure-builder.d.ts +0 -31
  58. package/dist/src/procedure-caller.d.ts +0 -20
  59. package/dist/src/procedure-implementer.d.ts +0 -22
  60. package/dist/src/router-builder.d.ts +0 -27
  61. package/dist/src/router-caller.d.ts +0 -22
  62. package/dist/src/router-implementer.d.ts +0 -24
  63. package/dist/src/types.d.ts +0 -8
package/dist/index.js CHANGED
@@ -1,436 +1,355 @@
1
1
  import {
2
2
  LAZY_LOADER_SYMBOL,
3
3
  Procedure,
4
- createFlattenLazy,
5
- createLazy,
6
- createProcedureCaller,
7
- decorateLazy,
8
- decorateMiddleware,
9
- decorateProcedure,
4
+ adaptRouter,
5
+ addMiddleware,
6
+ convertPathToHttpPath,
7
+ createAccessibleLazyRouter,
8
+ createContractedProcedure,
9
+ createLazyProcedureFormAnyLazy,
10
+ createProcedureClient,
11
+ deepSetLazyRouterPrefix,
12
+ eachAllContractProcedure,
13
+ eachContractProcedure,
14
+ flatLazy,
15
+ getLazyRouterPrefix,
16
+ getRouterChild,
17
+ getRouterContract,
10
18
  isLazy,
11
19
  isProcedure,
12
- loadLazy,
13
- mergeContext
14
- } from "./chunk-FL4ZAGNE.js";
20
+ lazy,
21
+ middlewareOutputFn,
22
+ setRouterContract,
23
+ unlazy
24
+ } from "./chunk-NOA3GBJQ.js";
15
25
 
16
26
  // src/builder.ts
17
- import {
18
- ContractProcedure,
19
- isContractProcedure as isContractProcedure2
20
- } from "@orpc/contract";
27
+ import { mergeErrorMap as mergeErrorMap2, mergeMeta as mergeMeta2, mergePrefix, mergeRoute as mergeRoute2, mergeTags } from "@orpc/contract";
21
28
 
22
- // src/procedure-builder.ts
23
- import {
24
- DecoratedContractProcedure as DecoratedContractProcedure2
25
- } from "@orpc/contract";
26
-
27
- // src/router-builder.ts
28
- import { DecoratedContractProcedure, prefixHTTPPath } from "@orpc/contract";
29
- var LAZY_ROUTER_PREFIX_SYMBOL = Symbol("ORPC_LAZY_ROUTER_PREFIX");
30
- var RouterBuilder = class _RouterBuilder {
31
- constructor(zz$rb) {
32
- this.zz$rb = zz$rb;
33
- if (zz$rb.prefix && zz$rb.prefix.includes("{")) {
34
- throw new Error('Prefix cannot contain "{" for dynamic routing');
35
- }
36
- }
37
- prefix(prefix) {
38
- return new _RouterBuilder({
39
- ...this.zz$rb,
40
- prefix: `${this.zz$rb.prefix ?? ""}${prefix}`
41
- });
42
- }
43
- tags(...tags) {
44
- if (!tags.length)
45
- return this;
46
- return new _RouterBuilder({
47
- ...this.zz$rb,
48
- tags: [...this.zz$rb.tags ?? [], ...tags]
49
- });
50
- }
51
- use(middleware, mapInput) {
52
- const middleware_ = mapInput ? decorateMiddleware(middleware).mapInput(mapInput) : middleware;
53
- return new _RouterBuilder({
54
- ...this.zz$rb,
55
- middlewares: [...this.zz$rb.middlewares || [], middleware_]
56
- });
57
- }
58
- router(router) {
59
- const handled = adaptRouter({
60
- routerOrChild: router,
61
- middlewares: this.zz$rb.middlewares,
62
- tags: this.zz$rb.tags,
63
- prefix: this.zz$rb.prefix
64
- });
65
- return handled;
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];
66
37
  }
67
- lazy(loader) {
68
- const lazy = adaptLazyRouter({
69
- current: createLazy(loader),
70
- middlewares: this.zz$rb.middlewares,
71
- tags: this.zz$rb.tags,
72
- prefix: this.zz$rb.prefix
38
+ return value;
39
+ }
40
+
41
+ // src/middleware-decorated.ts
42
+ function decorateMiddleware(middleware) {
43
+ const decorated = middleware;
44
+ decorated.mapInput = (mapInput) => {
45
+ const mapped = decorateMiddleware(
46
+ (options, input, ...rest) => middleware(options, mapInput(input), ...rest)
47
+ );
48
+ return mapped;
49
+ };
50
+ decorated.concat = (concatMiddleware, mapInput) => {
51
+ const mapped = mapInput ? decorateMiddleware(concatMiddleware).mapInput(mapInput) : concatMiddleware;
52
+ const concatted = decorateMiddleware((options, input, output, ...rest) => {
53
+ const next = async (...[nextOptions]) => {
54
+ return mapped({ ...options, context: { ...nextOptions?.context, ...options.context } }, input, output, ...rest);
55
+ };
56
+ const merged = middleware({ ...options, next }, input, output, ...rest);
57
+ return merged;
73
58
  });
74
- return lazy;
75
- }
76
- };
77
- function adaptRouter(options) {
78
- if (isProcedure(options.routerOrChild)) {
79
- return adaptProcedure({
80
- ...options,
81
- procedure: options.routerOrChild
59
+ return concatted;
60
+ };
61
+ return decorated;
62
+ }
63
+
64
+ // src/procedure-decorated.ts
65
+ import { mergeErrorMap, mergeMeta, mergeRoute } from "@orpc/contract";
66
+ var DecoratedProcedure = class _DecoratedProcedure extends Procedure {
67
+ errors(errors) {
68
+ return new _DecoratedProcedure({
69
+ ...this["~orpc"],
70
+ errorMap: mergeErrorMap(this["~orpc"].errorMap, errors)
82
71
  });
83
72
  }
84
- if (isLazy(options.routerOrChild)) {
85
- return adaptLazyRouter({
86
- ...options,
87
- current: options.routerOrChild
73
+ meta(meta) {
74
+ return new _DecoratedProcedure({
75
+ ...this["~orpc"],
76
+ meta: mergeMeta(this["~orpc"].meta, meta)
88
77
  });
89
78
  }
90
- const handled = {};
91
- for (const key in options.routerOrChild) {
92
- handled[key] = adaptRouter({
93
- ...options,
94
- routerOrChild: options.routerOrChild[key]
79
+ route(route) {
80
+ return new _DecoratedProcedure({
81
+ ...this["~orpc"],
82
+ route: mergeRoute(this["~orpc"].route, route)
95
83
  });
96
84
  }
97
- return handled;
98
- }
99
- function adaptLazyRouter(options) {
100
- const loader = async () => {
101
- const current = (await loadLazy(options.current)).default;
102
- return {
103
- default: adaptRouter({
104
- ...options,
105
- routerOrChild: current
106
- })
107
- };
108
- };
109
- let lazyRouterPrefix = options.prefix;
110
- if (LAZY_ROUTER_PREFIX_SYMBOL in options.current && typeof options.current[LAZY_ROUTER_PREFIX_SYMBOL] === "string") {
111
- lazyRouterPrefix = lazyRouterPrefix ? prefixHTTPPath(options.current[LAZY_ROUTER_PREFIX_SYMBOL], lazyRouterPrefix) : options.current[LAZY_ROUTER_PREFIX_SYMBOL];
112
- }
113
- const decoratedLazy = Object.assign(decorateLazy(createLazy(loader)), {
114
- [LAZY_ROUTER_PREFIX_SYMBOL]: lazyRouterPrefix
115
- });
116
- const recursive = new Proxy(decoratedLazy, {
117
- get(target, key) {
118
- if (typeof key !== "string") {
119
- return Reflect.get(target, key);
120
- }
121
- return adaptLazyRouter({
122
- ...options,
123
- current: createLazy(async () => {
124
- const current = (await loadLazy(options.current)).default;
125
- return { default: current[key] };
126
- })
127
- });
128
- }
129
- });
130
- return recursive;
131
- }
132
- function adaptProcedure(options) {
133
- const builderMiddlewares = options.middlewares ?? [];
134
- const procedureMiddlewares = options.procedure.zz$p.middlewares ?? [];
135
- const middlewares = [
136
- ...builderMiddlewares,
137
- ...procedureMiddlewares.filter(
138
- (item) => !builderMiddlewares.includes(item)
139
- )
140
- ];
141
- let contract = DecoratedContractProcedure.decorate(
142
- options.procedure.zz$p.contract
143
- ).addTags(...options.tags ?? []);
144
- if (options.prefix) {
145
- contract = contract.prefix(options.prefix);
146
- }
147
- return decorateProcedure({
148
- zz$p: {
149
- ...options.procedure.zz$p,
150
- contract,
151
- middlewares
152
- }
153
- });
154
- }
155
-
156
- // src/procedure-implementer.ts
157
- var ProcedureImplementer = class _ProcedureImplementer {
158
- constructor(zz$pi) {
159
- this.zz$pi = zz$pi;
160
- }
161
85
  use(middleware, mapInput) {
162
- const middleware_ = mapInput ? decorateMiddleware(middleware).mapInput(mapInput) : middleware;
163
- return new _ProcedureImplementer({
164
- ...this.zz$pi,
165
- middlewares: [...this.zz$pi.middlewares ?? [], middleware_]
86
+ const mapped = mapInput ? decorateMiddleware(middleware).mapInput(mapInput) : middleware;
87
+ return new _DecoratedProcedure({
88
+ ...this["~orpc"],
89
+ middlewares: addMiddleware(this["~orpc"].middlewares, mapped)
166
90
  });
167
91
  }
168
- func(func) {
169
- return decorateProcedure({
170
- zz$p: {
171
- middlewares: this.zz$pi.middlewares,
172
- contract: this.zz$pi.contract,
173
- func
174
- }
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"]
175
99
  });
176
100
  }
177
- lazy(loader) {
178
- return new RouterBuilder(this.zz$pi).lazy(loader);
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);
179
106
  }
180
107
  };
181
108
 
182
- // src/procedure-builder.ts
183
- var ProcedureBuilder = class _ProcedureBuilder {
184
- constructor(zz$pb) {
185
- this.zz$pb = zz$pb;
109
+ // src/builder.ts
110
+ var Builder = class _Builder {
111
+ "~orpc";
112
+ constructor(def) {
113
+ this["~orpc"] = def;
186
114
  }
187
115
  /**
188
- * Self chainable
116
+ * Reset config
189
117
  */
190
- route(opts) {
191
- return new _ProcedureBuilder({
192
- ...this.zz$pb,
193
- contract: DecoratedContractProcedure2.decorate(this.zz$pb.contract).route(
194
- opts
195
- )
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({
122
+ ...this["~orpc"],
123
+ config,
124
+ inputValidationIndex: fallbackConfig("initialInputValidationIndex", config.initialInputValidationIndex) + inputValidationCount,
125
+ outputValidationIndex: fallbackConfig("initialOutputValidationIndex", config.initialOutputValidationIndex) + outputValidationCount
196
126
  });
197
127
  }
198
- input(schema, example) {
199
- return new _ProcedureBuilder({
200
- ...this.zz$pb,
201
- contract: DecoratedContractProcedure2.decorate(this.zz$pb.contract).input(
202
- schema,
203
- example
204
- )
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)
205
137
  });
206
138
  }
207
- output(schema, example) {
208
- return new _ProcedureBuilder({
209
- ...this.zz$pb,
210
- contract: DecoratedContractProcedure2.decorate(this.zz$pb.contract).output(
211
- schema,
212
- example
213
- )
139
+ /**
140
+ * Reset initial meta
141
+ */
142
+ $meta(initialMeta) {
143
+ return new _Builder({
144
+ ...this["~orpc"],
145
+ meta: initialMeta
214
146
  });
215
147
  }
216
- use(middleware, mapInput) {
217
- if (!mapInput) {
218
- return new ProcedureImplementer({
219
- contract: this.zz$pb.contract,
220
- middlewares: this.zz$pb.middlewares
221
- }).use(middleware);
222
- }
223
- return new ProcedureImplementer({
224
- contract: this.zz$pb.contract,
225
- middlewares: this.zz$pb.middlewares
226
- }).use(middleware, mapInput);
227
- }
228
148
  /**
229
- * Convert to Procedure
149
+ * Reset initial route
230
150
  */
231
- func(func) {
232
- return decorateProcedure({
233
- zz$p: {
234
- middlewares: this.zz$pb.middlewares,
235
- contract: this.zz$pb.contract,
236
- func
237
- }
151
+ $route(initialRoute) {
152
+ return new _Builder({
153
+ ...this["~orpc"],
154
+ route: initialRoute
238
155
  });
239
156
  }
240
- };
241
-
242
- // src/router-implementer.ts
243
- import { isContractProcedure } from "@orpc/contract";
244
- var ROUTER_CONTRACT_SYMBOL = Symbol("ORPC_ROUTER_CONTRACT");
245
- var RouterImplementer = class {
246
- constructor(zz$ri) {
247
- this.zz$ri = zz$ri;
248
- }
249
- router(router) {
250
- return Object.assign(new RouterBuilder({}).router(router), {
251
- [ROUTER_CONTRACT_SYMBOL]: this.zz$ri.contract
252
- });
157
+ middleware(middleware) {
158
+ return decorateMiddleware(middleware);
253
159
  }
254
- lazy(loader) {
255
- const lazy = createLazy(loader);
256
- const decorated = decorateLazy(lazy);
257
- return Object.assign(decorated, {
258
- [ROUTER_CONTRACT_SYMBOL]: this.zz$ri.contract
160
+ errors(errors) {
161
+ return new _Builder({
162
+ ...this["~orpc"],
163
+ errorMap: mergeErrorMap2(this["~orpc"].errorMap, errors)
259
164
  });
260
165
  }
261
- };
262
- function chainRouterImplementer(contract, middlewares) {
263
- const result = {};
264
- for (const key in contract) {
265
- const item = contract[key];
266
- if (isContractProcedure(item)) {
267
- result[key] = new ProcedureImplementer({
268
- contract: item,
269
- middlewares
270
- });
271
- } else {
272
- result[key] = chainRouterImplementer(item, middlewares);
273
- }
274
- }
275
- const implementer = new RouterImplementer({ contract });
276
- return Object.assign(implementer, result);
277
- }
278
-
279
- // src/builder.ts
280
- var Builder = class _Builder {
281
- constructor(zz$b = {}) {
282
- this.zz$b = zz$b;
283
- }
284
- /**
285
- * Self chainable
286
- */
287
- context() {
288
- return this;
289
- }
290
166
  use(middleware, mapInput) {
291
- const middleware_ = mapInput ? decorateMiddleware(middleware).mapInput(mapInput) : middleware;
167
+ const mapped = mapInput ? decorateMiddleware(middleware).mapInput(mapInput) : middleware;
292
168
  return new _Builder({
293
- ...this.zz$b,
294
- middlewares: [...this.zz$b.middlewares || [], middleware_]
169
+ ...this["~orpc"],
170
+ middlewares: addMiddleware(this["~orpc"].middlewares, mapped)
295
171
  });
296
172
  }
297
- /**
298
- * Convert to ContractProcedureBuilder
299
- */
300
- route(opts) {
301
- return new ProcedureBuilder({
302
- middlewares: this.zz$b.middlewares,
303
- contract: new ContractProcedure({
304
- ...opts,
305
- InputSchema: void 0,
306
- OutputSchema: void 0
307
- })
173
+ meta(meta) {
174
+ return new _Builder({
175
+ ...this["~orpc"],
176
+ meta: mergeMeta2(this["~orpc"].meta, meta)
308
177
  });
309
178
  }
310
- input(schema, example) {
311
- return new ProcedureBuilder({
312
- middlewares: this.zz$b.middlewares,
313
- contract: new ContractProcedure({
314
- OutputSchema: void 0,
315
- InputSchema: schema,
316
- inputExample: example
317
- })
179
+ route(route) {
180
+ return new _Builder({
181
+ ...this["~orpc"],
182
+ route: mergeRoute2(this["~orpc"].route, route)
318
183
  });
319
184
  }
320
- output(schema, example) {
321
- return new ProcedureBuilder({
322
- middlewares: this.zz$b.middlewares,
323
- contract: new ContractProcedure({
324
- InputSchema: void 0,
325
- OutputSchema: schema,
326
- outputExample: example
327
- })
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
328
190
  });
329
191
  }
330
- /**
331
- * Convert to Procedure
332
- */
333
- func(func) {
334
- return decorateProcedure({
335
- zz$p: {
336
- middlewares: this.zz$b.middlewares,
337
- contract: new ContractProcedure({
338
- InputSchema: void 0,
339
- OutputSchema: void 0
340
- }),
341
- func
342
- }
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
343
197
  });
344
198
  }
345
- /**
346
- * Convert to ProcedureImplementer | RouterBuilder
347
- */
348
- contract(contract) {
349
- if (isContractProcedure2(contract)) {
350
- return new ProcedureImplementer({
351
- contract,
352
- middlewares: this.zz$b.middlewares
353
- });
354
- }
355
- return chainRouterImplementer(
356
- contract,
357
- this.zz$b.middlewares
358
- );
359
- }
360
- /**
361
- * Create ExtendedMiddleware
362
- */
363
- // TODO: TOutput always any, infer not work at all, because TOutput used inside middleware params,
364
- // solution (maybe): create new generic for .output() method
365
- middleware(middleware) {
366
- return decorateMiddleware(middleware);
199
+ handler(handler) {
200
+ return new DecoratedProcedure({
201
+ ...this["~orpc"],
202
+ handler
203
+ });
367
204
  }
368
205
  prefix(prefix) {
369
- return new RouterBuilder({
370
- ...this.zz$b,
371
- prefix
206
+ return new _Builder({
207
+ ...this["~orpc"],
208
+ prefix: mergePrefix(this["~orpc"].prefix, prefix)
372
209
  });
373
210
  }
374
- tags(...tags) {
375
- return new RouterBuilder({
376
- ...this.zz$b,
377
- tags
211
+ tag(...tags) {
212
+ return new _Builder({
213
+ ...this["~orpc"],
214
+ tags: mergeTags(this["~orpc"].tags, tags)
378
215
  });
379
216
  }
380
- /**
381
- * Create DecoratedRouter
382
- */
383
217
  router(router) {
384
- return new RouterBuilder(this.zz$b).router(router);
218
+ return adaptRouter(router, this["~orpc"]);
385
219
  }
386
220
  lazy(loader) {
387
- return new RouterBuilder(this.zz$b).lazy(loader);
221
+ return adaptRouter(flatLazy(lazy(loader)), this["~orpc"]);
388
222
  }
389
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
+ });
390
235
 
391
- // src/router.ts
392
- import {
393
- isContractProcedure as isContractProcedure3
394
- } from "@orpc/contract";
395
- function toContractRouter(router) {
396
- const contract = {};
397
- for (const key in router) {
398
- const item = router[key];
399
- if (isContractProcedure3(item)) {
400
- contract[key] = item;
401
- } else if (isProcedure(item)) {
402
- contract[key] = item.zz$p.contract;
403
- } else {
404
- contract[key] = toContractRouter(item);
405
- }
406
- }
407
- return contract;
236
+ // src/context.ts
237
+ function mergeContext(context, other) {
238
+ return { ...context, ...other };
408
239
  }
409
240
 
410
- // src/router-caller.ts
411
- function createRouterCaller(options) {
412
- return createRouterCallerInternal({
413
- current: options.router,
414
- context: options.context,
415
- path: options.basePath ?? []
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
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
+ }
416
298
  });
299
+ return impl;
417
300
  }
418
- function createRouterCallerInternal(options) {
419
- const procedureCaller = isLazy(options.current) || isProcedure(options.current) ? createProcedureCaller({
420
- procedure: options.current,
421
- context: options.context,
422
- path: options.path
423
- }) : {};
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);
337
+ return caller;
338
+ }
339
+ const procedureCaller = isLazy(router) ? createProcedureClient(createLazyProcedureFormAnyLazy(router), ...rest) : {};
424
340
  const recursive = new Proxy(procedureCaller, {
425
341
  get(target, key) {
426
342
  if (typeof key !== "string") {
427
343
  return Reflect.get(target, key);
428
344
  }
429
- const next = options.current[key];
430
- return createRouterCallerInternal({
431
- current: next,
432
- context: options.context,
433
- path: [...options.path, key]
345
+ const next = getRouterChild(router, key);
346
+ if (!next) {
347
+ return Reflect.get(target, key);
348
+ }
349
+ const [options] = rest;
350
+ return createRouterClient(next, {
351
+ ...options,
352
+ path: [...options?.path ?? [], key]
434
353
  });
435
354
  }
436
355
  });
@@ -438,31 +357,48 @@ function createRouterCallerInternal(options) {
438
357
  }
439
358
 
440
359
  // src/index.ts
441
- export * from "@orpc/shared/error";
442
- var os = new Builder();
360
+ import { isDefinedError, ORPCError, safe, type, ValidationError } from "@orpc/contract";
361
+ import { onError, onFinish, onStart, onSuccess } from "@orpc/shared";
443
362
  export {
444
363
  Builder,
364
+ DecoratedProcedure,
445
365
  LAZY_LOADER_SYMBOL,
446
- LAZY_ROUTER_PREFIX_SYMBOL,
366
+ ORPCError,
447
367
  Procedure,
448
- ProcedureBuilder,
449
- ProcedureImplementer,
450
- ROUTER_CONTRACT_SYMBOL,
451
- RouterBuilder,
452
- RouterImplementer,
453
- chainRouterImplementer,
454
- createFlattenLazy,
455
- createLazy,
456
- createProcedureCaller,
457
- createRouterCaller,
458
- decorateLazy,
368
+ ValidationError,
369
+ adaptRouter,
370
+ call,
371
+ convertPathToHttpPath,
372
+ createAccessibleLazyRouter,
373
+ createContractedProcedure,
374
+ createLazyProcedureFormAnyLazy,
375
+ createProcedureClient,
376
+ createRouterClient,
459
377
  decorateMiddleware,
460
- decorateProcedure,
378
+ deepSetLazyRouterPrefix,
379
+ eachAllContractProcedure,
380
+ eachContractProcedure,
381
+ fallbackConfig,
382
+ flatLazy,
383
+ getLazyRouterPrefix,
384
+ getRouterChild,
385
+ getRouterContract,
386
+ implement,
387
+ implementerInternal,
388
+ isDefinedError,
461
389
  isLazy,
462
390
  isProcedure,
463
- loadLazy,
391
+ lazy,
464
392
  mergeContext,
393
+ middlewareOutputFn,
394
+ onError,
395
+ onFinish,
396
+ onStart,
397
+ onSuccess,
465
398
  os,
466
- toContractRouter
399
+ safe,
400
+ setRouterContract,
401
+ type,
402
+ unlazy
467
403
  };
468
404
  //# sourceMappingURL=index.js.map