import { __getMiddleware } from '../core/use' ; import { __getMiddlewareOptions } from '../core/useOptions' ; import { AxleTypes } from '../index' ; import handleStatus from '../utils/handleStatus' ; import isJSON from '../utils/isJSON' ; import AxleResponse from './response' ; export default class AxleRequest < t = Record < string , any > > { public method : string ; public url : string ; public body : t | FormData | undefined | null ; public options : AxleTypes . AxleOptions ; public response : AxleResponse | undefined ; constructor ( method , url , body , options ) { this . method = method . toUpperCase ( ) ; this . url = url ; this . body = body ; this . options = options ; this . response ; } public async run ( ) { let middlewareOptions = { } ; __getMiddlewareOptions ( ) . forEach ( ( option ) => { middlewareOptions = { ... middlewareOptions , ... option } ; } ) ; let fetchBody = this . body ; if ( isJSON ( fetchBody ) ) { fetchBody = JSON . stringify ( this . body ) as string ; } else { fetchBody = this . body as FormData ; } const fetchOptions = { ... this . options , method : this . method , body : fetchBody , ... middlewareOptions , } ; const timeStart = performance . now ( ) ; const res = await fetch ( this . url , fetchOptions ) ; const timeEnd = performance . now ( ) ; if ( fetchOptions . handleStatus ) { if ( fetchOptions . handleStatus ( res . status , res . statusText ) ) { if ( res . statusText . trim ( ) !== '' ) { this . response = new AxleResponse ( res , timeStart , timeEnd ) ; console . error ( `${ this . method . toUpperCase ( ) }: Fetch returned with error code ${ res . status }. Status Message: "${ res . statusText }"` ) ; return Promise . reject ( { status : res . status , message : `${ this . method . toUpperCase ( ) }: Fetch returned with error code ${ res . status }. Status Message: "${ res . statusText }"` , response : this . response , request : this , } ) ; } else { this . response = new AxleResponse ( res , timeStart , timeEnd ) ; console . error ( `${ this . method . toUpperCase ( ) }: Fetch returned with error code ${ res . status }.` ) ; return Promise . reject ( { status : res . status , message : `${ this . method . toUpperCase ( ) }: Fetch returned with error code ${ res . status }.` , response : this . response , request : this , } ) ; } } else { this . response = new AxleResponse ( res , timeStart , timeEnd ) ; __getMiddleware ( ) . forEach ( ( cb ) => { cb ( this , this . response as AxleResponse ) ; } ) ; return this . response ; } } else { if ( handleStatus ( this . method , res . status , res . statusText ) ) { this . response = new AxleResponse ( res , timeStart , timeEnd ) ; console . error ( handleStatus ( this . method , res . status , res . statusText ) ) ; return Promise . reject ( { status : res . status , message : handleStatus ( this . method , res . status , res . statusText ) , response : this . response , request : this , } ) ; } else { this . response = new AxleResponse ( res , timeStart , timeEnd ) ; __getMiddleware ( ) . forEach ( ( cb ) => { cb ( this , this . response as AxleResponse ) ; } ) ; return this . response ; } } } }