-
Notifications
You must be signed in to change notification settings - Fork 6.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
perf: 优化 router/guard.ts #4402
perf: 优化 router/guard.ts #4402
Conversation
|
WalkthroughThe changes involve a significant refactoring of the access control logic in the Changes
Sequence Diagram(s)sequenceDiagram
participant User
participant Router
participant AccessGuard
participant AccessStore
User->>Router: Request Route
Router->>AccessGuard: Call setupAccessGuard
AccessGuard->>AccessGuard: handleBaseRoutes
AccessGuard->>AccessGuard: handleNoAccessToken
AccessGuard->>AccessStore: generateAndSetAccess
AccessStore-->>AccessGuard: Update Access
AccessGuard-->>Router: Redirect or Allow Access
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
Outside diff range and nitpick comments (3)
playground/src/router/guard.ts (3)
55-63
: Base route handling logic looks good!The function correctly handles the base route logic by checking if the target route is the login path and if an access token exists, redirecting appropriately. Using the
DEFAULT_HOME_PATH
constant for redirection is a good practice.Consider using more specific types for the
to
andaccessStore
parameters instead ofany
to improve type safety.
65-77
: No access token handling logic looks good!The function correctly handles scenarios where no access token is present by checking if access should be ignored based on route metadata and redirecting to the login page if necessary. Using route metadata to determine if access should be ignored is a good practice.
Consider using more specific types for the
to
andmeta
parameters instead ofany
to improve type safety.
79-94
: Access generation and setting logic looks good!The function correctly generates and sets access permissions by using the
generateAccess
function to generate accessible menus and routes based on user roles and routes, and updating the access store accordingly. Using thegenerateAccess
function for this purpose is a good practice.Consider using a more specific type for the
accessStore
parameter instead ofany
to improve type safety.
Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Files ignored due to path filters (1)
pnpm-lock.yaml
is excluded by!**/pnpm-lock.yaml
Files selected for processing (5)
- apps/web-antd/src/router/guard.ts (1 hunks)
- apps/web-ele/src/router/guard.ts (1 hunks)
- apps/web-naive/src/router/guard.ts (1 hunks)
- package.json (1 hunks)
- playground/src/router/guard.ts (1 hunks)
Additional comments not posted (14)
apps/web-antd/src/router/guard.ts (4)
56-63
: LGTM!The
handleBaseRoutes
function correctly handles the basic routes and access token checks. It safely decodes the redirect path from the query usingdecodeURIComponent
and returns the appropriate value based on the conditions.
66-77
: LGTM!The
handleNoAccessToken
function correctly handles the case when there is no access token. It checks if access should be ignored based on themeta.ignoreAccess
property and redirects to the login page with the original path encoded in the query usingencodeURIComponent
if necessary. The function returns the appropriate value based on the conditions.
80-94
: LGTM!The
generateAndSetAccess
function correctly generates and sets access permissions based on the user roles. It calls thegenerateAccess
function with the necessary parameters to retrieve the accessible menus and routes. The function updates the access store with the retrieved data and sets theisAccessChecked
flag to indicate that the access check has been performed.
Line range hint
97-126
: Great refactoring!The changes made to the
setupAccessGuard
function significantly improve the readability and maintainability of the access guard logic. The extraction of specific functionalities into separate helper functions (handleBaseRoutes
,handleNoAccessToken
, andgenerateAndSetAccess
) encapsulates responsibilities and makes the main guard function more concise and easier to understand.The refactoring enhances the code organization while preserving the original functionality of the access guard. The helper functions are well-defined and handle their respective tasks effectively.
Overall, the changes are a positive improvement to the codebase.
apps/web-ele/src/router/guard.ts (4)
56-63
: LGTM!The
handleBaseRoutes
function is well-structured and correctly handles the base routes based on the provided conditions. The function name and parameters are descriptive, making the code readable and maintainable.
66-77
: LGTM!The
handleNoAccessToken
function is well-structured and correctly handles the scenarios where no access token is available. It checks the metadata to determine if access should be ignored and redirects to the login page when necessary. The function name and parameters are descriptive, making the code readable and maintainable.
80-94
: LGTM!The
generateAndSetAccess
function is well-structured and correctly generates and sets the access permissions based on user roles. It utilizes thegenerateAccess
function to obtain the accessible menus and routes and updates the access store accordingly. The function name and parameters are descriptive, making the code readable and maintainable. The use ofawait
ensures that the asynchronous operation is handled correctly.
Line range hint
97-126
: Great refactoring!The refactored
setupAccessGuard
function is more readable and maintainable due to the separation of concerns into smaller, focused helper functions. The function correctly utilizes the new helper functions to handle base routes, no access token scenarios, and generating access permissions. The use ofawait
ensures that the asynchronous operations are handled correctly, and the function returns the appropriate redirects ortrue
based on the access conditions. This refactoring enhances the clarity and modularity of the access control logic.apps/web-naive/src/router/guard.ts (4)
56-63
: LGTM!The
handleBaseRoutes
function correctly handles the redirection logic for core routes. It checks if the route is the login path and an access token exists, then redirects to the original destination or the default home path. The use ofdecodeURIComponent
to safely decode the redirect query parameter is a good practice to prevent potential security issues.
66-77
: LGTM!The
handleNoAccessToken
function correctly handles the case when no access token is available. It checks if access should be ignored based on route metadata and redirects to the login page if necessary. The use ofencodeURIComponent
to safely encode the redirect query parameter is a good practice to prevent potential security issues.
80-94
: LGTM!The
generateAndSetAccess
function correctly generates and sets the accessible menus and routes based on user roles. It uses thegenerateAccess
function to generate the accessible menus and routes, updates the access store with the generated data, and marks access as checked. This function streamlines the process of setting up user permissions.
Line range hint
97-126
: LGTM!The
setupAccessGuard
function correctly sets up the access guard for the router. It uses thehandleBaseRoutes
,handleNoAccessToken
, andgenerateAndSetAccess
functions to handle different scenarios, which improves the maintainability and readability of the code. It also fetches the user info and roles if access is not checked, which is a good practice to ensure that the user has the necessary permissions.playground/src/router/guard.ts (1)
Line range hint
96-125
: Great job restructuring thesetupAccessGuard
function!The restructuring has significantly improved the readability and maintainability of the function. It now primarily orchestrates the access guard flow by calling the appropriate helper functions to handle base routes, no access token scenarios, and access generation.
Using the
coreRouteNames
array to check for core route names and therouter.resolve
method to resolve the redirect path are good practices.Overall, the changes have made the access guard logic easier to follow and maintain.
package.json (1)
79-79
: Dependency addition looks good!The
@vue/language-server
dependency has been added with an appropriate version constraint. This change aligns with the PR objective of enhancing the development experience for Vue.js applications.
Description
Type of change
Please delete options that are not relevant.
pnpm-lock.yaml
unless you introduce a new test example.Checklist
pnpm run docs:dev
command.pnpm test
.feat:
,fix:
,perf:
,docs:
, orchore:
.Summary by CodeRabbit
New Features
Bug Fixes
Chores