return-fetch-json
Version:
An extended function of return-fetch ot serialize request body and deserialize response body as object.
3 lines (2 loc) • 937 B
JavaScript
import t from"return-fetch";function e(){return e=Object.assign?Object.assign.bind():function(t){for(var e=1;e<arguments.length;e++){var r=arguments[e];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(t[n]=r[n])}return t},e.apply(this,arguments)}const r=r=>{const n=t(r);return async(t,o)=>{const a=new Headers(null==o?void 0:o.headers);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");const s=await n(t,e({},o,{headers:a,body:(null==o?void 0:o.body)&&JSON.stringify(o.body)})),i=((t,e=JSON.parse)=>{try{return e(t)}catch(e){if("SyntaxError"!==e.name)throw e;return t.trim()}})(await s.text(),null==r?void 0:r.jsonParser);return{headers:s.headers,ok:s.ok,redirected:s.redirected,status:s.status,statusText:s.statusText,type:s.type,url:s.url,body:i}}};export{r as default};
//# sourceMappingURL=index.js.map