Skip to content
Merged

v0.5 #129

Show file tree
Hide file tree
Changes from 38 commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
7e6fffc
chore: basic cleanup
MathurAditya724 Jun 20, 2025
49cdb4c
chore: major rearragements
MathurAditya724 Jun 20, 2025
57e92e7
chore: changed the folders
MathurAditya724 Jun 20, 2025
ee20b81
feat: major changes
MathurAditya724 Jun 29, 2025
9b1bdf7
chore: renamed openapi file to handler
MathurAditya724 Jun 29, 2025
4b6e90c
chore: made some adjustments
MathurAditya724 Jun 29, 2025
d25fc95
chore: minor changes
MathurAditya724 Jun 29, 2025
3401f37
chore: wip
MathurAditya724 Jun 29, 2025
b0b5d75
chore: formatted the code
MathurAditya724 Jun 29, 2025
7583d15
wip
MathurAditya724 Jul 2, 2025
4093630
wip
MathurAditya724 Jul 4, 2025
428e150
chore: added a basic test
MathurAditya724 Jul 4, 2025
1c9d722
feat: added more test and resolved issues
MathurAditya724 Jul 5, 2025
5a7eaf0
feat: added the route handler
MathurAditya724 Jul 5, 2025
aabea71
feat: formated the code and corrected the entry file location
MathurAditya724 Jul 5, 2025
18e2069
chore: minor changes
MathurAditya724 Jul 5, 2025
71c1737
chore: update @standard-community/standard-openapi package
MathurAditya724 Jul 17, 2025
4be8b45
feat: corrected the metadata implmentation
MathurAditya724 Jul 17, 2025
8a8a1bb
chore: released rc 0
MathurAditya724 Jul 17, 2025
f13bb72
chore: exported openAPIRouteHandler
MathurAditya724 Jul 17, 2025
dead7c5
chore: release RC 1
MathurAditya724 Jul 17, 2025
8013817
feat: added workflow
MathurAditya724 Jul 17, 2025
62a91ab
chore: made options partial in openAPIRouteHandler
MathurAditya724 Jul 22, 2025
381ccd7
wip
MathurAditya724 Jul 22, 2025
96e0d96
feat: completed the response handler middleware
MathurAditya724 Jul 22, 2025
4d9390d
chore: formatting changes
MathurAditya724 Jul 22, 2025
78306fb
chore: release rc 2
MathurAditya724 Jul 22, 2025
3ce882d
chore: release rc 3
MathurAditya724 Jul 26, 2025
0968bdf
fix: corrected the tests
MathurAditya724 Jul 26, 2025
b0f8601
fix: corrected the description position in the schema and minor changes
MathurAditya724 Sep 2, 2025
36d5d68
chore: minor change
MathurAditya724 Sep 2, 2025
2a7083f
fix: passing option to the OpenAPI function
MathurAditya724 Sep 2, 2025
5be5dad
fix: added dynamic operationId and improved the hidden fn
MathurAditya724 Sep 2, 2025
71ab48c
chore: updated lockfile
MathurAditya724 Sep 2, 2025
c484087
fix: improved the ref in params
MathurAditya724 Sep 3, 2025
fe89e3f
fix: added option for custom media type for request body
MathurAditya724 Sep 3, 2025
13897b5
chore: minor change
MathurAditya724 Sep 3, 2025
070e836
releasing RC 4
MathurAditya724 Sep 3, 2025
990c19d
Update src/__tests__/zodv4.test.ts
MathurAditya724 Sep 4, 2025
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
14 changes: 0 additions & 14 deletions .czrc

This file was deleted.

25 changes: 25 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: Run Tests

on: push

jobs:
test:
runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Setup Node.js LTS
uses: actions/setup-node@v4
with:
node-version: 'lts/*'

- name: Install PNPM
uses: pnpm/action-setup@v4

- name: Install dependencies
run: pnpm install

- name: Run tests
run: pnpm test
5 changes: 1 addition & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,4 @@ testem.log

# System Files
.DS_Store
Thumbs.db

.nx/cache
.nx/workspace-data
Thumbs.db
1 change: 0 additions & 1 deletion .husky/pre-commit

This file was deleted.

4 changes: 0 additions & 4 deletions .nano-staged.js

This file was deleted.

2 changes: 0 additions & 2 deletions .npmrc

This file was deleted.

28 changes: 0 additions & 28 deletions .verdaccio/config.yml

This file was deleted.

3 changes: 0 additions & 3 deletions .vscode/extensions.json

This file was deleted.

6 changes: 6 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"editor.codeActionsOnSave": {
"source.organizeImports.biome": "explicit",
"source.fixAll.biome": "explicit"
}
}
256 changes: 3 additions & 253 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,269 +3,19 @@
[![Ask DeepWiki](https://deepwiki.com/badge.svg)](https://deepwiki.com/rhinobase/hono-openapi)
[![npm version](https://img.shields.io/npm/v/hono-openapi.svg)](https://npmjs.org/package/hono-openapi "View this project on NPM")
[![npm downloads](https://img.shields.io/npm/dm/hono-openapi)](https://www.npmjs.com/package/hono-openapi)
[![license](https://img.shields.io/npm/l/hono-openapi)](LICENSE)

This can automatically generate the OpenAPI specification for the Hono API using your validation schema, which can be used to generate client libraries, documentation, and more.

Supported Validation Libraries:
This lib supports all the validation libs which are [Standard Schema](https://standardschema.dev/) compliant.

- [x] [Zod](https://zod.dev/)
- [x] [Valibot](https://valibot.dev/)
- [x] [ArkType](https://arktype.io/)
- [x] [TypeBox](https://github.com/sinclairzx81/typebox)
- [x] [Effect](https://effect.website/docs/schema/introduction/)
For documentation visit [honohub.dev](https://honohub.dev).

> [!Note]
> This package is still in development and your feedback is highly appreciated. If you have any suggestions or issues, please let us know by creating an issue on GitHub.

## Usage

### Installation

You can install the package using favorite package manager.

#### For Zod

```bash
pnpm add hono-openapi @hono/zod-validator zod zod-openapi
```

#### For Valibot

```bash
pnpm add hono-openapi @hono/valibot-validator valibot @valibot/to-json-schema
```

#### For ArkType

```bash
pnpm add hono-openapi @hono/arktype-validator arktype
```

#### For TypeBox

```bash
pnpm add hono-openapi @hono/typebox-validator @sinclair/typebox
```

#### For Effect

```bash
pnpm add hono-openapi @hono/effect-validator effect
```

> [!IMPORTANT]
>
> Requires `effect@^3.10.0`.
> Also, use the `Schema` class from the `effect` package, as `@effect/schema` is not supported.

### Basic Usage

#### Setting up your application

First, define your schemas, here is an example using Zod:

```ts
import z from "zod";

// For extending the Zod schema with OpenAPI properties
import "zod-openapi/extend";

const querySchema = z
.object({
name: z.string().optional().openapi({ example: "Steven" }),
})
.openapi({ ref: "Query" });

const responseSchema = z.string().openapi({ example: "Hello Steven!" });
```

Extending the Zod schema with OpenAPI properties is optional, but it will help you generate the OpenAPI specification. You can learn more about it here - [https://github.com/samchungy/zod-openapi](https://github.com/samchungy/zod-openapi).

> [!Tip]
> The `querySchema` schema will be registered as "#/components/schemas/Query" refs in the OpenAPI document. If you want to register the schema as referenced components, use .openapi() method.

Next, create your route -

```ts
import { Hono } from "hono";
import { describeRoute } from "hono-openapi";
import { resolver, validator as zValidator } from "hono-openapi/zod";

const app = new Hono();

app.get(
"/",
describeRoute({
description: "Say hello to the user",
responses: {
200: {
description: "Successful greeting response",
content: {
"text/plain": {
schema: resolver(responseSchema),
},
},
},
},
}),
zValidator("query", querySchema),
(c) => {
const query = c.req.valid("query");
return c.text(`Hello ${query?.name ?? "Hono"}!`);
}
);
```

You might be wondering why are we importing `validator` from `hono-openapi/zod` instead of `@hono/zod-validator` and as `zValidator`? This is because `hono-openapi` provides a wrapper around the `@hono/zod-validator` to make it easier to use. The idea is if you are already using `@hono/zod-validator` to validate your schemas, you can easily switch to `hono-openapi` without changing much of your code.

Finally, generate the OpenAPI specification -

```ts
app.get(
"/openapi",
openAPISpecs(app, {
documentation: {
info: {
title: "Hono",
version: "1.0.0",
description: "API for greeting users",
},
servers: [
{
url: "http://localhost:3000",
description: "Local server",
},
],
},
})
);
```

Now, you can access the OpenAPI specification by visiting `http://localhost:3000/openapi`, and you can use this specification to generate client libraries, documentation, and more. Some tools that I used to generate documentation are -

- [Swagger UI](https://github.com/honojs/middleware/tree/main/packages/swagger-ui)
- [Scalar](https://www.npmjs.com/package/@scalar/hono-api-reference)

##### Scalar Example

```ts
app.get(
"/docs",
Scalar({
theme: "saturn",
url: "/openapi",
})
);
```

And that's it! You have successfully generated the OpenAPI specification for your Hono API.

### Advanced Usage

#### Adding Security Definitions

You can add security definitions to your OpenAPI specification by using the `security` property in the `openAPISpecs` function.

```ts
app.get(
"/openapi",
openAPISpecs(appRouter, {
documentation: {
info: {
title: "Rhinobase Cloud",
version: "1.0.0",
description: "API Documentation",
},
components: {
securitySchemes: {
bearerAuth: {
type: "http",
scheme: "bearer",
bearerFormat: "JWT",
},
},
},
security: [
{
bearerAuth: [],
},
],
servers: [
{
url: "http://localhost:3004",
description: "Local server",
},
],
},
})
);
```

#### Conditionaly Hiding Routes

You can conditionally hide routes from the OpenAPI specification by using the `hide` property in the `describeRoute` function.

```ts
app.get(
"/",
describeRoute({
// ...
hide: process.env.NODE_ENV === "production",
}),
(c) => {
return c.text("Private Route");
}
);
```

#### Validating Responses

> [!Warning]
> Experimental

You can validate the responses using the `validateResponse` property in the `describeRoute` function. This will validate the response against the schema and return an error if the response is invalid.

```ts
app.get(
"/",
describeRoute({
// ...
validateResponse: true,
}),
(c) => {
return c.json({ message: "This response will be validated" });
}
);
```

#### Persisting OpenAPI Spec to a file

You can save the spec to a file for cache or any other external use.

```ts
import fs from 'node:fs';
import { openAPISpecs, generateSpecs } from 'hono-openapi';

const options = {/* ... */};
const app = new Hono()
.get(
"/openapi",
openAPISpecs(app, options),
);

generateSpecs(app, options)
.then(spec => {
const pathToSpec = "openapi.json"
fs.writeFileSync(pathToSpec, JSON.stringify(spec, null, 2));
})
```

## Contributing

We would love to have more contributors involved!

To get started, please read our [Contributing Guide](https://github.com/rhinobase/hono-openapi/blob/main/CONTRIBUTING.md).
Visit our [contributing docs](https://github.com/rhinobase/hono-openapi/blob/main/CONTRIBUTING.md).

## Credits

Expand Down
Loading