Skip to content

Commit

Permalink
fix(error-retry plugin): if have request inteceptors, when error re…
Browse files Browse the repository at this point in the history
…try, retry with the latest request cofnig from `request inteceptors`
  • Loading branch information
suhaotian committed Mar 25, 2024
1 parent bc2d0c5 commit dd9c308
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 25 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# CHANGELOG 📝

## v0.3.1 2024/03/25

- fix(error-retry plugin): if have `request inteceptors`, when error retry, retry with the latest request cofnig from `request inteceptors`

## v0.3.0 2024/03/24

- fix(core): POST/DELETE/PUT/PATCH methods when `content-type=application/x-www-form-urlencoded`, use formData to in body(previous put in url)
Expand Down
4 changes: 2 additions & 2 deletions Mock-plugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ instance.get('/api/hello').then((res) => {
Using jsDelivr CDN:

```html
<script src="https://cdn.jsdelivr.net/npm/xior@0.3.0/dist/xior.umd.js"></script>
<script src="https://cdn.jsdelivr.net/npm/xior@0.3.1/dist/xior.umd.js"></script>
<script src="https://cdn.jsdelivr.net/npm/xior@0.2.6/plugins/mock.umd.js"></script>

<!-- Usage -->
Expand All @@ -79,7 +79,7 @@ Using jsDelivr CDN:
Using unpkg CDN:

```html
<script src="https://unpkg.com/xior@0.3.0/dist/xior.umd.js"></script>
<script src="https://unpkg.com/xior@0.3.1/dist/xior.umd.js"></script>
<script src="https://unpkg.com/xior@0.2.6/plugins/mock.umd.js"></script>

<!-- Usage -->
Expand Down
30 changes: 15 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ yarn add xior
Use jsDelivr CDN:

```html
<script src="https://cdn.jsdelivr.net/npm/xior@0.3.0/dist/xior.umd.js"></script>
<script src="https://cdn.jsdelivr.net/npm/xior@0.3.1/dist/xior.umd.js"></script>

<!-- Usage -->
<script>
Expand All @@ -132,7 +132,7 @@ Use jsDelivr CDN:
Use unpkg CDN:

```html
<script src="https://unpkg.com/xior@0.3.0/dist/xior.umd.js"></script>
<script src="https://unpkg.com/xior@0.3.1/dist/xior.umd.js"></script>

<!-- Usage -->
<script>
Expand Down Expand Up @@ -498,7 +498,7 @@ Use CDN:
Using jsDelivr CDN:

```html
<script src="https://cdn.jsdelivr.net/npm/xior@0.3.0/dist/xior.umd.js"></script>
<script src="https://cdn.jsdelivr.net/npm/xior@0.3.1/dist/xior.umd.js"></script>
<!-- Load plugin -->
<script src="https://cdn.jsdelivr.net/npm/xior@0.2.6/plugins/error-retry.umd.js"></script>

Expand All @@ -513,7 +513,7 @@ Using jsDelivr CDN:
Using unpkg CDN:

```html
<script src="https://unpkg.com/xior@0.3.0/dist/xior.umd.js"></script>
<script src="https://unpkg.com/xior@0.3.1/dist/xior.umd.js"></script>

<!-- Load plugin -->
<script src="https://unpkg.com/xior@0.2.6/plugins/error-retry.umd.js"></script>
Expand Down Expand Up @@ -599,7 +599,7 @@ Use CDN:
Using jsDelivr CDN:

```html
<script src="https://cdn.jsdelivr.net/npm/xior@0.3.0/dist/xior.umd.js"></script>
<script src="https://cdn.jsdelivr.net/npm/xior@0.3.1/dist/xior.umd.js"></script>
<!-- Load plugin -->
<script src="https://cdn.jsdelivr.net/npm/xior@0.2.6/plugins/throttle.umd.js"></script>

Expand All @@ -614,7 +614,7 @@ Using jsDelivr CDN:
Using unpkg CDN:

```html
<script src="https://unpkg.com/xior@0.3.0/dist/xior.umd.js"></script>
<script src="https://unpkg.com/xior@0.3.1/dist/xior.umd.js"></script>

<!-- Load plugin -->
<script src="https://unpkg.com/xior@0.2.6/plugins/throttle.umd.js"></script>
Expand Down Expand Up @@ -665,7 +665,7 @@ Use CDN:
Using jsDelivr CDN:

```html
<script src="https://cdn.jsdelivr.net/npm/xior@0.3.0/dist/xior.umd.js"></script>
<script src="https://cdn.jsdelivr.net/npm/xior@0.3.1/dist/xior.umd.js"></script>
<!-- Load plugin -->
<script src="https://cdn.jsdelivr.net/npm/xior@0.2.6/plugins/dedupe.umd.js"></script>

Expand All @@ -680,7 +680,7 @@ Using jsDelivr CDN:
Using unpkg CDN:

```html
<script src="https://unpkg.com/xior@0.3.0/dist/xior.umd.js"></script>
<script src="https://unpkg.com/xior@0.3.1/dist/xior.umd.js"></script>

<!-- Load plugin -->
<script src="https://unpkg.com/xior@0.2.6/plugins/dedupe.umd.js"></script>
Expand Down Expand Up @@ -741,7 +741,7 @@ Use CDN:
Using jsDelivr CDN:

```html
<script src="https://cdn.jsdelivr.net/npm/xior@0.3.0/dist/xior.umd.js"></script>
<script src="https://cdn.jsdelivr.net/npm/xior@0.3.1/dist/xior.umd.js"></script>
<!-- Load plugin -->
<script src="https://cdn.jsdelivr.net/npm/xior@0.2.6/plugins/error-cache.umd.js"></script>

Expand All @@ -756,7 +756,7 @@ Using jsDelivr CDN:
Using unpkg CDN:

```html
<script src="https://unpkg.com/xior@0.3.0/dist/xior.umd.js"></script>
<script src="https://unpkg.com/xior@0.3.1/dist/xior.umd.js"></script>

<!-- Load plugin -->
<script src="https://unpkg.com/xior@0.2.6/plugins/error-cache.umd.js"></script>
Expand Down Expand Up @@ -894,7 +894,7 @@ Use CDN:
Using jsDelivr CDN:

```html
<script src="https://cdn.jsdelivr.net/npm/xior@0.3.0/dist/xior.umd.js"></script>
<script src="https://cdn.jsdelivr.net/npm/xior@0.3.1/dist/xior.umd.js"></script>
<!-- Load plugin -->
<script src="https://cdn.jsdelivr.net/npm/xior@0.2.6/plugins/progress.umd.js"></script>

Expand All @@ -909,7 +909,7 @@ Using jsDelivr CDN:
Using unpkg CDN:

```html
<script src="https://unpkg.com/xior@0.3.0/dist/xior.umd.js"></script>
<script src="https://unpkg.com/xior@0.3.1/dist/xior.umd.js"></script>

<!-- Load plugin -->
<script src="https://unpkg.com/xior@0.2.6/plugins/progress.umd.js"></script>
Expand Down Expand Up @@ -1019,7 +1019,7 @@ Use CDN:
Using jsDelivr CDN:

```html
<script src="https://cdn.jsdelivr.net/npm/xior@0.3.0/dist/xior.umd.js"></script>
<script src="https://cdn.jsdelivr.net/npm/xior@0.3.1/dist/xior.umd.js"></script>
<!-- Load plugin -->
<script src="https://cdn.jsdelivr.net/npm/xior@0.2.6/plugins/mock.umd.js"></script>

Expand All @@ -1034,7 +1034,7 @@ Using jsDelivr CDN:
Using unpkg CDN:

```html
<script src="https://unpkg.com/xior@0.3.0/dist/xior.umd.js"></script>
<script src="https://unpkg.com/xior@0.3.1/dist/xior.umd.js"></script>

<!-- Load plugin -->
<script src="https://unpkg.com/xior@0.2.6/plugins/mock.umd.js"></script>
Expand All @@ -1059,7 +1059,7 @@ Here are examples:
import xior from 'xior';

const instance = xior.create();
instance.plugins.use(function logPlugin(adapter) {
instance.plugins.use(function logPlugin(adapter, instance) {
return async (config) => {
const start = Date.now();
const res = await adapter(config);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "xior",
"version": "0.3.0",
"version": "0.3.1",
"description": "A lite request lib based on fetch with plugins support, and axios similar API",
"repository": "suhaotian/xior",
"bugs": "https://github.com/suhaotian/xior/issues",
Expand Down
13 changes: 9 additions & 4 deletions src/plugins/error-retry.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { delay } from './utils';
import { XiorPlugin, XiorRequestConfig } from '../types';
import { XiorInterceptorRequestConfig, XiorPlugin, XiorRequestConfig } from '../types';
import { XiorError } from '../utils';

export type ErrorRetryOptions = {
Expand Down Expand Up @@ -37,7 +37,7 @@ export default function xiorErrorRetryPlugin(options: ErrorRetryOptions = {}): X
...(options || {}),
};

return function (adapter) {
return function (adapter, instance) {
return async (config) => {
const {
retryTimes = _retryTimes,
Expand All @@ -49,7 +49,12 @@ export default function xiorErrorRetryPlugin(options: ErrorRetryOptions = {}): X
let timeUp = false;
let count = 0;

async function handleRequest() {
async function handleRequest(isRetry = false) {
if (isRetry && instance?.REQI) {
for (const item of instance.REQI) {
config = await item(config as XiorInterceptorRequestConfig);
}
}
try {
return await adapter(config);
} catch (error) {
Expand Down Expand Up @@ -77,7 +82,7 @@ export default function xiorErrorRetryPlugin(options: ErrorRetryOptions = {}): X
}
count++;
if (onRetry) onRetry(config, error as XiorError, count);
return handleRequest();
return handleRequest(true);
}
}

Expand Down
5 changes: 4 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { XiorInstance } from './xior';

export interface XiorRequestConfig<T = any> extends Omit<RequestInit, 'body'> {
url?: string;
headers?: Record<string, any>;
Expand Down Expand Up @@ -41,5 +43,6 @@ export interface XiorResponse<T = any> {
}

export type XiorPlugin = (
adapter: (request: XiorRequestConfig) => Promise<XiorResponse>
adapter: (request: XiorRequestConfig) => Promise<XiorResponse>,
instance?: XiorInstance
) => (request: XiorRequestConfig) => Promise<XiorResponse<any>>;
4 changes: 2 additions & 2 deletions src/xior.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export class xior {
static create(options?: XiorRequestConfig): XiorInstance {
return new xior(options);
}
static VERSION = '0.3.0';
static VERSION = '0.3.1';

config?: XiorRequestConfig;
defaults: XiorInterceptorRequestConfig;
Expand Down Expand Up @@ -176,7 +176,7 @@ export class xior {

let finalPlugin = this.fetch.bind(this);
this.P.forEach((plugin) => {
finalPlugin = plugin(finalPlugin);
finalPlugin = plugin(finalPlugin, this);
});
return finalPlugin<T>(requestConfig);
}
Expand Down
53 changes: 53 additions & 0 deletions tests/src/tests/plugins/error-retry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,57 @@ describe('xior error retry plugin tests', () => {
assert.strictEqual(error?.response?.data.errorCount, 2);
assert.strictEqual(error?.response?.data.count, 3);
});

it("should use the latest request inteceptor's request config when error retry", async () => {
await xior.get('/reset-error', { baseURL });

const instance = xior.create({ baseURL });

let S = '';
let errorCount = 0;

instance.interceptors.request.use((config) => {
if (S) {
config.headers['S'] = S;
}
return config;
});

instance.interceptors.response.use(
(config) => {
return config;
},
() => {
errorCount++;
if (!S) {
S = 'S123456';
}
}
);

instance.plugins.use(
xiorErrorRetryPlugin({
retryInterval: (count) => count * 500,
onRetry(config, error, count) {
console.log(`${config.method} ${config.url} retry ${count} times`);
},
})
);
let error: XiorError | undefined = undefined;
let msg = '';
try {
const { data } = await instance.post('/retry-error', { count: 3 }, {
retryTimes: 3,
enableRetry: true,
} as any);
msg = data.msg;
} catch (e) {
if (isXiorError(e)) {
error = e as XiorError;
}
}
assert.strictEqual(errorCount, 1);
assert.strictEqual(msg, 'ok');
assert.strictEqual(typeof error === 'undefined', true);
});
});
3 changes: 3 additions & 0 deletions tests/src/tests/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ export async function startServer(port: number) {
res.end('ok');
});
app.post('/retry-error', (req, res) => {
if (req.headers['s'] || req.headers['S']) {
return res.status(200).send({ msg: 'ok' });
}
if (errorCount < +(req.body.count || 2)) {
errorCount += 1;
return res.status(400).send({ errorCount: errorCount - 1, count: +(req.body.count || '2') });
Expand Down

0 comments on commit dd9c308

Please sign in to comment.