@frank-auth/react
Version:
Flexible and customizable React UI components for Frank Authentication
3 lines (2 loc) • 8.1 kB
JavaScript
;Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});var nr=Object.defineProperty,sr=(r,t,e)=>t in r?nr(r,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):r[t]=e,i=(r,t,e)=>sr(r,typeof t!="symbol"?t+"":t,e);class o extends Error{constructor(t,e="FRANK_AUTH_ERROR",n,O,m){super(t),i(this,"code"),i(this,"statusCode"),i(this,"details"),i(this,"timestamp"),i(this,"context"),this.name="FrankAuthError",this.code=e,this.statusCode=n,this.details=O,this.timestamp=new Date().toISOString(),this.context=m,Error.captureStackTrace&&Error.captureStackTrace(this,this.constructor)}toJSON(){return{code:this.code,message:this.message,details:this.details,statusCode:this.statusCode}}}class u extends o{constructor(t="Authentication failed",e){super(t,"AUTHENTICATION_ERROR",401,e,"authentication")}}class R extends o{constructor(t="Access denied",e){super(t,"AUTHORIZATION_ERROR",403,e,"authorization")}}class d extends o{constructor(t="Validation failed",e={},n){super(t,"VALIDATION_ERROR",400,n,"validation"),i(this,"fieldErrors"),this.fieldErrors=e}getFieldError(t){const e=this.fieldErrors[t];return e&&e.length>0?e[0]:void 0}hasFieldError(t){return!!this.fieldErrors[t]?.length}getAllFieldErrors(){return Object.values(this.fieldErrors).flat()}}class A extends o{constructor(t="Network request failed",e=!0,n){super(t,"NETWORK_ERROR",void 0,n,"network"),i(this,"isRetryable"),this.isRetryable=e}}class h extends o{constructor(t="Request timed out",e){super(t,"TIMEOUT_ERROR",408,e,"timeout")}}class f extends o{constructor(t="Rate limit exceeded",e,n){super(t,"RATE_LIMIT_ERROR",429,n,"rate_limit"),i(this,"retryAfter"),this.retryAfter=e}}class c extends o{constructor(t="Internal server error",e){super(t,"SERVER_ERROR",500,e,"server")}}class p extends o{constructor(t="Configuration error",e){super(t,"CONFIGURATION_ERROR",void 0,e,"configuration")}}class w extends o{constructor(t="Session error",e){super(t,"SESSION_ERROR",401,e,"session")}}class T extends o{constructor(t="Multi-factor authentication required",e,n){super(t,"MFA_ERROR",428,n,"mfa"),i(this,"challenge"),this.challenge=e}}class N extends o{constructor(t="Passkey operation failed",e){super(t,"PASSKEY_ERROR",void 0,e,"passkey")}}class I extends o{constructor(t="OAuth authentication failed",e,n,O){super(t,"OAUTH_ERROR",400,O,"oauth"),i(this,"provider"),i(this,"errorCode"),this.provider=e,this.errorCode=n}}class S extends o{constructor(t="Organization error",e){super(t,"ORGANIZATION_ERROR",400,e,"organization")}}class g extends o{constructor(t="Invitation error",e){super(t,"INVITATION_ERROR",400,e,"invitation")}}const C=(r,t,e)=>{switch(r){case"AUTHENTICATION_ERROR":return new u(t,e);case"AUTHORIZATION_ERROR":return new R(t,e);case"VALIDATION_ERROR":return new d(t,{},e);case"NETWORK_ERROR":return new A(t,!0,e);case"TIMEOUT_ERROR":return new h(t,e);case"RATE_LIMIT_ERROR":return new f(t,void 0,e);case"SERVER_ERROR":return new c(t,e);case"CONFIGURATION_ERROR":return new p(t,e);case"SESSION_ERROR":return new w(t,e);case"MFA_ERROR":return new T(t,void 0,e);case"PASSKEY_ERROR":return new N(t,e);case"OAUTH_ERROR":return new I(t,void 0,void 0,e);case"ORGANIZATION_ERROR":return new S(t,e);case"INVITATION_ERROR":return new g(t,e);default:return new o(t,r,void 0,e)}},M=(r,t)=>new u(r,t),P=(r,t)=>new R(r,t),V=(r,t,e)=>new d(r,t,e),H=(r,t,e)=>new A(r,t,e),L=(r,t)=>new w(r,t),z=(r,t,e)=>new T(r,t,e),s=r=>r instanceof o||r&&typeof r=="object"&&r.code&&r.message,K=r=>r instanceof u||s(r)&&r.code==="AUTHENTICATION_ERROR",B=r=>r instanceof R||s(r)&&r.code==="AUTHORIZATION_ERROR",D=r=>r instanceof d||s(r)&&r.code==="VALIDATION_ERROR",v=r=>r instanceof A||s(r)&&r.code==="NETWORK_ERROR",y=r=>r instanceof h||s(r)&&r.code==="TIMEOUT_ERROR",k=r=>r instanceof f||s(r)&&r.code==="RATE_LIMIT_ERROR",x=r=>r instanceof c||s(r)&&r.code==="SERVER_ERROR",W=r=>r instanceof w||s(r)&&r.code==="SESSION_ERROR",q=r=>r instanceof T||s(r)&&r.code==="MFA_ERROR",Z=r=>r instanceof N||s(r)&&r.code==="PASSKEY_ERROR",G=r=>r instanceof I||s(r)&&r.code==="OAUTH_ERROR",U=r=>v(r)?r.isRetryable:!!(y(r)||k(r)||x(r)),E=(r,t,e)=>s(r)?r:r instanceof Error?new o(r.message||e||"An error occurred","UNKNOWN_ERROR",void 0,{originalError:r.name},t):typeof r=="string"?new o(r||e||"An error occurred","UNKNOWN_ERROR",void 0,void 0,t):new o(e||"An unknown error occurred","UNKNOWN_ERROR",void 0,{originalError:r},t),$=r=>{if(!r)return new c("No response received");const{status:t,data:e}=r;switch(t){case 400:return e?.errors?new d(e.message||"Validation failed",e.errors,e):new o(e?.message||"Bad request","BAD_REQUEST",400,e);case 401:return new u(e?.message||"Authentication required",e);case 403:return new R(e?.message||"Access denied",e);case 404:return new o(e?.message||"Resource not found","NOT_FOUND",404,e);case 408:return new h(e?.message||"Request timeout",e);case 429:return new f(e?.message||"Rate limit exceeded",e?.retryAfter,e);case 500:case 502:case 503:case 504:return new c(e?.message||"Server error",e);default:return new o(e?.message||`HTTP ${t} error`,`HTTP_${t}`,t,e)}},Y=r=>s(r)||r instanceof Error?r.message:typeof r=="string"?r:"An unknown error occurred",j=r=>s(r)?r.code:r instanceof Error?r.name:"UNKNOWN_ERROR",J=r=>{if(s(r))return r.details},Q=()=>({error:(r,t,e)=>{console.error(r,t,e)},warn:(r,t)=>{console.warn(r,t)},info:(r,t)=>{console.info(r,t)},debug:(r,t)=>{console.debug(r,t)}}),X=(r,t=Q(),e)=>{const n=E(r);t.error(`[${n.code}] ${n.message}`,{error:n,stack:n.stack,timestamp:n.timestamp,context:n.context,...e})},rr=async(r,t={})=>{const{maxAttempts:e=3,delay:n=1e3,backoff:O="exponential",shouldRetry:m=U,onRetry:F}=t;let b;for(let a=1;a<=e;a++)try{return await r()}catch(_){if(b=_,a===e||!m(_,a))throw E(_);F&&F(_,a);let l=n;switch(O){case"exponential":l=n*Math.pow(2,a-1);break;case"linear":l=n*a;break;case"fixed":default:l=n;break}l+=Math.random()*1e3,await new Promise(or=>setTimeout(or,l))}throw E(b)},er=()=>({hasError:!1,error:void 0}),tr=(r,t)=>({hasError:!0,error:E(r,"error_boundary","Component error occurred")}),ir={FrankAuthBaseError:o,AuthenticationError:u,AuthorizationError:R,ValidationError:d,NetworkError:A,TimeoutError:h,RateLimitError:f,ServerError:c,ConfigurationError:p,SessionError:w,MFAError:T,PasskeyError:N,OAuthError:I,OrganizationError:S,InvitationError:g,createError:C,createAuthenticationError:M,createAuthorizationError:P,createValidationError:V,createNetworkError:H,createSessionError:L,createMFAError:z,isFrankAuthError:s,isAuthenticationError:K,isAuthorizationError:B,isValidationError:D,isNetworkError:v,isTimeoutError:y,isRateLimitError:k,isServerError:x,isSessionError:W,isMFAError:q,isPasskeyError:Z,isOAuthError:G,isRetryableError:U,handleError:E,parseAPIError:$,formatErrorMessage:Y,getErrorCode:j,getErrorDetails:J,logError:X,withRetry:rr,createErrorBoundaryState:er,handleErrorBoundaryError:tr};exports.AuthenticationError=u;exports.AuthorizationError=R;exports.ConfigurationError=p;exports.ErrorUtils=ir;exports.FrankAuthBaseError=o;exports.InvitationError=g;exports.MFAError=T;exports.NetworkError=A;exports.OAuthError=I;exports.OrganizationError=S;exports.PasskeyError=N;exports.RateLimitError=f;exports.ServerError=c;exports.SessionError=w;exports.TimeoutError=h;exports.ValidationError=d;exports.createAuthenticationError=M;exports.createAuthorizationError=P;exports.createConsoleLogger=Q;exports.createError=C;exports.createErrorBoundaryState=er;exports.createMFAError=z;exports.createNetworkError=H;exports.createSessionError=L;exports.createValidationError=V;exports.formatErrorMessage=Y;exports.getErrorCode=j;exports.getErrorDetails=J;exports.handleError=E;exports.handleErrorBoundaryError=tr;exports.isAuthenticationError=K;exports.isAuthorizationError=B;exports.isFrankAuthError=s;exports.isMFAError=q;exports.isNetworkError=v;exports.isOAuthError=G;exports.isPasskeyError=Z;exports.isRateLimitError=k;exports.isRetryableError=U;exports.isServerError=x;exports.isSessionError=W;exports.isTimeoutError=y;exports.isValidationError=D;exports.logError=X;exports.parseAPIError=$;exports.withRetry=rr;
//# sourceMappingURL=error.cjs.map