Skip to content

Commit 2360e1a

Browse files
docs: improve readme
1 parent 96d8922 commit 2360e1a

File tree

1 file changed

+106
-0
lines changed

1 file changed

+106
-0
lines changed

README.md

+106
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,109 @@ Options:
3838
-e, --ext <value> file extension to use for generated files (default: ".ts")
3939
-h, --help display help for command
4040
```
41+
42+
## Project example
43+
44+
Here is a project structure that I'm usually using with that module.
45+
Each endpoint has a folder with a `schema.yaml` and `index.ts`
46+
47+
```
48+
package.json
49+
src/
50+
app.ts
51+
api/
52+
healtheck/
53+
index.ts
54+
schema.yaml
55+
anotherEndpoint/
56+
index.ts
57+
schema.yaml
58+
...
59+
```
60+
61+
Running `npx fastify-schema-to-typescript` will convert `schema.yaml` to `schema.ts` that you can then you in your apps like that
62+
63+
app.ts
64+
65+
```
66+
import fastify from 'fastify';
67+
import { healthcheck } from './api'
68+
69+
export async function run() {
70+
const app = fastify();
71+
await app.register(healthcheck);
72+
73+
await app.listen(3000);
74+
}
75+
76+
run();
77+
```
78+
79+
api/index.ts
80+
81+
```
82+
export * from './healthcheck';
83+
```
84+
85+
api/healtheck/schema.yaml
86+
87+
```
88+
headers:
89+
type: object
90+
properties:
91+
...
92+
93+
body:
94+
type: object
95+
properties:
96+
...
97+
98+
query:
99+
type: object
100+
properties:
101+
...
102+
103+
params:
104+
type: object
105+
properties:
106+
...
107+
```
108+
109+
api/healtheck/index.ts
110+
111+
```
112+
import { FastifyPluginAsync } from 'fastify';
113+
import { RouteGeneric, schema } from './schema';
114+
115+
export const getHealthcheck: FastifyPluginAsync = async (app) => {
116+
app.get<RouteGeneric>('/healthcheck', { schema }, async () => {
117+
return { ok: true };
118+
});
119+
};
120+
```
121+
122+
I usually add the following in my package.json so I'm sure the code is in syn with the schemas
123+
124+
package.json
125+
126+
```
127+
...
128+
"scripts": {
129+
...
130+
"build": "npm run clean && npm run generate && npm run compile",
131+
"compile": "tsc",
132+
"clean": "rimraf dist",
133+
"generate": "npx fastify-schema-to-typescript",
134+
...
135+
}
136+
```
137+
138+
and I also update `.gitignore` to not include the generate `schema.ts`
139+
140+
.gitignore
141+
142+
```
143+
...
144+
schema.ts
145+
...
146+
```

0 commit comments

Comments
 (0)