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

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