util-request
util-request is a fetch wrapper.
Master
Dev
Table of Contents
Content
request.js (fetch wrapper)
Usage:
import request from 'request-utils'
Content:
import 'whatwg-fetch'; /** * Parses the JSON returned by a network request * * @param * * @return */ { if responsestatus === 204 || responsestatus === 205 return null; return response;} /** * Checks if a network request came back fine, and throws an error if not * * @param * * @return */ { if responsestatus >= 200 && responsestatus < 300 return response; const error = responsestatusText; errorresponse = response; throw error;} /** * Requests a URL, returning a promise * * @param * @param * * @return */ { return ;}
utils.js
Usage:
import toQueryString getParameter getCookie from 'util-request';
Content:
/** * Transform object to query string * @param params * @returns */ { const esc = encodeURIComponent; return Object // eslint-disable-line prefer-template ;} /** * Return a parameter from a url or by default window.location.href * @param name * @param url * @returns */ { if !customUrl customUrl = windowlocationhref; // eslint-disable-line no-param-reassign name = name; // eslint-disable-line no-param-reassign const regex = `[?&](=([^&#]*)|&|#|$)`; const results = regex; if !results return null; if !results2 return ''; return ;} /** * return a cookie from it's name * @param name * @returns */ { const value = `; `; const parts = value; return partslength === 2 ? parts : null;}
Reminders
⚠️ When using this plugin, you must import in the first line of your application javascript entry file babel-polyfill
: ⚠️
import "babel-polyfill";
To enable ES features in older browsers, you MUST include in the package.json
"browserslist": ["ie >= 9", "last 2 versions"]
// or
"browserslist": ["ie >= 10", "last 2 versions"]
Quick start
Installation
npm install util-request --save-dev