hono-route-docs 0.1.0
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 +295 -0
- package/dist/index.cjs +170 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +6 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +165 -0
- package/dist/index.js.map +1 -0
- package/dist/standard.cjs +203 -0
- package/dist/standard.cjs.map +1 -0
- package/dist/standard.d.cts +34 -0
- package/dist/standard.d.ts +34 -0
- package/dist/standard.js +194 -0
- package/dist/standard.js.map +1 -0
- package/dist/types-CguT4Ne5.d.cts +83 -0
- package/dist/types-CguT4Ne5.d.ts +83 -0
- package/dist/zod.cjs +215 -0
- package/dist/zod.cjs.map +1 -0
- package/dist/zod.d.cts +19 -0
- package/dist/zod.d.ts +19 -0
- package/dist/zod.js +209 -0
- package/dist/zod.js.map +1 -0
- package/package.json +105 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Gabriel Augusto
|
|
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 all
|
|
13
|
+
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 THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,295 @@
|
|
|
1
|
+
# hono-route-docs
|
|
2
|
+
|
|
3
|
+
Fluent router API with automatic OpenAPI 3.1 schema generation for [Hono](https://hono.dev). **Zero dependencies** beyond Hono itself. UI-agnostic — use [Scalar](https://scalar.com), [Swagger UI](https://swagger.io/tools/swagger-ui/), or any OpenAPI viewer.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- **Pure Hono** — No dependency on `@hono/zod-openapi` or any validation library
|
|
8
|
+
- **Fluent Router API** — Chainable `get`, `post`, `put`, `patch`, `delete` methods
|
|
9
|
+
- **Auto-generated OpenAPI 3.1 spec** — Built-in registry generates the full spec as JSON
|
|
10
|
+
- **UI-agnostic** — Use Scalar, Swagger UI, RapiDoc, or anything else
|
|
11
|
+
- **Sub-router support** — Mount sub-routers with inherited tags, security, and doc options
|
|
12
|
+
- **Auto path params** — `:id` is automatically added to the OpenAPI spec as `{id}`
|
|
13
|
+
- **Schema-agnostic** — Pass raw JSON Schema, or convert from Zod/Valibot/TypeBox/etc.
|
|
14
|
+
|
|
15
|
+
## Installation
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
npm install hono-route-docs
|
|
19
|
+
# or
|
|
20
|
+
bun add hono-route-docs
|
|
21
|
+
# or
|
|
22
|
+
pnpm add hono-route-docs
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## Quick Start
|
|
26
|
+
|
|
27
|
+
```ts
|
|
28
|
+
import { Hono } from 'hono';
|
|
29
|
+
import { createRouter } from 'hono-route-docs';
|
|
30
|
+
|
|
31
|
+
const app = new Hono();
|
|
32
|
+
const { router, get, route, doc } = createRouter();
|
|
33
|
+
|
|
34
|
+
get('/health', { summary: 'Health Check' }, c => {
|
|
35
|
+
return c.json({ status: 'ok' });
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
app.route('/api', router);
|
|
39
|
+
|
|
40
|
+
app.get(
|
|
41
|
+
'/openapi.json',
|
|
42
|
+
doc({
|
|
43
|
+
title: 'My API',
|
|
44
|
+
version: '1.0.0',
|
|
45
|
+
prefix: '/api',
|
|
46
|
+
}),
|
|
47
|
+
);
|
|
48
|
+
|
|
49
|
+
import { Scalar } from '@scalar/hono-api-reference';
|
|
50
|
+
|
|
51
|
+
app.get('/docs', Scalar({ url: '/openapi.json' }));
|
|
52
|
+
|
|
53
|
+
export default { port: 3000, fetch: app.fetch };
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
## API
|
|
57
|
+
|
|
58
|
+
### `createRouter()`
|
|
59
|
+
|
|
60
|
+
Creates a fluent router instance. Returns:
|
|
61
|
+
|
|
62
|
+
| Property | Description |
|
|
63
|
+
| ---------- | --------------------------------------------------- |
|
|
64
|
+
| `router` | The underlying `Hono` instance |
|
|
65
|
+
| `route()` | Mount a sub-router with optional doc options |
|
|
66
|
+
| `get()` | Register a GET route |
|
|
67
|
+
| `post()` | Register a POST route |
|
|
68
|
+
| `put()` | Register a PUT route |
|
|
69
|
+
| `patch()` | Register a PATCH route |
|
|
70
|
+
| `delete()` | Register a DELETE route |
|
|
71
|
+
| `doc()` | Returns a handler that serves the OpenAPI JSON spec |
|
|
72
|
+
|
|
73
|
+
All HTTP methods accept:
|
|
74
|
+
|
|
75
|
+
```ts
|
|
76
|
+
method(path, handler);
|
|
77
|
+
method(path, options, handler);
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
### `DocOptions`
|
|
81
|
+
|
|
82
|
+
Uses raw OpenAPI field names — no dependency on any validation library:
|
|
83
|
+
|
|
84
|
+
```ts
|
|
85
|
+
type DocOptions = {
|
|
86
|
+
tags?: string[];
|
|
87
|
+
summary?: string;
|
|
88
|
+
description?: string;
|
|
89
|
+
deprecated?: boolean;
|
|
90
|
+
security?: Record<string, string[]>[];
|
|
91
|
+
parameters?: OpenAPIParameter[];
|
|
92
|
+
requestBody?: OpenAPIRequestBody;
|
|
93
|
+
responses?: Record<string, OpenAPIResponse>;
|
|
94
|
+
};
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
### `openAPIDoc(router, config)`
|
|
98
|
+
|
|
99
|
+
Standalone function to create a handler that serves the spec:
|
|
100
|
+
|
|
101
|
+
```ts
|
|
102
|
+
import { openAPIDoc } from 'hono-route-docs';
|
|
103
|
+
|
|
104
|
+
app.get(
|
|
105
|
+
'/openapi.json',
|
|
106
|
+
openAPIDoc(router, {
|
|
107
|
+
title: 'My API',
|
|
108
|
+
version: '1.0.0',
|
|
109
|
+
prefix: '/api',
|
|
110
|
+
description: 'Optional description',
|
|
111
|
+
servers: [{ url: 'https://api.example.com' }],
|
|
112
|
+
components: {
|
|
113
|
+
securitySchemes: {
|
|
114
|
+
bearerAuth: { type: 'http', scheme: 'bearer' },
|
|
115
|
+
},
|
|
116
|
+
},
|
|
117
|
+
}),
|
|
118
|
+
);
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
### `generateSpec(registry, config)`
|
|
122
|
+
|
|
123
|
+
Low-level function to generate the spec object (useful for testing or custom endpoints):
|
|
124
|
+
|
|
125
|
+
```ts
|
|
126
|
+
import { generateSpec, getRegistry } from 'hono-route-docs';
|
|
127
|
+
|
|
128
|
+
const spec = generateSpec(getRegistry(router)!, config);
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
## Sub-Routers
|
|
132
|
+
|
|
133
|
+
```ts
|
|
134
|
+
import { createRouter } from 'hono-route-docs';
|
|
135
|
+
|
|
136
|
+
const { router, get, post } = createRouter();
|
|
137
|
+
|
|
138
|
+
get('/', { summary: 'List users' }, c => c.json([]));
|
|
139
|
+
|
|
140
|
+
post(
|
|
141
|
+
'/',
|
|
142
|
+
{
|
|
143
|
+
summary: 'Create user',
|
|
144
|
+
requestBody: {
|
|
145
|
+
required: true,
|
|
146
|
+
content: {
|
|
147
|
+
'application/json': {
|
|
148
|
+
schema: {
|
|
149
|
+
type: 'object',
|
|
150
|
+
properties: {
|
|
151
|
+
name: { type: 'string' },
|
|
152
|
+
email: { type: 'string', format: 'email' },
|
|
153
|
+
},
|
|
154
|
+
required: ['name', 'email'],
|
|
155
|
+
},
|
|
156
|
+
},
|
|
157
|
+
},
|
|
158
|
+
},
|
|
159
|
+
responses: { 201: { description: 'User created' } },
|
|
160
|
+
},
|
|
161
|
+
c => c.json({ id: 1 }, 201),
|
|
162
|
+
);
|
|
163
|
+
|
|
164
|
+
get('/:id', { summary: 'Get user' }, c => c.json({ id: c.req.param('id') }));
|
|
165
|
+
|
|
166
|
+
export default router;
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
```ts
|
|
170
|
+
import { createRouter } from 'hono-route-docs';
|
|
171
|
+
import usersRouter from './users/routes';
|
|
172
|
+
|
|
173
|
+
const { router, route } = createRouter();
|
|
174
|
+
|
|
175
|
+
route('/users', usersRouter, { tags: ['Users'] });
|
|
176
|
+
|
|
177
|
+
export default router;
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
## Using with Zod / Valibot / TypeBox
|
|
181
|
+
|
|
182
|
+
The base entry point accepts raw JSON Schema. Use any converter you like:
|
|
183
|
+
|
|
184
|
+
```ts
|
|
185
|
+
import { z } from 'zod';
|
|
186
|
+
|
|
187
|
+
const UserSchema = z.object({ name: z.string(), email: z.string().email() });
|
|
188
|
+
|
|
189
|
+
post(
|
|
190
|
+
'/users',
|
|
191
|
+
{
|
|
192
|
+
requestBody: {
|
|
193
|
+
content: {
|
|
194
|
+
'application/json': { schema: z.toJSONSchema(UserSchema) },
|
|
195
|
+
},
|
|
196
|
+
},
|
|
197
|
+
},
|
|
198
|
+
handler,
|
|
199
|
+
);
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
## Adapter: Zod (`/zod`)
|
|
203
|
+
|
|
204
|
+
Uses `@hono/zod-openapi` under the hood. Accepts Zod schemas directly in `request` and `responses`.
|
|
205
|
+
|
|
206
|
+
```bash
|
|
207
|
+
npm install @hono/zod-openapi zod
|
|
208
|
+
```
|
|
209
|
+
|
|
210
|
+
```ts
|
|
211
|
+
import { createRouter, openAPIDoc } from 'hono-route-docs/zod';
|
|
212
|
+
import { z } from 'zod';
|
|
213
|
+
|
|
214
|
+
const { router, get, post } = createRouter();
|
|
215
|
+
|
|
216
|
+
get(
|
|
217
|
+
'/hello',
|
|
218
|
+
{
|
|
219
|
+
tags: ['Hello'],
|
|
220
|
+
summary: 'Say hello',
|
|
221
|
+
request: {
|
|
222
|
+
query: z.object({
|
|
223
|
+
name: z.string().optional(),
|
|
224
|
+
page: z.coerce.number().default(1),
|
|
225
|
+
}),
|
|
226
|
+
},
|
|
227
|
+
responses: {
|
|
228
|
+
200: { description: 'Greeting' },
|
|
229
|
+
},
|
|
230
|
+
},
|
|
231
|
+
c => c.json({ message: 'Hello!' }),
|
|
232
|
+
);
|
|
233
|
+
```
|
|
234
|
+
|
|
235
|
+
## Adapter: Standard Schema (`/standard`)
|
|
236
|
+
|
|
237
|
+
Uses [`hono-openapi`](https://github.com/rhinobase/hono-openapi) under the hood. Works with **any** [Standard Schema](https://standardschema.dev/)–compliant validation library: Zod, Valibot, ArkType, TypeBox, Effect, etc.
|
|
238
|
+
|
|
239
|
+
```bash
|
|
240
|
+
npm install hono-openapi @hono/standard-validator
|
|
241
|
+
```
|
|
242
|
+
|
|
243
|
+
```ts
|
|
244
|
+
import { createRouter, openAPIDoc, resolver } from 'hono-route-docs/standard';
|
|
245
|
+
import * as v from 'valibot'; // or zod, arktype, typebox, etc.
|
|
246
|
+
|
|
247
|
+
const { router, get } = createRouter();
|
|
248
|
+
|
|
249
|
+
get(
|
|
250
|
+
'/hello',
|
|
251
|
+
{
|
|
252
|
+
tags: ['Hello'],
|
|
253
|
+
summary: 'Say hello',
|
|
254
|
+
request: {
|
|
255
|
+
query: v.object({ name: v.optional(v.string()) }),
|
|
256
|
+
},
|
|
257
|
+
responses: {
|
|
258
|
+
200: {
|
|
259
|
+
description: 'Greeting',
|
|
260
|
+
content: {
|
|
261
|
+
'application/json': { schema: resolver(v.string()) },
|
|
262
|
+
},
|
|
263
|
+
},
|
|
264
|
+
},
|
|
265
|
+
},
|
|
266
|
+
c => {
|
|
267
|
+
const { name } = c.req.valid('query');
|
|
268
|
+
return c.text(`Hello ${name ?? 'World'}!`);
|
|
269
|
+
},
|
|
270
|
+
);
|
|
271
|
+
|
|
272
|
+
const app = new Hono();
|
|
273
|
+
|
|
274
|
+
app.route('/api', router);
|
|
275
|
+
app.get(
|
|
276
|
+
'/openapi.json',
|
|
277
|
+
openAPIDoc(router, {
|
|
278
|
+
title: 'My API',
|
|
279
|
+
version: '1.0.0',
|
|
280
|
+
prefix: '/api',
|
|
281
|
+
}),
|
|
282
|
+
);
|
|
283
|
+
```
|
|
284
|
+
|
|
285
|
+
## Entry points summary
|
|
286
|
+
|
|
287
|
+
| Entry point | Install | Schemas |
|
|
288
|
+
| -------------------------- | ----------------------------------------- | ------------------------------------------------------------ |
|
|
289
|
+
| `hono-route-docs` | `hono` | Raw JSON Schema |
|
|
290
|
+
| `hono-route-docs/zod` | `+ @hono/zod-openapi zod` | Zod schemas |
|
|
291
|
+
| `hono-route-docs/standard` | `+ hono-openapi @hono/standard-validator` | Any Standard Schema (Zod, Valibot, ArkType, TypeBox, Effect) |
|
|
292
|
+
|
|
293
|
+
## License
|
|
294
|
+
|
|
295
|
+
MIT
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var hono = require('hono');
|
|
4
|
+
|
|
5
|
+
// src/router.ts
|
|
6
|
+
|
|
7
|
+
// src/registry.ts
|
|
8
|
+
var REGISTRY = /* @__PURE__ */ Symbol.for("hono-openapi-registry");
|
|
9
|
+
var RouteRegistry = class {
|
|
10
|
+
entries = [];
|
|
11
|
+
add(entry) {
|
|
12
|
+
this.entries.push(entry);
|
|
13
|
+
}
|
|
14
|
+
merge(prefix, other, opts) {
|
|
15
|
+
for (const entry of other.entries) {
|
|
16
|
+
const merged = {
|
|
17
|
+
...entry,
|
|
18
|
+
path: prefix + (entry.path === "/" ? "" : entry.path)
|
|
19
|
+
};
|
|
20
|
+
if (opts) {
|
|
21
|
+
const hasOwnTags = entry.tags && entry.tags.length > 0;
|
|
22
|
+
if (opts.tags && !hasOwnTags) merged.tags = opts.tags;
|
|
23
|
+
if (opts.summary && !entry.summary) merged.summary = opts.summary;
|
|
24
|
+
if (opts.description && !entry.description) merged.description = opts.description;
|
|
25
|
+
if (opts.security && !entry.security) merged.security = opts.security;
|
|
26
|
+
if (opts.deprecated !== void 0 && entry.deprecated === void 0)
|
|
27
|
+
merged.deprecated = opts.deprecated;
|
|
28
|
+
}
|
|
29
|
+
this.entries.push(merged);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
function getRegistry(router) {
|
|
34
|
+
return router?.[REGISTRY];
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// src/spec.ts
|
|
38
|
+
function honoPathToOpenAPI(path) {
|
|
39
|
+
return path.replace(/:([a-zA-Z_][a-zA-Z0-9_]*)/g, "{$1}");
|
|
40
|
+
}
|
|
41
|
+
function extractPathParams(path) {
|
|
42
|
+
const params = [];
|
|
43
|
+
const regex = /:([a-zA-Z_][a-zA-Z0-9_]*)/g;
|
|
44
|
+
let match;
|
|
45
|
+
while ((match = regex.exec(path)) !== null) {
|
|
46
|
+
params.push({
|
|
47
|
+
name: match[1],
|
|
48
|
+
in: "path",
|
|
49
|
+
required: true,
|
|
50
|
+
schema: { type: "string" }
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
return params;
|
|
54
|
+
}
|
|
55
|
+
function generateSpec(registry, config) {
|
|
56
|
+
const paths = {};
|
|
57
|
+
const prefix = config.prefix ?? "";
|
|
58
|
+
for (const entry of registry.entries) {
|
|
59
|
+
const rawPath = prefix + entry.path;
|
|
60
|
+
const fullPath = honoPathToOpenAPI(rawPath);
|
|
61
|
+
if (!paths[fullPath]) paths[fullPath] = {};
|
|
62
|
+
const operation = {};
|
|
63
|
+
if (entry.tags && entry.tags.length > 0) operation.tags = entry.tags;
|
|
64
|
+
if (entry.summary) operation.summary = entry.summary;
|
|
65
|
+
if (entry.description) operation.description = entry.description;
|
|
66
|
+
if (entry.deprecated) operation.deprecated = entry.deprecated;
|
|
67
|
+
if (entry.security) operation.security = entry.security;
|
|
68
|
+
if (entry.requestBody) operation.requestBody = entry.requestBody;
|
|
69
|
+
const autoParams = extractPathParams(rawPath);
|
|
70
|
+
const userParams = entry.parameters ?? [];
|
|
71
|
+
const autoParamNames = new Set(userParams.map((p) => p.name));
|
|
72
|
+
const mergedParams = [...userParams, ...autoParams.filter((p) => !autoParamNames.has(p.name))];
|
|
73
|
+
if (mergedParams.length > 0) operation.parameters = mergedParams;
|
|
74
|
+
operation.responses = entry.responses;
|
|
75
|
+
paths[fullPath][entry.method] = operation;
|
|
76
|
+
}
|
|
77
|
+
const spec = {
|
|
78
|
+
openapi: "3.1.0",
|
|
79
|
+
info: {
|
|
80
|
+
title: config.title,
|
|
81
|
+
version: config.version
|
|
82
|
+
},
|
|
83
|
+
paths
|
|
84
|
+
};
|
|
85
|
+
if (config.description) spec.info.description = config.description;
|
|
86
|
+
if (config.servers) spec.servers = config.servers;
|
|
87
|
+
if (config.components) spec.components = config.components;
|
|
88
|
+
if (config.security) spec.security = config.security;
|
|
89
|
+
return spec;
|
|
90
|
+
}
|
|
91
|
+
function openAPIDoc(router, config) {
|
|
92
|
+
const registry = getRegistry(router);
|
|
93
|
+
return (c) => {
|
|
94
|
+
if (!registry) {
|
|
95
|
+
return c.json({
|
|
96
|
+
openapi: "3.1.0",
|
|
97
|
+
info: { title: config.title, version: config.version },
|
|
98
|
+
paths: {}
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
return c.json(generateSpec(registry, config));
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// src/router.ts
|
|
106
|
+
function createRouter() {
|
|
107
|
+
const router = new hono.Hono();
|
|
108
|
+
const registry = new RouteRegistry();
|
|
109
|
+
router[REGISTRY] = registry;
|
|
110
|
+
function addRoute(method, path, handlerOrOpts, maybeHandler) {
|
|
111
|
+
const hasOptions = typeof handlerOrOpts !== "function";
|
|
112
|
+
const options = hasOptions ? handlerOrOpts : {};
|
|
113
|
+
const handler = hasOptions ? maybeHandler : handlerOrOpts;
|
|
114
|
+
router[method](path, handler);
|
|
115
|
+
registry.add({
|
|
116
|
+
method,
|
|
117
|
+
path,
|
|
118
|
+
tags: options.tags,
|
|
119
|
+
summary: options.summary,
|
|
120
|
+
description: options.description,
|
|
121
|
+
deprecated: options.deprecated,
|
|
122
|
+
security: options.security,
|
|
123
|
+
parameters: options.parameters,
|
|
124
|
+
requestBody: options.requestBody,
|
|
125
|
+
responses: options.responses ?? {
|
|
126
|
+
200: { description: "Successful response" }
|
|
127
|
+
}
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
const self = {
|
|
131
|
+
router,
|
|
132
|
+
route: (path, subRouter, opts) => {
|
|
133
|
+
const subRegistry = getRegistry(subRouter);
|
|
134
|
+
if (subRegistry) {
|
|
135
|
+
registry.merge(path, subRegistry, opts);
|
|
136
|
+
}
|
|
137
|
+
router.route(path, subRouter);
|
|
138
|
+
return self;
|
|
139
|
+
},
|
|
140
|
+
get: (path, handlerOrOpts, handler) => {
|
|
141
|
+
addRoute("get", path, handlerOrOpts, handler);
|
|
142
|
+
return self;
|
|
143
|
+
},
|
|
144
|
+
post: (path, handlerOrOpts, handler) => {
|
|
145
|
+
addRoute("post", path, handlerOrOpts, handler);
|
|
146
|
+
return self;
|
|
147
|
+
},
|
|
148
|
+
put: (path, handlerOrOpts, handler) => {
|
|
149
|
+
addRoute("put", path, handlerOrOpts, handler);
|
|
150
|
+
return self;
|
|
151
|
+
},
|
|
152
|
+
patch: (path, handlerOrOpts, handler) => {
|
|
153
|
+
addRoute("patch", path, handlerOrOpts, handler);
|
|
154
|
+
return self;
|
|
155
|
+
},
|
|
156
|
+
delete: (path, handlerOrOpts, handler) => {
|
|
157
|
+
addRoute("delete", path, handlerOrOpts, handler);
|
|
158
|
+
return self;
|
|
159
|
+
},
|
|
160
|
+
doc: (config) => openAPIDoc(router, config)
|
|
161
|
+
};
|
|
162
|
+
return self;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
exports.createRouter = createRouter;
|
|
166
|
+
exports.generateSpec = generateSpec;
|
|
167
|
+
exports.getRegistry = getRegistry;
|
|
168
|
+
exports.openAPIDoc = openAPIDoc;
|
|
169
|
+
//# sourceMappingURL=index.cjs.map
|
|
170
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/registry.ts","../src/spec.ts","../src/router.ts"],"names":["Hono"],"mappings":";;;;;;;AAEO,IAAM,QAAA,mBAAW,MAAA,CAAO,GAAA,CAAI,uBAAuB,CAAA;AAenD,IAAM,gBAAN,MAAoB;AAAA,EAChB,UAAwB,EAAC;AAAA,EAElC,IAAI,KAAA,EAAyB;AAC3B,IAAA,IAAA,CAAK,OAAA,CAAQ,KAAK,KAAK,CAAA;AAAA,EACzB;AAAA,EAEA,KAAA,CAAM,MAAA,EAAgB,KAAA,EAAsB,IAAA,EAAyB;AACnE,IAAA,KAAA,MAAW,KAAA,IAAS,MAAM,OAAA,EAAS;AACjC,MAAA,MAAM,MAAA,GAAqB;AAAA,QACzB,GAAG,KAAA;AAAA,QACH,MAAM,MAAA,IAAU,KAAA,CAAM,IAAA,KAAS,GAAA,GAAM,KAAK,KAAA,CAAM,IAAA;AAAA,OAClD;AAEA,MAAA,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AO,CAAA;AAC/C,MAAA,OAAO,IAAA;AAAA,IACT,CAAA;AAAA,IACA,GAAA,EAAK,CAAA,MAAA,KAAU,UAAA,CAAW,MAAA,EAAQ,MAAM;AAAA,GAC1C;AAEA,EAAA,OAAO,IAAA;AACT","file":"index.cjs","sourcesContent":["import type { DocOptions, HttpMethod } from './types';\n\nexport const REGISTRY = Symbol.for('hono-openapi-registry');\n\nexport interface RouteEntry {\n method: HttpMethod;\n path: string;\n tags?: string[];\n summary?: string;\n description?: string;\n deprecated?: boolean;\n security?: Record<string, string[]>[];\n parameters?: any[];\n requestBody?: any;\n responses?: Record<string, any>;\n}\n\nexport class RouteRegistry {\n readonly entries: RouteEntry[] = [];\n\n add(entry: RouteEntry): void {\n this.entries.push(entry);\n }\n\n merge(prefix: string, other: RouteRegistry, opts?: DocOptions): void {\n for (const entry of other.entries) {\n const merged: RouteEntry = {\n ...entry,\n path: prefix + (entry.path === '/' ? '' : entry.path),\n };\n\n if (opts) {\n const hasOwnTags = entry.tags && entry.tags.length > 0;\n\n if (opts.tags && !hasOwnTags) merged.tags = opts.tags;\n if (opts.summary && !entry.summary) merged.summary = opts.summary;\n if (opts.description && !entry.description) merged.description = opts.description;\n if (opts.security && !entry.security) merged.security = opts.security;\n if (opts.deprecated !== undefined && entry.deprecated === undefined)\n merged.deprecated = opts.deprecated;\n }\n\n this.entries.push(merged);\n }\n }\n}\n\nexport function getRegistry(router: any): RouteRegistry | undefined {\n return router?.[REGISTRY];\n}\n","import { RouteRegistry, getRegistry } from './registry';\n\nexport interface SpecConfig {\n title: string;\n version: string;\n description?: string;\n prefix?: string;\n servers?: { url: string; description?: string }[];\n components?: Record<string, any>;\n security?: Record<string, string[]>[];\n}\n\nfunction honoPathToOpenAPI(path: string): string {\n return path.replace(/:([a-zA-Z_][a-zA-Z0-9_]*)/g, '{$1}');\n}\n\nfunction extractPathParams(path: string): any[] {\n const params: any[] = [];\n const regex = /:([a-zA-Z_][a-zA-Z0-9_]*)/g;\n let match: RegExpExecArray | null;\n while ((match = regex.exec(path)) !== null) {\n params.push({\n name: match[1],\n in: 'path',\n required: true,\n schema: { type: 'string' },\n });\n }\n return params;\n}\n\nexport function generateSpec(registry: RouteRegistry, config: SpecConfig): Record<string, any> {\n const paths: Record<string, any> = {};\n const prefix = config.prefix ?? '';\n\n for (const entry of registry.entries) {\n const rawPath = prefix + entry.path;\n const fullPath = honoPathToOpenAPI(rawPath);\n\n if (!paths[fullPath]) paths[fullPath] = {};\n\n const operation: Record<string, any> = {};\n\n if (entry.tags && entry.tags.length > 0) operation.tags = entry.tags;\n if (entry.summary) operation.summary = entry.summary;\n if (entry.description) operation.description = entry.description;\n if (entry.deprecated) operation.deprecated = entry.deprecated;\n if (entry.security) operation.security = entry.security;\n if (entry.requestBody) operation.requestBody = entry.requestBody;\n\n const autoParams = extractPathParams(rawPath);\n const userParams = entry.parameters ?? [];\n const autoParamNames = new Set(userParams.map((p: any) => p.name));\n const mergedParams = [...userParams, ...autoParams.filter(p => !autoParamNames.has(p.name))];\n if (mergedParams.length > 0) operation.parameters = mergedParams;\n\n operation.responses = entry.responses;\n\n paths[fullPath][entry.method] = operation;\n }\n\n const spec: Record<string, any> = {\n openapi: '3.1.0',\n info: {\n title: config.title,\n version: config.version,\n },\n paths,\n };\n\n if (config.description) spec.info.description = config.description;\n if (config.servers) spec.servers = config.servers;\n if (config.components) spec.components = config.components;\n if (config.security) spec.security = config.security;\n\n return spec;\n}\n\nexport function openAPIDoc(router: any, config: SpecConfig) {\n const registry = getRegistry(router);\n return (c: any) => {\n if (!registry) {\n return c.json({\n openapi: '3.1.0',\n info: { title: config.title, version: config.version },\n paths: {},\n });\n }\n return c.json(generateSpec(registry, config));\n };\n}\n","import { Hono } from 'hono';\nimport { RouteRegistry, REGISTRY, getRegistry } from './registry';\nimport { openAPIDoc } from './spec';\nimport type { DocOptions, Handler, HttpMethod, RouterInstance } from './types';\n\nexport function createRouter(): RouterInstance {\n const router = new Hono();\n const registry = new RouteRegistry();\n (router as any)[REGISTRY] = registry;\n\n function addRoute(\n method: HttpMethod,\n path: string,\n handlerOrOpts: Handler | DocOptions,\n maybeHandler?: Handler,\n ) {\n const hasOptions = typeof handlerOrOpts !== 'function';\n const options: DocOptions = hasOptions ? (handlerOrOpts as DocOptions) : {};\n const handler: Handler = hasOptions ? maybeHandler! : (handlerOrOpts as Handler);\n\n router[method](path, handler as any);\n\n registry.add({\n method,\n path,\n tags: options.tags,\n summary: options.summary,\n description: options.description,\n deprecated: options.deprecated,\n security: options.security,\n parameters: options.parameters,\n requestBody: options.requestBody,\n responses: options.responses ?? {\n 200: { description: 'Successful response' },\n },\n });\n }\n\n const self: RouterInstance = {\n router,\n route: (path: string, subRouter: any, opts?: DocOptions) => {\n const subRegistry = getRegistry(subRouter);\n if (subRegistry) {\n registry.merge(path, subRegistry, opts);\n }\n router.route(path, subRouter);\n return self;\n },\n get: (path: string, handlerOrOpts: Handler | DocOptions, handler?: Handler) => {\n addRoute('get', path, handlerOrOpts, handler);\n return self;\n },\n post: (path: string, handlerOrOpts: Handler | DocOptions, handler?: Handler) => {\n addRoute('post', path, handlerOrOpts, handler);\n return self;\n },\n put: (path: string, handlerOrOpts: Handler | DocOptions, handler?: Handler) => {\n addRoute('put', path, handlerOrOpts, handler);\n return self;\n },\n patch: (path: string, handlerOrOpts: Handler | DocOptions, handler?: Handler) => {\n addRoute('patch', path, handlerOrOpts, handler);\n return self;\n },\n delete: (path: string, handlerOrOpts: Handler | DocOptions, handler?: Handler) => {\n addRoute('delete', path, handlerOrOpts, handler);\n return self;\n },\n doc: config => openAPIDoc(router, config),\n };\n\n return self;\n}\n"]}
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { R as RouterInstance } from './types-CguT4Ne5.cjs';
|
|
2
|
+
export { D as DocOptions, H as Handler, a as HttpMethod, O as OpenAPIParameter, b as OpenAPIRequestBody, c as OpenAPIResponse, d as RouteEntry, S as SpecConfig, g as generateSpec, e as getRegistry, o as openAPIDoc } from './types-CguT4Ne5.cjs';
|
|
3
|
+
|
|
4
|
+
declare function createRouter(): RouterInstance;
|
|
5
|
+
|
|
6
|
+
export { RouterInstance, createRouter };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { R as RouterInstance } from './types-CguT4Ne5.js';
|
|
2
|
+
export { D as DocOptions, H as Handler, a as HttpMethod, O as OpenAPIParameter, b as OpenAPIRequestBody, c as OpenAPIResponse, d as RouteEntry, S as SpecConfig, g as generateSpec, e as getRegistry, o as openAPIDoc } from './types-CguT4Ne5.js';
|
|
3
|
+
|
|
4
|
+
declare function createRouter(): RouterInstance;
|
|
5
|
+
|
|
6
|
+
export { RouterInstance, createRouter };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
import { Hono } from 'hono';
|
|
2
|
+
|
|
3
|
+
// src/router.ts
|
|
4
|
+
|
|
5
|
+
// src/registry.ts
|
|
6
|
+
var REGISTRY = /* @__PURE__ */ Symbol.for("hono-openapi-registry");
|
|
7
|
+
var RouteRegistry = class {
|
|
8
|
+
entries = [];
|
|
9
|
+
add(entry) {
|
|
10
|
+
this.entries.push(entry);
|
|
11
|
+
}
|
|
12
|
+
merge(prefix, other, opts) {
|
|
13
|
+
for (const entry of other.entries) {
|
|
14
|
+
const merged = {
|
|
15
|
+
...entry,
|
|
16
|
+
path: prefix + (entry.path === "/" ? "" : entry.path)
|
|
17
|
+
};
|
|
18
|
+
if (opts) {
|
|
19
|
+
const hasOwnTags = entry.tags && entry.tags.length > 0;
|
|
20
|
+
if (opts.tags && !hasOwnTags) merged.tags = opts.tags;
|
|
21
|
+
if (opts.summary && !entry.summary) merged.summary = opts.summary;
|
|
22
|
+
if (opts.description && !entry.description) merged.description = opts.description;
|
|
23
|
+
if (opts.security && !entry.security) merged.security = opts.security;
|
|
24
|
+
if (opts.deprecated !== void 0 && entry.deprecated === void 0)
|
|
25
|
+
merged.deprecated = opts.deprecated;
|
|
26
|
+
}
|
|
27
|
+
this.entries.push(merged);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
function getRegistry(router) {
|
|
32
|
+
return router?.[REGISTRY];
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// src/spec.ts
|
|
36
|
+
function honoPathToOpenAPI(path) {
|
|
37
|
+
return path.replace(/:([a-zA-Z_][a-zA-Z0-9_]*)/g, "{$1}");
|
|
38
|
+
}
|
|
39
|
+
function extractPathParams(path) {
|
|
40
|
+
const params = [];
|
|
41
|
+
const regex = /:([a-zA-Z_][a-zA-Z0-9_]*)/g;
|
|
42
|
+
let match;
|
|
43
|
+
while ((match = regex.exec(path)) !== null) {
|
|
44
|
+
params.push({
|
|
45
|
+
name: match[1],
|
|
46
|
+
in: "path",
|
|
47
|
+
required: true,
|
|
48
|
+
schema: { type: "string" }
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
return params;
|
|
52
|
+
}
|
|
53
|
+
function generateSpec(registry, config) {
|
|
54
|
+
const paths = {};
|
|
55
|
+
const prefix = config.prefix ?? "";
|
|
56
|
+
for (const entry of registry.entries) {
|
|
57
|
+
const rawPath = prefix + entry.path;
|
|
58
|
+
const fullPath = honoPathToOpenAPI(rawPath);
|
|
59
|
+
if (!paths[fullPath]) paths[fullPath] = {};
|
|
60
|
+
const operation = {};
|
|
61
|
+
if (entry.tags && entry.tags.length > 0) operation.tags = entry.tags;
|
|
62
|
+
if (entry.summary) operation.summary = entry.summary;
|
|
63
|
+
if (entry.description) operation.description = entry.description;
|
|
64
|
+
if (entry.deprecated) operation.deprecated = entry.deprecated;
|
|
65
|
+
if (entry.security) operation.security = entry.security;
|
|
66
|
+
if (entry.requestBody) operation.requestBody = entry.requestBody;
|
|
67
|
+
const autoParams = extractPathParams(rawPath);
|
|
68
|
+
const userParams = entry.parameters ?? [];
|
|
69
|
+
const autoParamNames = new Set(userParams.map((p) => p.name));
|
|
70
|
+
const mergedParams = [...userParams, ...autoParams.filter((p) => !autoParamNames.has(p.name))];
|
|
71
|
+
if (mergedParams.length > 0) operation.parameters = mergedParams;
|
|
72
|
+
operation.responses = entry.responses;
|
|
73
|
+
paths[fullPath][entry.method] = operation;
|
|
74
|
+
}
|
|
75
|
+
const spec = {
|
|
76
|
+
openapi: "3.1.0",
|
|
77
|
+
info: {
|
|
78
|
+
title: config.title,
|
|
79
|
+
version: config.version
|
|
80
|
+
},
|
|
81
|
+
paths
|
|
82
|
+
};
|
|
83
|
+
if (config.description) spec.info.description = config.description;
|
|
84
|
+
if (config.servers) spec.servers = config.servers;
|
|
85
|
+
if (config.components) spec.components = config.components;
|
|
86
|
+
if (config.security) spec.security = config.security;
|
|
87
|
+
return spec;
|
|
88
|
+
}
|
|
89
|
+
function openAPIDoc(router, config) {
|
|
90
|
+
const registry = getRegistry(router);
|
|
91
|
+
return (c) => {
|
|
92
|
+
if (!registry) {
|
|
93
|
+
return c.json({
|
|
94
|
+
openapi: "3.1.0",
|
|
95
|
+
info: { title: config.title, version: config.version },
|
|
96
|
+
paths: {}
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
return c.json(generateSpec(registry, config));
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// src/router.ts
|
|
104
|
+
function createRouter() {
|
|
105
|
+
const router = new Hono();
|
|
106
|
+
const registry = new RouteRegistry();
|
|
107
|
+
router[REGISTRY] = registry;
|
|
108
|
+
function addRoute(method, path, handlerOrOpts, maybeHandler) {
|
|
109
|
+
const hasOptions = typeof handlerOrOpts !== "function";
|
|
110
|
+
const options = hasOptions ? handlerOrOpts : {};
|
|
111
|
+
const handler = hasOptions ? maybeHandler : handlerOrOpts;
|
|
112
|
+
router[method](path, handler);
|
|
113
|
+
registry.add({
|
|
114
|
+
method,
|
|
115
|
+
path,
|
|
116
|
+
tags: options.tags,
|
|
117
|
+
summary: options.summary,
|
|
118
|
+
description: options.description,
|
|
119
|
+
deprecated: options.deprecated,
|
|
120
|
+
security: options.security,
|
|
121
|
+
parameters: options.parameters,
|
|
122
|
+
requestBody: options.requestBody,
|
|
123
|
+
responses: options.responses ?? {
|
|
124
|
+
200: { description: "Successful response" }
|
|
125
|
+
}
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
const self = {
|
|
129
|
+
router,
|
|
130
|
+
route: (path, subRouter, opts) => {
|
|
131
|
+
const subRegistry = getRegistry(subRouter);
|
|
132
|
+
if (subRegistry) {
|
|
133
|
+
registry.merge(path, subRegistry, opts);
|
|
134
|
+
}
|
|
135
|
+
router.route(path, subRouter);
|
|
136
|
+
return self;
|
|
137
|
+
},
|
|
138
|
+
get: (path, handlerOrOpts, handler) => {
|
|
139
|
+
addRoute("get", path, handlerOrOpts, handler);
|
|
140
|
+
return self;
|
|
141
|
+
},
|
|
142
|
+
post: (path, handlerOrOpts, handler) => {
|
|
143
|
+
addRoute("post", path, handlerOrOpts, handler);
|
|
144
|
+
return self;
|
|
145
|
+
},
|
|
146
|
+
put: (path, handlerOrOpts, handler) => {
|
|
147
|
+
addRoute("put", path, handlerOrOpts, handler);
|
|
148
|
+
return self;
|
|
149
|
+
},
|
|
150
|
+
patch: (path, handlerOrOpts, handler) => {
|
|
151
|
+
addRoute("patch", path, handlerOrOpts, handler);
|
|
152
|
+
return self;
|
|
153
|
+
},
|
|
154
|
+
delete: (path, handlerOrOpts, handler) => {
|
|
155
|
+
addRoute("delete", path, handlerOrOpts, handler);
|
|
156
|
+
return self;
|
|
157
|
+
},
|
|
158
|
+
doc: (config) => openAPIDoc(router, config)
|
|
159
|
+
};
|
|
160
|
+
return self;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
export { createRouter, generateSpec, getRegistry, openAPIDoc };
|
|
164
|
+
//# sourceMappingURL=index.js.map
|
|
165
|
+
//# sourceMappingURL=index.js.map
|