UNPKG

@clerk/nextjs

Version:

Clerk SDK for NextJS

80 lines 3.06 kB
import "../chunk-BUSYA2B4.js"; const CONTROL_FLOW_ERROR = { REDIRECT_TO_URL: "CLERK_PROTECT_REDIRECT_TO_URL", REDIRECT_TO_SIGN_IN: "CLERK_PROTECT_REDIRECT_TO_SIGN_IN" }; const LEGACY_NOT_FOUND_ERROR_CODE = "NEXT_NOT_FOUND"; function isLegacyNextjsNotFoundError(error) { if (typeof error !== "object" || error === null || !("digest" in error)) { return false; } return error.digest === LEGACY_NOT_FOUND_ERROR_CODE; } const HTTPAccessErrorStatusCodes = { NOT_FOUND: 404, FORBIDDEN: 403, UNAUTHORIZED: 401 }; const ALLOWED_CODES = new Set(Object.values(HTTPAccessErrorStatusCodes)); const HTTP_ERROR_FALLBACK_ERROR_CODE = "NEXT_HTTP_ERROR_FALLBACK"; function isHTTPAccessFallbackError(error) { if (typeof error !== "object" || error === null || !("digest" in error) || typeof error.digest !== "string") { return false; } const [prefix, httpStatus] = error.digest.split(";"); return prefix === HTTP_ERROR_FALLBACK_ERROR_CODE && ALLOWED_CODES.has(Number(httpStatus)); } function whichHTTPAccessFallbackError(error) { if (!isHTTPAccessFallbackError(error)) { return void 0; } const [, httpStatus] = error.digest.split(";"); return Number(httpStatus); } function isNextjsNotFoundError(error) { return isLegacyNextjsNotFoundError(error) || // Checks for the error thrown from `notFound()` for canary versions of next@15 whichHTTPAccessFallbackError(error) === HTTPAccessErrorStatusCodes.NOT_FOUND; } const REDIRECT_ERROR_CODE = "NEXT_REDIRECT"; function nextjsRedirectError(url, extra, type = "replace", statusCode = 307) { const error = new Error(REDIRECT_ERROR_CODE); error.digest = `${REDIRECT_ERROR_CODE};${type};${url};${statusCode};`; error.clerk_digest = CONTROL_FLOW_ERROR.REDIRECT_TO_URL; Object.assign(error, extra); throw error; } function redirectToSignInError(url, returnBackUrl) { nextjsRedirectError(url, { clerk_digest: CONTROL_FLOW_ERROR.REDIRECT_TO_SIGN_IN, returnBackUrl: returnBackUrl === null ? "" : returnBackUrl || url }); } function isNextjsRedirectError(error) { if (typeof error !== "object" || error === null || !("digest" in error) || typeof error.digest !== "string") { return false; } const digest = error.digest.split(";"); const [errorCode, type] = digest; const destination = digest.slice(2, -2).join(";"); const status = digest.at(-2); const statusCode = Number(status); return errorCode === REDIRECT_ERROR_CODE && (type === "replace" || type === "push") && typeof destination === "string" && !isNaN(statusCode) && statusCode === 307; } function isRedirectToSignInError(error) { if (isNextjsRedirectError(error) && "clerk_digest" in error) { return error.clerk_digest === CONTROL_FLOW_ERROR.REDIRECT_TO_SIGN_IN; } return false; } export { HTTP_ERROR_FALLBACK_ERROR_CODE, isHTTPAccessFallbackError, isLegacyNextjsNotFoundError, isNextjsNotFoundError, isNextjsRedirectError, isRedirectToSignInError, nextjsRedirectError, redirectToSignInError, whichHTTPAccessFallbackError }; //# sourceMappingURL=nextErrors.js.map