sindresorhus / ky

🌳 Tiny & elegant JavaScript HTTP client based on the Fetch API
MIT License
13.62k stars 363 forks source link

Want to stop or fetch/get token values inside hooks #496

Closed code7070 closed 3 months ago

code7070 commented 1 year ago

I love Ky, especially as an alternative to migrating from axios and apicase

Can I hold or stop or perform certain requests before executing the original request using hooks?

example: if an API get-product is fired, I want to check the token first. If the token doesn't exist I want to make a token request then continue the API request.

sindresorhus commented 1 year ago

Hooks are async, so the request will wait for the hooks: https://github.com/sindresorhus/ky#hooksafterresponse

sholladay commented 3 months ago

It sounds like you want a beforeRequest hook, but refreshing the token in an afterResponse hook only if a request failed, as in the example that Sindre linked to, is definitely the more idiomatic and efficient way to do it. Hope that helps!