Skip to content

Commit

Permalink
feat: support retry and default to 1
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Nov 4, 2021
1 parent ad3785c commit ec83366
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,16 @@ In order to bypass errors as response you can use `error.data`:
await $fetch(...).catch((error) => error.data)
```

## ✔️ Auto Retry

`$fetch` Automatically retries the request if an error happens. Default is `1` (except for `POST`, `PUT` and `PATCH` methods that is `0`)

```ts
await $fetch('http://google.com/404', {
retry: 3
})
```

## ✔️ Type Friendly

Response can be type assisted:
Expand Down
2 changes: 1 addition & 1 deletion src/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function normalizeStack (stack: string = '') {
.filter(l =>
!l.includes('createFetchError') &&
!l.includes('at $fetch') &&
!l.includes('at raw') &&
!l.includes('at $fetchRaw') &&
!l.includes('processTicksAndRejections')
)
.join('\n')
Expand Down
17 changes: 13 additions & 4 deletions src/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export interface FetchOptions extends Omit<RequestInit, 'body'> {
params?: SearchParams
parseResponse?: (responseText: string) => any
response?: boolean
retry?: number
}

export interface FetchResponse<T> extends Response { data?: T }
Expand Down Expand Up @@ -45,15 +46,16 @@ export function setHeader (options: FetchOptions, _key: string, value: string) {
}

export function createFetch ({ fetch }: CreateFetchOptions): $Fetch {
const raw: $Fetch['raw'] = async function (request, opts = {}) {
const $fetchRaw: $Fetch['raw'] = async function (request, opts = {}) {
const hasPayload = payloadMethods.includes((opts.method || '').toLowerCase())
if (typeof request === 'string') {
if (opts.baseURL) {
request = joinURL(opts.baseURL, request)
}
if (opts.params) {
request = withQuery(request, opts.params)
}
if (opts.body && opts.body.toString() === '[object Object]' && payloadMethods.includes((opts.method || '').toLowerCase() || '')) {
if (opts.body && opts.body.toString() === '[object Object]' && hasPayload) {
opts.body = JSON.stringify(opts.body)
setHeader(opts, 'content-type', 'application/json')
}
Expand All @@ -63,16 +65,23 @@ export function createFetch ({ fetch }: CreateFetchOptions): $Fetch {
const parseFn = opts.parseResponse || destr
response.data = parseFn(text)
if (!response.ok) {
const retries = opts.retry ?? (hasPayload ? 0 : 1)
if (retries > 0) {
return $fetchRaw(request, {
...opts,
retry: retries - 1
})
}
throw createFetchError(request, response)
}
return response
}

const $fetch = function (request, opts) {
return raw(request, opts).then(r => r.data)
return $fetchRaw(request, opts).then(r => r.data)
} as $Fetch

$fetch.raw = raw
$fetch.raw = $fetchRaw

return $fetch
}

0 comments on commit ec83366

Please sign in to comment.