You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+39-3Lines changed: 39 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,7 +8,7 @@ JSON serializable, fully typesafe, and [zod](https://www.npmjs.com/package/zod)
8
8
9
9
### Routing
10
10
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.
12
12
13
13
### Search Params
14
14
@@ -20,11 +20,13 @@ Traditional Search Param APIs usually assume a few things:
20
20
- They are mostly flat
21
21
- Serializing and deserializing using URLSearchParams is good enough (Spoiler alert: it's not, it sucks)
22
22
23
+
## Typesafety Isn’t Optional
24
+
23
25
### How does `next-typesafe-url` solve these problems?
24
26
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
26
28
- 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
28
30
29
31
## Installation
30
32
@@ -123,13 +125,47 @@ import { $path } from "next-typesafe-url";
123
125
124
126
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.
125
127
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.
// value of bar will be the string "undefined", not undefined
149
+
```
150
+
151
+
---
152
+
126
153
## Hooks
127
154
128
155
`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.
// 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
+
}
133
169
```
134
170
135
171
`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