UNPKG

fetch-plugin

Version:

fetch polyfill with TIMEOUT setting and other options, extend from whatwg-fetch

2 lines (1 loc) 3.64 kB
"use strict";function t(){return(t=Object.assign||function(t){for(var e=1;e<arguments.length;e++){var n=arguments[e];for(var r in n)Object.prototype.hasOwnProperty.call(n,r)&&(t[r]=n[r])}return t}).apply(this,arguments)}require("whatwg-fetch");var e={"Content-Type":"application/json"},n={headers:new Headers(e),mode:"same-origin",credentials:"include",cache:"reload",redirect:"follow",timeout:3e4,fetchStart:function(t){return t}},r=function(){for(var r=arguments.length,o=new Array(r),c=0;c<r;c++)o[c]=arguments[c];var i=t.apply(void 0,[{}].concat(o)),u=t({},e,i.headers),a=null;return(a=t({},n,i)).headers=new Headers(u),{resultOptions:a,resultHealers:u}},o=function(t){return t.text().then((function(e){try{return JSON.parse(e)}catch(n){throw new Error("JSON Parse Error: ".concat(n,", URL: ").concat(t.url,", ").concat(e.slice(0,500)))}}))},c=function(t){if(t.status>=200&&t.status<300||304==t.status)return t;throw new Error("HTTP Status Code: ".concat(t.status,", URL: ").concat(t.url))},i=function(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};if("[object Object]"!==Object.prototype.toString.call(e)||0===Object.keys(e).length)return t;var n=[];for(var r in e)n.push("".concat(encodeURIComponent(r),"=").concat(encodeURIComponent(e[r])));return t+(-1===t.indexOf("?")?"?":"")+n.join("&")},u=function(t){return"function"==typeof n.fetchSuccess&&n.fetchSuccess(t),t},a=function(t){throw"function"==typeof n.fetchError&&n.fetchError(t),t=t instanceof Error?t:new Error(t)},s=function(t,e){return new Promise((function(n,r){var o=0;Promise.resolve(e.fetchStart({url:t,fetchOption:e})).then((function(t){if(!1===t){var e=new Error("".concat(t.url," cancel"));e.fetchOption=t.fetchOption,r(e)}var n=new Request(t.url,t.fetchOption);return o=setTimeout((function(){var e=new Error("".concat(t.url," timeout"));e.fetchOption=t.fetchOption,r(e)}),t.fetchOption.timeout),fetch(n)})).then((function(t){clearTimeout(o),t.fetchOption=e,n(t)}),(function(n){clearTimeout(o),n.url=t,n.fetchOption=e,r(n)}))})).then(c)},h={setOptions:function(t){n=r(t).resultOptions,e=r(t).resultHealers},getJSONP:function(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{},r="jsonp"+ +new Date,o=document.createElement("script");e[n.callbackName||"_callback"]=r;var c=i(t,e),u=document.head||document.querySelector("head")||document.documentElement;return o.setAttribute("src",c),o.setAttribute("charset","utf-8"),o.setAttribute("defer",!0),o.setAttribute("async",!0),u.insertBefore(o,u.firstChild),new Promise((function(t,e){window[r]=function(e){t(e),u.removeChild(o)},o.onerror=function(){e(),u.removeChild(o)}}))},getJSON:function(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{},c=r({method:"GET"},n).resultOptions,h=i(t,e);return s(h,c).then(o).then(u,a)},postJSON:function(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{},c=r({method:"POST",body:JSON.stringify(e)},n).resultOptions,i=t;return s(i,c).then(o).then(u,a)},putJSON:function(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{},c=r({method:"PUT",body:JSON.stringify(e)},n).resultOptions,i=t;return s(i,c).then(o).then(u,a)},deleteJSON:function(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{},c=r({method:"DELETE"},n).resultOptions,h=i(t,e);return s(h,c).then(o).then(u,a)}};module.exports=h;