refactor: Use typed responses for api calls
Opened this issue · 0 comments
theborakompanioni commented
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(() => ({}))
}