@orpc/server 0.0.0-next.316c163 → 0.0.0-next.32cb70c

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