@hono/zod-openapi 0.1.1 → 0.2.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/README.md CHANGED
@@ -6,7 +6,6 @@ _Note: This is not standalone middleware but is hosted on the monorepo "[github.
6
6
 
7
7
  ## Limitations
8
8
 
9
- - Currently, it does not support validation of _headers_ and _cookies_.
10
9
  - An instance of Zod OpenAPI Hono cannot be used as a "subApp" in conjunction with `rootApp.route('/api', subApp)`.
11
10
 
12
11
  ## Usage
@@ -63,7 +62,7 @@ import { createRoute } from '@hono/zod-openapi'
63
62
 
64
63
  const route = createRoute({
65
64
  method: 'get',
66
- path: '/users/:id',
65
+ path: '/users/{id}',
67
66
  request: {
68
67
  params: ParamsSchema,
69
68
  },
@@ -134,7 +133,7 @@ Then, add the error response:
134
133
  ```ts
135
134
  const route = createRoute({
136
135
  method: 'get',
137
- path: '/users/:id',
136
+ path: '/users/{id}',
138
137
  request: {
139
138
  params: ParamsSchema,
140
139
  },
@@ -159,7 +158,7 @@ app.openapi(
159
158
  (c) => {
160
159
  const { id } = c.req.valid('param')
161
160
  return c.jsonT({
162
- id: Number(id),
161
+ id,
163
162
  age: 20,
164
163
  name: 'Ultra-man',
165
164
  })
package/dist/index.cjs CHANGED
@@ -64,6 +64,14 @@ var OpenAPIHono = class extends import_hono.Hono {
64
64
  const validator = (0, import_zod_validator.zValidator)("param", route.request.params, hook);
65
65
  validators.push(validator);
66
66
  }
67
+ if (route.request?.headers) {
68
+ const validator = (0, import_zod_validator.zValidator)("header", route.request.headers, hook);
69
+ validators.push(validator);
70
+ }
71
+ if (route.request?.cookies) {
72
+ const validator = (0, import_zod_validator.zValidator)("cookie", route.request.cookies, hook);
73
+ validators.push(validator);
74
+ }
67
75
  const bodyContent = route.request?.body?.content;
68
76
  if (bodyContent) {
69
77
  for (const mediaType of Object.keys(bodyContent)) {
package/dist/index.d.cts CHANGED
@@ -1,7 +1,7 @@
1
1
  import * as openapi3_ts_oas30 from 'openapi3-ts/oas30';
2
2
  import { RouteConfig, ZodRequestBody, ZodContentObject, ResponseConfig } from '@asteasolutions/zod-to-openapi';
3
3
  import { OpenAPIObjectConfig } from '@asteasolutions/zod-to-openapi/dist/v3.0/openapi-generator';
4
- import { Env, Hono, Input, Handler, Schema, Context, TypedResponse } from 'hono';
4
+ import { Env, Schema, Hono, Input, Handler, ToSchema, Context, TypedResponse } from 'hono';
5
5
  import { AnyZodObject, z, ZodSchema, ZodError, ZodType } from 'zod';
6
6
  export { z } from 'zod';
7
7
 
@@ -48,10 +48,10 @@ type Hook<T, E extends Env, P extends string, O> = (result: {
48
48
  error: ZodError;
49
49
  }, c: Context<E, P>) => TypedResponse<O> | Promise<TypedResponse<T>> | void;
50
50
  type ConvertPathType<T extends string> = T extends `${infer _}/{${infer Param}}${infer _}` ? `/:${Param}` : T;
51
- declare class OpenAPIHono<E extends Env = Env, S = {}, BasePath extends string = '/'> extends Hono<E, S, BasePath> {
51
+ declare class OpenAPIHono<E extends Env = Env, S extends Schema = {}, BasePath extends string = '/'> extends Hono<E, S, BasePath> {
52
52
  #private;
53
53
  constructor();
54
- openapi: <R extends RouteConfig, I extends Input = InputTypeBase<R, "params", "param"> & InputTypeBase<R, "query", "query"> & 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, Schema<R["method"], P, I["in"], OutputType<R>>, BasePath>;
54
+ 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>;
55
55
  getOpenAPIDocument: (config: OpenAPIObjectConfig) => openapi3_ts_oas30.OpenAPIObject;
56
56
  doc: (path: string, config: OpenAPIObjectConfig) => void;
57
57
  }
package/dist/index.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import * as openapi3_ts_oas30 from 'openapi3-ts/oas30';
2
2
  import { RouteConfig, ZodRequestBody, ZodContentObject, ResponseConfig } from '@asteasolutions/zod-to-openapi';
3
3
  import { OpenAPIObjectConfig } from '@asteasolutions/zod-to-openapi/dist/v3.0/openapi-generator';
4
- import { Env, Hono, Input, Handler, Schema, Context, TypedResponse } from 'hono';
4
+ import { Env, Schema, Hono, Input, Handler, ToSchema, Context, TypedResponse } from 'hono';
5
5
  import { AnyZodObject, z, ZodSchema, ZodError, ZodType } from 'zod';
6
6
  export { z } from 'zod';
7
7
 
@@ -48,10 +48,10 @@ type Hook<T, E extends Env, P extends string, O> = (result: {
48
48
  error: ZodError;
49
49
  }, c: Context<E, P>) => TypedResponse<O> | Promise<TypedResponse<T>> | void;
50
50
  type ConvertPathType<T extends string> = T extends `${infer _}/{${infer Param}}${infer _}` ? `/:${Param}` : T;
51
- declare class OpenAPIHono<E extends Env = Env, S = {}, BasePath extends string = '/'> extends Hono<E, S, BasePath> {
51
+ declare class OpenAPIHono<E extends Env = Env, S extends Schema = {}, BasePath extends string = '/'> extends Hono<E, S, BasePath> {
52
52
  #private;
53
53
  constructor();
54
- openapi: <R extends RouteConfig, I extends Input = InputTypeBase<R, "params", "param"> & InputTypeBase<R, "query", "query"> & 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, Schema<R["method"], P, I["in"], OutputType<R>>, BasePath>;
54
+ 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>;
55
55
  getOpenAPIDocument: (config: OpenAPIObjectConfig) => openapi3_ts_oas30.OpenAPIObject;
56
56
  doc: (path: string, config: OpenAPIObjectConfig) => void;
57
57
  }
package/dist/index.js CHANGED
@@ -39,6 +39,14 @@ var OpenAPIHono = class extends Hono {
39
39
  const validator = zValidator("param", route.request.params, hook);
40
40
  validators.push(validator);
41
41
  }
42
+ if (route.request?.headers) {
43
+ const validator = zValidator("header", route.request.headers, hook);
44
+ validators.push(validator);
45
+ }
46
+ if (route.request?.cookies) {
47
+ const validator = zValidator("cookie", route.request.cookies, hook);
48
+ validators.push(validator);
49
+ }
42
50
  const bodyContent = route.request?.body?.content;
43
51
  if (bodyContent) {
44
52
  for (const mediaType of Object.keys(bodyContent)) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hono/zod-openapi",
3
- "version": "0.1.1",
3
+ "version": "0.2.0",
4
4
  "description": "A wrapper class of Hono which supports OpenAPI.",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -31,14 +31,14 @@
31
31
  "zod": "3.*"
32
32
  },
33
33
  "devDependencies": {
34
- "hono": "^3.4.3",
34
+ "hono": "^3.5.4",
35
35
  "zod": "^3.22.1"
36
36
  },
37
37
  "dependencies": {
38
38
  "@asteasolutions/zod-to-openapi": "^5.5.0",
39
- "@hono/zod-validator": "^0.1.7"
39
+ "@hono/zod-validator": "^0.1.8"
40
40
  },
41
41
  "engines": {
42
42
  "node": ">=16.0.0"
43
43
  }
44
- }
44
+ }