Skip to content

Commit

Permalink
docs: add guide on how to use ser and zod together (#1572)
Browse files Browse the repository at this point in the history
* docs: add guide for `swr` with `zod`

* chore: remove unnecessary codes from `swr-with-zod` sample app
  • Loading branch information
soartec-lab authored Aug 11, 2024
1 parent 9ed9ec2 commit 40fc4c6
Show file tree
Hide file tree
Showing 18 changed files with 83 additions and 234 deletions.
5 changes: 5 additions & 0 deletions docs/src/manifests/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@
"title": "Zod",
"path": "/guides/zod",
"editUrl": "/guides/zod.md"
},
{
"title": "Client with Zod",
"path": "/guides/client-with-zod",
"editUrl": "/guides/client-with-zod.md"
}
]
},
Expand Down
78 changes: 78 additions & 0 deletions docs/src/pages/guides/client-with-zod.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
---
id: client-with-zod
title: client-with-zod
---

If you want to use `zod` with your `swr` or `TanStack Query` client, you can do so by configuring it as follows.

#### Example of orval.config.js

```js
module.exports = {
petstore: {
input: {
target: './petstore.yaml',
},
output: {
mode: 'tags-split',
client: 'swr',
target: 'src/gen/endpoints',
schemas: 'src/gen/models',
mock: true,
},
},
petstoreZod: {
input: {
target: './petstore.yaml',
},
output: {
mode: 'tags-split',
client: 'zod',
target: 'src/gen/endpoints',
fileExtension: '.zod.ts',
},
},
};
```

Here we describe the generation of `zod` and the definition of `swr`. `petstoreZod` avoids conflicts in generated file names by specifying `.zod.ts` for `output.fileExtension` and not defining `schemas`. Also, `mock` only needs to be generated once, so it is defined only in `petstore`.

The files that are actually automatically generated are as follows:

```
samples/swr-with-zod/src/gen/
├── endpoints
│ └── pets
│ ├── pets.msw.ts
│ ├── pets.ts
│ └── pets.zod.ts
└── models
```

The automatically generated `swr` and `zod` definitions can be used in your application as follows:

```ts
import { Pet } from './gen/models';
import { useListPets, useCreatePets } from './gen/endpoints/pets/pets';
import { createPetsBodyItem } from './gen/endpoints/pets/pets.zod';

function App() {
const { data } = useListPets();
const { trigger } = useCreatePets();

const createPet = async () => {
// For example, specifying the number 123 in the name will result in a `zod` error.
const pet = { name: '123', tag: 'test' };

try {
const parsedPet = createPetsBodyItem.parse(pet);

await trigger([parsedPet]);
} catch (error) {
console.log('raise error', error);
}
};
}
```

See below for the complete [sample app](/~https://github.com/orval-labs/orval/tree/master/samples/swr-with-zod).
12 changes: 0 additions & 12 deletions samples/swr-with-zod/src/gen/model/cat.ts

This file was deleted.

13 changes: 0 additions & 13 deletions samples/swr-with-zod/src/gen/model/catType.ts

This file was deleted.

11 changes: 0 additions & 11 deletions samples/swr-with-zod/src/gen/model/createPetsBodyItem.ts

This file was deleted.

12 changes: 0 additions & 12 deletions samples/swr-with-zod/src/gen/model/dachshund.ts

This file was deleted.

14 changes: 0 additions & 14 deletions samples/swr-with-zod/src/gen/model/dachshundBreed.ts

This file was deleted.

19 changes: 0 additions & 19 deletions samples/swr-with-zod/src/gen/model/dog.ts

This file was deleted.

13 changes: 0 additions & 13 deletions samples/swr-with-zod/src/gen/model/dogType.ts

This file was deleted.

11 changes: 0 additions & 11 deletions samples/swr-with-zod/src/gen/model/error.ts

This file was deleted.

22 changes: 0 additions & 22 deletions samples/swr-with-zod/src/gen/model/index.ts

This file was deleted.

12 changes: 0 additions & 12 deletions samples/swr-with-zod/src/gen/model/labradoodle.ts

This file was deleted.

14 changes: 0 additions & 14 deletions samples/swr-with-zod/src/gen/model/labradoodleBreed.ts

This file was deleted.

13 changes: 0 additions & 13 deletions samples/swr-with-zod/src/gen/model/listPetsParams.ts

This file was deleted.

30 changes: 0 additions & 30 deletions samples/swr-with-zod/src/gen/model/pet.ts

This file was deleted.

15 changes: 0 additions & 15 deletions samples/swr-with-zod/src/gen/model/petCallingCode.ts

This file was deleted.

14 changes: 0 additions & 14 deletions samples/swr-with-zod/src/gen/model/petCountry.ts

This file was deleted.

9 changes: 0 additions & 9 deletions samples/swr-with-zod/src/gen/model/pets.ts

This file was deleted.

0 comments on commit 40fc4c6

Please sign in to comment.