1.0.0 • Published 1 year ago

ts-path-params v1.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

Path Params

Description

This module is utility module to replace path with params and infer the params from the path

Table Of Content

Example

CodeSandbox

Usage

Default

generatePath('/users/:userId', { userId: 1 }); // "/users/1"
type Params = PathVariable<'/users/:userId'> // { userId: string | number }

Customizing

const PathParamsPattern = {
  Default: createParamsPattern(':'),
  NextJSRoute: createParamsPattern('[', ']')
}
const generatePath = createPathGenerator(
  PathParamsPattern.Default,
  PathParamsPattern.NextJSRoute
)

generatePath('/users/:userId', { userId: 1 });
generatePath('/users/[userId]', { userId: 1 });
type Params = PathVariable<'/users/[userId]', typeof PathParamsPattern.NextJSRoute>

API

generatePath(path, params)

generatePath replaces path with params

  generatePath('/user/:userId', { userId: 1 });

createParamsPattern(prefix, postfix?)

return value is ParamPattern and used for createPathGenerator

  • /user/:userId => createParamsPattern(':')
  • /user/[userId] => createParamsPattern('[', ']')

createPathGenerator(...patterns)

createPathGenerator creates the generatePath function.
Created function replaces path by pattern

  const genreatePath = createPathGenerator(
    createParamsPattern(':'),
    createParamsPattern('[', ']')
  )
  genreatePath('/user/:userId', { userId: 1 });
  genreatePath('/user/[userId]', { userId: 1 });

type PathVariable<Path, Pattern?>

PathVariable infers the type from the path.

  type MyParams = PathVariable<'/user/:userId'>; 
  // { userId: string | number }

  const pattern = createParamsPattern('[', ']');
  type MySecondParams = PathVariable<'/user/[userId]', typeof pattern> 
  // { userId: string | number }
1.0.0

1 year ago

0.1.0

1 year ago

0.1.1

1 year ago

0.0.8

1 year ago

0.0.7

1 year ago

0.0.6

1 year ago

0.0.5

1 year ago

0.0.4

1 year ago

0.0.3

1 year ago

0.0.2

1 year ago

0.0.1

1 year ago