@busymango/fetch-driver
Version:
fetch with middlewares for the browser
67 lines (66 loc) • 2.33 kB
TypeScript
/**
* @author mango
* @description fetch type define
*/
import type DriveContext from "./context";
export type Next = () => Promise<void>;
export declare abstract class DriveReport {
abstract beforeInit?(context: DriveContext): void;
abstract afterInit?(context: DriveContext): void;
abstract beforeFetch?(context: DriveContext): void;
abstract affterFetch?(context: DriveContext): void;
abstract beforeParse?(context: DriveContext): void;
abstract afterParse?(context: DriveContext): void;
}
export type Middleware<T> = (context: T, next: Next) => Promise<void>;
export type DriveMiddleware<T = unknown> = (context: DriveContext<T>, next: Next) => void;
export type DriveContextOptions = {
headers: Headers;
} & Omit<RequestInit, "headers">;
export interface ReceiverFunc<T> {
(params: {
size: number;
done: boolean;
percentage: number;
context: DriveContext<T>;
reader: ReadableStreamDefaultReader<Uint8Array>;
value?: Uint8Array;
}): void;
}
export interface BodyParseFunc<T> {
(response: Response, context: DriveContext<T>, extra?: {
receiver?: ReceiverFunc<T>;
}): Promise<void>;
}
export type ExtraOptions<T> = {
/** abort fetch before timeout */
timeout?: number;
/** use extra middleware in current fetch */
use?: DriveMiddleware<T>[];
/** progress callback */
receiver?: ReceiverFunc<T>;
/** body parse */
parse?: BodyParseFunc<T>;
};
export type DriveOptions<T> = RequestInit & ExtraOptions<T> & {
api: string;
data?: object;
};
export interface DriveMethodFunc {
<T>(api: string, data?: object, init?: Omit<RequestInit, "method">): Promise<T>;
}
export type DriveFuncAttrs = Record<Lowercase<FetchMethod>, DriveMethodFunc>;
export interface DriveFunc extends DriveFuncAttrs {
<T>(opts: DriveOptions<T>): Promise<T>;
<T>(
/** the fetch USVString */
api: string,
/** the fetch body */
data?: object,
/** the fetch request init */
init?: RequestInit): Promise<T>;
}
export type FirstParam<T> = string | DriveOptions<T>;
export type FetchContext<T> = Required<DriveContext<T>>;
export type FetchMethod = "GET" | "PUT" | "POST" | "HEAD" | "TRACE" | "PATCH" | "DELETE" | "CONNECT" | "OPTIONS";
export declare const methods: readonly FetchMethod[];