@orpc/server 0.0.0-next.954e6e2 → 0.0.0-next.9581bcc

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 +34 -21
  2. package/dist/adapters/aws-lambda/index.d.mts +46 -0
  3. package/dist/adapters/aws-lambda/index.d.ts +46 -0
  4. package/dist/adapters/aws-lambda/index.mjs +42 -0
  5. package/dist/adapters/bun-ws/index.d.mts +36 -0
  6. package/dist/adapters/bun-ws/index.d.ts +36 -0
  7. package/dist/adapters/bun-ws/index.mjs +47 -0
  8. package/dist/adapters/crossws/index.d.mts +33 -0
  9. package/dist/adapters/crossws/index.d.ts +33 -0
  10. package/dist/adapters/crossws/index.mjs +45 -0
  11. package/dist/adapters/fetch/index.d.mts +108 -11
  12. package/dist/adapters/fetch/index.d.ts +108 -11
  13. package/dist/adapters/fetch/index.mjs +178 -7
  14. package/dist/adapters/message-port/index.d.mts +31 -0
  15. package/dist/adapters/message-port/index.d.ts +31 -0
  16. package/dist/adapters/message-port/index.mjs +39 -0
  17. package/dist/adapters/node/index.d.mts +86 -22
  18. package/dist/adapters/node/index.d.ts +86 -22
  19. package/dist/adapters/node/index.mjs +144 -22
  20. package/dist/adapters/standard/index.d.mts +11 -19
  21. package/dist/adapters/standard/index.d.ts +11 -19
  22. package/dist/adapters/standard/index.mjs +5 -4
  23. package/dist/adapters/standard-peer/index.d.mts +18 -0
  24. package/dist/adapters/standard-peer/index.d.ts +18 -0
  25. package/dist/adapters/standard-peer/index.mjs +7 -0
  26. package/dist/adapters/websocket/index.d.mts +56 -0
  27. package/dist/adapters/websocket/index.d.ts +56 -0
  28. package/dist/adapters/websocket/index.mjs +67 -0
  29. package/dist/adapters/ws/index.d.mts +31 -0
  30. package/dist/adapters/ws/index.d.ts +31 -0
  31. package/dist/adapters/ws/index.mjs +37 -0
  32. package/dist/helpers/index.d.mts +149 -0
  33. package/dist/helpers/index.d.ts +149 -0
  34. package/dist/helpers/index.mjs +198 -0
  35. package/dist/hibernation/index.d.mts +44 -0
  36. package/dist/hibernation/index.d.ts +44 -0
  37. package/dist/hibernation/index.mjs +65 -0
  38. package/dist/index.d.mts +679 -131
  39. package/dist/index.d.ts +679 -131
  40. package/dist/index.mjs +212 -53
  41. package/dist/plugins/index.d.mts +160 -19
  42. package/dist/plugins/index.d.ts +160 -19
  43. package/dist/plugins/index.mjs +201 -17
  44. package/dist/shared/server.B4BGqy3Y.d.mts +193 -0
  45. package/dist/shared/server.B4BGqy3Y.d.ts +193 -0
  46. package/dist/shared/server.Bxx6tqNe.mjs +219 -0
  47. package/dist/shared/server.COL12UTb.d.ts +32 -0
  48. package/dist/shared/server.CVKCo60T.d.mts +12 -0
  49. package/dist/shared/server.Cb6yD7DZ.d.ts +42 -0
  50. package/dist/shared/server.Ck-gOLzq.d.mts +32 -0
  51. package/dist/shared/server.DBCUJijK.d.mts +74 -0
  52. package/dist/shared/server.DNtJ-p60.d.ts +12 -0
  53. package/dist/shared/server.DPIFWpxG.d.ts +74 -0
  54. package/dist/shared/server.DZ5BIITo.mjs +9 -0
  55. package/dist/shared/server.Ds4HPpvH.mjs +409 -0
  56. package/dist/shared/server.DzV1hr3z.d.mts +42 -0
  57. package/dist/shared/server.TEVCLCFC.mjs +39 -0
  58. package/dist/shared/server.UVMTOWrk.mjs +26 -0
  59. package/package.json +72 -20
  60. package/dist/adapters/hono/index.d.mts +0 -20
  61. package/dist/adapters/hono/index.d.ts +0 -20
  62. package/dist/adapters/hono/index.mjs +0 -32
  63. package/dist/adapters/next/index.d.mts +0 -27
  64. package/dist/adapters/next/index.d.ts +0 -27
  65. package/dist/adapters/next/index.mjs +0 -29
  66. package/dist/shared/server.B-ewprcf.d.ts +0 -77
  67. package/dist/shared/server.BBGuTxHE.mjs +0 -163
  68. package/dist/shared/server.CA-o8cUY.d.mts +0 -9
  69. package/dist/shared/server.Cn9ybJtE.d.mts +0 -152
  70. package/dist/shared/server.Cn9ybJtE.d.ts +0 -152
  71. package/dist/shared/server.DJrh0Ceu.d.mts +0 -77
  72. package/dist/shared/server.DPQt9YYq.d.ts +0 -9
  73. package/dist/shared/server.KwueCzFr.mjs +0 -26
  74. package/dist/shared/server.Q6ZmnTgO.mjs +0 -12
  75. package/dist/shared/server.V6zT5iYQ.mjs +0 -379
