return-fetch-json
Version:
An extended function of return-fetch ot serialize request body and deserialize response body as object.
3 lines (2 loc) • 1.37 kB
JavaScript
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t(require("return-fetch")):"function"==typeof define&&define.amd?define(["return-fetch"],t):(e||self).returnFetchJson=t(e.returnFetch)}(this,function(e){function t(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var r=/*#__PURE__*/t(e);function n(){return n=Object.assign?Object.assign.bind():function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(e[n]=r[n])}return e},n.apply(this,arguments)}return function(e){var t=r.default(e);return function(r,o){try{var u=new Headers(null==o?void 0:o.headers);return null!=o&&o.method&&"GET"!==o.method.toUpperCase()&&(u.get("Content-Type")||u.set("Content-Type","application/json")),u.get("Accept")||u.set("Accept","application/json"),Promise.resolve(t(r,n({},o,{headers:u,body:(null==o?void 0:o.body)&&JSON.stringify(o.body)}))).then(function(t){return Promise.resolve(t.text()).then(function(r){var n=function(e,t){void 0===t&&(t=JSON.parse);try{return t(e)}catch(t){if("SyntaxError"!==t.name)throw t;return e.trim()}}(r,null==e?void 0:e.jsonParser);return{headers:t.headers,ok:t.ok,redirected:t.redirected,status:t.status,statusText:t.statusText,type:t.type,url:t.url,body:n}})})}catch(e){return Promise.reject(e)}}}});
//# sourceMappingURL=index.umd.js.map