From 517fc51f61bcca7d394818eef4f123e278f40073 Mon Sep 17 00:00:00 2001 From: Edoardo Rsnghieri Date: Wed, 11 Dec 2024 11:46:24 +0100 Subject: [PATCH] feat: add support for `unauthorized` function --- packages/next-safe-action/src/action-builder.ts | 5 +++++ packages/next-safe-action/src/index.types.ts | 2 ++ packages/next-safe-action/src/utils.ts | 4 ++++ 3 files changed, 11 insertions(+) diff --git a/packages/next-safe-action/src/action-builder.ts b/packages/next-safe-action/src/action-builder.ts index fc55bbd..88c8b77 100644 --- a/packages/next-safe-action/src/action-builder.ts +++ b/packages/next-safe-action/src/action-builder.ts @@ -19,6 +19,7 @@ import { isFrameworkError, isNotFoundError, isRedirectError, + isUnauthorizedError, winningBoolean, } from "./utils"; import { @@ -310,6 +311,7 @@ export function actionBuilder< hasRedirected: isRedirectError(frameworkError), hasNotFound: isNotFoundError(frameworkError), hasForbidden: isForbiddenError(frameworkError), + hasUnauthorized: isUnauthorizedError(frameworkError), }) ); @@ -323,6 +325,7 @@ export function actionBuilder< hasRedirected: isRedirectError(frameworkError), hasNotFound: isNotFoundError(frameworkError), hasForbidden: isForbiddenError(frameworkError), + hasUnauthorized: isUnauthorizedError(frameworkError), }) ); @@ -372,6 +375,7 @@ export function actionBuilder< hasRedirected: false, hasNotFound: false, hasForbidden: false, + hasUnauthorized: false, }) ); } else { @@ -397,6 +401,7 @@ export function actionBuilder< hasRedirected: false, hasNotFound: false, hasForbidden: false, + hasUnauthorized: false, }) ); diff --git a/packages/next-safe-action/src/index.types.ts b/packages/next-safe-action/src/index.types.ts index 99d8eda..b78f2f6 100644 --- a/packages/next-safe-action/src/index.types.ts +++ b/packages/next-safe-action/src/index.types.ts @@ -176,6 +176,7 @@ export type SafeActionUtils< hasRedirected: boolean; hasNotFound: boolean; hasForbidden: boolean; + hasUnauthorized: boolean; }) => Promise; onError?: (args: { error: Prettify, "data">>; @@ -193,6 +194,7 @@ export type SafeActionUtils< hasRedirected: boolean; hasNotFound: boolean; hasForbidden: boolean; + hasUnauthorized: boolean; }) => Promise; }; diff --git a/packages/next-safe-action/src/utils.ts b/packages/next-safe-action/src/utils.ts index c0e5387..f58b00a 100644 --- a/packages/next-safe-action/src/utils.ts +++ b/packages/next-safe-action/src/utils.ts @@ -37,4 +37,8 @@ export function isForbiddenError(error: unknown): error is HTTPAccessFallbackErr return isHTTPAccessFallbackError(error) && getAccessFallbackHTTPStatus(error) === 403; } +export function isUnauthorizedError(error: unknown): error is HTTPAccessFallbackError { + return isHTTPAccessFallbackError(error) && getAccessFallbackHTTPStatus(error) === 401; +} + export { isRedirectError };