From f31044b229ea55a550a7e875e554900dbebff4e6 Mon Sep 17 00:00:00 2001 From: Ferdi Koomen Date: Tue, 16 Jun 2020 21:54:55 +0200 Subject: [PATCH] - Added option to download items --- rollup.config.js | 10 +++++++++- src/index.spec.ts | 16 ++++++++++++++++ src/utils/getOpenApiSpec.ts | 28 +++++----------------------- src/utils/readSpec.ts | 13 +++++++++++++ src/utils/readSpecFromDisk.ts | 21 +++++++++++++++++++++ src/utils/readSpecFromHttp.ts | 22 ++++++++++++++++++++++ src/utils/readSpecFromHttps.ts | 22 ++++++++++++++++++++++ 7 files changed, 108 insertions(+), 24 deletions(-) create mode 100644 src/utils/readSpec.ts create mode 100644 src/utils/readSpecFromDisk.ts create mode 100644 src/utils/readSpecFromHttp.ts create mode 100644 src/utils/readSpecFromHttps.ts diff --git a/rollup.config.js b/rollup.config.js index 2edc665c..d6050c46 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -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({ diff --git a/src/index.spec.ts b/src/index.spec.ts index 0a3fbae7..224e660f 100644 --- a/src/index.spec.ts +++ b/src/index.spec.ts @@ -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, + }); + }); }); diff --git a/src/utils/getOpenApiSpec.ts b/src/utils/getOpenApiSpec.ts index 5936fbd4..fa32c665 100644 --- a/src/utils/getOpenApiSpec.ts +++ b/src/utils/getOpenApiSpec.ts @@ -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 { - 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 { * @param input */ export async function getOpenApiSpec(input: string): Promise { - 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}"`); } } } diff --git a/src/utils/readSpec.ts b/src/utils/readSpec.ts new file mode 100644 index 00000000..ade19525 --- /dev/null +++ b/src/utils/readSpec.ts @@ -0,0 +1,13 @@ +import { readSpecFromDisk } from './readSpecFromDisk'; +import { readSpecFromHttp } from './readSpecFromHttp'; +import { readSpecFromHttps } from './readSpecFromHttps'; + +export async function readSpec(input: string): Promise { + if (input.startsWith('https://')) { + return await readSpecFromHttps(input); + } + if (input.startsWith('http://')) { + return await readSpecFromHttp(input); + } + return await readSpecFromDisk(input); +} diff --git a/src/utils/readSpecFromDisk.ts b/src/utils/readSpecFromDisk.ts new file mode 100644 index 00000000..3ee415f7 --- /dev/null +++ b/src/utils/readSpecFromDisk.ts @@ -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 { + 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}"`); +} diff --git a/src/utils/readSpecFromHttp.ts b/src/utils/readSpecFromHttp.ts new file mode 100644 index 00000000..cf02197f --- /dev/null +++ b/src/utils/readSpecFromHttp.ts @@ -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 { + return new Promise((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}"`); + }); + }); + }); +} diff --git a/src/utils/readSpecFromHttps.ts b/src/utils/readSpecFromHttps.ts new file mode 100644 index 00000000..a1b1c737 --- /dev/null +++ b/src/utils/readSpecFromHttps.ts @@ -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 { + return new Promise((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}"`); + }); + }); + }); +}