Skip to content

Commit cbd5b5e

Browse files
authored
#main expanded example section (wip)
1 parent 224bf22 commit cbd5b5e

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed

docs/examples.md

+81
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Fetching data can be done simply and safely using an **automatically-typed fetch
1313

1414
- [openapi-fetch](/openapi-fetch/) (recommended)
1515
- [openapi-typescript-fetch](https://www.npmjs.com/package/openapi-typescript-fetch) by [@ajaishankar](https://github.com/ajaishankar)
16+
- [feature-fetch](https://www.npmjs.com/package/feature-fetch) by [builder.group](https://github.com/builder-group)
1617

1718
::: tip
1819

@@ -60,6 +61,86 @@ TypeChecking in server environments can be tricky, as you’re often querying da
6061

6162
:::
6263

64+
## Hono with `@blgc/openapi-router`
65+
66+
```ts [src/router.ts]
67+
import { createHonoOpenApiRouter } from '@blgc/openapi-router';
68+
import { Hono } from 'hono';
69+
import { zValidator } from 'validation-adapters/zod';
70+
import * as z from 'zod';
71+
72+
import { paths } from './gen/v1';
73+
import { PetSchema } from './schemas';
74+
75+
export const router = new Hono();
76+
export const openApiRouter = createHonoOpenApiRouter<paths>(router);
77+
78+
openApiRouter.get('/pet/{petId}', {
79+
pathValidator: zValidator(
80+
z.object({
81+
petId: z.number()
82+
})
83+
),
84+
handler: (c) => {
85+
const { petId } = c.req.valid('param');
86+
87+
return c.json({
88+
name: 'Falko',
89+
photoUrls: []
90+
});
91+
}
92+
});
93+
94+
openApiRouter.post('/pet', {
95+
bodyValidator: zValidator(PetSchema),
96+
handler: (c) => {
97+
const { name, photoUrls } = c.req.valid('json');
98+
99+
return c.json({ name, photoUrls });
100+
}
101+
});
102+
```
103+
104+
## Express with `@blgc/openapi-router`
105+
106+
```ts [src/router.ts]
107+
import { createExpressOpenApiRouter } from '@blgc/openapi-router';
108+
import { Router } from 'express';
109+
import * as v from 'valibot';
110+
import { vValidator } from 'validation-adapters/valibot';
111+
112+
import { type paths } from './gen/v1';
113+
import { PetSchema } from './schemas';
114+
115+
export const router: Router = Router();
116+
export const openApiRouter = createExpressOpenApiRouter<paths>(router);
117+
118+
openApiRouter.get('/pet/{petId}', {
119+
pathValidator: vValidator(
120+
v.object({
121+
petId: v.number()
122+
})
123+
),
124+
handler: (req, res) => {
125+
const { petId } = req.params;
126+
127+
res.send({
128+
name: 'Falko',
129+
photoUrls: []
130+
});
131+
}
132+
});
133+
134+
openApiRouter.post('/pet', {
135+
bodyValidator: vValidator(PetSchema),
136+
handler: (req, res) => {
137+
const { name, photoUrls } = req.body;
138+
139+
res.send({ name, photoUrls });
140+
}
141+
});
142+
```
143+
63144
## Mock-Service-Worker (MSW)
64145

65146
If you are using [Mock Service Worker (MSW)](https://mswjs.io) to define your API mocks, you can use a **small, automatically-typed wrapper** around MSW, which enables you to address conflicts in your API mocks easily when your OpenAPI specification changes. Ultimately, you can have the same level of confidence in your application's API client **and** API mocks.

0 commit comments

Comments
 (0)