UNPKG

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.16 kB
function e(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var t=/*#__PURE__*/e(require("return-fetch"));function r(){return r=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},r.apply(this,arguments)}module.exports=function(e){var n=t.default(e);return function(t,o){try{var a=new Headers(null==o?void 0:o.headers);return null!=o&&o.method&&"GET"!==o.method.toUpperCase()&&(a.get("Content-Type")||a.set("Content-Type","application/json")),a.get("Accept")||a.set("Accept","application/json"),Promise.resolve(n(t,r({},o,{headers:a,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.cjs.map