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

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