fetchagent
Advanced tools
Comparing version 1.1.3 to 2.0.0
2.0.0 / 2019-02-02 | ||
================== | ||
* rewrite in ES6 | ||
1.1.3 / 2018-03-24 | ||
@@ -3,0 +8,0 @@ ================== |
@@ -5,6 +5,4 @@ /* global Headers */ | ||
['get', 'put', 'post', 'delete'].forEach(function(method) { | ||
fetchagent[method] = function(url) { | ||
return fetchagent(method.toUpperCase(), url); | ||
}; | ||
['get', 'put', 'post', 'delete'].forEach(method => { | ||
fetchagent[method] = url => fetchagent(method.toUpperCase(), url); | ||
}); | ||
@@ -15,5 +13,3 @@ | ||
function setAll(destination, source) { | ||
Object.keys(source).forEach(function(p) { | ||
destination.set(p, source[p]); | ||
}); | ||
Object.keys(source).forEach(p => destination.set(p, source[p])); | ||
} | ||
@@ -31,5 +27,5 @@ | ||
} | ||
var qs = Object | ||
const qs = Object | ||
.keys(query) | ||
.map(function(name) { return name + '=' + encode(query[name]); }) | ||
.map(name => `${name}=${encode(query[name])}`) | ||
.join('&'); | ||
@@ -39,36 +35,35 @@ if (!qs) { | ||
} | ||
return prefix + '?' + qs; | ||
return `${prefix}?${qs}`; | ||
} | ||
function defaultContentParser(contentType) { | ||
return contentType && contentType.indexOf('json') !== -1 | ||
? 'json' | ||
: 'text'; | ||
return contentType && contentType.includes('json') ? 'json': 'text'; | ||
} | ||
function fetchagent(method, url) { | ||
var req = { | ||
url: url, | ||
const req = { | ||
url, | ||
query: undefined | ||
}; | ||
var init = { | ||
method: method, | ||
const init = { | ||
method, | ||
redirect: 'manual', | ||
credentials: 'same-origin' | ||
}; | ||
var self = { | ||
end: end, | ||
json: json, | ||
parser: parser, | ||
query: query, | ||
redirect: redirect, | ||
send: send, | ||
set: set, | ||
text: text | ||
const self = { | ||
end, | ||
json, | ||
parser, | ||
query, | ||
redirect, | ||
send, | ||
set, | ||
text | ||
}; | ||
var error; | ||
var contentParser = defaultContentParser; | ||
let error; | ||
let contentParser = defaultContentParser; | ||
function end(fn) { | ||
var fetched = fetch(formatUrl(req.url, req.query), init); | ||
const fetched = fetch(formatUrl(req.url, req.query), init); | ||
@@ -80,3 +75,3 @@ if (!fn) { | ||
fetched | ||
.then(function(res) { | ||
.then(res => { | ||
if (!res.ok) { | ||
@@ -88,3 +83,3 @@ error = { | ||
} | ||
var parser = contentParser(res.headers.get('Content-Type')); | ||
const parser = contentParser(res.headers.get('Content-Type')); | ||
if (parser) { | ||
@@ -100,4 +95,4 @@ return res[parser](); | ||
.then( | ||
function(body) { return fn(error, body); }, | ||
function(e) { | ||
body => fn(error, body), | ||
e => { | ||
error = error || {}; | ||
@@ -111,7 +106,7 @@ error.error = e; | ||
function json() { | ||
return end().then(function(res) { return res.json(); }); | ||
return end().then(res => res.json()); | ||
} | ||
function text() { | ||
return end().then(function(res) { return res.text(); }); | ||
return end().then(res => res.text()); | ||
} | ||
@@ -118,0 +113,0 @@ |
{ | ||
"name": "fetchagent", | ||
"version": "1.1.3", | ||
"version": "2.0.0", | ||
"description": "Flex API for global fetch", | ||
@@ -22,9 +22,9 @@ "author": { | ||
"devDependencies": { | ||
"isomorphic-fetch": "^2.2.1", | ||
"jsdom": "^11.6.2", | ||
"jsdom-global": "^3.0.2", | ||
"jshint": "^2.9.4", | ||
"mocha": "^3.2.0", | ||
"replay": "^2.1.2", | ||
"should": "^11.1.2" | ||
"isomorphic-fetch": "~2", | ||
"jsdom": "~13", | ||
"jsdom-global": "~3", | ||
"jshint": "~2", | ||
"mocha": "~5", | ||
"@pirxpilot/replay": "~2", | ||
"should": "~13" | ||
}, | ||
@@ -31,0 +31,0 @@ "scripts": { |
[![NPM version][npm-image]][npm-url] | ||
[![Build Status][travis-image]][travis-url] | ||
[![Dependency Status][gemnasium-image]][gemnasium-url] | ||
[![Dependency Status][deps-image]][deps-url] | ||
[![Dev Dependency Status][deps-dev-image]][deps-dev-url] | ||
@@ -69,3 +70,6 @@ # fetchagent | ||
[gemnasium-image]: https://img.shields.io/gemnasium/pirxpilot/fetchagent.svg | ||
[gemnasium-url]: https://gemnasium.com/pirxpilot/fetchagent | ||
[deps-image]: https://img.shields.io/david/pirxpilot/fetchagent.svg | ||
[deps-url]: https://david-dm.org/pirxpilot/fetchagent | ||
[deps-dev-image]: https://img.shields.io/david/dev/pirxpilot/fetchagent.svg | ||
[deps-dev-url]: https://david-dm.org/pirxpilot/fetchagent?type=dev |
6122
75
126