Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support parseAs option for error reponses in client-fetch #1606

Open
crtl opened this issue Jan 21, 2025 · 0 comments
Open

Support parseAs option for error reponses in client-fetch #1606

crtl opened this issue Jan 21, 2025 · 0 comments
Labels
feature 🚀 New feature or request

Comments

@crtl
Copy link

crtl commented Jan 21, 2025

Description

Currently all error responses are expected to be json:

https://github.com/hey-api/openapi-ts/blob/ea24bca1ef3e30e5b6543d4b5b791ca9fb4fd322/packages/client-fetch/src/index.ts#L125C1-L132C1

    let error = await response.text();

    try {
      error = JSON.parse(error);
    } catch {
      // noop
    }

I am currently implementing refresh token logic using response interceptor and I want to use the client to retry the request but this will cause the body to be parsed twice in case of error response.

async function refreshTokenInterceptor(
        response: Response,
        request: Request,
        options: RequestOptions<false> & {_retry?: boolean},
{
  if (response.status !== 401) return response;

  // Refresh token ...

  // this .request() call will call .text() on the response
  // The original .request() call before token was refreshed will call .text() after below request is resolved, 
  // throwing an error because body cant be read twice
  return options.client.request({...options, parseAs: "stream", _retry: true});
}

Passing parseAs: stream circumvents the parsing of the body on success responses but it does not work on error responses.

parseAs should also be applied when parsing error responses.

Package versions are:

"@hey-api/client-fetch": "^0.7.1",
"@hey-api/openapi-ts": "^0.62.3",
```
@crtl crtl added the feature 🚀 New feature or request label Jan 21, 2025
@crtl crtl changed the title Support parseAs option for error reponses Support parseAs option for error reponses in client-fetch Jan 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature 🚀 New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant