>$ npm install @fatcherjs/middleware-aborter
<script src="https://cdn.jsdelivr.net/npm/@fatcherjs/middleware-aborter/dist/index.min.js"></script>
declare module 'fatcher' {
interface FatcherRequest {
abort: (reason?: string) => void;
}
}
Middleware can get a abort
function after call aborter
;
fatcher('https://foo.bar', {
middlewares: [
aborter(),
(req, next) => {
console.log(typeof req.abort); // 'function'
return next();
},
],
});
import { fatcher } from 'fatcher';
import { aborter } from '@fatcherjs/middleware-aborter';
fatcher('https://foo.bar', {
onAbort: () => console.log('aborted'),
middlewares: [aborter()],
});
import { fatcher } from 'fatcher';
import { aborter, timeout } from '@fatcherjs/middleware-aborter';
fatcher('https://foo.bar', {
onAbort: () => console.log('aborted'),
timeout: 1000 * 10, // 10s
middlewares: [aborter(), timeout() /* must call after aborter */],
});
import { fatcher } from 'fatcher';
import { aborter } from '@fatcherjs/middleware-aborter';
const abortController = new AbortController();
fatcher('https://foo.bar', {
onAbort: () => console.log('aborted'),
abortController,
middlewares: [aborter()],
}).catch(error => {
// abort error
});
abortController.abort();
import { fatcher } from 'fatcher';
import { aborter, isAbortError } from '@fatcherjs/middleware-aborter';
const abortController = new AbortController();
fatcher('https://foo.bar', {
onAbort: () => console.log('aborted'),
abortController,
middlewares: [aborter()],
}).catch(error => {
if (isAbortError(error)) {
// do something..
return;
}
// other error
});
abortController.abort();