Skip to content

Commit 3d0aa27

Browse files
nijitaropi0
authored andcommitted
feat: add onRetry hook (#79)
1 parent 55b8975 commit 3d0aa27

3 files changed

Lines changed: 19 additions & 1 deletion

File tree

docs/guide/advanced.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,18 @@ For registering hooks, you have to create a nuxt plugin:
2929
**plugins/http.js**
3030

3131
```js
32+
import ky from 'ky-universal'
33+
3234
export default function ({ $http }) {
3335
$http.onRequest(config => {
3436
console.log('Making request to ' + config.url)
3537
})
3638

39+
$http.onRetry(async (request, options, errors, retryCount) => {
40+
const token = await ky('https://example.com/refresh-token')
41+
options.header.set('Authorization', `Bearer ${token}`)
42+
})
43+
3744
$http.onError(error => {
3845
if(error.response.status === 500) {
3946
alert('Request Error!')

lib/plugin.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ class HTTP {
3434
this._hook('beforeRequest', fn)
3535
}
3636

37+
onRetry(fn) {
38+
this._hook('beforeRetry', fn)
39+
}
40+
3741
onResponse(fn) {
3842
this._hook('afterResponse', fn)
3943
}

types/index.d.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import Vue from 'vue'
2-
import { ResponsePromise, Options, BeforeRequestHook, AfterResponseHook, HTTPError } from 'ky'
2+
import { ResponsePromise, Options, BeforeRequestHook, BeforeRetryHook, AfterResponseHook, HTTPError } from 'ky'
33
import './vuex'
44

55
type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;
@@ -126,6 +126,13 @@ interface NuxtHTTPInstance {
126126
*/
127127
onRequest(hook: BeforeRequestHook): void
128128

129+
/**
130+
* Set a hook on `beforeRetry` (Before request is sent)
131+
*
132+
* This hook enables you to modify the request right before retry. It will make no further changes to the request after this. The hook function receives the normalized input and options, an error instance and the retry count as arguments. You could, for example, modify `options.headers` here.
133+
*/
134+
onRetry(hook: BeforeRetryHook): void
135+
129136
/**
130137
* Set a hook on `afterResponse` (After the response is received)
131138
*

0 commit comments

Comments
 (0)