Skip to content

Commit

Permalink
feat: Support OpenAPI specs from URL (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
bennycode committed May 26, 2019
1 parent ab5fa9a commit d7581c0
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Display the CLI options by running
swaxios --help
```

If you pass an [OpenAPI definition](https://swagger.io/docs/specification/2-0/basic-structure/) file (v2.0; valid JSON or YAML) to Swaxios, then it will generate you an API client that uses axios under the hood and is written in TypeScript.
If you pass an [OpenAPI Specification (OAS)](https://swagger.io/docs/specification/2-0/basic-structure/) (v2.0; JSON or YAML) to Swaxios, then it will generate you an API client that uses axios under the hood and is written in TypeScript.

Examples:

Expand Down Expand Up @@ -44,4 +44,4 @@ client.rest.identityProvidersService
});
```

Inspired by [swagger-codegen](https://github.com/swagger-api/swagger-codegen).
This project is inspired by [swagger-codegen](https://github.com/swagger-api/swagger-codegen).
11 changes: 10 additions & 1 deletion src/Swaxios.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import axios from 'axios';
import fs from 'fs-extra';
import initializeHelpers from 'handlebars-helpers';
import path from 'path';
Expand Down Expand Up @@ -63,9 +64,17 @@ async function generateClient(swaggerJson: Spec, outputDirectory: string): Promi
await buildIndexFiles(fileIndex);
}

async function readInputURL(inputURL: string): Promise<Spec> {
console.info(`Reading OpenAPI specification from URL "${inputURL}" ...`);
const response = await axios.get<Spec>(inputURL);
return response.data;
}

async function readInputFile(inputFile: string): Promise<Spec> {
let swaggerJson: Spec;

console.info(`Reading OpenAPI specification from file "${inputFile}" ...`);

try {
await fs.access(inputFile);
} catch (error) {
Expand All @@ -89,7 +98,7 @@ async function readInputFile(inputFile: string): Promise<Spec> {
}

export async function writeClient(inputFile: string, outputDirectory: string): Promise<void> {
const swaggerJson = await readInputFile(inputFile);
const swaggerJson = inputFile.startsWith('http:') ? await readInputURL(inputFile) : await readInputFile(inputFile);
await validateConfig(swaggerJson);
return generateClient(swaggerJson, outputDirectory);
}
6 changes: 3 additions & 3 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ program
.name(binName)
.description(description)
.version(version, '-v, --version')
.option('-i, --input <file>', 'Set the input file (required)')
.option('-o, --output <directory>', 'Set the output directory (required)')
.option('-i, --input <file>', 'File path (or URL) to OpenAPI Specification, i.e. swagger.json (required)')
.option('-o, --output <directory>', 'Path to output directory for generated TypeScript code (required)')
.parse(process.argv);

if (!program.input || !program.output) {
program.outputHelp();
process.exit(1);
}

const inputFile = path.resolve(process.cwd(), program.input);
const inputFile = program.input.startsWith('http:') ? program.input : path.resolve(process.cwd(), program.input);
const outputDirectory = path.resolve(process.cwd(), program.output);

writeClient(inputFile, outputDirectory)
Expand Down

0 comments on commit d7581c0

Please sign in to comment.