From 87460e976ff8c86b0a5acefd679a085b07991b0c Mon Sep 17 00:00:00 2001 From: Quinn Blenkinsop Date: Wed, 6 Nov 2019 15:02:43 -0800 Subject: [PATCH 1/4] Support importing from url --- .gitignore | 1 + package.json | 3 ++- src/bin/restful-react-import.ts | 46 ++++++++++++++++++++++++++++++--- 3 files changed, 46 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index 07d22757..6de25426 100644 --- a/.gitignore +++ b/.gitignore @@ -105,4 +105,5 @@ examples/*.tsx # Files managed by operational-scripts tslint.json +tsconfig.json .prettierrc diff --git a/package.json b/package.json index 8d57f549..a692b3f7 100644 --- a/package.json +++ b/package.json @@ -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" }, @@ -107,4 +108,4 @@ "peerDependencies": { "react": ">=16.8.5" } -} +} \ No newline at end of file diff --git a/src/bin/restful-react-import.ts b/src/bin/restful-react-import.ts index 61424734..73b2d0a4 100644 --- a/src/bin/restful-react-import.ts +++ b/src/bin/restful-react-import.ts @@ -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; @@ -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)"); @@ -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) { @@ -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 support 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; @@ -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"); } }; From 7a14d2ec6b7effb0b1a1a7eebda5d185a70f9a5b Mon Sep 17 00:00:00 2001 From: Quinn Blenkinsop Date: Wed, 6 Nov 2019 15:08:33 -0800 Subject: [PATCH 2/4] Update readme --- README.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/README.md b/README.md index fb1c7069..1d3f7316 100644 --- a/README.md +++ b/README.md @@ -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) @@ -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._** _(how is this real life_ 🔥 _)_ From 7e968b01f71dd91b2171ce4c94942ae6b3152f91 Mon Sep 17 00:00:00 2001 From: Quinn Blenkinsop Date: Wed, 6 Nov 2019 16:17:04 -0800 Subject: [PATCH 3/4] Fix typo --- src/bin/restful-react-import.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/bin/restful-react-import.ts b/src/bin/restful-react-import.ts index 73b2d0a4..55dfd587 100644 --- a/src/bin/restful-react-import.ts +++ b/src/bin/restful-react-import.ts @@ -87,7 +87,7 @@ const importSpecs = async (options: AdvancedOptions) => { // Attempt to determine format // will default to yaml as it - // also support json fully + // also supports json fully let format: 'json' | 'yaml' = 'yaml'; if (url.endsWith('.json') || response.headers['content-type'] === 'application/json') { format = 'json'; From 459a0bc67169f1e1477be00cc9b1bfe9a0ab536f Mon Sep 17 00:00:00 2001 From: Quinn Blenkinsop Date: Thu, 14 Nov 2019 10:24:11 -0800 Subject: [PATCH 4/4] Remove .gitignore changes --- .gitignore | 1 - 1 file changed, 1 deletion(-) diff --git a/.gitignore b/.gitignore index 6de25426..07d22757 100644 --- a/.gitignore +++ b/.gitignore @@ -105,5 +105,4 @@ examples/*.tsx # Files managed by operational-scripts tslint.json -tsconfig.json .prettierrc