-
-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs(types): add type helper documentation
- Loading branch information
Showing
2 changed files
with
49 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
--- | ||
sidebar_position: 5 | ||
--- | ||
|
||
# Typescript | ||
|
||
Even though zodios is written in typescript, you can use it with javascript. However, if you are using typescript, you can benefit from the typescript type helpers. | ||
|
||
## Example | ||
|
||
```ts | ||
import { | ||
makeCrudApi, | ||
Response, | ||
Body, | ||
PathParams, | ||
QueryParams, | ||
} from "../src/index"; | ||
import z from "zod"; | ||
|
||
const user = z.object({ | ||
id: z.number(), | ||
name: z.string(), | ||
email: z.string().email(), | ||
phone: z.string(), | ||
}); | ||
|
||
const api = makeCrudApi("user", user); | ||
|
||
type User = z.infer<typeof user>; | ||
type Api = typeof api; | ||
|
||
type Users = Response<Api, "get", "/users">; | ||
// ^? type Users = { id: number; name: string; email: string; phone: string; }[] | ||
type UserById = Response<Api, "get", "/users/:id">; | ||
// ^? type UserById = { id: number; name: string; email: string; phone: string; } | ||
type GetUserParams = PathParams<"/users/:id">; | ||
// ^? type GetUserParams = { id: string; } | ||
type GetUserQueries = QueryParams<Api, "get", "/users/:id">; | ||
// ^? type GetUserQueries = never | ||
type CreateUserBody = Body<Api, "post", "/users">; | ||
// ^? type CreateUserBody = { name: string; email: string; phone: string; } | ||
type CreateUserResponse = Response<Api, "post", "/users">; | ||
// ^? type CreateUserResponse = { id: number; name: string; email: string; phone: string; } | ||
type UpdateUserBody = Body<Api, "put", "/users/:id">; | ||
// ^? type UpdateUserBody = { name: string; email: string; phone: string; } | ||
type PatchUserBody = Body<Api, "patch", "/users/:id">; | ||
// ^? type PatchUserBody = { name?: string | undefined; email?: string | undefined; phone?: string | undefined; } | ||
``` |