@@ -52,7 +52,7 @@ export const resolver = <T, Chain, R, E>(wretch: T & Wretch<T, Chain, R, E>) =>
5252 if ( ! response . ok ) {
5353 const err = new WretchError ( )
5454 err [ "cause" ] = referenceError
55- err . stack = err . stack + "\nCAUSE: " + referenceError . stack
55+ err . stack += "\nCAUSE: " + referenceError . stack
5656 err . response = response
5757 err . status = response . status
5858 err . url = finalUrl
@@ -77,34 +77,35 @@ export const resolver = <T, Chain, R, E>(wretch: T & Wretch<T, Chain, R, E>) =>
7777 return response
7878 } )
7979 // Wraps the Promise in order to dispatch the error to a matching catcher
80- const catchersWrapper = < T > ( promise : Promise < T > ) : Promise < void | T > => {
81- return promise . catch ( err => {
82- const fetchErrorFlag = Object . prototype . hasOwnProperty . call ( err , FETCH_ERROR )
80+ const catchersWrapper = < T > ( promise : Promise < T > ) : Promise < void | T > =>
81+ promise . catch ( err => {
82+ const fetchErrorFlag = FETCH_ERROR in err
8383 const error = fetchErrorFlag ? err [ FETCH_ERROR ] : err
8484
8585 const catcher =
8686 ( error ?. status && catchers . get ( error . status ) ) ||
87- catchers . get ( error ?. name ) || (
88- fetchErrorFlag && catchers . has ( FETCH_ERROR ) && catchers . get ( FETCH_ERROR )
89- )
87+ catchers . get ( error ?. name ) ||
88+ ( fetchErrorFlag && catchers . get ( FETCH_ERROR ) ) ||
89+ catchers . get ( CATCHER_FALLBACK )
9090
9191 if ( catcher )
9292 return catcher ( error , wretch )
9393
94- const catcherFallback = catchers . get ( CATCHER_FALLBACK )
95- if ( catcherFallback )
96- return catcherFallback ( error , wretch )
97-
9894 throw error
9995 } )
100- }
10196 // Enforces the proper promise type when a body parsing method is called.
102- type BodyParser = < Type > ( funName : "json" | "blob" | "formData" | "arrayBuffer" | "text" | null ) => < Result = void > ( cb ?: ( type : Type ) => Result ) => Promise < Awaited < Result > >
103- const bodyParser : BodyParser = funName => cb => funName ?
97+ type BodyParser =
98+ < Type > ( funName : "json" | "blob" | "formData" | "arrayBuffer" | "text" | null )
99+ => < Result = void > ( cb ?: ( type : Type ) => Result )
100+ => Promise < Awaited < Result > >
101+ const bodyParser : BodyParser = funName => cb => {
102+ const promise = funName ?
104103 // If a callback is provided, then callback with the body result otherwise return the parsed body itself.
105- catchersWrapper ( throwingPromise . then ( _ => _ && _ [ funName ] ( ) ) . then ( _ => cb ? cb ( _ ) : _ ) ) :
104+ throwingPromise . then ( _ => _ ?. [ funName ] ( ) ) :
106105 // No body parsing method - return the response
107- catchersWrapper ( throwingPromise . then ( _ => cb ? cb ( _ as any ) : _ ) )
106+ throwingPromise
107+ return catchersWrapper ( cb ? promise . then ( cb ) : promise )
108+ }
108109
109110 const responseChain : WretchResponseChain < T , Chain , R , E > = {
110111 _wretchReq : wretch ,
0 commit comments