@@ -13,56 +13,57 @@ import {
13
13
} from "./spec" ;
14
14
import { StatusCode } from "./hono-types" ;
15
15
import { JSONSchema7 } from "json-schema" ;
16
+ import { StandardSchemaV1 } from "@standard-schema/spec" ;
16
17
17
18
// eslint-disable-next-line @typescript-eslint/no-explicit-any
18
- type ToSchema = ( schema : any ) => JSONSchema7 ;
19
+ type ToSchema = ( schema : StandardSchemaV1 < any > ) => Promise < JSONSchema7 > ;
19
20
20
- export const toJsonSchemaApiEndpoints = < E extends AnyApiEndpoints > (
21
+ export const toJsonSchemaApiEndpoints = async < E extends AnyApiEndpoints > (
21
22
toSchema : ToSchema ,
22
23
endpoints : E ,
23
- ) : JsonSchemaApiEndpoints => {
24
+ ) : Promise < JsonSchemaApiEndpoints > => {
24
25
const ret : JsonSchemaApiEndpoints = { } ;
25
26
for ( const path of Object . keys ( endpoints ) ) {
26
- ret [ path ] = toJsonSchemaEndpoint ( toSchema , endpoints [ path ] ) ;
27
+ ret [ path ] = await toJsonSchemaEndpoint ( toSchema , endpoints [ path ] ) ;
27
28
}
28
29
return ret ;
29
30
} ;
30
31
31
- export const toJsonSchemaEndpoint = < Endpoint extends AnyApiEndpoint > (
32
+ export const toJsonSchemaEndpoint = async < Endpoint extends AnyApiEndpoint > (
32
33
toSchema : ToSchema ,
33
34
endpoint : Endpoint ,
34
- ) => {
35
+ ) : Promise < Partial < Record < Method , JsonSchemaApiSpec > > > => {
35
36
const ret : Partial < Record < Method , JsonSchemaApiSpec > > = { } ;
36
37
for ( const method of Method ) {
37
38
const spec = endpoint [ method ] ;
38
39
if ( spec ) {
39
- ret [ method ] = toJsonSchemaApiSpec ( toSchema , spec ) ;
40
+ ret [ method ] = await toJsonSchemaApiSpec ( toSchema , spec ) ;
40
41
}
41
42
}
42
43
return ret ;
43
44
} ;
44
45
45
- export const toJsonSchemaApiSpec = < Spec extends AnyApiSpec > (
46
+ export const toJsonSchemaApiSpec = async < Spec extends AnyApiSpec > (
46
47
toSchema : ToSchema ,
47
48
spec : Spec ,
48
- ) : JsonSchemaApiSpec => {
49
+ ) : Promise < JsonSchemaApiSpec > => {
49
50
const extraProps = extractExtraApiSpecProps ( spec ) ;
50
51
const ret : JsonSchemaApiSpec = {
51
- responses : toJsonSchemaResponses ( toSchema , spec . responses ) ,
52
+ responses : await toJsonSchemaResponses ( toSchema , spec . responses ) ,
52
53
} ;
53
54
for ( const key of apiSpecRequestKeys ) {
54
55
if ( spec [ key ] ) {
55
56
// eslint-disable-next-line @typescript-eslint/no-explicit-any
56
- ret [ key ] = toSchema ( spec [ key ] ) ;
57
+ ret [ key ] = await toSchema ( spec [ key ] ) ;
57
58
}
58
59
}
59
60
return { ...extraProps , ...ret } ;
60
61
} ;
61
62
62
- const toJsonSchemaResponses = (
63
+ const toJsonSchemaResponses = async (
63
64
toSchema : ToSchema ,
64
65
responses : AnyApiResponses ,
65
- ) : JsonSchemaApiResponses => {
66
+ ) : Promise < JsonSchemaApiResponses > => {
66
67
const statusCodes = Object . keys ( responses ) . map ( Number ) as StatusCode [ ] ;
67
68
const ret : JsonSchemaApiResponses = { } ;
68
69
for ( const statusCode of statusCodes ) {
@@ -72,8 +73,8 @@ const toJsonSchemaResponses = (
72
73
}
73
74
ret [ statusCode ] = {
74
75
...extractExtraResponseProps ( r ) ,
75
- body : toSchema ( r . body ) ,
76
- headers : r . headers ? toSchema ( r . headers ) : undefined ,
76
+ body : await toSchema ( r . body ) ,
77
+ headers : r . headers ? await toSchema ( r . headers ) : undefined ,
77
78
} ;
78
79
}
79
80
return ret ;
0 commit comments