@orpc/server 0.0.0-next.ef3ba82 → 0.0.0-next.f22c7ec

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