@orpc/server 0.34.0 → 0.35.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (43) hide show
  1. package/dist/chunk-JBPBLCBJ.js +136 -0
  2. package/dist/{chunk-KK4SDLC7.js → chunk-NOA3GBJQ.js} +69 -9
  3. package/dist/chunk-OWMUECGT.js +111 -0
  4. package/dist/chunk-SUGWQSMQ.js +308 -0
  5. package/dist/fetch.js +12 -9
  6. package/dist/hono.js +25 -13
  7. package/dist/index.js +14 -1
  8. package/dist/next.js +13 -10
  9. package/dist/node.js +149 -61
  10. package/dist/plugins.js +11 -0
  11. package/dist/src/adapters/fetch/index.d.ts +2 -4
  12. package/dist/src/adapters/fetch/rpc-handler.d.ts +10 -0
  13. package/dist/src/adapters/fetch/types.d.ts +2 -10
  14. package/dist/src/adapters/fetch/utils.d.ts +6 -0
  15. package/dist/src/adapters/hono/middleware.d.ts +3 -2
  16. package/dist/src/adapters/next/serve.d.ts +3 -2
  17. package/dist/src/adapters/node/index.d.ts +2 -3
  18. package/dist/src/adapters/node/rpc-handler.d.ts +10 -0
  19. package/dist/src/adapters/node/types.d.ts +13 -14
  20. package/dist/src/adapters/node/utils.d.ts +5 -0
  21. package/dist/src/adapters/standard/handler.d.ts +47 -0
  22. package/dist/src/adapters/standard/index.d.ts +7 -0
  23. package/dist/src/adapters/standard/rpc-codec.d.ts +15 -0
  24. package/dist/src/adapters/standard/rpc-handler.d.ts +8 -0
  25. package/dist/src/adapters/standard/rpc-matcher.d.ts +10 -0
  26. package/dist/src/adapters/standard/rpc-serializer.d.ts +16 -0
  27. package/dist/src/adapters/standard/types.d.ts +44 -0
  28. package/dist/src/index.d.ts +2 -0
  29. package/dist/src/plugins/base.d.ts +11 -0
  30. package/dist/src/plugins/cors.d.ts +18 -0
  31. package/dist/src/plugins/index.d.ts +4 -0
  32. package/dist/src/plugins/response-headers.d.ts +10 -0
  33. package/dist/src/utils.d.ts +24 -0
  34. package/dist/standard.js +17 -0
  35. package/package.json +17 -3
  36. package/dist/chunk-ESTRJAOX.js +0 -299
  37. package/dist/chunk-WUOGVGWG.js +0 -1
  38. package/dist/src/adapters/fetch/orpc-handler.d.ts +0 -20
  39. package/dist/src/adapters/fetch/orpc-payload-codec.d.ts +0 -16
  40. package/dist/src/adapters/fetch/orpc-procedure-matcher.d.ts +0 -12
  41. package/dist/src/adapters/fetch/super-json.d.ts +0 -12
  42. package/dist/src/adapters/node/orpc-handler.d.ts +0 -12
  43. package/dist/src/adapters/node/request-listener.d.ts +0 -28
package/dist/hono.js CHANGED
@@ -1,30 +1,42 @@
1
- import "./chunk-WUOGVGWG.js";
2
1
  import {
3
- ORPCPayloadCodec,
4
- ORPCProcedureMatcher,
5
2
  RPCHandler,
6
- super_json_exports
7
- } from "./chunk-ESTRJAOX.js";
8
- import "./chunk-KK4SDLC7.js";
3
+ fetchReToStandardBody,
4
+ fetchRequestToStandardRequest,
5
+ standardBodyToFetchBody,
6
+ standardResponseToFetchResponse
7
+ } from "./chunk-JBPBLCBJ.js";
8
+ import "./chunk-SUGWQSMQ.js";
9
+ import "./chunk-NOA3GBJQ.js";
10
+ import "./chunk-OWMUECGT.js";
9
11
 
10
12
  // src/adapters/hono/middleware.ts
11
13
  import { value } from "@orpc/shared";
