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 Hono.
module.exports = {petstore: {input: {target: './petstore.yaml',},output: {mode: 'split',client: 'hono',target: 'src/petstore.ts',override: {hono: {handlers: 'src/handlers',},},},},};
Currently, Please note that the hono
client only works in split
mode.
orval
generates a file like the following:
src/├── handlers│ ├── createPets.ts│ ├── listPets.ts│ ├── showPetById.ts│ └── updatePets.ts├── index.ts├── petstore.context.ts├── petstore.schemas.ts├── petstore.ts├── petstore.validator.ts└── petstore.zod.ts
Orval
generates a handler template for Hono
. For example, check out listPets.ts
.
Validation is defined for request and response. Only the actual processing is not implemented.
import { createFactory } from 'hono/factory';import { zValidator } from '../petstore.validator';import { ListPetsContext } from '../petstore.context';import { listPetsQueryParams, listPetsResponse } from '../petstore.zod';const factory = createFactory();export const listPetsHandlers = factory.createHandlers(zValidator('query', listPetsQueryParams),zValidator('response', listPetsResponse),async (c: ListPetsContext) => {},);
You can implement the API just by defining the response according to the response schema.
import { createFactory } from 'hono/factory';import { zValidator } from '../petstore.validator';import { ListPetsContext } from '../petstore.context';import { listPetsQueryParams, listPetsResponse } from '../petstore.zod';const factory = createFactory();export const listPetsHandlers = factory.createHandlers(zValidator('query', listPetsQueryParams),zValidator('response', listPetsResponse),async (c: ListPetsContext) => {+ return c.json([+ {+ id: 1,+ name: 'doggie',+ },+ ]);},);
Hono
dev serveryou can run and check by wrangler dev
commnad.
The entrypoint is src/petstore.ts
instead of src/index.ts
.
yarn wrangler dev src/petstore.tscurl http://localhost:8787/pets #=> [{"id":1,"name":"doggie"}]
Checkout here the full example. And if you want to develop both the frontend and backend with Typescript
using Hono
, fetch
, and Next.js
, please check here as well.