This module is utility module to replace path with params and infer the params from the path
generatePath('/users/:userId', { userId: 1 }); // "/users/1"
type Params = PathVariable<'/users/:userId'> // { userId: string | number }
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>
generatePath
replaces path with params
generatePath('/user/:userId', { userId: 1 });
return value is ParamPattern
and used for createPathGenerator
-
/user/:userId
=>createParamsPattern(':')
-
/user/[userId]
=>createParamsPattern('[', ']')
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 });
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 }