-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
fix(router-core): support parsed params in matchRoute #7776
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
Open
LadyBluenotes
wants to merge
7
commits into
main
Choose a base branch
from
match-route-parsed-params
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
35af8c8
reproduce matchRoute typed param failure
LadyBluenotes 2471c1c
reproduce matchRoute typed param failures
LadyBluenotes 22af4b8
enhance parameter parsing and matching with custom functions
LadyBluenotes 24d14d8
ci: apply automated fixes
autofix-ci[bot] 94c8373
changeset
LadyBluenotes f396cca
Merge branch 'match-route-parsed-params' of https://github.com/TanSta…
LadyBluenotes 24a76d9
updates as per comments
LadyBluenotes File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@tanstack/router-core': patch | ||
| --- | ||
|
|
||
| Fix matchRoute matching for custom parsed path parameters |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,277 @@ | ||
| import { describe, expect, it } from 'vitest' | ||
| import { createMemoryHistory } from '@tanstack/history' | ||
| import { BaseRootRoute, BaseRoute } from '../src' | ||
| import { createTestRouter } from './routerTestUtils' | ||
|
|
||
| describe('matchRoute', () => { | ||
| function createInvoiceRouter(initialEntry = '/invoices/123') { | ||
| const rootRoute = new BaseRootRoute({}) | ||
| const invoiceRoute = new BaseRoute({ | ||
| getParentRoute: () => rootRoute, | ||
| path: '/invoices/$invoiceId', | ||
| params: { | ||
| parse: ({ invoiceId }: { invoiceId: string }) => { | ||
| const parsed = Number(invoiceId) | ||
| return Number.isInteger(parsed) ? { invoiceId: parsed } : false | ||
| }, | ||
| stringify: ({ invoiceId }: { invoiceId: number }) => ({ | ||
| invoiceId: String(invoiceId), | ||
| }), | ||
| }, | ||
| }) | ||
|
|
||
| return createTestRouter({ | ||
| routeTree: rootRoute.addChildren([invoiceRoute]), | ||
| history: createMemoryHistory({ initialEntries: [initialEntry] }), | ||
| }) | ||
| } | ||
|
|
||
| it('matches typed params from routes with custom parse and stringify functions', async () => { | ||
| const router = createInvoiceRouter() | ||
|
|
||
| await router.load() | ||
|
|
||
| expect( | ||
| router.matchRoute({ | ||
| to: '/invoices/$invoiceId', | ||
| params: { invoiceId: 123 }, | ||
| }), | ||
| ).toEqual({ invoiceId: 123 }) | ||
| }) | ||
|
|
||
| it('does not match a different typed param', async () => { | ||
| const router = createInvoiceRouter() | ||
|
|
||
| await router.load() | ||
|
|
||
| expect( | ||
| router.matchRoute({ | ||
| to: '/invoices/$invoiceId', | ||
| params: { invoiceId: 456 }, | ||
| }), | ||
| ).toBe(false) | ||
| }) | ||
|
|
||
| it('does not match a lower-priority route', async () => { | ||
| const rootRoute = new BaseRootRoute({}) | ||
| const postRoute = new BaseRoute({ | ||
| getParentRoute: () => rootRoute, | ||
| path: '/posts/$postId', | ||
| params: { | ||
| parse: ({ postId }: { postId: string }) => ({ postId }), | ||
| }, | ||
| }) | ||
| const editRoute = new BaseRoute({ | ||
| getParentRoute: () => rootRoute, | ||
| path: '/posts/edit', | ||
| }) | ||
| const router = createTestRouter({ | ||
| routeTree: rootRoute.addChildren([postRoute, editRoute]), | ||
| history: createMemoryHistory({ initialEntries: ['/posts/edit'] }), | ||
| }) | ||
|
|
||
| await router.load() | ||
|
|
||
| expect( | ||
| router.matchRoute({ | ||
| to: '/posts/$postId', | ||
| params: { postId: 'edit' }, | ||
| }), | ||
| ).toBe(false) | ||
| }) | ||
|
|
||
| it('does not match params rejected by the route parser', async () => { | ||
| const router = createInvoiceRouter('/invoices/not-a-number') | ||
|
|
||
| await router.load() | ||
|
|
||
| expect( | ||
| router.matchRoute({ | ||
| to: '/invoices/$invoiceId', | ||
| params: { invoiceId: 123 }, | ||
| }), | ||
| ).toBe(false) | ||
| }) | ||
|
|
||
| it('does not throw parser errors while checking a match', async () => { | ||
| const rootRoute = new BaseRootRoute({}) | ||
| const invoiceRoute = new BaseRoute({ | ||
| getParentRoute: () => rootRoute, | ||
| path: '/invoices/$invoiceId', | ||
| params: { | ||
| parse: () => { | ||
| throw new Error('invalid invoice') | ||
| }, | ||
| stringify: ({ invoiceId }: { invoiceId: number }) => ({ | ||
| invoiceId: String(invoiceId), | ||
| }), | ||
| }, | ||
| }) | ||
| const router = createTestRouter({ | ||
| routeTree: rootRoute.addChildren([invoiceRoute]), | ||
| history: createMemoryHistory({ initialEntries: ['/invoices/123'] }), | ||
| }) | ||
|
|
||
| await router.load() | ||
|
|
||
| expect( | ||
| router.matchRoute({ | ||
| to: '/invoices/$invoiceId', | ||
| params: { invoiceId: 123 }, | ||
| }), | ||
| ).toBe(false) | ||
| }) | ||
|
|
||
| it('matches an internal ID parsed from a non-canonical public slug', async () => { | ||
| const rootRoute = new BaseRootRoute({}) | ||
| const thingRoute = new BaseRoute({ | ||
| getParentRoute: () => rootRoute, | ||
| path: '/things/$thingId', | ||
| params: { | ||
| parse: ({ thingId }: { thingId: string }) => ({ | ||
| thingId: thingId.slice(thingId.lastIndexOf('-') + 1), | ||
| }), | ||
| stringify: ({ thingId }: { thingId: string }) => ({ | ||
| thingId, | ||
| }), | ||
| }, | ||
| }) | ||
| const router = createTestRouter({ | ||
| routeTree: rootRoute.addChildren([thingRoute]), | ||
| history: createMemoryHistory({ | ||
| initialEntries: ['/things/Title-of-the-thing-abc123'], | ||
| }), | ||
| }) | ||
|
|
||
| await router.load() | ||
|
|
||
| expect( | ||
| router.matchRoute({ | ||
| to: '/things/$thingId', | ||
| params: { thingId: 'abc123' }, | ||
| }), | ||
| ).toEqual({ thingId: 'abc123' }) | ||
| }) | ||
|
|
||
| it('passes parsed parent params to child route parsers', async () => { | ||
| const rootRoute = new BaseRootRoute({}) | ||
| const orgRoute = new BaseRoute({ | ||
| getParentRoute: () => rootRoute, | ||
| path: '/orgs/$orgId', | ||
| params: { | ||
| parse: ({ orgId }: { orgId: string }) => ({ | ||
| orgId: Number(orgId), | ||
| }), | ||
| }, | ||
| }) | ||
| const projectRoute = new BaseRoute({ | ||
| getParentRoute: () => orgRoute, | ||
| path: '/projects/$projectId', | ||
| params: { | ||
| parse: (params: { projectId: string }) => { | ||
| if (typeof (params as Record<string, unknown>).orgId !== 'number') { | ||
| return false | ||
| } | ||
| return { projectId: Number(params.projectId) } | ||
| }, | ||
| }, | ||
| }) | ||
| const router = createTestRouter({ | ||
| routeTree: rootRoute.addChildren([orgRoute.addChildren([projectRoute])]), | ||
| history: createMemoryHistory({ | ||
| initialEntries: ['/orgs/42/projects/7'], | ||
| }), | ||
| }) | ||
|
|
||
| await router.load() | ||
|
|
||
| expect( | ||
| router.matchRoute({ | ||
| to: '/orgs/$orgId/projects/$projectId', | ||
| params: { orgId: 42, projectId: 7 }, | ||
| }), | ||
| ).toEqual({ orgId: 42, projectId: 7 }) | ||
| }) | ||
|
|
||
| it('keeps generic route templates working with string params', async () => { | ||
| const router = createInvoiceRouter() | ||
|
|
||
| await router.load() | ||
|
|
||
| expect( | ||
| router.matchRoute({ | ||
| to: '/invoices/$id', | ||
| params: { id: '123' }, | ||
| } as any), | ||
| ).toEqual({ id: '123' }) | ||
| }) | ||
|
|
||
| it('keeps fuzzy matching behavior with string params', async () => { | ||
| const router = createInvoiceRouter('/invoices/123/details') | ||
|
|
||
| await router.load() | ||
|
|
||
| expect( | ||
| router.matchRoute( | ||
| { | ||
| to: '/invoices/$invoiceId', | ||
| params: { invoiceId: 123 }, | ||
| }, | ||
| { fuzzy: true }, | ||
| ), | ||
| ).toEqual({ invoiceId: 123, '**': 'details' }) | ||
| }) | ||
|
|
||
| it('keeps search matching behavior', async () => { | ||
| const router = createInvoiceRouter('/invoices/123?tab=details') | ||
|
|
||
| await router.load() | ||
|
|
||
| expect( | ||
| router.matchRoute({ | ||
| to: '/invoices/$invoiceId', | ||
| params: { invoiceId: 123 }, | ||
| search: { tab: 'details' }, | ||
| }), | ||
| ).toEqual({ invoiceId: 123 }) | ||
| expect( | ||
| router.matchRoute({ | ||
| to: '/invoices/$invoiceId', | ||
| params: { invoiceId: 123 }, | ||
| search: { tab: 'other' }, | ||
| }), | ||
| ).toBe(false) | ||
| }) | ||
|
|
||
| it('matches typed params with a router basepath', async () => { | ||
| const rootRoute = new BaseRootRoute({}) | ||
| const invoiceRoute = new BaseRoute({ | ||
| getParentRoute: () => rootRoute, | ||
| path: '/invoices/$invoiceId', | ||
| params: { | ||
| parse: ({ invoiceId }: { invoiceId: string }) => ({ | ||
| invoiceId: Number(invoiceId), | ||
| }), | ||
| stringify: ({ invoiceId }: { invoiceId: number }) => ({ | ||
| invoiceId: String(invoiceId), | ||
| }), | ||
| }, | ||
| }) | ||
| const router = createTestRouter({ | ||
| routeTree: rootRoute.addChildren([invoiceRoute]), | ||
| basepath: '/app', | ||
| history: createMemoryHistory({ | ||
| initialEntries: ['/app/invoices/123'], | ||
| }), | ||
| }) | ||
|
|
||
| await router.load() | ||
|
|
||
| expect( | ||
| router.matchRoute({ | ||
| to: '/invoices/$invoiceId', | ||
| params: { invoiceId: 123 }, | ||
| }), | ||
| ).toEqual({ invoiceId: 123 }) | ||
| }) | ||
| }) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.