Skip to content

feat: add resolvePath export - #9949

Merged
Rich-Harris merged 7 commits into
masterfrom
elliott/9934-build-path
May 18, 2023
Merged

Rich-Harris merged 7 commits into
masterfrom
elliott/9934-build-path

Conversation

@elliott-with-the-longest-name-on-github

Copy link
Copy Markdown
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

changeset-bot Bot commented May 17, 2023 •

Copy link
Copy Markdown

🦋 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

Comment thread packages/kit/src/exports/index.js Outdated
* @param {Record<string, string>} params
* @returns {string}
*/
export function resolvePath(id, params) {

Copy link
Copy Markdown
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
Copy Markdown
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.

Copy link
Copy Markdown
Contributor Author

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

Comment thread packages/kit/src/exports/index.js Outdated
* @returns {string}
*/
export function resolvePath(id, params) {
return resolve_entry(id, params);

Copy link
Copy Markdown
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)

Copy link
Copy Markdown
Contributor Author

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.

Co-authored-by: Rich Harris <richard.a.harris@gmail.com>

@Rich-Harris Rich-Harris left a comment

Copy link
Copy Markdown
Member

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
Copy Markdown
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
Copy Markdown
Member

Why is the second argument optional?

@elliott-with-the-longest-name-on-github

Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown

Woohoo! That was fast.

This branch was successfully deployed

2 active deployments
Preview – kit-svelte-dev — 110d716e Deployed May 17, 2023 by vercel[bot]
Preview – kit — 110d716e Deployed May 17, 2023 by vercel[bot]
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