|
| 1 | +--- |
| 2 | +id: hono |
| 3 | +title: Hono |
| 4 | +--- |
| 5 | + |
| 6 | +If you want to generate a hono, define the `client` property to `hono` and a template of `Hono` will be generated in the target file and directory. You can check <a href="https://hono.dev/docs/getting-started/cloudflare-workers" target="_blank">Hono</a>. |
| 7 | + |
| 8 | +#### Example of orval.config.js |
| 9 | + |
| 10 | +```js |
| 11 | +module.exports = { |
| 12 | + petstore: { |
| 13 | + input: { |
| 14 | + target: './petstore.yaml', |
| 15 | + }, |
| 16 | + output: { |
| 17 | + mode: 'split', |
| 18 | + client: 'hono', |
| 19 | + target: 'src/petstore.ts', |
| 20 | + override: { |
| 21 | + hono: { |
| 22 | + handlers: 'src/handlers', |
| 23 | + }, |
| 24 | + }, |
| 25 | + }, |
| 26 | + }, |
| 27 | +}; |
| 28 | +``` |
| 29 | + |
| 30 | +Currently, Please note that the `hono` client only works in `split` mode. |
| 31 | + |
| 32 | +#### generate template |
| 33 | + |
| 34 | +`orval` generates a file like the following: |
| 35 | + |
| 36 | +``` |
| 37 | +src/ |
| 38 | +├── handlers |
| 39 | +│ ├── createPets.ts |
| 40 | +│ ├── listPets.ts |
| 41 | +│ ├── showPetById.ts |
| 42 | +│ └── updatePets.ts |
| 43 | +├── index.ts |
| 44 | +├── petstore.context.ts |
| 45 | +├── petstore.schemas.ts |
| 46 | +├── petstore.ts |
| 47 | +├── petstore.validator.ts |
| 48 | +└── petstore.zod.ts |
| 49 | +``` |
| 50 | + |
| 51 | +- petstore.ts: Initializes hono and defines endpoints. |
| 52 | +- handlers: Contains templates for each endpoint. |
| 53 | +- petstore.schemas.ts: Defines request and response schemas. |
| 54 | +- petstore.validator.ts: Implements hono validator. |
| 55 | +- petstore.zod.ts: Defines schemas using zod for validation. |
| 56 | +- petstore.context.ts: Defines context for endpoints. |
| 57 | + |
| 58 | +#### implement endpoint proccess to handler |
| 59 | + |
| 60 | +`Orval` generates a handler template for `Hono`. For example, check out `listPets.ts`. |
| 61 | +Validation is defined for request and response. Only the actual processing is not implemented. |
| 62 | + |
| 63 | +```ts |
| 64 | +import { createFactory } from 'hono/factory'; |
| 65 | +import { zValidator } from '../petstore.validator'; |
| 66 | +import { ListPetsContext } from '../petstore.context'; |
| 67 | +import { listPetsQueryParams, listPetsResponse } from '../petstore.zod'; |
| 68 | + |
| 69 | +const factory = createFactory(); |
| 70 | + |
| 71 | +export const listPetsHandlers = factory.createHandlers( |
| 72 | + zValidator('query', listPetsQueryParams), |
| 73 | + zValidator('response', listPetsResponse), |
| 74 | + async (c: ListPetsContext) => {}, |
| 75 | +); |
| 76 | +``` |
| 77 | + |
| 78 | +You can implement the API just by defining the response according to the response schema. |
| 79 | + |
| 80 | +```diff |
| 81 | +import { createFactory } from 'hono/factory'; |
| 82 | +import { zValidator } from '../petstore.validator'; |
| 83 | +import { ListPetsContext } from '../petstore.context'; |
| 84 | +import { listPetsQueryParams, listPetsResponse } from '../petstore.zod'; |
| 85 | + |
| 86 | +const factory = createFactory(); |
| 87 | + |
| 88 | +export const listPetsHandlers = factory.createHandlers( |
| 89 | + zValidator('query', listPetsQueryParams), |
| 90 | + zValidator('response', listPetsResponse), |
| 91 | + async (c: ListPetsContext) => { |
| 92 | ++ return c.json([ |
| 93 | ++ { |
| 94 | ++ id: 1, |
| 95 | ++ name: 'doggie', |
| 96 | ++ }, |
| 97 | ++ ]); |
| 98 | + }, |
| 99 | +); |
| 100 | +``` |
| 101 | + |
| 102 | +#### run `Hono` dev server |
| 103 | + |
| 104 | +you can run and check by `wrangler dev` commnad. |
| 105 | +The entrypoint is `src/petstore.ts` instead of `src/index.ts`. |
| 106 | + |
| 107 | +```bash |
| 108 | +yarn wrangler dev src/petstore.ts |
| 109 | +curl http://localhost:8787/pets #=> [{"id":1,"name":"doggie"}] |
| 110 | +``` |
| 111 | + |
| 112 | +Checkout <a href="https://github.com/orval-labs/orval/tree/master/samples/hono/hono-with-zod" target="_blank">here</a> the full example. And if you want to develop both the frontend and backend with `Typescript` using `Hono`, `fetch`, and `Next.js`, please check <a href="https://github.com/orval-labs/orval/tree/master/samples/hono/hono-with-fetch-client" target="_blank">here</a> as well. |
0 commit comments