3.0.0-alpha-10 • Published 8 months ago

@fatcherjs/middleware-aborter v3.0.0-alpha-10

Weekly downloads
-
License
-
Repository
github
Last release
8 months ago

@fatcherjs/middleware-aborter

install 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

3.0.0-alpha-10

8 months ago

3.0.0-alpha-6

8 months ago

3.0.0-alpha-5

8 months ago

3.0.0-alpha-8

8 months ago

3.0.0-alpha-7

8 months ago

3.0.0-alpha-9

8 months ago

3.0.0-alpha-4

8 months ago

1.7.2

3 years ago

1.8.0

3 years ago

2.0.0

3 years ago

1.7.1

3 years ago

1.7.0

3 years ago

1.6.1

3 years ago

1.6.0

3 years ago

1.5.0

3 years ago

1.4.1

3 years ago

1.4.0

3 years ago

1.3.0

3 years ago

1.2.0

3 years ago

1.0.0

3 years ago