mirror of
https://github.com/ferdikoomen/openapi-typescript-codegen.git
synced 2025-12-08 20:16:21 +00:00
- Added option to download items
This commit is contained in:
parent
f18754a09c
commit
f31044b229
@ -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({
|
||||
|
||||
@ -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,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@ -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
13
src/utils/readSpec.ts
Normal 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);
|
||||
}
|
||||
21
src/utils/readSpecFromDisk.ts
Normal file
21
src/utils/readSpecFromDisk.ts
Normal 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}"`);
|
||||
}
|
||||
22
src/utils/readSpecFromHttp.ts
Normal file
22
src/utils/readSpecFromHttp.ts
Normal 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}"`);
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
22
src/utils/readSpecFromHttps.ts
Normal file
22
src/utils/readSpecFromHttps.ts
Normal 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}"`);
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user