Skip to content

Commit

Permalink
feat: combine reading spec from url
Browse files Browse the repository at this point in the history
  • Loading branch information
jordanshatford committed Mar 18, 2024
1 parent a701e81 commit 17edbb5
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 46 deletions.
7 changes: 3 additions & 4 deletions src/utils/readSpec.ts
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);
};
21 changes: 0 additions & 21 deletions src/utils/readSpecFromHttp.ts

This file was deleted.

21 changes: 0 additions & 21 deletions src/utils/readSpecFromHttps.ts

This file was deleted.

28 changes: 28 additions & 0 deletions src/utils/readSpecFromUrl.ts
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);
}
});

0 comments on commit 17edbb5

Please sign in to comment.