Skip to content

Commit

Permalink
feat: new hooks API
Browse files Browse the repository at this point in the history
  • Loading branch information
ronag committed Apr 4, 2024
1 parent cae5625 commit 446ed44
Showing 1 changed file with 38 additions and 2 deletions.
40 changes: 38 additions & 2 deletions types/dispatcher.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,13 +215,48 @@ declare namespace Dispatcher {
context: object;
}
export type StreamFactory = (data: StreamFactoryData) => Writable;
export interface Controller {
readonly aborted: boolean;
readonly reason: Error;
readonly paused: boolean;

pause(): void;
resume(): void;
abort(reason: Error): void;
}
export interface DispatchHandlers {
// New API

/** Invoked before request is starting to be processed */
onRequestStart?(controller: Controller): void;
/** Invoked before headers data is sent */
onRequestHeaders?(headers: Record<string, string>): void;
/** Invoked before payload data is sent. */
onRequestData?(chunk: Buffer | string): void;
/** Invoked after request has finished sending */
onRequestEnd?(): void;
/** Invoked after request has errored while sending */
onRequestError?(err: Error): void;

/** Invoked before response is starting to be processed */
onResponseStart?(controller: Controller): void;
/** Invoked after status headers data has been received */
onResponseHeaders?(headers: Record<string, string>): void;
/** Invoked after response payload data is received. */
onResponseData?(chunk: Buffer | string): void;
/** Invoked after response has finished */
onResponseEnd?(): void;
/** Invoked after response has errored */
onResponseError?(err: Error): void;

// Legacy API

/** Invoked when request is upgraded either due to a `Upgrade` header or `CONNECT` method. */
onUpgrade?(statusCode: number, headers: Buffer[] | string[] | null, socket: Duplex): void;
/** Invoked before request is dispatched on socket. May be invoked multiple times when a request is retried when the request at the head of the pipeline fails. */
onConnect?(abort: () => void): void;
/** Invoked when an error has occurred. */
onError?(err: Error): void;
/** Invoked when request is upgraded either due to a `Upgrade` header or `CONNECT` method. */
onUpgrade?(statusCode: number, headers: Buffer[] | string[] | null, socket: Duplex): void;
/** Invoked when response is received, before headers have been read. **/
onResponseStarted?(): void;
/** Invoked when statusCode and headers have been received. May be invoked multiple times due to 1xx informational headers. */
Expand All @@ -232,6 +267,7 @@ declare namespace Dispatcher {
onComplete?(trailers: string[] | null): void;
/** Invoked when a body chunk is sent to the server. May be invoked multiple times for chunked requests */
onBodySent?(chunkSize: number, totalBytesSent: number): void;

}
export type PipelineHandler = (data: PipelineHandlerData) => Readable;
export type HttpMethod = 'GET' | 'HEAD' | 'POST' | 'PUT' | 'DELETE' | 'CONNECT' | 'OPTIONS' | 'TRACE' | 'PATCH';
Expand Down

0 comments on commit 446ed44

Please sign in to comment.