@hono/zod-openapi 0.4.0 → 0.5.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/README.md +12 -0
- package/dist/index.cjs +10 -10
- package/dist/index.d.cts +7 -4
- package/dist/index.d.ts +7 -4
- package/dist/index.js +15 -11
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -203,6 +203,18 @@ import { prettyJSON } from 'hono/pretty-json'
|
|
|
203
203
|
app.use('/doc/*', prettyJSON())
|
|
204
204
|
```
|
|
205
205
|
|
|
206
|
+
### Configure middleware for each endpoint
|
|
207
|
+
|
|
208
|
+
You can configure middleware for each endpoint from a route created by `createRoute` as follows.
|
|
209
|
+
|
|
210
|
+
```ts
|
|
211
|
+
import { prettyJSON } from 'hono/pretty-json'
|
|
212
|
+
import { cache } from 'honoc/cache'
|
|
213
|
+
|
|
214
|
+
app.use(route.getRoutingPath(), prettyJSON(), cache({ cacheName: "my-cache" }))
|
|
215
|
+
app.openapi(route, handler)
|
|
216
|
+
```
|
|
217
|
+
|
|
206
218
|
### RPC Mode
|
|
207
219
|
|
|
208
220
|
Zod OpenAPI Hono supports Hono's RPC mode. You can define types for the Hono Client as follows:
|
package/dist/index.cjs
CHANGED
|
@@ -107,11 +107,7 @@ var OpenAPIHono = class _OpenAPIHono extends import_hono.Hono {
|
|
|
107
107
|
app.openAPIRegistry.definitions.forEach((def) => {
|
|
108
108
|
switch (def.type) {
|
|
109
109
|
case "component":
|
|
110
|
-
return this.openAPIRegistry.registerComponent(
|
|
111
|
-
def.componentType,
|
|
112
|
-
def.name,
|
|
113
|
-
def.component
|
|
114
|
-
);
|
|
110
|
+
return this.openAPIRegistry.registerComponent(def.componentType, def.name, def.component);
|
|
115
111
|
case "route":
|
|
116
112
|
return this.openAPIRegistry.registerPath({
|
|
117
113
|
...def.route,
|
|
@@ -123,10 +119,7 @@ var OpenAPIHono = class _OpenAPIHono extends import_hono.Hono {
|
|
|
123
119
|
path: `${path}${def.webhook.path}`
|
|
124
120
|
});
|
|
125
121
|
case "schema":
|
|
126
|
-
return this.openAPIRegistry.register(
|
|
127
|
-
def.schema._def.openapi._internal.refId,
|
|
128
|
-
def.schema
|
|
129
|
-
);
|
|
122
|
+
return this.openAPIRegistry.register(def.schema._def.openapi._internal.refId, def.schema);
|
|
130
123
|
case "parameter":
|
|
131
124
|
return this.openAPIRegistry.registerParameter(
|
|
132
125
|
def.schema._def.openapi._internal.refId,
|
|
@@ -141,7 +134,14 @@ var OpenAPIHono = class _OpenAPIHono extends import_hono.Hono {
|
|
|
141
134
|
return this;
|
|
142
135
|
}
|
|
143
136
|
};
|
|
144
|
-
var createRoute = (routeConfig) =>
|
|
137
|
+
var createRoute = (routeConfig) => {
|
|
138
|
+
return {
|
|
139
|
+
...routeConfig,
|
|
140
|
+
getRoutingPath() {
|
|
141
|
+
return routeConfig.path.replaceAll(/\/{(.+?)}/g, "/:$1");
|
|
142
|
+
}
|
|
143
|
+
};
|
|
144
|
+
};
|
|
145
145
|
(0, import_zod_to_openapi2.extendZodWithOpenApi)(import_zod.z);
|
|
146
146
|
// Annotate the CommonJS export names for ESM import in node:
|
|
147
147
|
0 && (module.exports = {
|
package/dist/index.d.cts
CHANGED
|
@@ -23,7 +23,7 @@ type InputTypeBase<R extends RouteConfig, Part extends string, Type extends stri
|
|
|
23
23
|
[K in Type]: z.input<RequestPart<R, Part>>;
|
|
24
24
|
};
|
|
25
25
|
out: {
|
|
26
|
-
[K in Type]: z.
|
|
26
|
+
[K in Type]: z.output<RequestPart<R, Part>>;
|
|
27
27
|
};
|
|
28
28
|
} : {} : {};
|
|
29
29
|
type InputTypeJson<R extends RouteConfig> = R['request'] extends RequestTypes ? R['request']['body'] extends ZodRequestBody ? R['request']['body']['content'] extends ZodContentObject ? IsJson<keyof R['request']['body']['content']> extends never ? {} : R['request']['body']['content'][keyof R['request']['body']['content']]['schema'] extends ZodSchema<any> ? {
|
|
@@ -31,7 +31,7 @@ type InputTypeJson<R extends RouteConfig> = R['request'] extends RequestTypes ?
|
|
|
31
31
|
json: z.input<R['request']['body']['content'][keyof R['request']['body']['content']]['schema']>;
|
|
32
32
|
};
|
|
33
33
|
out: {
|
|
34
|
-
json: z.
|
|
34
|
+
json: z.output<R['request']['body']['content'][keyof R['request']['body']['content']]['schema']>;
|
|
35
35
|
};
|
|
36
36
|
} : {} : {} : {} : {};
|
|
37
37
|
type InputTypeForm<R extends RouteConfig> = R['request'] extends RequestTypes ? R['request']['body'] extends ZodRequestBody ? R['request']['body']['content'] extends ZodContentObject ? IsForm<keyof R['request']['body']['content']> extends never ? {} : R['request']['body']['content'][keyof R['request']['body']['content']]['schema'] extends ZodSchema<any> ? {
|
|
@@ -39,7 +39,7 @@ type InputTypeForm<R extends RouteConfig> = R['request'] extends RequestTypes ?
|
|
|
39
39
|
form: z.input<R['request']['body']['content'][keyof R['request']['body']['content']]['schema']>;
|
|
40
40
|
};
|
|
41
41
|
out: {
|
|
42
|
-
form: z.
|
|
42
|
+
form: z.output<R['request']['body']['content'][keyof R['request']['body']['content']]['schema']>;
|
|
43
43
|
};
|
|
44
44
|
} : {} : {} : {} : {};
|
|
45
45
|
type OutputType<R extends RouteConfig> = R['responses'] extends Record<infer _, infer C> ? C extends ResponseConfig ? C['content'] extends ZodContentObject ? IsJson<keyof C['content']> extends never ? {} : C['content'][keyof C['content']]['schema'] extends ZodSchema ? z.infer<C['content'][keyof C['content']]['schema']> : {} : {} : {} : {};
|
|
@@ -64,8 +64,11 @@ declare class OpenAPIHono<E extends Env = Env, S extends Schema = {}, BasePath e
|
|
|
64
64
|
route<SubPath extends string, SubEnv extends Env, SubSchema extends Schema, SubBasePath extends string>(path: SubPath, app: Hono<SubEnv, SubSchema, SubBasePath>): OpenAPIHono<E, MergeSchemaPath<SubSchema, MergePath<BasePath, SubPath>> & S, BasePath>;
|
|
65
65
|
route<SubPath extends string>(path: SubPath): Hono<E, RemoveBlankRecord<S>, BasePath>;
|
|
66
66
|
}
|
|
67
|
+
type RoutingPath<P extends string> = P extends `${infer Head}/{${infer Param}}${infer Tail}` ? `${Head}/:${Param}${RoutingPath<Tail>}` : P;
|
|
67
68
|
declare const createRoute: <P extends string, R extends Omit<RouteConfig, "path"> & {
|
|
68
69
|
path: P;
|
|
69
|
-
}>(routeConfig: R) => R
|
|
70
|
+
}>(routeConfig: R) => R & {
|
|
71
|
+
getRoutingPath(): RoutingPath<R["path"]>;
|
|
72
|
+
};
|
|
70
73
|
|
|
71
74
|
export { OpenAPIHono, createRoute };
|
package/dist/index.d.ts
CHANGED
|
@@ -23,7 +23,7 @@ type InputTypeBase<R extends RouteConfig, Part extends string, Type extends stri
|
|
|
23
23
|
[K in Type]: z.input<RequestPart<R, Part>>;
|
|
24
24
|
};
|
|
25
25
|
out: {
|
|
26
|
-
[K in Type]: z.
|
|
26
|
+
[K in Type]: z.output<RequestPart<R, Part>>;
|
|
27
27
|
};
|
|
28
28
|
} : {} : {};
|
|
29
29
|
type InputTypeJson<R extends RouteConfig> = R['request'] extends RequestTypes ? R['request']['body'] extends ZodRequestBody ? R['request']['body']['content'] extends ZodContentObject ? IsJson<keyof R['request']['body']['content']> extends never ? {} : R['request']['body']['content'][keyof R['request']['body']['content']]['schema'] extends ZodSchema<any> ? {
|
|
@@ -31,7 +31,7 @@ type InputTypeJson<R extends RouteConfig> = R['request'] extends RequestTypes ?
|
|
|
31
31
|
json: z.input<R['request']['body']['content'][keyof R['request']['body']['content']]['schema']>;
|
|
32
32
|
};
|
|
33
33
|
out: {
|
|
34
|
-
json: z.
|
|
34
|
+
json: z.output<R['request']['body']['content'][keyof R['request']['body']['content']]['schema']>;
|
|
35
35
|
};
|
|
36
36
|
} : {} : {} : {} : {};
|
|
37
37
|
type InputTypeForm<R extends RouteConfig> = R['request'] extends RequestTypes ? R['request']['body'] extends ZodRequestBody ? R['request']['body']['content'] extends ZodContentObject ? IsForm<keyof R['request']['body']['content']> extends never ? {} : R['request']['body']['content'][keyof R['request']['body']['content']]['schema'] extends ZodSchema<any> ? {
|
|
@@ -39,7 +39,7 @@ type InputTypeForm<R extends RouteConfig> = R['request'] extends RequestTypes ?
|
|
|
39
39
|
form: z.input<R['request']['body']['content'][keyof R['request']['body']['content']]['schema']>;
|
|
40
40
|
};
|
|
41
41
|
out: {
|
|
42
|
-
form: z.
|
|
42
|
+
form: z.output<R['request']['body']['content'][keyof R['request']['body']['content']]['schema']>;
|
|
43
43
|
};
|
|
44
44
|
} : {} : {} : {} : {};
|
|
45
45
|
type OutputType<R extends RouteConfig> = R['responses'] extends Record<infer _, infer C> ? C extends ResponseConfig ? C['content'] extends ZodContentObject ? IsJson<keyof C['content']> extends never ? {} : C['content'][keyof C['content']]['schema'] extends ZodSchema ? z.infer<C['content'][keyof C['content']]['schema']> : {} : {} : {} : {};
|
|
@@ -64,8 +64,11 @@ declare class OpenAPIHono<E extends Env = Env, S extends Schema = {}, BasePath e
|
|
|
64
64
|
route<SubPath extends string, SubEnv extends Env, SubSchema extends Schema, SubBasePath extends string>(path: SubPath, app: Hono<SubEnv, SubSchema, SubBasePath>): OpenAPIHono<E, MergeSchemaPath<SubSchema, MergePath<BasePath, SubPath>> & S, BasePath>;
|
|
65
65
|
route<SubPath extends string>(path: SubPath): Hono<E, RemoveBlankRecord<S>, BasePath>;
|
|
66
66
|
}
|
|
67
|
+
type RoutingPath<P extends string> = P extends `${infer Head}/{${infer Param}}${infer Tail}` ? `${Head}/:${Param}${RoutingPath<Tail>}` : P;
|
|
67
68
|
declare const createRoute: <P extends string, R extends Omit<RouteConfig, "path"> & {
|
|
68
69
|
path: P;
|
|
69
|
-
}>(routeConfig: R) => R
|
|
70
|
+
}>(routeConfig: R) => R & {
|
|
71
|
+
getRoutingPath(): RoutingPath<R["path"]>;
|
|
72
|
+
};
|
|
70
73
|
|
|
71
74
|
export { OpenAPIHono, createRoute };
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
// src/index.ts
|
|
2
|
-
import {
|
|
2
|
+
import {
|
|
3
|
+
OpenApiGeneratorV3,
|
|
4
|
+
OpenApiGeneratorV31,
|
|
5
|
+
OpenAPIRegistry
|
|
6
|
+
} from "@asteasolutions/zod-to-openapi";
|
|
3
7
|
import { extendZodWithOpenApi } from "@asteasolutions/zod-to-openapi";
|
|
4
8
|
import { zValidator } from "@hono/zod-validator";
|
|
5
9
|
import { Hono } from "hono";
|
|
@@ -81,11 +85,7 @@ var OpenAPIHono = class _OpenAPIHono extends Hono {
|
|
|
81
85
|
app.openAPIRegistry.definitions.forEach((def) => {
|
|
82
86
|
switch (def.type) {
|
|
83
87
|
case "component":
|
|
84
|
-
return this.openAPIRegistry.registerComponent(
|
|
85
|
-
def.componentType,
|
|
86
|
-
def.name,
|
|
87
|
-
def.component
|
|
88
|
-
);
|
|
88
|
+
return this.openAPIRegistry.registerComponent(def.componentType, def.name, def.component);
|
|
89
89
|
case "route":
|
|
90
90
|
return this.openAPIRegistry.registerPath({
|
|
91
91
|
...def.route,
|
|
@@ -97,10 +97,7 @@ var OpenAPIHono = class _OpenAPIHono extends Hono {
|
|
|
97
97
|
path: `${path}${def.webhook.path}`
|
|
98
98
|
});
|
|
99
99
|
case "schema":
|
|
100
|
-
return this.openAPIRegistry.register(
|
|
101
|
-
def.schema._def.openapi._internal.refId,
|
|
102
|
-
def.schema
|
|
103
|
-
);
|
|
100
|
+
return this.openAPIRegistry.register(def.schema._def.openapi._internal.refId, def.schema);
|
|
104
101
|
case "parameter":
|
|
105
102
|
return this.openAPIRegistry.registerParameter(
|
|
106
103
|
def.schema._def.openapi._internal.refId,
|
|
@@ -115,7 +112,14 @@ var OpenAPIHono = class _OpenAPIHono extends Hono {
|
|
|
115
112
|
return this;
|
|
116
113
|
}
|
|
117
114
|
};
|
|
118
|
-
var createRoute = (routeConfig) =>
|
|
115
|
+
var createRoute = (routeConfig) => {
|
|
116
|
+
return {
|
|
117
|
+
...routeConfig,
|
|
118
|
+
getRoutingPath() {
|
|
119
|
+
return routeConfig.path.replaceAll(/\/{(.+?)}/g, "/:$1");
|
|
120
|
+
}
|
|
121
|
+
};
|
|
122
|
+
};
|
|
119
123
|
extendZodWithOpenApi(z);
|
|
120
124
|
export {
|
|
121
125
|
OpenAPIHono,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hono/zod-openapi",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.1",
|
|
4
4
|
"description": "A wrapper class of Hono which supports OpenAPI.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -31,7 +31,7 @@
|
|
|
31
31
|
"zod": "3.*"
|
|
32
32
|
},
|
|
33
33
|
"devDependencies": {
|
|
34
|
-
"hono": "^3.
|
|
34
|
+
"hono": "^3.6.3",
|
|
35
35
|
"zod": "^3.22.1"
|
|
36
36
|
},
|
|
37
37
|
"dependencies": {
|