@fatcherjs/middleware-aborter
TypeScript icon, indicating that this package has built-in type declarations

3.0.0-alpha-10 • Public • Published

@fatcherjs/middleware-aborter

npm package install size Size

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();

License

MIT

Readme

Keywords

none

Package Sidebar

Install

npm i @fatcherjs/middleware-aborter

Weekly Downloads

13

Version

3.0.0-alpha-10

License

none

Unpacked Size

8.31 kB

Total Files

7

Last publish

Collaborators

  • fanhaoyuan