@orpc/server 0.0.0-next.44bdf93 → 0.0.0-next.47e44e3

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 (75) hide show
  1. package/README.md +118 -0
  2. package/dist/chunk-7A2ZRAPK.js +182 -0
  3. package/dist/chunk-MEBJNUSY.js +32 -0
  4. package/dist/chunk-MHVECKBC.js +421 -0
  5. package/dist/chunk-WQNNSBXW.js +120 -0
  6. package/dist/fetch.js +6 -11
  7. package/dist/hono.js +18 -14
  8. package/dist/index.js +216 -790
  9. package/dist/next.js +6 -11
  10. package/dist/node.js +19 -75
  11. package/dist/plugins.js +11 -0
  12. package/dist/src/adapters/fetch/index.d.ts +1 -4
  13. package/dist/src/adapters/fetch/rpc-handler.d.ts +11 -0
  14. package/dist/src/adapters/fetch/types.d.ts +4 -11
  15. package/dist/src/adapters/hono/middleware.d.ts +6 -6
  16. package/dist/src/adapters/next/serve.d.ts +11 -11
  17. package/dist/src/adapters/node/index.d.ts +1 -3
  18. package/dist/src/adapters/node/rpc-handler.d.ts +11 -0
  19. package/dist/src/adapters/node/types.d.ts +15 -15
  20. package/dist/src/adapters/standard/handler.d.ts +53 -0
  21. package/dist/src/adapters/standard/index.d.ts +6 -0
  22. package/dist/src/adapters/standard/rpc-codec.d.ts +16 -0
  23. package/dist/src/adapters/standard/rpc-handler.d.ts +8 -0
  24. package/dist/src/adapters/standard/rpc-matcher.d.ts +10 -0
  25. package/dist/src/adapters/standard/types.d.ts +21 -0
  26. package/dist/src/builder-variants.d.ts +75 -0
  27. package/dist/src/builder.d.ts +51 -36
  28. package/dist/src/config.d.ts +6 -0
  29. package/dist/src/context.d.ts +7 -10
  30. package/dist/src/error.d.ts +8 -6
  31. package/dist/src/hidden.d.ts +5 -3
  32. package/dist/src/implementer-procedure.d.ts +33 -0
  33. package/dist/src/implementer-variants.d.ts +18 -0
  34. package/dist/src/implementer.d.ts +29 -0
  35. package/dist/src/index.d.ts +12 -12
  36. package/dist/src/lazy-utils.d.ts +4 -2
  37. package/dist/src/lazy.d.ts +9 -5
  38. package/dist/src/middleware-decorated.d.ts +7 -6
  39. package/dist/src/middleware-utils.d.ts +5 -0
  40. package/dist/src/middleware.d.ts +23 -21
  41. package/dist/src/plugins/base.d.ts +11 -0
  42. package/dist/src/plugins/cors.d.ts +19 -0
  43. package/dist/src/plugins/index.d.ts +4 -0
  44. package/dist/src/plugins/response-headers.d.ts +10 -0
  45. package/dist/src/procedure-client.d.ts +23 -12
  46. package/dist/src/procedure-decorated.d.ts +13 -15
  47. package/dist/src/procedure-utils.d.ts +6 -4
  48. package/dist/src/procedure.d.ts +18 -32
  49. package/dist/src/router-accessible-lazy.d.ts +8 -0
  50. package/dist/src/router-client.d.ts +8 -22
  51. package/dist/src/router.d.ts +26 -12
  52. package/dist/src/utils.d.ts +23 -2
  53. package/dist/standard.js +13 -0
  54. package/package.json +20 -3
  55. package/dist/chunk-GK2Z6B6W.js +0 -230
  56. package/dist/chunk-SXUFCJBY.js +0 -301
  57. package/dist/chunk-WUOGVGWG.js +0 -1
  58. package/dist/src/adapters/fetch/orpc-handler.d.ts +0 -20
  59. package/dist/src/adapters/fetch/orpc-payload-codec.d.ts +0 -16
  60. package/dist/src/adapters/fetch/orpc-procedure-matcher.d.ts +0 -12
  61. package/dist/src/adapters/fetch/super-json.d.ts +0 -12
  62. package/dist/src/adapters/node/orpc-handler.d.ts +0 -12
  63. package/dist/src/adapters/node/request-listener.d.ts +0 -28
  64. package/dist/src/builder-with-errors-middlewares.d.ts +0 -50
  65. package/dist/src/builder-with-errors.d.ts +0 -49
  66. package/dist/src/builder-with-middlewares.d.ts +0 -47
  67. package/dist/src/implementer-chainable.d.ts +0 -14
  68. package/dist/src/lazy-decorated.d.ts +0 -7
  69. package/dist/src/procedure-builder-with-input.d.ts +0 -34
  70. package/dist/src/procedure-builder-with-output.d.ts +0 -33
  71. package/dist/src/procedure-builder.d.ts +0 -27
  72. package/dist/src/procedure-implementer.d.ts +0 -22
  73. package/dist/src/router-builder.d.ts +0 -33
  74. package/dist/src/router-implementer.d.ts +0 -22
  75. package/dist/src/types.d.ts +0 -14
package/dist/index.js CHANGED
@@ -1,32 +1,42 @@
1
1
  import {
2
2
  LAZY_LOADER_SYMBOL,
3
3
  Procedure,
4
- createORPCErrorConstructorMap,
4
+ adaptRouter,
5
+ addMiddleware,
6
+ convertPathToHttpPath,
7
+ createAccessibleLazyRouter,
8
+ createContractedProcedure,
9
+ createLazyProcedureFormAnyLazy,
5
10
  createProcedureClient,
11
+ deepSetLazyRouterPrefix,
12
+ eachAllContractProcedure,
13
+ eachContractProcedure,
6
14
  flatLazy,
15
+ getLazyRouterPrefix,
7
16
  getRouterChild,
17
+ getRouterContract,
8
18
  isLazy,
9
19
  isProcedure,
10
20
  lazy,
11
- mergeContext,
12
21
  middlewareOutputFn,
22
+ setRouterContract,
13
23
  unlazy
14
- } from "./chunk-GK2Z6B6W.js";
24
+ } from "./chunk-MHVECKBC.js";
15
25
 
