Skip to content

Option to return non Response object from ky via afterResponse #696

@superr-fluous

Description

@superr-fluous

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions