@orpc/server 2.0.0-beta.46 → 2.0.0-beta.48

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 (169) hide show
  1. package/README.md +6 -4
  2. package/dist/adapters/aws-lambda/handler.d.mts +25 -0
  3. package/dist/adapters/aws-lambda/handler.mjs +21 -0
  4. package/dist/adapters/aws-lambda/index.d.mts +14 -49
  5. package/dist/adapters/aws-lambda/index.mjs +2 -39
  6. package/dist/adapters/aws-lambda/rpc-handler.d.mts +18 -0
  7. package/dist/adapters/aws-lambda/rpc-handler.mjs +10 -0
  8. package/dist/adapters/crossws/handler.d.mts +41 -0
  9. package/dist/adapters/crossws/handler.mjs +38 -0
  10. package/dist/adapters/crossws/index.d.mts +2 -63
  11. package/dist/adapters/crossws/index.mjs +2 -68
  12. package/dist/adapters/crossws/rpc-handler.d.mts +15 -0
  13. package/dist/adapters/crossws/rpc-handler.mjs +10 -0
  14. package/dist/adapters/fastify/handler.d.mts +21 -0
  15. package/dist/adapters/fastify/handler.mjs +20 -0
  16. package/dist/adapters/fastify/index.d.mts +2 -42
  17. package/dist/adapters/fastify/index.mjs +2 -39
  18. package/dist/adapters/fastify/rpc-handler.d.mts +14 -0
  19. package/dist/adapters/fastify/rpc-handler.mjs +10 -0
  20. package/dist/adapters/fetch/handler.d.mts +23 -0
  21. package/dist/adapters/fetch/handler.mjs +22 -0
  22. package/dist/adapters/fetch/index.d.mts +6 -46
  23. package/dist/adapters/fetch/index.mjs +4 -41
  24. package/dist/adapters/fetch/rpc-handler.d.mts +16 -0
  25. package/dist/adapters/fetch/rpc-handler.mjs +10 -0
  26. package/dist/adapters/message-port/handler.d.mts +57 -0
  27. package/dist/adapters/message-port/handler.mjs +62 -0
  28. package/dist/adapters/message-port/index.d.mts +2 -77
  29. package/dist/adapters/message-port/index.mjs +2 -87
  30. package/dist/adapters/message-port/rpc-handler.d.mts +15 -0
  31. package/dist/adapters/message-port/rpc-handler.mjs +10 -0
  32. package/dist/adapters/node/handler.d.mts +21 -0
  33. package/dist/adapters/node/handler.mjs +20 -0
  34. package/dist/adapters/node/index.d.mts +6 -43
  35. package/dist/adapters/node/index.mjs +4 -40
  36. package/dist/adapters/node/rpc-handler.d.mts +15 -0
  37. package/dist/adapters/node/rpc-handler.mjs +10 -0
  38. package/dist/adapters/standard/codec.d.mts +16 -0
  39. package/dist/adapters/standard/codec.mjs +1 -0
  40. package/dist/adapters/standard/handler.d.mts +68 -0
  41. package/dist/adapters/standard/handler.mjs +185 -0
  42. package/dist/adapters/standard/index.d.mts +6 -95
  43. package/dist/adapters/standard/index.mjs +5 -8
  44. package/dist/adapters/standard/plugin.d.mts +32 -0
  45. package/dist/adapters/standard/plugin.mjs +16 -0
  46. package/dist/adapters/standard/rpc-handler-codec.d.mts +42 -0
  47. package/dist/adapters/standard/rpc-handler-codec.mjs +51 -0
  48. package/dist/adapters/standard/rpc-matcher.d.mts +53 -0
  49. package/dist/adapters/standard/rpc-matcher.mjs +140 -0
  50. package/dist/adapters/standard/utils.d.mts +10 -0
  51. package/dist/adapters/standard/utils.mjs +6 -0
  52. package/dist/adapters/standard-peer/index.d.mts +1 -11
  53. package/dist/adapters/standard-peer/index.mjs +1 -10
  54. package/dist/adapters/standard-peer/utils.d.mts +4 -0
  55. package/dist/adapters/standard-peer/utils.mjs +11 -0
  56. package/dist/adapters/websocket/handler.d.mts +55 -0
  57. package/dist/adapters/websocket/handler.mjs +55 -0
  58. package/dist/adapters/websocket/index.d.mts +2 -77
  59. package/dist/adapters/websocket/index.mjs +2 -87
  60. package/dist/adapters/websocket/rpc-handler.d.mts +15 -0
  61. package/dist/adapters/websocket/rpc-handler.mjs +10 -0
  62. package/dist/builder-variants.d.mts +267 -0
  63. package/dist/builder-variants.mjs +1 -0
  64. package/dist/builder.d.mts +141 -0
  65. package/dist/builder.mjs +145 -0
  66. package/dist/constants.d.mts +2 -0
  67. package/dist/constants.mjs +2 -0
  68. package/dist/context.d.mts +13 -0
  69. package/dist/context.mjs +1 -0
  70. package/dist/extensions/callable.d.mts +16 -9
  71. package/dist/extensions/callable.mjs +5 -9
  72. package/dist/helpers/base64url.d.mts +31 -0
  73. package/dist/helpers/base64url.mjs +25 -0
  74. package/dist/helpers/cookie.d.mts +50 -0
  75. package/dist/helpers/cookie.mjs +35 -0
  76. package/dist/helpers/encryption.d.mts +28 -0
  77. package/dist/helpers/encryption.mjs +62 -0
  78. package/dist/helpers/index.d.mts +4 -167
  79. package/dist/helpers/index.mjs +4 -200
  80. package/dist/helpers/signing.d.mts +50 -0
  81. package/dist/helpers/signing.mjs +43 -0
  82. package/dist/implementer-procedure.d.mts +36 -0
  83. package/dist/implementer-procedure.mjs +38 -0
  84. package/dist/implementer-router.d.mts +78 -0
  85. package/dist/implementer-router.mjs +77 -0
  86. package/dist/implementer.d.mts +22 -0
  87. package/dist/implementer.mjs +30 -0
  88. package/dist/index.d.mts +57 -476
  89. package/dist/index.mjs +25 -458
  90. package/dist/lazy.d.mts +20 -0
  91. package/dist/lazy.mjs +22 -0
  92. package/dist/middleware-decorated.d.mts +27 -0
  93. package/dist/middleware-decorated.mjs +65 -0
  94. package/dist/middleware.d.mts +70 -0
  95. package/dist/middleware.mjs +1 -0
  96. package/dist/plugins/batch.d.mts +104 -0
  97. package/dist/plugins/batch.mjs +373 -0
  98. package/dist/plugins/cors.d.mts +67 -0
  99. package/dist/plugins/cors.mjs +134 -0
  100. package/dist/plugins/get-method-csrf-protection.d.mts +23 -0
  101. package/dist/plugins/get-method-csrf-protection.mjs +45 -0
  102. package/dist/plugins/head-method.d.mts +23 -0
  103. package/dist/plugins/head-method.mjs +95 -0
  104. package/dist/plugins/index.d.mts +39 -432
  105. package/dist/plugins/index.mjs +23 -967
  106. package/dist/plugins/method-override.d.mts +38 -0
  107. package/dist/plugins/method-override.mjs +50 -0
  108. package/dist/plugins/request-compression.d.mts +16 -0
  109. package/dist/plugins/request-compression.mjs +67 -0
  110. package/dist/plugins/request-headers.d.mts +22 -0
  111. package/dist/plugins/request-headers.mjs +21 -0
  112. package/dist/plugins/request-limit.d.mts +30 -0
  113. package/dist/plugins/request-limit.mjs +53 -0
  114. package/dist/plugins/response-compression.d.mts +59 -0
  115. package/dist/plugins/response-compression.mjs +172 -0
  116. package/dist/plugins/response-headers.d.mts +30 -0
  117. package/dist/plugins/response-headers.mjs +34 -0
  118. package/dist/plugins/rethrow.d.mts +33 -0
  119. package/dist/plugins/rethrow.mjs +46 -0
  120. package/dist/plugins/signal-sync.d.mts +13 -0
  121. package/dist/plugins/signal-sync.mjs +84 -0
  122. package/dist/plugins/timeout.d.mts +37 -0
  123. package/dist/plugins/timeout.mjs +56 -0
  124. package/dist/procedure-client.d.mts +41 -0
  125. package/dist/procedure-client.mjs +157 -0
  126. package/dist/procedure-decorated.d.mts +20 -0
  127. package/dist/procedure-decorated.mjs +41 -0
  128. package/dist/procedure-utils.d.mts +52 -0
  129. package/dist/procedure-utils.mjs +33 -0
  130. package/dist/procedure.d.mts +93 -0
  131. package/dist/procedure.mjs +20 -0
  132. package/dist/router-client.d.mts +27 -0
  133. package/dist/router-client.mjs +35 -0
  134. package/dist/router-hidden.d.mts +5 -0
  135. package/dist/router-hidden.mjs +12 -0
  136. package/dist/router-utils.d.mts +59 -0
  137. package/dist/router-utils.mjs +191 -0
  138. package/dist/router.d.mts +61 -0
  139. package/dist/router.mjs +1 -0
  140. package/package.json +11 -11
  141. package/dist/adapters/aws-lambda/index.d.ts +0 -51
  142. package/dist/adapters/crossws/index.d.ts +0 -63
  143. package/dist/adapters/fastify/index.d.ts +0 -42
  144. package/dist/adapters/fetch/index.d.ts +0 -47
  145. package/dist/adapters/message-port/index.d.ts +0 -77
  146. package/dist/adapters/node/index.d.ts +0 -44
  147. package/dist/adapters/standard/index.d.ts +0 -95
  148. package/dist/adapters/standard-peer/index.d.ts +0 -11
  149. package/dist/adapters/websocket/index.d.ts +0 -77
  150. package/dist/extensions/callable.d.ts +0 -10
  151. package/dist/helpers/index.d.ts +0 -167
  152. package/dist/index.d.ts +0 -476
  153. package/dist/plugins/index.d.ts +0 -432
  154. package/dist/shared/server.1ExWFMze.mjs +0 -69
  155. package/dist/shared/server.9B1jEm5a.d.mts +0 -105
  156. package/dist/shared/server.BB464y-6.mjs +0 -207
  157. package/dist/shared/server.BC5BIu02.d.mts +0 -73
  158. package/dist/shared/server.BSYGskId.d.ts +0 -14
  159. package/dist/shared/server.BvG3boBj.d.mts +0 -14
  160. package/dist/shared/server.CTV_mC2P.d.mts +0 -34
  161. package/dist/shared/server.CYbIpHbH.mjs +0 -239
  162. package/dist/shared/server.CgBF3Vj1.d.ts +0 -34
  163. package/dist/shared/server.Chq5FpXf.mjs +0 -353
  164. package/dist/shared/server.DFPxP2v3.d.mts +0 -166
  165. package/dist/shared/server.DFPxP2v3.d.ts +0 -166
  166. package/dist/shared/server.DOg7ke6y.d.ts +0 -105
  167. package/dist/shared/server.Dz7j9BEh.d.ts +0 -73
  168. package/dist/shared/server.W91HSRkE.mjs +0 -9
  169. package/dist/shared/server.sBQSgqK3.mjs +0 -41
