@@ -14,22 +14,9 @@ interface SchemaMap {
1414const EXT_RE = / \. ( y a m l | y m l | j s o n ) # ? \/ ? / i;
1515export const VIRTUAL_JSON_URL = `file:///_json` ; // fake URL reserved for dynamic JSON
1616
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 ) ;
3320}
3421
3522export function resolveSchema ( filename : string ) : URL {
@@ -108,8 +95,6 @@ export default async function load(schema: URL | Subschema | Readable, options:
10895 }
10996 options . urlCache . add ( schemaID ) ;
11097
111- const ext = path . extname ( schema . pathname ) . toLowerCase ( ) ;
112-
11398 // remote
11499 if ( schema . protocol . startsWith ( "http" ) ) {
115100 const headers : Record < string , string > = { "User-Agent" : "openapi-typescript" } ;
@@ -126,33 +111,13 @@ export default async function load(schema: URL | Subschema | Readable, options:
126111 method : ( options . httpMethod as Dispatcher . HttpMethod ) || "GET" ,
127112 headers,
128113 } ) ;
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 ) } ;
141116 }
142117 // local file
143118 else {
144119 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 ) } ;
156121 }
157122 }
158123 // 1b. Readable stream
@@ -169,11 +134,7 @@ export default async function load(schema: URL | Subschema | Readable, options:
169134 resolve ( content . trim ( ) ) ;
170135 } ) ;
171136 } ) ;
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 ) } ;
177138 }
178139 // 1c. inline
179140 else if ( typeof schema === "object" ) {
0 commit comments