@hono/zod-openapi
Advanced tools
Comparing version 0.2.0 to 0.3.0
import * as openapi3_ts_oas30 from 'openapi3-ts/oas30'; | ||
import { RouteConfig, ZodRequestBody, ZodContentObject, ResponseConfig } from '@asteasolutions/zod-to-openapi'; | ||
import { OpenAPIRegistry, RouteConfig, ZodRequestBody, ZodContentObject, ResponseConfig } from '@asteasolutions/zod-to-openapi'; | ||
import { OpenAPIObjectConfig } from '@asteasolutions/zod-to-openapi/dist/v3.0/openapi-generator'; | ||
@@ -51,6 +51,7 @@ import { Env, Schema, Hono, Input, Handler, ToSchema, Context, TypedResponse } from 'hono'; | ||
type ConvertPathType<T extends string> = T extends `${infer _}/{${infer Param}}${infer _}` ? `/:${Param}` : T; | ||
type HandlerResponse<O> = TypedResponse<O> | Promise<TypedResponse<O>>; | ||
declare class OpenAPIHono<E extends Env = Env, S extends Schema = {}, BasePath extends string = '/'> extends Hono<E, S, BasePath> { | ||
#private; | ||
openAPIRegistry: OpenAPIRegistry; | ||
constructor(); | ||
openapi: <R extends RouteConfig, I extends Input = InputTypeBase<R, "params", "param"> & InputTypeBase<R, "query", "query"> & InputTypeBase<R, "headers", "header"> & InputTypeBase<R, "cookies", "cookie"> & InputTypeForm<R> & InputTypeJson<R>, P extends string = ConvertPathType<R["path"]>>(route: R, handler: Handler<E, P, I, OutputType<R>>, hook?: Hook<I, E, P, OutputType<R>> | undefined) => Hono<E, ToSchema<R["method"], P, I["in"], OutputType<R>>, BasePath>; | ||
openapi: <R extends RouteConfig, I extends Input = InputTypeBase<R, "params", "param"> & InputTypeBase<R, "query", "query"> & InputTypeBase<R, "headers", "header"> & InputTypeBase<R, "cookies", "cookie"> & InputTypeForm<R> & InputTypeJson<R>, P extends string = ConvertPathType<R["path"]>>(route: R, handler: Handler<E, P, I, HandlerResponse<OutputType<R>>>, hook?: Hook<I, E, P, OutputType<R>> | undefined) => Hono<E, ToSchema<R["method"], P, I["in"], OutputType<R>>, BasePath>; | ||
getOpenAPIDocument: (config: OpenAPIObjectConfig) => openapi3_ts_oas30.OpenAPIObject; | ||
@@ -57,0 +58,0 @@ doc: (path: string, config: OpenAPIObjectConfig) => void; |
@@ -1,20 +0,1 @@ | ||
var __accessCheck = (obj, member, msg) => { | ||
if (!member.has(obj)) | ||
throw TypeError("Cannot " + msg); | ||
}; | ||
var __privateGet = (obj, member, getter) => { | ||
__accessCheck(obj, member, "read from private field"); | ||
return getter ? getter.call(obj) : member.get(obj); | ||
}; | ||
var __privateAdd = (obj, member, value) => { | ||
if (member.has(obj)) | ||
throw TypeError("Cannot add the same private member more than once"); | ||
member instanceof WeakSet ? member.add(obj) : member.set(obj, value); | ||
}; | ||
var __privateSet = (obj, member, value, setter) => { | ||
__accessCheck(obj, member, "write to private field"); | ||
setter ? setter.call(obj, value) : member.set(obj, value); | ||
return value; | ||
}; | ||
// src/index.ts | ||
@@ -26,9 +7,7 @@ import { OpenApiGeneratorV3, OpenAPIRegistry } from "@asteasolutions/zod-to-openapi"; | ||
import { z, ZodType } from "zod"; | ||
var _registry; | ||
var OpenAPIHono = class extends Hono { | ||
constructor() { | ||
super(); | ||
__privateAdd(this, _registry, void 0); | ||
this.openapi = (route, handler, hook) => { | ||
__privateGet(this, _registry).registerPath(route); | ||
this.openAPIRegistry.registerPath(route); | ||
const validators = []; | ||
@@ -74,3 +53,3 @@ if (route.request?.query) { | ||
this.getOpenAPIDocument = (config) => { | ||
const generator = new OpenApiGeneratorV3(__privateGet(this, _registry).definitions); | ||
const generator = new OpenApiGeneratorV3(this.openAPIRegistry.definitions); | ||
const document = generator.generateDocument(config); | ||
@@ -85,6 +64,5 @@ return document; | ||
}; | ||
__privateSet(this, _registry, new OpenAPIRegistry()); | ||
this.openAPIRegistry = new OpenAPIRegistry(); | ||
} | ||
}; | ||
_registry = new WeakMap(); | ||
var createRoute = (routeConfig) => routeConfig; | ||
@@ -91,0 +69,0 @@ extendZodWithOpenApi(z); |
{ | ||
"name": "@hono/zod-openapi", | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"description": "A wrapper class of Hono which supports OpenAPI.", | ||
@@ -34,3 +34,3 @@ "type": "module", | ||
"devDependencies": { | ||
"hono": "^3.5.4", | ||
"hono": "^3.5.8", | ||
"zod": "^3.22.1" | ||
@@ -45,2 +45,2 @@ }, | ||
} | ||
} | ||
} |
@@ -7,6 +7,2 @@ # Zod OpenAPI Hono | ||
## Limitations | ||
- An instance of Zod OpenAPI Hono cannot be used as a "subApp" in conjunction with `rootApp.route('/api', subApp)`. | ||
## Usage | ||
@@ -182,2 +178,10 @@ | ||
### The Registry | ||
You can access the [`OpenAPIRegistry`](https://github.com/asteasolutions/zod-to-openapi#the-registry) object via `app.openAPIRegistry`: | ||
```ts | ||
const registry = app.openAPIRegistry | ||
``` | ||
### Middleware | ||
@@ -213,2 +217,43 @@ | ||
## Limitations | ||
An instance of Zod OpenAPI Hono cannot be used as a "subApp" in conjunction with `rootApp.route('/api', subApp)`. | ||
Use `app.mount('/api', subApp.fetch)` instead. | ||
```ts | ||
const api = OpenAPIHono() | ||
// ... | ||
// Set the `/api` as a base path in the document. | ||
api.get('/doc', (c) => { | ||
const url = new URL(c.req.url) | ||
url.pathname = '/api' | ||
url.search = '' | ||
return c.json( | ||
// `api.getOpenAPIDocument()` will return a JSON object of the docs. | ||
api.getOpenAPIDocument({ | ||
openapi: '3.0.0', | ||
info: { | ||
version: '1.0.0', | ||
title: 'My API', | ||
}, | ||
servers: [ | ||
{ | ||
url: `${url.toString()}`, | ||
}, | ||
], | ||
}) | ||
) | ||
}) | ||
const app = new Hono() | ||
// Mount the Open API app to `/api` in the main app. | ||
app.mount('/api', api.fetch) | ||
export default app | ||
``` | ||
## References | ||
@@ -215,0 +260,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
269
21377
233