hono-openapi 0.5.0-rc.1 → 0.5.0-rc.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/dist/index.cjs +33 -0
- package/dist/index.d.cts +22 -172
- package/dist/index.d.ts +22 -172
- package/dist/index.js +33 -1
- package/package.json +3 -3
package/dist/index.cjs
CHANGED
|
@@ -407,8 +407,41 @@ function describeRoute(spec) {
|
|
|
407
407
|
}
|
|
408
408
|
});
|
|
409
409
|
}
|
|
410
|
+
function describeResponse(handler, responses) {
|
|
411
|
+
const _responses = Object.entries(responses).reduce(
|
|
412
|
+
(acc, [statusCode, response]) => {
|
|
413
|
+
if (response.content) {
|
|
414
|
+
const content = Object.entries(response.content).reduce(
|
|
415
|
+
(contentAcc, [mediaType, media]) => {
|
|
416
|
+
if (media.vSchema) {
|
|
417
|
+
contentAcc[mediaType] = {
|
|
418
|
+
...media,
|
|
419
|
+
schema: resolver(media.vSchema)
|
|
420
|
+
};
|
|
421
|
+
} else {
|
|
422
|
+
contentAcc[mediaType] = media;
|
|
423
|
+
}
|
|
424
|
+
return contentAcc;
|
|
425
|
+
},
|
|
426
|
+
{}
|
|
427
|
+
);
|
|
428
|
+
acc[statusCode] = { ...response, content };
|
|
429
|
+
} else {
|
|
430
|
+
acc[statusCode] = response;
|
|
431
|
+
}
|
|
432
|
+
return acc;
|
|
433
|
+
},
|
|
434
|
+
{}
|
|
435
|
+
);
|
|
436
|
+
return Object.assign(handler, {
|
|
437
|
+
[uniqueSymbol]: {
|
|
438
|
+
spec: { responses: _responses }
|
|
439
|
+
}
|
|
440
|
+
});
|
|
441
|
+
}
|
|
410
442
|
|
|
411
443
|
exports.ALLOWED_METHODS = ALLOWED_METHODS;
|
|
444
|
+
exports.describeResponse = describeResponse;
|
|
412
445
|
exports.describeRoute = describeRoute;
|
|
413
446
|
exports.generateSpecs = generateSpecs;
|
|
414
447
|
exports.openAPIRouteHandler = openAPIRouteHandler;
|
package/dist/index.d.cts
CHANGED
|
@@ -1,175 +1,10 @@
|
|
|
1
1
|
import * as openapi_types from 'openapi-types';
|
|
2
2
|
import { OpenAPIV3_1 } from 'openapi-types';
|
|
3
|
-
import {
|
|
4
|
-
import { ValidationTargets as ValidationTargets$1, RouterRoute, BlankEnv, Input as Input$1,
|
|
3
|
+
import { Env, Input, Context, Next, MiddlewareHandler, ValidationTargets, Hono } from 'hono';
|
|
4
|
+
import { BlankInput, TypedResponse, ValidationTargets as ValidationTargets$1, RouterRoute, BlankEnv, Input as Input$1, Schema, BlankSchema, MiddlewareHandler as MiddlewareHandler$1 } from 'hono/types';
|
|
5
|
+
import * as json_schema from 'json-schema';
|
|
5
6
|
import { Hook } from '@hono/standard-validator';
|
|
6
|
-
|
|
7
|
-
// ==================================================================================================
|
|
8
|
-
// JSON Schema Draft 07
|
|
9
|
-
// ==================================================================================================
|
|
10
|
-
// https://tools.ietf.org/html/draft-handrews-json-schema-validation-01
|
|
11
|
-
// --------------------------------------------------------------------------------------------------
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* Primitive type
|
|
15
|
-
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.1.1
|
|
16
|
-
*/
|
|
17
|
-
type JSONSchema7TypeName =
|
|
18
|
-
| "string" //
|
|
19
|
-
| "number"
|
|
20
|
-
| "integer"
|
|
21
|
-
| "boolean"
|
|
22
|
-
| "object"
|
|
23
|
-
| "array"
|
|
24
|
-
| "null";
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* Primitive type
|
|
28
|
-
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.1.1
|
|
29
|
-
*/
|
|
30
|
-
type JSONSchema7Type =
|
|
31
|
-
| string //
|
|
32
|
-
| number
|
|
33
|
-
| boolean
|
|
34
|
-
| JSONSchema7Object
|
|
35
|
-
| JSONSchema7Array
|
|
36
|
-
| null;
|
|
37
|
-
|
|
38
|
-
// Workaround for infinite type recursion
|
|
39
|
-
interface JSONSchema7Object {
|
|
40
|
-
[key: string]: JSONSchema7Type;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
// Workaround for infinite type recursion
|
|
44
|
-
// https://github.com/Microsoft/TypeScript/issues/3496#issuecomment-128553540
|
|
45
|
-
interface JSONSchema7Array extends Array<JSONSchema7Type> {}
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* Meta schema
|
|
49
|
-
*
|
|
50
|
-
* Recommended values:
|
|
51
|
-
* - 'http://json-schema.org/schema#'
|
|
52
|
-
* - 'http://json-schema.org/hyper-schema#'
|
|
53
|
-
* - 'http://json-schema.org/draft-07/schema#'
|
|
54
|
-
* - 'http://json-schema.org/draft-07/hyper-schema#'
|
|
55
|
-
*
|
|
56
|
-
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-5
|
|
57
|
-
*/
|
|
58
|
-
type JSONSchema7Version = string;
|
|
59
|
-
|
|
60
|
-
/**
|
|
61
|
-
* JSON Schema v7
|
|
62
|
-
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01
|
|
63
|
-
*/
|
|
64
|
-
type JSONSchema7Definition = JSONSchema7 | boolean;
|
|
65
|
-
interface JSONSchema7 {
|
|
66
|
-
$id?: string | undefined;
|
|
67
|
-
$ref?: string | undefined;
|
|
68
|
-
$schema?: JSONSchema7Version | undefined;
|
|
69
|
-
$comment?: string | undefined;
|
|
70
|
-
|
|
71
|
-
/**
|
|
72
|
-
* @see https://datatracker.ietf.org/doc/html/draft-bhutton-json-schema-00#section-8.2.4
|
|
73
|
-
* @see https://datatracker.ietf.org/doc/html/draft-bhutton-json-schema-validation-00#appendix-A
|
|
74
|
-
*/
|
|
75
|
-
$defs?: {
|
|
76
|
-
[key: string]: JSONSchema7Definition;
|
|
77
|
-
} | undefined;
|
|
78
|
-
|
|
79
|
-
/**
|
|
80
|
-
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.1
|
|
81
|
-
*/
|
|
82
|
-
type?: JSONSchema7TypeName | JSONSchema7TypeName[] | undefined;
|
|
83
|
-
enum?: JSONSchema7Type[] | undefined;
|
|
84
|
-
const?: JSONSchema7Type | undefined;
|
|
85
|
-
|
|
86
|
-
/**
|
|
87
|
-
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.2
|
|
88
|
-
*/
|
|
89
|
-
multipleOf?: number | undefined;
|
|
90
|
-
maximum?: number | undefined;
|
|
91
|
-
exclusiveMaximum?: number | undefined;
|
|
92
|
-
minimum?: number | undefined;
|
|
93
|
-
exclusiveMinimum?: number | undefined;
|
|
94
|
-
|
|
95
|
-
/**
|
|
96
|
-
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.3
|
|
97
|
-
*/
|
|
98
|
-
maxLength?: number | undefined;
|
|
99
|
-
minLength?: number | undefined;
|
|
100
|
-
pattern?: string | undefined;
|
|
101
|
-
|
|
102
|
-
/**
|
|
103
|
-
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.4
|
|
104
|
-
*/
|
|
105
|
-
items?: JSONSchema7Definition | JSONSchema7Definition[] | undefined;
|
|
106
|
-
additionalItems?: JSONSchema7Definition | undefined;
|
|
107
|
-
maxItems?: number | undefined;
|
|
108
|
-
minItems?: number | undefined;
|
|
109
|
-
uniqueItems?: boolean | undefined;
|
|
110
|
-
contains?: JSONSchema7Definition | undefined;
|
|
111
|
-
|
|
112
|
-
/**
|
|
113
|
-
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.5
|
|
114
|
-
*/
|
|
115
|
-
maxProperties?: number | undefined;
|
|
116
|
-
minProperties?: number | undefined;
|
|
117
|
-
required?: string[] | undefined;
|
|
118
|
-
properties?: {
|
|
119
|
-
[key: string]: JSONSchema7Definition;
|
|
120
|
-
} | undefined;
|
|
121
|
-
patternProperties?: {
|
|
122
|
-
[key: string]: JSONSchema7Definition;
|
|
123
|
-
} | undefined;
|
|
124
|
-
additionalProperties?: JSONSchema7Definition | undefined;
|
|
125
|
-
dependencies?: {
|
|
126
|
-
[key: string]: JSONSchema7Definition | string[];
|
|
127
|
-
} | undefined;
|
|
128
|
-
propertyNames?: JSONSchema7Definition | undefined;
|
|
129
|
-
|
|
130
|
-
/**
|
|
131
|
-
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.6
|
|
132
|
-
*/
|
|
133
|
-
if?: JSONSchema7Definition | undefined;
|
|
134
|
-
then?: JSONSchema7Definition | undefined;
|
|
135
|
-
else?: JSONSchema7Definition | undefined;
|
|
136
|
-
|
|
137
|
-
/**
|
|
138
|
-
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.7
|
|
139
|
-
*/
|
|
140
|
-
allOf?: JSONSchema7Definition[] | undefined;
|
|
141
|
-
anyOf?: JSONSchema7Definition[] | undefined;
|
|
142
|
-
oneOf?: JSONSchema7Definition[] | undefined;
|
|
143
|
-
not?: JSONSchema7Definition | undefined;
|
|
144
|
-
|
|
145
|
-
/**
|
|
146
|
-
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-7
|
|
147
|
-
*/
|
|
148
|
-
format?: string | undefined;
|
|
149
|
-
|
|
150
|
-
/**
|
|
151
|
-
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-8
|
|
152
|
-
*/
|
|
153
|
-
contentMediaType?: string | undefined;
|
|
154
|
-
contentEncoding?: string | undefined;
|
|
155
|
-
|
|
156
|
-
/**
|
|
157
|
-
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-9
|
|
158
|
-
*/
|
|
159
|
-
definitions?: {
|
|
160
|
-
[key: string]: JSONSchema7Definition;
|
|
161
|
-
} | undefined;
|
|
162
|
-
|
|
163
|
-
/**
|
|
164
|
-
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-10
|
|
165
|
-
*/
|
|
166
|
-
title?: string | undefined;
|
|
167
|
-
description?: string | undefined;
|
|
168
|
-
default?: JSONSchema7Type | undefined;
|
|
169
|
-
readOnly?: boolean | undefined;
|
|
170
|
-
writeOnly?: boolean | undefined;
|
|
171
|
-
examples?: JSONSchema7Type | undefined;
|
|
172
|
-
}
|
|
7
|
+
import { StatusCode } from 'hono/utils/http-status';
|
|
173
8
|
|
|
174
9
|
/** The Standard Schema interface. */
|
|
175
10
|
interface StandardSchemaV1<Input = unknown, Output = Input> {
|
|
@@ -236,7 +71,7 @@ declare namespace StandardSchemaV1 {
|
|
|
236
71
|
declare function resolver<Schema extends StandardSchemaV1>(schema: Schema): {
|
|
237
72
|
vendor: string;
|
|
238
73
|
validate: (value: unknown) => StandardSchemaV1.Result<unknown> | Promise<StandardSchemaV1.Result<unknown>>;
|
|
239
|
-
toJSONSchema: (options?: Record<string, unknown>) => Promise<JSONSchema7>;
|
|
74
|
+
toJSONSchema: (options?: Record<string, unknown>) => Promise<json_schema.JSONSchema7>;
|
|
240
75
|
toOpenAPISchema: (options?: Record<string, unknown>) => Promise<{
|
|
241
76
|
schema: OpenAPIV3_1.SchemaObject;
|
|
242
77
|
components: OpenAPIV3_1.ComponentsObject | undefined;
|
|
@@ -270,6 +105,21 @@ declare function validator<Schema extends StandardSchemaV1, Target extends keyof
|
|
|
270
105
|
* @returns Middleware handler
|
|
271
106
|
*/
|
|
272
107
|
declare function describeRoute(spec: DescribeRouteOptions): MiddlewareHandler;
|
|
108
|
+
type ResponseObject<T extends Partial<Record<StatusCode, StandardSchemaV1>>> = {
|
|
109
|
+
[K in keyof T]: OpenAPIV3_1.ReferenceObject | (OpenAPIV3_1.ResponseObject & {
|
|
110
|
+
content?: {
|
|
111
|
+
[media: string]: OpenAPIV3_1.MediaTypeObject & {
|
|
112
|
+
vSchema?: T[K];
|
|
113
|
+
};
|
|
114
|
+
};
|
|
115
|
+
});
|
|
116
|
+
};
|
|
117
|
+
type Num<T> = T extends `${infer N extends number}` ? N : T;
|
|
118
|
+
type HandlerResponse<T extends Partial<Record<StatusCode, StandardSchemaV1>> = Partial<Record<StatusCode, StandardSchemaV1>>> = {
|
|
119
|
+
[K in keyof T]: T[K] extends StandardSchemaV1 ? PromiseOr<TypedResponse<StandardSchemaV1.InferOutput<T[K]>, Num<K> extends StatusCode ? Num<K> : never>> : never;
|
|
120
|
+
}[keyof T];
|
|
121
|
+
type Handler<E extends Env = any, P extends string = any, I extends Input = BlankInput, T extends Partial<Record<StatusCode, StandardSchemaV1>> = Partial<Record<StatusCode, StandardSchemaV1>>> = (c: Context<E, P, I>, next: Next) => HandlerResponse<T>;
|
|
122
|
+
declare function describeResponse<E extends Env = any, P extends string = any, I extends Input = BlankInput, T extends Partial<Record<StatusCode, StandardSchemaV1>> = Partial<Record<StatusCode, StandardSchemaV1>>>(handler: Handler<E, P, I, T>, responses: ResponseObject<T>): Handler<E, P, I, T>;
|
|
273
123
|
|
|
274
124
|
/**
|
|
275
125
|
* The unique symbol for the middlewares, which makes it easier to identify them. Not meant to be used directly, unless you're creating a custom middleware.
|
|
@@ -360,7 +210,7 @@ type RegisterSchemaPathOptions = {
|
|
|
360
210
|
* @param options Options for generating OpenAPI specs
|
|
361
211
|
* @returns Middleware handler for OpenAPI specs
|
|
362
212
|
*/
|
|
363
|
-
declare function openAPIRouteHandler<E extends Env = BlankEnv, P extends string = string, I extends Input$1 = BlankInput, S extends Schema = BlankSchema>(hono: Hono<E, S, P>, options?: GenerateSpecOptions): MiddlewareHandler$1<E, P, I>;
|
|
213
|
+
declare function openAPIRouteHandler<E extends Env = BlankEnv, P extends string = string, I extends Input$1 = BlankInput, S extends Schema = BlankSchema>(hono: Hono<E, S, P>, options?: Partial<GenerateSpecOptions>): MiddlewareHandler$1<E, P, I>;
|
|
364
214
|
/**
|
|
365
215
|
* Generate OpenAPI specs for the given Hono instance
|
|
366
216
|
* @param hono Instance of Hono
|
|
@@ -417,4 +267,4 @@ declare function generateSpecs<E extends Env = BlankEnv, P extends string = stri
|
|
|
417
267
|
jsonSchemaDialect?: string;
|
|
418
268
|
}>;
|
|
419
269
|
|
|
420
|
-
export { ALLOWED_METHODS, type AllowedMethods, type DescribeRouteOptions, type GenerateSpecOptions, type HandlerUniqueProperty, type PromiseOr, type RegisterSchemaPathOptions, type ResolverReturnType, type SanitizedGenerateSpecOptions, describeRoute, generateSpecs, openAPIRouteHandler, registerSchemaPath, removeExcludedPaths, resolver, uniqueSymbol, validator };
|
|
270
|
+
export { ALLOWED_METHODS, type AllowedMethods, type DescribeRouteOptions, type GenerateSpecOptions, type HandlerUniqueProperty, type PromiseOr, type RegisterSchemaPathOptions, type ResolverReturnType, type SanitizedGenerateSpecOptions, describeResponse, describeRoute, generateSpecs, openAPIRouteHandler, registerSchemaPath, removeExcludedPaths, resolver, uniqueSymbol, validator };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,175 +1,10 @@
|
|
|
1
1
|
import * as openapi_types from 'openapi-types';
|
|
2
2
|
import { OpenAPIV3_1 } from 'openapi-types';
|
|
3
|
-
import {
|
|
4
|
-
import { ValidationTargets as ValidationTargets$1, RouterRoute, BlankEnv, Input as Input$1,
|
|
3
|
+
import { Env, Input, Context, Next, MiddlewareHandler, ValidationTargets, Hono } from 'hono';
|
|
4
|
+
import { BlankInput, TypedResponse, ValidationTargets as ValidationTargets$1, RouterRoute, BlankEnv, Input as Input$1, Schema, BlankSchema, MiddlewareHandler as MiddlewareHandler$1 } from 'hono/types';
|
|
5
|
+
import * as json_schema from 'json-schema';
|
|
5
6
|
import { Hook } from '@hono/standard-validator';
|
|
6
|
-
|
|
7
|
-
// ==================================================================================================
|
|
8
|
-
// JSON Schema Draft 07
|
|
9
|
-
// ==================================================================================================
|
|
10
|
-
// https://tools.ietf.org/html/draft-handrews-json-schema-validation-01
|
|
11
|
-
// --------------------------------------------------------------------------------------------------
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* Primitive type
|
|
15
|
-
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.1.1
|
|
16
|
-
*/
|
|
17
|
-
type JSONSchema7TypeName =
|
|
18
|
-
| "string" //
|
|
19
|
-
| "number"
|
|
20
|
-
| "integer"
|
|
21
|
-
| "boolean"
|
|
22
|
-
| "object"
|
|
23
|
-
| "array"
|
|
24
|
-
| "null";
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* Primitive type
|
|
28
|
-
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.1.1
|
|
29
|
-
*/
|
|
30
|
-
type JSONSchema7Type =
|
|
31
|
-
| string //
|
|
32
|
-
| number
|
|
33
|
-
| boolean
|
|
34
|
-
| JSONSchema7Object
|
|
35
|
-
| JSONSchema7Array
|
|
36
|
-
| null;
|
|
37
|
-
|
|
38
|
-
// Workaround for infinite type recursion
|
|
39
|
-
interface JSONSchema7Object {
|
|
40
|
-
[key: string]: JSONSchema7Type;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
// Workaround for infinite type recursion
|
|
44
|
-
// https://github.com/Microsoft/TypeScript/issues/3496#issuecomment-128553540
|
|
45
|
-
interface JSONSchema7Array extends Array<JSONSchema7Type> {}
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* Meta schema
|
|
49
|
-
*
|
|
50
|
-
* Recommended values:
|
|
51
|
-
* - 'http://json-schema.org/schema#'
|
|
52
|
-
* - 'http://json-schema.org/hyper-schema#'
|
|
53
|
-
* - 'http://json-schema.org/draft-07/schema#'
|
|
54
|
-
* - 'http://json-schema.org/draft-07/hyper-schema#'
|
|
55
|
-
*
|
|
56
|
-
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-5
|
|
57
|
-
*/
|
|
58
|
-
type JSONSchema7Version = string;
|
|
59
|
-
|
|
60
|
-
/**
|
|
61
|
-
* JSON Schema v7
|
|
62
|
-
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01
|
|
63
|
-
*/
|
|
64
|
-
type JSONSchema7Definition = JSONSchema7 | boolean;
|
|
65
|
-
interface JSONSchema7 {
|
|
66
|
-
$id?: string | undefined;
|
|
67
|
-
$ref?: string | undefined;
|
|
68
|
-
$schema?: JSONSchema7Version | undefined;
|
|
69
|
-
$comment?: string | undefined;
|
|
70
|
-
|
|
71
|
-
/**
|
|
72
|
-
* @see https://datatracker.ietf.org/doc/html/draft-bhutton-json-schema-00#section-8.2.4
|
|
73
|
-
* @see https://datatracker.ietf.org/doc/html/draft-bhutton-json-schema-validation-00#appendix-A
|
|
74
|
-
*/
|
|
75
|
-
$defs?: {
|
|
76
|
-
[key: string]: JSONSchema7Definition;
|
|
77
|
-
} | undefined;
|
|
78
|
-
|
|
79
|
-
/**
|
|
80
|
-
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.1
|
|
81
|
-
*/
|
|
82
|
-
type?: JSONSchema7TypeName | JSONSchema7TypeName[] | undefined;
|
|
83
|
-
enum?: JSONSchema7Type[] | undefined;
|
|
84
|
-
const?: JSONSchema7Type | undefined;
|
|
85
|
-
|
|
86
|
-
/**
|
|
87
|
-
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.2
|
|
88
|
-
*/
|
|
89
|
-
multipleOf?: number | undefined;
|
|
90
|
-
maximum?: number | undefined;
|
|
91
|
-
exclusiveMaximum?: number | undefined;
|
|
92
|
-
minimum?: number | undefined;
|
|
93
|
-
exclusiveMinimum?: number | undefined;
|
|
94
|
-
|
|
95
|
-
/**
|
|
96
|
-
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.3
|
|
97
|
-
*/
|
|
98
|
-
maxLength?: number | undefined;
|
|
99
|
-
minLength?: number | undefined;
|
|
100
|
-
pattern?: string | undefined;
|
|
101
|
-
|
|
102
|
-
/**
|
|
103
|
-
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.4
|
|
104
|
-
*/
|
|
105
|
-
items?: JSONSchema7Definition | JSONSchema7Definition[] | undefined;
|
|
106
|
-
additionalItems?: JSONSchema7Definition | undefined;
|
|
107
|
-
maxItems?: number | undefined;
|
|
108
|
-
minItems?: number | undefined;
|
|
109
|
-
uniqueItems?: boolean | undefined;
|
|
110
|
-
contains?: JSONSchema7Definition | undefined;
|
|
111
|
-
|
|
112
|
-
/**
|
|
113
|
-
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.5
|
|
114
|
-
*/
|
|
115
|
-
maxProperties?: number | undefined;
|
|
116
|
-
minProperties?: number | undefined;
|
|
117
|
-
required?: string[] | undefined;
|
|
118
|
-
properties?: {
|
|
119
|
-
[key: string]: JSONSchema7Definition;
|
|
120
|
-
} | undefined;
|
|
121
|
-
patternProperties?: {
|
|
122
|
-
[key: string]: JSONSchema7Definition;
|
|
123
|
-
} | undefined;
|
|
124
|
-
additionalProperties?: JSONSchema7Definition | undefined;
|
|
125
|
-
dependencies?: {
|
|
126
|
-
[key: string]: JSONSchema7Definition | string[];
|
|
127
|
-
} | undefined;
|
|
128
|
-
propertyNames?: JSONSchema7Definition | undefined;
|
|
129
|
-
|
|
130
|
-
/**
|
|
131
|
-
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.6
|
|
132
|
-
*/
|
|
133
|
-
if?: JSONSchema7Definition | undefined;
|
|
134
|
-
then?: JSONSchema7Definition | undefined;
|
|
135
|
-
else?: JSONSchema7Definition | undefined;
|
|
136
|
-
|
|
137
|
-
/**
|
|
138
|
-
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.7
|
|
139
|
-
*/
|
|
140
|
-
allOf?: JSONSchema7Definition[] | undefined;
|
|
141
|
-
anyOf?: JSONSchema7Definition[] | undefined;
|
|
142
|
-
oneOf?: JSONSchema7Definition[] | undefined;
|
|
143
|
-
not?: JSONSchema7Definition | undefined;
|
|
144
|
-
|
|
145
|
-
/**
|
|
146
|
-
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-7
|
|
147
|
-
*/
|
|
148
|
-
format?: string | undefined;
|
|
149
|
-
|
|
150
|
-
/**
|
|
151
|
-
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-8
|
|
152
|
-
*/
|
|
153
|
-
contentMediaType?: string | undefined;
|
|
154
|
-
contentEncoding?: string | undefined;
|
|
155
|
-
|
|
156
|
-
/**
|
|
157
|
-
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-9
|
|
158
|
-
*/
|
|
159
|
-
definitions?: {
|
|
160
|
-
[key: string]: JSONSchema7Definition;
|
|
161
|
-
} | undefined;
|
|
162
|
-
|
|
163
|
-
/**
|
|
164
|
-
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-10
|
|
165
|
-
*/
|
|
166
|
-
title?: string | undefined;
|
|
167
|
-
description?: string | undefined;
|
|
168
|
-
default?: JSONSchema7Type | undefined;
|
|
169
|
-
readOnly?: boolean | undefined;
|
|
170
|
-
writeOnly?: boolean | undefined;
|
|
171
|
-
examples?: JSONSchema7Type | undefined;
|
|
172
|
-
}
|
|
7
|
+
import { StatusCode } from 'hono/utils/http-status';
|
|
173
8
|
|
|
174
9
|
/** The Standard Schema interface. */
|
|
175
10
|
interface StandardSchemaV1<Input = unknown, Output = Input> {
|
|
@@ -236,7 +71,7 @@ declare namespace StandardSchemaV1 {
|
|
|
236
71
|
declare function resolver<Schema extends StandardSchemaV1>(schema: Schema): {
|
|
237
72
|
vendor: string;
|
|
238
73
|
validate: (value: unknown) => StandardSchemaV1.Result<unknown> | Promise<StandardSchemaV1.Result<unknown>>;
|
|
239
|
-
toJSONSchema: (options?: Record<string, unknown>) => Promise<JSONSchema7>;
|
|
74
|
+
toJSONSchema: (options?: Record<string, unknown>) => Promise<json_schema.JSONSchema7>;
|
|
240
75
|
toOpenAPISchema: (options?: Record<string, unknown>) => Promise<{
|
|
241
76
|
schema: OpenAPIV3_1.SchemaObject;
|
|
242
77
|
components: OpenAPIV3_1.ComponentsObject | undefined;
|
|
@@ -270,6 +105,21 @@ declare function validator<Schema extends StandardSchemaV1, Target extends keyof
|
|
|
270
105
|
* @returns Middleware handler
|
|
271
106
|
*/
|
|
272
107
|
declare function describeRoute(spec: DescribeRouteOptions): MiddlewareHandler;
|
|
108
|
+
type ResponseObject<T extends Partial<Record<StatusCode, StandardSchemaV1>>> = {
|
|
109
|
+
[K in keyof T]: OpenAPIV3_1.ReferenceObject | (OpenAPIV3_1.ResponseObject & {
|
|
110
|
+
content?: {
|
|
111
|
+
[media: string]: OpenAPIV3_1.MediaTypeObject & {
|
|
112
|
+
vSchema?: T[K];
|
|
113
|
+
};
|
|
114
|
+
};
|
|
115
|
+
});
|
|
116
|
+
};
|
|
117
|
+
type Num<T> = T extends `${infer N extends number}` ? N : T;
|
|
118
|
+
type HandlerResponse<T extends Partial<Record<StatusCode, StandardSchemaV1>> = Partial<Record<StatusCode, StandardSchemaV1>>> = {
|
|
119
|
+
[K in keyof T]: T[K] extends StandardSchemaV1 ? PromiseOr<TypedResponse<StandardSchemaV1.InferOutput<T[K]>, Num<K> extends StatusCode ? Num<K> : never>> : never;
|
|
120
|
+
}[keyof T];
|
|
121
|
+
type Handler<E extends Env = any, P extends string = any, I extends Input = BlankInput, T extends Partial<Record<StatusCode, StandardSchemaV1>> = Partial<Record<StatusCode, StandardSchemaV1>>> = (c: Context<E, P, I>, next: Next) => HandlerResponse<T>;
|
|
122
|
+
declare function describeResponse<E extends Env = any, P extends string = any, I extends Input = BlankInput, T extends Partial<Record<StatusCode, StandardSchemaV1>> = Partial<Record<StatusCode, StandardSchemaV1>>>(handler: Handler<E, P, I, T>, responses: ResponseObject<T>): Handler<E, P, I, T>;
|
|
273
123
|
|
|
274
124
|
/**
|
|
275
125
|
* The unique symbol for the middlewares, which makes it easier to identify them. Not meant to be used directly, unless you're creating a custom middleware.
|
|
@@ -360,7 +210,7 @@ type RegisterSchemaPathOptions = {
|
|
|
360
210
|
* @param options Options for generating OpenAPI specs
|
|
361
211
|
* @returns Middleware handler for OpenAPI specs
|
|
362
212
|
*/
|
|
363
|
-
declare function openAPIRouteHandler<E extends Env = BlankEnv, P extends string = string, I extends Input$1 = BlankInput, S extends Schema = BlankSchema>(hono: Hono<E, S, P>, options?: GenerateSpecOptions): MiddlewareHandler$1<E, P, I>;
|
|
213
|
+
declare function openAPIRouteHandler<E extends Env = BlankEnv, P extends string = string, I extends Input$1 = BlankInput, S extends Schema = BlankSchema>(hono: Hono<E, S, P>, options?: Partial<GenerateSpecOptions>): MiddlewareHandler$1<E, P, I>;
|
|
364
214
|
/**
|
|
365
215
|
* Generate OpenAPI specs for the given Hono instance
|
|
366
216
|
* @param hono Instance of Hono
|
|
@@ -417,4 +267,4 @@ declare function generateSpecs<E extends Env = BlankEnv, P extends string = stri
|
|
|
417
267
|
jsonSchemaDialect?: string;
|
|
418
268
|
}>;
|
|
419
269
|
|
|
420
|
-
export { ALLOWED_METHODS, type AllowedMethods, type DescribeRouteOptions, type GenerateSpecOptions, type HandlerUniqueProperty, type PromiseOr, type RegisterSchemaPathOptions, type ResolverReturnType, type SanitizedGenerateSpecOptions, describeRoute, generateSpecs, openAPIRouteHandler, registerSchemaPath, removeExcludedPaths, resolver, uniqueSymbol, validator };
|
|
270
|
+
export { ALLOWED_METHODS, type AllowedMethods, type DescribeRouteOptions, type GenerateSpecOptions, type HandlerUniqueProperty, type PromiseOr, type RegisterSchemaPathOptions, type ResolverReturnType, type SanitizedGenerateSpecOptions, describeResponse, describeRoute, generateSpecs, openAPIRouteHandler, registerSchemaPath, removeExcludedPaths, resolver, uniqueSymbol, validator };
|
package/dist/index.js
CHANGED
|
@@ -405,5 +405,37 @@ function describeRoute(spec) {
|
|
|
405
405
|
}
|
|
406
406
|
});
|
|
407
407
|
}
|
|
408
|
+
function describeResponse(handler, responses) {
|
|
409
|
+
const _responses = Object.entries(responses).reduce(
|
|
410
|
+
(acc, [statusCode, response]) => {
|
|
411
|
+
if (response.content) {
|
|
412
|
+
const content = Object.entries(response.content).reduce(
|
|
413
|
+
(contentAcc, [mediaType, media]) => {
|
|
414
|
+
if (media.vSchema) {
|
|
415
|
+
contentAcc[mediaType] = {
|
|
416
|
+
...media,
|
|
417
|
+
schema: resolver(media.vSchema)
|
|
418
|
+
};
|
|
419
|
+
} else {
|
|
420
|
+
contentAcc[mediaType] = media;
|
|
421
|
+
}
|
|
422
|
+
return contentAcc;
|
|
423
|
+
},
|
|
424
|
+
{}
|
|
425
|
+
);
|
|
426
|
+
acc[statusCode] = { ...response, content };
|
|
427
|
+
} else {
|
|
428
|
+
acc[statusCode] = response;
|
|
429
|
+
}
|
|
430
|
+
return acc;
|
|
431
|
+
},
|
|
432
|
+
{}
|
|
433
|
+
);
|
|
434
|
+
return Object.assign(handler, {
|
|
435
|
+
[uniqueSymbol]: {
|
|
436
|
+
spec: { responses: _responses }
|
|
437
|
+
}
|
|
438
|
+
});
|
|
439
|
+
}
|
|
408
440
|
|
|
409
|
-
export { ALLOWED_METHODS, describeRoute, generateSpecs, openAPIRouteHandler, registerSchemaPath, removeExcludedPaths, resolver, uniqueSymbol, validator };
|
|
441
|
+
export { ALLOWED_METHODS, describeResponse, describeRoute, generateSpecs, openAPIRouteHandler, registerSchemaPath, removeExcludedPaths, resolver, uniqueSymbol, validator };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "hono-openapi",
|
|
3
3
|
"description": "OpenAPI schema generator for Hono",
|
|
4
|
-
"version": "0.5.0-rc.
|
|
4
|
+
"version": "0.5.0-rc.3",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.cjs",
|
|
7
7
|
"module": "dist/index.js",
|
|
@@ -44,8 +44,9 @@
|
|
|
44
44
|
"peerDependencies": {
|
|
45
45
|
"@hono/standard-validator": "^0.1.2",
|
|
46
46
|
"@sinclair/typebox": "^0.34.9",
|
|
47
|
-
"@standard-community/standard-json": "^0.3.0-rc.
|
|
47
|
+
"@standard-community/standard-json": "^0.3.0-rc.3",
|
|
48
48
|
"@standard-community/standard-openapi": "^0.2.0-rc.1",
|
|
49
|
+
"@types/json-schema": "^7.0.15",
|
|
49
50
|
"arktype": "^2.0.0",
|
|
50
51
|
"effect": "^3.16.12",
|
|
51
52
|
"hono": "^4.8.3",
|
|
@@ -79,7 +80,6 @@
|
|
|
79
80
|
"devDependencies": {
|
|
80
81
|
"@biomejs/biome": "^2.0.6",
|
|
81
82
|
"@standard-schema/spec": "^1.0.0",
|
|
82
|
-
"@types/json-schema": "^7.0.15",
|
|
83
83
|
"@valibot/to-json-schema": "^1.3.0",
|
|
84
84
|
"pkgroll": "^2.13.1",
|
|
85
85
|
"typescript": "^5.8.3",
|