Package detail

@fatcherjs/middleware-aborter

fanhaoyuan3213.0.0-alpha-11

npm package [install size](https://p

readme

@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

changelog

v3.0.0

  • 🚀 Support Named Middleware and Functional Middleware
  • ❌ Remove Fatcher Error
  • ❌ Remove exception middleware

v3.0.0-beta-2

  • 🚀 add fatcher-middleware-parameter to deal with query string (#266)

v3.0.0-beta-1

  • 🐛 Pass fetch options to fetch api (#264)

v3.0.0-beta

  • 🔧 Refactor fatcher request core, same behavior with fetch.
  • 🚀 Built-in fatcher-middleware-aborter
  • 🚀 Built-in fatcher-middleware-json
  • ❌ Deprecated createScopedRequest
  • 📚 Archived @fatcherjs/middleware-json
  • 📚 Archived @fatcherjs/middleware-aborter
  • 📚 Archived @fatcherjs/middleware-cache
  • 📚 Archived @fatcherjs/middleware-form-data
  • 📚 Archived @fatcherjs/middleware-mock
  • 📚 Archived @fatcherjs/middleware-progress
  • 📚 Archived @fatcherjs/utils-shared

v2.1.0

  • 🚀 Supports string inputs.

v2.0.0

  • ❌ Deprecated setDefaultOptions
  • ❌ Deprecated mergeOptions
  • ❌ Deprecated isAbortError
  • ❌ Deprecated chunkStreamReader
  • 🚀 Add combine for merging options and context
  • 🚀 Add defineMiddleware helper function to declare middleware
  • 🚀 RequestMethod allows lower method
  • 🚀 Context with required context.
  • 🔧 Refactor object middleware to functions

v1.8.0

  • 🚀 middleware can provide context before fetching

v1.7.1

  • 🐛 Filter undefined value in payload when transform to querystring

v1.7.0

  • 🚀 Supports async middlewares. (#183)

v1.6.1

  • 🐛 fix parse error when query string had absolute url (#180)
  • 🐛 fix parse exception when base url with an absolute request url (#181)

v1.6.0

  • 🚀 add options validateCode for passing custom response status code. (#174)
  • ⚡️ normalizeURL downgrade to use match. (#179)

v1.5.0

  • 🚀 readStreamByChunk supports async callback (#167)
  • 🚀 request url supports relative path like ../ and ./ (#170)

v1.4.1

  • 🔧 Remove some overt options (#161)

v1.4.0

  • ❌ Deprecate isAbortError, move it to @fanhaoyuan/middleware-aborter. (#157)

v1.3.0

  • 🚀 Middleware add presets options, can set preset middlewares before using this middleware. (#152)

v1.2.0

  • 🚀 Use Headers class for sending http headers. (#129)
  • 🚀 Add params into request options (#128)
  • 🚀 Change context in middleware.use to readonly (#127)
  • 🐛 When using createScopedRequest, headers will cover another request headers (#140)
  • 🐛 Should not return request headers but response headers (#137)
  • 🔧 Using requestHeaders instead of headers in context (#142)

v1.1.3

  • 🐛 ResponseResult headers should be response headers (#126)
  • 🧪 Upgrade node version to 18 and setup units tests (#124)

v1.1.2

  • 🐛 It is not exported isAbortError and readStreamByChunk (#120)
  • 🔧 Rename chunkStreamReader to readStreamByChunk (#119)
  • 🐛 The global options overriding headers rather than merging headers (#118)

v1.1.1

  • 🐛 Using body instead of inline url params with application/x-www-form-urlencoded (#113)
  • ⚡️ Headers using Record<string, string> to send request (#109)

v1.1.0

  • 🔧 Move @fanhaoyuan/utils-shared to utils
  • 🚀 Add default request init for same behavior during fetch in different browsers

v1.0.0

  • 🚀 Add payload-consumer

v1.0.0-beta.1

v0.3.2

  • 🐛 fix normalize error when baseURL is not '/' #62

v0.3.1

  • 🐛 fix normalize url error
  • 🐛 fix merge options error

v0.3.0

  • 🚀 add FatcherError
  • 🚀 add isFatcherError for custom middleware
  • 📦 export clone helper function

v0.2.0

  • 🚀 Add Timeout aborter.
  • 🚀 Throw AbortError during aborting fetch.
  • 🔧 Add compatibility with esModule

v0.1.1

  • 🐞 Fix can not abort fetch when request pending.

v0.1.0

  • 🚀 Basic Fetch.
  • 🚀 Cancelable.
  • 🚀 Add custom middlewares.
  • 🚀 Auto Transform Request Payload.
  • 🚀 Auto Transform Response data
  • 🚀 Immutable Context
  • 🚀 Download Progress