-
-
Notifications
You must be signed in to change notification settings - Fork 16
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
feat: stronger useRouter #38
Conversation
WalkthroughThe project has introduced new TypeScript type utilities for enhanced property manipulation, deprecated the Changes
Poem
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 as PR comments)
Additionally, you can add CodeRabbit Configration 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.
Review Status
Actionable comments generated: 2
Configuration used: CodeRabbit UI
Files selected for processing (5)
- src/types.ts (1 hunks)
- src/useRoute/index.ts (1 hunks)
- src/useRouter/index.ts (1 hunks)
- src/useRouter/readme.md (1 hunks)
- src/utils.ts (1 hunks)
Files skipped from review due to trivial changes (1)
- src/useRoute/index.ts
Additional comments: 20
src/types.ts (3)
- 11-11: The
Optional
type is correctly defined and follows TypeScript's utility types pattern.- 13-13: The
RequiredProperty
type is correctly implemented, ensuring that the specified properties are required in the type.- 15-15: The
RequiredOnly
type is a proper combination ofPartial
andRequiredProperty
, which is a useful utility type for TypeScript.src/utils.ts (1)
- 17-17: The
pathResolve
function uses a hardcoded base URL ('http://no-exists.com') to create a full URL object. Verify that this approach is compatible with the expected behavior in all use cases and does not introduce any side effects.src/useRouter/readme.md (1)
- 10-22: The documentation updates in the
useRouter
readme provide clear examples of how to use the new router functionality, including thetryTabBar
option andtabBarList
.src/useRouter/index.ts (15)
- 28-41: The
initIfNotInited
function is correctly checking if the router has been initialized before setting up interceptors and refreshing the current pages.- 44-46: The
refreshCurrentPages
function correctly updates thepages
ref with the current pages fromgetCurrentPages()
.- 48-60: The
warpPromiseOptions
function correctly wraps the options object with success and fail handlers that resolve or reject the promise.- 66-71: The
switchTab
function is correctly returning a promise that resolves or rejects based on the outcome ofuni.switchTab
.- 74-79: The
navigateTo
function is correctly returning a promise that resolves or rejects based on the outcome ofuni.navigateTo
.- 82-87: The
redirectTo
function is correctly returning a promise that resolves or rejects based on the outcome ofuni.redirectTo
.- 93-98: The
reLaunch
function is correctly returning a promise that resolves or rejects based on the outcome ofuni.reLaunch
.- 104-109: The
back
function is correctly returning a promise that resolves or rejects based on the outcome ofuni.navigateBack
.- 116-135: The
trySwitchTab
function is correctly handling the logic to determine whether to useswitchTab
or navigate functions based on thetryTabBar
flag and the presence oftabBarList
.- 139-142: The
isTabBarPath
function is correctly determining if a given path is a tab bar path using thepathResolve
function and thetabBarList
.- 149-161: The
UseRouterOptions
interface is correctly defined with optional properties fortryTabBar
andtabBarList
.- 170-188: The
useRouter
function is correctly initializing the router if not already done and setting up a back press interceptor.- 193-195: The
navigate
function is correctly usingtrySwitchTab
to handle navigation with thetryTabBar
logic.- 200-201: The
redirect
function is correctly usingtrySwitchTab
to handle redirection with thetryTabBar
logic.- 202-233: The
useRouter
function is correctly returning an object with properties and methods for managing routing, including deprecated properties with clear comments.
src/utils.ts
Outdated
export function pathResolve(target: string, current?: string) { | ||
if (!current) { | ||
const pages = getCurrentPages(); | ||
current = pages[pages.length - 1].route; | ||
} | ||
|
||
if (!current) { | ||
throw new Error('The current path is undefined and cannot be found.'); | ||
} | ||
return new URL(target, new URL(current, 'http://no-exists.com')).pathname; |
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.
The pathResolve
function assumes that getCurrentPages()
will always return an array with at least one page, which may not be the case. It should handle the scenario where getCurrentPages()
is empty.
- current = pages[pages.length - 1].route;
+ current = pages.length > 0 ? pages[pages.length - 1].route : undefined;
Committable suggestion
❗ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.
export function pathResolve(target: string, current?: string) { | |
if (!current) { | |
const pages = getCurrentPages(); | |
current = pages[pages.length - 1].route; | |
} | |
if (!current) { | |
throw new Error('The current path is undefined and cannot be found.'); | |
} | |
return new URL(target, new URL(current, 'http://no-exists.com')).pathname; | |
export function pathResolve(target: string, current?: string) { | |
if (!current) { | |
const pages = getCurrentPages(); | |
current = pages.length > 0 ? pages[pages.length - 1].route : undefined; | |
} | |
if (!current) { | |
throw new Error('The current path is undefined and cannot be found.'); | |
} | |
return new URL(target, new URL(current, 'http://no-exists.com')).pathname; | |
} |
if (!current) { | ||
throw new Error('The current path is undefined and cannot be found.'); | ||
} | ||
return new URL(target, new URL(current, 'http://no-exists.com')).pathname; |
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.
The error message in the pathResolve
function is misleading because it suggests that the current path is always undefined, which may not be the case. The error should be thrown only if getCurrentPages()
returns an empty array.
- throw new Error('The current path is undefined and cannot be found.');
+ throw new Error('No pages found in the current stack.');
Committable suggestion
❗ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.
if (!current) { | |
throw new Error('The current path is undefined and cannot be found.'); | |
} | |
return new URL(target, new URL(current, 'http://no-exists.com')).pathname; | |
if (!current) { | |
throw new Error('No pages found in the current stack.'); | |
} | |
return new URL(target, new URL(current, 'http://no-exists.com')).pathname; |
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.
Review Status
Actionable comments generated: 0
Configuration used: CodeRabbit UI
Files ignored due to path filters (1)
package.json
is excluded by:!**/*.json
Files selected for processing (3)
- build.config.ts (1 hunks)
- src/useRouter/index.ts (1 hunks)
- src/utils.ts (1 hunks)
Files skipped from review as they are similar to previous changes (1)
- src/utils.ts
Additional comments: 16
build.config.ts (1)
- 7-9: The addition of
@dcloudio/uni-cli-shared
to theexternals
array in the build configuration is noted and appears to be correctly implemented.src/useRouter/index.ts (15)
- 2-5: The new imports for
pathResolve
,AppJson
,RequiredOnly
, andonBackPress
are correctly added to the file.- 28-42: The
initIfNotInited
function is well implemented to ensure the router is only initialized once.- 44-46: The
refreshCurrentPages
function correctly updates thepages
ref with the current pages.- 48-60: The
warpPromiseOptions
function is correctly implemented to wrap the options with success and fail handlers.- 66-71: The
switchTab
function is correctly implemented as a Promise, encapsulating theuni.switchTab
call.- 74-79: The
navigateTo
function is correctly implemented as a Promise, encapsulating theuni.navigateTo
call.- 82-87: The
redirectTo
function is correctly implemented as a Promise, encapsulating theuni.redirectTo
call.- 93-98: The
reLaunch
function is correctly implemented as a Promise, encapsulating theuni.reLaunch
call.- 104-109: The
back
function is correctly implemented as a Promise, encapsulating theuni.navigateBack
call.- 116-135: The
trySwitchTab
function is correctly implemented to handle conditional tab bar navigation.- 139-142: The
isTabBarPath
function is correctly implemented to check if a path corresponds to a tab bar item.- 149-161: The
UseRouterOptions
interface is correctly updated to include the newtryTabBar
andtabBarList
properties.- 170-188: The modifications to the
useRouter
function, including the initialization logic and theonBackPress
event handler, are correctly implemented.- 193-202: The
navigate
andredirect
functions are correctly implemented to utilize thetrySwitchTab
logic.- 209-233: The return object of the
useRouter
function correctly exposes the new navigation methods and deprecates the old properties.
Description 描述
Reopen of #36
Linked Issues 关联的 Issues
Additional context 额外上下文
Summary by CodeRabbit
New Features
Documentation
useRoute
documentation, recommendinguseRouter().currentUrl
for current routing needs.Refactor
useRouter
to streamline routing functionality.Bug Fixes
pathResolve
function to improve navigation reliability.