Comparing version 0.0.4 to 0.0.5
@@ -94,2 +94,8 @@ (function webpackUniversalModuleDefinition(root, factory) { | ||
var context = {}; | ||
if (!defaults.headers) defaults.headers = {}; | ||
context.defaults = defaults; | ||
var host = defaults.host || ''; | ||
@@ -112,3 +118,3 @@ | ||
var request = function request(resource, method) { | ||
context.request = function (resource, method) { | ||
var body = arguments.length <= 2 || arguments[2] === undefined ? {} : arguments[2]; | ||
@@ -125,3 +131,3 @@ var options = arguments.length <= 3 || arguments[3] === undefined ? {} : arguments[3]; | ||
//Options & defaults | ||
for (var key in defaults) { | ||
for (var key in context.defaults) { | ||
if (key != 'headers') { | ||
@@ -144,3 +150,3 @@ args[key] = defaults[key]; | ||
for (var key in defaults.headers) { | ||
for (var key in context.defaults.headers) { | ||
args.headers[key] = defaults.headers[key]; | ||
@@ -158,3 +164,3 @@ } | ||
var get = function get(resource) { | ||
context.get = function (resource) { | ||
var params = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1]; | ||
@@ -164,20 +170,20 @@ var options = arguments.length <= 2 || arguments[2] === undefined ? {} : arguments[2]; | ||
var qs = query(params); | ||
return request(queryURL(resource, qs), 'GET', {}, options); | ||
return context.request(queryURL(resource, qs), 'GET', {}, options); | ||
}; | ||
var put = function put(resource) { | ||
context.put = function (resource) { | ||
var body = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1]; | ||
var options = arguments.length <= 2 || arguments[2] === undefined ? {} : arguments[2]; | ||
return request(resource, 'PUT', body, options); | ||
return context.request(resource, 'PUT', body, options); | ||
}; | ||
var post = function post(resource) { | ||
context.post = function (resource) { | ||
var body = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1]; | ||
var options = arguments.length <= 2 || arguments[2] === undefined ? {} : arguments[2]; | ||
return request(resource, 'POST', body, options); | ||
return context.request(resource, 'POST', body, options); | ||
}; | ||
var del = function del(resource) { | ||
context.del = function (resource) { | ||
var params = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1]; | ||
@@ -187,6 +193,6 @@ var options = arguments.length <= 2 || arguments[2] === undefined ? {} : arguments[2]; | ||
var qs = query(params); | ||
return request(queryURL(resource, qs), 'DELETE', {}, options); | ||
return context.request(queryURL(resource, qs), 'DELETE', {}, options); | ||
}; | ||
return { request: request, get: get, put: put, post: post, del: del, fetch: _isomorphicFetch2.default }; | ||
return context; | ||
}; | ||
@@ -193,0 +199,0 @@ |
@@ -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){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),a=r(2),u=o(a);e.client=s.default,e.response=u},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 f in t)"headers"!=f&&(u[f]=t[f]);for(var f in a)"headers"!=f&&(u[f]=a[f]);for(var f in a.headers)u.headers[f]=a.headers[f];for(var f in t.headers)u.headers[f]=t.headers[f];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)},f=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)},d=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:f,del:d,fetch:s.default}};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 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(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 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)},p.formData&&(this.formData=function(){return this.text().then(h)}),this.json=function(){return this.text().then(JSON.parse)},this}function f(t){var e=t.toUpperCase();return y.indexOf(e)>-1?e:t}function d(t,e){e=e||{};var r=e.body;if(d.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=f(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"];d.prototype.clone=function(){return new d(this)},u.call(d.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=d,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=d.prototype.isPrototypeOf(t)&&!e?t:new d(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)}])}); | ||
!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),a=r(2),u=o(a);e.client=s.default,e.response=u},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.headers||(t.headers={}),e.defaults=t;var r=t.host||"",o=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("&"):""},i=function(t,e){return e.length?[t,e].join("?"):t};return e.request=function(o,i){var a=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{},u=arguments.length>3&&void 0!==arguments[3]?arguments[3]:{};i=i.toUpperCase();var d={method:i,headers:{}};for(var f in e.defaults)"headers"!=f&&(d[f]=t[f]);for(var f in u)"headers"!=f&&(d[f]=u[f]);for(var f in u.headers)d.headers[f]=u.headers[f];for(var f in e.defaults.headers)d.headers[f]=t.headers[f];return d.headers["Content-Type"]||(d.headers["Content-Type"]="application/json"),"GET"!=i&&(d.body="object"==(void 0===a?"undefined":n(a))?JSON.stringify(a):a),(0,s.default)(r+o,d)},e.get=function(t){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{},s=o(r);return e.request(i(t,s),"GET",{},n)},e.put=function(t){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},o=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{};return e.request(t,"PUT",r,o)},e.post=function(t){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},o=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{};return e.request(t,"POST",r,o)},e.del=function(t){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{},s=o(r);return e.request(i(t,s),"DELETE",{},n)},e};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 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(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 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)},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 l(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 c(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)},u.call(f.prototype),u.call(c.prototype),c.prototype.clone=function(){return new c(this._bodyInit,{status:this.status,statusText:this.statusText,headers:new o(this.headers),url:this.url})},c.error=function(){var t=new c(null,{status:0,statusText:""});return t.type="error",t};var b=[301,302,303,307,308];c.redirect=function(t,e){if(-1===b.indexOf(e))throw new RangeError("Invalid status code");return new c(null,{status:e,headers:{location:t}})},t.Headers=o,t.Request=f,t.Response=c,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:l(s),url:n()},i="response"in s?s.response:s.responseText;r(new c(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)}])}); |
@@ -12,2 +12,8 @@ 'use strict'; | ||
var context = {}; | ||
if (!defaults.headers) defaults.headers = {}; | ||
context.defaults = defaults; | ||
var host = defaults.host || ''; | ||
@@ -30,3 +36,3 @@ | ||
var request = function request(resource, method) { | ||
context.request = function (resource, method) { | ||
var body = arguments.length <= 2 || arguments[2] === undefined ? {} : arguments[2]; | ||
@@ -43,3 +49,3 @@ var options = arguments.length <= 3 || arguments[3] === undefined ? {} : arguments[3]; | ||
//Options & defaults | ||
for (var key in defaults) { | ||
for (var key in context.defaults) { | ||
if (key != 'headers') { | ||
@@ -62,3 +68,3 @@ args[key] = defaults[key]; | ||
for (var key in defaults.headers) { | ||
for (var key in context.defaults.headers) { | ||
args.headers[key] = defaults.headers[key]; | ||
@@ -76,3 +82,3 @@ } | ||
var get = function get(resource) { | ||
context.get = function (resource) { | ||
var params = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1]; | ||
@@ -82,20 +88,20 @@ var options = arguments.length <= 2 || arguments[2] === undefined ? {} : arguments[2]; | ||
var qs = query(params); | ||
return request(queryURL(resource, qs), 'GET', {}, options); | ||
return context.request(queryURL(resource, qs), 'GET', {}, options); | ||
}; | ||
var put = function put(resource) { | ||
context.put = function (resource) { | ||
var body = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1]; | ||
var options = arguments.length <= 2 || arguments[2] === undefined ? {} : arguments[2]; | ||
return request(resource, 'PUT', body, options); | ||
return context.request(resource, 'PUT', body, options); | ||
}; | ||
var post = function post(resource) { | ||
context.post = function (resource) { | ||
var body = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1]; | ||
var options = arguments.length <= 2 || arguments[2] === undefined ? {} : arguments[2]; | ||
return request(resource, 'POST', body, options); | ||
return context.request(resource, 'POST', body, options); | ||
}; | ||
var del = function del(resource) { | ||
context.del = function (resource) { | ||
var params = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1]; | ||
@@ -105,6 +111,6 @@ var options = arguments.length <= 2 || arguments[2] === undefined ? {} : arguments[2]; | ||
var qs = query(params); | ||
return request(queryURL(resource, qs), 'DELETE', {}, options); | ||
return context.request(queryURL(resource, qs), 'DELETE', {}, options); | ||
}; | ||
return { request: request, get: get, put: put, post: post, del: del, fetch: _isomorphicFetch2.default }; | ||
return context; | ||
}; | ||
@@ -111,0 +117,0 @@ |
@@ -12,2 +12,8 @@ 'use strict'; | ||
var context = {}; | ||
if (!defaults.headers) defaults.headers = {}; | ||
context.defaults = defaults; | ||
var host = defaults.host || ''; | ||
@@ -30,3 +36,3 @@ | ||
var request = function request(resource, method) { | ||
context.request = function (resource, method) { | ||
var body = arguments.length <= 2 || arguments[2] === undefined ? {} : arguments[2]; | ||
@@ -43,3 +49,3 @@ var options = arguments.length <= 3 || arguments[3] === undefined ? {} : arguments[3]; | ||
//Options & defaults | ||
for (var key in defaults) { | ||
for (var key in context.defaults) { | ||
if (key != 'headers') { | ||
@@ -62,3 +68,3 @@ args[key] = defaults[key]; | ||
for (var key in defaults.headers) { | ||
for (var key in context.defaults.headers) { | ||
args.headers[key] = defaults.headers[key]; | ||
@@ -76,3 +82,3 @@ } | ||
var get = function get(resource) { | ||
context.get = function (resource) { | ||
var params = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1]; | ||
@@ -82,20 +88,20 @@ var options = arguments.length <= 2 || arguments[2] === undefined ? {} : arguments[2]; | ||
var qs = query(params); | ||
return request(queryURL(resource, qs), 'GET', {}, options); | ||
return context.request(queryURL(resource, qs), 'GET', {}, options); | ||
}; | ||
var put = function put(resource) { | ||
context.put = function (resource) { | ||
var body = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1]; | ||
var options = arguments.length <= 2 || arguments[2] === undefined ? {} : arguments[2]; | ||
return request(resource, 'PUT', body, options); | ||
return context.request(resource, 'PUT', body, options); | ||
}; | ||
var post = function post(resource) { | ||
context.post = function (resource) { | ||
var body = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1]; | ||
var options = arguments.length <= 2 || arguments[2] === undefined ? {} : arguments[2]; | ||
return request(resource, 'POST', body, options); | ||
return context.request(resource, 'POST', body, options); | ||
}; | ||
var del = function del(resource) { | ||
context.del = function (resource) { | ||
var params = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1]; | ||
@@ -105,6 +111,6 @@ var options = arguments.length <= 2 || arguments[2] === undefined ? {} : arguments[2]; | ||
var qs = query(params); | ||
return request(queryURL(resource, qs), 'DELETE', {}, options); | ||
return context.request(queryURL(resource, qs), 'DELETE', {}, options); | ||
}; | ||
return { request: request, get: get, put: put, post: post, del: del, fetch: _isomorphicFetch2.default }; | ||
return context; | ||
}; | ||
@@ -111,0 +117,0 @@ |
{ | ||
"name": "http-otro", | ||
"version": "0.0.4", | ||
"version": "0.0.5", | ||
"description": "Easily configurable basic http requests on top of isomorphic-fetch", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
import fetch from 'isomorphic-fetch' | ||
export default function(defaults={}){ | ||
var context = {}; | ||
if(!defaults.headers) defaults.headers = {}; | ||
context.defaults = defaults; | ||
let host = defaults.host || ''; | ||
@@ -21,3 +27,3 @@ | ||
const request = (resource, method, body={}, options={})=>{ | ||
context.request = (resource, method, body={}, options={})=>{ | ||
method = method.toUpperCase(); | ||
@@ -31,3 +37,3 @@ | ||
//Options & defaults | ||
for(var key in defaults){ | ||
for(var key in context.defaults){ | ||
if(key != 'headers'){ | ||
@@ -50,3 +56,3 @@ args[key] = defaults[key]; | ||
for(var key in defaults.headers){ | ||
for(var key in context.defaults.headers){ | ||
args.headers[key] = defaults.headers[key]; | ||
@@ -64,21 +70,21 @@ } | ||
const get = (resource, params={}, options={})=>{ | ||
context.get = (resource, params={}, options={})=>{ | ||
let qs = query(params); | ||
return request(queryURL(resource, qs), 'GET', {}, options); | ||
return context.request(queryURL(resource, qs), 'GET', {}, options); | ||
} | ||
const put = (resource, body={}, options={})=>{ | ||
return request(resource, 'PUT', body, options); | ||
context.put = (resource, body={}, options={})=>{ | ||
return context.request(resource, 'PUT', body, options); | ||
} | ||
const post = (resource, body={}, options={})=>{ | ||
return request(resource, 'POST', body, options); | ||
context.post = (resource, body={}, options={})=>{ | ||
return context.request(resource, 'POST', body, options); | ||
} | ||
const del = (resource, params={}, options={})=>{ | ||
context.del = (resource, params={}, options={})=>{ | ||
let qs = query(params); | ||
return request(queryURL(resource, qs), 'DELETE', {}, options); | ||
return context.request(queryURL(resource, qs), 'DELETE', {}, options); | ||
} | ||
return { request, get, put, post, del, fetch }; | ||
return context; | ||
} |
@@ -61,2 +61,10 @@ import expect from 'expect' | ||
}); | ||
it('Should allow us to change defaults dynamically, affecting headers', (done)=>{ | ||
let c = client({host: url}); | ||
expect(JSON.stringify(c.defaults.headers)).toBe("{}"); | ||
c.defaults.headers['foo'] = 'bar'; | ||
expect(c.defaults.headers.foo).toBe('bar'); | ||
done(); | ||
}); | ||
}) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
45547
969
1