@rcmade/hono-docs 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +302 -0
- package/dist/cli/index.d.mts +1 -0
- package/dist/cli/index.d.ts +1 -0
- package/dist/cli/index.js +464 -0
- package/dist/cli/index.js.map +1 -0
- package/dist/cli/index.mjs +449 -0
- package/dist/cli/index.mjs.map +1 -0
- package/dist/core/index.d.mts +35 -0
- package/dist/core/index.d.ts +35 -0
- package/dist/core/index.js +461 -0
- package/dist/core/index.js.map +1 -0
- package/dist/core/index.mjs +432 -0
- package/dist/core/index.mjs.map +1 -0
- package/dist/index.d.mts +2 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +461 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +432 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +76 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 [Rcmade]
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the “Software”), to deal
|
|
7
|
+
in the Software without restriction—including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software—and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,302 @@
|
|
|
1
|
+
# @rcmade/hono-docs
|
|
2
|
+
|
|
3
|
+
> Auto-generate OpenAPI 3.0 spec and TypeScript type snapshots from Hono route type definitions
|
|
4
|
+
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
## Features
|
|
8
|
+
|
|
9
|
+
- **One-stop CLI** (`hono-docs generate`) to:
|
|
10
|
+
- Extract your route `AppType` definitions via **ts-morph**
|
|
11
|
+
- Emit `.d.ts` “snapshot” files per API prefix under `output/types`
|
|
12
|
+
- Generate a merged `openapi.json` spec at your configured output path
|
|
13
|
+
- Full TypeScript support (TS & JS config files, inference via `defineConfig`)
|
|
14
|
+
|
|
15
|
+
---
|
|
16
|
+
|
|
17
|
+
## Table of Contents
|
|
18
|
+
|
|
19
|
+
- [@rcmade/hono-docs](#rcmadehono-docs)
|
|
20
|
+
- [Features](#features)
|
|
21
|
+
- [Table of Contents](#table-of-contents)
|
|
22
|
+
- [Install](#install)
|
|
23
|
+
- [Quick Start](#quick-start)
|
|
24
|
+
- [Serving the OpenAPI Docs](#serving-the-openapi-docs)
|
|
25
|
+
- [Configuration](#configuration)
|
|
26
|
+
- [CLI Usage](#cli-usage)
|
|
27
|
+
- [Programmatic Usage](#programmatic-usage)
|
|
28
|
+
- [Examples](#examples)
|
|
29
|
+
- [Development](#development)
|
|
30
|
+
- [Contributing](#contributing)
|
|
31
|
+
- [License](#license)
|
|
32
|
+
|
|
33
|
+
---
|
|
34
|
+
|
|
35
|
+
## Install
|
|
36
|
+
|
|
37
|
+
```bash
|
|
38
|
+
# using npm
|
|
39
|
+
npm install --save-dev @rcmade/hono-docs
|
|
40
|
+
|
|
41
|
+
# using pnpm
|
|
42
|
+
pnpm add -D @rcmade/hono-docs
|
|
43
|
+
|
|
44
|
+
# using yarn
|
|
45
|
+
yarn add -D @rcmade/hono-docs
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
---
|
|
49
|
+
|
|
50
|
+
## Quick Start
|
|
51
|
+
|
|
52
|
+
1. **Create a config file** at the root of your project (`hono-docs.ts`):
|
|
53
|
+
|
|
54
|
+
```ts
|
|
55
|
+
import { defineConfig } from "@rcmade/hono-docs";
|
|
56
|
+
|
|
57
|
+
export default defineConfig({
|
|
58
|
+
tsConfigPath: "./tsconfig.json",
|
|
59
|
+
openApi: {
|
|
60
|
+
openapi: "3.0.0",
|
|
61
|
+
info: { title: "My API", version: "1.0.0" },
|
|
62
|
+
servers: [{ url: "http://localhost:3000" }],
|
|
63
|
+
},
|
|
64
|
+
outputs: {
|
|
65
|
+
openApiJson: "./openapi/openapi.json",
|
|
66
|
+
},
|
|
67
|
+
apis: [
|
|
68
|
+
{
|
|
69
|
+
name: "Auth Routes",
|
|
70
|
+
apiPrefix: "/auth", // This will be prepended to all `api` values below
|
|
71
|
+
appTypePath: "src/routes/authRoutes.ts", // Path to your AppType export
|
|
72
|
+
|
|
73
|
+
api: [
|
|
74
|
+
// ✅ Custom OpenAPI metadata for the GET /auth/u/{id} endpoint
|
|
75
|
+
{
|
|
76
|
+
api: "/u/{id}", // Final route = /auth/u/{id}
|
|
77
|
+
method: "get",
|
|
78
|
+
summary: "Fetch user by ID", // Optional: title shown in docs
|
|
79
|
+
description: "Returns a user object based on the provided ID.",
|
|
80
|
+
tag: ["User"],
|
|
81
|
+
},
|
|
82
|
+
|
|
83
|
+
// ✅ Another example with metadata for GET /auth
|
|
84
|
+
{
|
|
85
|
+
api: "/", // Final route = /auth/
|
|
86
|
+
method: "get",
|
|
87
|
+
summary: "Get current user",
|
|
88
|
+
description:
|
|
89
|
+
"Returns the currently authenticated user's information.",
|
|
90
|
+
tag: ["User Info"],
|
|
91
|
+
},
|
|
92
|
+
],
|
|
93
|
+
},
|
|
94
|
+
],
|
|
95
|
+
});
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
2. **Route Definitions & AppType**
|
|
99
|
+
This library supports **only change routes** via a single AppType export in your routes file. You **must** export:
|
|
100
|
+
|
|
101
|
+
```ts
|
|
102
|
+
export type AppType = typeof yourRoutesVariable;
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
**Example:**
|
|
106
|
+
|
|
107
|
+
```ts
|
|
108
|
+
// src/routes/userRoutes.ts
|
|
109
|
+
import { Hono } from "hono";
|
|
110
|
+
import { z } from "zod";
|
|
111
|
+
|
|
112
|
+
export const userRoutes = new Hono()
|
|
113
|
+
.get("/u/:id", (c) => {
|
|
114
|
+
/* … */
|
|
115
|
+
})
|
|
116
|
+
.post("/", async (c) => {
|
|
117
|
+
/* … */
|
|
118
|
+
});
|
|
119
|
+
// Must add AppType
|
|
120
|
+
export type AppType = typeof userRoutes;
|
|
121
|
+
export default userRoutes;
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
---
|
|
125
|
+
|
|
126
|
+
1. **Add an npm script** to `package.json`:
|
|
127
|
+
|
|
128
|
+
```jsonc
|
|
129
|
+
{
|
|
130
|
+
"scripts": {
|
|
131
|
+
"docs": "hono-docs generate --config ./hono-docs.ts"
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
2. **Run the CLI**:
|
|
137
|
+
|
|
138
|
+
```bash
|
|
139
|
+
npm run docs
|
|
140
|
+
# or
|
|
141
|
+
npx hono-docs generate --config ./hono-docs.ts
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
You’ll see:
|
|
145
|
+
|
|
146
|
+
```text
|
|
147
|
+
⏳ Generating Type Snapshots…
|
|
148
|
+
✅ Wrote: node_modules/@rcmade/hono-docs/output/types/user.d.ts
|
|
149
|
+
⏳ Generating OpenAPI Spec…
|
|
150
|
+
✅ OpenAPI written to ./openapi/openapi.json
|
|
151
|
+
🎉 Done
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
## Serving the OpenAPI Docs
|
|
155
|
+
|
|
156
|
+
Install the viewer:
|
|
157
|
+
|
|
158
|
+
```bash
|
|
159
|
+
# npm
|
|
160
|
+
npm install @scalar/hono-api-reference
|
|
161
|
+
|
|
162
|
+
# yarn
|
|
163
|
+
yarn add @scalar/hono-api-reference
|
|
164
|
+
|
|
165
|
+
# pnpm
|
|
166
|
+
pnpm add @scalar/hono-api-reference
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
Mount in your Hono app:
|
|
170
|
+
|
|
171
|
+
```ts
|
|
172
|
+
// src/routes/docs.ts
|
|
173
|
+
import { Hono } from "hono";
|
|
174
|
+
import { Scalar } from "@scalar/hono-api-reference";
|
|
175
|
+
import fs from "node:fs/promises";
|
|
176
|
+
import path from "node:path";
|
|
177
|
+
|
|
178
|
+
const docs = new Hono()
|
|
179
|
+
.get(
|
|
180
|
+
"/",
|
|
181
|
+
Scalar({
|
|
182
|
+
url: "/api/docs/open-api",
|
|
183
|
+
theme: "kepler",
|
|
184
|
+
layout: "modern",
|
|
185
|
+
defaultHttpClient: { targetKey: "js", clientKey: "axios" },
|
|
186
|
+
})
|
|
187
|
+
)
|
|
188
|
+
.get("/open-api", async (c) => {
|
|
189
|
+
const raw = await fs.readFile(
|
|
190
|
+
path.join(process.cwd(), "./openapi/openapi.json"),
|
|
191
|
+
"utf-8"
|
|
192
|
+
);
|
|
193
|
+
return c.json(JSON.parse(raw));
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
export type AppType = typeof docs;
|
|
197
|
+
export default docs;
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
In `src/index.ts`:
|
|
201
|
+
|
|
202
|
+
```ts
|
|
203
|
+
import { Hono } from "hono";
|
|
204
|
+
import docs from "./routes/docs";
|
|
205
|
+
import userRoutes from "./routes/userRoutes";
|
|
206
|
+
|
|
207
|
+
export default new Hono()
|
|
208
|
+
.basePath("/api")
|
|
209
|
+
.route("/docs", docs)
|
|
210
|
+
.route("/user", userRoutes);
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
Visiting `/api/docs` shows the UI; `/api/docs/open-api` serves the JSON.
|
|
214
|
+
|
|
215
|
+
---
|
|
216
|
+
|
|
217
|
+
## Configuration
|
|
218
|
+
|
|
219
|
+
All options live in your `defineConfig({ ... })` object:
|
|
220
|
+
|
|
221
|
+
| Field | Type | Required | Description |
|
|
222
|
+
| ------------------------ | -------------------------------------------------------- | -------- | ----------------------------------------------------------------- |
|
|
223
|
+
| `tsConfigPath` | `string` | Yes | Path to your project’s `tsconfig.json` |
|
|
224
|
+
| `openApi` | `object` | Yes | Base OpenAPI fields |
|
|
225
|
+
| `openApi.openapi` | `string` | Yes | OpenAPI version (e.g. `"3.0.0"`) |
|
|
226
|
+
| `openApi.info` | `{ title: string; version: string }` | Yes | API title & version |
|
|
227
|
+
| `openApi.servers` | `Array<{ url: string }>` | Yes | List of server URL objects |
|
|
228
|
+
| `outputs` | `object` | Yes | Output paths |
|
|
229
|
+
| `outputs.openApiJson` | `string` | Yes | File path for generated `openapi.json` |
|
|
230
|
+
| `apis` | `Array<ApiBlock>` | Yes | One block per group of routes |
|
|
231
|
+
| ‐ `ApiBlock.name` | `string` | Yes | Human-readable name for logging |
|
|
232
|
+
| ‐ `ApiBlock.apiPrefix` | `string` | Yes | Base path prefix (e.g. `/user`) |
|
|
233
|
+
| ‐ `ApiBlock.appTypePath` | `string` | Yes | Path to the TS file exporting `type AppType = yourRoutesVariable` |
|
|
234
|
+
| ‐ `ApiBlock.api` | `Array<{ api: string; method: string; tag?: string[] }>` | No | Methods to include; defaults to all in `AppType` if omitted |
|
|
235
|
+
| `preDefineTypeContent` | `string` | No | Content injected at top of each generated `.d.ts` snapshot |
|
|
236
|
+
|
|
237
|
+
---
|
|
238
|
+
|
|
239
|
+
## CLI Usage
|
|
240
|
+
|
|
241
|
+
```text
|
|
242
|
+
Usage: hono-docs generate --config <path> [--output <file>]
|
|
243
|
+
|
|
244
|
+
Options:
|
|
245
|
+
-c, --config Path to your config file (TS or JS) [string] [required]
|
|
246
|
+
-h, --help Show help [boolean]
|
|
247
|
+
```
|
|
248
|
+
|
|
249
|
+
---
|
|
250
|
+
|
|
251
|
+
## Programmatic Usage
|
|
252
|
+
|
|
253
|
+
You can use the API directly in code:
|
|
254
|
+
|
|
255
|
+
```ts
|
|
256
|
+
import { runGenerate, defineConfig } from "@rcmade/hono-docs";
|
|
257
|
+
|
|
258
|
+
(async () => {
|
|
259
|
+
const cfg = defineConfig({
|
|
260
|
+
/* ... */
|
|
261
|
+
});
|
|
262
|
+
await runGenerate(cfg as any);
|
|
263
|
+
})();
|
|
264
|
+
```
|
|
265
|
+
|
|
266
|
+
---
|
|
267
|
+
|
|
268
|
+
## Examples
|
|
269
|
+
|
|
270
|
+
Check out [`examples/basic-app/`](https://github.com/rcmade/hono-docs/examples/basic-app) for a minimal setup.
|
|
271
|
+
|
|
272
|
+
---
|
|
273
|
+
|
|
274
|
+
## Development
|
|
275
|
+
|
|
276
|
+
1. Clone & install dependencies:
|
|
277
|
+
|
|
278
|
+
git clone [https://github.com/rcmade/hono-docs.git](https://github.com/rcmade/hono-docs.git)
|
|
279
|
+
cd hono-docs
|
|
280
|
+
pnpm install
|
|
281
|
+
|
|
282
|
+
````
|
|
283
|
+
2. Implement or modify code under `src/`.
|
|
284
|
+
3. Build and watch: pnpm build --watch
|
|
285
|
+
````
|
|
286
|
+
|
|
287
|
+
4. Test locally via `npm link` or `file:` install in a demo project.
|
|
288
|
+
|
|
289
|
+
---
|
|
290
|
+
|
|
291
|
+
## Contributing
|
|
292
|
+
|
|
293
|
+
1. Fork the repo
|
|
294
|
+
2. Create a feature branch
|
|
295
|
+
3. Open a PR with a clear description & tests
|
|
296
|
+
4. Ensure tests pass & linting is clean
|
|
297
|
+
|
|
298
|
+
---
|
|
299
|
+
|
|
300
|
+
## License
|
|
301
|
+
|
|
302
|
+
[MIT](./LICENSE)
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
#!/usr/bin/env node
|