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

http-otro

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

http-otro - npm Package Compare versions

Comparing version 0.0.1 to 0.0.2

es/client.js

69

dist/http-otro.js

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

});
exports.response = exports.client = undefined;
var _client2 = __webpack_require__(1);
var _client3 = _interopRequireDefault(_client2);
var _response2 = __webpack_require__(2);
var _response = _interopRequireWildcard(_response2);
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
exports.client = _client3.default;
exports.response = _response;
/***/ },
/* 1 */
/***/ function(module, exports, __webpack_require__) {
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
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; };

@@ -147,3 +173,3 @@

var _isomorphicFetch = __webpack_require__(1);
var _isomorphicFetch = __webpack_require__(3);

@@ -155,3 +181,38 @@ var _isomorphicFetch2 = _interopRequireDefault(_isomorphicFetch);

/***/ },
/* 1 */
/* 2 */
/***/ function(module, exports) {
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.json = json;
exports.body = body;
function json(request) {
return new Promise(function (resolve, reject) {
request.then(function (response) {
return response.json();
}).then(function (json) {
resolve(json);
}).catch(function (e) {
reject(e);
});
});
}
function body(request) {
return new Promise(function (resolve, reject) {
request.then(function (response) {
return response.body();
}).then(function (body) {
resolve(body);
}).catch(function (e) {
reject(e);
});
});
}
/***/ },
/* 3 */
/***/ function(module, exports, __webpack_require__) {

@@ -163,3 +224,3 @@

// Return that as the export for use in Webpack, Browserify etc.
__webpack_require__(2);
__webpack_require__(4);
module.exports = self.fetch.bind(self);

@@ -169,3 +230,3 @@

/***/ },
/* 2 */
/* 4 */
/***/ function(module, exports) {

@@ -172,0 +233,0 @@

2

dist/http-otro.min.js

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

!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports["http-otro"]=e():t["http-otro"]=e()}(this,function(){return function(t){function e(o){if(r[o])return r[o].exports;var n=r[o]={exports:{},id:o,loaded:!1};return t[o].call(n.exports,n,n.exports,e),n.loaded=!0,n.exports}var r={};return e.m=t,e.c=r,e.p="",e(0)}([function(t,e,r){"use strict";function o(t){return t&&t.__esModule?t:{"default":t}}Object.defineProperty(e,"__esModule",{value:!0});var n="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol?"symbol":typeof t};e.default=function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},e=t.host||"",r=function(t){var e=[];for(var r in t)t.hasOwnProperty(r)&&e.push(encodeURIComponent(r)+"="+encodeURIComponent(t[r]));return e.length>0?e.join("&"):""},o=function(t,e){return e.length?[t,e].join("?"):t},i=function(r,o){var i=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{},a=arguments.length>3&&void 0!==arguments[3]?arguments[3]:{};o=o.toUpperCase();var u={method:o,headers:{}};for(var d in a.headers)u.headers[d]=a.headers[d];for(var d in t.headers)u.headers[d]=t.headers[d];return u.headers["Content-Type"]||(u.headers["Content-Type"]="application/json"),"GET"!=o&&(u.body="object"==(void 0===i?"undefined":n(i))?JSON.stringify(i):i),(0,s.default)(e+r,u)},a=function(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{},s=r(e);return i(o(t,s),"GET",{},n)},u=function(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{};return i(t,"PUT",e,r)},d=function(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{};return i(t,"POST",e,r)},h=function(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{},s=r(e);return i(o(t,s),"DELETE",{},n)};return{request:i,get:a,put:u,post:d,del:h}};var i=r(1),s=o(i)},function(t,e,r){r(2),t.exports=self.fetch.bind(self)},function(t,e){!function(t){"use strict";function e(t){if("string"!=typeof t&&(t+=""),/[^a-z0-9\-#$%&'*+.\^_`|~]/i.test(t))throw new TypeError("Invalid character in header field name");return t.toLowerCase()}function r(t){return"string"!=typeof t&&(t+=""),t}function o(t){this.map={},t instanceof o?t.forEach(function(t,e){this.append(e,t)},this):t&&Object.getOwnPropertyNames(t).forEach(function(e){this.append(e,t[e])},this)}function n(t){return t.bodyUsed?Promise.reject(new TypeError("Already read")):void(t.bodyUsed=!0)}function i(t){return new Promise(function(e,r){t.onload=function(){e(t.result)},t.onerror=function(){r(t.error)}})}function s(t){var e=new FileReader;return e.readAsArrayBuffer(t),i(e)}function a(t){var e=new FileReader;return e.readAsText(t),i(e)}function u(){return this.bodyUsed=!1,this._initBody=function(t){if(this._bodyInit=t,"string"==typeof t)this._bodyText=t;else if(c.blob&&Blob.prototype.isPrototypeOf(t))this._bodyBlob=t;else if(c.formData&&FormData.prototype.isPrototypeOf(t))this._bodyFormData=t;else if(t){if(!c.arrayBuffer||!ArrayBuffer.prototype.isPrototypeOf(t))throw Error("unsupported BodyInit type")}else this._bodyText="";this.headers.get("content-type")||("string"==typeof t?this.headers.set("content-type","text/plain;charset=UTF-8"):this._bodyBlob&&this._bodyBlob.type&&this.headers.set("content-type",this._bodyBlob.type))},c.blob?(this.blob=function(){var t=n(this);if(t)return t;if(this._bodyBlob)return Promise.resolve(this._bodyBlob);if(this._bodyFormData)throw Error("could not read FormData body as blob");return Promise.resolve(new Blob([this._bodyText]))},this.arrayBuffer=function(){return this.blob().then(s)},this.text=function(){var t=n(this);if(t)return t;if(this._bodyBlob)return a(this._bodyBlob);if(this._bodyFormData)throw Error("could not read FormData body as text");return Promise.resolve(this._bodyText)}):this.text=function(){var t=n(this);return t?t:Promise.resolve(this._bodyText)},c.formData&&(this.formData=function(){return this.text().then(f)}),this.json=function(){return this.text().then(JSON.parse)},this}function d(t){var e=t.toUpperCase();return y.indexOf(e)>-1?e:t}function h(t,e){e=e||{};var r=e.body;if(h.prototype.isPrototypeOf(t)){if(t.bodyUsed)throw new TypeError("Already read");this.url=t.url,this.credentials=t.credentials,e.headers||(this.headers=new o(t.headers)),this.method=t.method,this.mode=t.mode,r||(r=t._bodyInit,t.bodyUsed=!0)}else this.url=t;if(this.credentials=e.credentials||this.credentials||"omit",!e.headers&&this.headers||(this.headers=new o(e.headers)),this.method=d(e.method||this.method||"GET"),this.mode=e.mode||this.mode||null,this.referrer=null,("GET"===this.method||"HEAD"===this.method)&&r)throw new TypeError("Body not allowed for GET or HEAD requests");this._initBody(r)}function f(t){var e=new FormData;return t.trim().split("&").forEach(function(t){if(t){var r=t.split("="),o=r.shift().replace(/\+/g," "),n=r.join("=").replace(/\+/g," ");e.append(decodeURIComponent(o),decodeURIComponent(n))}}),e}function p(t){var e=new o,r=t.getAllResponseHeaders().trim().split("\n");return r.forEach(function(t){var r=t.trim().split(":"),o=r.shift().trim(),n=r.join(":").trim();e.append(o,n)}),e}function l(t,e){e||(e={}),this.type="default",this.status=e.status,this.ok=this.status>=200&&300>this.status,this.statusText=e.statusText,this.headers=e.headers instanceof o?e.headers:new o(e.headers),this.url=e.url||"",this._initBody(t)}if(!t.fetch){o.prototype.append=function(t,o){t=e(t),o=r(o);var n=this.map[t];n||(n=[],this.map[t]=n),n.push(o)},o.prototype.delete=function(t){delete this.map[e(t)]},o.prototype.get=function(t){var r=this.map[e(t)];return r?r[0]:null},o.prototype.getAll=function(t){return this.map[e(t)]||[]},o.prototype.has=function(t){return this.map.hasOwnProperty(e(t))},o.prototype.set=function(t,o){this.map[e(t)]=[r(o)]},o.prototype.forEach=function(t,e){Object.getOwnPropertyNames(this.map).forEach(function(r){this.map[r].forEach(function(o){t.call(e,o,r,this)},this)},this)};var c={blob:"FileReader"in t&&"Blob"in t&&function(){try{return new Blob,!0}catch(t){return!1}}(),formData:"FormData"in t,arrayBuffer:"ArrayBuffer"in t},y=["DELETE","GET","HEAD","OPTIONS","POST","PUT"];h.prototype.clone=function(){return new h(this)},u.call(h.prototype),u.call(l.prototype),l.prototype.clone=function(){return new l(this._bodyInit,{status:this.status,statusText:this.statusText,headers:new o(this.headers),url:this.url})},l.error=function(){var t=new l(null,{status:0,statusText:""});return t.type="error",t};var b=[301,302,303,307,308];l.redirect=function(t,e){if(-1===b.indexOf(e))throw new RangeError("Invalid status code");return new l(null,{status:e,headers:{location:t}})},t.Headers=o,t.Request=h,t.Response=l,t.fetch=function(t,e){return new Promise(function(r,o){function n(){return"responseURL"in s?s.responseURL:/^X-Request-URL:/m.test(s.getAllResponseHeaders())?s.getResponseHeader("X-Request-URL"):void 0}var i;i=h.prototype.isPrototypeOf(t)&&!e?t:new h(t,e);var s=new XMLHttpRequest;s.onload=function(){var t=1223===s.status?204:s.status;if(100>t||t>599)return void o(new TypeError("Network request failed"));var e={status:t,statusText:s.statusText,headers:p(s),url:n()},i="response"in s?s.response:s.responseText;r(new l(i,e))},s.onerror=function(){o(new TypeError("Network request failed"))},s.open(i.method,i.url,!0),"include"===i.credentials&&(s.withCredentials=!0),"responseType"in s&&c.blob&&(s.responseType="blob"),i.headers.forEach(function(t,e){s.setRequestHeader(e,t)}),s.send(void 0===i._bodyInit?null:i._bodyInit)})},t.fetch.polyfill=!0}}("undefined"!=typeof self?self:this)}])});
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports["http-otro"]=e():t["http-otro"]=e()}(this,function(){return function(t){function e(o){if(r[o])return r[o].exports;var n=r[o]={exports:{},id:o,loaded:!1};return t[o].call(n.exports,n,n.exports,e),n.loaded=!0,n.exports}var r={};return e.m=t,e.c=r,e.p="",e(0)}([function(t,e,r){"use strict";function o(t){if(t&&t.__esModule)return t;var e={};if(null!=t)for(var r in t)Object.prototype.hasOwnProperty.call(t,r)&&(e[r]=t[r]);return e.default=t,e}function n(t){return t&&t.__esModule?t:{"default":t}}Object.defineProperty(e,"__esModule",{value:!0}),e.response=e.client=void 0;var i=r(1),s=n(i),u=r(2),a=o(u);e.client=s.default,e.response=a},function(t,e,r){"use strict";function o(t){return t&&t.__esModule?t:{"default":t}}Object.defineProperty(e,"__esModule",{value:!0});var n="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol?"symbol":typeof t};e.default=function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},e=t.host||"",r=function(t){var e=[];for(var r in t)t.hasOwnProperty(r)&&e.push(encodeURIComponent(r)+"="+encodeURIComponent(t[r]));return e.length>0?e.join("&"):""},o=function(t,e){return e.length?[t,e].join("?"):t},i=function(r,o){var i=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{},u=arguments.length>3&&void 0!==arguments[3]?arguments[3]:{};o=o.toUpperCase();var a={method:o,headers:{}};for(var d in u.headers)a.headers[d]=u.headers[d];for(var d in t.headers)a.headers[d]=t.headers[d];return a.headers["Content-Type"]||(a.headers["Content-Type"]="application/json"),"GET"!=o&&(a.body="object"==(void 0===i?"undefined":n(i))?JSON.stringify(i):i),(0,s.default)(e+r,a)},u=function(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{},s=r(e);return i(o(t,s),"GET",{},n)},a=function(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{};return i(t,"PUT",e,r)},d=function(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{};return i(t,"POST",e,r)},f=function(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{},s=r(e);return i(o(t,s),"DELETE",{},n)};return{request:i,get:u,put:a,post:d,del:f}};var i=r(3),s=o(i)},function(t,e){"use strict";function r(t){return new Promise(function(e,r){t.then(function(t){return t.json()}).then(function(t){e(t)}).catch(function(t){r(t)})})}function o(t){return new Promise(function(e,r){t.then(function(t){return t.body()}).then(function(t){e(t)}).catch(function(t){r(t)})})}Object.defineProperty(e,"__esModule",{value:!0}),e.json=r,e.body=o},function(t,e,r){r(4),t.exports=self.fetch.bind(self)},function(t,e){!function(t){"use strict";function e(t){if("string"!=typeof t&&(t+=""),/[^a-z0-9\-#$%&'*+.\^_`|~]/i.test(t))throw new TypeError("Invalid character in header field name");return t.toLowerCase()}function r(t){return"string"!=typeof t&&(t+=""),t}function o(t){this.map={},t instanceof o?t.forEach(function(t,e){this.append(e,t)},this):t&&Object.getOwnPropertyNames(t).forEach(function(e){this.append(e,t[e])},this)}function n(t){return t.bodyUsed?Promise.reject(new TypeError("Already read")):void(t.bodyUsed=!0)}function i(t){return new Promise(function(e,r){t.onload=function(){e(t.result)},t.onerror=function(){r(t.error)}})}function s(t){var e=new FileReader;return e.readAsArrayBuffer(t),i(e)}function u(t){var e=new FileReader;return e.readAsText(t),i(e)}function a(){return this.bodyUsed=!1,this._initBody=function(t){if(this._bodyInit=t,"string"==typeof t)this._bodyText=t;else if(p.blob&&Blob.prototype.isPrototypeOf(t))this._bodyBlob=t;else if(p.formData&&FormData.prototype.isPrototypeOf(t))this._bodyFormData=t;else if(t){if(!p.arrayBuffer||!ArrayBuffer.prototype.isPrototypeOf(t))throw Error("unsupported BodyInit type")}else this._bodyText="";this.headers.get("content-type")||("string"==typeof t?this.headers.set("content-type","text/plain;charset=UTF-8"):this._bodyBlob&&this._bodyBlob.type&&this.headers.set("content-type",this._bodyBlob.type))},p.blob?(this.blob=function(){var t=n(this);if(t)return t;if(this._bodyBlob)return Promise.resolve(this._bodyBlob);if(this._bodyFormData)throw Error("could not read FormData body as blob");return Promise.resolve(new Blob([this._bodyText]))},this.arrayBuffer=function(){return this.blob().then(s)},this.text=function(){var t=n(this);if(t)return t;if(this._bodyBlob)return u(this._bodyBlob);if(this._bodyFormData)throw Error("could not read FormData body as text");return Promise.resolve(this._bodyText)}):this.text=function(){var t=n(this);return t?t:Promise.resolve(this._bodyText)},p.formData&&(this.formData=function(){return this.text().then(h)}),this.json=function(){return this.text().then(JSON.parse)},this}function d(t){var e=t.toUpperCase();return y.indexOf(e)>-1?e:t}function f(t,e){e=e||{};var r=e.body;if(f.prototype.isPrototypeOf(t)){if(t.bodyUsed)throw new TypeError("Already read");this.url=t.url,this.credentials=t.credentials,e.headers||(this.headers=new o(t.headers)),this.method=t.method,this.mode=t.mode,r||(r=t._bodyInit,t.bodyUsed=!0)}else this.url=t;if(this.credentials=e.credentials||this.credentials||"omit",!e.headers&&this.headers||(this.headers=new o(e.headers)),this.method=d(e.method||this.method||"GET"),this.mode=e.mode||this.mode||null,this.referrer=null,("GET"===this.method||"HEAD"===this.method)&&r)throw new TypeError("Body not allowed for GET or HEAD requests");this._initBody(r)}function h(t){var e=new FormData;return t.trim().split("&").forEach(function(t){if(t){var r=t.split("="),o=r.shift().replace(/\+/g," "),n=r.join("=").replace(/\+/g," ");e.append(decodeURIComponent(o),decodeURIComponent(n))}}),e}function c(t){var e=new o,r=t.getAllResponseHeaders().trim().split("\n");return r.forEach(function(t){var r=t.trim().split(":"),o=r.shift().trim(),n=r.join(":").trim();e.append(o,n)}),e}function l(t,e){e||(e={}),this.type="default",this.status=e.status,this.ok=this.status>=200&&300>this.status,this.statusText=e.statusText,this.headers=e.headers instanceof o?e.headers:new o(e.headers),this.url=e.url||"",this._initBody(t)}if(!t.fetch){o.prototype.append=function(t,o){t=e(t),o=r(o);var n=this.map[t];n||(n=[],this.map[t]=n),n.push(o)},o.prototype.delete=function(t){delete this.map[e(t)]},o.prototype.get=function(t){var r=this.map[e(t)];return r?r[0]:null},o.prototype.getAll=function(t){return this.map[e(t)]||[]},o.prototype.has=function(t){return this.map.hasOwnProperty(e(t))},o.prototype.set=function(t,o){this.map[e(t)]=[r(o)]},o.prototype.forEach=function(t,e){Object.getOwnPropertyNames(this.map).forEach(function(r){this.map[r].forEach(function(o){t.call(e,o,r,this)},this)},this)};var p={blob:"FileReader"in t&&"Blob"in t&&function(){try{return new Blob,!0}catch(t){return!1}}(),formData:"FormData"in t,arrayBuffer:"ArrayBuffer"in t},y=["DELETE","GET","HEAD","OPTIONS","POST","PUT"];f.prototype.clone=function(){return new f(this)},a.call(f.prototype),a.call(l.prototype),l.prototype.clone=function(){return new l(this._bodyInit,{status:this.status,statusText:this.statusText,headers:new o(this.headers),url:this.url})},l.error=function(){var t=new l(null,{status:0,statusText:""});return t.type="error",t};var b=[301,302,303,307,308];l.redirect=function(t,e){if(-1===b.indexOf(e))throw new RangeError("Invalid status code");return new l(null,{status:e,headers:{location:t}})},t.Headers=o,t.Request=f,t.Response=l,t.fetch=function(t,e){return new Promise(function(r,o){function n(){return"responseURL"in s?s.responseURL:/^X-Request-URL:/m.test(s.getAllResponseHeaders())?s.getResponseHeader("X-Request-URL"):void 0}var i;i=f.prototype.isPrototypeOf(t)&&!e?t:new f(t,e);var s=new XMLHttpRequest;s.onload=function(){var t=1223===s.status?204:s.status;if(100>t||t>599)return void o(new TypeError("Network request failed"));var e={status:t,statusText:s.statusText,headers:c(s),url:n()},i="response"in s?s.response:s.responseText;r(new l(i,e))},s.onerror=function(){o(new TypeError("Network request failed"))},s.open(i.method,i.url,!0),"include"===i.credentials&&(s.withCredentials=!0),"responseType"in s&&p.blob&&(s.responseType="blob"),i.headers.forEach(function(t,e){s.setRequestHeader(e,t)}),s.send(void 0===i._bodyInit?null:i._bodyInit)})},t.fetch.polyfill=!0}}("undefined"!=typeof self?self:this)}])});

@@ -6,90 +6,17 @@ 'use strict';

});
exports.response = exports.client = undefined;
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; };
var _client2 = require('./client');
exports.default = function () {
var defaults = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0];
var _client3 = _interopRequireDefault(_client2);
var host = defaults.host || '';
var _response2 = require('./response');
var query = function query(params) {
var arr = [];
for (var p in params) {
if (params.hasOwnProperty(p)) {
arr.push(encodeURIComponent(p) + "=" + encodeURIComponent(params[p]));
}
}
var _response = _interopRequireWildcard(_response2);
return arr.length > 0 ? arr.join('&') : '';
};
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
var queryURL = function queryURL(resource, qs) {
return qs.length ? [resource, qs].join('?') : resource;
};
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var request = function request(resource, method) {
var body = arguments.length <= 2 || arguments[2] === undefined ? {} : arguments[2];
var headers = arguments.length <= 3 || arguments[3] === undefined ? {} : arguments[3];
method = method.toUpperCase();
var args = {
method: method,
headers: {}
};
for (var key in headers.headers) {
args.headers[key] = headers.headers[key];
}
for (var key in defaults.headers) {
args.headers[key] = defaults.headers[key];
}
if (!args.headers['Content-Type']) {
args.headers['Content-Type'] = 'application/json';
}
if (method != 'GET') args.body = (typeof body === 'undefined' ? 'undefined' : _typeof(body)) == 'object' ? JSON.stringify(body) : body;
return (0, _isomorphicFetch2.default)(host + resource, args);
};
var get = function get(resource) {
var params = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
var headers = arguments.length <= 2 || arguments[2] === undefined ? {} : arguments[2];
var qs = query(params);
return request(queryURL(resource, qs), 'GET', {}, headers);
};
var put = function put(resource) {
var body = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
var headers = arguments.length <= 2 || arguments[2] === undefined ? {} : arguments[2];
return request(resource, 'PUT', body, headers);
};
var post = function post(resource) {
var body = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
var headers = arguments.length <= 2 || arguments[2] === undefined ? {} : arguments[2];
return request(resource, 'POST', body, headers);
};
var del = function del(resource) {
var params = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
var headers = arguments.length <= 2 || arguments[2] === undefined ? {} : arguments[2];
var qs = query(params);
return request(queryURL(resource, qs), 'DELETE', {}, headers);
};
return { request: request, get: get, put: put, post: post, del: del };
};
var _isomorphicFetch = require('isomorphic-fetch');
var _isomorphicFetch2 = _interopRequireDefault(_isomorphicFetch);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
exports.client = _client3.default;
exports.response = _response;

@@ -6,90 +6,17 @@ 'use strict';

});
exports.response = exports.client = undefined;
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; };
var _client2 = require('./client');
exports.default = function () {
var defaults = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0];
var _client3 = _interopRequireDefault(_client2);
var host = defaults.host || '';
var _response2 = require('./response');
var query = function query(params) {
var arr = [];
for (var p in params) {
if (params.hasOwnProperty(p)) {
arr.push(encodeURIComponent(p) + "=" + encodeURIComponent(params[p]));
}
}
var _response = _interopRequireWildcard(_response2);
return arr.length > 0 ? arr.join('&') : '';
};
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
var queryURL = function queryURL(resource, qs) {
return qs.length ? [resource, qs].join('?') : resource;
};
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var request = function request(resource, method) {
var body = arguments.length <= 2 || arguments[2] === undefined ? {} : arguments[2];
var headers = arguments.length <= 3 || arguments[3] === undefined ? {} : arguments[3];
method = method.toUpperCase();
var args = {
method: method,
headers: {}
};
for (var key in headers.headers) {
args.headers[key] = headers.headers[key];
}
for (var key in defaults.headers) {
args.headers[key] = defaults.headers[key];
}
if (!args.headers['Content-Type']) {
args.headers['Content-Type'] = 'application/json';
}
if (method != 'GET') args.body = (typeof body === 'undefined' ? 'undefined' : _typeof(body)) == 'object' ? JSON.stringify(body) : body;
return (0, _isomorphicFetch2.default)(host + resource, args);
};
var get = function get(resource) {
var params = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
var headers = arguments.length <= 2 || arguments[2] === undefined ? {} : arguments[2];
var qs = query(params);
return request(queryURL(resource, qs), 'GET', {}, headers);
};
var put = function put(resource) {
var body = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
var headers = arguments.length <= 2 || arguments[2] === undefined ? {} : arguments[2];
return request(resource, 'PUT', body, headers);
};
var post = function post(resource) {
var body = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
var headers = arguments.length <= 2 || arguments[2] === undefined ? {} : arguments[2];
return request(resource, 'POST', body, headers);
};
var del = function del(resource) {
var params = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
var headers = arguments.length <= 2 || arguments[2] === undefined ? {} : arguments[2];
var qs = query(params);
return request(queryURL(resource, qs), 'DELETE', {}, headers);
};
return { request: request, get: get, put: put, post: post, del: del };
};
var _isomorphicFetch = require('isomorphic-fetch');
var _isomorphicFetch2 = _interopRequireDefault(_isomorphicFetch);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
exports.client = _client3.default;
exports.response = _response;
{
"name": "http-otro",
"version": "0.0.1",
"version": "0.0.2",
"description": "Easily configurable basic http requests on top of isomorphic-fetch",

@@ -5,0 +5,0 @@ "main": "lib/index.js",

# http-otro
A configuration lib and caller for (isomorphic-fetch)[https://github.com/matthew-andrews/isomorphic-fetch]:
A configuration lib and caller for [isomorphic-fetch](https://github.com/matthew-andrews/isomorphic-fetch):
```
import http from 'http-otro'
import { client } from 'http-otro'
import { fetch } from 'isomorphic-fetch'
http({host: 'myhost', headers: {'Authorization': 'Bearer SomeToken'}}).get('/foo', {bar: 'baz'}) // Requests GET /foo?bar=baz
client({host: 'myhost', headers: {'Authorization': 'Bearer SomeToken'}}).get('/foo', {bar: 'baz'}) // Requests GET /foo?bar=baz
.then((res)=>{

@@ -15,8 +15,23 @@ //deal with response

Default `host` and `headers` can be provided via `http`, or you can just call `http().method`.
Default `host` and `headers` can be provided via `client`, or you can just call `client().method`.
For all methods, headers can be given via the last param. A `Content-Type` of `application/json` is the default for all requests.
# API
There are also *response handlers* which provide a shorthand for working with promises. For instance, the `json` handler allows you to write:
```
json(client().get('/foo'))
```
...instead of:
```
client().get('/foo').then((res)=>{
return res.json();
}).then((json)=>{
//Do something with json
});
```
# Client API
## request(resource, method, body={}, headers={})

@@ -35,1 +50,5 @@

## del(resource, body={}, headers={})
# Response Handling API
## json(request)

@@ -1,65 +0,2 @@

import fetch from 'isomorphic-fetch'
export default function(defaults={}){
let host = defaults.host || '';
const query = (params)=>{
var arr = [];
for(var p in params){
if (params.hasOwnProperty(p)) {
arr.push(encodeURIComponent(p) + "=" + encodeURIComponent(params[p]));
}
}
return (arr.length > 0) ? arr.join('&') : '';
}
const queryURL = (resource, qs)=>{
return qs.length ? [resource, qs].join('?') : resource;
}
const request = (resource, method, body={}, headers={})=>{
method = method.toUpperCase();
let args = {
method,
headers: {}
};
for(var key in headers.headers){
args.headers[key] = headers.headers[key];
}
for(var key in defaults.headers){
args.headers[key] = defaults.headers[key];
}
if(!args.headers['Content-Type']){
args.headers['Content-Type'] = 'application/json';
}
if(method != 'GET') args.body = typeof(body) == 'object' ? JSON.stringify(body) : body;
return fetch(host + resource, args);
}
const get = (resource, params={}, headers={})=>{
let qs = query(params);
return request(queryURL(resource, qs), 'GET', {}, headers);
}
const put = (resource, body={}, headers={})=>{
return request(resource, 'PUT', body, headers);
}
const post = (resource, body={}, headers={})=>{
return request(resource, 'POST', body, headers);
}
const del = (resource, params={}, headers={})=>{
let qs = query(params);
return request(queryURL(resource, qs), 'DELETE', {}, headers);
}
return { request, get, put, post, del };
}
export client from './client'
export * as response from './response'

@@ -19,2 +19,6 @@ import express from 'express'

let p = new Promise((resolve)=>{
app.get('/json', (req, res)=>{
res.json({foo: 'bar'});
});
app.post('/test', echoBody);

@@ -21,0 +25,0 @@

import expect from 'expect'
import http from '../src'
import { client, response } from '../src'
import testServer from '../test-server'

@@ -35,3 +35,3 @@

before((done)=>{
lib = http({host: url});
lib = client({host: url});
testServer().then(done);

@@ -55,2 +55,9 @@ })

});
it('Should handle a json response', (done)=>{
response.json(lib.get('/json')).then((json)=>{
expect(JSON.stringify(json)).toBe(JSON.stringify({foo: 'bar'}));
done();
});
});
})
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