Skip to content

Commit 1e0feee

Browse files
committed
docs additions
1 parent 918ce1c commit 1e0feee

4 files changed

Lines changed: 46 additions & 40 deletions

File tree

.changeset/swift-bulldogs-walk.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"next-typesafe-url": patch
3+
---
4+
5+
docs: point out reserved string keywords, add example of using data, and rewrite current next routing description

.npmignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@
55
!dist/
66
!LICENSE
77
!README.md
8-
!package.json
8+
!package.json
9+
!CHANGELOG.md

CHANGELOG.md

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -11,39 +11,3 @@
1111
### Major Changes
1212

1313
- d7e4983: ship it.
14-
15-
## 0.0.13
16-
17-
### Patch Changes
18-
19-
- ffa5972: FIXED CI!
20-
21-
## 0.0.12
22-
23-
### Patch Changes
24-
25-
- 5742328: fixed public access?
26-
27-
## 0.0.11
28-
29-
### Patch Changes
30-
31-
- 30bdc53: updated npm script advice
32-
33-
## 0.0.10
34-
35-
### Patch Changes
36-
37-
- 25e9aa1: fixed minor typo that left out type keyword
38-
39-
## 0.0.9
40-
41-
### Patch Changes
42-
43-
- d797c8d: test2
44-
45-
## 0.0.8
46-
47-
### Patch Changes
48-
49-
- 6412922: test

README.md

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ JSON serializable, fully typesafe, and [zod](https://www.npmjs.com/package/zod)
88

99
### Routing
1010

11-
Typesafe routing in NextJS is nonexistant. What we do have currently is a pain. You have to manually type out the route params and search params for each route, and if you make a mistake, you will get a runtime error.
11+
Next.js's non-typesafe routing can lead to runtime errors and make it difficult to catch routing-related issues during development, as it relies on string literals instead of type-safe constructs.
1212

1313
### Search Params
1414

@@ -20,11 +20,13 @@ Traditional Search Param APIs usually assume a few things:
2020
- They are mostly flat
2121
- Serializing and deserializing using URLSearchParams is good enough (Spoiler alert: it's not, it sucks)
2222

23+
## Typesafety Isn’t Optional
24+
2325
### How does `next-typesafe-url` solve these problems?
2426

25-
- Fully typesafe routing, from the path to the route params to the search params
27+
- **Fully typesafe routing-** all the way from the path, to the route params, to the search params
2628
- Search params (and technically route params too) are JSON-first, so you can pass numbers, booleans, nulls, and even nested objects and arrays
27-
- Search and route params are validated at runtime using zod, so you can be sure that the data you are getting is valid
29+
- Search and route params are validated at runtime using zod, so you can be sure that the data you are getting matches the schema you expect
2830

2931
## Installation
3032

@@ -123,13 +125,47 @@ import { $path } from "next-typesafe-url";
123125

124126
If the path is not a valid route, or any of the route params or search params are missing or of the wrong type, it will throw a type error.
125127

128+
---
129+
130+
**Note:** Strings are passed to the url without quotes, so to provide support for booleans, nulls and numbers there are reserved keywords that are converted to their respective values.
131+
132+
Those keywords are:
133+
134+
- `"true"` -> `true`
135+
- `"false"` -> `false`
136+
- `"null"` -> `null`
137+
- `"<some_numberic_literal>"` -> `number`
138+
139+
**`"undefined"` is not a special keyword**, and will be converted to a string if passed as the value of a search param. Passing undefined as the value of a search param will cause it to be left out of the path.
140+
141+
```typescript
142+
143+
$path({ path: "/" searchParams: { foo: undefined, bar: true } }) // => "/?bar=true"
144+
145+
// ------------------------
146+
147+
// URL: /?bar=undefined
148+
// value of bar will be the string "undefined", not undefined
149+
```
150+
151+
---
152+
126153
## Hooks
127154

128155
`next-typesafe-url` exports a `useRouteParams` and `useSearchParams` hook that will return the route params / search params for the current route. They take one argument, the zod schema for either route params or search params from the Route object.
129156

130157
```tsx
131158
const params = useSearchParams(Route.searchParams);
132159
const { data, isReady, isError, error } = params;
160+
161+
// if isReady is true, and isError is false, then data will be in the shape of Route.searchParams
162+
// in this case, data will be { userInfo: { name: string, age: number } }
163+
164+
if (isError) {
165+
return <div>Invalid search params</div>;
166+
} else {
167+
return <div>{data.userInfo.name}</div>;
168+
}
133169
```
134170

135171
`isReady` is the internal state of next/router, and `isError` is a boolean that is true if the params do not match the schema. If `isError` is true, then `error` will be a zod error object you can use to get more information about the error. (_also check out [zod-validation-error](https://github.com/causaly/zod-validation-error) to get a nice error message_)

0 commit comments

Comments
 (0)