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

fetch-wrapper

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fetch-wrapper

Wrapper around isomorphic-fetch to retry the request if there is an error

  • 1.0.2
  • latest
  • Source
  • npm
  • Socket score

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

fetch-wrapper

Wrapper around isomorphic-fetch for resending fetch request if there is an error.

NPM

Getting Started

Installation

$ npm i fetch-wrapper --save

To run the tests first clone the repo:

$ git clone https://github.com/nikhilaravi/fetch-wrapper.git

Run the tests:

$ npm test

Request wrapper

The sendRequest function retries the fetch request if there is an error.

ParamDefaultTypeDescription
options{}objectREQUIRED: Object specifying the request, onSuccess and onError functions. See below
retryIntervals[1000]array of numbersOPTIONAL: Time intervals at which to retry the fetch request
attempt0numberDo not need to specify

options object

| Key | Type | Description | | :------------ |:---------------:| :---------------:| :-----| | request |function | function returns a fetch request. Can be created using the create-request helpers (see create-request.js) | | responseType | enum (text,json) | Format of the response. Used to parse the response body using either 'response.text' or 'response.json' methods | | onSuccess(response) |function | function to be called with the response when the fetch request returns successfully | | onError(response) |function | function to be called when there is an error in the fetch request |

options.onError will only be called if there is an:

  • error in the fetch request
  • error in options.onSuccess function (e.g. redux error)

The error object passed to onError is of the form

{
  status: '', //either a status code or 'error'
  message: ''
}

options.onSuccess will be called on

  • successful requests
  • network errors e.g. 404/500

The response passed to onSuccess is either the response data (json/text) or in the case of a network/server/parsing error, an error object of the form

{
  status: '', //'error'
  message: '' // e.g. 'Invalid Response Type' or 'No response body'
}

Request Creator Helpers

Helper functions that return a function that send a fetch request The promise returned by fetch is then resolved inside the sendRequest function.

Options available for sending get, post and put requests with and without authentication.

The parameters for each helper are outline below in the order they need to be specified.

getReq

ParamDefaultTypeDescription
urlundefinedstringurl of the request
header{}objectOptional header options

postReq

ParamDefaultTypeDescription
urlundefinedstringurl of the request
datanullobjectrequest body which will be stringified
header{}objectOptional header options

putReq

ParamDefaultTypeDescription
urlundefinedstringurl of the request
datanullobjectrequest body which will be stringified
header{}objectOptional header options

getAuthReq

ParamDefaultTypeDescription
urlundefinedstringurl of the request
tokenundefinedstringAuthentication token which will be set to the 'Authorization' key in the request header object
header{}objectOptional further header options

postAuthReq

ParamDefaultTypeDescription
urlundefinedstringurl of the request
datanullobjectrequest body which will be stringified
tokenundefinedstringAuthentication token which will be set to the 'Authorization' key in the request header object
header{}objectOptional further header options

putAuthReq

ParamDefaultTypeDescription
urlundefinedstringurl of the request
datanullobjectrequest body which will be stringified
tokenundefinedstringAuthentication token which will be set to the 'Authorization' key in the request header object
header{}objectOptional further header options

Example usage

import { postReq, sendRequest } from 'fetch-wrapper'

sendRequest({
  request: postReq('http://localhost:9009/login', {name: 'name'}), //this should be a function that returns a fetch request
  responseType: 'json'
  onSuccess: json => { //on success code here },
  onError: error => { //on error code here }
})

Credits

Collaborators: @besartshyti

Keywords

FAQs

Package last updated on 22 Jan 2016

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