/**
 * An alias to `window.fetch` type definition.
 */ type Fetch = typeof fetch ; /**
 * Options to do a request (`"url"` + `fetch`'s second argument).
 */ export interface RequestOptions extends RequestInit { url : string ; } /**
 * Interceptors to handle request errors.
 */ export interface ErrorInterceptors { param reason onError ? : ( reason : unknown ) => Promise < never > ; param reason onRequestError ? : ( reason : unknown ) => Promise < never > ; param reason onResponseError ? : ( reason : unknown ) => Promise < never > ; } /**
 * Interceptors to handle request, response and errors.
 */ export interface RequestInterceptors extends ErrorInterceptors { onRequest ? : ( ... params : [ RequestOptions ] ) => RequestOptions ; onResponse ? : ( response : Response ) => Response | PromiseLike < Response > ; } param fetch param interceptors export default function createRequest < A extends any [ ] , R = Response > ( fetch , interceptors ? ) ; export default function createRequest < A extends any [ ] > ( fetch , interceptors ? ) ; export default function createRequest < R = Response > ( fetch , interceptors ? ) ; export default function createRequest ( fetch , interceptors ? ) ; param { Fetch } fetch param { RequestInterceptors } interceptors export default function createRequest ( fetch , { onError = ( reason ? ) => Promise . reject ( reason ) , onRequest = ( options ) => options , onRequestError = onError , onResponse = ( response ) => Promise . resolve ( response ) , onResponseError = onError , } = { } ) { return function ( ) : Promise < Response > { const params = arguments as unknown as [ RequestOptions ] ; try { const options = onRequest . apply ( null , params ) ; return fetch ( options . url , options ) . then ( onResponse ) . catch ( onResponseError ) ; } catch ( reason ) { return onRequestError ( reason ) ; } } ; }