Skip to content

Commit

Permalink
feat: allow for the response error to be configurable (#73)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicosantangelo committed Jun 16, 2020
1 parent 7d4d17b commit b546d5d
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/lib/api.ts
Expand Up @@ -40,9 +40,9 @@ export class BaseAPI {
const data = response.data
const result = data.data // One for axios data, another for the servers data

return data && !data.ok
? Promise.reject({ message: data.error, data: result })
: result
const error = this.getResponseError(data)

return error ? Promise.reject({ message: error, data: result }) : result
})
.catch((error: AxiosError) => {
console.warn(`[API] HTTP request failed: ${error.message || ''}`, error)
Expand All @@ -53,4 +53,11 @@ export class BaseAPI {
getUrl(path: string) {
return `${this.url}${path}`
}

getResponseError(data: any): string | undefined {
if (data && !data.ok) {
return data.error
}
return undefined
}
}

0 comments on commit b546d5d

Please sign in to comment.