@orpc/server 0.0.0-next.c59d67c → 0.0.0-next.c6c659d

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 (51) hide show
  1. package/dist/chunk-MB74GD7C.js +301 -0
  2. package/dist/chunk-WUOGVGWG.js +1 -0
  3. package/dist/chunk-ZBJYYEII.js +227 -0
  4. package/dist/fetch.js +11 -102
  5. package/dist/hono.js +30 -0
  6. package/dist/index.js +430 -274
  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.d.ts +30 -43
  24. package/dist/src/error.d.ts +10 -0
  25. package/dist/src/hidden.d.ts +6 -0
  26. package/dist/src/implementer-chainable.d.ts +10 -0
  27. package/dist/src/index.d.ts +14 -4
  28. package/dist/src/lazy-decorated.d.ts +7 -0
  29. package/dist/src/lazy-utils.d.ts +4 -0
  30. package/dist/src/lazy.d.ts +18 -0
  31. package/dist/src/middleware-decorated.d.ts +9 -0
  32. package/dist/src/middleware.d.ts +21 -12
  33. package/dist/src/procedure-builder.d.ts +19 -26
  34. package/dist/src/procedure-client.d.ts +22 -0
  35. package/dist/src/procedure-decorated.d.ts +26 -0
  36. package/dist/src/procedure-implementer.d.ts +16 -15
  37. package/dist/src/procedure-utils.d.ts +17 -0
  38. package/dist/src/procedure.d.ts +39 -25
  39. package/dist/src/router-builder.d.ts +24 -17
  40. package/dist/src/router-client.d.ts +27 -0
  41. package/dist/src/router-implementer.d.ts +18 -17
  42. package/dist/src/router.d.ts +11 -15
  43. package/dist/src/types.d.ts +10 -4
  44. package/package.json +22 -11
  45. package/dist/chunk-TDFYNRZV.js +0 -190
  46. package/dist/src/fetch/handle.d.ts +0 -7
  47. package/dist/src/fetch/handler.d.ts +0 -3
  48. package/dist/src/fetch/index.d.ts +0 -4
  49. package/dist/src/fetch/types.d.ts +0 -35
  50. package/dist/src/procedure-caller.d.ts +0 -19
  51. package/dist/src/router-caller.d.ts +0 -22
package/dist/index.js CHANGED
@@ -1,394 +1,550 @@
1
1
  import {
2
+ LAZY_LOADER_SYMBOL,
2
3
  Procedure,
3
- createProcedureCaller,
4
- decorateMiddleware,
5
- decorateProcedure,
4
+ createORPCErrorConstructorMap,
5
+ createProcedureClient,
6
+ flatLazy,
7
+ getRouterChild,
8
+ isLazy,
6
9
  isProcedure,
7
- mergeContext
8
- } from "./chunk-TDFYNRZV.js";
10
+ lazy,
11
+ mergeContext,
12
+ unlazy
13
+ } from "./chunk-ZBJYYEII.js";
9
14
 
10
15
  // src/builder.ts
11
- import {
12
- ContractProcedure,
13
- isContractProcedure as isContractProcedure2
14
- } from "@orpc/contract";
16
+ import { ContractProcedure } from "@orpc/contract";
15
17
 
16
- // src/procedure-builder.ts
17
- import {
18
- DecoratedContractProcedure
19
- } from "@orpc/contract";
18
+ // src/implementer-chainable.ts
19
+ import { isContractProcedure } from "@orpc/contract";
20
+ import { createCallableObject as createCallableObject2 } from "@orpc/shared";
20
21
 
21
- // src/procedure-implementer.ts
22
- var ProcedureImplementer = class _ProcedureImplementer {
23
- constructor(zz$pi) {
24
- this.zz$pi = zz$pi;
22
+ // src/middleware-decorated.ts
23
+ function decorateMiddleware(middleware) {
24
+ const decorated = middleware;
25
+ decorated.mapInput = (mapInput) => {
26
+ const mapped = decorateMiddleware(
27
+ (options, input, ...rest) => middleware(options, mapInput(input), ...rest)
28
+ );
29
+ return mapped;
30
+ };
31
+ decorated.concat = (concatMiddleware, mapInput) => {
32
+ const mapped = mapInput ? decorateMiddleware(concatMiddleware).mapInput(mapInput) : concatMiddleware;
33
+ 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);
38
+ return merged;
39
+ });
40
+ return concatted;
41
+ };
42
+ return decorated;
43
+ }
44
+
45
+ // src/procedure-decorated.ts
46
+ import { DecoratedContractProcedure } from "@orpc/contract";
47
+ import { createCallableObject } from "@orpc/shared";
48
+ var DecoratedProcedure = class _DecoratedProcedure extends Procedure {
49
+ static decorate(procedure) {
50
+ if (procedure instanceof _DecoratedProcedure) {
51
+ return procedure;
52
+ }
53
+ return new _DecoratedProcedure(procedure["~orpc"]);
54
+ }
55
+ prefix(prefix) {
56
+ return new _DecoratedProcedure({
57
+ ...this["~orpc"],
58
+ contract: DecoratedContractProcedure.decorate(this["~orpc"].contract).prefix(prefix)
59
+ });
60
+ }
61
+ route(route) {
62
+ return new _DecoratedProcedure({
63
+ ...this["~orpc"],
64
+ contract: DecoratedContractProcedure.decorate(this["~orpc"].contract).route(route)
65
+ });
25
66
  }
26
67
  use(middleware, mapInput) {
27
68
  const middleware_ = mapInput ? decorateMiddleware(middleware).mapInput(mapInput) : middleware;
28
- return new _ProcedureImplementer({
29
- ...this.zz$pi,
30
- middlewares: [...this.zz$pi.middlewares ?? [], middleware_]
69
+ return new _DecoratedProcedure({
70
+ ...this["~orpc"],
71
+ middlewares: [...this["~orpc"].middlewares ?? [], middleware_]
31
72
  });
32
73
  }
33
- func(func) {
34
- return decorateProcedure({
35
- zz$p: {
36
- middlewares: this.zz$pi.middlewares,
37
- contract: this.zz$pi.contract,
38
- func
39
- }
74
+ unshiftTag(...tags) {
75
+ return new _DecoratedProcedure({
76
+ ...this["~orpc"],
77
+ contract: DecoratedContractProcedure.decorate(this["~orpc"].contract).unshiftTag(...tags)
40
78
  });
41
79
  }
42
- };
43
-
44
- // src/procedure-builder.ts
45
- var ProcedureBuilder = class _ProcedureBuilder {
46
- constructor(zz$pb) {
47
- this.zz$pb = zz$pb;
80
+ unshiftMiddleware(...middlewares) {
81
+ const castedMiddlewares = middlewares;
82
+ if (this["~orpc"].middlewares?.length) {
83
+ let min = 0;
84
+ for (let i = 0; i < this["~orpc"].middlewares.length; i++) {
85
+ const index = castedMiddlewares.indexOf(this["~orpc"].middlewares[i], min);
86
+ if (index === -1) {
87
+ castedMiddlewares.push(...this["~orpc"].middlewares.slice(i));
88
+ break;
89
+ }
90
+ min = index + 1;
91
+ }
92
+ }
93
+ return new _DecoratedProcedure({
94
+ ...this["~orpc"],
95
+ middlewares: castedMiddlewares
96
+ });
48
97
  }
49
98
  /**
50
- * Self chainable
99
+ * Make this procedure callable (works like a function while still being a procedure).
100
+ * **Note**: this only takes effect when this method is called at the end of the chain.
51
101
  */
52
- route(opts) {
53
- return new _ProcedureBuilder({
54
- ...this.zz$pb,
55
- contract: DecoratedContractProcedure.decorate(this.zz$pb.contract).route(
56
- opts
57
- )
58
- });
102
+ callable(...rest) {
103
+ return createCallableObject(this, createProcedureClient(this, ...rest));
59
104
  }
60
- input(schema, example) {
61
- return new _ProcedureBuilder({
62
- ...this.zz$pb,
63
- contract: DecoratedContractProcedure.decorate(this.zz$pb.contract).input(
64
- schema,
65
- example
66
- )
67
- });
105
+ /**
106
+ * Make this procedure compatible with server action (the same as .callable, but the type is compatible with server action).
107
+ * **Note**: this only takes effect when this method is called at the end of the chain.
108
+ */
109
+ actionable(...rest) {
110
+ return this.callable(...rest);
68
111
  }
69
- output(schema, example) {
70
- return new _ProcedureBuilder({
71
- ...this.zz$pb,
72
- contract: DecoratedContractProcedure.decorate(this.zz$pb.contract).output(
73
- schema,
74
- example
75
- )
76
- });
112
+ };
113
+
114
+ // src/procedure-implementer.ts
115
+ var ProcedureImplementer = class _ProcedureImplementer {
116
+ "~type" = "ProcedureImplementer";
117
+ "~orpc";
118
+ constructor(def) {
119
+ this["~orpc"] = def;
77
120
  }
78
121
  use(middleware, mapInput) {
79
- if (!mapInput) {
80
- return new ProcedureImplementer({
81
- contract: this.zz$pb.contract,
82
- middlewares: this.zz$pb.middlewares
83
- }).use(middleware);
84
- }
85
- return new ProcedureImplementer({
86
- contract: this.zz$pb.contract,
87
- middlewares: this.zz$pb.middlewares
88
- }).use(middleware, mapInput);
122
+ const mappedMiddleware = mapInput ? decorateMiddleware(middleware).mapInput(mapInput) : middleware;
123
+ return new _ProcedureImplementer({
124
+ ...this["~orpc"],
125
+ middlewares: [...this["~orpc"].middlewares ?? [], mappedMiddleware]
126
+ });
89
127
  }
90
- /**
91
- * Convert to Procedure
92
- */
93
- func(func) {
94
- return decorateProcedure({
95
- zz$p: {
96
- middlewares: this.zz$pb.middlewares,
97
- contract: this.zz$pb.contract,
98
- func
99
- }
128
+ handler(handler) {
129
+ return new DecoratedProcedure({
130
+ middlewares: this["~orpc"].middlewares,
131
+ contract: this["~orpc"].contract,
132
+ handler
100
133
  });
101
134
  }
102
135
  };
103
136
 
137
+ // src/hidden.ts
138
+ var ROUTER_CONTRACT_SYMBOL = Symbol("ORPC_ROUTER_CONTRACT");
139
+ function setRouterContract(obj, contract) {
140
+ return new Proxy(obj, {
141
+ get(target, key) {
142
+ if (key === ROUTER_CONTRACT_SYMBOL) {
143
+ return contract;
144
+ }
145
+ return Reflect.get(target, key);
146
+ }
147
+ });
148
+ }
149
+ function getRouterContract(obj) {
150
+ return obj[ROUTER_CONTRACT_SYMBOL];
151
+ }
152
+ var LAZY_ROUTER_PREFIX_SYMBOL = Symbol("ORPC_LAZY_ROUTER_PREFIX");
153
+ function deepSetLazyRouterPrefix(router, prefix) {
154
+ return new Proxy(router, {
155
+ get(target, key) {
156
+ if (key !== LAZY_ROUTER_PREFIX_SYMBOL) {
157
+ const val = Reflect.get(target, key);
158
+ if (val && (typeof val === "object" || typeof val === "function")) {
159
+ return deepSetLazyRouterPrefix(val, prefix);
160
+ }
161
+ return val;
162
+ }
163
+ return prefix;
164
+ }
165
+ });
166
+ }
167
+ function getLazyRouterPrefix(obj) {
168
+ return obj[LAZY_ROUTER_PREFIX_SYMBOL];
169
+ }
170
+
171
+ // src/lazy-decorated.ts
172
+ function decorateLazy(lazied) {
173
+ const flattenLazy = flatLazy(lazied);
174
+ const recursive = new Proxy(flattenLazy, {
175
+ get(target, key) {
176
+ if (typeof key !== "string") {
177
+ return Reflect.get(target, key);
178
+ }
179
+ const next = getRouterChild(flattenLazy, key);
180
+ return decorateLazy(next);
181
+ }
182
+ });
183
+ return recursive;
184
+ }
185
+
104
186
  // src/router-builder.ts
105
- import { DecoratedContractProcedure as DecoratedContractProcedure2 } from "@orpc/contract";
106
187
  var RouterBuilder = class _RouterBuilder {
107
- constructor(zz$rb) {
108
- this.zz$rb = zz$rb;
188
+ "~type" = "RouterBuilder";
189
+ "~orpc";
190
+ constructor(def) {
191
+ this["~orpc"] = def;
192
+ if (def.prefix && def.prefix.includes("{")) {
193
+ throw new Error(`
194
+ Dynamic routing in prefix not supported yet.
195
+ Please remove "{" from "${def.prefix}".
196
+ `);
197
+ }
109
198
  }
110
199
  prefix(prefix) {
111
200
  return new _RouterBuilder({
112
- ...this.zz$rb,
113
- prefix: `${this.zz$rb.prefix ?? ""}${prefix}`
201
+ ...this["~orpc"],
202
+ prefix: `${this["~orpc"].prefix ?? ""}${prefix}`
114
203
  });
115
204
  }
116
- tags(...tags) {
117
- if (!tags.length)
118
- return this;
205
+ tag(...tags) {
119
206
  return new _RouterBuilder({
120
- ...this.zz$rb,
121
- tags: [...this.zz$rb.tags ?? [], ...tags]
207
+ ...this["~orpc"],
208
+ tags: [...this["~orpc"].tags ?? [], ...tags]
122
209
  });
123
210
  }
124
- use(middleware, mapInput) {
125
- const middleware_ = mapInput ? decorateMiddleware(middleware).mapInput(mapInput) : middleware;
211
+ use(middleware) {
126
212
  return new _RouterBuilder({
127
- ...this.zz$rb,
128
- middlewares: [...this.zz$rb.middlewares || [], middleware_]
213
+ ...this["~orpc"],
214
+ middlewares: [...this["~orpc"].middlewares ?? [], middleware]
129
215
  });
130
216
  }
131
217
  router(router) {
132
- const handled = {};
133
- for (const key in router) {
134
- const item = router[key];
135
- if (isProcedure(item)) {
136
- const builderMiddlewares = this.zz$rb.middlewares ?? [];
137
- const itemMiddlewares = item.zz$p.middlewares ?? [];
138
- const middlewares = [
139
- ...builderMiddlewares,
140
- ...itemMiddlewares.filter(
141
- (item2) => !builderMiddlewares.includes(item2)
142
- )
143
- ];
144
- const contract = DecoratedContractProcedure2.decorate(
145
- item.zz$p.contract
146
- ).addTags(...this.zz$rb.tags ?? []);
147
- handled[key] = decorateProcedure({
148
- zz$p: {
149
- ...item.zz$p,
150
- contract: this.zz$rb.prefix ? contract.prefix(this.zz$rb.prefix) : contract,
151
- middlewares
152
- }
153
- });
154
- } else {
155
- handled[key] = this.router(item);
156
- }
157
- }
158
- return handled;
218
+ const adapted = adapt(router, this["~orpc"]);
219
+ return adapted;
220
+ }
221
+ lazy(loader) {
222
+ const adapted = adapt(flatLazy(lazy(loader)), this["~orpc"]);
223
+ return adapted;
159
224
  }
160
225
  };
226
+ function adapt(item, options) {
227
+ if (isLazy(item)) {
228
+ const adaptedLazy = decorateLazy(lazy(async () => {
229
+ const routerOrProcedure = (await unlazy(item)).default;
230
+ const adapted2 = adapt(routerOrProcedure, options);
231
+ return { default: adapted2 };
232
+ }));
233
+ const lazyPrefix = getLazyRouterPrefix(item);
234
+ if (options.prefix || lazyPrefix) {
235
+ const prefixed = deepSetLazyRouterPrefix(adaptedLazy, `${options.prefix ?? ""}${lazyPrefix ?? ""}`);
236
+ return prefixed;
237
+ }
238
+ return adaptedLazy;
239
+ }
240
+ if (isProcedure(item)) {
241
+ let decorated = DecoratedProcedure.decorate(item);
242
+ if (options.tags?.length) {
243
+ decorated = decorated.unshiftTag(...options.tags);
244
+ }
245
+ if (options.prefix) {
246
+ decorated = decorated.prefix(options.prefix);
247
+ }
248
+ if (options.middlewares?.length) {
249
+ decorated = decorated.unshiftMiddleware(...options.middlewares);
250
+ }
251
+ return decorated;
252
+ }
253
+ const adapted = {};
254
+ for (const key in item) {
255
+ adapted[key] = adapt(item[key], options);
256
+ }
257
+ return adapted;
258
+ }
161
259
 
162
260
  // src/router-implementer.ts
163
- import {
164
- isContractProcedure
165
- } from "@orpc/contract";
166
- var RouterImplementer = class {
167
- constructor(zz$ri) {
168
- this.zz$ri = zz$ri;
261
+ var RouterImplementer = class _RouterImplementer {
262
+ "~type" = "RouterImplementer";
263
+ "~orpc";
264
+ constructor(def) {
265
+ this["~orpc"] = def;
266
+ }
267
+ use(middleware) {
268
+ return new _RouterImplementer({
269
+ ...this["~orpc"],
270
+ middlewares: [...this["~orpc"].middlewares ?? [], middleware]
271
+ });
169
272
  }
170
273
  router(router) {
171
- assertRouterImplementation(this.zz$ri.contract, router);
172
- return router;
274
+ const adapted = new RouterBuilder(this["~orpc"]).router(router);
275
+ const contracted = setRouterContract(adapted, this["~orpc"].contract);
276
+ return contracted;
277
+ }
278
+ lazy(loader) {
279
+ const adapted = new RouterBuilder(this["~orpc"]).lazy(loader);
280
+ const contracted = setRouterContract(adapted, this["~orpc"].contract);
281
+ return contracted;
173
282
  }
174
283
  };
175
- function chainRouterImplementer(contract, middlewares) {
176
- const result = {};
177
- for (const key in contract) {
178
- const item = contract[key];
179
- if (isContractProcedure(item)) {
180
- result[key] = new ProcedureImplementer({
181
- contract: item,
182
- middlewares
183
- });
184
- } else {
185
- result[key] = chainRouterImplementer(item, middlewares);
186
- }
284
+
285
+ // src/implementer-chainable.ts
286
+ function createChainableImplementer(contract, middlewares) {
287
+ if (isContractProcedure(contract)) {
288
+ const implementer = new ProcedureImplementer({
289
+ contract,
290
+ middlewares
291
+ });
292
+ return implementer;
187
293
  }
188
- const implementer = new RouterImplementer({ contract });
189
- return Object.assign(implementer, result);
190
- }
191
- function assertRouterImplementation(contract, router, path = []) {
294
+ const chainable = {};
192
295
  for (const key in contract) {
193
- const currentPath = [...path, key];
194
- const contractItem = contract[key];
195
- const routerItem = router[key];
196
- if (!routerItem) {
197
- throw new Error(
198
- `Missing implementation for procedure at [${currentPath.join(".")}]`
199
- );
200
- }
201
- if (isContractProcedure(contractItem)) {
202
- if (isProcedure(routerItem)) {
203
- if (routerItem.zz$p.contract !== contractItem) {
204
- throw new Error(
205
- `Mismatch implementation for procedure at [${currentPath.join(".")}]`
206
- );
207
- }
208
- } else {
209
- throw new Error(
210
- `Mismatch implementation for procedure at [${currentPath.join(".")}]`
211
- );
296
+ chainable[key] = createChainableImplementer(contract[key], middlewares);
297
+ }
298
+ const routerImplementer = new RouterImplementer({ contract, middlewares });
299
+ const merged = new Proxy(chainable, {
300
+ get(target, key) {
301
+ const next = Reflect.get(target, key);
302
+ const method = Reflect.get(routerImplementer, key);
303
+ if (typeof key !== "string" || typeof method !== "function") {
304
+ return next;
212
305
  }
213
- } else {
214
- assertRouterImplementation(
215
- contractItem,
216
- routerItem,
217
- currentPath
218
- );
306
+ if (!next) {
307
+ return method.bind(routerImplementer);
308
+ }
309
+ return createCallableObject2(next, method.bind(routerImplementer));
219
310
  }
220
- }
311
+ });
312
+ return merged;
221
313
  }
222
314
 
315
+ // src/procedure-builder.ts
316
+ import {
317
+ DecoratedContractProcedure as DecoratedContractProcedure2
318
+ } from "@orpc/contract";
319
+ var ProcedureBuilder = class _ProcedureBuilder {
320
+ "~type" = "ProcedureBuilder";
321
+ "~orpc";
322
+ constructor(def) {
323
+ this["~orpc"] = def;
324
+ }
325
+ route(route) {
326
+ return new _ProcedureBuilder({
327
+ ...this["~orpc"],
328
+ contract: DecoratedContractProcedure2.decorate(this["~orpc"].contract).route(route)
329
+ });
330
+ }
331
+ input(schema, example) {
332
+ return new _ProcedureBuilder({
333
+ ...this["~orpc"],
334
+ contract: DecoratedContractProcedure2.decorate(this["~orpc"].contract).input(schema, example)
335
+ });
336
+ }
337
+ output(schema, example) {
338
+ return new _ProcedureBuilder({
339
+ ...this["~orpc"],
340
+ contract: DecoratedContractProcedure2.decorate(this["~orpc"].contract).output(schema, example)
341
+ });
342
+ }
343
+ errors(errors) {
344
+ return new _ProcedureBuilder({
345
+ ...this["~orpc"],
346
+ contract: DecoratedContractProcedure2.decorate(this["~orpc"].contract).errors(errors)
347
+ });
348
+ }
349
+ 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 new DecoratedProcedure({
363
+ middlewares: this["~orpc"].middlewares,
364
+ contract: this["~orpc"].contract,
365
+ handler
366
+ });
367
+ }
368
+ };
369
+
223
370
  // src/builder.ts
224
371
  var Builder = class _Builder {
225
- constructor(zz$b = {}) {
226
- this.zz$b = zz$b;
372
+ "~type" = "Builder";
373
+ "~orpc";
374
+ constructor(def) {
375
+ this["~orpc"] = def;
227
376
  }
228
- /**
229
- * Self chainable
230
- */
231
377
  context() {
232
- return this;
378
+ return new _Builder({});
233
379
  }
234
- use(middleware, mapInput) {
235
- const middleware_ = mapInput ? decorateMiddleware(middleware).mapInput(mapInput) : middleware;
380
+ use(middleware) {
236
381
  return new _Builder({
237
- ...this.zz$b,
238
- middlewares: [...this.zz$b.middlewares || [], middleware_]
382
+ ...this["~orpc"],
383
+ middlewares: [...this["~orpc"].middlewares ?? [], middleware]
239
384
  });
240
385
  }
241
- /**
242
- * Convert to ContractProcedureBuilder
243
- */
244
- route(opts) {
386
+ middleware(middleware) {
387
+ return decorateMiddleware(middleware);
388
+ }
389
+ route(route) {
245
390
  return new ProcedureBuilder({
246
- middlewares: this.zz$b.middlewares,
391
+ middlewares: this["~orpc"].middlewares,
247
392
  contract: new ContractProcedure({
248
- ...opts,
393
+ route,
249
394
  InputSchema: void 0,
250
- OutputSchema: void 0
395
+ OutputSchema: void 0,
396
+ errorMap: void 0
251
397
  })
252
398
  });
253
399
  }
254
400
  input(schema, example) {
255
401
  return new ProcedureBuilder({
256
- middlewares: this.zz$b.middlewares,
402
+ middlewares: this["~orpc"].middlewares,
257
403
  contract: new ContractProcedure({
258
404
  OutputSchema: void 0,
259
405
  InputSchema: schema,
260
- inputExample: example
406
+ inputExample: example,
407
+ errorMap: void 0
261
408
  })
262
409
  });
263
410
  }
264
411
  output(schema, example) {
265
412
  return new ProcedureBuilder({
266
- middlewares: this.zz$b.middlewares,
413
+ middlewares: this["~orpc"].middlewares,
267
414
  contract: new ContractProcedure({
268
415
  InputSchema: void 0,
269
416
  OutputSchema: schema,
270
- outputExample: example
417
+ outputExample: example,
418
+ errorMap: void 0
271
419
  })
272
420
  });
273
421
  }
274
- /**
275
- * Convert to Procedure
276
- */
277
- func(func) {
278
- return decorateProcedure({
279
- zz$p: {
280
- middlewares: this.zz$b.middlewares,
281
- contract: new ContractProcedure({
282
- InputSchema: void 0,
283
- OutputSchema: void 0
284
- }),
285
- func
286
- }
422
+ errors(errors) {
423
+ return new ProcedureBuilder({
424
+ middlewares: this["~orpc"].middlewares,
425
+ contract: new ContractProcedure({
426
+ InputSchema: void 0,
427
+ OutputSchema: void 0,
428
+ errorMap: errors
429
+ })
287
430
  });
288
431
  }
289
- /**
290
- * Convert to ProcedureImplementer | RouterBuilder
291
- */
292
- contract(contract) {
293
- if (isContractProcedure2(contract)) {
294
- return new ProcedureImplementer({
295
- contract,
296
- middlewares: this.zz$b.middlewares
297
- });
298
- }
299
- return chainRouterImplementer(
300
- contract,
301
- this.zz$b.middlewares
302
- );
303
- }
304
- /**
305
- * Create ExtendedMiddleware
306
- */
307
- // TODO: TOutput always any, infer not work at all, because TOutput used inside middleware params,
308
- // solution (maybe): create new generic for .output() method
309
- middleware(middleware) {
310
- return decorateMiddleware(middleware);
432
+ handler(handler) {
433
+ return new DecoratedProcedure({
434
+ middlewares: this["~orpc"].middlewares,
435
+ contract: new ContractProcedure({
436
+ InputSchema: void 0,
437
+ OutputSchema: void 0,
438
+ errorMap: void 0
439
+ }),
440
+ handler
441
+ });
311
442
  }
312
443
  prefix(prefix) {
313
444
  return new RouterBuilder({
314
- ...this.zz$b,
445
+ middlewares: this["~orpc"].middlewares,
315
446
  prefix
316
447
  });
317
448
  }
318
- tags(...tags) {
449
+ tag(...tags) {
319
450
  return new RouterBuilder({
320
- ...this.zz$b,
451
+ middlewares: this["~orpc"].middlewares,
321
452
  tags
322
453
  });
323
454
  }
324
- /**
325
- * Create DecoratedRouter
326
- */
327
455
  router(router) {
328
- return new RouterBuilder(this.zz$b).router(router);
456
+ return new RouterBuilder(this["~orpc"]).router(router);
457
+ }
458
+ lazy(loader) {
459
+ return new RouterBuilder(this["~orpc"]).lazy(loader);
460
+ }
461
+ contract(contract) {
462
+ return createChainableImplementer(contract, this["~orpc"].middlewares);
329
463
  }
330
464
  };
331
465
 
332
- // src/router.ts
333
- import {
334
- isContractProcedure as isContractProcedure3
335
- } from "@orpc/contract";
336
- function toContractRouter(router) {
337
- const contract = {};
338
- for (const key in router) {
339
- const item = router[key];
340
- if (isContractProcedure3(item)) {
341
- contract[key] = item;
342
- } else if (isProcedure(item)) {
343
- contract[key] = item.zz$p.contract;
344
- } else {
345
- contract[key] = toContractRouter(item);
466
+ // src/procedure-utils.ts
467
+ function call(procedure, input, ...rest) {
468
+ return createProcedureClient(procedure, ...rest)(input);
469
+ }
470
+
471
+ // src/lazy-utils.ts
472
+ function createLazyProcedureFormAnyLazy(lazied) {
473
+ const lazyProcedure = lazy(async () => {
474
+ const { default: maybeProcedure } = await unlazy(flatLazy(lazied));
475
+ if (!isProcedure(maybeProcedure)) {
476
+ throw new Error(`
477
+ Expected a lazy<procedure> but got lazy<unknown>.
478
+ This should be caught by TypeScript compilation.
479
+ Please report this issue if this makes you feel uncomfortable.
480
+ `);
346
481
  }
347
- }
348
- return contract;
482
+ return { default: maybeProcedure };
483
+ });
484
+ return lazyProcedure;
349
485
  }
350
486
 
351
- // src/router-caller.ts
352
- function createRouterCaller(options) {
353
- const caller = {};
354
- for (const key in options.router) {
355
- const path = [...options.basePath ?? [], key];
356
- const item = options.router[key];
357
- if (isProcedure(item)) {
358
- caller[key] = createProcedureCaller({
359
- procedure: item,
360
- context: options.context,
361
- path
362
- });
363
- } else {
364
- caller[key] = createRouterCaller({
365
- router: item,
366
- context: options.context,
367
- basePath: path
487
+ // src/router-client.ts
488
+ function createRouterClient(router, ...rest) {
489
+ if (isProcedure(router)) {
490
+ const caller = createProcedureClient(router, ...rest);
491
+ return caller;
492
+ }
493
+ const procedureCaller = isLazy(router) ? createProcedureClient(createLazyProcedureFormAnyLazy(router), ...rest) : {};
494
+ const recursive = new Proxy(procedureCaller, {
495
+ get(target, key) {
496
+ if (typeof key !== "string") {
497
+ return Reflect.get(target, key);
498
+ }
499
+ const next = getRouterChild(router, key);
500
+ if (!next) {
501
+ return Reflect.get(target, key);
502
+ }
503
+ const [options] = rest;
504
+ return createRouterClient(next, {
505
+ ...options,
506
+ path: [...options?.path ?? [], key]
368
507
  });
369
508
  }
370
- }
371
- return caller;
509
+ });
510
+ return recursive;
372
511
  }
373
512
 
374
513
  // src/index.ts
375
- export * from "@orpc/shared/error";
376
- var os = new Builder();
514
+ import { configGlobal, fallbackToGlobalConfig, isDefinedError, ORPCError, safe } from "@orpc/contract";
515
+ var os = new Builder({});
377
516
  export {
378
517
  Builder,
518
+ DecoratedProcedure,
519
+ LAZY_LOADER_SYMBOL,
520
+ ORPCError,
379
521
  Procedure,
380
522
  ProcedureBuilder,
381
523
  ProcedureImplementer,
524
+ RouterBuilder,
382
525
  RouterImplementer,
383
- assertRouterImplementation,
384
- chainRouterImplementer,
385
- createProcedureCaller,
386
- createRouterCaller,
526
+ call,
527
+ configGlobal,
528
+ createChainableImplementer,
529
+ createORPCErrorConstructorMap,
530
+ createProcedureClient,
531
+ createRouterClient,
532
+ decorateLazy,
387
533
  decorateMiddleware,
388
- decorateProcedure,
534
+ deepSetLazyRouterPrefix,
535
+ fallbackToGlobalConfig,
536
+ flatLazy,
537
+ getLazyRouterPrefix,
538
+ getRouterChild,
539
+ getRouterContract,
540
+ isDefinedError,
541
+ isLazy,
389
542
  isProcedure,
543
+ lazy,
390
544
  mergeContext,
391
545
  os,
392
- toContractRouter
546
+ safe,
547
+ setRouterContract,
548
+ unlazy
393
549
  };
394
550
  //# sourceMappingURL=index.js.map