Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@akiyamka/extended-fetch

Package Overview
Dependencies
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@akiyamka/extended-fetch

Tiny window.fetch JavaScript implementation over XMLHttpRequest with additional features

  • 0.0.9
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
18
decreased by-30.77%
Maintainers
1
Weekly downloads
 
Created
Source

extended-fetch

NPM Version NPM Type Definitions spring-easing's badge Module type: ESM Module type: CJS

This library allows you to cath Timeout Error without enforcing a time restriction

🧹 No dependencies
🤏 Tiny size
🧩 Does not patching existing fetch
🔀 In most cases can be used as drop in replacement for fetch

⚠️ It's not a fetch polyfill. It uses Request and Response objects from fetch implementation

Installation

npm install @akiyamka/extended-fetch

Usage

Have fetch like api, but with few additional features:

Catch timeout error

Fetch does not allow the user to know if his request was failed due to a 504 error.
Instead it throws common TypeError: Failed to fetch
But extended-fetch throw 'Timeout Error' error for that case

import { fetch, isTimeoutError } from '@akiyamka/extended-fetch'

fetch('/users', {
  method: 'POST',
  body: JSON.stringify({ foo: 'bar' }),
}).catch((error) => {
  // Allow identify timeout error
  console.assert(error.message, 'Timeout Error')
  console.assert(isTimeoutError(error), true)
})

Subscribe to upload and download events with progress:

import { fetch } from 'extended-fetch'

fetch(
  '/users',
  // Fetch configuration
  {
    method: 'POST',
    body: JSON.stringify({ foo: 'bar' }),
  },
  // Additional settings
  {
    onUploadProgress: (event) => {
       console.log(`Uploaded: ${event.progress}% (${event.bytes} bytes)`)
    },
    onDownloadProgress: (event) => {
       console.log(`Downloaded: ${event.progress}% (${event.bytes} bytes)`)
    },
  }
)

Catch Abort error

The library has a typed helper for Abort error detection

import { fetch, isAbortError } from 'extended-fetch'

const abortController = new AbortController()
abortController.abort()
try {
  const reference = await fetch(srv.readyCheck(), {
    signal: abortController.signal,
  })
} catch (err) {
  if (isAbortError(e)) {
    // request was aborted
  }
}

Motivation

Fetch has a pretty good api but doesn't cover some of the frequent occurrences of what an XHR query can give us

Fetch and timeout error

Currently, there is no way to determine that the reason the request failed is due to the Timeout Error using the fetch API, but sometimes it needed, for example, for meaningful UI reaction.

The most popular workaround for this today is to set a forced limit on the client side, which will only work if it less than the existing limit outside, and it will also break functionality in situations where the limit has been raised above the standard limit

Fetch and uploading progress

Using fetch we can get download progress information using readable stream(if supported), but we still don't have a way to get upload progress using fetch api.

Let's combine fetch api and XHR capabilities

I wrapped XHR in a fetch api (taking some code from the fetch polyfill) and added the missing functionality available from the XHR api

Keywords

FAQs

Package last updated on 10 Jun 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc