-
-
Notifications
You must be signed in to change notification settings - Fork 410
Description
Hi! First of all, thanks for the lib, really like it so far!
As per docs for the afterResponse
hook:
The return value of the hook function will be used by Ky as the response object if it's an instance of Response
I was wondering if there is a specific reason to always return a Response object and is it a viable idea to add an option to return just the value provided by the last afterResponse
hook?
The use-case for this could be to de-serialize JSON
at a single point in an app and possibly to add some additional data to the returned value (basically to narrow the scope of the returned data from network requests).
ky.create({
hooks: {
afterResponse: async (req, opt, res) => {
if (res.status === 200) {
return { ok: true, ...(await res.json()) };
}
return { ok: false, {/* some additional fields */} };
},
},
})
The issue with the current implementation would be the need to re-serialize data when creating a new Response
object, thus creating unnecessary overhead, and, well, the fact that one would need to de-serealize it again.
Thanks for taking time to look at my issue! And my apologies in case I missed some info about my use-case in the docs or failed to spot a related existing issue.