Skip to content
This repository has been archived by the owner on Feb 12, 2019. It is now read-only.

Latest commit

 

History

History
137 lines (96 loc) · 6.8 KB

README.md

File metadata and controls

137 lines (96 loc) · 6.8 KB

http-francis

[!!!] This repository has been deprecated, we suggest to use https://github.com/docomodigital/js-fetcher instead

Build Status Coverage Status npm version Bower version GitHub version

  • retry
  • attempts
  • promise based

Commands

  • npm run build
  • npm run test
  • npm run test:watch // run tests while typing
  • npm run documentation // generate docs folder with .html, append docs to the readme.md

Release a new version

  • npm version patch|minor|major This will generate the docs/ and dist/

API

Francis#Http

constructor

an http class to make requests over the net with retry and interval between them

Parameters

  • requestParams object object where you can specify the options of the request
    • requestParams.type [string] the type of the request: possible values POST, GET, PUT, DELETE and JSONP (optional, default GET)
    • requestParams.url string the url to request for
    • requestParams.headers [object] the headers object (optional, default {})
    • requestParams.timeout [string] the timeout of the request in ms (optional, default 2000)
    • requestParams.attempt [string] attempts. if it fails for some reason retry (optional, default 1)
    • requestParams.retryAfter [number] the interval between requests in ms: 500 for example (optional, default 0)
    • requestParams.async [boolean] the request could be synchrounous, default async (optional, default true)
  • options
  • callback [function] onend callback (optional, default function(){})

Examples

<pre>
npm install --save http-francis

import { Http, getImageRaw, JSONPRequest } from 'http-francis';
OR
var Http = require("http-francis").Http;
OR
<script src='http-francis.js'></script> 
// in global case window['http-francis'].Http
 var getTask = new Http({
      method: "GET",
      url: "https://someimageurl/image.png",
      responseType: "blob",
      mimeType: "image/png",
      onProgress:(percentage)=>{ 
          // there must be Content-Length header in the response to get the right percentage
          // otherwise percentage is a NaN
      }
  });

  getTask.promise.then((response) => { 
      var imgTag = document.createElement("img");
      imgTag.src = response[0];
      document.body.appendChild(imgTag);       
  });
</pre>

parseResponse

parseResponse

Parameters

Returns array [responseData, statusCode, xhr]

getImageRaw

getImageRaw from a specific url

Parameters

  • options Object the options object
    • options.url String http or whatever
    • options.responseType [String] possible values arraybuffer|blob (optional, default "blob")
    • options.mimeType [String] possible values "image/png"|"image/jpeg" used only if "blob" is set as responseType (optional, default "image/jpeg")
  • _onProgress [Function] (optional, default function(){})

Returns Promise<(Blob | ArrayBuffer | Error)>

JSONPRequest

Make a jsonp request, remember only GET The function create a tag script and append a callback param in querystring. The promise will be reject after 3s if the url fail to respond

Parameters

  • url String the url with querystring but without &callback at the end or &function
  • timeout [Number](default 3000) ms range for the response

Examples

<pre>
request = new JSONPRequest("http://www.someapi.com/asd?somequery=1");
request.then((data) => {});
</pre>

Returns Promise<(Object | String)>

getJSON

getJSON

Parameters

Returns Promise the string error is the statuscode

Francis

an http module to make requests over the net with retry and interval between them