3 lines
4.8 KiB
JavaScript
3 lines
4.8 KiB
JavaScript
import{d as S}from"./constantes.js";import{r as F}from"./Either.js";const H="application/json",w="Content-Type",_=Symbol(),P=Symbol();function O(t={}){var e;return(e=Object.entries(t).find(([o])=>o.toLowerCase()===w.toLowerCase()))===null||e===void 0?void 0:e[1]}function R(t){return/^application\/.*json.*/.test(t)}const f=function(t,e,o=!1){return Object.entries(e).reduce((n,[i,l])=>{const h=t[i];return Array.isArray(h)&&Array.isArray(l)?n[i]=o?[...h,...l]:l:typeof h=="object"&&typeof l=="object"?n[i]=f(h,l,o):n[i]=l,n},{...t})},y={options:{},errorType:"text",polyfills:{},polyfill(t,e=!0,o=!1,...n){const i=this.polyfills[t]||(typeof self<"u"?self[t]:null)||(typeof global<"u"?global[t]:null);if(e&&!i)throw new Error(t+" is not defined");return o&&i?new i(...n):i}};function L(t,e=!1){y.options=e?t:f(y.options,t)}function N(t,e=!1){y.polyfills=e?t:f(y.polyfills,t)}function I(t){y.errorType=t}const J=t=>e=>t.reduceRight((o,n)=>n(o),e)||e;class x extends Error{}const U=t=>{const e=Object.create(null);t=t._addons.reduce((r,s)=>s.beforeRequest&&s.beforeRequest(r,t._options,e)||r,t);const{_url:o,_options:n,_config:i,_catchers:l,_resolvers:h,_middlewares:g,_addons:m}=t,p=new Map(l),D=f(i.options,n);let T=o;const j=J(g)((r,s)=>(T=r,i.polyfill("fetch")(r,s)))(o,D),b=new Error,v=j.catch(r=>{throw{[_]:r}}).then(r=>{if(!r.ok){const s=new x;if(s.cause=b,s.stack=s.stack+`
|
|
CAUSE: `+b.stack,s.response=r,s.url=T,r.type==="opaque")throw s;return r.text().then(a=>{var c;if(s.message=a,i.errorType==="json"||((c=r.headers.get("Content-Type"))===null||c===void 0?void 0:c.split(";")[0])==="application/json")try{s.json=JSON.parse(a)}catch{}throw s.text=a,s.status=r.status,s})}return r}),A=r=>r.catch(s=>{const a=s.hasOwnProperty(_),c=a?s[_]:s,E=c?.status&&p.get(c.status)||p.get(c?.name)||a&&p.has(_)&&p.get(_);if(E)return E(c,t);const C=p.get(P);if(C)return C(c,t);throw c}),d=r=>s=>A(r?v.then(a=>a&&a[r]()).then(a=>s?s(a):a):v.then(a=>s?s(a):a)),M={_wretchReq:t,_fetchReq:j,_sharedState:e,res:d(null),json:d("json"),blob:d("blob"),formData:d("formData"),arrayBuffer:d("arrayBuffer"),text:d("text"),error(r,s){return p.set(r,s),this},badRequest(r){return this.error(400,r)},unauthorized(r){return this.error(401,r)},forbidden(r){return this.error(403,r)},notFound(r){return this.error(404,r)},timeout(r){return this.error(408,r)},internalError(r){return this.error(500,r)},fetchError(r){return this.error(_,r)}},q=m.reduce((r,s)=>({...r,...typeof s.resolver=="function"?s.resolver(r):s.resolver}),M);return h.reduce((r,s)=>s(r,t),q)},k={_url:"",_options:{},_config:y,_catchers:new Map,_resolvers:[],_deferred:[],_middlewares:[],_addons:[],addon(t){return{...this,_addons:[...this._addons,t],...t.wretch}},errorType(t){return{...this,_config:{...this._config,errorType:t}}},polyfills(t,e=!1){return{...this,_config:{...this._config,polyfills:e?t:f(this._config.polyfills,t)}}},url(t,e=!1){if(e)return{...this,_url:t};const o=this._url.split("?");return{...this,_url:o.length>1?o[0]+t+"?"+o[1]:this._url+t}},options(t,e=!1){return{...this,_options:e?t:f(this._options,t)}},headers(t){const e=t?Array.isArray(t)?Object.fromEntries(t):"entries"in t?Object.fromEntries(t.entries()):t:{};return{...this,_options:f(this._options,{headers:e})}},accept(t){return this.headers({Accept:t})},content(t){return this.headers({[w]:t})},auth(t){return this.headers({Authorization:t})},catcher(t,e){const o=new Map(this._catchers);return o.set(t,e),{...this,_catchers:o}},catcherFallback(t){return this.catcher(P,t)},resolve(t,e=!1){return{...this,_resolvers:e?[t]:[...this._resolvers,t]}},defer(t,e=!1){return{...this,_deferred:e?[t]:[...this._deferred,t]}},middlewares(t,e=!1){return{...this,_middlewares:e?t:[...this._middlewares,...t]}},fetch(t=this._options.method,e="",o=null){let n=this.url(e).options({method:t});const i=O(n._options.headers),l=this._config.polyfill("FormData",!1),h=typeof o=="object"&&!(l&&o instanceof l)&&(!n._options.headers||!i||R(i));return n=o?h?n.json(o,i):n.body(o):n,U(n._deferred.reduce((g,m)=>m(g,g._url,g._options),n))},get(t=""){return this.fetch("GET",t)},delete(t=""){return this.fetch("DELETE",t)},put(t,e=""){return this.fetch("PUT",e,t)},post(t,e=""){return this.fetch("POST",e,t)},patch(t,e=""){return this.fetch("PATCH",e,t)},head(t=""){return this.fetch("HEAD",t)},opts(t=""){return this.fetch("OPTIONS",t)},body(t){return{...this,_options:{...this._options,body:t}}},json(t,e){const o=O(this._options.headers);return this.content(e||R(o)&&o||H).body(JSON.stringify(t))}};function u(t="",e={}){return{...k,_url:t,_options:e}}u.default=u;u.options=L;u.errorType=I;u.polyfills=N;u.WretchError=x;fetch(S,{credentials:"same-origin",headers:{"Content-Type":"application/json"},method:"GET",mode:"same-origin",signal:AbortSignal.timeout(5e3)}).then(F);u(S).content("application/json").options({credentials:"same-origin",mode:"same-origin",signal:AbortSignal.timeout(5e3)}).get().json().catch();
|
|
//# sourceMappingURL=api.js.map
|