- Added option to download items

This commit is contained in:
Ferdi Koomen 2020-06-16 21:54:55 +02:00
parent f18754a09c
commit f31044b229
7 changed files with 108 additions and 24 deletions

View File

@ -50,7 +50,15 @@ export default {
file: './dist/index.js',
format: 'cjs',
},
external: ['fs', 'os', 'util', 'handlebars/runtime', ...external],
external: [
'fs',
'os',
'util',
'http',
'https',
'handlebars/runtime',
...external,
],
plugins: [
handlebarsPlugin(),
typescript({

View File

@ -20,4 +20,20 @@ describe('index', () => {
write: false,
});
});
it('downloads and parses v2 without issues', async () => {
await OpenAPI.generate({
input: 'https://raw.githubusercontent.com/ferdikoomen/openapi-typescript-codegen/master/test/mock/v2/spec.json',
output: './test/result/v22/',
write: false,
});
});
it('downloads and parses v3 without issues', async () => {
await OpenAPI.generate({
input: 'https://raw.githubusercontent.com/ferdikoomen/openapi-typescript-codegen/master/test/mock/v3/spec.json',
output: './test/result/v33/',
write: false,
});
});
});

View File

@ -1,24 +1,7 @@
import * as yaml from 'js-yaml';
import * as path from 'path';
import { exists, readFile } from './fileSystem';
/**
* Check if given file exists and try to read the content as string.
* @param filePath
*/
async function read(filePath: string): Promise<string> {
const fileExists = await exists(filePath);
if (fileExists) {
try {
const content = await readFile(filePath, 'utf8');
return content.toString();
} catch (e) {
throw new Error(`Could not read OpenApi spec: "${filePath}"`);
}
}
throw new Error(`Could not find OpenApi spec: "${filePath}"`);
}
import { readSpec } from './readSpec';
/**
* 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> {
* @param input
*/
export async function getOpenApiSpec(input: string): Promise<any> {
const file = path.resolve(process.cwd(), input);
const extname = path.extname(file).toLowerCase();
const content = await read(file);
const extname = path.extname(input).toLowerCase();
const content = await readSpec(input);
switch (extname) {
case '.yml':
case '.yaml':
try {
return yaml.safeLoad(content);
} catch (e) {
throw new Error(`Could not parse OpenApi YAML: "${file}"`);
throw new Error(`Could not parse OpenApi YAML: "${input}"`);
}
default:
try {
return JSON.parse(content);
} catch (e) {
throw new Error(`Could not parse OpenApi JSON: "${file}"`);
throw new Error(`Could not parse OpenApi JSON: "${input}"`);
}
}
}

13
src/utils/readSpec.ts Normal file
View File

@ -0,0 +1,13 @@
import { readSpecFromDisk } from './readSpecFromDisk';
import { readSpecFromHttp } from './readSpecFromHttp';
import { readSpecFromHttps } from './readSpecFromHttps';
export async function readSpec(input: string): Promise<string> {
if (input.startsWith('https://')) {
return await readSpecFromHttps(input);
}
if (input.startsWith('http://')) {
return await readSpecFromHttp(input);
}
return await readSpecFromDisk(input);
}

View File

@ -0,0 +1,21 @@
import path from 'path';
import { exists, readFile } from './fileSystem';
/**
* Check if given file exists and try to read the content as string.
* @param input
*/
export async function readSpecFromDisk(input: string): Promise<string> {
const filePath = path.resolve(process.cwd(), input);
const fileExists = await exists(filePath);
if (fileExists) {
try {
const content = await readFile(filePath, 'utf8');
return content.toString();
} catch (e) {
throw new Error(`Could not read OpenApi spec: "${filePath}"`);
}
}
throw new Error(`Could not find OpenApi spec: "${filePath}"`);
}

View File

@ -0,0 +1,22 @@
import http from 'http';
/**
* Download the spec file from a HTTP resource
* @param url
*/
export async function readSpecFromHttp(url: string): Promise<string> {
return new Promise<string>((resolve, reject) => {
http.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}"`);
});
});
});
}

View File

@ -0,0 +1,22 @@
import https from 'https';
/**
* Download the spec file from a HTTPS resource
* @param url
*/
export async function readSpecFromHttps(url: string): Promise<string> {
return new Promise<string>((resolve, reject) => {
https.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}"`);
});
});
});
}