Skip to content

Commit

Permalink
feat: add support for unauthorized function
Browse files Browse the repository at this point in the history
  • Loading branch information
TheEdoRan committed Dec 11, 2024
1 parent 07a2059 commit 517fc51
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 0 deletions.
5 changes: 5 additions & 0 deletions packages/next-safe-action/src/action-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
isFrameworkError,
isNotFoundError,
isRedirectError,
isUnauthorizedError,
winningBoolean,
} from "./utils";
import {
Expand Down Expand Up @@ -310,6 +311,7 @@ export function actionBuilder<
hasRedirected: isRedirectError(frameworkError),
hasNotFound: isNotFoundError(frameworkError),
hasForbidden: isForbiddenError(frameworkError),
hasUnauthorized: isUnauthorizedError(frameworkError),
})
);

Expand All @@ -323,6 +325,7 @@ export function actionBuilder<
hasRedirected: isRedirectError(frameworkError),
hasNotFound: isNotFoundError(frameworkError),
hasForbidden: isForbiddenError(frameworkError),
hasUnauthorized: isUnauthorizedError(frameworkError),
})
);

Expand Down Expand Up @@ -372,6 +375,7 @@ export function actionBuilder<
hasRedirected: false,
hasNotFound: false,
hasForbidden: false,
hasUnauthorized: false,
})
);
} else {
Expand All @@ -397,6 +401,7 @@ export function actionBuilder<
hasRedirected: false,
hasNotFound: false,
hasForbidden: false,
hasUnauthorized: false,
})
);

Expand Down
2 changes: 2 additions & 0 deletions packages/next-safe-action/src/index.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ export type SafeActionUtils<
hasRedirected: boolean;
hasNotFound: boolean;
hasForbidden: boolean;
hasUnauthorized: boolean;
}) => Promise<unknown>;
onError?: (args: {
error: Prettify<Omit<SafeActionResult<ServerError, S, BAS, CVE, CBAVE, Data>, "data">>;
Expand All @@ -193,6 +194,7 @@ export type SafeActionUtils<
hasRedirected: boolean;
hasNotFound: boolean;
hasForbidden: boolean;
hasUnauthorized: boolean;
}) => Promise<unknown>;
};

Expand Down
4 changes: 4 additions & 0 deletions packages/next-safe-action/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };

0 comments on commit 517fc51

Please sign in to comment.