onetime-sdk
Version:
A secure SDK for sending and verifying OTPs via email and SMS.
2 lines • 1.53 kB
JavaScript
import C from"axios";var a=class extends Error{constructor(s,r=400){super(s),this.name="OnetimeClientError",this.statusCode=r}},p=class extends Error{constructor(s,r=500){super(s),this.name="OnetimeServerError",this.statusCode=r}},u=class extends Error{constructor(s){super(s),this.name="OnetimeNetworkError"}};var i,d=({apiKey:e,baseURL:s="https://api.onetime.dev",timeout:r=5e3,retries:O=2})=>(i=C.create({baseURL:s,timeout:r,headers:{Authorization:`Bearer ${e}`,"Content-Type":"application/json"}}),i.interceptors.response.use(void 0,async t=>{var c,l,f;let o=t.config;if((!o||!o.retryCount)&&(o.retryCount=0),(t.code==="ECONNABORTED"||t.message.includes("timeout")||typeof((c=t.response)==null?void 0:c.status)=="number"&&t.response.status>=500)&&o.retryCount<O)return o.retryCount+=1,await new Promise(n=>setTimeout(n,2**o.retryCount*100)),i(o);if(t.response){let n=t.response.data;if((l=t.response)!=null&&l.status&&t.response.status>=400&&t.response.status<500)throw new a((n==null?void 0:n.message)||"Client Error",t.response.status);if((f=t.response)!=null&&f.status&&t.response.status>=500)throw new p((n==null?void 0:n.message)||"Server Error",t.response.status)}throw new u(t.message||"Network Error")}),i),m=()=>{if(!i)throw new Error("OneTime SDK not initialized. Call initOTPClient() first.");return i};var y=async e=>(await m().post("/send-otp",e)).data,P=async e=>(await m().post("/verify-otp",e)).data;var N=e=>{d(e)};export{N as initOTPClient,y as sendOTP,P as verifyOTP};
//# sourceMappingURL=index.mjs.map