12
14
  function createMiddleware(handler, ...[options]) {
13
15
  return async (c, next) => {
16
+ const bodyProps = /* @__PURE__ */ new Set(["arrayBuffer", "blob", "formData", "json", "text"]);
17
+ const request = c.req.method === "GET" || c.req.method === "HEAD" ? c.req.raw : new Proxy(c.req.raw, {
18
+ // https://github.com/honojs/middleware/blob/main/packages/trpc-server/src/index.ts#L39
19
+ get(target, prop) {
20
+ if (bodyProps.has(prop)) {
21
+ return () => c.req[prop]();
22
+ }
23
+ return Reflect.get(target, prop, target);
24
+ }
25
+ });
14
26
  const context = await value(options?.context ?? {}, c);
15
- const { matched, response } = await handler.handle(c.req.raw, { ...options, context });
27
+ const { matched, response } = await handler.handle(request, { ...options, context });
16
28
  if (matched) {
17
- c.res = response;
18
- return;
29
+ return c.body(response.body, response);
19
30
  }
20
31
  await next();
21
32
  };
22
33
  }
23
34
  export {
24
- ORPCPayloadCodec,
25
- ORPCProcedureMatcher,
26
35
  RPCHandler,
27
- super_json_exports as SuperJSON,
28
- createMiddleware
36
+ createMiddleware,
37
+ fetchReToStandardBody,
38
+ fetchRequestToStandardRequest,
39
+ standardBodyToFetchBody,
40
+ standardResponseToFetchResponse
29
41
  };
30
42
  //# sourceMappingURL=hono.js.map
package/dist/index.js CHANGED
@@ -3,10 +3,14 @@ import {
3
3
  Procedure,
4
4
  adaptRouter,
5
5
  addMiddleware,
6
+ convertPathToHttpPath,
6
7
  createAccessibleLazyRouter,
8
+ createContractedProcedure,
7
9
  createLazyProcedureFormAnyLazy,
8
10
  createProcedureClient,
9
11
  deepSetLazyRouterPrefix,
12
+ eachAllContractProcedure,
13
+ eachContractProcedure,
10
14
  flatLazy,
11
15
  getLazyRouterPrefix,
12
16
  getRouterChild,
@@ -17,7 +21,7 @@ import {
17
21
  middlewareOutputFn,
18
22
  setRouterContract,
19
23
  unlazy
20
- } from "./chunk-KK4SDLC7.js";
24
+ } from "./chunk-NOA3GBJQ.js";
21
25
 
22
26
  // src/builder.ts
23
27
  import { mergeErrorMap as mergeErrorMap2, mergeMeta as mergeMeta2, mergePrefix, mergeRoute as mergeRoute2, mergeTags } from "@orpc/contract";
@@ -354,6 +358,7 @@ function createRouterClient(router, ...rest) {
354
358
 
355
359
  // src/index.ts
356
360
  import { isDefinedError, ORPCError, safe, type, ValidationError } from "@orpc/contract";
361
+ import { onError, onFinish, onStart, onSuccess } from "@orpc/shared";
357
362
  export {
358
363
  Builder,
359
364
  DecoratedProcedure,
@@ -363,12 +368,16 @@ export {
363
368
  ValidationError,
364
369
  adaptRouter,
365
370
  call,
371
+ convertPathToHttpPath,
366
372
  createAccessibleLazyRouter,
373
+ createContractedProcedure,
367
374
  createLazyProcedureFormAnyLazy,
368
375
  createProcedureClient,
369
376
  createRouterClient,
370
377
  decorateMiddleware,
371
378
  deepSetLazyRouterPrefix,
379
+ eachAllContractProcedure,
380
+ eachContractProcedure,
372
381
  fallbackConfig,
373
382
  flatLazy,
374
383
  getLazyRouterPrefix,
@@ -382,6 +391,10 @@ export {
382
391
  lazy,
383
392
  mergeContext,
384
393
  middlewareOutputFn,
394
+ onError,
395
+ onFinish,
396
+ onStart,
397
+ onSuccess,
385
398
  os,
386
399
  safe,
387
400
  setRouterContract,
package/dist/next.js CHANGED
@@ -1,11 +1,13 @@
1
- import "./chunk-WUOGVGWG.js";
2
1
  import {
3
- ORPCPayloadCodec,
4
- ORPCProcedureMatcher,
5
2
  RPCHandler,
6
- super_json_exports
7
- } from "./chunk-ESTRJAOX.js";
8
- import "./chunk-KK4SDLC7.js";
3
+ fetchReToStandardBody,
4
+ fetchRequestToStandardRequest,
5
+ standardBodyToFetchBody,
6
+ standardResponseToFetchResponse
7
+ } from "./chunk-JBPBLCBJ.js";
8
+ import "./chunk-SUGWQSMQ.js";
9
+ import "./chunk-NOA3GBJQ.js";
10
+ import "./chunk-OWMUECGT.js";
9
11
 
10
12
  // src/adapters/next/serve.ts
11
13
  import { value } from "@orpc/shared";
@@ -27,10 +29,11 @@ function serve(handler, ...[options]) {
27
29
  };
28
30
  }
29
31
  export {
30
- ORPCPayloadCodec,
31
- ORPCProcedureMatcher,
32
32
  RPCHandler,
33
- super_json_exports as SuperJSON,
34
- serve
33
+ fetchReToStandardBody,
34
+ fetchRequestToStandardRequest,
35
+ serve,
36
+ standardBodyToFetchBody,
37
+ standardResponseToFetchResponse
35
38
  };
36
39
  //# sourceMappingURL=next.js.map
package/dist/node.js CHANGED
@@ -1,87 +1,175 @@
1
1
  import {
2
- RPCHandler
3
- } from "./chunk-ESTRJAOX.js";
4
- import "./chunk-KK4SDLC7.js";
2
+ RPCCodec,
3
+ RPCMatcher,
4
+ StandardHandler
5
+ } from "./chunk-SUGWQSMQ.js";
6
+ import "./chunk-NOA3GBJQ.js";
7
+ import "./chunk-OWMUECGT.js";
5
8
 
6
- // src/adapters/node/request-listener.ts
7
- function createRequest(req, res) {
8
- const controller = new AbortController();
9
- res.on("close", () => {
10
- controller.abort();
11
- });
9
+ // src/adapters/node/utils.ts
10
+ import { Buffer, File } from "node:buffer";
11
+ import { Readable } from "node:stream";
12
+ import { once } from "@orpc/shared";
13
+ import cd from "content-disposition";
14
+ function nodeHttpToStandardRequest(req, res) {
12
15
  const method = req.method ?? "GET";
13
- const headers = createHeaders(req);
14
16
  const protocol = "encrypted" in req.socket && req.socket.encrypted ? "https:" : "http:";
15
- const host = headers.get("Host") ?? "localhost";
17
+ const host = req.headers.host ?? "localhost";
16
18
  const url = new URL(req.originalUrl ?? req.url ?? "/", `${protocol}//${host}`);
17
- const init = { method, headers, signal: controller.signal };
18
- if (method !== "GET" && method !== "HEAD") {
19
- init.body = new ReadableStream({
20
- start(controller2) {
21
- req.on("data", (chunk) => {
22
- controller2.enqueue(new Uint8Array(chunk.buffer, chunk.byteOffset, chunk.byteLength));
23
- });
24
- req.on("end", () => {
25
- controller2.close();
26
- });
19
+ return {
20
+ raw: { request: req, response: res },
21
+ method,
22
+ url,
23
+ headers: req.headers,
24
+ body: once(() => {
25
+ return nodeHttpRequestToStandardBody(req);
26
+ }),
27
+ get signal() {
28
+ const signal = nodeHttpResponseToAbortSignal(res);
29
+ Object.defineProperty(this, "signal", { value: signal, writable: true });
30
+ return signal;
31
+ },
32
+ set signal(value) {
33
+ Object.defineProperty(this, "signal", { value, writable: true });
34
+ }
35
+ };
36
+ }
37
+ function nodeHttpResponseSendStandardResponse(res, standardResponse) {
38
+ return new Promise((resolve, reject) => {
39
+ res.on("error", reject);
40
+ res.on("finish", resolve);
41
+ if (standardResponse.body === void 0) {
42
+ res.writeHead(standardResponse.status, standardResponse.headers);
43
+ res.end();
44
+ return;
45
+ }
46
+ if (standardResponse.body instanceof Blob) {
47
+ const resHeaders = {
48
+ ...standardResponse.headers,
49
+ "content-type": standardResponse.body.type,
50
+ "content-length": standardResponse.body.size.toString()
51
+ };
52
+ if (!standardResponse.headers["content-disposition"] && standardResponse.body instanceof Blob) {
53
+ resHeaders["content-disposition"] = cd(standardResponse.body instanceof File ? standardResponse.body.name : "blob");
27
54
  }
55
+ res.writeHead(standardResponse.status, resHeaders);
56
+ Readable.fromWeb(
57
+ standardResponse.body.stream()
58
+ // Conflict between types=node and lib=dom so we need to cast it
59
+ ).pipe(res);
60
+ return;
61
+ }
62
+ if (standardResponse.body instanceof FormData) {
63
+ const response = new Response(standardResponse.body);
64
+ res.writeHead(standardResponse.status, {
65
+ ...standardResponse.headers,
66
+ "content-type": response.headers.get("content-type")
67
+ });
68
+ Readable.fromWeb(
69
+ response.body
70
+ // Conflict between types=node and lib=dom so we need to cast it
71
+ ).pipe(res);
72
+ return;
73
+ }
74
+ if (standardResponse.body instanceof URLSearchParams) {
75
+ res.writeHead(standardResponse.status, {
76
+ ...standardResponse.headers,
77
+ "content-type": "application/x-www-form-urlencoded"
78
+ });
79
+ const string2 = standardResponse.body.toString();
80
+ res.end(string2);
81
+ return;
82
+ }
83
+ res.writeHead(standardResponse.status, {
84
+ ...standardResponse.headers,
85
+ "content-type": "application/json"
28
86
  });
29
- init.duplex = "half";
30
- }
31
- return new Request(url, init);
87
+ const string = JSON.stringify(standardResponse.body);
88
+ res.end(string);
89
+ });
32
90
  }
33
- function createHeaders(req) {
34
- const headers = new Headers();
35
- const rawHeaders = req.rawHeaders;
36
- for (let i = 0; i < rawHeaders.length; i += 2) {
37
- headers.append(rawHeaders[i], rawHeaders[i + 1]);
91
+ async function nodeHttpRequestToStandardBody(req) {
92
+ const method = req.method ?? "GET";
93
+ if (method === "GET" || method === "HEAD") {
94
+ return void 0;
38
95
  }
39
- return headers;
40
- }
41
- async function sendResponse(res, response) {
42
- const headers = {};
43
- for (const [key, value] of response.headers) {
44
- if (key in headers) {
45
- if (Array.isArray(headers[key])) {
46
- headers[key].push(value);
47
- } else {
48
- headers[key] = [headers[key], value];
49
- }
50
- } else {
51
- headers[key] = value;
96
+ const contentDisposition = req.headers["content-disposition"];
97
+ const fileName = contentDisposition ? cd.parse(contentDisposition).parameters.filename : void 0;
98
+ const contentType = req.headers["content-type"];
99
+ if (fileName) {
100
+ return await streamToFile(req, fileName, contentType || "application/octet-stream");
101
+ }
102
+ if (!contentType || contentType.startsWith("application/json")) {
103
+ const text = await streamToString(req);
104
+ if (!text) {
105
+ return void 0;
52
106
  }
107
+ return JSON.parse(text);
108
+ }
109
+ if (contentType.startsWith("multipart/form-data")) {
110
+ return await streamToFormData(req, contentType);
111
+ }
112
+ if (contentType.startsWith("application/x-www-form-urlencoded")) {
113
+ const text = await streamToString(req);
114
+ return new URLSearchParams(text);
115
+ }
116
+ if (contentType.startsWith("text/")) {
117
+ return await streamToString(req);
53
118
  }
54
- res.writeHead(response.status, headers);
55
- if (response.body != null && res.req.method !== "HEAD") {
56
- for await (const chunk of response.body) {
57
- res.write(chunk);
119
+ return streamToFile(req, "blob", contentType);
120
+ }
121
+ function streamToFormData(stream, contentType) {
122
+ const response = new Response(stream, {
123
+ // Conflict between types=node and lib=dom so we need to cast it
124
+ headers: {
125
+ "content-type": contentType
58
126
  }
127
+ });
128
+ return response.formData();
129
+ }
130
+ async function streamToString(stream) {
131
+ let string = "";
132
+ for await (const chunk of stream) {
133
+ string += chunk.toString();
59
134
  }
60
- res.end();
135
+ return string;
136
+ }
137
+ async function streamToFile(stream, fileName, contentType) {
138
+ const chunks = [];
139
+ for await (const chunk of stream) {
140
+ chunks.push(chunk);
141
+ }
142
+ return new File([Buffer.concat(chunks)], fileName, { type: contentType });
143
+ }
144
+ function nodeHttpResponseToAbortSignal(res) {
145
+ const controller = new AbortController();
146
+ res.on("close", () => {
147
+ controller.abort();
148
+ });
149
+ return controller.signal;
61
150
  }
62
151
 
63
- // src/adapters/node/orpc-handler.ts
64
- var RPCHandler2 = class {
65
- orpcFetchHandler;
152
+ // src/adapters/node/rpc-handler.ts
153
+ var RPCHandler = class {
154
+ standardHandler;
66
155
  constructor(router, options) {
67
- this.orpcFetchHandler = new RPCHandler(router, options);
156
+ const codec = options?.codec ?? new RPCCodec();
157
+ const matcher = options?.matcher ?? new RPCMatcher();
158
+ this.standardHandler = new StandardHandler(router, matcher, codec, options);
68
159
  }
69
160
  async handle(req, res, ...rest) {
70
- const request = createRequest(req, res);
71
- const result = await this.orpcFetchHandler.handle(request, ...rest);
72
- if (result.matched === false) {
161
+ const standardRequest = nodeHttpToStandardRequest(req, res);
162
+ const result = await this.standardHandler.handle(standardRequest, ...rest);
163
+ if (!result.matched) {
73
164
  return { matched: false };
74
165
  }
75
- const context = rest[0]?.context ?? {};
76
- await rest[0]?.beforeSend?.(result.response, context);
77
- await sendResponse(res, result.response);
166
+ await nodeHttpResponseSendStandardResponse(res, result.response);
78
167
  return { matched: true };
79
168
  }
80
169
  };
81
170
  export {
82
- RPCHandler2 as RPCHandler,
83
- createHeaders,
84
- createRequest,
85
- sendResponse
171
+ RPCHandler,
172
+ nodeHttpResponseSendStandardResponse,
173
+ nodeHttpToStandardRequest
86
174
  };
87
175
  //# sourceMappingURL=node.js.map
@@ -0,0 +1,11 @@
1
+ import {
2
+ CORSPlugin,
3
+ CompositePlugin,
4
+ ResponseHeadersPlugin
5
+ } from "./chunk-OWMUECGT.js";
6
+ export {
7
+ CORSPlugin,
8
+ CompositePlugin,
9
+ ResponseHeadersPlugin
10
+ };
11
+ //# sourceMappingURL=plugins.js.map
@@ -1,6 +1,4 @@
1
- export * from './orpc-handler';
2
- export * from './orpc-payload-codec';
3
- export * from './orpc-procedure-matcher';
4
- export * as SuperJSON from './super-json';
1
+ export * from './rpc-handler';
5
2
  export * from './types';
3
+ export * from './utils';
6
4
  //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1,10 @@
1
+ import type { Context } from '../../context';
2
+ import type { Router } from '../../router';
3
+ import type { RPCHandlerOptions, StandardHandleRest } from '../standard';
4
+ import type { FetchHandler, FetchHandleResult } from './types';
5
+ export declare class RPCHandler<T extends Context> implements FetchHandler<T> {
6
+ private readonly standardHandler;
7
+ constructor(router: Router<T, any>, options?: NoInfer<RPCHandlerOptions<T>>);
8
+ handle(request: Request, ...rest: StandardHandleRest<T>): Promise<FetchHandleResult>;
9
+ }
10
+ //# sourceMappingURL=rpc-handler.d.ts.map
@@ -1,13 +1,5 @@
1
- import type { HTTPPath } from '@orpc/contract';
2
1
  import type { Context } from '../../context';
3
- export type FetchHandleOptions<T extends Context> = {
4
- prefix?: HTTPPath;
5
- } & (Record<never, never> extends T ? {
6
- context?: T;
7
- } : {
8
- context: T;
9
- });
10
- export type FetchHandleRest<T extends Context> = [options: FetchHandleOptions<T>] | (Record<never, never> extends T ? [] : never);
2
+ import type { StandardHandleRest } from '../standard';
11
3
  export type FetchHandleResult = {
12
4
  matched: true;
13
5
  response: Response;
@@ -16,6 +8,6 @@ export type FetchHandleResult = {
16
8
  response: undefined;
17
9
  };
18
10
  export interface FetchHandler<T extends Context> {
19
- handle(request: Request, ...rest: FetchHandleRest<T>): Promise<FetchHandleResult>;
11
+ handle(request: Request, ...rest: StandardHandleRest<T>): Promise<FetchHandleResult>;
20
12
  }
21
13
  //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1,6 @@
1
+ import type { StandardBody, StandardRequest, StandardResponse } from '../standard';
2
+ export declare function fetchReToStandardBody(re: Request | Response): Promise<StandardBody>;
3
+ export declare function fetchRequestToStandardRequest(request: Request): StandardRequest;
4
+ export declare function standardBodyToFetchBody(body: StandardBody): Blob | FormData | URLSearchParams | string | undefined;
5
+ export declare function standardResponseToFetchResponse(response: StandardResponse): Response;
6
+ //# sourceMappingURL=utils.d.ts.map
@@ -1,8 +1,9 @@
1
1
  import type { Context as HonoContext, MiddlewareHandler } from 'hono';
2
2
  import type { Context } from '../../context';
3
- import type { FetchHandleOptions, FetchHandler } from '../fetch';
3
+ import type { FetchHandler } from '../fetch';
4
+ import type { StandardHandleOptions } from '../standard';
4
5
  import { type Value } from '@orpc/shared';
5
- export type CreateMiddlewareOptions<T extends Context> = Omit<FetchHandleOptions<T>, 'context'> & (Record<never, never> extends T ? {
6
+ export type CreateMiddlewareOptions<T extends Context> = Omit<StandardHandleOptions<T>, 'context'> & (Record<never, never> extends T ? {
6
7
  context?: Value<T, [HonoContext]>;
7
8
  } : {
8
9
  context: Value<T, [HonoContext]>;
@@ -1,8 +1,9 @@
1
1
  import type { NextRequest } from 'next/server';
2
2
  import type { Context } from '../../context';
3
- import type { FetchHandleOptions, FetchHandler } from '../fetch';
3
+ import type { FetchHandler } from '../fetch';
4
+ import type { StandardHandleOptions } from '../standard';
4
5
  import { type Value } from '@orpc/shared';
5
- export type ServeOptions<T extends Context> = Omit<FetchHandleOptions<T>, 'context'> & (Record<never, never> extends T ? {
6
+ export type ServeOptions<T extends Context> = Omit<StandardHandleOptions<T>, 'context'> & (Record<never, never> extends T ? {
6
7
  context?: Value<T, [NextRequest]>;
7
8
  } : {
8
9
  context: Value<T, [NextRequest]>;
@@ -1,5 +1,4 @@
1
- export * from './orpc-handler';
2
- export * from './orpc-handler';
3
- export * from './request-listener';
1
+ export * from './rpc-handler';
4
2
  export * from './types';
3
+ export * from './utils';
5
4
  //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1,10 @@
1
+ import type { Context } from '../../context';
2
+ import type { Router } from '../../router';
3
+ import type { RPCHandlerOptions, StandardHandleRest } from '../standard';
4
+ import type { NodeHttpHandler, NodeHttpHandleResult, NodeHttpRequest, NodeHttpResponse } from './types';
5
+ export declare class RPCHandler<T extends Context> implements NodeHttpHandler<T> {
6
+ private readonly standardHandler;
7
+ constructor(router: Router<T, any>, options?: NoInfer<RPCHandlerOptions<T>>);
8
+ handle(req: NodeHttpRequest, res: NodeHttpResponse, ...rest: StandardHandleRest<T>): Promise<NodeHttpHandleResult>;
9
+ }
10
+ //# sourceMappingURL=rpc-handler.d.ts.map
@@ -1,22 +1,21 @@
1
- import type { HTTPPath } from '@orpc/contract';
2
- import type { Promisable } from '@orpc/shared';
3
1
  import type { IncomingMessage, ServerResponse } from 'node:http';
2
+ import type { Http2ServerRequest, Http2ServerResponse } from 'node:http2';
4
3
  import type { Context } from '../../context';
5
- export type RequestHandleOptions<T extends Context> = {
6
- prefix?: HTTPPath;
7
- beforeSend?(response: Response, context: T): Promisable<void>;
8
- } & (Record<never, never> extends T ? {
9
- context?: T;
10
- } : {
11
- context: T;
12
- });
13
- export type RequestHandleRest<T extends Context> = [options: RequestHandleOptions<T>] | (Record<never, never> extends T ? [] : never);
14
- export type RequestHandleResult = {
4
+ import type { StandardHandleRest } from '../standard';
5
+ export type NodeHttpRequest = (IncomingMessage | Http2ServerRequest) & {
6
+ /**
7
+ * Replace `req.url` with `req.originalUrl` when `req.originalUrl` is available.
8
+ * This is useful for `express.js` middleware.
9
+ */
10
+ originalUrl?: string;
11
+ };
12
+ export type NodeHttpResponse = ServerResponse | Http2ServerResponse;
13
+ export type NodeHttpHandleResult = {
15
14
  matched: true;
16
15
  } | {
17
16
  matched: false;
18
17
  };
19
- export interface RequestHandler<T extends Context> {
20
- handle(req: IncomingMessage, res: ServerResponse, ...rest: RequestHandleRest<T>): Promise<RequestHandleResult>;
18
+ export interface NodeHttpHandler<T extends Context> {
19
+ handle(req: NodeHttpRequest, res: NodeHttpResponse, ...rest: StandardHandleRest<T>): Promise<NodeHttpHandleResult>;
21
20
  }
22
21
  //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1,5 @@
1
+ import type { StandardRequest, StandardResponse } from '../standard';
2
+ import type { NodeHttpRequest, NodeHttpResponse } from './types';
3
+ export declare function nodeHttpToStandardRequest(req: NodeHttpRequest, res: NodeHttpResponse): StandardRequest;
4
+ export declare function nodeHttpResponseSendStandardResponse(res: NodeHttpResponse, standardResponse: StandardResponse): Promise<void>;
5
+ //# sourceMappingURL=utils.d.ts.map
@@ -0,0 +1,47 @@
1
+ import type { HTTPPath } from '@orpc/contract';
2
+ import type { Interceptor } from '@orpc/shared';
3
+ import type { Context } from '../../context';
4
+ import type { Plugin } from '../../plugins';
5
+ import type { Router } from '../../router';
6
+ import type { StandardCodec, StandardMatcher, StandardRequest, StandardResponse } from './types';
7
+ export type StandardHandleOptions<T extends Context> = {
8
+ prefix?: HTTPPath;
9
+ } & (Record<never, never> extends T ? {
10
+ context?: T;
11
+ } : {
12
+ context: T;
13
+ });
14
+ export type WellStandardHandleOptions<T extends Context> = StandardHandleOptions<T> & {
15
+ context: T;
16
+ };
17
+ export type StandardHandleRest<T extends Context> = [options: StandardHandleOptions<T>] | (Record<never, never> extends T ? [] : never);
18
+ export type StandardHandleResult = {
19
+ matched: true;
20
+ response: StandardResponse;
21
+ } | {
22
+ matched: false;
23
+ response: undefined;
24
+ };
25
+ export type StandardHandlerInterceptorOptions<TContext extends Context> = WellStandardHandleOptions<TContext> & {
26
+ request: StandardRequest;
27
+ };
28
+ export interface StandardHandlerOptions<TContext extends Context> {
29
+ plugins?: Plugin<TContext>[];
30
+ /**
31
+ * Interceptors at the request level, helpful when you want catch errors
32
+ */
33
+ interceptors?: Interceptor<StandardHandlerInterceptorOptions<TContext>, StandardHandleResult, unknown>[];
34
+ /**
35
+ * Interceptors at the root level, helpful when you want override the response
36
+ */
37
+ interceptorsRoot?: Interceptor<StandardHandlerInterceptorOptions<TContext>, StandardHandleResult, unknown>[];
38
+ }
39
+ export declare class StandardHandler<TContext extends Context> {
40
+ private readonly matcher;
41
+ private readonly codec;
42
+ private readonly options;
43
+ private readonly plugin;
44
+ constructor(router: Router<TContext, any>, matcher: StandardMatcher, codec: StandardCodec, options?: NoInfer<StandardHandlerOptions<TContext>>);
45
+ handle(request: StandardRequest, ...[options]: StandardHandleRest<TContext>): Promise<StandardHandleResult>;
46
+ }
47
+ //# sourceMappingURL=handler.d.ts.map
@@ -0,0 +1,7 @@
1
+ export * from './handler';
2
+ export * from './rpc-codec';
3
+ export * from './rpc-handler';
4
+ export * from './rpc-matcher';
5
+ export * from './rpc-serializer';
6
+ export * from './types';
7
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1,15 @@
1
+ import type { ORPCError } from '@orpc/contract';
2
+ import type { AnyProcedure } from '../../procedure';
3
+ import type { StandardCodec, StandardParams, StandardRequest, StandardResponse } from './types';
4
+ import { RPCSerializer } from './rpc-serializer';
5
+ export interface StandardCodecOptions {
6
+ serializer?: RPCSerializer;
7
+ }
8
+ export declare class RPCCodec implements StandardCodec {
9
+ private readonly serializer;
10
+ constructor(options?: StandardCodecOptions);
11
+ decode(request: StandardRequest, _params: StandardParams | undefined, _procedure: AnyProcedure): Promise<unknown>;
12
+ encode(output: unknown, _procedure: AnyProcedure): StandardResponse;
13
+ encodeError(error: ORPCError<any, any>): StandardResponse;
14
+ }
15
+ //# sourceMappingURL=rpc-codec.d.ts.map
@@ -0,0 +1,8 @@
1
+ import type { Context } from '../../context';
2
+ import type { StandardHandlerOptions } from './handler';
3
+ import type { StandardCodec, StandardMatcher } from './types';
4
+ export interface RPCHandlerOptions<T extends Context> extends StandardHandlerOptions<T> {
5
+ matcher?: StandardMatcher;
6
+ codec?: StandardCodec;
7
+ }
8
+ //# sourceMappingURL=rpc-handler.d.ts.map
@@ -0,0 +1,10 @@
1
+ import type { HTTPPath } from '@orpc/contract';
2
+ import type { StandardMatcher, StandardMatchResult } from './types';
3
+ import { type AnyRouter } from '../../router';
4
+ export declare class RPCMatcher implements StandardMatcher {
5
+ private readonly tree;
6
+ private pendingRouters;
7
+ init(router: AnyRouter, path?: string[]): void;
8
+ match(_method: string, pathname: HTTPPath): Promise<StandardMatchResult>;
9
+ }
10
+ //# sourceMappingURL=rpc-matcher.d.ts.map
@@ -0,0 +1,16 @@
1
+ import type { Segment } from '@orpc/shared';
2
+ export type RPCSerializedJsonMeta = ['bigint' | 'date' | 'nan' | 'undefined' | 'set' | 'map' | 'regexp' | 'url', Segment[]][];
3
+ export type RPCSerialized = {
4
+ json: unknown;
5
+ meta: RPCSerializedJsonMeta;
6
+ } | FormData | Blob;
7
+ export type RPCSerializedFormDataMaps = Segment[][];
8
+ export declare class RPCSerializer {
9
+ serialize(data: unknown): RPCSerialized;
10
+ deserialize(serialized: RPCSerialized): unknown;
11
+ }
12
+ export declare function serializeRPCJson(value: unknown, segments?: Segment[], meta?: RPCSerializedJsonMeta): {
13
+ json: unknown;
14
+ meta: RPCSerializedJsonMeta;
15
+ };
16
+ //# sourceMappingURL=rpc-serializer.d.ts.map