Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

http-client

Package Overview
Dependencies
Maintainers
1
Versions
25
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

http-client - npm Package Compare versions

Comparing version 4.1.1 to 4.2.0

index.js

19

CHANGES.md

@@ -0,2 +1,21 @@

## [v4.2.0]
> Sep 29, 2016
- Add `parse(parser)` middleware that puts the result of the parse in `response.body`
- Deprecated `parseJSON`, use `parse('json')` instead
- Deprecated `parseText`, use `parse('text')` instead
- Build everything into the package root
[v4.2.0]: https://github.com/mjackson/http-client/compare/v4.1.0...v4.2.0
## [v4.1.0]
> Jun 16, 2016
- Fixed `Content-Length` header for non-ASCII content bodies (see [#8])
[v4.1.0]: https://github.com/mjackson/http-client/compare/v4.0.1...v4.1.0
[#8]: https://github.com/mjackson/http-client/pull/8
## [v4.0.1]
> May 18, 2016

@@ -3,0 +22,0 @@ - Renamed `handleResponse` to `onResponse`

18

package.json
{
"name": "http-client",
"version": "4.1.1",
"version": "4.2.0",
"description": "Compose HTTP clients using JavaScript's fetch API",

@@ -8,10 +8,5 @@ "repository": "mjackson/http-client",

"license": "MIT",
"main": "lib",
"files": [
"lib",
"umd"
],
"scripts": {
"build": "node ./scripts/build.js",
"build-lib": "rimraf lib && babel ./modules -d lib --ignore '__tests__'",
"build-lib": "babel ./modules -d . --ignore '__tests__'",
"build-min": "webpack -p modules/index.js umd/http-client.min.js",

@@ -31,8 +26,8 @@ "build-umd": "webpack modules/index.js umd/http-client.js",

"babel-core": "^6.5.2",
"babel-eslint": "^6.0.0",
"babel-eslint": "^7.0.0",
"babel-loader": "^6.2.3",
"babel-preset-es2015": "^6.5.0",
"es6-promise": "^3.1.2",
"es6-promise": "^4.0.3",
"eslint": "^3.2.2",
"eslint-plugin-import": "^1.7.0",
"eslint-plugin-import": "^2.0.0",
"expect": "^1.14.0",

@@ -44,3 +39,3 @@ "gzip-size": "^3.0.0",

"karma-browserstack-launcher": "^1.0.1",
"karma-chrome-launcher": "^1.0.1",
"karma-chrome-launcher": "^2.0.0",
"karma-mocha": "^1.1.1",

@@ -53,5 +48,4 @@ "karma-mocha-reporter": "^2.0.4",

"readline-sync": "^1.4.1",
"rimraf": "^2.5.2",
"webpack": "^1.12.14"
}
}

@@ -21,3 +21,3 @@ # http-client [![Travis][build-badge]][build] [![npm package][npm-badge]][npm]

http-client requires you to bring your own [global `fetch`](https://developer.mozilla.org/en-US/docs/Web/API/GlobalFetch/fetch) function. [isomorphic-fetch](https://github.com/matthew-andrews/isomorphic-fetch) is a great polyfill.
http-client requires you to bring your own [global `fetch`](https://developer.mozilla.org/en-US/docs/Web/API/GlobalFetch/fetch) function (for convenience when using the top-level `createFetch` function). [isomorphic-fetch](https://github.com/matthew-andrews/isomorphic-fetch) is a great polyfill if you need to support environments that don't already have a global `fetch` function.

@@ -198,34 +198,26 @@ Then, use as you would anything else:

#### `parseJSON(propertyName = 'jsonData')`
#### `parse(parser, as = 'body')`
Reads the response body as JSON and puts it on `response.jsonData`.
Reads the response body as JSON and puts it on `response.body` (or whatever `as` is). `parser` must be the name of a valid [Body](https://developer.mozilla.org/en-US/docs/Web/API/Body) parsing method. The following parsers are available in [the spec](https://fetch.spec.whatwg.org/#body-mixin):
- `arrayBuffer`
- `blob`
- `formData`
- `json`
- `text`
```js
import { createFetch, parseJSON } from 'http-client'
import { createFetch, parse } from 'http-client'
const fetch = createFetch(
parseJSON()
parse('json')
)
fetch(input).then(response => {
console.log(response.jsonData)
console.log(response.body)
})
```
#### `parseText(propertyName = 'textString')`
Note: Some parsers may not be available when using a `fetch` polyfill. In particular if you're using `node-fetch`, you should be aware of [its limitations](https://github.com/bitinn/node-fetch/blob/master/LIMITS.md).
Reads the response body as text and puts it on `response.textString`.
```js
import { createFetch, parseText } from 'http-client'
const fetch = createFetch(
parseText()
)
fetch(input).then(response => {
console.log(response.textString)
})
```
#### `query(object)`

@@ -277,1 +269,25 @@

```
Stacks are also useful when you don't have a global `fetch` function, e.g. in node. In those cases, you can still use http-client middleware and supply your own `fetch` (we recommend [node-fetch](https://www.npmjs.com/package/node-fetch)) function directly, but make sure you "enhance" it first:
```js
const { enhanceFetch, createStack, header, base } = require('http-client')
// We need to "enhance" node-fetch so it knows how to
// handle responses correctly. Specifically, enhanceFetch
// gives a fetch function the ability to run response
// handlers registered with onResponse (which parseJSON,
// used below, uses behind the scenes).
const fetch = enhanceFetch(
require('node-fetch')
)
const stack = createStack(
header('X-Auth-Key', key),
header('X-Auth-Email', email),
base('https://api.cloudflare.com/client/v4'),
parseJSON()
)
stack(fetch, input, options)
```

@@ -62,3 +62,3 @@ (function webpackUniversalModuleDefinition(root, factory) {

});
exports.requestInfo = exports.parseJSON = exports.parseText = exports.handleResponse = exports.onResponse = exports.params = exports.json = exports.body = exports.query = exports.base = exports.accept = exports.auth = exports.header = exports.method = exports.init = exports.createFetch = exports.createStack = exports.fetch = exports.enhanceFetch = undefined;
exports.requestInfo = exports.parseText = exports.parseJSON = exports.parse = exports.handleResponse = exports.onResponse = exports.params = exports.json = exports.body = exports.query = exports.base = exports.accept = exports.auth = exports.header = exports.method = exports.init = exports.createFetch = exports.createStack = exports.fetch = exports.enhanceFetch = undefined;

@@ -73,3 +73,3 @@ var _queryString = __webpack_require__(1);

var globalFetch = fetch;
var global = (1, eval)('this');

@@ -105,3 +105,3 @@ var stringifyQuery = function stringifyQuery(query) {

var enhancedGlobalFetch = enhanceFetch(globalFetch);
var enhancedGlobalFetch = enhanceFetch(global.fetch);

@@ -117,2 +117,6 @@ exports.fetch = enhancedGlobalFetch;

* Creates a middleware "stack" function using all arguments.
* A "stack" is essentially a bunch of middleware composed into
* a single middleware function. Since all middleware share the
* same signature, stacks may further be combined to create more
* stacks with different characteristics.
*/

@@ -137,2 +141,13 @@ var createStack = exports.createStack = function createStack() {

* Creates a fetch function using all arguments as middleware.
* This function is a "stack" that uses the global fetch, so the
* following two examples are equivalent:
*
* const stack = createStack(middleware)
* stack(global.fetch, input, options)
*
* const fetch = createFetch(middleware)
* fetch(input, options)
*
* Thus, createFetch essentially eliminates some boilerplate code
* when you just want to use the global fetch function.
*/

@@ -146,3 +161,3 @@ var createFetch = exports.createFetch = function createFetch() {

var options = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
return stack(options.fetch || fetch, input, options);
return stack(global.fetch, input, options);
});

@@ -277,10 +292,15 @@ };

/**
* Adds the text of the response to response[propertyName].
* Reads the response stream to completion, parses its content
* using the given parser, and adds the result to response.body.
*/
var parseText = exports.parseText = function parseText() {
var propertyName = arguments.length <= 0 || arguments[0] === undefined ? 'textString' : arguments[0];
var parse = exports.parse = function parse(parser) {
var as = arguments.length <= 1 || arguments[1] === undefined ? 'body' : arguments[1];
return onResponse(function (response) {
return response.text().then(function (value) {
response[propertyName] = value;
if (as in response) return response[as];
return response[parser]().then(function (body) {
response[as] = body;
return response;
}, function (error) {
throw new Error('parse(\'' + parser + '\') error: ' + error.stack);
});

@@ -290,17 +310,14 @@ });

/**
* Adds the JSON of the response to response[propertyName].
*/
// Deprecated.
var parseJSON = exports.parseJSON = function parseJSON() {
var propertyName = arguments.length <= 0 || arguments[0] === undefined ? 'jsonData' : arguments[0];
return onResponse(function (response) {
return response.json().then(function (value) {
response[propertyName] = value;
return response;
}, function (error) {
throw new Error('Error parsing JSON: ' + error.stack);
});
});
return parse('json', propertyName);
};
// Deprecated.
var parseText = exports.parseText = function parseText() {
var propertyName = arguments.length <= 0 || arguments[0] === undefined ? 'textString' : arguments[0];
return parse('text', propertyName);
};
/**

@@ -307,0 +324,0 @@ * Adds the requestURL and requestOptions properties to the

@@ -1,1 +0,1 @@

!function(n,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.HTTPClient=t():n.HTTPClient=t()}(this,function(){return function(n){function t(r){if(e[r])return e[r].exports;var o=e[r]={exports:{},id:r,loaded:!1};return n[r].call(o.exports,o,o.exports,t),o.loaded=!0,o.exports}var e={};return t.m=n,t.c=e,t.p="",t(0)}([function(n,t,e){"use strict";function r(n){return n&&n.__esModule?n:{"default":n}}Object.defineProperty(t,"__esModule",{value:!0}),t.requestInfo=t.parseJSON=t.parseText=t.handleResponse=t.onResponse=t.params=t.json=t.body=t.query=t.base=t.accept=t.auth=t.header=t.method=t.init=t.createFetch=t.createStack=t.fetch=t.enhanceFetch=void 0;var o=e(3),u=e(1),i=r(u),c=fetch,f=function(n){return"string"==typeof n?n:(0,o.stringify)(n)},a=function(n){return"string"==typeof n?n:JSON.stringify(n)},s=function(n,t){return t.reduce(function(n,t){return n.then(t)},Promise.resolve(n))},p=t.enhanceFetch=function(n){return function(t){var e=arguments.length<=1||void 0===arguments[1]?{}:arguments[1];return n(t,e).then(function(n){var t=e.responseHandlers;return t&&t.length?s(n,t):n})}},l=p(c);t.fetch=l;var d=function(n,t,e){return n(t,e)},h=t.createStack=function(){for(var n=arguments.length,t=Array(n),e=0;e<n;e++)t[e]=arguments[e];return 0===t.length?d:t.reduceRight(function(n,t){return function(e,r,o){return t(function(t,r){return n(e,t,r)},r,o)}})},v=(t.createFetch=function(){if(0===arguments.length)return l;var n=h.apply(void 0,arguments);return p(function(t){var e=arguments.length<=1||void 0===arguments[1]?{}:arguments[1];return n(e.fetch||fetch,t,e)})},t.init=function(n,t){return function(e,r){var o=arguments.length<=2||void 0===arguments[2]?{}:arguments[2];return o[n]=t,e(r,o)}}),g=(t.method=function(n){return v("method",n)},function(n,t,e){(n.headers||(n.headers={}))[t]=e}),y=t.header=function(n,t){return function(e,r){var o=arguments.length<=2||void 0===arguments[2]?{}:arguments[2];return g(o,n,t),e(r,o)}},b=(t.auth=function(n){return y("Authorization",n)},t.accept=function(n){return y("Accept",n)},t.base=function(n){return function(t,e,r){return t(n+(e||""),r)}},t.query=function(n){var t=f(n);return function(n,e,r){return n(e+(e.indexOf("?")===-1?"?":"&")+t,r)}}),j=t.body=function(n,t){return function(e,r){var o=arguments.length<=2||void 0===arguments[2]?{}:arguments[2];return o.body=n,null!=n.length&&g(o,"Content-Length",(0,i["default"])(n)),t&&g(o,"Content-Type",t),e(r,o)}},O=(t.json=function(n){return j(a(n),"application/json")},t.params=function(n){var t=f(n);return function(n,e){var r=arguments.length<=2||void 0===arguments[2]?{}:arguments[2],o=(r.method||"GET").toUpperCase(),u="GET"===o||"HEAD"===o?b(t):j(t,"application/x-www-form-urlencoded");return u(n,e,r)}},t.onResponse=function(n){return function(t,e){var r=arguments.length<=2||void 0===arguments[2]?{}:arguments[2];return(r.responseHandlers||(r.responseHandlers=[])).push(n),t(e,r)}});t.handleResponse=O,t.parseText=function(){var n=arguments.length<=0||void 0===arguments[0]?"textString":arguments[0];return O(function(t){return t.text().then(function(e){return t[n]=e,t})})},t.parseJSON=function(){var n=arguments.length<=0||void 0===arguments[0]?"jsonData":arguments[0];return O(function(t){return t.json().then(function(e){return t[n]=e,t},function(n){throw new Error("Error parsing JSON: "+n.stack)})})},t.requestInfo=function(){return function(n,t,e){return n(t,e).then(function(n){return n.requestInput=t,n.requestOptions=e,n},function(){var n=arguments.length<=0||void 0===arguments[0]?new Error:arguments[0];throw n.requestInput=t,n.requestOptions=e,n})}}},function(n,t){"use strict";n.exports=function(n){var t,e;if(!n)return 0;for(n=n.toString(),t=e=n.length;t--;){var r=n[t].charCodeAt();56320<=r&&r<=57343&&t--,127<r&&r<=2047?e++:2047<r&&r<=65535&&(e+=2)}return e}},function(n,t){"use strict";function e(n){if(null===n||void 0===n)throw new TypeError("Object.assign cannot be called with null or undefined");return Object(n)}function r(){try{if(!Object.assign)return!1;var n=new String("abc");if(n[5]="de","5"===Object.getOwnPropertyNames(n)[0])return!1;for(var t={},e=0;e<10;e++)t["_"+String.fromCharCode(e)]=e;var r=Object.getOwnPropertyNames(t).map(function(n){return t[n]});if("0123456789"!==r.join(""))return!1;var o={};return"abcdefghijklmnopqrst".split("").forEach(function(n){o[n]=n}),"abcdefghijklmnopqrst"===Object.keys(Object.assign({},o)).join("")}catch(u){return!1}}var o=Object.prototype.hasOwnProperty,u=Object.prototype.propertyIsEnumerable;n.exports=r()?Object.assign:function(n,t){for(var r,i,c=e(n),f=1;f<arguments.length;f++){r=Object(arguments[f]);for(var a in r)o.call(r,a)&&(c[a]=r[a]);if(Object.getOwnPropertySymbols){i=Object.getOwnPropertySymbols(r);for(var s=0;s<i.length;s++)u.call(r,i[s])&&(c[i[s]]=r[i[s]])}}return c}},function(n,t,e){"use strict";function r(n,t){return t.encode?t.strict?o(n):encodeURIComponent(n):n}var o=e(4),u=e(2);t.extract=function(n){return n.split("?")[1]||""},t.parse=function(n){var t=Object.create(null);return"string"!=typeof n?t:(n=n.trim().replace(/^(\?|#|&)/,""))?(n.split("&").forEach(function(n){var e=n.replace(/\+/g," ").split("="),r=e.shift(),o=e.length>0?e.join("="):void 0;r=decodeURIComponent(r),o=void 0===o?null:decodeURIComponent(o),void 0===t[r]?t[r]=o:Array.isArray(t[r])?t[r].push(o):t[r]=[t[r],o]}),t):t},t.stringify=function(n,t){var e={encode:!0,strict:!0};return t=u(e,t),n?Object.keys(n).sort().map(function(e){var o=n[e];if(void 0===o)return"";if(null===o)return r(e,t);if(Array.isArray(o)){var u=[];return o.slice().forEach(function(n){void 0!==n&&(null===n?u.push(r(e,t)):u.push(r(e,t)+"="+r(n,t)))}),u.join("&")}return r(e,t)+"="+r(o,t)}).filter(function(n){return n.length>0}).join("&"):""}},function(n,t){"use strict";n.exports=function(n){return encodeURIComponent(n).replace(/[!'()*]/g,function(n){return"%"+n.charCodeAt(0).toString(16).toUpperCase()})}}])});
!function(n,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.HTTPClient=t():n.HTTPClient=t()}(this,function(){return function(n){function t(r){if(e[r])return e[r].exports;var o=e[r]={exports:{},id:r,loaded:!1};return n[r].call(o.exports,o,o.exports,t),o.loaded=!0,o.exports}var e={};return t.m=n,t.c=e,t.p="",t(0)}([function(n,t,e){"use strict";function r(n){return n&&n.__esModule?n:{"default":n}}Object.defineProperty(t,"__esModule",{value:!0}),t.requestInfo=t.parseText=t.parseJSON=t.parse=t.handleResponse=t.onResponse=t.params=t.json=t.body=t.query=t.base=t.accept=t.auth=t.header=t.method=t.init=t.createFetch=t.createStack=t.fetch=t.enhanceFetch=void 0;var o=e(3),u=e(1),i=r(u),c=(0,eval)("this"),a=function(n){return"string"==typeof n?n:(0,o.stringify)(n)},f=function(n){return"string"==typeof n?n:JSON.stringify(n)},s=function(n,t){return t.reduce(function(n,t){return n.then(t)},Promise.resolve(n))},p=t.enhanceFetch=function(n){return function(t){var e=arguments.length<=1||void 0===arguments[1]?{}:arguments[1];return n(t,e).then(function(n){var t=e.responseHandlers;return t&&t.length?s(n,t):n})}},l=p(c.fetch);t.fetch=l;var d=function(n,t,e){return n(t,e)},h=t.createStack=function(){for(var n=arguments.length,t=Array(n),e=0;e<n;e++)t[e]=arguments[e];return 0===t.length?d:t.reduceRight(function(n,t){return function(e,r,o){return t(function(t,r){return n(e,t,r)},r,o)}})},v=(t.createFetch=function(){if(0===arguments.length)return l;var n=h.apply(void 0,arguments);return p(function(t){var e=arguments.length<=1||void 0===arguments[1]?{}:arguments[1];return n(c.fetch,t,e)})},t.init=function(n,t){return function(e,r){var o=arguments.length<=2||void 0===arguments[2]?{}:arguments[2];return o[n]=t,e(r,o)}}),g=(t.method=function(n){return v("method",n)},function(n,t,e){(n.headers||(n.headers={}))[t]=e}),y=t.header=function(n,t){return function(e,r){var o=arguments.length<=2||void 0===arguments[2]?{}:arguments[2];return g(o,n,t),e(r,o)}},b=(t.auth=function(n){return y("Authorization",n)},t.accept=function(n){return y("Accept",n)},t.base=function(n){return function(t,e,r){return t(n+(e||""),r)}},t.query=function(n){var t=a(n);return function(n,e,r){return n(e+(e.indexOf("?")===-1?"?":"&")+t,r)}}),j=t.body=function(n,t){return function(e,r){var o=arguments.length<=2||void 0===arguments[2]?{}:arguments[2];return o.body=n,null!=n.length&&g(o,"Content-Length",(0,i["default"])(n)),t&&g(o,"Content-Type",t),e(r,o)}},m=(t.json=function(n){return j(f(n),"application/json")},t.params=function(n){var t=a(n);return function(n,e){var r=arguments.length<=2||void 0===arguments[2]?{}:arguments[2],o=(r.method||"GET").toUpperCase(),u="GET"===o||"HEAD"===o?b(t):j(t,"application/x-www-form-urlencoded");return u(n,e,r)}},t.onResponse=function(n){return function(t,e){var r=arguments.length<=2||void 0===arguments[2]?{}:arguments[2];return(r.responseHandlers||(r.responseHandlers=[])).push(n),t(e,r)}}),O=(t.handleResponse=m,t.parse=function(n){var t=arguments.length<=1||void 0===arguments[1]?"body":arguments[1];return m(function(e){return t in e?e[t]:e[n]().then(function(n){return e[t]=n,e},function(t){throw new Error("parse('"+n+"') error: "+t.stack)})})});t.parseJSON=function(){var n=arguments.length<=0||void 0===arguments[0]?"jsonData":arguments[0];return O("json",n)},t.parseText=function(){var n=arguments.length<=0||void 0===arguments[0]?"textString":arguments[0];return O("text",n)},t.requestInfo=function(){return function(n,t,e){return n(t,e).then(function(n){return n.requestInput=t,n.requestOptions=e,n},function(){var n=arguments.length<=0||void 0===arguments[0]?new Error:arguments[0];throw n.requestInput=t,n.requestOptions=e,n})}}},function(n,t){"use strict";n.exports=function(n){var t,e;if(!n)return 0;for(n=n.toString(),t=e=n.length;t--;){var r=n[t].charCodeAt();56320<=r&&r<=57343&&t--,127<r&&r<=2047?e++:2047<r&&r<=65535&&(e+=2)}return e}},function(n,t){"use strict";function e(n){if(null===n||void 0===n)throw new TypeError("Object.assign cannot be called with null or undefined");return Object(n)}function r(){try{if(!Object.assign)return!1;var n=new String("abc");if(n[5]="de","5"===Object.getOwnPropertyNames(n)[0])return!1;for(var t={},e=0;e<10;e++)t["_"+String.fromCharCode(e)]=e;var r=Object.getOwnPropertyNames(t).map(function(n){return t[n]});if("0123456789"!==r.join(""))return!1;var o={};return"abcdefghijklmnopqrst".split("").forEach(function(n){o[n]=n}),"abcdefghijklmnopqrst"===Object.keys(Object.assign({},o)).join("")}catch(u){return!1}}var o=Object.prototype.hasOwnProperty,u=Object.prototype.propertyIsEnumerable;n.exports=r()?Object.assign:function(n,t){for(var r,i,c=e(n),a=1;a<arguments.length;a++){r=Object(arguments[a]);for(var f in r)o.call(r,f)&&(c[f]=r[f]);if(Object.getOwnPropertySymbols){i=Object.getOwnPropertySymbols(r);for(var s=0;s<i.length;s++)u.call(r,i[s])&&(c[i[s]]=r[i[s]])}}return c}},function(n,t,e){"use strict";function r(n,t){return t.encode?t.strict?o(n):encodeURIComponent(n):n}var o=e(4),u=e(2);t.extract=function(n){return n.split("?")[1]||""},t.parse=function(n){var t=Object.create(null);return"string"!=typeof n?t:(n=n.trim().replace(/^(\?|#|&)/,""))?(n.split("&").forEach(function(n){var e=n.replace(/\+/g," ").split("="),r=e.shift(),o=e.length>0?e.join("="):void 0;r=decodeURIComponent(r),o=void 0===o?null:decodeURIComponent(o),void 0===t[r]?t[r]=o:Array.isArray(t[r])?t[r].push(o):t[r]=[t[r],o]}),t):t},t.stringify=function(n,t){var e={encode:!0,strict:!0};return t=u(e,t),n?Object.keys(n).sort().map(function(e){var o=n[e];if(void 0===o)return"";if(null===o)return r(e,t);if(Array.isArray(o)){var u=[];return o.slice().forEach(function(n){void 0!==n&&(null===n?u.push(r(e,t)):u.push(r(e,t)+"="+r(n,t)))}),u.join("&")}return r(e,t)+"="+r(o,t)}).filter(function(n){return n.length>0}).join("&"):""}},function(n,t){"use strict";n.exports=function(n){return encodeURIComponent(n).replace(/[!'()*]/g,function(n){return"%"+n.charCodeAt(0).toString(16).toUpperCase()})}}])});
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc