Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: combine reading spec from url #81

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
});
Loading