Skip to content

Commit fa0cfdb

Browse files
authored
Merge pull request #279 from IlmariKu/patch-1
Example on how to add the openapi-extension to your code
2 parents 81f003d + 8ea3a86 commit fa0cfdb

File tree

1 file changed

+34
-1
lines changed

1 file changed

+34
-1
lines changed

README.md

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,11 @@ It has three overloads:
123123

124124
For this to work, you need to call `extendZodWithOpenApi` once in your project.
125125

126-
Note: This should be done only once in a common-entrypoint file of your project (for example an `index.ts`/`app.ts`). If you're using tree-shaking with Webpack, mark that file as having side-effects.
126+
This should be done only once in a common-entrypoint file of your project (for example an `index.ts`/`app.ts`). If you're using tree-shaking with Webpack, mark that file as having side-effects.
127+
128+
It can be bit tricky to achieve this in your codebase, because *require* is synchronous and *import* is a async.
129+
130+
### The basic idea
127131

128132
```ts
129133
import { extendZodWithOpenApi } from '@asteasolutions/zod-to-openapi';
@@ -135,6 +139,35 @@ extendZodWithOpenApi(z);
135139
z.string().openapi({ description: 'Some string' });
136140
```
137141
142+
### Example 1: Calling the openapi-extension using tsx
143+
144+
```
145+
//zod-extend.ts
146+
147+
import { extendZodWithOpenApi } from '@asteasolutions/zod-to-openapi';
148+
import { z } from 'zod';
149+
150+
extendZodWithOpenApi(z);
151+
152+
// package.json
153+
154+
"scripts": {
155+
"start": "tsx --import ./zod-extend.ts ./index.ts",
156+
```
157+
158+
### Example 2 - require-syntax
159+
160+
```
161+
import { extendZodWithOpenApi } from '@asteasolutions/zod-to-openapi';
162+
import { z } from 'zod';
163+
164+
extendZodWithOpenApi(z);
165+
166+
const { startServer } = require('./server/start');
167+
startServer();
168+
```
169+
170+
138171
### The Registry
139172

140173
The `OpenAPIRegistry` is a utility that can be used to collect definitions which would later be passed to a `OpenApiGeneratorV3` or `OpenApiGeneratorV31` instance.

0 commit comments

Comments
 (0)