-
-
Notifications
You must be signed in to change notification settings - Fork 130
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a701e81
commit 17edbb5
Showing
4 changed files
with
31 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,12 @@ | ||
import { readSpecFromDisk } from './readSpecFromDisk'; | ||
import { readSpecFromHttp } from './readSpecFromHttp'; | ||
import { readSpecFromHttps } from './readSpecFromHttps'; | ||
import { readSpecFromUrl } from './readSpecFromUrl'; | ||
|
||
export const readSpec = async (input: string): Promise<string> => { | ||
if (input.startsWith('https://')) { | ||
return await readSpecFromHttps(input); | ||
return await readSpecFromUrl(input, 'https'); | ||
} | ||
if (input.startsWith('http://')) { | ||
return await readSpecFromHttp(input); | ||
return await readSpecFromUrl(input, 'http'); | ||
} | ||
return await readSpecFromDisk(input); | ||
}; |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import http from 'http'; | ||
import https from 'https'; | ||
|
||
/** | ||
* Download the spec file from a url resource | ||
* @param url | ||
* @param protocol | ||
*/ | ||
export const readSpecFromUrl = async (url: string, protocol: 'http' | 'https'): Promise<string> => | ||
new Promise<string>((resolve, reject) => { | ||
const cb = (response: http.IncomingMessage) => { | ||
let body = ''; | ||
response.on('data', chunk => { | ||
body += chunk; | ||
}); | ||
response.on('end', () => { | ||
resolve(body); | ||
}); | ||
response.on('error', () => { | ||
reject(`Could not read OpenApi spec: "${url}"`); | ||
}); | ||
}; | ||
if (protocol === 'http') { | ||
http.get(url, cb); | ||
} else if (protocol === 'https') { | ||
https.get(url, cb); | ||
} | ||
}); |