diff --git a/src/utils/readSpec.ts b/src/utils/readSpec.ts index 016bf929e..6faa4b22b 100644 --- a/src/utils/readSpec.ts +++ b/src/utils/readSpec.ts @@ -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 => { 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); }; diff --git a/src/utils/readSpecFromHttp.ts b/src/utils/readSpecFromHttp.ts deleted file mode 100644 index 2c33728cf..000000000 --- a/src/utils/readSpecFromHttp.ts +++ /dev/null @@ -1,21 +0,0 @@ -import { get } from 'http'; - -/** - * Download the spec file from a HTTP resource - * @param url - */ -export const readSpecFromHttp = async (url: string): Promise => - new Promise((resolve, reject) => { - get(url, response => { - let body = ''; - response.on('data', chunk => { - body += chunk; - }); - response.on('end', () => { - resolve(body); - }); - response.on('error', () => { - reject(`Could not read OpenApi spec: "${url}"`); - }); - }); - }); diff --git a/src/utils/readSpecFromHttps.ts b/src/utils/readSpecFromHttps.ts deleted file mode 100644 index 984bd966d..000000000 --- a/src/utils/readSpecFromHttps.ts +++ /dev/null @@ -1,21 +0,0 @@ -import { get } from 'https'; - -/** - * Download the spec file from a HTTPS resource - * @param url - */ -export const readSpecFromHttps = async (url: string): Promise => - new Promise((resolve, reject) => { - get(url, response => { - let body = ''; - response.on('data', chunk => { - body += chunk; - }); - response.on('end', () => { - resolve(body); - }); - response.on('error', () => { - reject(`Could not read OpenApi spec: "${url}"`); - }); - }); - }); diff --git a/src/utils/readSpecFromUrl.ts b/src/utils/readSpecFromUrl.ts new file mode 100644 index 000000000..533af2a93 --- /dev/null +++ b/src/utils/readSpecFromUrl.ts @@ -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 => + new Promise((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); + } + });