@@ -1,353 +0,0 @@
1
- import { ORPCError, toORPCError, RPCSerializer, COMMON_ERROR_STATUS_MAP } from '@orpc/client';
2
- import { sortPlugins, toArray, matchesHttpPathPrefix, value, intercept, getTracer, runWithSpan, ORPC_NAME, isAsyncIteratorObject, override, traceAsyncIterator, traceReadableStream, recordSpanError, wrapAsyncIterator, toTracingException, wrapReadableStream, pathToHttpPath, normalizeHttpPath, parseEmptyableJSON, getOwn } from '@orpc/shared';
3
- import { flattenStandardHeader, parseStandardUrl, ErrorEvent } from '@standard-server/core';
4
- import { c as createProcedureClient, u as unlazy, P as Procedure } from './server.BB464y-6.mjs';
5
- import { w as walkProcedureContractsSync, r as resolveContractProcedure, D as DEFAULT_SUCCESS_STATUS, a as DEFAULT_ERROR_STATUS } from './server.CYbIpHbH.mjs';
6
-
7
- class CompositeStandardHandlerPlugin {
8
- name = "~composite";
9
- plugins;
10
- constructor(plugins = []) {
11
- this.plugins = sortPlugins(plugins);
12
- }
13
- init(options) {
14
- for (const plugin of this.plugins) {
15
- if (plugin.init) {
16
- options = plugin.init(options);
17
- }
18
- }
19
- return options;
20
- }
21
- }
22
-
23
- class StandardHandler {
24
- constructor(codec, options) {
25
- this.codec = codec;
26
- options = new CompositeStandardHandlerPlugin([
27
- ...toArray(options.plugins),
28
- new TracingHandlerPlugin()
29
- ]).init(options);
30
- this.routingInterceptors = options.routingInterceptors;
31
- this.interceptors = options.interceptors;
32
- this.clientInterceptors = options.clientInterceptors;
33
- }
34
- routingInterceptors;
35
- interceptors;
36
- clientInterceptors;
37
- async handle(request, options) {
38
- const { prefix } = options;
39
- if (prefix && !matchesHttpPathPrefix(request.url, prefix)) {
40
- return { matched: false, response: void 0 };
41
- }
42
- const context = await value(options.context, request);
43
- return intercept(
44
- this.routingInterceptors,
45
- { context, prefix, request },
46
- async ({ context: context2, prefix: prefix2, request: request2 }) => {
47
- const span = getTracer()?.getActiveSpan();
48
- let step;
49
- const matchedOrNot = await runWithSpan("find_procedure", () => this.codec.resolveProcedure(request2, { context: context2, prefix: prefix2 }));
50
- if (!matchedOrNot) {
51
- span?.updateName(`${ORPC_NAME}_no_match`);
52
- span?.setAttribute("http.request.method", request2.method);
53
- span?.setAttribute("url.path", request2.url);
54
- return { matched: false };
55
- }
56
- const { path, procedure, decodeInput } = matchedOrNot;
57
- span?.updateName(`${ORPC_NAME}.${path.join("/")}`);
58
- span?.setAttribute("rpc.system", ORPC_NAME);
59
- span?.setAttribute("rpc.method", path.join("."));
60
- try {
61
- const response = await intercept(
62
- this.interceptors,
63
- { context: context2, prefix: prefix2, request: request2, path, procedure, decodeInput },
64
- async ({ context: context3, prefix: prefix3, request: request3, path: path2, procedure: procedure2, decodeInput: decodeInput2 }) => {
65
- step = "decode_input";
66
- let input = await runWithSpan("decode_input", decodeInput2);
67
- step = void 0;
68
- if (getTracer() && isAsyncIteratorObject(input)) {
69
- input = override(input, traceAsyncIterator("consume_async_iterator_object_input", input));
70
- } else if (getTracer() && input instanceof ReadableStream) {
71
- input = override(input, traceReadableStream("consume_octet_stream_input", input));
72
- }
73
- const client = createProcedureClient(procedure2, {
74
- context: context3,
75
- path: path2,
76
- interceptors: this.clientInterceptors
77
- });
78
- step = "call_procedure";
79
- const output = await client(input, {
80
- signal: request3.signal,
81
- lastEventId: flattenStandardHeader(request3.headers["last-event-id"])
82
- });
83
- step = void 0;
84
- const response2 = await this.codec.encodeOutput(output, procedure2, path2, { context: context3, prefix: prefix3 });
85
- return response2;
86
- }
87
- );
88
- return { matched: true, response };
89
- } catch (e) {
90
- if (step !== "call_procedure") {
91
- recordSpanError(span, e);
92
- }
93
- const error = step === "decode_input" && !(e instanceof ORPCError) ? new ORPCError("BAD_REQUEST", {
94
- message: `Malformed request. Ensure the request body is properly formatted and the 'Content-Type' header is set correctly.`,
95
- cause: e
96
- }) : toORPCError(e);
97
- const response = await this.codec.encodeError(error, procedure, path, { context: context2, prefix: prefix2 });
98
- return { matched: true, response };
99
- }
100
- }
101
- );
102
- }
103
- }
104
- class TracingHandlerPlugin {
105
- name = "~tracing";
106
- init(options) {
107
- return {
108
- ...options,
109
- routingInterceptors: [
110
- // Should be placed before user-provided interceptors to help them access the current active span.
111
- async ({ next, request }) => {
112
- const tracer = getTracer();
113
- if (!tracer) {
114
- return next();
115
- }
116
- const parent = tracer.extract?.(request.headers);
117
- const [pathname] = parseStandardUrl(request.url);
118
- return tracer.startActiveSpan(`${request.method} ${pathname}`, parent, async (span) => {
119
- try {
120
- const result = await next();
121
- if (!result.matched) {
122
- span.end();
123
- return result;
124
- }
125
- const body = result.response.body;
126
- if (isAsyncIteratorObject(body)) {
127
- return {
128
- ...result,
129
- response: {
130
- ...result.response,
131
- /**
132
- * @remarks
133
- * **Warning**: Remember use `override` for remaining special properties
134
- */
135
- body: override(body, wrapAsyncIterator(body, {
136
- runWith: (fn) => tracer.withActiveSpan(span, fn),
137
- onError(error) {
138
- if (!(error instanceof ErrorEvent)) {
139
- span.recordException("error", toTracingException(error));
140
- }
141
- },
142
- onFinish() {
143
- span.end();
144
- }
145
- }))
146
- }
147
- };
148
- }
149
- if (body instanceof ReadableStream) {
150
- return {
151
- ...result,
152
- response: {
153
- ...result.response,
154
- /**
155
- * @remarks
156
- * **Warning**: Remember use `override` for remaining special properties
157
- */
158
- body: override(body, wrapReadableStream(body, {
159
- runWith: (fn) => tracer.withActiveSpan(span, fn),
160
- onError(error) {
161
- span.recordException("error", toTracingException(error));
162
- },
163
- onFinish() {
164
- span.end();
165
- }
166
- }))
167
- }
168
- };
169
- }
170
- span.end();
171
- return result;
172
- } catch (e) {
173
- span.recordException("error", toTracingException(e));
174
- span.end();
175
- throw e;
176
- }
177
- });
178
- },
179
- ...toArray(options.routingInterceptors)
180
- ]
181
- };
182
- }
183
- }
184
-
185
- const RPC_DEFAULT_ALLOW_METHODS = ["POST", "PUT", "PATCH", "DELETE"];
186
- const NORMALIZABLE_PATHNAME_REGEX = /[^\w\-.!~*'()/]/;
187
- class RPCMatcher {
188
- filter;
189
- allowMethodsSet;
190
- allowMethodsFn;
191
- rootRouter;
192
- tree = /* @__PURE__ */ new Map();
193
- pendingLazyRouters = /* @__PURE__ */ new Map();
194
- /** segment count of the deepest pending key, never lowered so it stays an upper bound */
195
- maxPendingLazyRouterDepth = 0;
196
- constructor(router, options = {}) {
197
- this.filter = options.filter ?? true;
198
- const allowMethods = options.allowMethods ?? RPC_DEFAULT_ALLOW_METHODS;
199
- if (typeof allowMethods !== "function") {
200
- this.allowMethodsSet = new Set(allowMethods);
201
- } else {
202
- this.allowMethodsFn = allowMethods;
203
- }
204
- this.rootRouter = router;
205
- this.index(router);
206
- }
207
- index(router, path = []) {
208
- const lazyResults = walkProcedureContractsSync(router, (procedure, procedurePath) => {
209
- if (!value(this.filter, procedure, procedurePath)) {
210
- return;
211
- }
212
- this.tree.set(pathToHttpPath(procedurePath), {
213
- path: procedurePath,
214
- contract: procedure,
215
- // contract-first entries have no implementation yet, resolveProcedure fills it in on demand
216
- procedure: procedure instanceof Procedure ? procedure : void 0
217
- });
218
- }, path);
219
- for (const result of lazyResults) {
220
- this.pendingLazyRouters.set(pathToHttpPath(result.path), result);
221
- this.maxPendingLazyRouterDepth = Math.max(this.maxPendingLazyRouterDepth, result.path.length);
222
- }
223
- }
224
- async match(method, pathname, prefix) {
225
- if (this.allowMethodsSet && !this.allowMethodsSet.has(method)) {
226
- return void 0;
227
- }
228
- if (prefix) {
229
- if (!pathname.startsWith(prefix)) {
230
- return void 0;
231
- }
232
- const charAfterPrefix = pathname[prefix.length];
233
- if (charAfterPrefix === "/") {
234
- pathname = pathname.slice(prefix.length);
235
- } else if (charAfterPrefix === void 0) {
236
- pathname = "/";
237
- } else if (prefix[prefix.length - 1] === "/") {
238
- pathname = pathname.slice(prefix.length - 1);
239
- } else {
240
- return void 0;
241
- }
242
- }
243
- if (pathname.length > 1 && pathname.endsWith("/")) {
244
- pathname = pathname.slice(0, -1);
245
- }
246
- const loading = this.resolvePendingLazyRouters(pathname);
247
- if (loading !== void 0) {
248
- await loading;
249
- }
250
- let entry = this.tree.get(pathname);
251
- if (entry === void 0 && NORMALIZABLE_PATHNAME_REGEX.test(pathname)) {
252
- const normalizedPathname = normalizeHttpPath(pathname);
253
- const normalizedLoading = this.resolvePendingLazyRouters(normalizedPathname);
254
- if (normalizedLoading !== void 0) {
255
- await normalizedLoading;
256
- }
257
- entry = this.tree.get(normalizedPathname);
258
- }
259
- if (entry === void 0) {
260
- return void 0;
261
- }
262
- const procedure = entry.procedure ?? await this.resolveProcedure(entry);
263
- if (this.allowMethodsFn && !this.allowMethodsFn(method, procedure, entry.path)) {
264
- return void 0;
265
- }
266
- return {
267
- path: entry.path,
268
- procedure
269
- };
270
- }
271
- resolvePendingLazyRouters(pathname) {
272
- if (this.pendingLazyRouters.size === 0) {
273
- return;
274
- }
275
- let slashIndex = 0;
276
- for (let depth = 1; slashIndex !== -1 && depth <= this.maxPendingLazyRouterDepth; depth++) {
277
- const nextSlashIndex = pathname.indexOf("/", slashIndex + 1);
278
- const httpPathPrefix = nextSlashIndex === -1 ? pathname : pathname.slice(0, nextSlashIndex);
279
- const pending = this.pendingLazyRouters.get(httpPathPrefix);
280
- if (pending !== void 0) {
281
- return this.loadPendingLazyRouter(httpPathPrefix, pending).then(() => this.resolvePendingLazyRouters(pathname));
282
- }
283
- slashIndex = nextSlashIndex;
284
- }
285
- }
286
- loadPendingLazyRouter(httpPathPrefix, pending) {
287
- if (pending.loading === void 0) {
288
- pending.loading = this.indexPendingLazyRouter(httpPathPrefix, pending).catch((error) => {
289
- pending.loading = void 0;
290
- throw error;
291
- });
292
- }
293
- return pending.loading;
294
- }
295
- async indexPendingLazyRouter(httpPathPrefix, pending) {
296
- const { default: router } = await unlazy(pending.router);
297
- this.index(router, pending.path);
298
- this.pendingLazyRouters.delete(httpPathPrefix);
299
- }
300
- async resolveProcedure(entry) {
301
- entry.procedure = await resolveContractProcedure(this.rootRouter, entry.contract, entry.path);
302
- return entry.procedure;
303
- }
304
- }
305
-
306
- class RPCHandlerCodec {
307
- matcher;
308
- serializer;
309
- errorStatusMap;
310
- outputStatus;
311
- constructor(router, options = {}) {
312
- this.matcher = new RPCMatcher(router, options);
313
- this.serializer = options.serializer ?? new RPCSerializer();
314
- this.errorStatusMap = options.errorStatusMap ?? COMMON_ERROR_STATUS_MAP;
315
- this.outputStatus = options.outputStatus;
316
- }
317
- async resolveProcedure(request, options) {
318
- const [pathname, query] = parseStandardUrl(request.url);
319
- const matched = await this.matcher.match(request.method, pathname, options.prefix);
320
- if (!matched) {
321
- return void 0;
322
- }
323
- return {
324
- procedure: matched.procedure,
325
- path: matched.path,
326
- decodeInput: async () => {
327
- if (request.method === "GET") {
328
- const dataString = new URLSearchParams(query).getAll("data").at(-1);
329
- return this.serializer.deserialize(parseEmptyableJSON(dataString));
330
- }
331
- const body = await request.resolveBody();
332
- return this.serializer.deserialize(body);
333
- }
334
- };
335
- }
336
- encodeOutput(output, procedure, path, options) {
337
- return {
338
- headers: {},
339
- status: value(this.outputStatus, output, procedure, path, options) ?? DEFAULT_SUCCESS_STATUS,
340
- body: this.serializer.serialize(output)
341
- };
342
- }
343
- encodeError(error, _procedure, _path, _options) {
344
- const status = getOwn(this.errorStatusMap, error.code) ?? DEFAULT_ERROR_STATUS;
345
- return {
346
- headers: {},
347
- status,
348
- body: this.serializer.serialize(error.toJSON())
349
- };
350
- }
351
- }
352
-
353
- export { RPCHandlerCodec as R, StandardHandler as S, RPCMatcher as a, RPC_DEFAULT_ALLOW_METHODS as b };
@@ -1,166 +0,0 @@
1
- import { ClientContext, Client } from '@orpc/client';
2
- import { ErrorMap, AnyMetaPlugin, ORPCErrorConstructorMap, AnySchema, ProcedureContractDefinition, Meta, InferSchemaOutput, ORPCErrorFromErrorMap, InferSchemaInput } from '@orpc/contract';
3
- import { DistributiveOmit, MaybeOptionalOptions, Promisable, Interceptor, PromiseWithError, ThrowableError, Value } from '@orpc/shared';
4
-
5
- interface Context {
6
- [key: PropertyKey]: any;
7
- }
8
- type MergedInitialContext<TInitial extends Context, TOutContext extends Context, TInContext extends Context> = TInContext extends any ? Exclude<keyof TInContext, keyof TInitial | keyof TOutContext> extends never ? TInitial : TInitial & DistributiveOmit<TInContext, keyof TInitial | keyof TOutContext> : never;
9
- type MergedContext<TCurrent extends Context, TOutContext extends Context> = keyof TOutContext extends never ? TCurrent : DistributiveOmit<TCurrent, keyof TOutContext> & TOutContext;
10
-
11
- type MiddlewareResult<TOutContext extends Context, TOutput> = Promisable<{
12
- output: TOutput;
13
- context: TOutContext;
14
- }>;
15
- type MiddlewareNextOptions<TOutContext extends Context> = (object extends TOutContext ? {
16
- context?: TOutContext | undefined;
17
- } : {
18
- context: TOutContext;
19
- }) & {
20
- /** Overrides the signal for the rest of the chain. */
21
- signal?: AbortSignal | undefined;
22
- };
23
- interface MiddlewareNext<TOutput> {
24
- <T extends Context = object>(...rest: MaybeOptionalOptions<MiddlewareNextOptions<T>>): MiddlewareResult<T, TOutput>;
25
- }
26
- type MiddlewareDoneOptions<TOutContext, TOutput> = (object extends TOutContext ? {
27
- context?: TOutContext | undefined;
28
- } : {
29
- context: TOutContext;
30
- }) & {
31
- output: TOutput;
32
- };
33
- interface MiddlewareDone<TOutput> {
34
- /**
35
- * Create a successful result and terminate the middleware chain early.
36
- */
37
- <TOutContext extends Context = object>(...rest: MaybeOptionalOptions<MiddlewareDoneOptions<TOutContext, TOutput>>): MiddlewareResult<TOutContext, TOutput>;
38
- }
39
- interface MiddlewareOptions<TInContext extends Context, TOutput, TErrorConstructorMap extends ORPCErrorConstructorMap<any>> {
40
- context: TInContext;
41
- path: string[];
42
- procedure: AnyProcedure;
43
- signal?: AbortSignal | undefined;
44
- lastEventId: string | undefined;
45
- /**
46
- * Invoke to continue the middleware chain.
47
- */
48
- next: MiddlewareNext<TOutput>;
49
- errors: TErrorConstructorMap;
50
- }
51
- interface MiddlewareDefinition<TErrorMap extends ErrorMap> {
52
- errorMap?: TErrorMap | undefined;
53
- metaPlugins?: AnyMetaPlugin[] | undefined;
54
- }
55
- interface Middleware<TInContext extends Context, TOutContext extends Context, TInput, TOutput, TErrorMap extends ErrorMap> {
56
- /** this property should be optional to support inline middleware */
57
- '~orpc'?: MiddlewareDefinition<TErrorMap> | undefined;
58
- (opts: MiddlewareOptions<TInContext, TOutput, ORPCErrorConstructorMap<TErrorMap>>, input: TInput, done: MiddlewareDone<TOutput>): Promisable<MiddlewareResult<TOutContext, TOutput>>;
59
- }
60
- type AnyMiddleware = Middleware<any, any, any, any, any>;
61
-
62
- interface ProcedureHandlerOptions<TCurrentContext extends Context, TInput, TErrorConstructorMap extends ORPCErrorConstructorMap<any>> {
63
- context: TCurrentContext;
64
- input: TInput;
65
- path: string[];
66
- procedure: Procedure<Context, Context, AnySchema, AnySchema, ErrorMap>;
67
- signal?: AbortSignal | undefined;
68
- lastEventId?: string | undefined;
69
- errors: TErrorConstructorMap;
70
- }
71
- interface ProcedureHandler<TCurrentContext extends Context, TInput, THandlerOutput, TErrorConstructorMap extends ORPCErrorConstructorMap<any>> {
72
- (opts: ProcedureHandlerOptions<TCurrentContext, TInput, TErrorConstructorMap>, input: TInput): Promisable<THandlerOutput>;
73
- }
74
- interface OrderedMiddleware {
75
- /**
76
- * Snapshot of `inputSchemas.length`
77
- * at the time this middleware was used.
78
- *
79
- * @default 0
80
- */
81
- inputSchemasLengthAtUse?: number | undefined;
82
- /**
83
- * Snapshot of `outputSchemas.length`
84
- * at the time this middleware was used.
85
- *
86
- * @default 0
87
- */
88
- outputSchemasLengthAtUse?: number | undefined;
89
- middleware: AnyMiddleware;
90
- }
91
- interface ProcedureConfig {
92
- /**
93
- * When enabled, input schemas are not validated at runtime.
94
- * Schemas are still used for type inference and OpenAPI generation.
95
- *
96
- * @remarks
97
- * **Warning**: Do not disable validation for schemas that transform values.
98
- *
99
- * @default false
100
- */
101
- disableInputValidation?: boolean | undefined;
102
- /**
103
- * When enabled, output schemas are not validated at runtime.
104
- * Schemas are still used for type inference and OpenAPI generation.
105
- *
106
- * Useful when output schemas exist only for specification generation.
107
- *
108
- * @remarks
109
- * **Warning**: Do not disable validation for schemas that transform values.
110
- *
111
- * @default false
112
- */
113
- disableOutputValidation?: boolean | undefined;
114
- }
115
- interface ProcedureDefinition<TInitialContext extends Context, TInjectedContext extends Context, TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap> extends ProcedureContractDefinition<TInputSchema, TOutputSchema, TErrorMap>, ProcedureConfig {
116
- __TInitialContext?: (type: TInitialContext) => unknown;
117
- __TInjectedContext?: (type: TInjectedContext) => unknown;
118
- orderedMiddlewares: OrderedMiddleware[];
119
- handler: ProcedureHandler<any, any, any, any>;
120
- }
121
- declare class Procedure<TInitialContext extends Context, TInjectedContext extends Context, TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap> {
122
- '~orpc': ProcedureDefinition<TInitialContext, TInjectedContext, TInputSchema, TOutputSchema, TErrorMap>;
123
- constructor(def: ProcedureDefinition<TInitialContext, TInjectedContext, TInputSchema, TOutputSchema, TErrorMap>);
124
- /**
125
- * Checks if the given instance satisfies the {@link Procedure} class/interface.
126
- */
127
- static [Symbol.hasInstance](instance: unknown): boolean;
128
- }
129
- type AnyProcedure = Procedure<any, any, any, any, any>;
130
-
131
- interface LazyDefinition<T> {
132
- meta: Meta;
133
- metaPlugins?: AnyMetaPlugin[] | undefined;
134
- loader: () => Promise<{
135
- default: T;
136
- }>;
137
- }
138
- declare class Lazy<T> {
139
- '~orpc': LazyDefinition<T>;
140
- constructor(def: LazyDefinition<T>);
141
- /**
142
- * Checks if the given instance satisfies the {@link Lazy} class/interface.
143
- */
144
- static [Symbol.hasInstance](instance: unknown): boolean;
145
- }
146
- type Lazyable<T> = T | Lazy<T>;
147
- declare function unlazy<T extends Lazyable<any>>(maybeLazy: T): Promise<{
148
- default: T extends Lazy<infer U> ? U : T;
149
- }>;
150
-
151
- type ProcedureClient<TClientContext extends ClientContext, TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap> = Client<TClientContext, InferSchemaInput<TInputSchema>, InferSchemaOutput<TOutputSchema>, ORPCErrorFromErrorMap<TErrorMap> | ThrowableError>;
152
- interface ProcedureClientInterceptorOptions<TInitialContext extends Context, TErrorMap extends ErrorMap> extends ProcedureHandlerOptions<TInitialContext, unknown, ORPCErrorConstructorMap<TErrorMap>> {
153
- }
154
- type ProcedureClientInterceptor<TInitialContext extends Context, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap> = Interceptor<ProcedureClientInterceptorOptions<TInitialContext, TErrorMap>, PromiseWithError<InferSchemaOutput<TOutputSchema>, ORPCErrorFromErrorMap<TErrorMap> | ThrowableError>>;
155
- type ProcedureClientOptions<TInitialContext extends Context, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TClientContext extends ClientContext> = {
156
- path?: string[] | undefined;
157
- interceptors?: ProcedureClientInterceptor<TInitialContext, TOutputSchema, TErrorMap>[] | undefined;
158
- } & (object extends TInitialContext ? {
159
- context?: Value<Promisable<TInitialContext>, [clientContext: TClientContext]> | undefined;
160
- } : {
161
- context: Value<Promisable<TInitialContext>, [clientContext: TClientContext]>;
162
- });
163
- declare function createProcedureClient<TInitialContext extends Context, TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TClientContext extends ClientContext = object>(lazyableProcedure: Lazyable<Procedure<TInitialContext, any, TInputSchema, TOutputSchema, TErrorMap>>, ...rest: MaybeOptionalOptions<ProcedureClientOptions<TInitialContext, TOutputSchema, TErrorMap, TClientContext>>): ProcedureClient<TClientContext, TInputSchema, TOutputSchema, TErrorMap>;
164
-
165
- export { Procedure as a, Lazy as f, createProcedureClient as u, unlazy as v };
166
- export type { AnyProcedure as A, Context as C, Lazyable as L, MergedContext as M, OrderedMiddleware as O, ProcedureClientOptions as P, ProcedureClient as b, ProcedureClientInterceptor as c, Middleware as d, MergedInitialContext as e, ProcedureHandler as g, ProcedureConfig as h, AnyMiddleware as i, LazyDefinition as j, MiddlewareDefinition as k, MiddlewareDone as l, MiddlewareDoneOptions as m, MiddlewareNext as n, MiddlewareNextOptions as o, MiddlewareOptions as p, MiddlewareResult as q, ProcedureClientInterceptorOptions as r, ProcedureDefinition as s, ProcedureHandlerOptions as t };
@@ -1,166 +0,0 @@
1
- import { ClientContext, Client } from '@orpc/client';
2
- import { ErrorMap, AnyMetaPlugin, ORPCErrorConstructorMap, AnySchema, ProcedureContractDefinition, Meta, InferSchemaOutput, ORPCErrorFromErrorMap, InferSchemaInput } from '@orpc/contract';
3
- import { DistributiveOmit, MaybeOptionalOptions, Promisable, Interceptor, PromiseWithError, ThrowableError, Value } from '@orpc/shared';
4
-
5
- interface Context {
6
- [key: PropertyKey]: any;
7
- }
8
- type MergedInitialContext<TInitial extends Context, TOutContext extends Context, TInContext extends Context> = TInContext extends any ? Exclude<keyof TInContext, keyof TInitial | keyof TOutContext> extends never ? TInitial : TInitial & DistributiveOmit<TInContext, keyof TInitial | keyof TOutContext> : never;
9
- type MergedContext<TCurrent extends Context, TOutContext extends Context> = keyof TOutContext extends never ? TCurrent : DistributiveOmit<TCurrent, keyof TOutContext> & TOutContext;
10
-
11
- type MiddlewareResult<TOutContext extends Context, TOutput> = Promisable<{
12
- output: TOutput;
13
- context: TOutContext;
14
- }>;
15
- type MiddlewareNextOptions<TOutContext extends Context> = (object extends TOutContext ? {
16
- context?: TOutContext | undefined;
17
- } : {
18
- context: TOutContext;
19
- }) & {
20
- /** Overrides the signal for the rest of the chain. */
21
- signal?: AbortSignal | undefined;
22
- };
23
- interface MiddlewareNext<TOutput> {
24
- <T extends Context = object>(...rest: MaybeOptionalOptions<MiddlewareNextOptions<T>>): MiddlewareResult<T, TOutput>;
25
- }
26
- type MiddlewareDoneOptions<TOutContext, TOutput> = (object extends TOutContext ? {
27
- context?: TOutContext | undefined;
28
- } : {
29
- context: TOutContext;
30
- }) & {
31
- output: TOutput;
32
- };
33
- interface MiddlewareDone<TOutput> {
34
- /**
35
- * Create a successful result and terminate the middleware chain early.
36
- */
37
- <TOutContext extends Context = object>(...rest: MaybeOptionalOptions<MiddlewareDoneOptions<TOutContext, TOutput>>): MiddlewareResult<TOutContext, TOutput>;
38
- }
39
- interface MiddlewareOptions<TInContext extends Context, TOutput, TErrorConstructorMap extends ORPCErrorConstructorMap<any>> {
40
- context: TInContext;
41
- path: string[];
42
- procedure: AnyProcedure;
43
- signal?: AbortSignal | undefined;
44
- lastEventId: string | undefined;
45
- /**
46
- * Invoke to continue the middleware chain.
47
- */
48
- next: MiddlewareNext<TOutput>;
49
- errors: TErrorConstructorMap;
50
- }
51
- interface MiddlewareDefinition<TErrorMap extends ErrorMap> {
52
- errorMap?: TErrorMap | undefined;
53
- metaPlugins?: AnyMetaPlugin[] | undefined;
54
- }
55
- interface Middleware<TInContext extends Context, TOutContext extends Context, TInput, TOutput, TErrorMap extends ErrorMap> {
56
- /** this property should be optional to support inline middleware */
57
- '~orpc'?: MiddlewareDefinition<TErrorMap> | undefined;
58
- (opts: MiddlewareOptions<TInContext, TOutput, ORPCErrorConstructorMap<TErrorMap>>, input: TInput, done: MiddlewareDone<TOutput>): Promisable<MiddlewareResult<TOutContext, TOutput>>;
59
- }
60
- type AnyMiddleware = Middleware<any, any, any, any, any>;
61
-
62
- interface ProcedureHandlerOptions<TCurrentContext extends Context, TInput, TErrorConstructorMap extends ORPCErrorConstructorMap<any>> {
63
- context: TCurrentContext;
64
- input: TInput;
65
- path: string[];
66
- procedure: Procedure<Context, Context, AnySchema, AnySchema, ErrorMap>;
67
- signal?: AbortSignal | undefined;
68
- lastEventId?: string | undefined;
69
- errors: TErrorConstructorMap;
70
- }
71
- interface ProcedureHandler<TCurrentContext extends Context, TInput, THandlerOutput, TErrorConstructorMap extends ORPCErrorConstructorMap<any>> {
72
- (opts: ProcedureHandlerOptions<TCurrentContext, TInput, TErrorConstructorMap>, input: TInput): Promisable<THandlerOutput>;
73
- }
74
- interface OrderedMiddleware {
75
- /**
76
- * Snapshot of `inputSchemas.length`
77
- * at the time this middleware was used.
78
- *
79
- * @default 0
80
- */
81
- inputSchemasLengthAtUse?: number | undefined;
82
- /**
83
- * Snapshot of `outputSchemas.length`
84
- * at the time this middleware was used.
85
- *
86
- * @default 0
87
- */
88
- outputSchemasLengthAtUse?: number | undefined;
89
- middleware: AnyMiddleware;
90
- }
91
- interface ProcedureConfig {
92
- /**
93
- * When enabled, input schemas are not validated at runtime.
94
- * Schemas are still used for type inference and OpenAPI generation.
95
- *
96
- * @remarks
97
- * **Warning**: Do not disable validation for schemas that transform values.
98
- *
99
- * @default false
100
- */
101
- disableInputValidation?: boolean | undefined;
102
- /**
103
- * When enabled, output schemas are not validated at runtime.
104
- * Schemas are still used for type inference and OpenAPI generation.
105
- *
106
- * Useful when output schemas exist only for specification generation.
107
- *
108
- * @remarks
109
- * **Warning**: Do not disable validation for schemas that transform values.
110
- *
111
- * @default false
112
- */
113
- disableOutputValidation?: boolean | undefined;
114
- }
115
- interface ProcedureDefinition<TInitialContext extends Context, TInjectedContext extends Context, TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap> extends ProcedureContractDefinition<TInputSchema, TOutputSchema, TErrorMap>, ProcedureConfig {
116
- __TInitialContext?: (type: TInitialContext) => unknown;
117
- __TInjectedContext?: (type: TInjectedContext) => unknown;
118
- orderedMiddlewares: OrderedMiddleware[];
119
- handler: ProcedureHandler<any, any, any, any>;
120
- }
121
- declare class Procedure<TInitialContext extends Context, TInjectedContext extends Context, TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap> {
122
- '~orpc': ProcedureDefinition<TInitialContext, TInjectedContext, TInputSchema, TOutputSchema, TErrorMap>;
123
- constructor(def: ProcedureDefinition<TInitialContext, TInjectedContext, TInputSchema, TOutputSchema, TErrorMap>);
124
- /**
125
- * Checks if the given instance satisfies the {@link Procedure} class/interface.
126
- */
127
- static [Symbol.hasInstance](instance: unknown): boolean;
128
- }
129
- type AnyProcedure = Procedure<any, any, any, any, any>;
130
-
131
- interface LazyDefinition<T> {
132
- meta: Meta;
133
- metaPlugins?: AnyMetaPlugin[] | undefined;
134
- loader: () => Promise<{
135
- default: T;
136
- }>;
137
- }
138
- declare class Lazy<T> {
139
- '~orpc': LazyDefinition<T>;
140
- constructor(def: LazyDefinition<T>);
141
- /**
142
- * Checks if the given instance satisfies the {@link Lazy} class/interface.
143
- */
144
- static [Symbol.hasInstance](instance: unknown): boolean;
145
- }
146
- type Lazyable<T> = T | Lazy<T>;
147
- declare function unlazy<T extends Lazyable<any>>(maybeLazy: T): Promise<{
148
- default: T extends Lazy<infer U> ? U : T;
149
- }>;
150
-
151
- type ProcedureClient<TClientContext extends ClientContext, TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap> = Client<TClientContext, InferSchemaInput<TInputSchema>, InferSchemaOutput<TOutputSchema>, ORPCErrorFromErrorMap<TErrorMap> | ThrowableError>;
152
- interface ProcedureClientInterceptorOptions<TInitialContext extends Context, TErrorMap extends ErrorMap> extends ProcedureHandlerOptions<TInitialContext, unknown, ORPCErrorConstructorMap<TErrorMap>> {
153
- }
154
- type ProcedureClientInterceptor<TInitialContext extends Context, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap> = Interceptor<ProcedureClientInterceptorOptions<TInitialContext, TErrorMap>, PromiseWithError<InferSchemaOutput<TOutputSchema>, ORPCErrorFromErrorMap<TErrorMap> | ThrowableError>>;
155
- type ProcedureClientOptions<TInitialContext extends Context, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TClientContext extends ClientContext> = {
156
- path?: string[] | undefined;
157
- interceptors?: ProcedureClientInterceptor<TInitialContext, TOutputSchema, TErrorMap>[] | undefined;
158
- } & (object extends TInitialContext ? {
159
- context?: Value<Promisable<TInitialContext>, [clientContext: TClientContext]> | undefined;
160
- } : {
161
- context: Value<Promisable<TInitialContext>, [clientContext: TClientContext]>;
162
- });
163
- declare function createProcedureClient<TInitialContext extends Context, TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TClientContext extends ClientContext = object>(lazyableProcedure: Lazyable<Procedure<TInitialContext, any, TInputSchema, TOutputSchema, TErrorMap>>, ...rest: MaybeOptionalOptions<ProcedureClientOptions<TInitialContext, TOutputSchema, TErrorMap, TClientContext>>): ProcedureClient<TClientContext, TInputSchema, TOutputSchema, TErrorMap>;
164
-
165
- export { Procedure as a, Lazy as f, createProcedureClient as u, unlazy as v };
166
- export type { AnyProcedure as A, Context as C, Lazyable as L, MergedContext as M, OrderedMiddleware as O, ProcedureClientOptions as P, ProcedureClient as b, ProcedureClientInterceptor as c, Middleware as d, MergedInitialContext as e, ProcedureHandler as g, ProcedureConfig as h, AnyMiddleware as i, LazyDefinition as j, MiddlewareDefinition as k, MiddlewareDone as l, MiddlewareDoneOptions as m, MiddlewareNext as n, MiddlewareNextOptions as o, MiddlewareOptions as p, MiddlewareResult as q, ProcedureClientInterceptorOptions as r, ProcedureDefinition as s, ProcedureHandlerOptions as t };