16
26
  // src/builder.ts
17
- import { ContractProcedure as ContractProcedure4 } from "@orpc/contract";
18
-
19
- // src/builder-with-errors.ts
20
- import { ContractProcedure as ContractProcedure2 } from "@orpc/contract";
27
+ import { mergeErrorMap as mergeErrorMap2, mergeMeta as mergeMeta2, mergePrefix, mergeRoute as mergeRoute2, mergeTags } from "@orpc/contract";
21
28
 
22
- // src/builder-with-errors-middlewares.ts
23
- import { ContractProcedure } from "@orpc/contract";
24
-
25
- // src/procedure-builder.ts
26
- import { ContractProcedureBuilder, DecoratedContractProcedure as DecoratedContractProcedure4 } from "@orpc/contract";
27
-
28
- // src/procedure-builder-with-input.ts
29
- import { ContractProcedureBuilderWithInput, DecoratedContractProcedure as DecoratedContractProcedure2 } from "@orpc/contract";
29
+ // src/config.ts
30
+ var DEFAULT_CONFIG = {
31
+ initialInputValidationIndex: 0,
32
+ initialOutputValidationIndex: 0
33
+ };
34
+ function fallbackConfig(key, value) {
35
+ if (value === void 0) {
36
+ return DEFAULT_CONFIG[key];
37
+ }
38
+ return value;
39
+ }
30
40
 
31
41
  // src/middleware-decorated.ts
