kitbagjs / router

A type safe router for vuejs
MIT License
131 stars 1 forks source link

optional params alt approach #208

Closed stackoverfloweth closed 1 week ago

stackoverfloweth commented 1 week ago

Alternative approach for tracking optional params without modifying param itself

With this PR, knowing if a param should be optional has nothing to do with the Param itself. We've always taken the name of a param from the string part of a path/query. IE path('/something/[id]', {id: String}) for required string or path('/something/[?id]', {id: String}) for optional string of the same name. We always strip the param name out from the [ and ], we just now keep the ? if it's present.

Previously our optional id param would be stored on the route as

path: {
  ...,
  params: {
    requiredFoo: String,
    optionalBar: optional(String),
  }
}

Now will be stored as

path: {
  ...,
  params: {
    requiredFoo: String,
    '?optionalBar': String,
  }
}

Note, this key that now may start with "?" is only true internally. So when calling path or query, the 2nd argument does not expect a question mark

// correct
path('/something/[?id]', {id: String})

// incorrect
path('/something/[?id]', {'?id': String})

This is also true for router.push type functions as well as reading params from useRoute.

const route = useRoute('myRoute')

// correct
route.params.id

// incorrect
route.params['?id']

This alternative approach means we can delete the entire optional function, types, and file altogether.

netlify[bot] commented 1 week ago

Deploy Preview for kitbag-router ready!

Name Link
Latest commit 0ce5a1a44147ad4c22b7acd5bb500ca56a165b86
Latest deploy log https://app.netlify.com/sites/kitbag-router/deploys/667a2d7c493d54000892171c
Deploy Preview https://deploy-preview-208--kitbag-router.netlify.app
Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.