Skip to content
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 7 commits into from
May 18, 2023
Merged

feat: add resolvePath export #9949

merged 7 commits into from
May 18, 2023

Conversation

elliott-with-the-longest-name-on-github
Copy link
Contributor

Closes #9934.

Adds a resolvePath export for building relative paths from route IDs and params:

resolvePath(
  `/blog/[slug]/[...somethingElse]`,
  {
    slug: 'hello-world',
    somethingElse: 'something/else'
  }
); // `/blog/hello-world/something/else`

Please don't delete this checklist! Before submitting the PR, please make sure you do the following:

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.

Tests

  • Run the tests with pnpm test and lint the project with pnpm lint and pnpm check

Changesets

  • If your PR makes a change that should be noted in one or more packages' changelogs, generate a changeset by running pnpm changeset and following the prompts. Changesets that add features should be minor and those that fix bugs should be patch. Please prefix changeset messages with feat:, fix:, or chore:.

@changeset-bot
Copy link

changeset-bot bot commented May 17, 2023

🦋 Changeset detected

Latest commit: 110d716

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@sveltejs/kit Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

packages/kit/src/exports/index.js Outdated Show resolved Hide resolved
* @param {Record<string, string>} params
* @returns {string}
*/
export function resolvePath(id, params) {
Copy link
Member

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

Suggested change
export function resolvePath(id, params) {
export function resolveRoute(id, params) {

Alternatively even resolveRouteId.

Thoughts?

Copy link
Member

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 like path.resolve, i.e. you might expect to see resolvePath(base, relative). I don't know if there's a good name for this that doesn't sound clunky and un-SvelteKit-like.

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

* @returns {string}
*/
export function resolvePath(id, params) {
return resolve_entry(id, params);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rather than calling the function indirectly like this, we should either a) re-export it or b) put the implementation here, and use that internally instead of resolve_entry (probably better, though a more significant change)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, let's put the implementation here. Makes it more clear that changing the implementation affects the public API.

Copy link
Member

@Rich-Harris Rich-Harris left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ugh i just realised that this doesn't show up in the docs, because we also need to update ambient.d.ts

this fuels my desire to build the missing library for generating .d.ts bundles with declare module from source #9895 (comment)

@Rich-Harris
Copy link
Member

ah i tell a lie, it's not ambient.d.ts, it's types/index.d.ts which is even worse

@Rich-Harris
Copy link
Member

Why is the second argument optional?

@elliott-with-the-longest-name-on-github
Copy link
Contributor Author

@Rich-Harris

The second argument isn't optional, but the values of that record are because because optional route params are optional, so if you wanted to resolvePath(id, params) with auto typed params in a route with optional params, you'd need the function to accept Record<string, string | undefined> for params.

@Rich-Harris
Copy link
Member

ohhh i read it as Record<string, string> | undefined instead of Record<string, string | undefined>

@Rich-Harris Rich-Harris merged commit 195e9ac into master May 18, 2023
@Rich-Harris Rich-Harris deleted the elliott/9934-build-path branch May 18, 2023 14:15
@github-actions github-actions bot mentioned this pull request May 18, 2023
@darrylyeo
Copy link

Woohoo! That was fast.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Utility function to build a URL from route.id + params
4 participants