2 lines
3.2 KiB
JavaScript
2 lines
3.2 KiB
JavaScript
function f(r){return typeof r=="object"&&r!==null&&Object.prototype.hasOwnProperty.call(r,"data")&&Object.prototype.hasOwnProperty.call(r,"error")}function w(r){return r instanceof DOMException&&r.name==="AbortError"?new Error("api.error.timeouterror"):r instanceof TypeError&&r.message==="Failed to fetch"?new Error("api.error.connectionerror"):r instanceof Error?r:new Error(String(r))}class d{apiUrl;localStorage;constructor(t){this.apiUrl=t,this.localStorage=window.localStorage}async request(t,e,s,o=7e3,i=!1){const u={"Cache-Control":"no-cache"};if(i||(u["Content-Type"]="application/json"),this.localStorage){const n=this.localStorage.getItem("Auth-Token");n&&(u["Auth-Token"]=n)}const h=new AbortController,y=setTimeout(()=>h.abort(),o),l={method:t,cache:"no-store",mode:"cors",credentials:"include",headers:u,signal:h.signal};i?l.body=s:s!=null&&(l.body=JSON.stringify(s));try{const n=await fetch(e,l);if(!n.ok)throw new Error("api.error."+n.statusText);if(this.localStorage){const p=n.headers.get("Auth-Token");p&&this.localStorage.setItem("Auth-Token",p)}const c=await n.json();if(!f(c))throw new Error("api.error.wrongdatatype");if(c.error)throw new Error(c.error);return c}catch(n){throw w(n)}finally{clearTimeout(y)}}processResult(t){return typeof t.data!="object"?{data:t.data,error:null}:(t.data||(t.data={}),{data:t.data,error:null})}processError(t){const e=w(t);return e.message==="api.error.timeouterror"?(Object.defineProperty(e,"__api_error__",{value:e.message,writable:!1}),{data:null,error:e.message}):e.message==="api.error.connectionerror"?(Object.defineProperty(e,"__api_error__",{value:e.message,writable:!1}),{data:null,error:e.message}):{data:null,error:e.message}}async POST(t,e,s){try{const o=t.includes("/upload/"),i=await this.request("POST",this.apiUrl+t,e,s,o);return this.processResult(i)}catch(o){return this.processError(o)}}async GET(t,e){try{const s=await this.request("GET",this.apiUrl+t,null,e);return this.processResult(s)}catch(s){return this.processError(s)}}async PUT(t,e,s){try{const o=await this.request("PUT",this.apiUrl+t,e,s);return this.processResult(o)}catch(o){return this.processError(o)}}async DELETE(t,e){try{const s=await this.request("DELETE",this.apiUrl+t,null,e);return this.processResult(s)}catch(s){return this.processError(s)}}async UPLOAD(t,e,s){try{const o=await this.request("POST",this.apiUrl+t,e,s,!0);return this.processResult(o)}catch(o){return this.processError(o)}}}const a=new d("http://localhost:3000"),g={UserStatusPending:"pending",UserStatusActive:"active",UserStatusDisabled:"disabled"},E=async()=>await a.GET("/maildebug"),T=async r=>await a.POST("/admin/users",r),m=async(r,t)=>await a.PUT(`/admin/users/${r}/block`,t),S=async r=>await a.POST("/auth/register",r),P=async()=>await a.GET("/metrics"),U=async r=>await a.POST("/auth/login",r),b=async r=>await a.POST("/auth/refresh",r),O=async r=>await a.POST("/auth/password/forgot",r),j=async r=>await a.POST("/auth/password/reset",r),A=async()=>await a.GET("/health"),_=async()=>await a.GET("/auth/me"),R=async r=>await a.GET(`/users/${r}`),k=async r=>await a.POST("/users",r),q=async(r,t)=>await a.PUT(`/users/${r}`,t);export{g as E,j as a,P as b,b as c,T as d,E as e,O as f,k as g,A as h,m as i,R as j,U as l,_ as m,S as r,q as u};
|