Skip to content

Commit

Permalink
feat: retry option for failed requests (closes #29)
Browse files Browse the repository at this point in the history
  • Loading branch information
johannschopplich committed Aug 14, 2023
1 parent b051739 commit a76a483
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 1 deletion.
2 changes: 2 additions & 0 deletions docs/.vitepress/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ function nav(): DefaultTheme.NavItem[] {
{ text: 'Hydration', link: '/guide/hydration' },
{ text: 'Caching', link: '/guide/caching' },
{ text: 'Cookies', link: '/guide/cookies' },
{ text: 'Retries', link: '/guide/retries' },
{ text: 'Dynamic Backend URL', link: '/guide/dynamic-backend-url' },
],
},
Expand Down Expand Up @@ -140,6 +141,7 @@ function sidebarGuide(): DefaultTheme.SidebarItem[] {
{ text: 'Hydration', link: '/guide/hydration' },
{ text: 'Caching', link: '/guide/caching' },
{ text: 'Cookies', link: '/guide/cookies' },
{ text: 'Retries', link: '/guide/retries' },
{ text: 'Dynamic Backend URL', link: '/guide/dynamic-backend-url' },
],
},
Expand Down
1 change: 1 addition & 0 deletions docs/api/use-api-party-data.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type UseApiDataOptions<T> =
| 'query'
| 'headers'
| 'method'
| 'retry'
>
& {
body?: MaybeRef<string | Record<string, any> | FormData | null | undefined>
Expand Down
40 changes: 40 additions & 0 deletions docs/guide/retries.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Retries

From time to time, fetch requests may fail. This can happen for a variety of reasons, like a network error or a server error. In these cases, you may want to retry the request a few times before giving up.

You can configure retries for a single request by passing a `retry` option to the `useApiPartyData` and `$apiParty` composables. It can be a number, `false` or `undefined`, either reactive or not:

```ts
const retry: MaybeRef<number | false | undefined>
```
Example:
```ts
// Retry failed requests 3 times
const { data } = await useJsonPlaceholderData('posts/1', {
retry: 3,
})
```

By default, the `retry` option is set to `undefined`, meaning that no retries will be attempted.

::: info
These examples assume that you have set up an API endpoint called `jsonPlaceholder`:

```ts
// `nuxt.config.ts`
export default defineNuxtConfig({
modules: ['nuxt-api-party'],

apiParty: {
endpoints: {
jsonPlaceholder: {
url: 'https://jsonplaceholder.typicode.com'
}
}
}
})
```

:::
1 change: 1 addition & 0 deletions docs/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"name": "docs",
"type": "module",
"private": true,
"scripts": {
"dev": "vitepress dev",
Expand Down
3 changes: 2 additions & 1 deletion src/runtime/composables/useApiData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import type { EndpointFetchOptions, MaybeRef, MaybeRefOrGetter } from '../utils'
import { useAsyncData, useRequestHeaders, useRuntimeConfig } from '#imports'

type ComputedOptions<T extends Record<string, any>> = {
[K in keyof T]: T[K] extends Function
[K in keyof T]: T[K] extends (...args: any[]) => any
? T[K]
: T[K] extends Record<string, any>
? ComputedOptions<T[K]> | MaybeRef<T[K]>
Expand All @@ -29,6 +29,7 @@ export type UseApiDataOptions<T> =
| 'query'
| 'headers'
| 'method'
| 'retry'
>
& {
body?: MaybeRef<string | Record<string, any> | FormData | null | undefined>
Expand Down

0 comments on commit a76a483

Please sign in to comment.