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