package/dist/index.mjs CHANGED
@@ -1,14 +1,17 @@
1
- import { mergeErrorMap, mergeMeta, mergeRoute, mergePrefix, mergeTags, isContractProcedure } from '@orpc/contract';
2
- export { ValidationError, eventIterator, type } from '@orpc/contract';
3
- import { P as Procedure, d as addMiddleware, c as createProcedureClient, f as adaptRouter, h as flatLazy, l as lazy, s as setRouterContract, i as isProcedure, j as isLazy, k as createLazyProcedureFormAnyLazy, g as getRouterChild } from './shared/server.V6zT5iYQ.mjs';
4
- export { L as LAZY_LOADER_SYMBOL, a as convertPathToHttpPath, q as createAccessibleLazyRouter, b as createContractedProcedure, n as deepSetLazyRouterPrefix, r as eachAllContractProcedure, e as eachContractProcedure, o as getLazyRouterPrefix, m as getRouterContract, p as middlewareOutputFn, u as unlazy } from './shared/server.V6zT5iYQ.mjs';
1
+ import { mergeErrorMap, mergeMeta, mergeRoute, mergePrefix, mergeTags, isContractProcedure, getContractRouter, fallbackContractConfig } from '@orpc/contract';
2
+ export { ValidationError, eventIterator, type, validateORPCError } from '@orpc/contract';
3
+ import { P as Procedure, b as addMiddleware, c as createProcedureClient, e as enhanceRouter, l as lazy, s as setHiddenRouterContract, u as unlazy, g as getRouter, i as isProcedure, d as isLazy, f as createAssertedLazyProcedure } from './shared/server.Ds4HPpvH.mjs';
4
+ export { L as LAZY_SYMBOL, p as call, r as createAccessibleLazyRouter, a as createContractedProcedure, h as createORPCErrorConstructorMap, q as getHiddenRouterContract, j as getLazyMeta, n as isStartWithMiddlewares, m as mergeCurrentContext, o as mergeMiddlewares, k as middlewareOutputFn, v as resolveContractProcedures, t as traverseContractProcedures, w as unlazyRouter } from './shared/server.Ds4HPpvH.mjs';
5
+ import { toORPCError } from '@orpc/client';
5
6
  export { ORPCError, isDefinedError, safe } from '@orpc/client';
6
- export { onError, onFinish, onStart, onSuccess } from '@orpc/shared';
7
+ import { isObject, resolveMaybeOptionalOptions } from '@orpc/shared';
8
+ export { AsyncIteratorClass, EventPublisher, asyncIteratorToStream as eventIteratorToStream, onError, onFinish, onStart, onSuccess, streamToAsyncIteratorClass as streamToEventIterator } from '@orpc/shared';
7
9
  export { getEventMeta, withEventMeta } from '@orpc/standard-server';
8
10
 
9
11
  const DEFAULT_CONFIG = {
10
12
  initialInputValidationIndex: 0,
11
- initialOutputValidationIndex: 0
13
+ initialOutputValidationIndex: 0,
14
+ dedupeLeadingMiddlewares: true
12
15
  };
