-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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: add resolvePath
export
#9949
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
388df56
feat: add `resolvePath` export for combining route IDs and params int…
elliott-with-the-longest-name-on-github e246021
changeset
elliott-with-the-longest-name-on-github 90c9642
Update packages/kit/src/exports/index.js
elliott-with-the-longest-name-on-github 2ea88f9
chore: Move things
elliott-with-the-longest-name-on-github 32541b2
Merge branch 'elliott/9934-build-path' of github.com:sveltejs/kit int…
elliott-with-the-longest-name-on-github ab7078b
Merge remote-tracking branch 'origin' into elliott/9934-build-path
elliott-with-the-longest-name-on-github 110d716
add resolvePath to types/index.d.ts
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 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 @@ | ||
--- | ||
'@sveltejs/kit': minor | ||
--- | ||
|
||
feat: add `resolvePath` export for building relative paths from route IDs and parameters |
This file contains 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 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 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,54 @@ | ||
import { assert, expect, test } from 'vitest'; | ||
import { resolvePath } from './index.js'; | ||
|
||
const from_params_tests = [ | ||
{ | ||
route: '/blog/[one]/[two]', | ||
params: { one: 'one', two: 'two' }, | ||
expected: '/blog/one/two' | ||
}, | ||
{ | ||
route: '/blog/[one=matcher]/[...two]', | ||
params: { one: 'one', two: 'two/three' }, | ||
expected: '/blog/one/two/three' | ||
}, | ||
{ | ||
route: '/blog/[one=matcher]/[[two]]', | ||
params: { one: 'one' }, | ||
expected: '/blog/one' | ||
}, | ||
{ | ||
route: '/blog/[one]/[two]-and-[three]', | ||
params: { one: 'one', two: '2', three: '3' }, | ||
expected: '/blog/one/2-and-3' | ||
}, | ||
{ | ||
route: '/blog/[one]/[...two]-not-three', | ||
params: { one: 'one', two: 'two/2' }, | ||
expected: '/blog/one/two/2-not-three' | ||
} | ||
]; | ||
|
||
for (const { route, params, expected } of from_params_tests) { | ||
test(`resolvePath generates correct path for ${route}`, () => { | ||
const result = resolvePath(route, params); | ||
assert.equal(result, expected); | ||
}); | ||
} | ||
|
||
test('resolvePath errors on missing params for required param', () => { | ||
expect(() => resolvePath('/blog/[one]/[two]', { one: 'one' })).toThrow( | ||
"Missing parameter 'two' in route /blog/[one]/[two]" | ||
); | ||
}); | ||
|
||
test('resolvePath errors on params values starting or ending with slashes', () => { | ||
assert.throws( | ||
() => resolvePath('/blog/[one]/[two]', { one: 'one', two: '/two' }), | ||
"Parameter 'two' in route /blog/[one]/[two] cannot start or end with a slash -- this would cause an invalid route like foo//bar" | ||
); | ||
assert.throws( | ||
() => resolvePath('/blog/[one]/[two]', { one: 'one', two: 'two/' }), | ||
"Parameter 'two' in route /blog/[one]/[two] cannot start or end with a slash -- this would cause an invalid route like foo//bar" | ||
); | ||
}); |
This file contains 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 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 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
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.
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.
Since we call it route id in other places I think it would make sense to reuse that nomenclature here
Alternatively even
resolveRouteId
.Thoughts?
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.
I think
resolvePath
is correct — we're resolving a path from a route ID + some parameters.That said,
resolvePath
sounds likepath.resolve
, i.e. you might expect to seeresolvePath(base, relative)
. I don't know if there's a good name for this that doesn't sound clunky and un-SvelteKit-like.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.
Yeah, that was my reasoning as well, Rich -- I think the current name is a good mix of what it does and brevity, and the example is pretty darn clear