Skip to content

Commit 22036aa

Browse files
committed
v0.2.0 more APIs
1 parent eaa6d1d commit 22036aa

22 files changed

+497
-98
lines changed

docs/modules.md

Lines changed: 133 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
[react-router-infer - v0.1.0](README.md) / Exports
1+
[react-router-infer - v0.2.0](README.md) / Exports
22

3-
# react-router-infer - v0.1.0
3+
# react-router-infer - v0.2.0
44

55
## Table of contents
66

@@ -12,6 +12,9 @@
1212
### Functions
1313

1414
- [Link](modules.md#link)
15+
- [NavLink](modules.md#navlink)
16+
- [create\_generatePath](modules.md#create_generatepath)
17+
- [generatePath](modules.md#generatepath)
1518
- [r](modules.md#r)
1619
- [useNavigate](modules.md#usenavigate)
1720
- [useParams](modules.md#useparams)
@@ -44,7 +47,7 @@ const routes = [...] as const satisfies RouteObject[]
4447

4548
#### Defined in
4649

47-
[core.ts:14](https://github.com/haivuw/react-router-infer/blob/449bf32/src/core.ts#L14)
50+
[core.ts:14](https://github.com/haivuw/react-router-infer/blob/eaa6d1d/src/core.ts#L14)
4851

4952
___
5053

@@ -56,7 +59,7 @@ Extends `react-router-dom`'s `RouteObject` with `parseSearch` fields
5659

5760
#### Defined in
5861

59-
[core.ts:35](https://github.com/haivuw/react-router-infer/blob/449bf32/src/core.ts#L35)
62+
[core.ts:35](https://github.com/haivuw/react-router-infer/blob/eaa6d1d/src/core.ts#L35)
6063

6164
## Functions
6265

@@ -97,7 +100,119 @@ const render = (
97100

98101
#### Defined in
99102

100-
[Link.tsx:24](https://github.com/haivuw/react-router-infer/blob/449bf32/src/Link.tsx#L24)
103+
[Link.tsx:24](https://github.com/haivuw/react-router-infer/blob/eaa6d1d/src/Link.tsx#L24)
104+
105+
___
106+
107+
### NavLink
108+
109+
**NavLink**\<`To`\>(`props`): `Element`
110+
111+
#### Type parameters
112+
113+
| Name | Type |
114+
| :------ | :------ |
115+
| `To` | extends `string` |
116+
117+
#### Parameters
118+
119+
| Name | Type | Description |
120+
| :------ | :------ | :------ |
121+
| `props` | `Object` | T |
122+
123+
#### Returns
124+
125+
`Element`
126+
127+
**`Example`**
128+
129+
```ts
130+
const render = (
131+
<NavLink
132+
to='/:id'
133+
params={{ id: 1 }}
134+
search={{ page: 1 }}
135+
// react-router's NavigateOptions
136+
replace
137+
{...rest}
138+
/>
139+
)
140+
```
141+
142+
#### Defined in
143+
144+
[NavLink.tsx:22](https://github.com/haivuw/react-router-infer/blob/eaa6d1d/src/NavLink.tsx#L22)
145+
146+
___
147+
148+
### create\_generatePath
149+
150+
**create_generatePath**(`opts?`): `GeneratePath`
151+
152+
Return a new `generatePath` function that uses the custom `stringifySearch`
153+
154+
#### Parameters
155+
156+
| Name | Type |
157+
| :------ | :------ |
158+
| `opts?` | `Pick`\<`ParserUtils`, ``"stringifySearch"``\> |
159+
160+
#### Returns
161+
162+
`GeneratePath`
163+
164+
**`Example`**
165+
166+
```ts
167+
const generatePath = create_generatePath({
168+
stringifySearch: () => '?custom',
169+
})
170+
```
171+
172+
#### Defined in
173+
174+
[generatePath.ts:26](https://github.com/haivuw/react-router-infer/blob/eaa6d1d/src/generatePath.ts#L26)
175+
176+
___
177+
178+
### generatePath
179+
180+
**generatePath**\<`Routes`, `To`\>(`to`): `string`
181+
182+
#### Type parameters
183+
184+
| Name | Type |
185+
| :------ | :------ |
186+
| `Routes` | extends `BaseRoutes` = `BaseRoutes` |
187+
| `To` | extends `string` \| `number` \| `symbol` = keyof `Routes` |
188+
189+
#### Parameters
190+
191+
| Name | Type |
192+
| :------ | :------ |
193+
| `to` | `NavigateTo`\<`Routes`, `To`\> |
194+
195+
#### Returns
196+
197+
`string`
198+
199+
**`Example`**
200+
201+
```ts
202+
import { redirect } from 'react-router-dom'
203+
import { generatePath } from 'react-router-infer'
204+
205+
const path = generatePath({
206+
to: '/:id',
207+
params: { id: '1' },
208+
search: { page: 1 },
209+
})
210+
redirect(path)
211+
```
212+
213+
#### Defined in
214+
215+
[generatePath.ts:54](https://github.com/haivuw/react-router-infer/blob/eaa6d1d/src/generatePath.ts#L54)
101216

102217
___
103218

@@ -137,23 +252,17 @@ const singleRoute = r({ path: '/' })
137252

138253
#### Defined in
139254

140-
[core.ts:142](https://github.com/haivuw/react-router-infer/blob/449bf32/src/core.ts#L142)
255+
[core.ts:142](https://github.com/haivuw/react-router-infer/blob/eaa6d1d/src/core.ts#L142)
141256

142257
___
143258

144259
### useNavigate
145260

146-
**useNavigate**\<`Routes`\>(): `NavigateFunction`\<`Routes`\>
147-
148-
#### Type parameters
149-
150-
| Name | Type |
151-
| :------ | :------ |
152-
| `Routes` | extends `BaseRoutes` = `BaseRoutes` |
261+
**useNavigate**(): `NavigateFunction`\<`BaseRoutes`\>
153262

154263
#### Returns
155264

156-
`NavigateFunction`\<`Routes`\>
265+
`NavigateFunction`\<`BaseRoutes`\>
157266

158267
**`Example`**
159268

@@ -172,20 +281,19 @@ const navigate = useNavigate()
172281

173282
#### Defined in
174283

175-
[useNavigate.ts:20](https://github.com/haivuw/react-router-infer/blob/449bf32/src/useNavigate.ts#L20)
284+
[useNavigate.ts:20](https://github.com/haivuw/react-router-infer/blob/eaa6d1d/src/useNavigate.ts#L20)
176285

177286
___
178287

179288
### useParams
180289

181-
**useParams**\<`Routes`, `From`\>(`_opts?`): `IsUnion`\<`From`\> extends ``true`` ? `Partial`\<`GetAllParams`\<`Routes`, ``"params"``\>\> : `Partial`\<`ToObject`\<`Routes`[`From`][``"params"``]\>\>
290+
**useParams**\<`From`\>(`_opts?`): `IsUnion`\<`From`\> extends ``true`` ? `Partial`\<`ParsedParams`\> : `Partial`\<`ParsedParams`\>
182291

183292
#### Type parameters
184293

185294
| Name | Type |
186295
| :------ | :------ |
187-
| `Routes` | extends `BaseRoutes` = `BaseRoutes` |
188-
| `From` | extends `string` = `string` & keyof `Routes` |
296+
| `From` | extends `string` = `string` |
189297

190298
#### Parameters
191299

@@ -195,7 +303,7 @@ ___
195303

196304
#### Returns
197305

198-
`IsUnion`\<`From`\> extends ``true`` ? `Partial`\<`GetAllParams`\<`Routes`, ``"params"``\>\> : `Partial`\<`ToObject`\<`Routes`[`From`][``"params"``]\>\>
306+
`IsUnion`\<`From`\> extends ``true`` ? `Partial`\<`ParsedParams`\> : `Partial`\<`ParsedParams`\>
199307

200308
**`Example`**
201309

@@ -209,20 +317,19 @@ const { id } = useParams({
209317

210318
#### Defined in
211319

212-
[useParams.ts:14](https://github.com/haivuw/react-router-infer/blob/449bf32/src/useParams.ts#L14)
320+
[useParams.ts:14](https://github.com/haivuw/react-router-infer/blob/eaa6d1d/src/useParams.ts#L14)
213321

214322
___
215323

216324
### useSearch
217325

218-
**useSearch**\<`Routes`, `From`, `Throw`\>(`opts`): `UseSearchOutput`\<`Routes`, `From`, `Throw`, `ToObject`\<`Routes`[`From`][``"search"``]\>, \{ `isInvalidRoute`: ``false`` ; `search`: `ToObject`\<`Routes`[`From`][``"search"``]\> ; `setSearch`: (`search`: `ToObject`\<`Routes`[`From`][``"search"``]\>, `opts?`: `NavigateOptions`) => `void` }, \{ `isInvalidRoute`: ``true`` ; `search`: `undefined` ; `setSearch`: `undefined` }\>
326+
**useSearch**\<`From`, `Throw`\>(`opts`): `UseSearchOutput`\<`BaseRoutes`, `From`, `Throw`, `ParsedSearch`, \{ `isInvalidRoute`: ``false`` ; `search`: `ParsedSearch` ; `setSearch`: (`search`: `ParsedSearch`, `opts?`: `NavigateOptions`) => `void` }, \{ `isInvalidRoute`: ``true`` ; `search`: `undefined` ; `setSearch`: `undefined` }\>
219327

220328
#### Type parameters
221329

222330
| Name | Type |
223331
| :------ | :------ |
224-
| `Routes` | extends `BaseRoutes` = `BaseRoutes` |
225-
| `From` | extends `string` = `string` & keyof `Routes` |
332+
| `From` | extends `string` = `string` |
226333
| `Throw` | extends `boolean` = ``true`` |
227334

228335
#### Parameters
@@ -233,7 +340,7 @@ ___
233340

234341
#### Returns
235342

236-
`UseSearchOutput`\<`Routes`, `From`, `Throw`, `ToObject`\<`Routes`[`From`][``"search"``]\>, \{ `isInvalidRoute`: ``false`` ; `search`: `ToObject`\<`Routes`[`From`][``"search"``]\> ; `setSearch`: (`search`: `ToObject`\<`Routes`[`From`][``"search"``]\>, `opts?`: `NavigateOptions`) => `void` }, \{ `isInvalidRoute`: ``true`` ; `search`: `undefined` ; `setSearch`: `undefined` }\>
343+
`UseSearchOutput`\<`BaseRoutes`, `From`, `Throw`, `ParsedSearch`, \{ `isInvalidRoute`: ``false`` ; `search`: `ParsedSearch` ; `setSearch`: (`search`: `ParsedSearch`, `opts?`: `NavigateOptions`) => `void` }, \{ `isInvalidRoute`: ``true`` ; `search`: `undefined` ; `setSearch`: `undefined` }\>
237344

238345
**`Example`**
239346

@@ -262,7 +369,7 @@ const { search, setSearch, isInvalidRoute } = useSearch({
262369

263370
#### Defined in
264371

265-
[useSearch.ts:31](https://github.com/haivuw/react-router-infer/blob/449bf32/src/useSearch.ts#L31)
372+
[useSearch.ts:31](https://github.com/haivuw/react-router-infer/blob/eaa6d1d/src/useSearch.ts#L31)
266373

267374
___
268375

@@ -301,4 +408,4 @@ Wraps user defined routes with SearchParamsProvider
301408

302409
#### Defined in
303410

304-
[SearchContext.tsx:98](https://github.com/haivuw/react-router-infer/blob/449bf32/src/SearchContext.tsx#L98)
411+
[SearchContext.tsx:94](https://github.com/haivuw/react-router-infer/blob/eaa6d1d/src/SearchContext.tsx#L94)

example/src/routes.tsx

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
import { useParams, useNavigate, useSearch, r, Link } from 'react-router-infer'
1+
import {
2+
useParams,
3+
useNavigate,
4+
useSearch,
5+
r,
6+
Link,
7+
NavLink,
8+
} from 'react-router-infer'
29
import * as v from 'valibot'
310
import { Outlet } from 'react-router-dom'
411
import { pokemonRoute } from './pokemon'
@@ -20,9 +27,9 @@ function RootLayout() {
2027
<>
2128
<div className='container mx-auto px-4 py-4'>
2229
<nav className='flex gap-3'>
23-
<Link to='/'>Home</Link>
24-
<Link to='/book'>Books</Link>
25-
<Link to='/pokemon'>Pokemon</Link>
30+
<NavLink to='/'>Home</NavLink>
31+
<NavLink to='/book'>Books</NavLink>
32+
<NavLink to='/pokemon'>Pokemon</NavLink>
2633
</nav>
2734
<Outlet />
2835
</div>

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "react-router-infer",
33
"description": "Type-safe wrappers for react-router-dom",
4-
"version": "0.1.0",
4+
"version": "0.2.0",
55
"license": "MIT",
66
"author": "haivuw",
77
"type": "module",

src/Link.test.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { r, CreateRoutes } from './core'
2-
import { afterEach, describe, expect, test } from 'bun:test'
3-
import { fireEvent, render, screen } from '@testing-library/react'
2+
import { afterAll, afterEach, describe, expect, test } from 'bun:test'
3+
import { cleanup, fireEvent, render, screen } from '@testing-library/react'
44
import { createWrapper } from '../test/react-wrapper'
55
import * as RR from 'react-router-dom'
66
import { Equal } from './util'
@@ -9,6 +9,8 @@ import { IsKeyRequired, Assert, PickRequired } from '../test/util'
99
import { ComponentProps } from 'react'
1010
import { defaultStringifySearch } from './parser'
1111

12+
afterAll(cleanup)
13+
1214
let location: RR.Location | undefined
1315

1416
const Expected = () => {

src/Link.tsx

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import * as RR from 'react-router-dom'
22
import { NavigateTo } from './useNavigate'
33
import { RegisteredRoutes } from '.'
4-
import { Merge } from './util'
54
import { BaseRoutes, ParsedParams } from './core'
65
import React from 'react'
76
import { useParserContext } from './SearchContext'
7+
import { Any } from './util'
88

99
/**
1010
* @example
@@ -42,15 +42,10 @@ export const Link: TLink = (props) => {
4242
export type LinkProps<
4343
Routes extends BaseRoutes,
4444
To extends keyof Routes,
45-
> = Merge<
46-
[
47-
RR.LinkProps,
48-
NavigateTo<Routes, To>,
49-
{
50-
anchorRef?: React.Ref<HTMLAnchorElement>
51-
},
52-
]
53-
>
45+
> = Omit<RR.LinkProps, keyof NavigateTo<Any, Any>> &
46+
NavigateTo<Routes, To> & {
47+
anchorRef?: React.Ref<HTMLAnchorElement>
48+
}
5449

5550
export type TLink<Routes extends BaseRoutes = RegisteredRoutes> = <
5651
To extends keyof Routes,

0 commit comments

Comments
 (0)