Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unified withParams utility to replace individual path, query, and host utilities #447

Merged
merged 13 commits into from
Jan 30, 2025
Merged
6 changes: 3 additions & 3 deletions docs/advanced-concepts/route-matching.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ const route {
```ts
const route {
...
path: path('/parent/[id]', { id: Number })
path: withParams('/parent/[id]', { id: Number })
query: 'tab=[?tab]'
}
```
Expand All @@ -123,8 +123,8 @@ const route {
```ts
const route {
...
path: path('/parent/[id]', { id: Number })
query: query('tab=[?tab]', { tab: Boolean })
path: withParams('/parent/[id]', { id: Number })
query: withParams('tab=[?tab]', { tab: Boolean })
}
```

Expand Down
2 changes: 1 addition & 1 deletion docs/advanced-concepts/route-state.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Route State

It may be useful to store state for a given route to improve your user's experience. In situations like a form that a user might fill out, it might be useful to store form values in the [browser state](https://developer.mozilla.org/en-US/docs/Web/API/History/state) so that if the user navigates unexpectedly the values can be restored when going back. Kitbag Router extends this functionality by offering the same [param experience](/core-concepts/params#param-types) on `path`, `query`, etc on state as well.
It may be useful to store state for a given route to improve your user's experience. In situations like a form that a user might fill out, it might be useful to store form values in the [browser state](https://developer.mozilla.org/en-US/docs/Web/API/History/state) so that if the user navigates unexpectedly the values can be restored when going back. Kitbag Router extends this functionality by offering the same [param experience](/core-concepts/params#param-types) on state as well.

```ts
import { createRoute } from '@kitbag/router'
Expand Down
24 changes: 12 additions & 12 deletions docs/api/functions/createExternalRoute.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@
## Call Signature

```ts
function createExternalRoute<THost, TName, TPath, TQuery, THash, TMeta>(options): Route<ToName<TName>, ToHost<THost>, ToPath<TPath>, ToQuery<TQuery>, ToHash<THash>, TMeta>
function createExternalRoute<THost, TName, TPath, TQuery, THash, TMeta>(options): Route<ToName<TName>, ToWithParams<THost>, ToWithParams<TPath>, ToWithParams<TQuery>, ToWithParams<THash>, TMeta>
```

### Type Parameters

| Type Parameter | Default type |
| ------ | ------ |
| `THost` *extends* \| `string` \| `Host`\<`string`, `Record`\<`string`, `undefined` \| [`Param`](../types/Param.md)\>\> | - |
| `THost` *extends* \| `string` \| `WithParams`\<`string`, `Record`\<`string`, `undefined` \| [`Param`](../types/Param.md)\>\> | - |
| `TName` *extends* `undefined` \| `string` | `undefined` |
| `TPath` *extends* \| `undefined` \| `string` \| `Path`\<`string`, `Record`\<`string`, `undefined` \| [`Param`](../types/Param.md)\>\> | `undefined` |
| `TQuery` *extends* \| `undefined` \| `string` \| `Query`\<`string`, `Record`\<`string`, `undefined` \| [`Param`](../types/Param.md)\>\> | `undefined` |
| `THash` *extends* `undefined` \| `string` \| `Hash`\<`undefined` \| `string`\> | `undefined` |
| `TPath` *extends* \| `undefined` \| `string` \| `WithParams`\<`string`, `Record`\<`string`, `undefined` \| [`Param`](../types/Param.md)\>\> | `undefined` |
| `TQuery` *extends* \| `undefined` \| `string` \| `WithParams`\<`string`, `Record`\<`string`, `undefined` \| [`Param`](../types/Param.md)\>\> | `undefined` |
| `THash` *extends* \| `undefined` \| `string` \| `WithParams`\<`string`, `Record`\<`string`, `undefined` \| [`Param`](../types/Param.md)\>\> | `undefined` |
| `TMeta` *extends* `Record`\<`string`, `unknown`\> | `Record`\<`string`, `unknown`\> |

### Parameters
Expand All @@ -25,23 +25,23 @@ function createExternalRoute<THost, TName, TPath, TQuery, THash, TMeta>(options)

### Returns

[`Route`](../types/Route.md)\<`ToName`\<`TName`\>, `ToHost`\<`THost`\>, `ToPath`\<`TPath`\>, `ToQuery`\<`TQuery`\>, `ToHash`\<`THash`\>, `TMeta`\>
[`Route`](../types/Route.md)\<`ToName`\<`TName`\>, `ToWithParams`\<`THost`\>, `ToWithParams`\<`TPath`\>, `ToWithParams`\<`TQuery`\>, `ToWithParams`\<`THash`\>, `TMeta`\>

## Call Signature

```ts
function createExternalRoute<TParent, TName, TPath, TQuery, THash, TMeta>(options): Route<ToName<TName>, Host<"", {}>, CombinePath<TParent["path"], ToPath<TPath>>, CombineQuery<TParent["query"], ToQuery<TQuery>>, CombineHash<TParent["hash"], ToHash<THash>>, CombineMeta<TMeta, TParent["meta"]>>
function createExternalRoute<TParent, TName, TPath, TQuery, THash, TMeta>(options): Route<ToName<TName>, WithParams<"", {}>, CombinePath<TParent["path"], ToWithParams<TPath>>, CombineQuery<TParent["query"], ToWithParams<TQuery>>, CombineHash<TParent["hash"], ToWithParams<THash>>, CombineMeta<TMeta, TParent["meta"]>>
```

### Type Parameters

| Type Parameter | Default type |
| ------ | ------ |
| `TParent` *extends* [`Route`](../types/Route.md)\<`string`, `Host`\<`string`, `Record`\<`string`, `undefined` \| [`Param`](../types/Param.md)\>\>, `Path`\<`string`, `Record`\<`string`, `undefined` \| [`Param`](../types/Param.md)\>\>, `Query`\<`string`, `Record`\<`string`, `undefined` \| [`Param`](../types/Param.md)\>\>, `Hash`\<`undefined` \| `string`\>, `Record`\<`string`, `unknown`\>, `Record`\<`string`, [`Param`](../types/Param.md)\>, [`CreatedRouteOptions`](../types/CreatedRouteOptions.md)[]\> | - |
| `TParent` *extends* [`Route`](../types/Route.md)\<`string`, `WithParams`\<`string`, `Record`\<`string`, `undefined` \| [`Param`](../types/Param.md)\>\>, `WithParams`\<`string`, `Record`\<`string`, `undefined` \| [`Param`](../types/Param.md)\>\>, `WithParams`\<`string`, `Record`\<`string`, `undefined` \| [`Param`](../types/Param.md)\>\>, `WithParams`\<`string`, `Record`\<`string`, `undefined` \| [`Param`](../types/Param.md)\>\>, `Record`\<`string`, `unknown`\>, `Record`\<`string`, [`Param`](../types/Param.md)\>, [`CreatedRouteOptions`](../types/CreatedRouteOptions.md)[]\> | - |
| `TName` *extends* `undefined` \| `string` | `undefined` |
| `TPath` *extends* \| `undefined` \| `string` \| `Path`\<`string`, `Record`\<`string`, `undefined` \| [`Param`](../types/Param.md)\>\> | `undefined` |
| `TQuery` *extends* \| `undefined` \| `string` \| `Query`\<`string`, `Record`\<`string`, `undefined` \| [`Param`](../types/Param.md)\>\> | `undefined` |
| `THash` *extends* `undefined` \| `string` \| `Hash`\<`undefined` \| `string`\> | `undefined` |
| `TPath` *extends* \| `undefined` \| `string` \| `WithParams`\<`string`, `Record`\<`string`, `undefined` \| [`Param`](../types/Param.md)\>\> | `undefined` |
| `TQuery` *extends* \| `undefined` \| `string` \| `WithParams`\<`string`, `Record`\<`string`, `undefined` \| [`Param`](../types/Param.md)\>\> | `undefined` |
| `THash` *extends* \| `undefined` \| `string` \| `WithParams`\<`string`, `Record`\<`string`, `undefined` \| [`Param`](../types/Param.md)\>\> | `undefined` |
| `TMeta` *extends* `Record`\<`string`, `unknown`\> | `Record`\<`string`, `unknown`\> |

### Parameters
Expand All @@ -52,4 +52,4 @@ function createExternalRoute<TParent, TName, TPath, TQuery, THash, TMeta>(option

### Returns

[`Route`](../types/Route.md)\<`ToName`\<`TName`\>, `Host`\<`""`, \{\}\>, `CombinePath`\<`TParent`\[`"path"`\], `ToPath`\<`TPath`\>\>, `CombineQuery`\<`TParent`\[`"query"`\], `ToQuery`\<`TQuery`\>\>, `CombineHash`\<`TParent`\[`"hash"`\], `ToHash`\<`THash`\>\>, `CombineMeta`\<`TMeta`, `TParent`\[`"meta"`\]\>\>
[`Route`](../types/Route.md)\<`ToName`\<`TName`\>, `WithParams`\<`""`, \{\}\>, `CombinePath`\<`TParent`\[`"path"`\], `ToWithParams`\<`TPath`\>\>, `CombineQuery`\<`TParent`\[`"query"`\], `ToWithParams`\<`TQuery`\>\>, `CombineHash`\<`TParent`\[`"hash"`\], `ToWithParams`\<`THash`\>\>, `CombineMeta`\<`TMeta`, `TParent`\[`"meta"`\]\>\>
2 changes: 1 addition & 1 deletion docs/api/functions/createRoute.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ function createRoute<TOptions, TProps>(options, ...args): ToRoute<TOptions, Crea

| Type Parameter |
| ------ |
| `TOptions` *extends* [`CreateRouteOptions`](../types/CreateRouteOptions.md)\<`undefined` \| `string`, \| `undefined` \| `string` \| `Path`\<`string`, `Record`\<`string`, `undefined` \| [`Param`](../types/Param.md)\>\>, \| `undefined` \| `string` \| `Query`\<`string`, `Record`\<`string`, `undefined` \| [`Param`](../types/Param.md)\>\>, `undefined` \| `string` \| `Hash`\<`undefined` \| `string`\>, `Record`\<`string`, `unknown`\>\> |
| `TOptions` *extends* [`CreateRouteOptions`](../types/CreateRouteOptions.md)\<`undefined` \| `string`, \| `undefined` \| `string` \| `WithParams`\<`string`, `Record`\<`string`, `undefined` \| [`Param`](../types/Param.md)\>\>, \| `undefined` \| `string` \| `WithParams`\<`string`, `Record`\<`string`, `undefined` \| [`Param`](../types/Param.md)\>\>, \| `undefined` \| `string` \| `WithParams`\<`string`, `Record`\<`string`, `undefined` \| [`Param`](../types/Param.md)\>\>, `Record`\<`string`, `unknown`\>\> |
| `TProps` *extends* \| `PropsGetter`\<`TOptions`, `any`\[`any`\]\> \| `RoutePropsRecord`\<`TOptions`, `any`\[`any`\]\> \| `PropsGetter`\<`TOptions`, `__VLS_WithTemplateSlots`\<`DefineComponent`\<`__VLS_Props`, \{\}, \{\}, \{\}, \{\}, `ComponentOptionsMixin`, `ComponentOptionsMixin`, \{\}, `string`, `PublicProps`, `Readonly`\<`__VLS_Props`\> & `Readonly`\<\{\}\>, \{\}, \{\}, \{\}, \{\}, `string`, `ComponentProvideOptions`, `false`, \{\}, `any`\>, `Readonly`\<\{ `default`: (`props`) => `VNode`; \}\> & `object`\>\> |

## Parameters
Expand Down
40 changes: 0 additions & 40 deletions docs/api/functions/path.md

This file was deleted.

40 changes: 0 additions & 40 deletions docs/api/functions/query.md

This file was deleted.

35 changes: 35 additions & 0 deletions docs/api/functions/withParams.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Functions: withParams()

## Call Signature

```ts
function withParams<TValue, TParams>(value, params): WithParams<TValue, TParams>
```

### Type Parameters

| Type Parameter |
| ------ |
| `TValue` *extends* `string` |
| `TParams` *extends* `ParamsWithParamNameExtracted`\<`TValue`\> |

### Parameters

| Parameter | Type |
| ------ | ------ |
| `value` | `TValue` |
| `params` | `TParams` |

### Returns

`WithParams`\<`TValue`, `TParams`\>

## Call Signature

```ts
function withParams(): WithParams<"", {}>
```

### Returns

`WithParams`\<`""`, \{\}\>
3 changes: 1 addition & 2 deletions docs/api/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,10 @@
- [createRoute](functions/createRoute.md)
- [createRouter](functions/createRouter.md)
- [createRouterPlugin](functions/createRouterPlugin.md)
- [path](functions/path.md)
- [query](functions/query.md)
- [tupleOf](functions/tupleOf.md)
- [unionOf](functions/unionOf.md)
- [withDefault](functions/withDefault.md)
- [withParams](functions/withParams.md)

## Hooks

Expand Down
12 changes: 4 additions & 8 deletions docs/api/typedoc-sidebar.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,6 @@
"text": "createRouterPlugin",
"link": "/api/functions/createRouterPlugin.md"
},
{
"text": "path",
"link": "/api/functions/path.md"
},
{
"text": "query",
"link": "/api/functions/query.md"
},
{
"text": "tupleOf",
"link": "/api/functions/tupleOf.md"
Expand All @@ -114,6 +106,10 @@
{
"text": "withDefault",
"link": "/api/functions/withDefault.md"
},
{
"text": "withParams",
"link": "/api/functions/withParams.md"
}
]
},
Expand Down
6 changes: 3 additions & 3 deletions docs/api/types/CreateRouteOptions.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ Type params for additional data intended to be stored in history state, all keys
| Type Parameter | Default type |
| ------ | ------ |
| `TName` *extends* `string` \| `undefined` | `string` \| `undefined` |
| `TPath` *extends* `string` \| `Path` \| `undefined` | `string` \| `Path` \| `undefined` |
| `TQuery` *extends* `string` \| `Query` \| `undefined` | `string` \| `Query` \| `undefined` |
| `THash` *extends* `string` \| `Hash` \| `undefined` | `string` \| `Hash` \| `undefined` |
| `TPath` *extends* `string` \| `WithParams` \| `undefined` | `string` \| `WithParams` \| `undefined` |
| `TQuery` *extends* `string` \| `WithParams` \| `undefined` | `string` \| `WithParams` \| `undefined` |
| `THash` *extends* `string` \| `WithParams` \| `undefined` | `string` \| `WithParams` \| `undefined` |
| `TMeta` *extends* [`RouteMeta`](RouteMeta.md) | [`RouteMeta`](RouteMeta.md) |
8 changes: 4 additions & 4 deletions docs/api/types/Route.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ Represents the structure of a route within the application. Return value of `cre
| Type Parameter | Default type | Description |
| ------ | ------ | ------ |
| `TName` *extends* `string` | `string` | Represents the unique name identifying the route, typically a string. |
| `THost` *extends* `Host` | `Host` | - |
| `TPath` *extends* `Path` | `Path` | The type or structure of the route's path. |
| `TQuery` *extends* `Query` | `Query` | The type or structure of the query parameters associated with the route. |
| `THash` *extends* `Hash` | `Hash` | - |
| `THost` *extends* `WithParams` | `WithParams` | - |
| `TPath` *extends* `WithParams` | `WithParams` | The type or structure of the route's path. |
| `TQuery` *extends* `WithParams` | `WithParams` | The type or structure of the query parameters associated with the route. |
| `THash` *extends* `WithParams` | `WithParams` | - |
| `TMeta` *extends* [`RouteMeta`](RouteMeta.md) | [`RouteMeta`](RouteMeta.md) | - |
| `TState` *extends* `Record`\<`string`, [`Param`](Param.md)\> | `Record`\<`string`, [`Param`](Param.md)\> | - |
| `TMatches` *extends* [`CreatedRouteOptions`](CreatedRouteOptions.md)[] | [`CreatedRouteOptions`](CreatedRouteOptions.md)[] | - |
Expand Down
2 changes: 1 addition & 1 deletion docs/core-concepts/navigation.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const blog = createRoute({
const blogPost = createRoute({
parent: blog,
name: 'blogPost',
path: path('/[blogPostId]', {
path: withParams('/[blogPostId]', {
blogPostId: Number,
}),
})
Expand Down
Loading