32
42
  function decorateMiddleware(middleware) {
@@ -40,10 +50,14 @@ function decorateMiddleware(middleware) {
40
50
  decorated.concat = (concatMiddleware, mapInput) => {
41
51
  const mapped = mapInput ? decorateMiddleware(concatMiddleware).mapInput(mapInput) : concatMiddleware;
42
52
  const concatted = decorateMiddleware((options, input, output, ...rest) => {
43
- const next = async (nextOptions) => {
44
- return mapped({ ...options, context: mergeContext(nextOptions.context, options.context) }, input, output, ...rest);
45
- };
46
- const merged = middleware({ ...options, next }, input, output, ...rest);
53
+ const merged = middleware({
54
+ ...options,
55
+ next: (...[nextOptions1]) => mapped({
56
+ ...options,
57
+ context: { ...options.context, ...nextOptions1?.context },
58
+ next: (...[nextOptions2]) => options.next({ context: { ...nextOptions1?.context, ...nextOptions2?.context } })
59
+ }, input, output, ...rest)
60
+ }, input, output, ...rest);
47
61
  return merged;
48
62
  });
49
63
  return concatted;
@@ -52,64 +66,31 @@ function decorateMiddleware(middleware) {
52
66
  }
53
67
 
54
68
  // src/procedure-decorated.ts
55
- import { DecoratedContractProcedure } from "@orpc/contract";
69
+ import { mergeErrorMap, mergeMeta, mergeRoute } from "@orpc/contract";
56
70
  var DecoratedProcedure = class _DecoratedProcedure extends Procedure {
57
- static decorate(procedure) {
58
- if (procedure instanceof _DecoratedProcedure) {
59
- return procedure;
60
- }
61
- return new _DecoratedProcedure(procedure["~orpc"]);
62
- }
63
- prefix(prefix) {
64
- return new _DecoratedProcedure({
65
- ...this["~orpc"],
66
- contract: DecoratedContractProcedure.decorate(this["~orpc"].contract).prefix(prefix)
67
- });
68
- }
69
- route(route) {
70
- return new _DecoratedProcedure({
71
- ...this["~orpc"],
72
- contract: DecoratedContractProcedure.decorate(this["~orpc"].contract).route(route)
73
- });
74
- }
75
71
  errors(errors) {
76
72
  return new _DecoratedProcedure({
77
73
  ...this["~orpc"],
78
- contract: DecoratedContractProcedure.decorate(this["~orpc"].contract).errors(errors)
74
+ errorMap: mergeErrorMap(this["~orpc"].errorMap, errors)
79
75
  });
80
76
  }
81
- use(middleware, mapInput) {
82
- const middleware_ = mapInput ? decorateMiddleware(middleware).mapInput(mapInput) : middleware;
77
+ meta(meta) {
83
78
  return new _DecoratedProcedure({
84
79
  ...this["~orpc"],
85
- middlewares: [...this["~orpc"].middlewares, middleware_]
80
+ meta: mergeMeta(this["~orpc"].meta, meta)
86
81
  });
87
82
  }
88
- unshiftTag(...tags) {
83
+ route(route) {
89
84
  return new _DecoratedProcedure({
90
85
  ...this["~orpc"],
91
- contract: DecoratedContractProcedure.decorate(this["~orpc"].contract).unshiftTag(...tags)
86
+ route: mergeRoute(this["~orpc"].route, route)
92
87
  });
93
88
  }
94
- unshiftMiddleware(...middlewares) {
95
- const castedMiddlewares = middlewares;
96
- if (this["~orpc"].middlewares.length) {
97
- let min = 0;
98
- for (let i = 0; i < this["~orpc"].middlewares.length; i++) {
99
- const index = castedMiddlewares.indexOf(this["~orpc"].middlewares[i], min);
100
- if (index === -1) {
101
- castedMiddlewares.push(...this["~orpc"].middlewares.slice(i));
102
- break;
103
- }
104
- min = index + 1;
105
- }
106
- }
107
- const numNewMiddlewares = castedMiddlewares.length - this["~orpc"].middlewares.length;
89
+ use(middleware, mapInput) {
90
+ const mapped = mapInput ? decorateMiddleware(middleware).mapInput(mapInput) : middleware;
108
91
  return new _DecoratedProcedure({
109
92
  ...this["~orpc"],
110
- inputValidationIndex: this["~orpc"].inputValidationIndex + numNewMiddlewares,
111
- outputValidationIndex: this["~orpc"].outputValidationIndex + numNewMiddlewares,
112
- middlewares: castedMiddlewares
93
+ middlewares: addMiddleware(this["~orpc"].middlewares, mapped)
113
94
  });
114
95
  }
115
96
  /**
@@ -129,796 +110,230 @@ var DecoratedProcedure = class _DecoratedProcedure extends Procedure {
129
110
  }
130
111
  };
131
112
 
132
- // src/procedure-implementer.ts
133
- var ProcedureImplementer = class _ProcedureImplementer {
134
- "~type" = "ProcedureImplementer";
135
- "~orpc";
136
- constructor(def) {
137
- this["~orpc"] = def;
138
- }
139
- use(middleware, mapInput) {
140
- const mappedMiddleware = mapInput ? decorateMiddleware(middleware).mapInput(mapInput) : middleware;
141
- return new _ProcedureImplementer({
142
- ...this["~orpc"],
143
- middlewares: [...this["~orpc"].middlewares, mappedMiddleware]
144
- });
145
- }
146
- handler(handler) {
147
- return new DecoratedProcedure({
148
- ...this["~orpc"],
149
- handler
150
- });
151
- }
152
- };
153
-
154
- // src/procedure-builder-with-input.ts
155
- var ProcedureBuilderWithInput = class _ProcedureBuilderWithInput {
156
- "~type" = "ProcedureBuilderWithInput";
157
- "~orpc";
158
- constructor(def) {
159
- this["~orpc"] = def;
160
- }
161
- errors(errors) {
162
- return new _ProcedureBuilderWithInput({
163
- ...this["~orpc"],
164
- contract: DecoratedContractProcedure2.decorate(this["~orpc"].contract).errors(errors)
165
- });
166
- }
167
- route(route) {
168
- return new _ProcedureBuilderWithInput({
169
- ...this["~orpc"],
170
- contract: DecoratedContractProcedure2.decorate(this["~orpc"].contract).route(route)
171
- });
172
- }
173
- use(middleware, mapInput) {
174
- const maybeWithMapInput = mapInput ? decorateMiddleware(middleware).mapInput(mapInput) : middleware;
175
- return new _ProcedureBuilderWithInput({
176
- ...this["~orpc"],
177
- outputValidationIndex: this["~orpc"].outputValidationIndex + 1,
178
- middlewares: [...this["~orpc"].middlewares, maybeWithMapInput]
179
- });
180
- }
181
- output(schema, example) {
182
- return new ProcedureImplementer({
183
- ...this["~orpc"],
184
- contract: new ContractProcedureBuilderWithInput(this["~orpc"].contract["~orpc"]).output(schema, example)
185
- });
186
- }
187
- handler(handler) {
188
- return new DecoratedProcedure({
189
- ...this["~orpc"],
190
- handler
191
- });
192
- }
193
- };
194
-
195
- // src/procedure-builder-with-output.ts
196
- import { ContractProcedureBuilderWithOutput, DecoratedContractProcedure as DecoratedContractProcedure3 } from "@orpc/contract";
197
- var ProcedureBuilderWithOutput = class _ProcedureBuilderWithOutput {
198
- "~type" = "ProcedureBuilderWithOutput";
199
- "~orpc";
200
- constructor(def) {
201
- this["~orpc"] = def;
202
- }
203
- errors(errors) {
204
- return new _ProcedureBuilderWithOutput({
205
- ...this["~orpc"],
206
- contract: DecoratedContractProcedure3.decorate(this["~orpc"].contract).errors(errors)
207
- });
208
- }
209
- route(route) {
210
- return new _ProcedureBuilderWithOutput({
211
- ...this["~orpc"],
212
- contract: DecoratedContractProcedure3.decorate(this["~orpc"].contract).route(route)
213
- });
214
- }
215
- use(middleware) {
216
- return new _ProcedureBuilderWithOutput({
217
- ...this["~orpc"],
218
- inputValidationIndex: this["~orpc"].inputValidationIndex + 1,
219
- middlewares: [...this["~orpc"].middlewares, middleware]
220
- });
221
- }
222
- input(schema, example) {
223
- return new ProcedureImplementer({
224
- ...this["~orpc"],
225
- contract: new ContractProcedureBuilderWithOutput(this["~orpc"].contract["~orpc"]).input(schema, example)
226
- });
227
- }
228
- handler(handler) {
229
- return new DecoratedProcedure({
230
- ...this["~orpc"],
231
- handler
232
- });
233
- }
234
- };
235
-
236
- // src/procedure-builder.ts
237
- var ProcedureBuilder = class _ProcedureBuilder {
238
- "~type" = "ProcedureBuilder";
113
+ // src/builder.ts
114
+ var Builder = class _Builder {
239
115
  "~orpc";
240
116
  constructor(def) {
241
117
  this["~orpc"] = def;
242
118
  }
243
- errors(errors) {
244
- return new _ProcedureBuilder({
245
- ...this["~orpc"],
246
- contract: DecoratedContractProcedure4.decorate(this["~orpc"].contract).errors(errors)
247
- });
248
- }
249
- route(route) {
250
- return new _ProcedureBuilder({
251
- ...this["~orpc"],
252
- contract: DecoratedContractProcedure4.decorate(this["~orpc"].contract).route(route)
253
- });
254
- }
255
- use(middleware) {
256
- return new _ProcedureBuilder({
257
- ...this["~orpc"],
258
- inputValidationIndex: this["~orpc"].inputValidationIndex + 1,
259
- outputValidationIndex: this["~orpc"].outputValidationIndex + 1,
260
- middlewares: [...this["~orpc"].middlewares, middleware]
261
- });
262
- }
263
- input(schema, example) {
264
- return new ProcedureBuilderWithInput({
119
+ /**
120
+ * Reset config
121
+ */
122
+ $config(config) {
123
+ const inputValidationCount = this["~orpc"].inputValidationIndex - fallbackConfig("initialInputValidationIndex", this["~orpc"].config.initialInputValidationIndex);
124
+ const outputValidationCount = this["~orpc"].outputValidationIndex - fallbackConfig("initialOutputValidationIndex", this["~orpc"].config.initialOutputValidationIndex);
125
+ return new _Builder({
265
126
  ...this["~orpc"],
266
- contract: new ContractProcedureBuilder(this["~orpc"].contract["~orpc"]).input(schema, example)
127
+ config,
128
+ inputValidationIndex: fallbackConfig("initialInputValidationIndex", config.initialInputValidationIndex) + inputValidationCount,
129
+ outputValidationIndex: fallbackConfig("initialOutputValidationIndex", config.initialOutputValidationIndex) + outputValidationCount
267
130
  });
268
131
  }
269
- output(schema, example) {
270
- return new ProcedureBuilderWithOutput({
132
+ /**
133
+ * Reset initial context
134
+ */
135
+ $context() {
136
+ return new _Builder({
271
137
  ...this["~orpc"],
272
- contract: new ContractProcedureBuilder(this["~orpc"].contract["~orpc"]).output(schema, example)
138
+ middlewares: [],
139
+ inputValidationIndex: fallbackConfig("initialInputValidationIndex", this["~orpc"].config.initialInputValidationIndex),
140
+ outputValidationIndex: fallbackConfig("initialOutputValidationIndex", this["~orpc"].config.initialOutputValidationIndex)
273
141
  });
274
142
  }
275
- handler(handler) {
276
- return new DecoratedProcedure({
143
+ /**
144
+ * Reset initial meta
145
+ */
146
+ $meta(initialMeta) {
147
+ return new _Builder({
277
148
  ...this["~orpc"],
278
- handler
149
+ meta: initialMeta
279
150
  });
280
151
  }
281
- };
282
-
283
- // src/hidden.ts
284
- var ROUTER_CONTRACT_SYMBOL = Symbol("ORPC_ROUTER_CONTRACT");
285
- function setRouterContract(obj, contract) {
286
- return new Proxy(obj, {
287
- get(target, key) {
288
- if (key === ROUTER_CONTRACT_SYMBOL) {
289
- return contract;
290
- }
291
- return Reflect.get(target, key);
292
- }
293
- });
294
- }
295
- function getRouterContract(obj) {
296
- return obj[ROUTER_CONTRACT_SYMBOL];
297
- }
298
- var LAZY_ROUTER_PREFIX_SYMBOL = Symbol("ORPC_LAZY_ROUTER_PREFIX");
299
- function deepSetLazyRouterPrefix(router, prefix) {
300
- return new Proxy(router, {
301
- get(target, key) {
302
- if (key !== LAZY_ROUTER_PREFIX_SYMBOL) {
303
- const val = Reflect.get(target, key);
304
- if (val && (typeof val === "object" || typeof val === "function")) {
305
- return deepSetLazyRouterPrefix(val, prefix);
306
- }
307
- return val;
308
- }
309
- return prefix;
310
- }
311
- });
312
- }
313
- function getLazyRouterPrefix(obj) {
314
- return obj[LAZY_ROUTER_PREFIX_SYMBOL];
315
- }
316
-
317
- // src/lazy-decorated.ts
318
- function decorateLazy(lazied) {
319
- const flattenLazy = flatLazy(lazied);
320
- const recursive = new Proxy(flattenLazy, {
321
- get(target, key) {
322
- if (typeof key !== "string") {
323
- return Reflect.get(target, key);
324
- }
325
- const next = getRouterChild(flattenLazy, key);
326
- return decorateLazy(next);
327
- }
328
- });
329
- return recursive;
330
- }
331
-
332
- // src/router-builder.ts
333
- var RouterBuilder = class _RouterBuilder {
334
- "~type" = "RouterBuilder";
335
- "~orpc";
336
- constructor(def) {
337
- this["~orpc"] = def;
338
- if (def.prefix && def.prefix.includes("{")) {
339
- throw new Error(`
340
- Dynamic routing in prefix not supported yet.
341
- Please remove "{" from "${def.prefix}".
342
- `);
343
- }
344
- }
345
- prefix(prefix) {
346
- return new _RouterBuilder({
152
+ /**
153
+ * Reset initial route
154
+ */
155
+ $route(initialRoute) {
156
+ return new _Builder({
347
157
  ...this["~orpc"],
348
- prefix: `${this["~orpc"].prefix ?? ""}${prefix}`
158
+ route: initialRoute
349
159
  });
350
160
  }
351
- tag(...tags) {
352
- return new _RouterBuilder({
353
- ...this["~orpc"],
354
- tags: [...this["~orpc"].tags ?? [], ...tags]
355
- });
161
+ middleware(middleware) {
162
+ return decorateMiddleware(middleware);
356
163
  }
357
164
  errors(errors) {
358
- return new _RouterBuilder({
359
- ...this["~orpc"],
360
- errorMap: {
361
- ...this["~orpc"].errorMap,
362
- ...errors
363
- }
364
- });
365
- }
366
- use(middleware) {
367
- return new _RouterBuilder({
165
+ return new _Builder({
368
166
  ...this["~orpc"],
369
- middlewares: [...this["~orpc"].middlewares, middleware]
167
+ errorMap: mergeErrorMap2(this["~orpc"].errorMap, errors)
370
168
  });
371
169
  }
372
- router(router) {
373
- const adapted = adapt(router, this["~orpc"]);
374
- return adapted;
375
- }
376
- lazy(loader) {
377
- const adapted = adapt(flatLazy(lazy(loader)), this["~orpc"]);
378
- return adapted;
379
- }
380
- };
381
- function adapt(item, options) {
382
- if (isLazy(item)) {
383
- const adaptedLazy = decorateLazy(lazy(async () => {
384
- const routerOrProcedure = (await unlazy(item)).default;
385
- const adapted2 = adapt(routerOrProcedure, options);
386
- return { default: adapted2 };
387
- }));
388
- const lazyPrefix = getLazyRouterPrefix(item);
389
- if (options.prefix || lazyPrefix) {
390
- const prefixed = deepSetLazyRouterPrefix(adaptedLazy, `${options.prefix ?? ""}${lazyPrefix ?? ""}`);
391
- return prefixed;
392
- }
393
- return adaptedLazy;
394
- }
395
- if (isProcedure(item)) {
396
- let decorated = DecoratedProcedure.decorate(item);
397
- if (options.tags?.length) {
398
- decorated = decorated.unshiftTag(...options.tags);
399
- }
400
- if (options.prefix) {
401
- decorated = decorated.prefix(options.prefix);
402
- }
403
- if (options.middlewares?.length) {
404
- decorated = decorated.unshiftMiddleware(...options.middlewares);
405
- }
406
- if (Object.keys(options.errorMap).length) {
407
- decorated = decorated.errors(options.errorMap);
408
- }
409
- return decorated;
410
- }
411
- const adapted = {};
412
- for (const key in item) {
413
- adapted[key] = adapt(item[key], options);
414
- }
415
- return adapted;
416
- }
417
-
418
- // src/builder-with-errors-middlewares.ts
419
- var BuilderWithErrorsMiddlewares = class _BuilderWithErrorsMiddlewares {
420
- "~type" = "BuilderWithErrorsMiddlewares";
421
- "~orpc";
422
- constructor(def) {
423
- this["~orpc"] = def;
424
- }
425
- errors(errors) {
426
- return new _BuilderWithErrorsMiddlewares({
170
+ use(middleware, mapInput) {
171
+ const mapped = mapInput ? decorateMiddleware(middleware).mapInput(mapInput) : middleware;
172
+ return new _Builder({
427
173
  ...this["~orpc"],
428
- errorMap: {
429
- ...this["~orpc"].errorMap,
430
- ...errors
431
- }
174
+ middlewares: addMiddleware(this["~orpc"].middlewares, mapped)
432
175
  });
433
176
  }
434
- use(middleware) {
435
- return new _BuilderWithErrorsMiddlewares({
177
+ meta(meta) {
178
+ return new _Builder({
436
179
  ...this["~orpc"],
437
- inputValidationIndex: this["~orpc"].inputValidationIndex + 1,
438
- outputValidationIndex: this["~orpc"].outputValidationIndex + 1,
439
- middlewares: [...this["~orpc"].middlewares, middleware]
440
- // FIXME: I believe we can remove `as any` here
180
+ meta: mergeMeta2(this["~orpc"].meta, meta)
441
181
  });
442
182
  }
443
183
  route(route) {
444
- return new ProcedureBuilder({
184
+ return new _Builder({
445
185
  ...this["~orpc"],
446
- contract: new ContractProcedure({
447
- route,
448
- InputSchema: void 0,
449
- OutputSchema: void 0,
450
- errorMap: this["~orpc"].errorMap
451
- })
186
+ route: mergeRoute2(this["~orpc"].route, route)
452
187
  });
453
188
  }
454
- input(schema, example) {
455
- return new ProcedureBuilderWithInput({
189
+ input(schema) {
190
+ return new _Builder({
456
191
  ...this["~orpc"],
457
- contract: new ContractProcedure({
458
- OutputSchema: void 0,
459
- InputSchema: schema,
460
- inputExample: example,
461
- errorMap: this["~orpc"].errorMap
462
- })
192
+ inputSchema: schema,
193
+ inputValidationIndex: fallbackConfig("initialInputValidationIndex", this["~orpc"].config.initialInputValidationIndex) + this["~orpc"].middlewares.length
463
194
  });
464
195
  }
465
- output(schema, example) {
466
- return new ProcedureBuilderWithOutput({
196
+ output(schema) {
197
+ return new _Builder({
467
198
  ...this["~orpc"],
468
- contract: new ContractProcedure({
469
- InputSchema: void 0,
470
- OutputSchema: schema,
471
- outputExample: example,
472
- errorMap: this["~orpc"].errorMap
473
- })
199
+ outputSchema: schema,
200
+ outputValidationIndex: fallbackConfig("initialOutputValidationIndex", this["~orpc"].config.initialOutputValidationIndex) + this["~orpc"].middlewares.length
474
201
  });
475
202
  }
476
203
  handler(handler) {
477
204
  return new DecoratedProcedure({
478
205
  ...this["~orpc"],
479
- contract: new ContractProcedure({
480
- InputSchema: void 0,
481
- OutputSchema: void 0,
482
- errorMap: this["~orpc"].errorMap
483
- }),
484
206
  handler
485
207
  });
486
208
  }
487
209
  prefix(prefix) {
488
- return new RouterBuilder({
210
+ return new _Builder({
489
211
  ...this["~orpc"],
490
- prefix
212
+ prefix: mergePrefix(this["~orpc"].prefix, prefix)
491
213
  });
492
214
  }
493
215
  tag(...tags) {
494
- return new RouterBuilder({
216
+ return new _Builder({
495
217
  ...this["~orpc"],
496
- tags
218
+ tags: mergeTags(this["~orpc"].tags, tags)
497
219
  });
498
220
  }
499
221
  router(router) {
500
- return new RouterBuilder(this["~orpc"]).router(router);
222
+ return adaptRouter(router, this["~orpc"]);
501
223
  }
502
224
  lazy(loader) {
503
- return new RouterBuilder(this["~orpc"]).lazy(loader);
225
+ return adaptRouter(flatLazy(lazy(loader)), this["~orpc"]);
504
226
  }
505
227
  };
228
+ var os = new Builder({
229
+ config: {},
230
+ route: {},
231
+ meta: {},
232
+ errorMap: {},
233
+ inputSchema: void 0,
234
+ outputSchema: void 0,
235
+ inputValidationIndex: fallbackConfig("initialInputValidationIndex"),
236
+ outputValidationIndex: fallbackConfig("initialOutputValidationIndex"),
237
+ middlewares: []
238
+ });
506
239
 
507
- // src/builder-with-errors.ts
508
- var BuilderWithErrors = class _BuilderWithErrors {
509
- "~type" = "BuilderWithErrors";
510
- "~orpc";
511
- constructor(def) {
512
- this["~orpc"] = def;
513
- }
514
- context() {
515
- return this;
516
- }
517
- errors(errors) {
518
- return new _BuilderWithErrors({
519
- ...this["~orpc"],
520
- errorMap: {
521
- ...this["~orpc"].errorMap,
522
- ...errors
523
- }
524
- });
525
- }
526
- middleware(middleware) {
527
- return decorateMiddleware(middleware);
528
- }
529
- use(middleware) {
530
- return new BuilderWithErrorsMiddlewares({
531
- ...this["~orpc"],
532
- inputValidationIndex: 1,
533
- outputValidationIndex: 1,
534
- middlewares: [middleware]
535
- // FIXME: I believe we can remove `as any` here
536
- });
537
- }
538
- route(route) {
539
- return new ProcedureBuilder({
540
- middlewares: [],
541
- inputValidationIndex: 0,
542
- outputValidationIndex: 0,
543
- contract: new ContractProcedure2({
544
- route,
545
- InputSchema: void 0,
546
- OutputSchema: void 0,
547
- errorMap: this["~orpc"].errorMap
548
- })
549
- });
550
- }
551
- input(schema, example) {
552
- return new ProcedureBuilderWithInput({
553
- middlewares: [],
554
- inputValidationIndex: 0,
555
- outputValidationIndex: 0,
556
- contract: new ContractProcedure2({
557
- OutputSchema: void 0,
558
- InputSchema: schema,
559
- inputExample: example,
560
- errorMap: this["~orpc"].errorMap
561
- })
562
- });
563
- }
564
- output(schema, example) {
565
- return new ProcedureBuilderWithOutput({
566
- middlewares: [],
567
- inputValidationIndex: 0,
568
- outputValidationIndex: 0,
569
- contract: new ContractProcedure2({
570
- InputSchema: void 0,
571
- OutputSchema: schema,
572
- outputExample: example,
573
- errorMap: this["~orpc"].errorMap
574
- })
575
- });
576
- }
577
- handler(handler) {
578
- return new DecoratedProcedure({
579
- middlewares: [],
580
- inputValidationIndex: 0,
581
- outputValidationIndex: 0,
582
- contract: new ContractProcedure2({
583
- InputSchema: void 0,
584
- OutputSchema: void 0,
585
- errorMap: this["~orpc"].errorMap
586
- }),
587
- handler
588
- });
589
- }
590
- prefix(prefix) {
591
- return new RouterBuilder({
592
- middlewares: [],
593
- errorMap: this["~orpc"].errorMap,
594
- prefix
595
- });
596
- }
597
- tag(...tags) {
598
- return new RouterBuilder({
599
- middlewares: [],
600
- errorMap: this["~orpc"].errorMap,
601
- tags
602
- });
603
- }
604
- router(router) {
605
- return new RouterBuilder({
606
- middlewares: [],
607
- ...this["~orpc"]
608
- }).router(router);
609
- }
610
- lazy(loader) {
611
- return new RouterBuilder({
612
- middlewares: [],
613
- ...this["~orpc"]
614
- }).lazy(loader);
615
- }
616
- };
617
-
618
- // src/builder-with-middlewares.ts
619
- import { ContractProcedure as ContractProcedure3 } from "@orpc/contract";
240
+ // src/context.ts
241
+ function mergeContext(context, other) {
242
+ return { ...context, ...other };
243
+ }
620
244
 
621
- // src/implementer-chainable.ts
245
+ // src/implementer.ts
622
246
  import { isContractProcedure } from "@orpc/contract";
623
- import { createCallableObject } from "@orpc/shared";
624
-
625
- // src/router-implementer.ts
626
- var RouterImplementer = class _RouterImplementer {
627
- "~type" = "RouterImplementer";
628
- "~orpc";
629
- constructor(def) {
630
- this["~orpc"] = def;
631
- }
632
- use(middleware) {
633
- return new _RouterImplementer({
634
- ...this["~orpc"],
635
- middlewares: [...this["~orpc"].middlewares ?? [], middleware]
636
- });
637
- }
638
- router(router) {
639
- const adapted = new RouterBuilder({
640
- ...this["~orpc"],
641
- errorMap: {}
642
- }).router(router);
643
- const contracted = setRouterContract(adapted, this["~orpc"].contract);
644
- return contracted;
645
- }
646
- lazy(loader) {
647
- const adapted = new RouterBuilder({
648
- ...this["~orpc"],
649
- errorMap: {}
650
- }).lazy(loader);
651
- const contracted = setRouterContract(adapted, this["~orpc"].contract);
652
- return contracted;
653
- }
654
- };
655
-
656
- // src/implementer-chainable.ts
657
- function createChainableImplementer(contract, options) {
247
+ function implementerInternal(contract, config, middlewares) {
658
248
  if (isContractProcedure(contract)) {
659
- const implementer = new ProcedureImplementer({
660
- contract,
661
- middlewares: options.middlewares,
662
- inputValidationIndex: options.inputValidationIndex,
663
- outputValidationIndex: options.outputValidationIndex
664
- });
665
- return implementer;
666
- }
667
- const chainable = {};
668
- for (const key in contract) {
669
- chainable[key] = createChainableImplementer(contract[key], options);
670
- }
671
- const routerImplementer = new RouterImplementer({
672
- contract,
673
- middlewares: options.middlewares
249
+ const impl2 = new Builder({
250
+ ...contract["~orpc"],
251
+ config,
252
+ middlewares,
253
+ inputValidationIndex: fallbackConfig("initialInputValidationIndex", config?.initialInputValidationIndex) + middlewares.length,
254
+ outputValidationIndex: fallbackConfig("initialOutputValidationIndex", config?.initialOutputValidationIndex) + middlewares.length
255
+ });
256
+ return impl2;
257
+ }
258
+ const impl = new Proxy(contract, {
259
+ get: (target, key) => {
260
+ let method;
261
+ if (key === "middleware") {
262
+ method = (mid) => decorateMiddleware(mid);
263
+ } else if (key === "use") {
264
+ method = (mid) => {
265
+ return implementerInternal(
266
+ contract,
267
+ config,
268
+ addMiddleware(middlewares, mid)
269
+ );
270
+ };
271
+ } else if (key === "router") {
272
+ method = (router) => {
273
+ const adapted = adaptRouter(router, {
274
+ middlewares,
275
+ errorMap: {}
276
+ });
277
+ return setRouterContract(adapted, contract);
278
+ };
279
+ } else if (key === "lazy") {
280
+ method = (loader) => {
281
+ const adapted = adaptRouter(flatLazy(lazy(loader)), {
282
+ middlewares,
283
+ errorMap: {}
284
+ });
285
+ return setRouterContract(adapted, contract);
286
+ };
287
+ }
288
+ const next = Reflect.get(target, key);
289
+ if (!next || typeof next !== "function" && typeof next !== "object") {
290
+ return method ?? next;
291
+ }
292
+ const nextImpl = implementerInternal(next, config, middlewares);
293
+ if (method) {
294
+ return new Proxy(method, {
295
+ get(_, key2) {
296
+ return Reflect.get(nextImpl, key2);
297
+ }
298
+ });
299
+ }
300
+ return nextImpl;
301
+ }
674
302
  });
675
- const merged = new Proxy(chainable, {
676
- get(target, key) {
303
+ return impl;
304
+ }
305
+ function implement(contract, config = {}) {
306
+ const implInternal = implementerInternal(contract, config, []);
307
+ const impl = new Proxy(implInternal, {
308
+ get: (target, key) => {
309
+ let method;
310
+ if (key === "$context") {
311
+ method = () => impl;
312
+ } else if (key === "$config") {
313
+ method = (config2) => implement(contract, config2);
314
+ }
677
315
  const next = Reflect.get(target, key);
678
- const method = Reflect.get(routerImplementer, key);
679
- if (typeof key !== "string" || typeof method !== "function") {
680
- return next;
316
+ if (!next || typeof next !== "function" && typeof next !== "object") {
317
+ return method ?? next;
681
318
  }
682
- if (!next) {
683
- return method.bind(routerImplementer);
319
+ if (method) {
320
+ return new Proxy(method, {
321
+ get(_, key2) {
322
+ return Reflect.get(next, key2);
323
+ }
324
+ });
684
325
  }
685
- return createCallableObject(next, method.bind(routerImplementer));
326
+ return next;
686
327
  }
687
328
  });
688
- return merged;
329
+ return impl;
689
330
  }
690
331
 
691
- // src/builder-with-middlewares.ts
692
- var BuilderWithMiddlewares = class _BuilderWithMiddlewares {
693
- "~type" = "BuilderHasMiddlewares";
694
- "~orpc";
695
- constructor(def) {
696
- this["~orpc"] = def;
697
- }
698
- use(middleware) {
699
- return new _BuilderWithMiddlewares({
700
- ...this["~orpc"],
701
- inputValidationIndex: this["~orpc"].inputValidationIndex + 1,
702
- outputValidationIndex: this["~orpc"].outputValidationIndex + 1,
703
- middlewares: [...this["~orpc"].middlewares, middleware]
704
- });
705
- }
706
- errors(errors) {
707
- return new BuilderWithErrorsMiddlewares({
708
- ...this["~orpc"],
709
- errorMap: errors
710
- });
711
- }
712
- route(route) {
713
- return new ProcedureBuilder({
714
- ...this["~orpc"],
715
- contract: new ContractProcedure3({
716
- route,
717
- InputSchema: void 0,
718
- OutputSchema: void 0,
719
- errorMap: {}
720
- })
721
- });
722
- }
723
- input(schema, example) {
724
- return new ProcedureBuilderWithInput({
725
- ...this["~orpc"],
726
- contract: new ContractProcedure3({
727
- OutputSchema: void 0,
728
- InputSchema: schema,
729
- inputExample: example,
730
- errorMap: {}
731
- })
732
- });
733
- }
734
- output(schema, example) {
735
- return new ProcedureBuilderWithOutput({
736
- ...this["~orpc"],
737
- contract: new ContractProcedure3({
738
- InputSchema: void 0,
739
- OutputSchema: schema,
740
- outputExample: example,
741
- errorMap: {}
742
- })
743
- });
744
- }
745
- handler(handler) {
746
- return new DecoratedProcedure({
747
- ...this["~orpc"],
748
- contract: new ContractProcedure3({
749
- InputSchema: void 0,
750
- OutputSchema: void 0,
751
- errorMap: {}
752
- }),
753
- handler
754
- });
755
- }
756
- prefix(prefix) {
757
- return new RouterBuilder({
758
- middlewares: this["~orpc"].middlewares,
759
- errorMap: {},
760
- prefix
761
- });
762
- }
763
- tag(...tags) {
764
- return new RouterBuilder({
765
- middlewares: this["~orpc"].middlewares,
766
- errorMap: {},
767
- tags
768
- });
769
- }
770
- router(router) {
771
- return new RouterBuilder({
772
- errorMap: {},
773
- ...this["~orpc"]
774
- }).router(router);
775
- }
776
- lazy(loader) {
777
- return new RouterBuilder({
778
- errorMap: {},
779
- ...this["~orpc"]
780
- }).lazy(loader);
781
- }
782
- contract(contract) {
783
- return createChainableImplementer(contract, this["~orpc"]);
784
- }
785
- };
786
-
787
- // src/builder.ts
788
- var Builder = class {
789
- "~type" = "Builder";
790
- "~orpc";
791
- constructor(def) {
792
- this["~orpc"] = def;
793
- }
794
- context() {
795
- return this;
796
- }
797
- middleware(middleware) {
798
- return decorateMiddleware(middleware);
799
- }
800
- errors(errors) {
801
- return new BuilderWithErrors({
802
- errorMap: errors
803
- });
804
- }
805
- use(middleware) {
806
- return new BuilderWithMiddlewares({
807
- ...this["~orpc"],
808
- inputValidationIndex: 1,
809
- outputValidationIndex: 1,
810
- middlewares: [middleware]
811
- // FIXME: I believe we can remove `as any` here
812
- });
813
- }
814
- route(route) {
815
- return new ProcedureBuilder({
816
- middlewares: [],
817
- inputValidationIndex: 0,
818
- outputValidationIndex: 0,
819
- contract: new ContractProcedure4({
820
- route,
821
- InputSchema: void 0,
822
- OutputSchema: void 0,
823
- errorMap: {}
824
- })
825
- });
826
- }
827
- input(schema, example) {
828
- return new ProcedureBuilderWithInput({
829
- middlewares: [],
830
- inputValidationIndex: 0,
831
- outputValidationIndex: 0,
832
- contract: new ContractProcedure4({
833
- OutputSchema: void 0,
834
- InputSchema: schema,
835
- inputExample: example,
836
- errorMap: {}
837
- })
838
- });
839
- }
840
- output(schema, example) {
841
- return new ProcedureBuilderWithOutput({
842
- middlewares: [],
843
- inputValidationIndex: 0,
844
- outputValidationIndex: 0,
845
- contract: new ContractProcedure4({
846
- InputSchema: void 0,
847
- OutputSchema: schema,
848
- outputExample: example,
849
- errorMap: {}
850
- })
851
- });
852
- }
853
- handler(handler) {
854
- return new DecoratedProcedure({
855
- middlewares: [],
856
- inputValidationIndex: 0,
857
- outputValidationIndex: 0,
858
- contract: new ContractProcedure4({
859
- InputSchema: void 0,
860
- OutputSchema: void 0,
861
- errorMap: {}
862
- }),
863
- handler
864
- });
865
- }
866
- prefix(prefix) {
867
- return new RouterBuilder({
868
- middlewares: [],
869
- errorMap: {},
870
- prefix
871
- });
872
- }
873
- tag(...tags) {
874
- return new RouterBuilder({
875
- middlewares: [],
876
- errorMap: {},
877
- tags
878
- });
879
- }
880
- router(router) {
881
- return new RouterBuilder({
882
- middlewares: [],
883
- errorMap: []
884
- }).router(router);
885
- }
886
- lazy(loader) {
887
- return new RouterBuilder({
888
- middlewares: [],
889
- errorMap: {}
890
- }).lazy(loader);
891
- }
892
- contract(contract) {
893
- return createChainableImplementer(contract, {
894
- middlewares: [],
895
- inputValidationIndex: 0,
896
- outputValidationIndex: 0
897
- });
898
- }
899
- };
900
-
901
332
  // src/procedure-utils.ts
902
333
  function call(procedure, input, ...rest) {
903
334
  return createProcedureClient(procedure, ...rest)(input);
904
335
  }
905
336
 
906
- // src/lazy-utils.ts
907
- function createLazyProcedureFormAnyLazy(lazied) {
908
- const lazyProcedure = lazy(async () => {
909
- const { default: maybeProcedure } = await unlazy(flatLazy(lazied));
910
- if (!isProcedure(maybeProcedure)) {
911
- throw new Error(`
912
- Expected a lazy<procedure> but got lazy<unknown>.
913
- This should be caught by TypeScript compilation.
914
- Please report this issue if this makes you feel uncomfortable.
915
- `);
916
- }
917
- return { default: maybeProcedure };
918
- });
919
- return lazyProcedure;
920
- }
921
-
922
337
  // src/router-client.ts
923
338
  function createRouterClient(router, ...rest) {
924
339
  if (isProcedure(router)) {
@@ -946,42 +361,53 @@ function createRouterClient(router, ...rest) {
946
361
  }
947
362
 
948
363
  // src/index.ts
949
- import { configGlobal, fallbackToGlobalConfig, isDefinedError, ORPCError, safe, type } from "@orpc/contract";
950
- var os = new Builder({});
364
+ import { isDefinedError, ORPCError, safe } from "@orpc/client";
365
+ import { eventIterator, type, ValidationError } from "@orpc/contract";
366
+ import { getEventMeta, withEventMeta } from "@orpc/server-standard";
367
+ import { onError, onFinish, onStart, onSuccess } from "@orpc/shared";
951
368
  export {
952
369
  Builder,
953
370
  DecoratedProcedure,
954
371
  LAZY_LOADER_SYMBOL,
955
372
  ORPCError,
956
373
  Procedure,
957
- ProcedureBuilder,
958
- ProcedureImplementer,
959
- RouterBuilder,
960
- RouterImplementer,
374
+ ValidationError,
375
+ adaptRouter,
961
376
  call,
962
- configGlobal,
963
- createChainableImplementer,
964
- createORPCErrorConstructorMap,
377
+ convertPathToHttpPath,
378
+ createAccessibleLazyRouter,
379
+ createContractedProcedure,
380
+ createLazyProcedureFormAnyLazy,
965
381
  createProcedureClient,
966
382
  createRouterClient,
967
- decorateLazy,
968
383
  decorateMiddleware,
969
384
  deepSetLazyRouterPrefix,
970
- fallbackToGlobalConfig,
385
+ eachAllContractProcedure,
386
+ eachContractProcedure,
387
+ eventIterator,
388
+ fallbackConfig,
971
389
  flatLazy,
390
+ getEventMeta,
972
391
  getLazyRouterPrefix,
973
392
  getRouterChild,
974
393
  getRouterContract,
394
+ implement,
395
+ implementerInternal,
975
396
  isDefinedError,
976
397
  isLazy,
977
398
  isProcedure,
978
399
  lazy,
979
400
  mergeContext,
980
401
  middlewareOutputFn,
402
+ onError,
403
+ onFinish,
404
+ onStart,
405
+ onSuccess,
981
406
  os,
982
407
  safe,
983
408
  setRouterContract,
984
409
  type,
985
- unlazy
410
+ unlazy,
411
+ withEventMeta
986
412
  };
987
413
  //# sourceMappingURL=index.js.map