1.0.2 • Published 8 years ago

fetch-wrapper v1.0.2

Weekly downloads
7
License
ISC
Repository
github
Last release
8 years ago

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

KeyTypeDescription
requestfunctionfunction returns a fetch request. Can be created using the create-request helpers (see create-request.js)
responseTypeenum (text,json)Format of the response. Used to parse the response body using either 'response.text' or 'response.json' methods
onSuccess(response)functionfunction to be called with the response when the fetch request returns successfully
onError(response)functionfunction 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