Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: zod-openapi #118

Merged
merged 9 commits into from
Aug 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/nine-moles-roll.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@hono/zod-openapi': patch
---

introduce Zod OpenAPI
28 changes: 28 additions & 0 deletions .github/workflows/ci-zod-openapi.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: ci-zod-openapi
on:
push:
branches: [main]
paths:
- 'packages/zod-openapi/**'
pull_request:
branches: ['*']
paths:
- 'packages/zod-openapi/**'

jobs:
ci:
runs-on: ubuntu-latest
defaults:
run:
working-directory: ./packages/zod-openapi
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: 18.x
- run: yarn install --frozen-lockfile
- name: Build zod-validator in root directory
run: yarn build:zod-validator
working-directory: .
- run: yarn build
- run: yarn test
14 changes: 10 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
"name": "hono-middleware",
"version": "0.0.0",
"description": "Third-party middleware for Hono",
"workspaces": [
"packages/*"
],
"workspaces": {
"packages": [
"packages/*"
]
},
"scripts": {
"build:hello": "yarn workspace @hono/hello build",
"build:zod-validator": "yarn workspace @hono/zod-validator build",
Expand All @@ -16,6 +18,7 @@
"build:typebox-validator": "yarn workspace @hono/typebox-validator build",
"build:medley-router": "yarn workspace @hono/medley-router build",
"build:valibot-validator": "yarn workspace @hono/valibot-validator build",
"build:zod-openapi": "yarn workspace @hono/zod-openapi build",
"build": "run-p build:*"
},
"license": "MIT",
Expand Down Expand Up @@ -43,8 +46,11 @@
"jest-environment-miniflare": "^2.10.0",
"npm-run-all": "^4.1.5",
"prettier": "^2.7.1",
"publint": "^0.2.0",
"rimraf": "^3.0.2",
"ts-jest": "^29.0.5",
"typescript": "^4.7.4"
"tsup": "^7.2.0",
"typescript": "^4.7.4",
"vitest": "^0.34.2"
}
}
127 changes: 127 additions & 0 deletions packages/zod-openapi/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
# Zod OpenAPI Hono

A wrapper class for Hono that supports OpenAPI. With it, you can validate values and types using [Zod](https://zod.dev/) and generate OpenAPI Swagger documentation.
This is based on [Zod to OpenAPI](https://github.com/asteasolutions/zod-to-openapi).
For details on creating schemas and defining routes, please refer to this resource.

_This is not a middleware but hosted on this monorepo_

## Usage

### Install

```
npm i hono zod @hono/zod-openapi
```

### Write your application

Define schemas:

```ts
import { z } from '@hono/zod-openapi'

const ParamsSchema = z.object({
id: z
.string()
.min(3)
.openapi({
param: {
name: 'id',
in: 'path',
},
example: '1212121',
}),
})

const UserSchema = z
.object({
id: z.number().openapi({
example: 123,
}),
name: z.string().openapi({
example: 'John Doe',
}),
age: z.number().openapi({
example: 42,
}),
})
.openapi('User')
```

Create routes:

```ts
import { createRoute } from '@hono/zod-openapi'

const route = createRoute({
method: 'get',
path: '/users/:id',
request: {
params: ParamsSchema,
},
responses: {
200: {
content: {
'application/json': {
schema: UserSchema,
},
},
description: 'Get the user',
},
400: {
content: {
'application/json': {
schema: ErrorSchema,
},
},
description: 'Error!',
},
},
})
```

Create the App:

```ts
const app = new OpenAPIHono()

app.openapi(
route,
(c) => {
const { id } = c.req.valid('param')
return c.jsonT({
id: Number(id),
age: 20,
name: 'Ultra-man',
})
},
(result, c) => {
if (!result.success) {
const res = c.jsonT(
{
ok: false,
},
400
)
return res
}
}
)

app.doc('/doc', {
openapi: '3.0.0',
info: {
version: '1.0.0',
title: 'My API',
},
})
```

## Author

Yusuke Wada <https://github.com/yusukebe>

## License

MIT
44 changes: 44 additions & 0 deletions packages/zod-openapi/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"name": "@hono/zod-openapi",
"version": "0.0.0",
"description": "A wrapper class of Hono which supports OpenAPI.",
"type": "module",
"main": "dist/index.js",
"module": "dist/index.mjs",
"types": "dist/index.d.ts",
"files": [
"dist"
],
"scripts": {
"test": "vitest run",
"build": "tsup ./src/index.ts --format esm,cjs --dts",
"publint": "publint",
"release": "yarn build && yarn test && yarn publint && yarn publish"
},
"license": "MIT",
"private": false,
"publishConfig": {
"registry": "https://registry.npmjs.org",
"access": "public"
},
"repository": {
"type": "git",
"url": "https://github.com/honojs/middleware.git"
},
"homepage": "https://github.com/honojs/middleware",
"peerDependencies": {
"hono": "*",
"zod": "3.*"
},
"devDependencies": {
"hono": "^3.4.3",
"zod": "^3.22.1"
},
"dependencies": {
"@asteasolutions/zod-to-openapi": "^5.5.0",
"@hono/zod-validator": "^0.1.7"
},
"engines": {
"node": ">=16.0.0"
}
}
Loading