@fatcherjs/middleware-aborter
v3.0.0-alpha-10
Published
<a href="https://npmjs.com/package/@fatcherjs/middleware-aborter"><img src="https://img.shields.io/npm/v/@fatcherjs/middleware-aborter.svg" alt="npm package"></a> [![install size](https://packagephobia.com/badge?p=@fatcherjs/middleware-aborter)](https://p
Downloads
547
Readme
@fatcherjs/middleware-aborter
Install
NPM
>$ npm install @fatcherjs/middleware-aborter
CDN
<script src="https://cdn.jsdelivr.net/npm/@fatcherjs/middleware-aborter/dist/index.min.js"></script>
Provider
FatcherRequest
abort
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();
},
],
});
Usage
Basic
import { fatcher } from 'fatcher';
import { aborter } from '@fatcherjs/middleware-aborter';
fatcher('https://foo.bar', {
onAbort: () => console.log('aborted'),
middlewares: [aborter()],
});
Timeout
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 */],
});
User Cancelable
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();
isAbortError
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();