Skip to content

fatcherjs/middleware-aborter

Repository files navigation

@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-aborter

CDN

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

License

MIT

About

@fatcherjs/middleware-aborter

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages