Skip to content

Allow consuming the return of "ctx.fetch" as a mocked response #901

@kettanaito

Description

@kettanaito

Is your feature request related to a problem? Please describe.

Currently, if you wish to proxy a request and use some other response as the mocked response you'd have to construct the response composition chain manually, which is tedious and at times impossible without external utilities (like headers-utils):

export const handlers = [
  ...createProxy('/api/*', async (req, res, ctx) => {
    const proxyUrl = new URL(req.url.pathname, 'https://reqres.in')
    const proxy = await ctx.fetch(proxyUrl.href)

    return res(
      ctx.status(proxy.status),
      ctx.set(headersToObject(proxy.headers)),
      ctx.body(await proxy.text())
    )
  }),
]

Describe the solution you'd like
I'd like to have an opportunity to return whichever response I get from ctx.fetch from my resolver, so it could be used as a mocked response.

const proxy = await ctx.fetch(anotherUrl)

return res(proxy)

I'd still like to preserve the return type of ctx.fetch being Response, because it's familiar and standardized. Perhaps there can be a new API/extension that'd coerce Response to ResponseComposition or an input to it.

Describe alternatives you've considered
Manual response composition is the only current alternative.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions