@@ -22,36 +22,48 @@ async function loadLocalFile(file: string): Promise<[string, any]> {
2222 file = path . resolve ( file )
2323 const raw = fs . readFileSync ( file , { encoding : "utf-8" } )
2424
25- let result : any | undefined
25+ return [ file , await parseFile ( raw , file ) ]
26+ }
27+
28+ async function loadRemoteFile ( uri : string ) : Promise < [ string , any ] > {
29+ const res = await fetch ( uri )
30+
31+ if ( ! res . ok ) {
32+ throw new Error ( `failed to fetch remote file '${ uri } '` )
33+ }
34+
35+ const raw = await res . text ( )
2636
27- if ( file . endsWith ( ".json" ) ) {
37+ return [ uri , await parseFile ( raw , uri ) ]
38+ }
39+
40+ async function parseFile ( raw : string , filepath : string ) : Promise < unknown > {
41+ let result : unknown | undefined
42+
43+ // TODO: sniff format from raw text
44+ if ( filepath . endsWith ( ".json" ) ) {
2845 try {
2946 result = JSON . parse ( raw )
3047 } catch ( err : any ) {
3148 logger . error ( "error parsing json" , err . stack )
32- throw new Error ( `failed to parse json from file '${ file } '` )
49+ throw new Error ( `failed to parse json from '${ filepath } '` )
3350 }
3451 }
3552
36- if ( file . endsWith ( ".yaml" ) || file . endsWith ( ".yml" ) ) {
53+ if ( filepath . endsWith ( ".yaml" ) || filepath . endsWith ( ".yml" ) ) {
3754 try {
3855 result = yaml . load ( raw )
3956 } catch ( err : any ) {
4057 logger . error ( "error parsing yaml" , err . stack )
41- throw new Error ( `failed to parse yaml from file '${ file } '` )
58+ throw new Error ( `failed to parse yaml from '${ filepath } '` )
4259 }
4360 }
4461
4562 if ( ! result ) {
46- throw new Error ( `failed to load file '${ file } '` )
63+ throw new Error ( `failed to parse '${ filepath } '` )
4764 }
4865
49- return [ file , result ]
50- }
51-
52- async function loadRemoteFile ( uri : string ) : Promise < [ string , any ] > {
53- // todo: https://github.com/mnahkies/openapi-code-generator/issues/43
54- throw new Error ( `could not load ${ uri } - not implemented,` )
66+ return result
5567}
5668
5769export function isRemote ( location : string ) : boolean {
0 commit comments