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

@dnemoga/fetcher

Package Overview
Dependencies
Maintainers
1
Versions
14
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@dnemoga/fetcher

A minimalistic library built around the native Fetch API with zero dependencies.

  • 1.3.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

@dnemoga/fetcher

A minimalistic library built around the native Fetch API with zero dependencies.

Getting Started

Installation

npm install @dnemoga/fetcher

Importing

import { Fetcher } from '@dnemoga/fetcher';

Creating Instance

const fetcher = new Fetcher({ /* Fetcher Options */ });
Fetcher Options

These options apply to every request outcoming from the current instance.

Making Request

fetcher.get('/resource', { /* Request Options */ })
  .then(console.log, console.error);

| Note: Supported methods are get, head, post, put, patch, and delete.

Request Options
  • data
    Any body that you want to add to your request. Note that a request using the GET or HEAD method cannot have a body.

  • params
    Any search parameters you want to add to your request, contained within an object literal with string values.

  • headers
    Any headers you want to add to your request, contained within an object literal with string values. Note that some names are forbidden.

  • integrity
    Contains the subresource integrity value of the request.

  • keepalive
    The keepalive option can be used to allow the request to outlive the page. Fetch with the keepalive flag is a replacement for the Navigator.sendBeacon() API.

  • signal
    An AbortSignal object instance; allows you to communicate with a fetch request and abort it if required via an AbortController.

Next Steps

Interceptors

onRequest.use()
const customHeaders = async (request) => {
  request.headers.set('X-Foo', 'Foo');
  request.headers.set('X-Bar', 'Bar');

  return request;
};

fetcher.onRequest.use(customHeaders);
onRequest.eject()
fetcher.onRequest.eject(customHeaders);
onResponse.use()
const errorHandler = async (response) => {
  if (!response.ok) {
    throw new Error(response.statusText);
  }

  return response;
};

fetcher.onResponse.use(errorHandler);
onResponse.eject()
fetcher.onResponse.eject(errorHandler);

Request Timeout

fetcher.get('/resource', {
  signal: AbortSignal.timeout(30000)
});

Keywords

FAQs

Package last updated on 17 Oct 2023

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