kitbagjs / router

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

new `createParam` utility #200

Closed stackoverfloweth closed 2 weeks ago

stackoverfloweth commented 2 weeks ago

This utility effectively already existed under the private asParamGetSet function. This PR renames that to createParam and adds a defaultValue argument with overloads that retain the correct type for ParamWithDefault.

This small utility enables developers to create custom params slightly more easily.

createParam((value, { invalid }) => {
  if (value === 'asc' || value === 'desc') {
    return value
  }

  throw invalid()
}, 'asc')

The value of this utility really comes in the setting of default values. Without this utility, developers will need to define their custom ParamGetSets with satisfies ParamGetSet<MyType> to prevent losing the fact that the param has a default value. Without doing so, when accessing the routes params developers will find the type is MyType | undefined even though we know it has a default value.

The first argument is any Param, the second is an optional default value.

createParam(Boolean, true)

It also allows developers to add a default value to existing params.

createParam(MySortParam, 'asc')

If the param is a ParamGetSet with a default value it will be replaced.

netlify[bot] commented 2 weeks ago

Deploy Preview for kitbag-router ready!

Name Link
Latest commit 5ac0a89dbab1555ee2978856394c9e350d64ca46
Latest deploy log https://app.netlify.com/sites/kitbag-router/deploys/66763a64e05b230008f192d5
Deploy Preview https://deploy-preview-200--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.