Skip to content

refactor: Use typed responses for api calls #670

@theborakompanioni

Description

@theborakompanioni

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(() => ({}))
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    blockedMerging this pull request is blocked until another issue is resolvedrefactoringCode refactoring without functional changestech deptRemoves some technical debt

    Type

    No type

    Projects

    Status

    No status

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions