UNPKG

vormiaqueryjs

Version:

Vormia Query Js - A npm package for query management with VormiaPHP laravel Backend application

3 lines (2 loc) 3.6 kB
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const t=require("./utils/encryption.js"),e=t=>{const e={request:async e=>{const s=e.url?new URL(e.url,e.baseURL||t.baseURL).toString():"",a=(t=>{const e={};return t?(Object.entries(t).forEach(([t,s])=>{Array.isArray(s)?e[t]=s.join(","):void 0!==s&&(e[t]=s)}),e):e})({"Content-Type":"application/json",Accept:"application/json",...t.headers,...e.headers});try{const r=await fetch(s,{method:e.method||"GET",headers:a,body:e.data?JSON.stringify(e.data):void 0,credentials:e.withCredentials||t.withCredentials?"include":"same-origin"}),n=await r.json().catch(()=>({}));if(!r.ok)throw new Error(JSON.stringify({message:n.message||"Request failed",status:r.status,response:{data:n,status:r.status,statusText:r.statusText,headers:{}}}));return{data:n,status:r.status,statusText:r.statusText,headers:r.headers,config:e}}catch(r){let t;try{t=JSON.parse(r.message)}catch{t={message:r.message,status:0,response:{data:{message:r.message},status:0,statusText:"",headers:{}}}}const e=new Error(t.message);throw e.status=t.status,e.response=t.response,e}}};["get","delete","head","options"].forEach(t=>{e[t]=(s,a={})=>e.request({...a,method:t.toUpperCase(),url:s})});return["post","put","patch"].forEach(t=>{e[t]=(s,a,r={})=>e.request({...r,method:t.toUpperCase(),url:s,data:a})}),e};class s{constructor(t){this.config={authTokenKey:"auth_token",withCredentials:!1,timeout:3e4,rsaEncrypt:!1,publicKey:process.env.VORMIA_PUBLIC_KEY,privateKey:process.env.VORMIA_PRIVATE_KEY,...t},this.http=e({baseURL:this.config.baseURL,headers:{"Content-Type":"application/json",Accept:"application/json",...this.config.headers},withCredentials:this.config.withCredentials,timeout:this.config.timeout})}async handleRequest(e){const s=this.getAuthToken();return s&&(e.headers={...e.headers,Authorization:`Bearer ${s}`}),e.encryptData&&this.config.encryptionKey&&(e.data&&(e.data={encrypted:t.encryptData(JSON.stringify(e.data),this.config.encryptionKey)}),e.params&&(e.params={encrypted:t.encryptData(JSON.stringify(e.params),this.config.encryptionKey)})),e}handleUnauthorized(){this.removeAuthToken(),this.config.onUnauthenticated&&this.config.onUnauthenticated()}getAuthToken(){return"undefined"!=typeof window?localStorage.getItem(this.config.authTokenKey):null}setAuthToken(t){"undefined"!=typeof window&&localStorage.setItem(this.config.authTokenKey,t)}removeAuthToken(){"undefined"!=typeof window&&localStorage.removeItem(this.config.authTokenKey)}async request(e){try{const s=await this.handleRequest({...e,headers:{...this.config.headers,...e.headers}});let a=s.data;this.config.rsaEncrypt&&a&&(a=t.encryptWithPublicKey(a,this.config.publicKey),s.data={encrypted:a});const r=await this.http.request(s);let n=r.data;return this.config.rsaEncrypt&&n&&n.encrypted&&(n=t.decryptWithPrivateKey(n.encrypted,this.config.privateKey),r.data=n),r}catch(s){throw 401===s.status&&this.handleUnauthorized(),s}}get(t,e){return this.request({...e,method:"GET",url:t})}post(t,e,s){return this.request({...s,method:"POST",url:t,data:e})}put(t,e,s){return this.request({...s,method:"PUT",url:t,data:e})}patch(t,e,s){return this.request({...s,method:"PATCH",url:t,data:e})}delete(t,e){return this.request({...e,method:"DELETE",url:t})}}let a=null;exports.VormiaClient=s,exports.createVormiaClient=function(t){return new s(t)},exports.getGlobalVormiaClient=function(){if(!a)throw new Error("Vormia client not initialized. Call createVormiaClient first.");return a},exports.setGlobalVormiaClient=function(t){a=t}; //# sourceMappingURL=createVormiaClient.js.map