GitXplorerGitXplorer
n

fetch-wrapper

public
9 stars
0 forks
0 issues

Commits

List of commits on branch master.
Unverified
70711b95c285a426b8b2a447c3daac143a4b3f1f

Updates entry file in package.json

nnikhilaravi committed 9 years ago
Unverified
77b9779e43178c5711dcf8a49faea1f37f4e2fec

Merges with remote master

nnikhilaravi committed 9 years ago
Unverified
c31cb920d7c0eb831af988056d679eeaa35e6a48

Adds npm ignore and compiles code to es5

nnikhilaravi committed 9 years ago
Unverified
20e2132a8f637f5b453ff76aadcda62576c2cc06

Update README.md

nnikhilaravi committed 9 years ago
Unverified
dfaf867dd9b1a14e34ef2a680915aa6dfc510f62

Updates readme

nnikhilaravi committed 9 years ago
Unverified
0b82cb1966993840f8203057339cc767c50ebb75

Updates reamde

nnikhilaravi committed 9 years ago

README

The README file for this repository.

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.

Param Default Type Description
options {} object REQUIRED: Object specifying the request, onSuccess and onError functions. See below
retryIntervals [1000] array of numbers OPTIONAL: Time intervals at which to retry the fetch request
attempt 0 number Do 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

Param Default Type Description
url undefined string url of the request
header {} object Optional header options

postReq

Param Default Type Description
url undefined string url of the request
data null object request body which will be stringified
header {} object Optional header options

putReq

Param Default Type Description
url undefined string url of the request
data null object request body which will be stringified
header {} object Optional header options

getAuthReq

Param Default Type Description
url undefined string url of the request
token undefined string Authentication token which will be set to the 'Authorization' key in the request header object
header {} object Optional further header options

postAuthReq

Param Default Type Description
url undefined string url of the request
data null object request body which will be stringified
token undefined string Authentication token which will be set to the 'Authorization' key in the request header object
header {} object Optional further header options

putAuthReq

Param Default Type Description
url undefined string url of the request
data null object request body which will be stringified
token undefined string Authentication token which will be set to the 'Authorization' key in the request header object
header {} object Optional 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