UNPKG

solid-fetch

Version:

Http request client promise-based supporting injectable request properties like: headers, parameters, query, body, etc.

2 lines (1 loc) 3.73 kB
"use strict";var N=Object.defineProperty;var V=s=>{throw TypeError(s)};var T=(s,e,t)=>e in s?N(s,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):s[e]=t;var p=(s,e,t)=>T(s,typeof e!="symbol"?e+"":e,t),C=(s,e,t)=>e.has(s)||V("Cannot "+t);var m=(s,e,t)=>(C(s,e,"read from private field"),t?t.call(s):e.get(s)),$=(s,e,t)=>e.has(s)?V("Cannot add the same private member more than once"):e instanceof WeakSet?e.add(s):e.set(s,t);var v,R;class w{constructor({initInjectables:e={},interceptedReq:t=[],interceptedRes:n=[],interceptedErr:l=[],globalQuery:c={},globalHeaders:f={}}){p(this,"injectables");p(this,"interceptedReq");p(this,"interceptedRes");p(this,"interceptedErr");p(this,"globalQuery");p(this,"globalHeaders");$(this,v,/\?$/);$(this,R,/\?.+$/);this.injectables=()=>e,this.interceptedReq=t,this.interceptedRes=n,this.interceptedErr=l,this.globalQuery=c,this.globalHeaders=f}setConfig(e){Object.keys(e).forEach(n=>{this[n]=e[n]})}setInjectables(e){const t=this.injectables();this.injectables=()=>({...t,...e})}getInjectables(){const e=this.injectables();return Object.keys(e).reduce((t,n)=>typeof e[n]=="function"?{...t,[n]:e[n]()}:{...t,[n]:e[n]},{})}resolveDynamic(e){const t=this.getInjectables();if(Array.isArray(e)){let l=[];return e.length>0&&(l=e.map(c=>typeof c=="function"?c(t):c)),l}return Object.keys(e).reduce((l,c)=>typeof e[c]=="function"?{...l,[c]:e[c](t)}:{...l,[c]:e[c]},{})}generateRequest(e,...t){const n=this.resolveDynamic(t);return e.reduce((c,f,u)=>{const q=`${f}${n[u]!==void 0?n[u]:""}`;return`${c}${q}`},"")}request(e,...t){const n=this.generateRequest(e,...t);return({method:l="GET",query:c={},headers:f={},body:u}={})=>{const q={...this.globalQuery,...typeof c=="function"?c(this.getInjectables()):c},F={...this.globalHeaders,...typeof f=="function"?f(this.getInjectables()):f};let O="";m(this,v).test(n)?O="":m(this,R).test(n)?O="&":O="?";const E=this.resolveDynamic(q),I=Object.keys(E).reduce((r,i,d,o)=>{const a=d+1<o.length,j=`${i}=${E[i]}`;return`${r}${j}${a?"&":""}`},O),D=`${n}${Object.keys(q).length>0?I:""}`,x=this.resolveDynamic(F),y={method:l,headers:{...x}};let h=u;typeof u=="function"?h=u(this.getInjectables()):u instanceof FormData||typeof u=="object"&&(h=this.resolveDynamic(u)),h instanceof FormData?y.body=h:typeof h=="object"?(y.headers["Content-Type"]="application/json",y.body=JSON.stringify(h)):typeof h=="string"&&(y.body=h);let b=D,g={...y};if(this.interceptedReq.length>0){const r={url:b,requestOptions:g},i=this.interceptedReq.reduce((d,o)=>{const a=o.action(d);return a!==null?a:d},r);b=i.url,g=i.requestOptions}return fetch(b,g).then(async r=>{const i=Object.fromEntries(r.headers.entries());if(r.status<200||r.status>=300){const o=new Error(JSON.stringify({name:"NoSuccess",message:"request resulted in error",response:{bodyUsed:r.bodyUsed,headers:i,ok:r.ok,redirected:r.redirected,status:r.status,statusText:r.statusText,url:r.url},request:{url:b,requestOptions:g}}));throw o.name="NoSuccess",o}const d={request:{url:b,requestOptions:g},headers:i,ok:r.ok,redirected:r.redirected,status:r.status,statusText:r.statusText,data:await(async()=>{if(i!=null&&i["content-type"]){if(i["content-type"].includes("application/json"))return r.json();if(i["content-type"].includes("text/plain"))return r.text()}return r.arrayBuffer()})()};return this.interceptedRes.length>0?this.interceptedRes.reduce((a,j)=>{const S=j.action(a);return S!==void 0?S:a},d):d}).catch(r=>{let i=r;throw r.name!=="NoSuccess"&&(i=new Error(JSON.stringify({name:r.name,message:r.message,request:{url:b,requestOptions:g}})),i.name=r.name),this.interceptedErr.length>0?this.interceptedErr.reduce((o,a)=>{const j=a.action(o);return j!==void 0?j:o},i):i})}}}v=new WeakMap,R=new WeakMap;module.exports=w;