Skip to content

[bug] fix data mismatch between routes and APIFunctions #2021

Description

@adarshm11

almost every API function has this code:

if (res.ok) {
  const data = await res.json();
  // ... other code ...
}

the problem is that if this is called on a response from a route that has an OK response that returns a string and not json, like:

return res.status(OK).send("Card successfully verified");

an error will be thrown because the response body doesn't have json to parse. that means that errors will be thrown when the behavior was actually correct. so we can do one of two things:

either we

  • have API functions check the type of the response body:
let data;
const contentType = response.headers.get('content-type');

if (contentType?.includes('application/json')) {
  data = await response.json();
} else {
  data = await response.text();
}

OR we

  • refactor the routes' OK responses to just return these strings in a json format, like:
return res.status(OK).json({
  message: "Card successfully verified"
});

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions