@trpc/server
Version:
107 lines (106 loc) • 5.44 kB
text/typescript
import { Ln as TRPCRequestInfo, Ti as MaybePromise, gi as DistributiveOmit, jn as HTTPBaseHandlerOptions, rr as inferRouterContext, ur as CreateContextCallback, zn as AnyRouter } from "./unstable-core-do-not-import-CzDUc5m2.mjs";
import * as http$1 from "http";
import * as http2 from "http2";
//#region src/adapters/node-http/types.d.ts
/** A Node.js HTTP/1 or HTTP/2 request, as the Node.js adapters take it. */
type NodeHTTPRequest = DistributiveOmit<http$1.IncomingMessage | http2.Http2ServerRequest, 'socket'> & {
/**
* The body, if a framework or middleware parsed it already. tRPC reads
* it instead of the stream.
*/
body?: unknown;
/**
* Optional, and maybe partial: some hosts don't give a full Node.js
* socket (https://github.com/trpc/trpc/issues/6341).
*/
socket?: Partial<http$1.IncomingMessage['socket']> | Partial<http2.Http2ServerRequest['socket']>;
};
/** A Node.js HTTP/1 or HTTP/2 response, as the Node.js adapters take it. */
type NodeHTTPResponse = DistributiveOmit<http$1.ServerResponse | http2.Http2ServerResponse, 'write'> & {
/**
* Sends what a compression middleware (such as Express's `compression()`)
* has buffered so far. tRPC calls it while streaming.
*/
flush?: () => void;
write: (chunk: string | Uint8Array) => boolean;
};
/** The Node.js adapters' `createContext` option. */
type NodeHTTPCreateContextOption<TRouter extends AnyRouter, TRequest, TResponse> = CreateContextCallback<inferRouterContext<TRouter>, NodeHTTPCreateContextFn<TRouter, TRequest, TResponse>>;
/**
* @internal
*/
type ConnectMiddleware<TRequest extends NodeHTTPRequest = NodeHTTPRequest, TResponse extends NodeHTTPResponse = NodeHTTPResponse> = (req: TRequest, res: TResponse, next: (err?: any) => any) => void;
/** The options of the Node.js adapters. */
type NodeHTTPHandlerOptions<TRouter extends AnyRouter, TRequest extends NodeHTTPRequest, TResponse extends NodeHTTPResponse> = HTTPBaseHandlerOptions<TRouter, TRequest> & {
/**
* A Connect-style middleware that runs before tRPC. tRPC doesn't answer
* `OPTIONS` requests or set CORS headers, so use this for `cors()`.
* @example
* ```ts
* createHTTPServer({ middleware: cors(), router: appRouter });
* ```
* @see https://trpc.io/docs/server/adapters/standalone#handling-cors--options
*/
middleware?: ConnectMiddleware<TRequest, TResponse>;
/**
* The largest request body, in bytes. A larger one gets a
* `PAYLOAD_TOO_LARGE` error. It only applies to bodies tRPC reads from
* the stream; if a body-parsing middleware sets `req.body`, limit the
* size there.
*/
maxBodySize?: number;
} & NodeHTTPCreateContextOption<TRouter, TRequest, TResponse>;
/** The options of `nodeHTTPRequestHandler`. */
type NodeHTTPRequestHandlerOptions<TRouter extends AnyRouter, TRequest extends NodeHTTPRequest, TResponse extends NodeHTTPResponse> = NodeHTTPHandlerOptions<TRouter, TRequest, TResponse> & {
req: TRequest;
res: TResponse;
/**
* The procedure path to handle, such as `post.all`, or several joined by
* commas for a batch.
*/
path: string;
};
/**
* What `createContext` gets in the Node.js adapters: `req`, `res` and the
* request's `info`.
* @see https://trpc.io/docs/server/context#creating-the-context
*/
type NodeHTTPCreateContextFnOptions<TRequest, TResponse> = {
req: TRequest;
res: TResponse;
info: TRPCRequestInfo;
};
/** The `createContext` function the Node.js adapters take. */
type NodeHTTPCreateContextFn<TRouter extends AnyRouter, TRequest, TResponse> = (opts: NodeHTTPCreateContextFnOptions<TRequest, TResponse>) => MaybePromise<inferRouterContext<TRouter>>;
//#endregion
//#region src/adapters/node-http/nodeHTTPRequestHandler.d.ts
/**
* Answers with an error response when handling a request throws.
* @internal
*/
declare function internal_exceptionHandler<TRouter extends AnyRouter, TRequest extends NodeHTTPRequest, TResponse extends NodeHTTPResponse>(opts: NodeHTTPRequestHandlerOptions<TRouter, TRequest, TResponse>): (cause: unknown) => void;
/**
* Handles a Node.js request for a router. The adapters for Express,
* Next.js and the standalone server use it. The promise never rejects.
*/
declare function nodeHTTPRequestHandler<TRouter extends AnyRouter, TRequest extends NodeHTTPRequest, TResponse extends NodeHTTPResponse>(opts: NodeHTTPRequestHandlerOptions<TRouter, TRequest, TResponse>): Promise<void>;
//#endregion
//#region src/adapters/node-http/incomingMessageToRequest.d.ts
/**
* The full URL of a Node.js request, from its `Host` header and socket.
* Throws a `BAD_REQUEST` error if it isn't a valid URL.
*/
declare function createURL(req: NodeHTTPRequest): URL;
/**
* Turns a Node.js `IncomingMessage` into a Fetch API `Request`. The
* request's signal aborts when the client disconnects.
*/
declare function incomingMessageToRequest(req: NodeHTTPRequest, res: NodeHTTPResponse, opts: {
/**
* Max body size in bytes. If the body is larger than this, the request will be aborted
*/
maxBodySize: number | null;
}): Request;
//#endregion
export { NodeHTTPCreateContextFn as a, NodeHTTPHandlerOptions as c, NodeHTTPResponse as d, nodeHTTPRequestHandler as i, NodeHTTPRequest as l, incomingMessageToRequest as n, NodeHTTPCreateContextFnOptions as o, internal_exceptionHandler as r, NodeHTTPCreateContextOption as s, createURL as t, NodeHTTPRequestHandlerOptions as u };
//# sourceMappingURL=index-DVmu6VtT.d.mts.map