13
16
  function fallbackConfig(key, value) {
14
17
  if (value === void 0) {
@@ -18,7 +21,7 @@ function fallbackConfig(key, value) {
18
21
  }
19
22
 
20
23
  function decorateMiddleware(middleware) {
21
- const decorated = middleware;
24
+ const decorated = ((...args) => middleware(...args));
22
25
  decorated.mapInput = (mapInput) => {
23
26
  const mapped = decorateMiddleware(
24
27
  (options, input, ...rest) => middleware(options, mapInput(input), ...rest)
@@ -43,19 +46,56 @@ function decorateMiddleware(middleware) {
43
46
  return decorated;
44
47
  }
45
48
 
49
+ function createActionableClient(client) {
50
+ const action = async (input) => {
51
+ try {
52
+ return [null, await client(input)];
53
+ } catch (error) {
54
+ if (error instanceof Error && "digest" in error && typeof error.digest === "string" && error.digest.startsWith("NEXT_")) {
55
+ throw error;
56
+ }
57
+ if (error instanceof Response && "options" in error && isObject(error.options) || isObject(error) && error.isNotFound === true) {
58
+ throw error;
59
+ }
60
+ return [toORPCError(error).toJSON(), void 0];
61
+ }
62
+ };
63
+ return action;
64
+ }
65
+
46
66
  class DecoratedProcedure extends Procedure {
67
+ /**
68
+ * Adds type-safe custom errors.
69
+ * The provided errors are spared-merged with any existing errors.
70
+ *
71
+ * @see {@link https://orpc.unnoq.com/docs/error-handling#type%E2%80%90safe-error-handling Type-Safe Error Handling Docs}
72
+ */
47
73
  errors(errors) {
48
74
  return new DecoratedProcedure({
49
75
  ...this["~orpc"],
50
76
  errorMap: mergeErrorMap(this["~orpc"].errorMap, errors)
51
77
  });
52
78
  }
79
+ /**
80
+ * Sets or updates the metadata.
81
+ * The provided metadata is spared-merged with any existing metadata.
82
+ *
83
+ * @see {@link https://orpc.unnoq.com/docs/metadata Metadata Docs}
84
+ */
53
85
  meta(meta) {
54
86
  return new DecoratedProcedure({
55
87
  ...this["~orpc"],
56
88
  meta: mergeMeta(this["~orpc"].meta, meta)
57
89
  });
58
90
  }
91
+ /**
92
+ * Sets or updates the route definition.
93
+ * The provided route is spared-merged with any existing route.
94
+ * This option is typically relevant when integrating with OpenAPI.
95
+ *
96
+ * @see {@link https://orpc.unnoq.com/docs/openapi/routing OpenAPI Routing Docs}
97
+ * @see {@link https://orpc.unnoq.com/docs/openapi/input-output-structure OpenAPI Input/Output Structure Docs}
98
+ */
59
99
  route(route) {
60
100
  return new DecoratedProcedure({
61
101
  ...this["~orpc"],
@@ -71,28 +111,51 @@ class DecoratedProcedure extends Procedure {
71
111
  }
72
112
  /**
73
113
  * Make this procedure callable (works like a function while still being a procedure).
114
+ *
115
+ * @see {@link https://orpc.unnoq.com/docs/client/server-side Server-side Client Docs}
74
116
  */
75
117
  callable(...rest) {
76
- return Object.assign(createProcedureClient(this, ...rest), {
77
- "~type": "Procedure",
78
- "~orpc": this["~orpc"]
118
+ const client = createProcedureClient(this, ...rest);
119
+ return new Proxy(client, {
120
+ get: (target, key) => {
121
+ return Reflect.has(this, key) ? Reflect.get(this, key) : Reflect.get(target, key);
122
+ },
123
+ has: (target, key) => {
124
+ return Reflect.has(this, key) || Reflect.has(target, key);
125
+ }
79
126
  });
80
127
  }
81
128
  /**
82
- * Make this procedure compatible with server action (the same as .callable, but the type is compatible with server action).
129
+ * Make this procedure compatible with server action.
130
+ *
131
+ * @see {@link https://orpc.unnoq.com/docs/server-action Server Action Docs}
83
132
  */
84
133
  actionable(...rest) {
85
- return this.callable(...rest);
134
+ const action = createActionableClient(createProcedureClient(this, ...rest));
135
+ return new Proxy(action, {
136
+ get: (target, key) => {
137
+ return Reflect.has(this, key) ? Reflect.get(this, key) : Reflect.get(target, key);
138
+ },
139
+ has: (target, key) => {
140
+ return Reflect.has(this, key) || Reflect.has(target, key);
141
+ }
142
+ });
86
143
  }
87
144
  }
88
145
 
89
146
  class Builder {
147
+ /**
148
+ * This property holds the defined options.
149
+ */
90
150
  "~orpc";
91
151
  constructor(def) {
92
152
  this["~orpc"] = def;
93
153
  }
94
154
  /**
95
- * Reset config
155
+ * Sets or overrides the config.
156
+ *
157
+ * @see {@link https://orpc.unnoq.com/docs/client/server-side#middlewares-order Middlewares Order Docs}
158
+ * @see {@link https://orpc.unnoq.com/docs/best-practices/dedupe-middleware#configuration Dedupe Middleware Docs}
96
159
  */
97
160
  $config(config) {
98
161
  const inputValidationCount = this["~orpc"].inputValidationIndex - fallbackConfig("initialInputValidationIndex", this["~orpc"].config.initialInputValidationIndex);
@@ -100,12 +163,15 @@ class Builder {
100
163
  return new Builder({
101
164
  ...this["~orpc"],
102
165
  config,
166
+ dedupeLeadingMiddlewares: fallbackConfig("dedupeLeadingMiddlewares", config.dedupeLeadingMiddlewares),
103
167
  inputValidationIndex: fallbackConfig("initialInputValidationIndex", config.initialInputValidationIndex) + inputValidationCount,
104
168
  outputValidationIndex: fallbackConfig("initialOutputValidationIndex", config.initialOutputValidationIndex) + outputValidationCount
105
169
  });
106
170
  }
107
171
  /**
108
- * Reset initial context
172
+ * Set or override the initial context.
173
+ *
174
+ * @see {@link https://orpc.unnoq.com/docs/context Context Docs}
109
175
  */
110
176
  $context() {
111
177
  return new Builder({
@@ -116,7 +182,9 @@ class Builder {
116
182
  });
117
183
  }
118
184
  /**
119
- * Reset initial meta
185
+ * Sets or overrides the initial meta.
186
+ *
187
+ * @see {@link https://orpc.unnoq.com/docs/metadata Metadata Docs}
120
188
  */
121
189
  $meta(initialMeta) {
122
190
  return new Builder({
@@ -125,7 +193,11 @@ class Builder {
125
193
  });
126
194
  }
127
195
  /**
128
- * Reset initial route
196
+ * Sets or overrides the initial route.
197
+ * This option is typically relevant when integrating with OpenAPI.
198
+ *
199
+ * @see {@link https://orpc.unnoq.com/docs/openapi/routing OpenAPI Routing Docs}
200
+ * @see {@link https://orpc.unnoq.com/docs/openapi/input-output-structure OpenAPI Input/Output Structure Docs}
129
201
  */
130
202
  $route(initialRoute) {
131
203
  return new Builder({
@@ -133,9 +205,31 @@ class Builder {
133
205
  route: initialRoute
134
206
  });
135
207
  }
208
+ /**
209
+ * Sets or overrides the initial input schema.
210
+ *
211
+ * @see {@link https://orpc.unnoq.com/docs/procedure#initial-configuration Initial Procedure Configuration Docs}
212
+ */
213
+ $input(initialInputSchema) {
214
+ return new Builder({
215
+ ...this["~orpc"],
216
+ inputSchema: initialInputSchema
217
+ });
218
+ }
219
+ /**
220
+ * Creates a middleware.
221
+ *
222
+ * @see {@link https://orpc.unnoq.com/docs/middleware Middleware Docs}
223
+ */
136
224
  middleware(middleware) {
137
225
  return decorateMiddleware(middleware);
138
226
  }
227
+ /**
228
+ * Adds type-safe custom errors.
229
+ * The provided errors are spared-merged with any existing errors.
230
+ *
231
+ * @see {@link https://orpc.unnoq.com/docs/error-handling#type%E2%80%90safe-error-handling Type-Safe Error Handling Docs}
232
+ */
139
233
  errors(errors) {
140
234
  return new Builder({
141
235
  ...this["~orpc"],
@@ -149,18 +243,37 @@ class Builder {
149
243
  middlewares: addMiddleware(this["~orpc"].middlewares, mapped)
150
244
  });
151
245
  }
246
+ /**
247
+ * Sets or updates the metadata.
248
+ * The provided metadata is spared-merged with any existing metadata.
249
+ *
250
+ * @see {@link https://orpc.unnoq.com/docs/metadata Metadata Docs}
251
+ */
152
252
  meta(meta) {
153
253
  return new Builder({
154
254
  ...this["~orpc"],
155
255
  meta: mergeMeta(this["~orpc"].meta, meta)
156
256
  });
157
257
  }
258
+ /**
259
+ * Sets or updates the route definition.
260
+ * The provided route is spared-merged with any existing route.
261
+ * This option is typically relevant when integrating with OpenAPI.
262
+ *
263
+ * @see {@link https://orpc.unnoq.com/docs/openapi/routing OpenAPI Routing Docs}
264
+ * @see {@link https://orpc.unnoq.com/docs/openapi/input-output-structure OpenAPI Input/Output Structure Docs}
265
+ */
158
266
  route(route) {
159
267
  return new Builder({
160
268
  ...this["~orpc"],
161
269
  route: mergeRoute(this["~orpc"].route, route)
162
270
  });
163
271
  }
272
+ /**
273
+ * Defines the input validation schema.
274
+ *
275
+ * @see {@link https://orpc.unnoq.com/docs/procedure#input-output-validation Input Validation Docs}
276
+ */
164
277
  input(schema) {
165
278
  return new Builder({
166
279
  ...this["~orpc"],
@@ -168,6 +281,11 @@ class Builder {
168
281
  inputValidationIndex: fallbackConfig("initialInputValidationIndex", this["~orpc"].config.initialInputValidationIndex) + this["~orpc"].middlewares.length
169
282
  });
170
283
  }
284
+ /**
285
+ * Defines the output validation schema.
286
+ *
287
+ * @see {@link https://orpc.unnoq.com/docs/procedure#input-output-validation Output Validation Docs}
288
+ */
171
289
  output(schema) {
172
290
  return new Builder({
173
291
  ...this["~orpc"],
@@ -175,29 +293,59 @@ class Builder {
175
293
  outputValidationIndex: fallbackConfig("initialOutputValidationIndex", this["~orpc"].config.initialOutputValidationIndex) + this["~orpc"].middlewares.length
176
294
  });
177
295
  }
296
+ /**
297
+ * Defines the handler of the procedure.
298
+ *
299
+ * @see {@link https://orpc.unnoq.com/docs/procedure Procedure Docs}
300
+ */
178
301
  handler(handler) {
179
302
  return new DecoratedProcedure({
180
303
  ...this["~orpc"],
181
304
  handler
182
305
  });
183
306
  }
307
+ /**
308
+ * Prefixes all procedures in the router.
309
+ * The provided prefix is post-appended to any existing router prefix.
310
+ *
311
+ * @note This option does not affect procedures that do not define a path in their route definition.
312
+ *
313
+ * @see {@link https://orpc.unnoq.com/docs/openapi/routing#route-prefixes OpenAPI Route Prefixes Docs}
314
+ */
184
315
  prefix(prefix) {
185
316
  return new Builder({
186
317
  ...this["~orpc"],
187
318
  prefix: mergePrefix(this["~orpc"].prefix, prefix)
188
319
  });
189
320
  }
321
+ /**
322
+ * Adds tags to all procedures in the router.
323
+ * This helpful when you want to group procedures together in the OpenAPI specification.
324
+ *
325
+ * @see {@link https://orpc.unnoq.com/docs/openapi/openapi-specification#operation-metadata OpenAPI Operation Metadata Docs}
326
+ */
190
327
  tag(...tags) {
191
328
  return new Builder({
192
329
  ...this["~orpc"],
193
330
  tags: mergeTags(this["~orpc"].tags, tags)
194
331
  });
195
332
  }
333
+ /**
334
+ * Applies all of the previously defined options to the specified router.
335
+ *
336
+ * @see {@link https://orpc.unnoq.com/docs/router#extending-router Extending Router Docs}
337
+ */
196
338
  router(router) {
197
- return adaptRouter(router, this["~orpc"]);
339
+ return enhanceRouter(router, this["~orpc"]);
198
340
  }
341
+ /**
342
+ * Create a lazy router
343
+ * And applies all of the previously defined options to the specified router.
344
+ *
345
+ * @see {@link https://orpc.unnoq.com/docs/router#extending-router Extending Router Docs}
346
+ */
199
347
  lazy(loader) {
200
- return adaptRouter(flatLazy(lazy(loader)), this["~orpc"]);
348
+ return enhanceRouter(lazy(loader), this["~orpc"]);
201
349
  }
202
350
  }
203
351
  const os = new Builder({
@@ -205,17 +353,12 @@ const os = new Builder({
205
353
  route: {},
206
354
  meta: {},
207
355
  errorMap: {},
208
- inputSchema: void 0,
209
- outputSchema: void 0,
210
356
  inputValidationIndex: fallbackConfig("initialInputValidationIndex"),
211
357
  outputValidationIndex: fallbackConfig("initialOutputValidationIndex"),
212
- middlewares: []
358
+ middlewares: [],
359
+ dedupeLeadingMiddlewares: true
213
360
  });
214
361
 
215
- function mergeContext(context, other) {
216
- return { ...context, ...other };
217
- }
218
-
219
362
  function implementerInternal(contract, config, middlewares) {
220
363
  if (isContractProcedure(contract)) {
221
364
  const impl2 = new Builder({
@@ -223,12 +366,16 @@ function implementerInternal(contract, config, middlewares) {
223
366
  config,
224
367
  middlewares,
225
368
  inputValidationIndex: fallbackConfig("initialInputValidationIndex", config?.initialInputValidationIndex) + middlewares.length,
226
- outputValidationIndex: fallbackConfig("initialOutputValidationIndex", config?.initialOutputValidationIndex) + middlewares.length
369
+ outputValidationIndex: fallbackConfig("initialOutputValidationIndex", config?.initialOutputValidationIndex) + middlewares.length,
370
+ dedupeLeadingMiddlewares: fallbackConfig("dedupeLeadingMiddlewares", config.dedupeLeadingMiddlewares)
227
371
  });
228
372
  return impl2;
229
373
  }
230
374
  const impl = new Proxy(contract, {
231
375
  get: (target, key) => {
376
+ if (typeof key !== "string") {
377
+ return Reflect.get(target, key);
378
+ }
232
379
  let method;
233
380
  if (key === "middleware") {
234
381
  method = (mid) => decorateMiddleware(mid);
@@ -242,23 +389,29 @@ function implementerInternal(contract, config, middlewares) {
242
389
  };
243
390
  } else if (key === "router") {
244
391
  method = (router) => {
245
- const adapted = adaptRouter(router, {
392
+ const adapted = enhanceRouter(router, {
246
393
  middlewares,
247
- errorMap: {}
394
+ errorMap: {},
395
+ prefix: void 0,
396
+ tags: void 0,
397
+ dedupeLeadingMiddlewares: fallbackConfig("dedupeLeadingMiddlewares", config.dedupeLeadingMiddlewares)
248
398
  });
249
- return setRouterContract(adapted, contract);
399
+ return setHiddenRouterContract(adapted, contract);
250
400
  };
251
401
  } else if (key === "lazy") {
252
402
  method = (loader) => {
253
- const adapted = adaptRouter(flatLazy(lazy(loader)), {
403
+ const adapted = enhanceRouter(lazy(loader), {
254
404
  middlewares,
255
- errorMap: {}
405
+ errorMap: {},
406
+ prefix: void 0,
407
+ tags: void 0,
408
+ dedupeLeadingMiddlewares: fallbackConfig("dedupeLeadingMiddlewares", config.dedupeLeadingMiddlewares)
256
409
  });
257
- return setRouterContract(adapted, contract);
410
+ return setHiddenRouterContract(adapted, contract);
258
411
  };
259
412
  }
260
- const next = Reflect.get(target, key);
261
- if (!next || typeof next !== "function" && typeof next !== "object") {
413
+ const next = getContractRouter(target, [key]);
414
+ if (!next) {
262
415
  return method ?? next;
263
416
  }
264
417
  const nextImpl = implementerInternal(next, config, middlewares);
@@ -285,49 +438,55 @@ function implement(contract, config = {}) {
285
438
  method = (config2) => implement(contract, config2);
286
439
  }
287
440
  const next = Reflect.get(target, key);
288
- if (!next || typeof next !== "function" && typeof next !== "object") {
289
- return method ?? next;
441
+ if (!method || !next || typeof next !== "function" && typeof next !== "object") {
442
+ return method || next;
290
443
  }
291
- if (method) {
292
- return new Proxy(method, {
293
- get(_, key2) {
294
- return Reflect.get(next, key2);
295
- }
296
- });
297
- }
298
- return next;
444
+ return new Proxy(method, {
445
+ get(_, key2) {
446
+ return Reflect.get(next, key2);
447
+ }
448
+ });
299
449
  }
300
450
  });
301
451
  return impl;
302
452
  }
303
453
 
304
- function call(procedure, input, ...rest) {
305
- return createProcedureClient(procedure, ...rest)(input);
454
+ function inferRPCMethodFromRouter(router) {
455
+ return async (_, path) => {
456
+ const { default: procedure } = await unlazy(getRouter(router, path));
457
+ if (!isProcedure(procedure)) {
458
+ throw new Error(
459
+ `[inferRPCMethodFromRouter] No valid procedure found at path "${path.join(".")}". This may happen when the router is not properly configured.`
460
+ );
461
+ }
462
+ const method = fallbackContractConfig("defaultMethod", procedure["~orpc"].route.method);
463
+ return method === "HEAD" ? "GET" : method;
464
+ };
306
465
  }
307
466
 
308
467
  function createRouterClient(router, ...rest) {
468
+ const options = resolveMaybeOptionalOptions(rest);
309
469
  if (isProcedure(router)) {
310
- const caller = createProcedureClient(router, ...rest);
470
+ const caller = createProcedureClient(router, options);
311
471
  return caller;
312
472
  }
313
- const procedureCaller = isLazy(router) ? createProcedureClient(createLazyProcedureFormAnyLazy(router), ...rest) : {};
473
+ const procedureCaller = isLazy(router) ? createProcedureClient(createAssertedLazyProcedure(router), options) : {};
314
474
  const recursive = new Proxy(procedureCaller, {
315
475
  get(target, key) {
316
476
  if (typeof key !== "string") {
317
477
  return Reflect.get(target, key);
318
478
  }
319
- const next = getRouterChild(router, key);
479
+ const next = getRouter(router, [key]);
320
480
  if (!next) {
321
481
  return Reflect.get(target, key);
322
482
  }
323
- const [options] = rest;
324
483
  return createRouterClient(next, {
325
- ...options,
326
- path: [...options?.path ?? [], key]
484
+ ...rest[0],
485
+ path: [...rest[0]?.path ?? [], key]
327
486
  });
328
487
  }
329
488
  });
330
489
  return recursive;
331
490
  }
332
491
 
333
- export { Builder, DecoratedProcedure, Procedure, adaptRouter, call, createLazyProcedureFormAnyLazy, createProcedureClient, createRouterClient, decorateMiddleware, fallbackConfig, flatLazy, getRouterChild, implement, implementerInternal, isLazy, isProcedure, lazy, mergeContext, os, setRouterContract };
492
+ export { Builder, DecoratedProcedure, Procedure, addMiddleware, createActionableClient, createAssertedLazyProcedure, createProcedureClient, createRouterClient, decorateMiddleware, enhanceRouter, fallbackConfig, getRouter, implement, implementerInternal, inferRPCMethodFromRouter, isLazy, isProcedure, lazy, os, setHiddenRouterContract, unlazy };
@@ -1,31 +1,172 @@
1
- import { d as StandardHandlerInterceptorOptions, P as Plugin, a as StandardHandlerOptions } from '../shared/server.DJrh0Ceu.mjs';
2
- export { C as CompositePlugin } from '../shared/server.DJrh0Ceu.mjs';
3
- import { Value } from '@orpc/shared';
4
- import { C as Context } from '../shared/server.Cn9ybJtE.mjs';
5
- import '@orpc/contract';
6
- import '@orpc/standard-server';
7
- import '@orpc/client';
1
+ import { Value, Promisable } from '@orpc/shared';
2
+ import { StandardRequest, StandardHeaders } from '@orpc/standard-server';
3
+ import { BatchResponseBodyItem } from '@orpc/standard-server/batch';
4
+ import { d as StandardHandlerInterceptorOptions, g as StandardHandlerPlugin, e as StandardHandlerOptions } from '../shared/server.DBCUJijK.mjs';
5
+ import { C as Context, d as ProcedureClientInterceptorOptions } from '../shared/server.B4BGqy3Y.mjs';
6
+ import { Meta, ORPCError as ORPCError$1 } from '@orpc/contract';
7
+ import { ORPCError } from '@orpc/client';
8
8
 
9
- interface CORSOptions<TContext extends Context> {
10
- origin?: Value<string | string[] | null | undefined, [origin: string, options: StandardHandlerInterceptorOptions<TContext>]>;
11
- timingOrigin?: Value<string | string[] | null | undefined, [origin: string, options: StandardHandlerInterceptorOptions<TContext>]>;
12
- allowMethods?: string[];
13
- allowHeaders?: string[];
9
+ interface BatchHandlerOptions<T extends Context> {
10
+ /**
11
+ * The max size of the batch allowed.
12
+ *
13
+ * @default 10
14
+ */
15
+ maxSize?: Value<Promisable<number>, [StandardHandlerInterceptorOptions<T>]>;
16
+ /**
17
+ * Map the request before processing it.
18
+ *
19
+ * @default merged back batch request headers into the request
20
+ */
21
+ mapRequestItem?(request: StandardRequest, batchOptions: StandardHandlerInterceptorOptions<T>): StandardRequest;
22
+ /**
23
+ * Success batch response status code.
24
+ *
25
+ * @default 207
26
+ */
27
+ successStatus?: Value<Promisable<number>, [responses: Promise<BatchResponseBodyItem>[], batchOptions: StandardHandlerInterceptorOptions<T>]>;
28
+ /**
29
+ * success batch response headers.
30
+ *
31
+ * @default {}
32
+ */
33
+ headers?: Value<Promisable<StandardHeaders>, [responses: Promise<BatchResponseBodyItem>[], batchOptions: StandardHandlerInterceptorOptions<T>]>;
34
+ }
35
+ /**
36
+ * The Batch Requests Plugin allows you to combine multiple requests and responses into a single batch,
37
+ * reducing the overhead of sending each one separately.
38
+ *
39
+ * @see {@link https://orpc.unnoq.com/docs/plugins/batch-requests Batch Requests Plugin Docs}
40
+ */
41
+ declare class BatchHandlerPlugin<T extends Context> implements StandardHandlerPlugin<T> {
42
+ private readonly maxSize;
43
+ private readonly mapRequestItem;
44
+ private readonly successStatus;
45
+ private readonly headers;
46
+ order: number;
47
+ constructor(options?: BatchHandlerOptions<T>);
48
+ init(options: StandardHandlerOptions<T>): void;
49
+ }
50
+
51
+ interface CORSOptions<T extends Context> {
52
+ origin?: Value<Promisable<string | readonly string[] | null | undefined>, [origin: string, options: StandardHandlerInterceptorOptions<T>]>;
53
+ timingOrigin?: Value<Promisable<string | readonly string[] | null | undefined>, [origin: string, options: StandardHandlerInterceptorOptions<T>]>;
54
+ allowMethods?: readonly string[];
55
+ allowHeaders?: readonly string[];
14
56
  maxAge?: number;
15
57
  credentials?: boolean;
16
- exposeHeaders?: string[];
58
+ exposeHeaders?: readonly string[];
17
59
  }
18
- declare class CORSPlugin<TContext extends Context> implements Plugin<TContext> {
60
+ /**
61
+ * CORSPlugin is a plugin for oRPC that allows you to configure CORS for your API.
62
+ *
63
+ * @see {@link https://orpc.unnoq.com/docs/plugins/cors CORS Plugin Docs}
64
+ */
65
+ declare class CORSPlugin<T extends Context> implements StandardHandlerPlugin<T> {
19
66
  private readonly options;
20
- constructor(options?: Partial<CORSOptions<TContext>>);
21
- init(options: StandardHandlerOptions<TContext>): void;
67
+ order: number;
68
+ constructor(options?: CORSOptions<T>);
69
+ init(options: StandardHandlerOptions<T>): void;
70
+ }
71
+
72
+ interface RequestHeadersPluginContext {
73
+ reqHeaders?: Headers;
74
+ }
75
+ /**
76
+ * The Request Headers Plugin injects a `reqHeaders` instance into the context,
77
+ * allowing access to request headers in oRPC.
78
+ *
79
+ * @see {@link https://orpc.unnoq.com/docs/plugins/request-headers Request Headers Plugin Docs}
80
+ */
81
+ declare class RequestHeadersPlugin<T extends RequestHeadersPluginContext> implements StandardHandlerPlugin<T> {
82
+ init(options: StandardHandlerOptions<T>): void;
22
83
  }
23
84
 
24
85
  interface ResponseHeadersPluginContext {
25
86
  resHeaders?: Headers;
26
87
  }
27
- declare class ResponseHeadersPlugin<TContext extends ResponseHeadersPluginContext & Context> implements Plugin<TContext> {
28
- init(options: StandardHandlerOptions<TContext>): void;
88
+ /**
89
+ * The Response Headers Plugin allows you to set response headers in oRPC.
90
+ * It injects a resHeaders instance into the context, enabling you to modify response headers easily.
91
+ *
92
+ * @see {@link https://orpc.unnoq.com/docs/plugins/response-headers Response Headers Plugin Docs}
93
+ */
94
+ declare class ResponseHeadersPlugin<T extends ResponseHeadersPluginContext> implements StandardHandlerPlugin<T> {
95
+ init(options: StandardHandlerOptions<T>): void;
96
+ }
97
+
98
+ interface SimpleCsrfProtectionHandlerPluginOptions<T extends Context> {
99
+ /**
100
+ * The name of the header to check.
101
+ *
102
+ * @default 'x-csrf-token'
103
+ */
104
+ headerName?: Value<Promisable<string>, [options: StandardHandlerInterceptorOptions<T>]>;
105
+ /**
106
+ * The value of the header to check.
107
+ *
108
+ * @default 'orpc'
109
+ *
110
+ */
111
+ headerValue?: Value<Promisable<string>, [options: StandardHandlerInterceptorOptions<T>]>;
112
+ /**
113
+ * Exclude a procedure from the plugin.
114
+ *
115
+ * @default false
116
+ *
117
+ */
118
+ exclude?: Value<Promisable<boolean>, [options: ProcedureClientInterceptorOptions<T, Record<never, never>, Meta>]>;
119
+ /**
120
+ * The error thrown when the CSRF token is invalid.
121
+ *
122
+ * @default new ORPCError('CSRF_TOKEN_MISMATCH', {
123
+ * status: 403,
124
+ * message: 'Invalid CSRF token',
125
+ * })
126
+ */
127
+ error?: InstanceType<typeof ORPCError>;
128
+ }
129
+ /**
130
+ * This plugin adds basic Cross-Site Request Forgery (CSRF) protection to your oRPC application.
131
+ * It helps ensure that requests to your procedures originate from JavaScript code,
132
+ * not from other sources like standard HTML forms or direct browser navigation.
133
+ *
134
+ * @see {@link https://orpc.unnoq.com/docs/plugins/simple-csrf-protection Simple CSRF Protection Plugin Docs}
135
+ */
136
+ declare class SimpleCsrfProtectionHandlerPlugin<T extends Context> implements StandardHandlerPlugin<T> {
137
+ private readonly headerName;
138
+ private readonly headerValue;
139
+ private readonly exclude;
140
+ private readonly error;
141
+ constructor(options?: SimpleCsrfProtectionHandlerPluginOptions<T>);
142
+ order: number;
143
+ init(options: StandardHandlerOptions<T>): void;
144
+ }
145
+
146
+ interface StrictGetMethodPluginOptions {
147
+ /**
148
+ * The error thrown when a GET request is made to a procedure that doesn't allow GET.
149
+ *
150
+ * @default new ORPCError('METHOD_NOT_SUPPORTED')
151
+ */
152
+ error?: InstanceType<typeof ORPCError$1>;
153
+ }
154
+ /**
155
+ * This plugin enhances security by ensuring only procedures explicitly marked to accept GET requests
156
+ * can be called using the HTTP GET method for RPC Protocol. This helps prevent certain types of
157
+ * Cross-Site Request Forgery (CSRF) attacks.
158
+ *
159
+ * @see {@link https://orpc.unnoq.com/docs/plugins/strict-get-method Strict Get Method Plugin Docs}
160
+ */
161
+ declare class StrictGetMethodPlugin<T extends Context> implements StandardHandlerPlugin<T> {
162
+ private readonly error;
163
+ /**
164
+ * make sure execute before batch plugin to get real method
165
+ */
166
+ order: number;
167
+ constructor(options?: StrictGetMethodPluginOptions);
168
+ init(options: StandardHandlerOptions<T>): void;
29
169
  }
30
170
 
31
- export { type CORSOptions, CORSPlugin, Plugin, ResponseHeadersPlugin, type ResponseHeadersPluginContext };
171
+ export { BatchHandlerPlugin, CORSPlugin, RequestHeadersPlugin, ResponseHeadersPlugin, SimpleCsrfProtectionHandlerPlugin, StrictGetMethodPlugin };
172
+ export type { BatchHandlerOptions, CORSOptions, RequestHeadersPluginContext, ResponseHeadersPluginContext, SimpleCsrfProtectionHandlerPluginOptions, StrictGetMethodPluginOptions };