-
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.
- Loading branch information
Showing
3 changed files
with
76 additions
and
9 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,4 +1,69 @@ | ||
// TODO | ||
export default function xiorErrorRetryPlugin(options?: object) { | ||
// | ||
import { sleep } from './utils'; | ||
import { XiorPlugin } from '../types'; | ||
import { XiorError } from '../utils'; | ||
|
||
export type ErrorRetryOptions = { | ||
/** retry times, default: 0 */ | ||
retryTimes?: number; | ||
/** | ||
* Retry after milliseconds, default: 3000 | ||
* after first time error retry have interval | ||
*/ | ||
retryInterval?: number; | ||
/** | ||
* default: true, | ||
* it's useful because we don't want retry when the error because of token expired | ||
*/ | ||
shouldRetryOnError?: (error: XiorError) => boolean; | ||
}; | ||
|
||
/** @ts-ignore */ | ||
declare module 'xior' { | ||
interface XiorRequestConfig extends Omit<ErrorRetryOptions, 'shouldRetryOnError'> {} | ||
} | ||
|
||
export default function xiorErrorRetryPlugin(options: ErrorRetryOptions = {}): XiorPlugin { | ||
const { | ||
retryTimes: _retryTimes, | ||
retryInterval: _retryInterval, | ||
shouldRetryOnError, | ||
} = options || { | ||
retryTimes: 0, | ||
retryInterval: 3000, | ||
}; | ||
|
||
return function (adapter) { | ||
return async (config) => { | ||
const { retryTimes = _retryTimes, retryInterval = _retryInterval } = | ||
config as ErrorRetryOptions; | ||
|
||
let timeUp = false; | ||
let count = 0; | ||
|
||
async function handleRequest() { | ||
try { | ||
return await adapter(config); | ||
} catch (error) { | ||
if (error instanceof XiorError) { | ||
const shouldRetry = shouldRetryOnError ? shouldRetryOnError(error) : true; | ||
timeUp = retryTimes === count; | ||
if (timeUp || !shouldRetry) { | ||
throw error; | ||
} | ||
|
||
if (retryInterval && retryInterval > 0 && count > 0) { | ||
await sleep(retryInterval); | ||
} | ||
|
||
count++; | ||
return handleRequest(); | ||
} | ||
|
||
throw error; | ||
} | ||
} | ||
|
||
return handleRequest(); | ||
}; | ||
}; | ||
} |
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 |
---|---|---|
@@ -1,4 +1,10 @@ | ||
import { XiorPlugin } from '../types'; | ||
|
||
// TODO | ||
export default function xiorRepeatRequestsFilterPlugin(options?: object) { | ||
// | ||
export default function xiorRepeatRequestFilterPlugin(options = {}): XiorPlugin { | ||
return function (adapter) { | ||
return async (config) => { | ||
return adapter(config); | ||
}; | ||
}; | ||
} |