@@ -14,22 +14,9 @@ interface SchemaMap {
14
14
const EXT_RE = / \. ( y a m l | y m l | j s o n ) # ? \/ ? / i;
15
15
export const VIRTUAL_JSON_URL = `file:///_json` ; // fake URL reserved for dynamic JSON
16
16
17
- function parseYAML ( schema : string ) {
18
- try {
19
- return yaml . load ( schema ) ;
20
- } catch ( err ) {
21
- error ( `YAML: ${ String ( err ) } ` ) ;
22
- process . exit ( 1 ) ;
23
- }
24
- }
25
-
26
- function parseJSON ( schema : string ) {
27
- try {
28
- return JSON . parse ( schema ) ;
29
- } catch ( err ) {
30
- error ( `JSON: ${ String ( err ) } ` ) ;
31
- process . exit ( 1 ) ;
32
- }
17
+ /** parse OpenAPI schema s YAML or JSON */
18
+ function parseSchema ( source : string ) {
19
+ return source . trim ( ) . startsWith ( "{" ) ? JSON . parse ( source ) : yaml . load ( source ) ;
33
20
}
34
21
35
22
export function resolveSchema ( filename : string ) : URL {
@@ -108,8 +95,6 @@ export default async function load(schema: URL | Subschema | Readable, options:
108
95
}
109
96
options . urlCache . add ( schemaID ) ;
110
97
111
- const ext = path . extname ( schema . pathname ) . toLowerCase ( ) ;
112
-
113
98
// remote
114
99
if ( schema . protocol . startsWith ( "http" ) ) {
115
100
const headers : Record < string , string > = { "User-Agent" : "openapi-typescript" } ;
@@ -126,33 +111,13 @@ export default async function load(schema: URL | Subschema | Readable, options:
126
111
method : ( options . httpMethod as Dispatcher . HttpMethod ) || "GET" ,
127
112
headers,
128
113
} ) ;
129
- const contentType = res . headers . get ( "content-type" ) ;
130
- if ( ext === ".json" || contentType ?. includes ( "json" ) ) {
131
- options . schemas [ schemaID ] = {
132
- hint,
133
- schema : parseJSON ( await res . text ( ) ) ,
134
- } ;
135
- } else if ( ext === ".yaml" || ext === ".yml" || contentType ?. includes ( "yaml" ) ) {
136
- options . schemas [ schemaID ] = {
137
- hint,
138
- schema : parseYAML ( await res . text ( ) ) as any , // eslint-disable-line @typescript-eslint/no-explicit-any
139
- } ;
140
- }
114
+ const contents = await res . text ( ) ;
115
+ options . schemas [ schemaID ] = { hint, schema : parseSchema ( contents ) } ;
141
116
}
142
117
// local file
143
118
else {
144
119
const contents = fs . readFileSync ( schema , "utf8" ) ;
145
- if ( ext === ".yaml" || ext === ".yml" ) {
146
- options . schemas [ schemaID ] = {
147
- hint,
148
- schema : parseYAML ( contents ) as any , // eslint-disable-line @typescript-eslint/no-explicit-any
149
- } ;
150
- } else if ( ext === ".json" ) {
151
- options . schemas [ schemaID ] = {
152
- hint,
153
- schema : parseJSON ( contents ) ,
154
- } ;
155
- }
120
+ options . schemas [ schemaID ] = { hint, schema : parseSchema ( contents ) } ;
156
121
}
157
122
}
158
123
// 1b. Readable stream
@@ -169,11 +134,7 @@ export default async function load(schema: URL | Subschema | Readable, options:
169
134
resolve ( content . trim ( ) ) ;
170
135
} ) ;
171
136
} ) ;
172
- // if file starts with '{' assume JSON
173
- options . schemas [ schemaID ] = {
174
- hint : "OpenAPI3" ,
175
- schema : contents . startsWith ( "{" ) ? parseJSON ( contents ) : parseYAML ( contents ) ,
176
- } ;
137
+ options . schemas [ schemaID ] = { hint : "OpenAPI3" , schema : parseSchema ( contents ) } ;
177
138
}
178
139
// 1c. inline
179
140
else if ( typeof schema === "object" ) {
0 commit comments