import { TwirpError } from "../error/index.js"; import { Emitter } from "../eventEmitter/index.js"; export type ClientConfiguration = Partial<{ /** * The base URL for the RPC. The service path will be appended to this string. */ baseURL: string; /** * HTTP headers to include in the RPC. */ headers: Record; /** * A path prefix such as "/my/custom/prefix". Defaults to "/twirp", but can be set to "". */ prefix: string; /** * The transport to use for the RPC. Defaults to `fetch`. Overrides must conform to a subset of the fetch interface defined by the `RpcTransport` type. */ rpcTransport: RpcTransport; }>; interface MiddlewareConfig { /** * The URL for the RPC. This is the full URL for the request: the baseURL + prefix + the service path. */ url: string; /** * HTTP headers to include in the RPC. */ headers: Record; /** * The transport to use for the RPC. Defaults to `fetch`, but will use `nodeHttpTransport` in Node.js environments. Overrides must conform to a subset of the fetch interface defined by the `RpcTransport` type. */ rpcTransport: RpcTransport; } type ClientMiddleware = (context: MiddlewareConfig, next: (context: MiddlewareConfig) => Promise) => Promise; type HookListener = (context: Readonly) => void; type ErrorHookListener = (context: Readonly, err: TwirpError) => void; type ClientHooks = { requestPrepared: HookListener; responseReceived: HookListener; error: ErrorHookListener; }; type TwirpClientEvent = Emitter>; interface Client extends ClientConfiguration { /** * Registers middleware to manipulate the client request / response lifecycle. * * The middleware handler will receive `context` and `next` parameters. `context` sets the headers and url for the RPC. `next` invokes the next handler in the chain -- either the next registered middleware, or the Twirp RPC. * * Middleware is called in order of registration, with the Twirp RPC invoked last. */ use: (middleware: ClientMiddleware) => Client; /** * Registers event handler that can instrument a Twirp-generated client. * These callbacks all accept the current request `context`. * * `requestPrepared` is called as soon as a request has been created and before it has been sent to the Twirp server. * * `responseReceived` is called after a request has finished sending. * * `error` is called when an error occurs during the sending or receiving of a request. In addition to `context`, the error that occurred is passed as the second argument. */ on: (...args: Parameters["on"]>) => this; /** * Removes a registered event handler. */ off: (...args: Parameters["off"]>) => this; /** * The transport to use for the RPC. Defaults to `fetch`, but will use `nodeHttpTransport` in Node.js environments. Overrides must conform to a subset of the fetch interface defined by the `RpcTransport` type. */ rpcTransport: RpcTransport; } export interface RpcTransportOpts { method: string; headers: Record; body: string | Uint8Array | undefined | null; } /** * Subset of [Headers](https://developer.mozilla.org/en-US/docs/Web/API/Headers) from Fetch API. Redefined so that end users don't need to include lib.dom.d.ts in server only applications. */ interface Headers { get(name: string): string | null; } export interface RpcTransportResponse { arrayBuffer: () => Promise; json: () => Promise; readonly headers: Headers; readonly ok: boolean; readonly status: number; text: () => Promise; } export type RpcTransport = (url: string, opts: RpcTransportOpts) => Promise; /** * Global configuration for the TwirpScript clients. */ export declare const client: Client; export declare function JSONrequest(path: string, body?: string, config?: ClientConfiguration): Promise; export declare function PBrequest(path: string, body?: Uint8Array, config?: ClientConfiguration): Promise; export {};