@fatcherjs/middleware-aborter
v3.0.0
Published
<div align="center"> <a href="https://codecov.io/github/fatcherjs/middleware-aborter" > <img src="https://codecov.io/github/fatcherjs/middleware-aborter/graph/badge.svg?token=BEJA311FY2"/> </a> <a href="https://www.jsdelivr.com/package/npm/@fat
Readme
@fatcherjs/middleware-aborter
Tips
If you want to abort request manually, you should use AbortController and pass the signal to fatcher
import { fatcher } from 'fatcher';
const abortController = new AbortController();
fatcher('your-url', {
signal: abortController.signal,
}).catch(error => {
if (error instanceof DOMException && error.name === 'AbortError') {
// aborted.
return;
}
// other
});
abortController.abort();Install
NPM
>$ npm install @fatcherjs/middleware-aborterCDN
<script src="https://cdn.jsdelivr.net/npm/fatcher/dist/fatcher.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@fatcherjs/middleware-aborter/dist/index.min.js"></script>
<script>
Fatcher.fatcher('xxx', {
middlewares: [FatcherMiddlewareAborter.timeout],
onTimeout: () => {},
timeout: 30000,
})
.then(response => {
console.log(response);
})
.catch(error => {
if (FatcherMiddlewareAborter.isTimeoutError(error)) {
// do somethings
return;
}
// do other thing
});
</script>Usage
Timeout
Types
declare module 'fatcher' {
interface FatcherOptions {
timeout?: number; // default 60 * 1000 (60s)
onTimeout?: () => void;
}
}Basic
import { fatcher } from 'fatcher';
import { timeout } from '@fatcherjs/middleware-aborter';
const response = await fatcher('xxx', {
middlewares: [timeout],
timeout: 30 * 1000,
onTimeout: () => {
console.log('timeout!');
},
});isTimeoutError
import { fatcher } from 'fatcher';
import { isTimeoutError, timeout } from '@fatcherjs/middleware-aborter';
fatcher('https://foo.bar', {
timeout: 30 * 1000,
middlewares: [timeout],
}).catch(error => {
if (isTimeoutError(error)) {
// do something..
return;
}
// other error
});