2.0.1 • Published 1 year ago

auto-abortable-fetch v2.0.1

Weekly downloads
-
License
ISC
Repository
-
Last release
1 year ago

auto-abort-fetch.js

Pre-requisites

You can use it on Node 17.5.0 (or later) or a browser version that supports fetch.

Usage

import autoAbortableFetch from 'auto-abortable-fetch';
let [res, err] = await autoAbortableFetch({
    url: 'https://xxx',
    method: 'get',
    headers: {},
    timeout: 5000,
    querys: {},
    data: {},
    responseType: 'json',
});
let { config, ok, url, data, status, statusText, request } = res;
let { config, name, message, code } = err;

// If you want to manually control the request abort, you can pass AbortControl.signal

import autoAbortableFetch from 'auto-abortable-fetch';
let controller = autoAbortableFetch.CreateAbortSignal();
let signal = controller.signal;
let [res, err] = await autoAbortableFetch({
    url: 'https://xxx',
    signal,
});

// cancel request
controller.cancel();

TypeScript usage

interface ResponseData {
    message: string;
}

let [res, err] = await autoAbortableFetch<ResponseData>({ url: 'https://xxx' });
console.log(res.data.message);

Parameter Descriptions

requestInfo

ParametertypeDescription
urlstringThe URL address of the request.
headers{ key: string: string; }An object containing request headers.
methodstringThe HTTP method to use for the request.
timeoutnumberThe timeout for the request, in milliseconds.
dataanyThe data to send with the request.
responseType"arraybuffer" | "blob" | "document" | "json" | "text"The expected response type for the request.
signalAbortSignalThe signal used to cancel the request.
withCredentialsbooleanWhether to include credentials in the request.
validateStatus(status: number) => voidA function that determines whether the response status code is valid.
onUploadProgress(progressEvent: ProgressEvent) => voidA function to handle progress events during upload.
onDownloadProgress(progressEvent: ProgressEvent) => voidA function to handle progress events during download.
2.0.1

1 year ago

2.0.0

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago