Skip to content

Commit b79f0cb

Browse files
committed
docs: add retry documentation
1 parent 6672bcd commit b79f0cb

2 files changed

Lines changed: 65 additions & 11 deletions

File tree

README.md

Lines changed: 59 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Find and replace all on all files (CMD+SHIFT+F):
2424

2525
<!-- Highlight some of the features your module provide here -->
2626
- ✅ Using Fetch instead of XHR
27-
- ✅ Built-in adapter for Dedupe, and Priority Queue request.
27+
- ✅ Built-in adapter for Retry, Dedupe, and Priority Queue request.
2828
- ✅ Composable hook for Axios interceptors.
2929

3030
## Compabilities
@@ -133,26 +133,80 @@ onResponseError(async (error) => {
133133

134134
## Queue
135135

136-
All request per instance will be add into queue before sent with priority `1`.
136+
All request per instance will be add into queue before sent with priority MEDIUM (`20`).
137137
If you want to send your request first before the others, you can set using option `priority`. The higher priority will run first.
138138

139139
```ts
140+
import { QueuePriority } from '@privyid/nuapi/core'
141+
140142
useApi().get('/document/load', {
141-
priority: 2,
143+
// Using presets
144+
priority: QueuePriority.HIGH,
145+
// Or using number
146+
priority: 50,
142147
})
143148
```
144149

145150
## Dedupe
146151

147152
Sometime, you want to cancel request with same endpoint like when you working with searching or filter.
148153

149-
NuAPI has built in function for this case. Just set `requestId`, multiple sent request with same id will cancel last request before.
154+
NuAPI has built in function for this case. Just set `requestkey`, multiple sent request with same id will cancel last request before.
150155

151156
```ts
152157
useApi().get('/document/load', {
153-
requestId: 'document-load',
158+
requestkey: 'document-load',
154159
})
155160
```
161+
162+
### Cancel Manually
163+
164+
Cancel spesific request by `requestKey` using `.cancel()`
165+
166+
```ts
167+
useApi().cancel('document-load')
168+
```
169+
170+
Or cancel all requests that have `requestKey` using `.cancelAll()`
171+
172+
```ts
173+
useApi().cancelAll()
174+
```
175+
176+
## Retry
177+
178+
NuAPI automatically retries request when got an error with status code:
179+
180+
- 408 - Request Timeout
181+
- 409 - Conflict
182+
- 425 - Too Early
183+
- 429 - Too Many Requests
184+
- 500 - Internal Server Error
185+
- 502 - Bad Gateway
186+
- 503 - Service Unavailable
187+
- 504 - Gateway Timeout
188+
189+
By default will retries `3` times (except for `PATCH`, `POST`, `PUT`, `DELETE`) can be changed using option `retry`.
190+
191+
```ts
192+
useApi().get('/document/load', {
193+
retry: 5,
194+
})
195+
```
196+
197+
### Customize Retry Condition
198+
199+
You can customize when request should retries using `retryOn`
200+
201+
```ts
202+
useApi().get('/document/load', {
203+
retryOn (error) {
204+
return getCode(error) === 423
205+
&& error.config.retryCount < 3
206+
},
207+
})
208+
```
209+
156210
## API
157211

158212
👉 You can learn more about usage in [JSDocs Documentation](https://www.jsdocs.io/package/@privyid/nuapi).

src/core/index.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,10 @@ export interface LazyInstance {
108108
setApi: (instance: ApiInstance) => void,
109109
}
110110

111-
function resolveOptions (options: ApiConfig | (() => ApiConfig)) {
112-
return typeof options === 'function'
113-
? options()
114-
: options
111+
function toValue<T extends object = any> (value: T | (() => T)) {
112+
return typeof value === 'function'
113+
? value()
114+
: value
115115
}
116116

117117
/**
@@ -129,8 +129,8 @@ export function createLazyton (options: ApiConfig | (() => ApiConfig) = {}, fres
129129
const getApi = function () {
130130
if (!api) {
131131
api = fresh
132-
? createApi(resolveOptions(options))
133-
: useApi().create(resolveOptions(options))
132+
? createApi(toValue(options))
133+
: useApi().create(toValue(options))
134134
}
135135

136136
return api

0 commit comments

Comments
 (0)