-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
v0.4.1: remove undefined in
params
and body
- Loading branch information
Showing
10 changed files
with
210 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -61,8 +61,8 @@ instance.get('/api/hello').then((res) => { | |
Using jsDelivr CDN: | ||
|
||
```html | ||
<script src="https://cdn.jsdelivr.net/npm/[email protected].0/dist/xior.umd.js"></script> | ||
<script src="https://cdn.jsdelivr.net/npm/[email protected].0/plugins/mock.umd.js"></script> | ||
<script src="https://cdn.jsdelivr.net/npm/[email protected].1/dist/xior.umd.js"></script> | ||
<script src="https://cdn.jsdelivr.net/npm/[email protected].1/plugins/mock.umd.js"></script> | ||
|
||
<!-- Usage --> | ||
<script> | ||
|
@@ -79,8 +79,8 @@ Using jsDelivr CDN: | |
Using unpkg CDN: | ||
|
||
```html | ||
<script src="https://unpkg.com/[email protected].0/dist/xior.umd.js"></script> | ||
<script src="https://unpkg.com/[email protected].0/plugins/mock.umd.js"></script> | ||
<script src="https://unpkg.com/[email protected].1/dist/xior.umd.js"></script> | ||
<script src="https://unpkg.com/[email protected].1/plugins/mock.umd.js"></script> | ||
|
||
<!-- Usage --> | ||
<script> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -121,7 +121,7 @@ yarn add xior | |
Use jsDelivr CDN: | ||
|
||
```html | ||
<script src="https://cdn.jsdelivr.net/npm/[email protected].0/dist/xior.umd.js"></script> | ||
<script src="https://cdn.jsdelivr.net/npm/[email protected].1/dist/xior.umd.js"></script> | ||
|
||
<!-- Usage --> | ||
<script> | ||
|
@@ -136,7 +136,7 @@ Use jsDelivr CDN: | |
Use unpkg CDN: | ||
|
||
```html | ||
<script src="https://unpkg.com/[email protected].0/dist/xior.umd.js"></script> | ||
<script src="https://unpkg.com/[email protected].1/dist/xior.umd.js"></script> | ||
|
||
<!-- Usage --> | ||
<script> | ||
|
@@ -660,9 +660,9 @@ Use CDN: | |
Using jsDelivr CDN: | ||
|
||
```html | ||
<script src="https://cdn.jsdelivr.net/npm/[email protected].0/dist/xior.umd.js"></script> | ||
<script src="https://cdn.jsdelivr.net/npm/[email protected].1/dist/xior.umd.js"></script> | ||
<!-- Load plugin --> | ||
<script src="https://cdn.jsdelivr.net/npm/[email protected].0/plugins/error-retry.umd.js"></script> | ||
<script src="https://cdn.jsdelivr.net/npm/[email protected].1/plugins/error-retry.umd.js"></script> | ||
|
||
<!-- Usage --> | ||
<script> | ||
|
@@ -675,10 +675,10 @@ Using jsDelivr CDN: | |
Using unpkg CDN: | ||
|
||
```html | ||
<script src="https://unpkg.com/[email protected].0/dist/xior.umd.js"></script> | ||
<script src="https://unpkg.com/[email protected].1/dist/xior.umd.js"></script> | ||
|
||
<!-- Load plugin --> | ||
<script src="https://unpkg.com/[email protected].0/plugins/error-retry.umd.js"></script> | ||
<script src="https://unpkg.com/[email protected].1/plugins/error-retry.umd.js"></script> | ||
|
||
<!-- Usage --> | ||
<script> | ||
|
@@ -771,9 +771,9 @@ Use CDN: | |
Using jsDelivr CDN: | ||
|
||
```html | ||
<script src="https://cdn.jsdelivr.net/npm/[email protected].0/dist/xior.umd.js"></script> | ||
<script src="https://cdn.jsdelivr.net/npm/[email protected].1/dist/xior.umd.js"></script> | ||
<!-- Load plugin --> | ||
<script src="https://cdn.jsdelivr.net/npm/[email protected].0/plugins/throttle.umd.js"></script> | ||
<script src="https://cdn.jsdelivr.net/npm/[email protected].1/plugins/throttle.umd.js"></script> | ||
|
||
<!-- Usage --> | ||
<script> | ||
|
@@ -786,10 +786,10 @@ Using jsDelivr CDN: | |
Using unpkg CDN: | ||
|
||
```html | ||
<script src="https://unpkg.com/[email protected].0/dist/xior.umd.js"></script> | ||
<script src="https://unpkg.com/[email protected].1/dist/xior.umd.js"></script> | ||
|
||
<!-- Load plugin --> | ||
<script src="https://unpkg.com/[email protected].0/plugins/throttle.umd.js"></script> | ||
<script src="https://unpkg.com/[email protected].1/plugins/throttle.umd.js"></script> | ||
|
||
<!-- Usage --> | ||
<script> | ||
|
@@ -844,9 +844,9 @@ Use CDN: | |
Using jsDelivr CDN: | ||
|
||
```html | ||
<script src="https://cdn.jsdelivr.net/npm/[email protected].0/dist/xior.umd.js"></script> | ||
<script src="https://cdn.jsdelivr.net/npm/[email protected].1/dist/xior.umd.js"></script> | ||
<!-- Load plugin --> | ||
<script src="https://cdn.jsdelivr.net/npm/[email protected].0/plugins/dedupe.umd.js"></script> | ||
<script src="https://cdn.jsdelivr.net/npm/[email protected].1/plugins/dedupe.umd.js"></script> | ||
|
||
<!-- Usage --> | ||
<script> | ||
|
@@ -859,10 +859,10 @@ Using jsDelivr CDN: | |
Using unpkg CDN: | ||
|
||
```html | ||
<script src="https://unpkg.com/[email protected].0/dist/xior.umd.js"></script> | ||
<script src="https://unpkg.com/[email protected].1/dist/xior.umd.js"></script> | ||
|
||
<!-- Load plugin --> | ||
<script src="https://unpkg.com/[email protected].0/plugins/dedupe.umd.js"></script> | ||
<script src="https://unpkg.com/[email protected].1/plugins/dedupe.umd.js"></script> | ||
|
||
<!-- Usage --> | ||
<script> | ||
|
@@ -924,9 +924,9 @@ Use CDN: | |
Using jsDelivr CDN: | ||
|
||
```html | ||
<script src="https://cdn.jsdelivr.net/npm/[email protected].0/dist/xior.umd.js"></script> | ||
<script src="https://cdn.jsdelivr.net/npm/[email protected].1/dist/xior.umd.js"></script> | ||
<!-- Load plugin --> | ||
<script src="https://cdn.jsdelivr.net/npm/[email protected].0/plugins/error-cache.umd.js"></script> | ||
<script src="https://cdn.jsdelivr.net/npm/[email protected].1/plugins/error-cache.umd.js"></script> | ||
|
||
<!-- Usage --> | ||
<script> | ||
|
@@ -939,10 +939,10 @@ Using jsDelivr CDN: | |
Using unpkg CDN: | ||
|
||
```html | ||
<script src="https://unpkg.com/[email protected].0/dist/xior.umd.js"></script> | ||
<script src="https://unpkg.com/[email protected].1/dist/xior.umd.js"></script> | ||
|
||
<!-- Load plugin --> | ||
<script src="https://unpkg.com/[email protected].0/plugins/error-cache.umd.js"></script> | ||
<script src="https://unpkg.com/[email protected].1/plugins/error-cache.umd.js"></script> | ||
|
||
<!-- Usage --> | ||
<script> | ||
|
@@ -1087,9 +1087,9 @@ Use CDN: | |
Using jsDelivr CDN: | ||
|
||
```html | ||
<script src="https://cdn.jsdelivr.net/npm/[email protected].0/dist/xior.umd.js"></script> | ||
<script src="https://cdn.jsdelivr.net/npm/[email protected].1/dist/xior.umd.js"></script> | ||
<!-- Load plugin --> | ||
<script src="https://cdn.jsdelivr.net/npm/[email protected].0/plugins/progress.umd.js"></script> | ||
<script src="https://cdn.jsdelivr.net/npm/[email protected].1/plugins/progress.umd.js"></script> | ||
|
||
<!-- Usage --> | ||
<script> | ||
|
@@ -1102,10 +1102,10 @@ Using jsDelivr CDN: | |
Using unpkg CDN: | ||
|
||
```html | ||
<script src="https://unpkg.com/[email protected].0/dist/xior.umd.js"></script> | ||
<script src="https://unpkg.com/[email protected].1/dist/xior.umd.js"></script> | ||
|
||
<!-- Load plugin --> | ||
<script src="https://unpkg.com/[email protected].0/plugins/progress.umd.js"></script> | ||
<script src="https://unpkg.com/[email protected].1/plugins/progress.umd.js"></script> | ||
|
||
<!-- Usage --> | ||
<script> | ||
|
@@ -1212,9 +1212,9 @@ Use CDN: | |
Using jsDelivr CDN: | ||
|
||
```html | ||
<script src="https://cdn.jsdelivr.net/npm/[email protected].0/dist/xior.umd.js"></script> | ||
<script src="https://cdn.jsdelivr.net/npm/[email protected].1/dist/xior.umd.js"></script> | ||
<!-- Load plugin --> | ||
<script src="https://cdn.jsdelivr.net/npm/[email protected].0/plugins/mock.umd.js"></script> | ||
<script src="https://cdn.jsdelivr.net/npm/[email protected].1/plugins/mock.umd.js"></script> | ||
|
||
<!-- Usage --> | ||
<script> | ||
|
@@ -1227,10 +1227,10 @@ Using jsDelivr CDN: | |
Using unpkg CDN: | ||
|
||
```html | ||
<script src="https://unpkg.com/[email protected].0/dist/xior.umd.js"></script> | ||
<script src="https://unpkg.com/[email protected].1/dist/xior.umd.js"></script> | ||
|
||
<!-- Load plugin --> | ||
<script src="https://unpkg.com/[email protected].0/plugins/mock.umd.js"></script> | ||
<script src="https://unpkg.com/[email protected].1/plugins/mock.umd.js"></script> | ||
|
||
<!-- Usage --> | ||
<script> | ||
|
@@ -1319,6 +1319,7 @@ import { | |
isAbsoluteURL, | ||
joinPath, | ||
isXiorError, | ||
trimUndefined, | ||
} from 'xior'; | ||
``` | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import xior, { XiorError as AxiosError, XiorInstance, XiorResponse } from 'xior'; | ||
import errorRetry from 'xior/plugins/error-retry'; | ||
|
||
const http = xior.create(); | ||
|
||
http.plugins.use( | ||
errorRetry({ | ||
enableRetry: (config, error) => { | ||
Check failure on line 8 in src/plugins/token-refresh.ts GitHub Actions / Build
|
||
if (error.response && shouldRefreshToken(error.response)) { | ||
return true; | ||
} | ||
}, | ||
}) | ||
); | ||
setupTokenRefresh( | ||
http, | ||
// eslint-disable-next-line node/handle-callback-err | ||
async (error) => { | ||
const { data } = await http.post('/new/token'); | ||
if (data.token) { | ||
// Set new token here | ||
// localStorage.setItem('TOKEN', token); | ||
// or | ||
// http.defaults.headers['Authorization'] = `Beare ${data.token}`; | ||
} else { | ||
return Promise.reject(new Error('Wrong token')); | ||
} | ||
}, | ||
{ | ||
shouldRefresh(response) { | ||
return Boolean(response?.status && [401, 403].includes(response.status)); | ||
}, | ||
} | ||
); | ||
|
||
export function shouldRefreshToken(response: XiorResponse) { | ||
return response?.status && [401].includes(response.status); | ||
} | ||
export default function setupTokenRefresh( | ||
instance: XiorInstance, | ||
getToken: (error: AxiosError) => Promise<any>, | ||
options?: { | ||
shouldRefresh: (response: XiorResponse) => boolean; | ||
} | ||
) { | ||
const shouldRefresh = options?.shouldRefresh || shouldRefreshToken; | ||
|
||
let refreshingToken = false; | ||
const queue: (() => void)[] = []; | ||
instance.interceptors.response.use( | ||
async (response) => { | ||
return response; | ||
}, | ||
async (error: AxiosError) => { | ||
if (error?.response && shouldRefresh(error.response)) { | ||
if (refreshingToken) { | ||
await new Promise<void>((resolve) => { | ||
queue.push(resolve); | ||
}); | ||
} else { | ||
refreshingToken = true; | ||
try { | ||
await getToken(error); | ||
} finally { | ||
refreshingToken = false; | ||
queue.forEach((r) => r()); | ||
} | ||
} | ||
} | ||
return Promise.reject(error); | ||
} | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.