import { TwirpError } from "../error/index.js"; import { Emitter } from "../eventEmitter/index.js"; import type { ByteSource } from "protoscript"; export interface Response { body: string | Uint8Array; headers: { [key: string]: string | undefined; }; statusCode: number; } export interface Request { body: Uint8Array; headers: { [key: string]: string | undefined; }; url: string; } export interface InboundRequest extends Request { method: string; } export interface ServerResponse { end: (chunk: any, cb?: () => void) => void; writeHead: (statusCode: number, headers?: ServerRequest["headers"]) => void; } export interface ServerRequest { headers: Record; method?: string | undefined; url?: string | undefined; [Symbol.asyncIterator](): AsyncIterableIterator; } type ServerRequestWithBody = SR & Pick; type ServiceContext = { [Idx in keyof S]: { service: S[Idx]; method: NonNullable; }; }[number] | { service: undefined; method: undefined; }; /** * The requested content-type for the request. */ type ContentType = "JSON" | "Protobuf" | "Unknown"; export type TwirpContext = { contentType: ContentType; } & ServiceContext & ContextExt; interface Message { protobuf: { encode: (message: T) => Uint8Array; decode: (bytes: ByteSource) => T; }; json: { encode: (message: T) => string; decode: (str: string) => T; }; } export interface ServiceMethod { name: string; handler: (input: any, ctx: Context) => any; input: Message; output: Message; } export interface Service { name: string; methods: Record; } export declare function TwirpErrorResponse(error: TwirpError): Response; export declare function executeServiceMethod(method: ServiceMethod, req: Request, context: Context, ee: Emitter>): Promise; type Next = () => Promise; interface TwirpServerConfig { /** * Puts the Twirp server runtime into debug mode when set to true. This enables request logging. Defaults to true. */ debug?: boolean; /** * A path prefix such as "/my/custom/prefix". Defaults to "/twirp", but can be set to "". */ prefix?: string; } export type Middleware = (request: Request, context: TwirpContext, next: Next) => Promise; export declare function twirpHandler(ee: Emitter>): (req: InboundRequest, ctx: Context) => Promise; type ContextMethod = Exclude; export type ServerHooks = { requestReceived: (context: Context, request: Request) => void; requestRouted: (context: Context, input: ReturnType["input"]["protobuf"]["decode"]>) => void; responsePrepared: (context: Context, output: ReturnType["output"]["protobuf"]["decode"]>) => void; responseSent: (context: Context, response: Response) => void; error: (context: Context, error: TwirpError) => void; }; export interface TwirpServerRuntime { /** * Registers middleware to manipulate the server request / response lifecycle. * * The middleware handler will receive `request`, `context` and `next` parameters. `request` is the incoming request. `context` is a request context object which will be passed to each middleware handler and finally the Twirp service handler you implemented. `context` enables you to pass extra parameters to your service handlers that are not available via your service's defined request parameters, and can be used to implement things such as authentication or rate limiting. `next` invokes the next handler in the chain -- either the next registered middleware, or the Twirp service handler you implemented. * * Middleware is called in order of registration, with the Twirp service handler you implemented invoked last. */ use: (middleware: Middleware) => this; /** * Registers event handler that can instrument a Twirp-generated server. These callbacks all accept the current request `context` as their first argument. * * `requestReceived` is called as soon as a request enters the Twirp server at the earliest available moment. Called with the current `context` and the request. * * `requestRouted` is called when a request has been routed to a service method. Called with the current `context` and the input to the service method. * * `responsePrepared` is called when a request has been handled by a service method. Called with the current `context` and the response generated by the service method. * * `responseSent` is called when all bytes of a response (including an error response) have been written. Called with the current `context` and the response. * * `error` is called when an error occurs while handling a request. Called with the current `context` and the error that occurred. * */ on: >(event: Event, handler: ServerHooks[Event]) => this; /** * Removes a registered event handler. */ off: >(event: Event, handler: ServerHooks[Event]) => this; } interface TwirpServer extends TwirpServerRuntime> { (req: Request, res: ServerResponse): void; } interface TwirpServerless extends TwirpServerRuntime { (req: Request): Promise; } export declare function createTwirpServerless(services: Services, config?: TwirpServerConfig): TwirpServerless, Request>; export declare function createTwirpServer(services: Services, config?: TwirpServerConfig): TwirpServer, Request>; export {};