rokapi
Version:
32 lines (31 loc) • 1.22 kB
TypeScript
export type RequestMethod = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH';
export type RequireOne<T, K extends keyof T> = Omit<T, K> & Required<Pick<T, K>>;
export interface RequestOptions {
url?: string;
method?: RequestMethod;
headers?: Record<string, string>;
body?: BodyInit | null | undefined;
data?: Record<string, any>;
params?: Record<string, any>;
timeout?: number;
withCredentials?: boolean;
responseType?: XMLHttpRequestResponseType;
signal?: AbortSignal;
cancelKey?: string;
}
export interface Response<T = any> {
data: T;
status: number;
statusText: string;
toPlain: () => any;
}
export interface Requestor {
get<T = any>(url: string, options?: RequestOptions): Promise<Response<T>>;
post<T = any>(url: string, options?: RequestOptions): Promise<Response<T>>;
put<T = any>(url: string, options?: RequestOptions): Promise<Response<T>>;
delete<T = any>(url: string, options?: RequestOptions): Promise<Response<T>>;
patch<T = any>(url: string, options?: RequestOptions): Promise<Response<T>>;
}
export interface EventDrivenRequestor extends Requestor {
on(event: 'beforeRequest' | 'responseBody', callback: Function): void;
}