Skip to content

Commit f31044b

Browse files
committed
- Added option to download items
1 parent f18754a commit f31044b

File tree

7 files changed

+108
-24
lines changed

7 files changed

+108
-24
lines changed

rollup.config.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,15 @@ export default {
5050
file: './dist/index.js',
5151
format: 'cjs',
5252
},
53-
external: ['fs', 'os', 'util', 'handlebars/runtime', ...external],
53+
external: [
54+
'fs',
55+
'os',
56+
'util',
57+
'http',
58+
'https',
59+
'handlebars/runtime',
60+
...external,
61+
],
5462
plugins: [
5563
handlebarsPlugin(),
5664
typescript({

src/index.spec.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,20 @@ describe('index', () => {
2020
write: false,
2121
});
2222
});
23+
24+
it('downloads and parses v2 without issues', async () => {
25+
await OpenAPI.generate({
26+
input: 'https://raw.githubusercontent.com/ferdikoomen/openapi-typescript-codegen/master/test/mock/v2/spec.json',
27+
output: './test/result/v22/',
28+
write: false,
29+
});
30+
});
31+
32+
it('downloads and parses v3 without issues', async () => {
33+
await OpenAPI.generate({
34+
input: 'https://raw.githubusercontent.com/ferdikoomen/openapi-typescript-codegen/master/test/mock/v3/spec.json',
35+
output: './test/result/v33/',
36+
write: false,
37+
});
38+
});
2339
});

src/utils/getOpenApiSpec.ts

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,7 @@
11
import * as yaml from 'js-yaml';
22
import * as path from 'path';
33

4-
import { exists, readFile } from './fileSystem';
5-
6-
/**
7-
* Check if given file exists and try to read the content as string.
8-
* @param filePath
9-
*/
10-
async function read(filePath: string): Promise<string> {
11-
const fileExists = await exists(filePath);
12-
if (fileExists) {
13-
try {
14-
const content = await readFile(filePath, 'utf8');
15-
return content.toString();
16-
} catch (e) {
17-
throw new Error(`Could not read OpenApi spec: "${filePath}"`);
18-
}
19-
}
20-
throw new Error(`Could not find OpenApi spec: "${filePath}"`);
21-
}
4+
import { readSpec } from './readSpec';
225

236
/**
247
* Load and parse te open api spec. If the file extension is ".yml" or ".yaml"
@@ -27,23 +10,22 @@ async function read(filePath: string): Promise<string> {
2710
* @param input
2811
*/
2912
export async function getOpenApiSpec(input: string): Promise<any> {
30-
const file = path.resolve(process.cwd(), input);
31-
const extname = path.extname(file).toLowerCase();
32-
const content = await read(file);
13+
const extname = path.extname(input).toLowerCase();
14+
const content = await readSpec(input);
3315
switch (extname) {
3416
case '.yml':
3517
case '.yaml':
3618
try {
3719
return yaml.safeLoad(content);
3820
} catch (e) {
39-
throw new Error(`Could not parse OpenApi YAML: "${file}"`);
21+
throw new Error(`Could not parse OpenApi YAML: "${input}"`);
4022
}
4123

4224
default:
4325
try {
4426
return JSON.parse(content);
4527
} catch (e) {
46-
throw new Error(`Could not parse OpenApi JSON: "${file}"`);
28+
throw new Error(`Could not parse OpenApi JSON: "${input}"`);
4729
}
4830
}
4931
}

src/utils/readSpec.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { readSpecFromDisk } from './readSpecFromDisk';
2+
import { readSpecFromHttp } from './readSpecFromHttp';
3+
import { readSpecFromHttps } from './readSpecFromHttps';
4+
5+
export async function readSpec(input: string): Promise<string> {
6+
if (input.startsWith('https://')) {
7+
return await readSpecFromHttps(input);
8+
}
9+
if (input.startsWith('http://')) {
10+
return await readSpecFromHttp(input);
11+
}
12+
return await readSpecFromDisk(input);
13+
}

src/utils/readSpecFromDisk.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import path from 'path';
2+
3+
import { exists, readFile } from './fileSystem';
4+
5+
/**
6+
* Check if given file exists and try to read the content as string.
7+
* @param input
8+
*/
9+
export async function readSpecFromDisk(input: string): Promise<string> {
10+
const filePath = path.resolve(process.cwd(), input);
11+
const fileExists = await exists(filePath);
12+
if (fileExists) {
13+
try {
14+
const content = await readFile(filePath, 'utf8');
15+
return content.toString();
16+
} catch (e) {
17+
throw new Error(`Could not read OpenApi spec: "${filePath}"`);
18+
}
19+
}
20+
throw new Error(`Could not find OpenApi spec: "${filePath}"`);
21+
}

src/utils/readSpecFromHttp.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import http from 'http';
2+
3+
/**
4+
* Download the spec file from a HTTP resource
5+
* @param url
6+
*/
7+
export async function readSpecFromHttp(url: string): Promise<string> {
8+
return new Promise<string>((resolve, reject) => {
9+
http.get(url, response => {
10+
let body = '';
11+
response.on('data', chunk => {
12+
body += chunk;
13+
});
14+
response.on('end', () => {
15+
resolve(body);
16+
});
17+
response.on('error', () => {
18+
reject(`Could not read OpenApi spec: "${url}"`);
19+
});
20+
});
21+
});
22+
}

src/utils/readSpecFromHttps.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import https from 'https';
2+
3+
/**
4+
* Download the spec file from a HTTPS resource
5+
* @param url
6+
*/
7+
export async function readSpecFromHttps(url: string): Promise<string> {
8+
return new Promise<string>((resolve, reject) => {
9+
https.get(url, response => {
10+
let body = '';
11+
response.on('data', chunk => {
12+
body += chunk;
13+
});
14+
response.on('end', () => {
15+
resolve(body);
16+
});
17+
response.on('error', () => {
18+
reject(`Could not read OpenApi spec: "${url}"`);
19+
});
20+
});
21+
});
22+
}

0 commit comments

Comments
 (0)