@hono/zod-openapi 0.15.1 → 0.15.3

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
@@ -269,7 +269,7 @@ You can configure middleware for each endpoint from a route created by `createRo
269
269
 
270
270
  ```ts
271
271
  import { prettyJSON } from 'hono/pretty-json'
272
- import { cache } from 'honoc/cache'
272
+ import { cache } from 'hono/cache'
273
273
 
274
274
  app.use(route.getRoutingPath(), prettyJSON(), cache({ cacheName: 'my-cache' }))
275
275
  app.openapi(route, handler)
package/dist/index.js CHANGED
@@ -99,13 +99,39 @@ var OpenAPIHono = class _OpenAPIHono extends import_hono.Hono {
99
99
  if (!(schema instanceof import_zod.ZodType)) {
100
100
  continue;
101
101
  }
102
- if (/^application\/([a-z-\.]+\+)?json/.test(mediaType)) {
102
+ if (isJSONContentType(mediaType)) {
103
103
  const validator = (0, import_zod_validator.zValidator)("json", schema, hook);
104
- validators.push(validator);
104
+ if (route.request?.body?.required) {
105
+ validators.push(validator);
106
+ } else {
107
+ const mw = async (c, next) => {
108
+ if (c.req.header("content-type")) {
109
+ if (isJSONContentType(c.req.header("content-type"))) {
110
+ return await validator(c, next);
111
+ }
112
+ }
113
+ c.req.addValidatedData("json", {});
114
+ await next();
115
+ };
116
+ validators.push(mw);
117
+ }
105
118
  }
106
- if (mediaType.startsWith("multipart/form-data") || mediaType.startsWith("application/x-www-form-urlencoded")) {
119
+ if (isFormContentType(mediaType)) {
107
120
  const validator = (0, import_zod_validator.zValidator)("form", schema, hook);
108
- validators.push(validator);
121
+ if (route.request?.body?.required) {
122
+ validators.push(validator);
123
+ } else {
124
+ const mw = async (c, next) => {
125
+ if (c.req.header("content-type")) {
126
+ if (isFormContentType(c.req.header("content-type"))) {
127
+ return await validator(c, next);
128
+ }
129
+ }
130
+ c.req.addValidatedData("form", {});
131
+ await next();
132
+ };
133
+ validators.push(mw);
134
+ }
109
135
  }
110
136
  }
111
137
  }
@@ -210,6 +236,12 @@ function addBasePathToDocument(document, basePath) {
210
236
  paths: updatedPaths
211
237
  };
212
238
  }
239
+ function isJSONContentType(contentType) {
240
+ return /^application\/([a-z-\.]+\+)?json/.test(contentType);
241
+ }
242
+ function isFormContentType(contentType) {
243
+ return contentType.startsWith("multipart/form-data") || contentType.startsWith("application/x-www-form-urlencoded");
244
+ }
213
245
  // Annotate the CommonJS export names for ESM import in node:
214
246
  0 && (module.exports = {
215
247
  OpenAPIHono,
package/dist/index.mjs CHANGED
@@ -77,13 +77,39 @@ var OpenAPIHono = class _OpenAPIHono extends Hono {
77
77
  if (!(schema instanceof ZodType)) {
78
78
  continue;
79
79
  }
80
- if (/^application\/([a-z-\.]+\+)?json/.test(mediaType)) {
80
+ if (isJSONContentType(mediaType)) {
81
81
  const validator = zValidator("json", schema, hook);
82
- validators.push(validator);
82
+ if (route.request?.body?.required) {
83
+ validators.push(validator);
84
+ } else {
85
+ const mw = async (c, next) => {
86
+ if (c.req.header("content-type")) {
87
+ if (isJSONContentType(c.req.header("content-type"))) {
88
+ return await validator(c, next);
89
+ }
90
+ }
91
+ c.req.addValidatedData("json", {});
92
+ await next();
93
+ };
94
+ validators.push(mw);
95
+ }
83
96
  }
84
- if (mediaType.startsWith("multipart/form-data") || mediaType.startsWith("application/x-www-form-urlencoded")) {
97
+ if (isFormContentType(mediaType)) {
85
98
  const validator = zValidator("form", schema, hook);
86
- validators.push(validator);
99
+ if (route.request?.body?.required) {
100
+ validators.push(validator);
101
+ } else {
102
+ const mw = async (c, next) => {
103
+ if (c.req.header("content-type")) {
104
+ if (isFormContentType(c.req.header("content-type"))) {
105
+ return await validator(c, next);
106
+ }
107
+ }
108
+ c.req.addValidatedData("form", {});
109
+ await next();
110
+ };
111
+ validators.push(mw);
112
+ }
87
113
  }
88
114
  }
89
115
  }
@@ -188,6 +214,12 @@ function addBasePathToDocument(document, basePath) {
188
214
  paths: updatedPaths
189
215
  };
190
216
  }
217
+ function isJSONContentType(contentType) {
218
+ return /^application\/([a-z-\.]+\+)?json/.test(contentType);
219
+ }
220
+ function isFormContentType(contentType) {
221
+ return contentType.startsWith("multipart/form-data") || contentType.startsWith("application/x-www-form-urlencoded");
222
+ }
191
223
  export {
192
224
  OpenAPIHono,
193
225
  createRoute,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hono/zod-openapi",
3
- "version": "0.15.1",
3
+ "version": "0.15.3",
4
4
  "description": "A wrapper class of Hono which supports OpenAPI.",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
@@ -42,7 +42,7 @@
42
42
  },
43
43
  "devDependencies": {
44
44
  "@cloudflare/workers-types": "^4.20240117.0",
45
- "hono": "^4.3.6",
45
+ "hono": "^4.5.4",
46
46
  "jest": "^29.7.0",
47
47
  "tsup": "^8.0.1",
48
48
  "typescript": "^5.4.4",