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

feat(fetch): avoid exception when the response body has no content #1778

Conversation

soartec-lab
Copy link
Member

@soartec-lab soartec-lab commented Dec 29, 2024

Status

HOLD

I'm waiting for more options to gather in #1656

Description

fix #1656

This avoids an exception that occurs when executing res.json() if there is no content in the response body.

export const listPets = async (
  params?: ListPetsParams,
  options?: RequestInit,
): Promise<listPetsResponse> => {
  const res = await fetch(getListPetsUrl(params), {
    ...options,
    method: 'GET',
  });

-  const data: Pets = await res.json();
+  const data: Pets =
    [204, 205, 304].includes(res.status) || !res.body ? {} : await res.json();

  return { status: res.status, data, headers: res.headers };
};

Related PRs

none

Todos

  • Tests
  • Documentation
  • Changelog Entry (unreleased)

Steps to Test or Reproduce

you can check by sample apps

@soartec-lab soartec-lab changed the title feat(fetch): avoid exception when the response body has no content. feat(fetch): avoid exception when the response body has no content Dec 29, 2024
@@ -181,7 +181,7 @@ ${
`
: `const res = await fetch(${fetchFnOptions})

const data:${response.definition.success} = await res.json()
const data:${response.definition.success} = res.body ? await res.json() : {}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure this will avoid the error? I tried it with

const responseBody = res.body ? await res.json() : {};

but it throws

Uncaught (in promise) SyntaxError: JSON.parse: unexpected end of data at line 1 column 1 of the JSON data

if there is no response body. Using the following will not throw any errors:

const responseBody = await res.json().catch(() => ({}));

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As you pointed out, this code will cause an error if the body exists but is empty.
I think I need to check it by referencing the plain text once like const text = res.text().

@soartec-lab soartec-lab marked this pull request as ready for review January 4, 2025 02:00
@soartec-lab
Copy link
Member Author

@zZHorizonZz @nimo23 @clemeth

I have finalized the specifications based on discussions with everyone, so check them out.
Please note that the order of logic definitions has been changed for readability.

#1656 (comment)

@soartec-lab soartec-lab requested a review from nimo23 January 5, 2025 02:35
@melloware melloware merged commit 743a71f into orval-labs:master Jan 5, 2025
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

204 No content generated code throws JSON parsing error
5 participants