elbywan / wretch

A tiny wrapper built around fetch with an intuitive syntax. :candy:
MIT License
4.79k stars 96 forks source link

Issues with typing on `defer`ed `resolve` method #146

Closed shtaft closed 2 years ago

shtaft commented 2 years ago

Hey team! Thank you so much for maintaining this package, we are absolutely loving it.

We're in the process of upgrading to V2, and have noticed a small Typescript issue during the upgrade. It appears that deferring a resolve chain is not supported in the V2 types.

Here is a small repro:

import wretch from 'wretch'

const handle403 = () => {
  console.log('Unauthorized!')
}

const client = wretch('https://my-base-url.com')
  .defer((wretch) => {
    return wretch
        // Argument of type '(wretch: Wretch<unknown, unknown, undefined>) => Wretch<unknown, unknown, WretchResponseChain<unknown, unknown, undefined>>'
        // is not assignable to parameter of type 'WretchDeferredCallback<unknown, unknown, undefined>'
        .resolve((_) => _.forbidden(handle403));
  })

})
Argument of type '(wretch: Wretch<unknown, unknown, undefined>) => 
    Wretch<unknown, unknown, WretchResponseChain<unknown, unknown, undefined>>'
is not assignable to parameter of type 'WretchDeferredCallback<unknown, unknown, undefined>'

We like to modify requests made in the deferred callback, but can't in V2. Are we doing something wrong?

Thank you so much!

elbywan commented 2 years ago

Hey @shtaft,

Thank you so much for maintaining this package, we are absolutely loving it.

Thanks, this is heartwarming 🙇.

We like to modify requests made in the deferred callback, but can't in V2. Are we doing something wrong?

No you're not 😉. There is definitely someting fishy with the return type of the .defer() function. I just released version 2.1.4 which includes a fix and it should hopefully solve the problem.

Feel free to reopen the issue otherwise, and thanks for reporting the bug! 👍

shtaft commented 2 years ago

Unbelievable, thank you so much! Cannot believe your speed on resolving issues, incredible work.