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

refactor: Use typed responses for api calls #670

Open
theborakompanioni opened this issue Oct 4, 2023 · 0 comments
Open

refactor: Use typed responses for api calls #670

theborakompanioni opened this issue Oct 4, 2023 · 0 comments
Labels
refactoring Code refactoring without functional changes tech dept Removes some technical debt
Milestone

Comments

@theborakompanioni
Copy link
Collaborator

theborakompanioni commented Oct 4, 2023

Use typed responses instead of generic Response type from fetch.

Currently, every component parses the body by itself (including error handling).
With this change applied, calling api endpoints should be much more streamlined and simplified.

e.g. from https://eckertalex.dev/blog/typescript-fetch-wrapper

async function request<T>(path: string, config: RequestInit): Promise<T> {
  const response = await fetch(new Request(path, config))
  
  if (!response.ok) {
  // TODO: use Api.Helper here instead generic Error
  throw new Error({ name: response.status, message: response.statusText })
  }
  
  // may error if there is no body, return empty
  return response.json().catch(() => ({}))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
refactoring Code refactoring without functional changes tech dept Removes some technical debt
Projects
Status: No status
Development

No branches or pull requests

2 participants