@temporary-name/server 1.9.3-alpha.0d2fa3247b6b23bbc2e3097a95f631b86740e4d8 → 1.9.3-alpha.102eab0800942eb736f7669a86c850ebfdfcd4a3

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/adapters/aws-lambda/index.d.mts +12 -6
  2. package/dist/adapters/aws-lambda/index.d.ts +12 -6
  3. package/dist/adapters/aws-lambda/index.mjs +13 -4
  4. package/dist/adapters/fetch/index.d.mts +12 -6
  5. package/dist/adapters/fetch/index.d.ts +12 -6
  6. package/dist/adapters/fetch/index.mjs +13 -11
  7. package/dist/adapters/node/index.d.mts +12 -6
  8. package/dist/adapters/node/index.d.ts +12 -6
  9. package/dist/adapters/node/index.mjs +13 -11
  10. package/dist/adapters/standard/index.d.mts +28 -13
  11. package/dist/adapters/standard/index.d.ts +28 -13
  12. package/dist/adapters/standard/index.mjs +9 -100
  13. package/dist/index.d.mts +69 -441
  14. package/dist/index.d.ts +69 -441
  15. package/dist/index.mjs +149 -356
  16. package/dist/openapi/index.d.mts +220 -0
  17. package/dist/openapi/index.d.ts +220 -0
  18. package/dist/openapi/index.mjs +760 -0
  19. package/dist/plugins/index.d.mts +2 -2
  20. package/dist/plugins/index.d.ts +2 -2
  21. package/dist/shared/{server.Btxrgkj5.d.ts → server.BCcLYvdF.d.mts} +7 -24
  22. package/dist/shared/server.CHV9AQHl.mjs +412 -0
  23. package/dist/shared/{server.Bo94xDTv.d.mts → server.CdeqmULw.d.ts} +7 -24
  24. package/dist/shared/server.DHezmW6C.d.mts +23 -0
  25. package/dist/shared/server.DN9mVGfv.mjs +11 -0
  26. package/dist/shared/server.DWwaAM-a.mjs +255 -0
  27. package/dist/shared/server.Da-qLzdU.d.ts +23 -0
  28. package/dist/shared/{server.C1YnHvvf.d.ts → server.DecvGKtb.d.mts} +125 -75
  29. package/dist/shared/{server.C1YnHvvf.d.mts → server.DecvGKtb.d.ts} +125 -75
  30. package/dist/shared/server.JtIZ8YG7.mjs +237 -0
  31. package/package.json +18 -11
  32. package/dist/shared/server.BEQrAa3A.mjs +0 -207
  33. package/dist/shared/server.D6K9uoPI.mjs +0 -35
  34. package/dist/shared/server.DZ5BIITo.mjs +0 -9
  35. package/dist/shared/server.X0YaZxSJ.mjs +0 -13
package/dist/index.mjs CHANGED
@@ -1,335 +1,141 @@
1
- import { isContractProcedure, mergeErrorMap, mergeMeta, mergeRoute, mergePrefix, enhanceRoute, mergeTags, getContractRouter } from '@temporary-name/contract';
2
- export { ValidationError, eventIterator, type, validateORPCError } from '@temporary-name/contract';
1
+ import { mergePrefix, mergeTags, ContractProcedure, mergeErrorMap, mergeMeta, mergeRoute, prefixRoute, initialSchemas, isContractProcedure, getContractRouter } from '@temporary-name/contract';
2
+ export { ValidationError, eventIterator, validateORPCError } from '@temporary-name/contract';
3
3
  import { onError, resolveMaybeOptionalOptions } from '@temporary-name/shared';
4
4
  export { AsyncIteratorClass, EventPublisher, ORPCError, asyncIteratorToStream as eventIteratorToStream, isDefinedError, onError, onFinish, onStart, onSuccess, safe, streamToAsyncIteratorClass as streamToEventIterator } from '@temporary-name/shared';
5
- import { g as gatingContext } from './shared/server.D6K9uoPI.mjs';
6
- import { c as createProcedureClient, i as isLazy, g as getLazyMeta, l as lazy, u as unlazy } from './shared/server.BEQrAa3A.mjs';
7
- export { L as LAZY_SYMBOL, a as createORPCErrorConstructorMap, m as mergeCurrentContext, b as middlewareOutputFn } from './shared/server.BEQrAa3A.mjs';
5
+ import { SchemaClass, core, strictObject, gatingContext } from '@temporary-name/zod';
8
6
  export { getEventMeta, withEventMeta } from '@temporary-name/standard-server';
9
- import 'node:async_hooks';
10
- import 'zod';
11
- import 'zod/v4/core';
7
+ import { g as getDynamicParams } from './shared/server.DN9mVGfv.mjs';
8
+ import { P as Procedure, c as createProcedureClient, e as enhanceRouter, a as addMiddleware, s as setHiddenRouterContract, l as lazyInternal, g as getLazyMeta, u as unlazy, i as isProcedure, b as isLazy, d as getRouter } from './shared/server.CHV9AQHl.mjs';
9
+ export { L as LAZY_SYMBOL, p as createAccessibleLazyRouter, f as createORPCErrorConstructorMap, o as getHiddenRouterContract, k as isStartWithMiddlewares, h as lazy, m as mergeCurrentContext, n as mergeMiddlewares, j as middlewareOutputFn, r as resolveContractProcedures, t as traverseContractProcedures, q as unlazyRouter } from './shared/server.CHV9AQHl.mjs';
12
10
 
13
- function decorateMiddleware(middleware) {
14
- const decorated = ((...args) => middleware(...args));
15
- decorated.mapInput = (mapInput) => {
16
- const mapped = decorateMiddleware(
17
- (options, input, ...rest) => middleware(options, mapInput(input), ...rest)
18
- );
19
- return mapped;
20
- };
21
- decorated.concat = (concatMiddleware, mapInput) => {
22
- const mapped = mapInput ? decorateMiddleware(concatMiddleware).mapInput(mapInput) : concatMiddleware;
23
- const concatted = decorateMiddleware((options, input, output, ...rest) => {
24
- const merged = middleware(
25
- {
26
- ...options,
27
- next: (...[nextOptions1]) => mapped(
28
- {
29
- ...options,
30
- context: { ...options.context, ...nextOptions1?.context },
31
- next: (...[nextOptions2]) => options.next({ context: { ...nextOptions1?.context, ...nextOptions2?.context } })
32
- },
33
- input,
34
- output,
35
- ...rest
36
- )
37
- },
38
- input,
39
- output,
40
- ...rest
41
- );
42
- return merged;
11
+ class DecoratedProcedure extends Procedure {
12
+ /**
13
+ * Make this procedure callable (works like a function while still being a procedure).
14
+ *
15
+ * @see {@link https://orpc.unnoq.com/docs/client/server-side Server-side Client Docs}
16
+ */
17
+ callable(...rest) {
18
+ const client = createProcedureClient(this, ...rest);
19
+ return new Proxy(client, {
20
+ get: (target, key) => {
21
+ return Reflect.has(this, key) ? Reflect.get(this, key) : Reflect.get(target, key);
22
+ },
23
+ has: (target, key) => {
24
+ return Reflect.has(this, key) || Reflect.has(target, key);
25
+ }
43
26
  });
44
- return concatted;
45
- };
46
- return decorated;
47
- }
48
-
49
- function isStartWithMiddlewares(middlewares, compare) {
50
- if (compare.length > middlewares.length) {
51
- return false;
52
27
  }
53
- for (let i = 0; i < middlewares.length; i++) {
54
- if (compare[i] === void 0) {
55
- return true;
56
- }
57
- if (middlewares[i] !== compare[i]) {
58
- return false;
59
- }
60
- }
61
- return true;
62
- }
63
- function mergeMiddlewares(first, second, options) {
64
- if (options.dedupeLeading && isStartWithMiddlewares(second, first)) {
65
- return second;
66
- }
67
- return [...first, ...second];
68
- }
69
- function addMiddleware(middlewares, addition) {
70
- return [...middlewares, addition];
71
28
  }
72
29
 
73
- class Procedure {
74
- /**
75
- * This property holds the defined options.
76
- */
77
- "~orpc";
30
+ class ProcedureBuilder extends ContractProcedure {
31
+ z;
78
32
  constructor(def) {
79
- this["~orpc"] = def;
33
+ super(def);
34
+ this.z = new SchemaClass();
80
35
  }
81
- }
82
- function isProcedure(item) {
83
- if (item instanceof Procedure) {
84
- return true;
85
- }
86
- return isContractProcedure(item) && "middlewares" in item["~orpc"] && "inputValidationIndex" in item["~orpc"] && "outputValidationIndex" in item["~orpc"] && "handler" in item["~orpc"];
87
- }
88
-
89
- class DecoratedProcedure extends Procedure {
90
36
  /**
91
- * Adds type-safe custom errors.
92
- * The provided errors are spared-merged with any existing errors.
37
+ * Adds type-safe custom errors to the contract.
38
+ * The provided errors are spared-merged with any existing errors in the contract.
93
39
  *
94
40
  * @see {@link https://orpc.unnoq.com/docs/error-handling#type%E2%80%90safe-error-handling Type-Safe Error Handling Docs}
95
41
  */
96
42
  errors(errors) {
97
- return new DecoratedProcedure({
43
+ return new ProcedureBuilder({
98
44
  ...this["~orpc"],
99
45
  errorMap: mergeErrorMap(this["~orpc"].errorMap, errors)
100
46
  });
101
47
  }
102
48
  /**
103
- * Sets or updates the metadata.
104
- * The provided metadata is spared-merged with any existing metadata.
49
+ * Sets or updates the metadata for the contract.
50
+ * The provided metadata is spared-merged with any existing metadata in the contract.
105
51
  *
106
52
  * @see {@link https://orpc.unnoq.com/docs/metadata Metadata Docs}
107
53
  */
108
54
  meta(meta) {
109
- return new DecoratedProcedure({
55
+ return new ProcedureBuilder({
110
56
  ...this["~orpc"],
111
57
  meta: mergeMeta(this["~orpc"].meta, meta)
112
58
  });
113
59
  }
60
+ endpoint(endpoint, schema) {
61
+ const [method, path] = endpoint.split(" ", 2);
62
+ const pathParamNames = getDynamicParams(path)?.map((p) => p.name) ?? [];
63
+ if (schema && !(schema instanceof core.$ZodObject)) {
64
+ throw new Error(`Path schema for endpoint "${endpoint}" must be a ZodObject schema.`);
65
+ }
66
+ const schemaKeys = schema ? Object.keys(schema._zod.def.shape) : [];
67
+ if (pathParamNames.length !== schemaKeys.length || !pathParamNames.every((name) => schemaKeys.includes(name))) {
68
+ throw new Error(`Path schema keys do not match dynamic parameters in endpoint "${endpoint}".`);
69
+ }
70
+ const { prefix } = this["~orpc"];
71
+ const route = { method, path };
72
+ return new ProcedureBuilder({
73
+ ...this["~orpc"],
74
+ route: mergeRoute(this["~orpc"].route, prefix ? prefixRoute(route, prefix) : route),
75
+ schemas: {
76
+ ...this["~orpc"].schemas,
77
+ pathSchema: schema ?? strictObject({})
78
+ }
79
+ });
80
+ }
114
81
  /**
115
- * Sets or updates the route definition.
116
- * The provided route is spared-merged with any existing route.
82
+ * Sets or updates the route definition for the contract.
83
+ * The provided route is spared-merged with any existing route in the contract.
117
84
  * This option is typically relevant when integrating with OpenAPI.
118
85
  *
119
86
  * @see {@link https://orpc.unnoq.com/docs/openapi/routing OpenAPI Routing Docs}
120
87
  * @see {@link https://orpc.unnoq.com/docs/openapi/input-output-structure OpenAPI Input/Output Structure Docs}
121
88
  */
122
- route(route) {
123
- return new DecoratedProcedure({
89
+ endpointOpts(route) {
90
+ const { prefix } = this["~orpc"];
91
+ return new ProcedureBuilder({
124
92
  ...this["~orpc"],
125
- route: mergeRoute(this["~orpc"].route, route)
93
+ route: mergeRoute(this["~orpc"].route, prefix ? prefixRoute(route, prefix) : route)
126
94
  });
127
95
  }
128
- use(middleware, mapInput) {
129
- const mapped = mapInput ? decorateMiddleware(middleware).mapInput(mapInput) : middleware;
130
- return new DecoratedProcedure({
96
+ path(schema) {
97
+ return new ProcedureBuilder({
131
98
  ...this["~orpc"],
132
- middlewares: addMiddleware(this["~orpc"].middlewares, mapped)
133
- });
134
- }
135
- /**
136
- * Make this procedure callable (works like a function while still being a procedure).
137
- *
138
- * @see {@link https://orpc.unnoq.com/docs/client/server-side Server-side Client Docs}
139
- */
140
- callable(...rest) {
141
- const client = createProcedureClient(this, ...rest);
142
- return new Proxy(client, {
143
- get: (target, key) => {
144
- return Reflect.has(this, key) ? Reflect.get(this, key) : Reflect.get(target, key);
99
+ schemas: {
100
+ ...this["~orpc"].schemas,
101
+ pathSchema: schema
145
102
  },
146
- has: (target, key) => {
147
- return Reflect.has(this, key) || Reflect.has(target, key);
148
- }
103
+ inputValidationIndex: this["~orpc"].middlewares.length
149
104
  });
150
105
  }
151
- }
152
-
153
- const HIDDEN_ROUTER_CONTRACT_SYMBOL = Symbol("ORPC_HIDDEN_ROUTER_CONTRACT");
154
- function setHiddenRouterContract(router, contract) {
155
- return new Proxy(router, {
156
- get(target, key) {
157
- if (key === HIDDEN_ROUTER_CONTRACT_SYMBOL) {
158
- return contract;
159
- }
160
- return Reflect.get(target, key);
161
- }
162
- });
163
- }
164
- function getHiddenRouterContract(router) {
165
- return router[HIDDEN_ROUTER_CONTRACT_SYMBOL];
166
- }
167
-
168
- function getRouter(router, path) {
169
- let current = router;
170
- for (let i = 0; i < path.length; i++) {
171
- const segment = path[i];
172
- if (!current) {
173
- return void 0;
174
- }
175
- if (isProcedure(current)) {
176
- return void 0;
177
- }
178
- if (!isLazy(current)) {
179
- current = current[segment];
180
- continue;
181
- }
182
- const lazied = current;
183
- const rest = path.slice(i);
184
- return lazy(async () => {
185
- const unwrapped = await unlazy(lazied);
186
- const next = getRouter(unwrapped.default, rest);
187
- return unlazy(next);
188
- }, getLazyMeta(lazied));
189
- }
190
- return current;
191
- }
192
- function createAccessibleLazyRouter(lazied) {
193
- const recursive = new Proxy(lazied, {
194
- get(target, key) {
195
- if (typeof key !== "string") {
196
- return Reflect.get(target, key);
197
- }
198
- const next = getRouter(lazied, [key]);
199
- return createAccessibleLazyRouter(next);
200
- }
201
- });
202
- return recursive;
203
- }
204
- function enhanceRouter(router, options) {
205
- if (isLazy(router)) {
206
- const laziedMeta = getLazyMeta(router);
207
- const enhancedPrefix = laziedMeta?.prefix ? mergePrefix(options.prefix, laziedMeta?.prefix) : options.prefix;
208
- const enhanced2 = lazy(
209
- async () => {
210
- const { default: unlaziedRouter } = await unlazy(router);
211
- const enhanced3 = enhanceRouter(unlaziedRouter, options);
212
- return unlazy(enhanced3);
106
+ query(schema) {
107
+ return new ProcedureBuilder({
108
+ ...this["~orpc"],
109
+ schemas: {
110
+ ...this["~orpc"].schemas,
111
+ querySchema: schema
213
112
  },
214
- {
215
- ...laziedMeta,
216
- prefix: enhancedPrefix
217
- }
218
- );
219
- const accessible = createAccessibleLazyRouter(enhanced2);
220
- return accessible;
221
- }
222
- if (isProcedure(router)) {
223
- const newMiddlewares = mergeMiddlewares(options.middlewares, router["~orpc"].middlewares, {
224
- dedupeLeading: options.dedupeLeadingMiddlewares
225
- });
226
- const newMiddlewareAdded = newMiddlewares.length - router["~orpc"].middlewares.length;
227
- const enhanced2 = new Procedure({
228
- ...router["~orpc"],
229
- route: enhanceRoute(router["~orpc"].route, options),
230
- errorMap: mergeErrorMap(options.errorMap, router["~orpc"].errorMap),
231
- middlewares: newMiddlewares,
232
- inputValidationIndex: router["~orpc"].inputValidationIndex + newMiddlewareAdded,
233
- outputValidationIndex: router["~orpc"].outputValidationIndex + newMiddlewareAdded
113
+ inputValidationIndex: this["~orpc"].middlewares.length
234
114
  });
235
- return enhanced2;
236
- }
237
- const enhanced = {};
238
- for (const key in router) {
239
- enhanced[key] = enhanceRouter(router[key], options);
240
115
  }
241
- return enhanced;
242
- }
243
- function traverseContractProcedures(options, callback, lazyOptions = []) {
244
- let currentRouter = options.router;
245
- const hiddenContract = getHiddenRouterContract(options.router);
246
- if (hiddenContract !== void 0) {
247
- currentRouter = hiddenContract;
248
- }
249
- if (isLazy(currentRouter)) {
250
- lazyOptions.push({
251
- router: currentRouter,
252
- path: options.path
253
- });
254
- } else if (isContractProcedure(currentRouter)) {
255
- callback({
256
- contract: currentRouter,
257
- path: options.path
116
+ body(schema) {
117
+ return new ProcedureBuilder({
118
+ ...this["~orpc"],
119
+ schemas: {
120
+ ...this["~orpc"].schemas,
121
+ bodySchema: schema
122
+ },
123
+ inputValidationIndex: this["~orpc"].middlewares.length
258
124
  });
259
- } else {
260
- for (const key in currentRouter) {
261
- traverseContractProcedures(
262
- {
263
- router: currentRouter[key],
264
- path: [...options.path, key]
265
- },
266
- callback,
267
- lazyOptions
268
- );
269
- }
270
- }
271
- return lazyOptions;
272
- }
273
- async function resolveContractProcedures(options, callback) {
274
- const pending = [options];
275
- for (const options2 of pending) {
276
- const lazyOptions = traverseContractProcedures(options2, callback);
277
- for (const options3 of lazyOptions) {
278
- const { default: router } = await unlazy(options3.router);
279
- pending.push({
280
- router,
281
- path: options3.path
282
- });
283
- }
284
- }
285
- }
286
- async function unlazyRouter(router) {
287
- if (isProcedure(router)) {
288
- return router;
289
125
  }
290
- const unlazied = {};
291
- for (const key in router) {
292
- const item = router[key];
293
- const { default: unlaziedRouter } = await unlazy(item);
294
- unlazied[key] = await unlazyRouter(unlaziedRouter);
295
- }
296
- return unlazied;
297
- }
298
-
299
- class Builder {
300
126
  /**
301
- * This property holds the defined options.
302
- */
303
- "~orpc";
304
- constructor(def) {
305
- this["~orpc"] = def;
306
- }
307
- /**
308
- * Set or override the initial context.
309
- *
310
- * @see {@link https://orpc.unnoq.com/docs/context Context Docs}
311
- */
312
- $context() {
313
- return this;
314
- }
315
- /**
316
- * Creates a middleware.
317
- *
318
- * @see {@link https://orpc.unnoq.com/docs/middleware Middleware Docs}
319
- */
320
- middleware(middleware) {
321
- return decorateMiddleware(middleware);
322
- }
323
- /**
324
- * Adds type-safe custom errors.
325
- * The provided errors are spared-merged with any existing errors.
127
+ * Defines the output validation schema.
326
128
  *
327
- * @see {@link https://orpc.unnoq.com/docs/error-handling#type%E2%80%90safe-error-handling Type-Safe Error Handling Docs}
129
+ * @see {@link https://orpc.unnoq.com/docs/procedure#input-output-validation Output Validation Docs}
328
130
  */
329
- errors(errors) {
330
- return new Builder({
131
+ output(schema) {
132
+ return new ProcedureBuilder({
331
133
  ...this["~orpc"],
332
- errorMap: mergeErrorMap(this["~orpc"].errorMap, errors)
134
+ schemas: {
135
+ ...this["~orpc"].schemas,
136
+ outputSchema: schema
137
+ },
138
+ outputValidationIndex: this["~orpc"].middlewares.length
333
139
  });
334
140
  }
335
141
  /**
@@ -340,7 +146,7 @@ class Builder {
340
146
  * @see {@link https://orpc.unnoq.com/docs/middleware Middleware Docs}
341
147
  */
342
148
  use(middleware) {
343
- return new Builder({
149
+ return new this.constructor({
344
150
  ...this["~orpc"],
345
151
  middlewares: addMiddleware(this["~orpc"].middlewares, middleware)
346
152
  });
@@ -353,56 +159,6 @@ class Builder {
353
159
  );
354
160
  });
355
161
  }
356
- /**
357
- * Sets or updates the metadata.
358
- * The provided metadata is spared-merged with any existing metadata.
359
- *
360
- * @see {@link https://orpc.unnoq.com/docs/metadata Metadata Docs}
361
- */
362
- meta(meta) {
363
- return new Builder({
364
- ...this["~orpc"],
365
- meta: mergeMeta(this["~orpc"].meta, meta)
366
- });
367
- }
368
- /**
369
- * Sets or updates the route definition.
370
- * The provided route is spared-merged with any existing route.
371
- * This option is typically relevant when integrating with OpenAPI.
372
- *
373
- * @see {@link https://orpc.unnoq.com/docs/openapi/routing OpenAPI Routing Docs}
374
- * @see {@link https://orpc.unnoq.com/docs/openapi/input-output-structure OpenAPI Input/Output Structure Docs}
375
- */
376
- route(route) {
377
- return new Builder({
378
- ...this["~orpc"],
379
- route: mergeRoute(this["~orpc"].route, route)
380
- });
381
- }
382
- /**
383
- * Defines the input validation schema.
384
- *
385
- * @see {@link https://orpc.unnoq.com/docs/procedure#input-output-validation Input Validation Docs}
386
- */
387
- input(schema) {
388
- return new Builder({
389
- ...this["~orpc"],
390
- inputSchema: schema,
391
- inputValidationIndex: this["~orpc"].middlewares.length
392
- });
393
- }
394
- /**
395
- * Defines the output validation schema.
396
- *
397
- * @see {@link https://orpc.unnoq.com/docs/procedure#input-output-validation Output Validation Docs}
398
- */
399
- output(schema) {
400
- return new Builder({
401
- ...this["~orpc"],
402
- outputSchema: schema,
403
- outputValidationIndex: this["~orpc"].middlewares.length
404
- });
405
- }
406
162
  /**
407
163
  * Defines the handler of the procedure.
408
164
  *
@@ -414,6 +170,8 @@ class Builder {
414
170
  handler
415
171
  });
416
172
  }
173
+ }
174
+ class BuilderWithMiddlewares extends ProcedureBuilder {
417
175
  /**
418
176
  * Prefixes all procedures in the router.
419
177
  * The provided prefix is post-appended to any existing router prefix.
@@ -423,7 +181,7 @@ class Builder {
423
181
  * @see {@link https://orpc.unnoq.com/docs/openapi/routing#route-prefixes OpenAPI Route Prefixes Docs}
424
182
  */
425
183
  prefix(prefix) {
426
- return new Builder({
184
+ return new this.constructor({
427
185
  ...this["~orpc"],
428
186
  prefix: mergePrefix(this["~orpc"].prefix, prefix)
429
187
  });
@@ -435,7 +193,7 @@ class Builder {
435
193
  * @see {@link https://orpc.unnoq.com/docs/openapi/openapi-specification#operation-metadata OpenAPI Operation Metadata Docs}
436
194
  */
437
195
  tag(...tags) {
438
- return new Builder({
196
+ return new this.constructor({
439
197
  ...this["~orpc"],
440
198
  tags: mergeTags(this["~orpc"].tags, tags)
441
199
  });
@@ -448,33 +206,79 @@ class Builder {
448
206
  router(router) {
449
207
  return enhanceRouter(router, this["~orpc"]);
450
208
  }
209
+ }
210
+
211
+ function decorateMiddleware(middleware) {
212
+ const decorated = ((...args) => middleware(...args));
213
+ decorated.mapInput = (mapInput) => {
214
+ const mapped = decorateMiddleware(
215
+ (options, input, ...rest) => middleware(options, mapInput(input), ...rest)
216
+ );
217
+ return mapped;
218
+ };
219
+ decorated.concat = (concatMiddleware, mapInput) => {
220
+ const mapped = mapInput ? decorateMiddleware(concatMiddleware).mapInput(mapInput) : concatMiddleware;
221
+ const concatted = decorateMiddleware((options, input, output, ...rest) => {
222
+ const merged = middleware(
223
+ {
224
+ ...options,
225
+ next: (...[nextOptions1]) => mapped(
226
+ {
227
+ ...options,
228
+ context: { ...options.context, ...nextOptions1?.context },
229
+ next: (...[nextOptions2]) => options.next({ context: { ...nextOptions1?.context, ...nextOptions2?.context } })
230
+ },
231
+ input,
232
+ output,
233
+ ...rest
234
+ )
235
+ },
236
+ input,
237
+ output,
238
+ ...rest
239
+ );
240
+ return merged;
241
+ });
242
+ return concatted;
243
+ };
244
+ return decorated;
245
+ }
246
+
247
+ class Builder extends BuilderWithMiddlewares {
451
248
  /**
452
- * Create a lazy router
453
- * And applies all of the previously defined options to the specified router.
249
+ * Set or override the initial context.
454
250
  *
455
- * @see {@link https://orpc.unnoq.com/docs/router#extending-router Extending Router Docs}
251
+ * @see {@link https://orpc.unnoq.com/docs/context Context Docs}
252
+ */
253
+ $context() {
254
+ return this;
255
+ }
256
+ /**
257
+ * Creates a middleware.
258
+ *
259
+ * @see {@link https://orpc.unnoq.com/docs/middleware Middleware Docs}
456
260
  */
457
- lazyRoute(loader) {
458
- return enhanceRouter(lazy(loader), this["~orpc"]);
261
+ middleware(middleware) {
262
+ return decorateMiddleware(middleware);
459
263
  }
460
264
  }
461
265
  function createApiBuilder(opts = {}) {
462
- const base = new Builder({
266
+ return new Builder({
463
267
  route: {},
464
268
  meta: opts.meta ?? {},
465
269
  errorMap: {},
466
270
  inputValidationIndex: 0,
467
271
  outputValidationIndex: 0,
468
272
  middlewares: [
469
- onError((error, options) => {
273
+ onError((error, _options) => {
470
274
  console.dir(error, { depth: null });
471
275
  })
472
276
  ],
277
+ schemas: initialSchemas,
473
278
  // NB: this is a relic from orpc -- I'm not convinced there's a need for this (or if there is, that it's
474
279
  // the best solution). For now I've removed the interface to configure it externally.
475
280
  dedupeLeadingMiddlewares: true
476
281
  });
477
- return base;
478
282
  }
479
283
  const os = createApiBuilder();
480
284
 
@@ -512,17 +316,6 @@ function implementerInternal(contract, middlewares) {
512
316
  });
513
317
  return setHiddenRouterContract(adapted, contract);
514
318
  };
515
- } else if (key === "lazyRoute") {
516
- method = (loader) => {
517
- const adapted = enhanceRouter(lazy(loader), {
518
- middlewares,
519
- errorMap: {},
520
- prefix: void 0,
521
- tags: void 0,
522
- dedupeLeadingMiddlewares: true
523
- });
524
- return setHiddenRouterContract(adapted, contract);
525
- };
526
319
  }
527
320
  const next = getContractRouter(target, [key]);
528
321
  if (!next) {
@@ -564,7 +357,7 @@ function implement(contract) {
564
357
  }
565
358
 
566
359
  function createAssertedLazyProcedure(lazied) {
567
- const lazyProcedure = lazy(async () => {
360
+ const lazyProcedure = lazyInternal(async () => {
568
361
  const { default: maybeProcedure } = await unlazy(lazied);
569
362
  if (!isProcedure(maybeProcedure)) {
570
363
  throw new Error(`
@@ -615,4 +408,4 @@ function createRouterClient(router, ...rest) {
615
408
  return recursive;
616
409
  }
617
410
 
618
- export { Builder, DecoratedProcedure, Procedure, addMiddleware, call, createAccessibleLazyRouter, createApiBuilder, createAssertedLazyProcedure, createContractedProcedure, createProcedureClient, createRouterClient, decorateMiddleware, enhanceRouter, getHiddenRouterContract, getLazyMeta, getRouter, implement, implementerInternal, isLazy, isProcedure, isStartWithMiddlewares, lazy, mergeMiddlewares, os, resolveContractProcedures, setHiddenRouterContract, traverseContractProcedures, unlazy, unlazyRouter };
411
+ export { Builder, BuilderWithMiddlewares, DecoratedProcedure, Procedure, ProcedureBuilder, addMiddleware, call, createApiBuilder, createAssertedLazyProcedure, createContractedProcedure, createProcedureClient, createRouterClient, decorateMiddleware, enhanceRouter, getLazyMeta, getRouter, implement, implementerInternal, isLazy, isProcedure, lazyInternal, os, setHiddenRouterContract, unlazy };