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 2.3.1 to 2.4.0

7

CHANGES.md

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

## [HEAD]
- Export `handleResponse` helper for building middleware
- Fixed `browser` field in package config
[HEAD]: https://github.com/mjackson/http-client/compare/latest...HEAD
## [2.3.0]

@@ -2,0 +9,0 @@ > Mar 14, 2016

25

lib/index.js

@@ -6,3 +6,3 @@ 'use strict';

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

@@ -23,8 +23,10 @@ var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; };

return function (input, options, callback) {
if (typeof options === 'function') {
callback = options;
options = undefined;
} else if (typeof input === 'function') {
callback = input;
input = undefined;
if (typeof callback !== 'function') {
if (typeof options === 'function') {
callback = options;
options = undefined;
} else if (typeof input === 'function') {
callback = input;
input = undefined;
}
}

@@ -190,3 +192,6 @@

var enhanceResponse = function enhanceResponse(callback) {
/**
* A helper for creating middleware that handles a successful response.
*/
var handleResponse = exports.handleResponse = function handleResponse(callback) {
return function (fetch, input, options) {

@@ -202,3 +207,3 @@ return fetch(input, options).then(callback);

var propertyName = arguments.length <= 0 || arguments[0] === undefined ? 'textString' : arguments[0];
return enhanceResponse(function (response) {
return handleResponse(function (response) {
return response.text().then(function (value) {

@@ -216,3 +221,3 @@ response[propertyName] = value;

var propertyName = arguments.length <= 0 || arguments[0] === undefined ? 'jsonData' : arguments[0];
return enhanceResponse(function (response) {
return handleResponse(function (response) {
return response.json().then(function (value) {

@@ -219,0 +224,0 @@ response[propertyName] = value;

4

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

@@ -8,3 +8,3 @@ "author": "Michael Jackson",

"main": "lib",
"browser": "umd/HTTPClient.min.js",
"browser": "umd/http-client.min.js",
"files": [

@@ -11,0 +11,0 @@ "lib",

@@ -82,2 +82,20 @@ # http-client [![Travis][build-badge]][build] [![npm package][npm-badge]][npm]

## Top-level API
#### `fetch([input], [options], [callback])`
An enhanced `fetch` function that accepts an optional `callback` as a trailing argument. Use this directly if you don't need any middleware.
#### `createFetch(...middleware)`
Creates a `fetch` function that is fronted by some middleware. This function has the same signature as the enhanced `fetch` function in the top-level API.
#### `createStack(...middleware)`
Combines several middleware into one, in the same order they are provided as arguments. Use this function to create re-usable [middleware stacks](#stacks).
#### `handleResponse(callback)`
A helper for creating middleware that enhances the `response` object in some way. Used internally to create the `parseText` and `parseJSON` middleware.
## Middleware

@@ -188,3 +206,3 @@

#### `parseText(propertyName='textString')`
#### `parseText(propertyName = 'textString')`

@@ -205,3 +223,3 @@ Reads the response body as text and puts it on `response.textString`.

#### `parseJSON(propertyName='jsonData')`
#### `parseJSON(propertyName = 'jsonData')`

@@ -208,0 +226,0 @@ Reads the response body as JSON and puts it on `response.jsonData`.

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

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

@@ -79,8 +79,10 @@ var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; };

return function (input, options, callback) {
if (typeof options === 'function') {
callback = options;
options = undefined;
} else if (typeof input === 'function') {
callback = input;
input = undefined;
if (typeof callback !== 'function') {
if (typeof options === 'function') {
callback = options;
options = undefined;
} else if (typeof input === 'function') {
callback = input;
input = undefined;
}
}

@@ -246,3 +248,6 @@

var enhanceResponse = function enhanceResponse(callback) {
/**
* A helper for creating middleware that handles a successful response.
*/
var handleResponse = exports.handleResponse = function handleResponse(callback) {
return function (fetch, input, options) {

@@ -258,3 +263,3 @@ return fetch(input, options).then(callback);

var propertyName = arguments.length <= 0 || arguments[0] === undefined ? 'textString' : arguments[0];
return enhanceResponse(function (response) {
return handleResponse(function (response) {
return response.text().then(function (value) {

@@ -272,3 +277,3 @@ response[propertyName] = value;

var propertyName = arguments.length <= 0 || arguments[0] === undefined ? 'jsonData' : arguments[0];
return enhanceResponse(function (response) {
return handleResponse(function (response) {
return response.json().then(function (value) {

@@ -275,0 +280,0 @@ response[propertyName] = value;

@@ -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";Object.defineProperty(t,"__esModule",{value:!0}),t.requestInfo=t.parseJSON=t.parseText=t.params=t.json=t.body=t.query=t.base=t.accept=t.auth=t.header=t.method=t.createFetch=t.createStack=t.fetch=void 0;var r="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(n){return typeof n}:function(n){return n&&"function"==typeof Symbol&&n.constructor===Symbol?"symbol":typeof n},o=e(1),u="function"!=typeof fetch?"object"!==r(window)&&e(!function(){var n=new Error('Cannot find module "node-fetch"');throw n.code="MODULE_NOT_FOUND",n}()):fetch,i=function(n,t){return n.then(function(n){return t(null,n)},t)},c=function(n){return function(t,e,r){"function"==typeof e?(r=e,e=void 0):"function"==typeof t&&(r=t,t=void 0);var o=n(t,e);return"function"==typeof r?i(o,r):o}},f=c(u),a=function(n){return"string"==typeof n?n:JSON.stringify(n)},p=function(n){return"string"==typeof n?n:(0,o.stringify)(n)},s=function(n,t,e){return n(t,e)};t.fetch=f;var d=t.createStack=function(){for(var n=arguments.length,t=Array(n),e=0;n>e;e++)t[e]=arguments[e];return 0===t.length?s:t.reduceRight(function(n,t){return function(e,r,o){return t(function(t,r){return n(e,t,r)},r,o)}})},l=(t.createFetch=function(){if(0===arguments.length)return f;var n=d.apply(void 0,arguments);return c(function(t,e){return n(u,t,e)})},function(n,t,e){(n.headers||(n.headers={}))[t]=e}),h=(t.method=function(n){return function(t,e){var r=arguments.length<=2||void 0===arguments[2]?{}:arguments[2];return r.method=n,t(e,r)}},t.header=function(n,t){return function(e,r){var o=arguments.length<=2||void 0===arguments[2]?{}:arguments[2];return l(o,n,t),e(r,o)}}),y=(t.auth=function(n){return h("Authorization",n)},t.accept=function(n){return h("Accept",n)},t.base=function(n){return function(t,e,r){return t(n+(e||""),r)}},t.query=function(n){var t=p(n);return function(n,e,r){return n(e+(-1===e.indexOf("?")?"?":"&")+t,r)}}),v=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&&l(o,"Content-Length",n.length),t&&l(o,"Content-Type",t),e(r,o)}},g=(t.json=function(n){return v(a(n),"application/json")},t.params=function(n){var t=p(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?y(t):v(t,"application/x-www-form-urlencoded");return u(n,e,r)}},function(n){return function(t,e,r){return t(e,r).then(n)}});t.parseText=function(){var n=arguments.length<=0||void 0===arguments[0]?"textString":arguments[0];return g(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 g(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,e){"use strict";var r=e(2);t.extract=function(n){return n.split("?")[1]||""},t.parse=function(n){return"string"!=typeof n?{}:(n=n.trim().replace(/^(\?|#|&)/,""),n?n.split("&").reduce(function(n,t){var e=t.replace(/\+/g," ").split("="),r=e.shift(),o=e.length>0?e.join("="):void 0;return r=decodeURIComponent(r),o=void 0===o?null:decodeURIComponent(o),n.hasOwnProperty(r)?Array.isArray(n[r])?n[r].push(o):n[r]=[n[r],o]:n[r]=o,n},{}):{})},t.stringify=function(n){return n?Object.keys(n).sort().map(function(t){var e=n[t];return void 0===e?"":null===e?t:Array.isArray(e)?e.slice().sort().map(function(n){return r(t)+"="+r(n)}).join("&"):r(t)+"="+r(e)}).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";Object.defineProperty(t,"__esModule",{value:!0}),t.requestInfo=t.parseJSON=t.parseText=t.handleResponse=t.params=t.json=t.body=t.query=t.base=t.accept=t.auth=t.header=t.method=t.createFetch=t.createStack=t.fetch=void 0;var r="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(n){return typeof n}:function(n){return n&&"function"==typeof Symbol&&n.constructor===Symbol?"symbol":typeof n},o=e(1),u="function"!=typeof fetch?"object"!==r(window)&&e(!function(){var n=new Error('Cannot find module "node-fetch"');throw n.code="MODULE_NOT_FOUND",n}()):fetch,i=function(n,t){return n.then(function(n){return t(null,n)},t)},c=function(n){return function(t,e,r){"function"!=typeof r&&("function"==typeof e?(r=e,e=void 0):"function"==typeof t&&(r=t,t=void 0));var o=n(t,e);return"function"==typeof r?i(o,r):o}},f=c(u),a=function(n){return"string"==typeof n?n:JSON.stringify(n)},s=function(n){return"string"==typeof n?n:(0,o.stringify)(n)},p=function(n,t,e){return n(t,e)};t.fetch=f;var d=t.createStack=function(){for(var n=arguments.length,t=Array(n),e=0;n>e;e++)t[e]=arguments[e];return 0===t.length?p:t.reduceRight(function(n,t){return function(e,r,o){return t(function(t,r){return n(e,t,r)},r,o)}})},l=(t.createFetch=function(){if(0===arguments.length)return f;var n=d.apply(void 0,arguments);return c(function(t,e){return n(u,t,e)})},function(n,t,e){(n.headers||(n.headers={}))[t]=e}),h=(t.method=function(n){return function(t,e){var r=arguments.length<=2||void 0===arguments[2]?{}:arguments[2];return r.method=n,t(e,r)}},t.header=function(n,t){return function(e,r){var o=arguments.length<=2||void 0===arguments[2]?{}:arguments[2];return l(o,n,t),e(r,o)}}),y=(t.auth=function(n){return h("Authorization",n)},t.accept=function(n){return h("Accept",n)},t.base=function(n){return function(t,e,r){return t(n+(e||""),r)}},t.query=function(n){var t=s(n);return function(n,e,r){return n(e+(-1===e.indexOf("?")?"?":"&")+t,r)}}),v=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&&l(o,"Content-Length",n.length),t&&l(o,"Content-Type",t),e(r,o)}},g=(t.json=function(n){return v(a(n),"application/json")},t.params=function(n){var t=s(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?y(t):v(t,"application/x-www-form-urlencoded");return u(n,e,r)}},t.handleResponse=function(n){return function(t,e,r){return t(e,r).then(n)}});t.parseText=function(){var n=arguments.length<=0||void 0===arguments[0]?"textString":arguments[0];return g(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 g(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,e){"use strict";var r=e(2);t.extract=function(n){return n.split("?")[1]||""},t.parse=function(n){return"string"!=typeof n?{}:(n=n.trim().replace(/^(\?|#|&)/,""),n?n.split("&").reduce(function(n,t){var e=t.replace(/\+/g," ").split("="),r=e.shift(),o=e.length>0?e.join("="):void 0;return r=decodeURIComponent(r),o=void 0===o?null:decodeURIComponent(o),n.hasOwnProperty(r)?Array.isArray(n[r])?n[r].push(o):n[r]=[n[r],o]:n[r]=o,n},{}):{})},t.stringify=function(n){return n?Object.keys(n).sort().map(function(t){var e=n[t];return void 0===e?"":null===e?t:Array.isArray(e)?e.slice().sort().map(function(n){return r(t)+"="+r(n)}).join("&"):r(t)+"="+r(e)}).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