@@ -7,6 +7,7 @@ import { OpenapiSchemaConvertContext, type LibSchemaObject } from "../src/types.
77import { tsFactory } from "../src/ts-factory.ts" ;
88import { mapOpenApiEndpoints } from "../src/map-openapi-endpoints.ts" ;
99import { generateFile } from "../src/generator.ts" ;
10+ import { prettify } from "../src/format.ts" ;
1011
1112const factory = tsFactory ;
1213const makeCtx = ( schemas : SchemasObject ) : OpenapiSchemaConvertContext => ( {
@@ -15,51 +16,51 @@ const makeCtx = (schemas: SchemasObject): OpenapiSchemaConvertContext => ({
1516} ) ;
1617const makeDoc = ( schemas : SchemasObject ) => ( { components : { schemas } } as any ) ;
1718
18- const getSchemaBox = ( schema : LibSchemaObject ) => {
19- const output = generateFile ( mapOpenApiEndpoints ( makeDoc ( { _Test : schema } ) ) ) ;
19+ const getSchemaBox = async ( schema : LibSchemaObject ) => {
20+ const output = await prettify ( generateFile ( mapOpenApiEndpoints ( makeDoc ( { _Test : schema } ) ) ) ) ;
2021 const start = output . indexOf ( "// <Schemas>" ) ;
2122 const end = output . indexOf ( "// </Schemas>" ) ;
2223 return output . substring ( start + "// <Schemas>" . length , end ) . trim ( ) ;
2324} ;
2425
2526test ( "getSchemaBox" , async ( ) => {
26- expect ( getSchemaBox ( { type : "null" } ) ) . toMatchInlineSnapshot ( ' "export type _Test = null;"' ) ;
27- expect ( getSchemaBox ( { type : "boolean" } ) ) . toMatchInlineSnapshot ( ' "export type _Test = boolean;"' ) ;
28- expect ( getSchemaBox ( { type : "boolean" , nullable : true } ) ) . toMatchInlineSnapshot ( ' "export type _Test = boolean | null;"' ) ;
29- expect ( getSchemaBox ( { type : "string" } ) ) . toMatchInlineSnapshot ( ' "export type _Test = string;"' ) ;
30- expect ( getSchemaBox ( { type : "number" } ) ) . toMatchInlineSnapshot ( ' "export type _Test = number;"' ) ;
31- expect ( getSchemaBox ( { type : "integer" } ) ) . toMatchInlineSnapshot ( ' "export type _Test = number;"' ) ;
32- expect ( getSchemaBox ( { } ) ) . toMatchInlineSnapshot ( ' "export type _Test = unknown;"' ) ;
33-
34- expect ( getSchemaBox ( { type : "array" , items : { type : "string" } } ) ) . toMatchInlineSnapshot ( ' "export type _Test = Array<string>;"' ) ;
35- expect ( getSchemaBox ( { type : "object" } ) ) . toMatchInlineSnapshot (
36- ' "export type _Test = Record<string, unknown>;"' ,
27+ expect ( await getSchemaBox ( { type : "null" } ) ) . toMatchInlineSnapshot ( ` "export type _Test = null;"` ) ;
28+ expect ( await getSchemaBox ( { type : "boolean" } ) ) . toMatchInlineSnapshot ( ` "export type _Test = boolean;"` ) ;
29+ expect ( await getSchemaBox ( { type : "boolean" , nullable : true } ) ) . toMatchInlineSnapshot ( ` "export type _Test = boolean | null;"` ) ;
30+ expect ( await getSchemaBox ( { type : "string" } ) ) . toMatchInlineSnapshot ( ` "export type _Test = string;"` ) ;
31+ expect ( await getSchemaBox ( { type : "number" } ) ) . toMatchInlineSnapshot ( ` "export type _Test = number;"` ) ;
32+ expect ( await getSchemaBox ( { type : "integer" } ) ) . toMatchInlineSnapshot ( ` "export type _Test = number;"` ) ;
33+ expect ( await getSchemaBox ( { } ) ) . toMatchInlineSnapshot ( ` "export type _Test = unknown;"` ) ;
34+
35+ expect ( await getSchemaBox ( { type : "array" , items : { type : "string" } } ) ) . toMatchInlineSnapshot ( ` "export type _Test = Array<string>;"` ) ;
36+ expect ( await getSchemaBox ( { type : "object" } ) ) . toMatchInlineSnapshot (
37+ ` "export type _Test = Record<string, unknown>;"` ,
3738 ) ;
38- expect ( getSchemaBox ( { type : "object" , properties : { str : { type : "string" } } } ) ) . toMatchInlineSnapshot ( ' "export type _Test = Partial<{ str: string }>;"' ) ;
39- expect ( getSchemaBox ( { type : "object" , properties : { str : { type : "string" } , nb : { type : "number" } } } ) )
40- . toMatchInlineSnapshot ( ' "export type _Test = Partial<{ str: string; nb: number }>;"' ) ;
39+ expect ( await getSchemaBox ( { type : "object" , properties : { str : { type : "string" } } } ) ) . toMatchInlineSnapshot ( ` "export type _Test = Partial<{ str: string }>;"` ) ;
40+ expect ( await getSchemaBox ( { type : "object" , properties : { str : { type : "string" } , nb : { type : "number" } } } ) )
41+ . toMatchInlineSnapshot ( ` "export type _Test = Partial<{ str: string; nb: number }>;"` ) ;
4142
4243 // AllPropertiesRequired
4344 expect (
44- getSchemaBox ( {
45+ await getSchemaBox ( {
4546 type : "object" ,
4647 properties : { str : { type : "string" } , nb : { type : "number" } } ,
4748 required : [ "str" , "nb" ] ,
4849 } ) ,
49- ) . toMatchInlineSnapshot ( ' "export type _Test = { str: string; nb: number };"' ) ;
50+ ) . toMatchInlineSnapshot ( ` "export type _Test = { str: string; nb: number };"` ) ;
5051
5152 // SomeOptionalProps
5253 expect (
53- getSchemaBox ( {
54+ await getSchemaBox ( {
5455 type : "object" ,
5556 properties : { str : { type : "string" } , nb : { type : "number" } } ,
5657 required : [ "str" ] ,
5758 } ) ,
58- ) . toMatchInlineSnapshot ( ' "export type _Test = { str: string; nb?: number | undefined };"' ) ;
59+ ) . toMatchInlineSnapshot ( ` "export type _Test = { str: string; nb?: number | undefined };"` ) ;
5960
6061 // ObjectWithNestedProp
6162 expect (
62- getSchemaBox ( {
63+ await getSchemaBox ( {
6364 type : "object" ,
6465 properties : {
6566 str : { type : "string" } ,
@@ -72,24 +73,24 @@ test("getSchemaBox", async () => {
7273 } ,
7374 } ,
7475 } ) ,
75- ) . toMatchInlineSnapshot ( ' "export type _Test = Partial<{ str: string; nb: number; nested: Partial<{ nested_prop: boolean }> }>;"' ) ;
76+ ) . toMatchInlineSnapshot ( ` "export type _Test = Partial<{ str: string; nb: number; nested: Partial<{ nested_prop: boolean }> }>;"` ) ;
7677
7778 // ObjectWithAdditionalPropsNb
7879 expect (
79- getSchemaBox ( { type : "object" , properties : { str : { type : "string" } } , additionalProperties : { type : "number" } } ) ,
80- ) . toMatchInlineSnapshot ( ' "export type _Test = Partial<{ str: string } & { string: number }>;"' ) ;
80+ await getSchemaBox ( { type : "object" , properties : { str : { type : "string" } } , additionalProperties : { type : "number" } } ) ,
81+ ) . toMatchInlineSnapshot ( ` "export type _Test = Partial<{ str: string } & { string: number }>;"` ) ;
8182
8283 // ObjectWithNestedRecordBoolean
8384 expect (
84- getSchemaBox ( {
85+ await getSchemaBox ( {
8586 type : "object" ,
8687 properties : { str : { type : "string" } } ,
8788 additionalProperties : { type : "object" , properties : { prop : { type : "boolean" } } } ,
8889 } ) ,
89- ) . toMatchInlineSnapshot ( ' "export type _Test = Partial<{ str: string } & { string: Partial<{ prop: boolean }> }>;"' ) ;
90+ ) . toMatchInlineSnapshot ( ` "export type _Test = Partial<{ str: string } & { string: Partial<{ prop: boolean }> }>;"` ) ;
9091
9192 expect (
92- getSchemaBox ( {
93+ await getSchemaBox ( {
9394 type : "array" ,
9495 items : {
9596 type : "object" ,
@@ -98,10 +99,10 @@ test("getSchemaBox", async () => {
9899 } ,
99100 } ,
100101 } ) ,
101- ) . toMatchInlineSnapshot ( ' "export type _Test = Array<Partial<{ str: string }>>;"' ) ;
102+ ) . toMatchInlineSnapshot ( ` "export type _Test = Array<Partial<{ str: string }>>;"` ) ;
102103
103104 expect (
104- getSchemaBox ( {
105+ await getSchemaBox ( {
105106 type : "array" ,
106107 items : {
107108 type : "array" ,
@@ -110,79 +111,79 @@ test("getSchemaBox", async () => {
110111 } ,
111112 } ,
112113 } ) ,
113- ) . toMatchInlineSnapshot ( ' "export type _Test = Array<Array<string>>;"' ) ;
114+ ) . toMatchInlineSnapshot ( ` "export type _Test = Array<Array<string>>;"` ) ;
114115
115116 // ObjectWithEnum
116117 expect (
117- getSchemaBox ( {
118+ await getSchemaBox ( {
118119 type : "object" ,
119120 properties : {
120121 enumprop : { type : "string" , enum : [ "aaa" , "bbb" , "ccc" ] } ,
121122 } ,
122123 } ) ,
123- ) . toMatchInlineSnapshot ( ' "export type _Test = Partial<{ enumprop: "aaa" | "bbb" | "ccc" }>;"' ) ;
124+ ) . toMatchInlineSnapshot ( ` "export type _Test = Partial<{ enumprop: "aaa" | "bbb" | "ccc" }>;"` ) ;
124125
125- expect ( getSchemaBox ( { type : "string" , enum : [ "aaa" , "bbb" , "ccc" ] } ) ) . toMatchInlineSnapshot (
126- ' "export type _Test = "aaa" | "bbb" | "ccc";"' ,
126+ expect ( await getSchemaBox ( { type : "string" , enum : [ "aaa" , "bbb" , "ccc" ] } ) ) . toMatchInlineSnapshot (
127+ ` "export type _Test = "aaa" | "bbb" | "ccc";"` ,
127128 ) ;
128129
129130 // StringENum
130- expect ( getSchemaBox ( { type : "string" , enum : [ "aaa" , "bbb" , "ccc" ] } ) ) . toMatchInlineSnapshot ( ' "export type _Test = "aaa" | "bbb" | "ccc";"' ) ;
131+ expect ( await getSchemaBox ( { type : "string" , enum : [ "aaa" , "bbb" , "ccc" ] } ) ) . toMatchInlineSnapshot ( ` "export type _Test = "aaa" | "bbb" | "ccc";"` ) ;
131132
132133 // ObjectWithUnion
133134 expect (
134- getSchemaBox ( {
135+ await getSchemaBox ( {
135136 type : "object" ,
136137 properties : {
137138 union : { oneOf : [ { type : "string" } , { type : "number" } ] } ,
138139 } ,
139140 } ) ,
140- ) . toMatchInlineSnapshot ( ' "export type _Test = Partial<{ union: string | number }>;"' ) ;
141- expect ( getSchemaBox ( { oneOf : [ { type : "string" } , { type : "number" } ] } ) ) . toMatchInlineSnapshot (
142- ' "export type _Test = string | number;"' ,
141+ ) . toMatchInlineSnapshot ( ` "export type _Test = Partial<{ union: string | number }>;"` ) ;
142+ expect ( await getSchemaBox ( { oneOf : [ { type : "string" } , { type : "number" } ] } ) ) . toMatchInlineSnapshot (
143+ ` "export type _Test = string | number;"` ,
143144 ) ;
144145
145146 // StringOrNumber
146- expect ( getSchemaBox ( { oneOf : [ { type : "string" } , { type : "number" } ] } ) ) . toMatchInlineSnapshot ( ' "export type _Test = string | number;"' ) ;
147+ expect ( await getSchemaBox ( { oneOf : [ { type : "string" } , { type : "number" } ] } ) ) . toMatchInlineSnapshot ( ` "export type _Test = string | number;"` ) ;
147148
148- expect ( getSchemaBox ( { allOf : [ { type : "string" } , { type : "number" } ] } ) ) . toMatchInlineSnapshot (
149- ' "export type _Test = string & number;"' ,
149+ expect ( await getSchemaBox ( { allOf : [ { type : "string" } , { type : "number" } ] } ) ) . toMatchInlineSnapshot (
150+ ` "export type _Test = string & number;"` ,
150151 ) ;
151152
152153 // StringAndNumber
153- expect ( getSchemaBox ( { allOf : [ { type : "string" } , { type : "number" } ] } ) ) . toMatchInlineSnapshot ( ' "export type _Test = string & number;"' ) ;
154+ expect ( await getSchemaBox ( { allOf : [ { type : "string" } , { type : "number" } ] } ) ) . toMatchInlineSnapshot ( ` "export type _Test = string & number;"` ) ;
154155
155- expect ( getSchemaBox ( { anyOf : [ { type : "string" } , { type : "number" } ] } ) ) . toMatchInlineSnapshot (
156- ' "export type _Test = string | number | Array<string | number>;"' ,
156+ expect ( await getSchemaBox ( { anyOf : [ { type : "string" } , { type : "number" } ] } ) ) . toMatchInlineSnapshot (
157+ ` "export type _Test = string | number | Array<string | number>;"` ,
157158 ) ;
158159
159160 // StringAndNumberMaybeMultiple
160- expect ( getSchemaBox ( { anyOf : [ { type : "string" } , { type : "number" } ] } ) ) . toMatchInlineSnapshot ( ' "export type _Test = string | number | Array<string | number>;"' ) ;
161+ expect ( await getSchemaBox ( { anyOf : [ { type : "string" } , { type : "number" } ] } ) ) . toMatchInlineSnapshot ( ` "export type _Test = string | number | Array<string | number>;"` ) ;
161162
162163 // ObjectWithArrayUnion
163164 expect (
164- getSchemaBox ( {
165+ await getSchemaBox ( {
165166 type : "object" ,
166167 properties : {
167168 unionOrArrayOfUnion : { anyOf : [ { type : "string" } , { type : "number" } ] } ,
168169 } ,
169170 } ) ,
170- ) . toMatchInlineSnapshot ( ' "export type _Test = Partial<{ unionOrArrayOfUnion: string | number | Array<string | number> }>;"' ) ;
171+ ) . toMatchInlineSnapshot ( ` "export type _Test = Partial<{ unionOrArrayOfUnion: string | number | Array<string | number> }>;"` ) ;
171172
172173 // ObjectWithIntersection
173174 expect (
174- getSchemaBox ( {
175+ await getSchemaBox ( {
175176 type : "object" ,
176177 properties : {
177178 intersection : { allOf : [ { type : "string" } , { type : "number" } ] } ,
178179 } ,
179180 } ) ,
180- ) . toMatchInlineSnapshot ( ' "export type _Test = Partial<{ intersection: string & number }>;"' ) ;
181+ ) . toMatchInlineSnapshot ( ` "export type _Test = Partial<{ intersection: string & number }>;"` ) ;
181182
182- expect ( getSchemaBox ( { type : "string" , enum : [ "aaa" , "bbb" , "ccc" ] } ) ) . toMatchInlineSnapshot (
183- ' "export type _Test = "aaa" | "bbb" | "ccc";"' ,
183+ expect ( await getSchemaBox ( { type : "string" , enum : [ "aaa" , "bbb" , "ccc" ] } ) ) . toMatchInlineSnapshot (
184+ ` "export type _Test = "aaa" | "bbb" | "ccc";"` ,
184185 ) ;
185- expect ( getSchemaBox ( { type : "number" , enum : [ 1 , 2 , 3 ] } ) ) . toMatchInlineSnapshot ( ' "export type _Test = 1 | 2 | 3;"' ) ;
186+ expect ( await getSchemaBox ( { type : "number" , enum : [ 1 , 2 , 3 ] } ) ) . toMatchInlineSnapshot ( ` "export type _Test = 1 | 2 | 3;"` ) ;
186187} ) ;
187188
188189describe ( "getSchemaBox with context" , ( ) => {
0 commit comments