-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add
resolvePath
export (#9949)
* feat: add `resolvePath` export for combining route IDs and params into a relative path * changeset * Update packages/kit/src/exports/index.js Co-authored-by: Rich Harris <richard.a.harris@gmail.com> * chore: Move things * add resolvePath to types/index.d.ts --------- Co-authored-by: Rich Harris <richard.a.harris@gmail.com> Co-authored-by: Rich Harris <git@rich-harris.dev>
- Loading branch information
1 parent
35eafa9
commit 195e9ac
Showing
7 changed files
with
123 additions
and
93 deletions.
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
195e9ac
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.
@Rich-Harris Is there any way to include the host in entries? I'm trying to figure out the best way to prerender pages in my "platforms" style application. Long story short, I use the host in hooks.server.js to determine which components to render on each page. I saw this commit and felt like it was a good place to bring this up, though it should probably live in a feature request unless you have a better idea of how to handle this. Thanks!