Skip to content
This repository has been archived by the owner on Nov 11, 2023. It is now read-only.

Commit

Permalink
Support pulling spec from URL (#169)
Browse files Browse the repository at this point in the history
* Support importing from url

* Update readme

* Fix typo

* Remove .gitignore changes
  • Loading branch information
qw-in authored and Tejas Kumar committed Nov 18, 2019
1 parent 0ce7bb6 commit 225318b
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 4 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ As an abstraction, this tool allows for greater consistency and maintainability
- [Code Generation](#code-generation)
- [Usage](#usage)
- [Validation of the OpenAPI specification](#validation-of-the-openapi-specification)
- [Import from URL](#import-from-url)
- [Import from GitHub](#import-from-github)
- [Transforming an Original Spec](#transforming-an-original-spec)
- [Advanced configuration](#advanced-configuration)
Expand Down Expand Up @@ -536,6 +537,12 @@ To enforce the best quality as possible of specification, we have integrated the

To activate this, add a `--validation` flag to your `restful-react` call.

#### Import from URL

Adding the `--url` flag to `restful-react import` instead of using the `--file` flag will attempt to fetch the spec from that endpoint.

- `restful-react import --url https://api.mine.com/openapi.json --output my-awesome-generated-types.tsx`

#### Import from GitHub

Adding the `--github` flag to `restful-react import` instead of using the `--file` flag allows us to **create React components from an OpenAPI spec _remotely hosted on GitHub._** <sup>_(how is this real life_ 🔥 _)_</sup>
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"ci": "[ ! -z $DANGER_GITHUB_API_TOKEN ] && yarn danger ci || echo \"Skipping Danger for External Contributor\"",
"examples": "run-p example:*",
"example:github": "node lib/bin/restful-react.js import --github OAI:OpenAPI-Specification:master:examples/v3.0/petstore.yaml --output examples/petstoreFromGithubSpec.tsx",
"example:url": "node lib/bin/restful-react.js import --url https://petstore.swagger.io/v2/swagger.json --output examples/petstoreFromUrlSpec.tsx",
"example:file": "node lib/bin/restful-react.js import --file examples/petstore.yaml --output examples/petstoreFromFileSpec.tsx",
"example:advanced": "node lib/bin/restful-react.js import --config examples/restful-react.config.js"
},
Expand Down Expand Up @@ -107,4 +108,4 @@
"peerDependencies": {
"react": ">=16.8.5"
}
}
}
46 changes: 43 additions & 3 deletions src/bin/restful-react-import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const log = console.log; // tslint:disable-line:no-console
export interface Options {
output: string;
file?: string;
url?: string;
github?: string;
transformer?: string;
validation?: boolean;
Expand All @@ -31,6 +32,7 @@ export interface ExternalConfigFile {

program.option("-o, --output [value]", "output file destination");
program.option("-f, --file [value]", "input file (yaml or json openapi specs)");
program.option("-u, --url [value]", "url to spec (yaml or json openapi specs)");
program.option("-g, --github [value]", "github path (format: `owner:repo:branch:path`)");
program.option("-t, --transformer [value]", "transformer function path");
program.option("--validation", "add the validation step (provided by ibm-openapi-validator)");
Expand All @@ -49,8 +51,8 @@ const successWithoutOutputMessage = chalk.yellow("Success! No output path specif
const importSpecs = async (options: AdvancedOptions) => {
const transformer = options.transformer ? require(join(process.cwd(), options.transformer)) : undefined;

if (!options.file && !options.github) {
throw new Error("You need to provide an input specification with `--file` or `--github`");
if (!options.file && !options.url && !options.github) {
throw new Error("You need to provide an input specification with `--file`, '--url', or `--github`");
}

if (options.file) {
Expand All @@ -66,6 +68,44 @@ const importSpecs = async (options: AdvancedOptions) => {
customImport: options.customImport,
customProps: options.customProps,
});
} else if (options.url) {
const { url } = options;

const urlSpecReq = {
method: "GET",
url,
headers: {
"user-agent": "restful-react-importer",
},
};

return new Promise((resolve, reject) => {
request(urlSpecReq, async (error, response, body) => {
if (error) {
return reject(error);
}

// Attempt to determine format
// will default to yaml as it
// also supports json fully
let format: 'json' | 'yaml' = 'yaml';
if (url.endsWith('.json') || response.headers['content-type'] === 'application/json') {
format = 'json';
}

resolve(
importOpenApi({
data: body,
format,
transformer,
validation: options.validation,
customImport: options.customImport,
customProps: options.customProps,
}),
);
});
});

} else if (options.github) {
const { github } = options;

Expand Down Expand Up @@ -153,7 +193,7 @@ const importSpecs = async (options: AdvancedOptions) => {
});
});
} else {
return Promise.reject("Please provide a file (--file) or a github (--github) input");
return Promise.reject("Please provide a file (--file), a url (--url), or a github (--github) input");
}
};

Expand Down

0 comments on commit 225318b

Please sign in to comment.