From 55bd704d5a9202a06c67fcf77680f28fdfa96ef2 Mon Sep 17 00:00:00 2001 From: Jon Ursenbach Date: Mon, 19 Dec 2022 12:17:57 -0800 Subject: [PATCH 1/9] feat(openapi): exposing our spec conversion tooling to a new `:convert` command --- README.md | 15 ++++ __tests__/__snapshots__/index.test.ts.snap | 3 + __tests__/cmds/openapi/convert.test.ts | 81 +++++++++++++++++ .../lib/__snapshots__/commands.test.ts.snap | 5 ++ src/cmds/index.ts | 2 + src/cmds/openapi/convert.ts | 90 +++++++++++++++++++ src/lib/logger.ts | 12 ++- src/lib/prepareOas.ts | 14 ++- 8 files changed, 216 insertions(+), 6 deletions(-) create mode 100644 __tests__/cmds/openapi/convert.test.ts create mode 100644 src/cmds/openapi/convert.ts diff --git a/README.md b/README.md index 96d9e6d35..6f3fb22c5 100644 --- a/README.md +++ b/README.md @@ -145,6 +145,7 @@ With `rdme`, you have access to a variety of tools to manage your API definition - [Syncing](#syncing-an-api-definition-to-readme) 🦉 - [Validation](#validating-an-api-definition) ✅ +- [Conversion](#converting-an-api-definition) ⏩ - [Reduction](#reducing-an-api-definition) 📉 - [Inspection](#inspecting-an-api-definition) 🔍 @@ -250,6 +251,20 @@ rdme openapi:validate [url-or-local-path-to-file] Similar to the `openapi` command, you can also [omit the file path](#omitting-the-file-path). +#### Converting an API definition + +You can also convert any Swagger or Postman Collection to an OpenAPI 3.0 definition. + +```sh +rdme openapi:convert [url-or-local-path-to-file] +``` + +Similar to the `openapi` command, you can also [omit the file path](#omitting-the-file-path). + +> **Note:** +> +> All of our OpenAPI commands already do this conversion automatically, but incase you need to utilize this exclusive functionality outside of the context of those you can. + #### Reducing an API Definition We also offer a tool that allows you to reduce a large API definition down to a specific set of tags or paths (again, no ReadMe account required!). This can be useful if you're debugging a problematic schema somewhere, or if you have a file that is too big to maintain. diff --git a/__tests__/__snapshots__/index.test.ts.snap b/__tests__/__snapshots__/index.test.ts.snap index 524461b6b..6a75493cd 100644 --- a/__tests__/__snapshots__/index.test.ts.snap +++ b/__tests__/__snapshots__/index.test.ts.snap @@ -30,6 +30,7 @@ Options Related commands + $ rdme openapi:convert Convert a Swagger or Postman Collection to OpenAPI. $ rdme openapi:inspect Analyze an OpenAPI/Swagger definition for various OpenAPI and ReadMe feature usage. $ rdme openapi:reduce Reduce an OpenAPI definition into a smaller subset. @@ -67,6 +68,7 @@ Options Related commands + $ rdme openapi:convert Convert a Swagger or Postman Collection to OpenAPI. $ rdme openapi:inspect Analyze an OpenAPI/Swagger definition for various OpenAPI and ReadMe feature usage. $ rdme openapi:reduce Reduce an OpenAPI definition into a smaller subset. @@ -104,6 +106,7 @@ Options Related commands + $ rdme openapi:convert Convert a Swagger or Postman Collection to OpenAPI. $ rdme openapi:inspect Analyze an OpenAPI/Swagger definition for various OpenAPI and ReadMe feature usage. $ rdme openapi:reduce Reduce an OpenAPI definition into a smaller subset. diff --git a/__tests__/cmds/openapi/convert.test.ts b/__tests__/cmds/openapi/convert.test.ts new file mode 100644 index 000000000..93f6256ed --- /dev/null +++ b/__tests__/cmds/openapi/convert.test.ts @@ -0,0 +1,81 @@ +import fs from 'fs'; + +import prompts from 'prompts'; + +import OpenAPIConvertCommand from '../../../src/cmds/openapi/convert'; + +const convert = new OpenAPIConvertCommand(); + +const successfulConversion = () => 'Your converted API definition has been saved to output.json!'; + +const testWorkingDir = process.cwd(); + +describe('rdme openapi:convert', () => { + afterEach(() => { + process.chdir(testWorkingDir); + + jest.clearAllMocks(); + }); + + describe('converting', () => { + it.each([ + ['Swagger 2.0', 'json', '2.0'], + ['Swagger 2.0', 'yaml', '2.0'], + ])('should support reducing a %s definition (format: %s)', async (_, format, specVersion) => { + const spec = require.resolve(`@readme/oas-examples/${specVersion}/${format}/petstore-simple.${format}`); + + let reducedSpec; + fs.writeFileSync = jest.fn((fileName, data) => { + reducedSpec = JSON.parse(data as string); + }); + + prompts.inject(['output.json']); + + await expect( + convert.run({ + spec, + }) + ).resolves.toBe(successfulConversion()); + + expect(fs.writeFileSync).toHaveBeenCalledWith('output.json', expect.any(String)); + expect(reducedSpec.tags).toHaveLength(1); + expect(Object.keys(reducedSpec.paths)).toStrictEqual(['/pet/{petId}']); + expect(Object.keys(reducedSpec.paths['/pet/{petId}'])).toStrictEqual(['get', 'post', 'delete']); + }); + + it('should convert with no prompts via opts', async () => { + const spec = 'petstore-simple.json'; + + let reducedSpec; + fs.writeFileSync = jest.fn((fileName, data) => { + reducedSpec = JSON.parse(data as string); + }); + + await expect( + convert.run({ + spec, + workingDirectory: require.resolve(`@readme/oas-examples/2.0/json/${spec}`).replace(spec, ''), + out: 'output.json', + }) + ).resolves.toBe(successfulConversion()); + + expect(fs.writeFileSync).toHaveBeenCalledWith('output.json', expect.any(String)); + expect(Object.keys(reducedSpec.paths)).toStrictEqual(['/pet/{petId}']); + expect(Object.keys(reducedSpec.paths['/pet/{petId}'])).toStrictEqual(['get', 'post', 'delete']); + }); + }); + + describe('error handling', () => { + it.each([['json'], ['yaml']])('should fail if given an OpenAPI 3.0 definition (format: %s)', async format => { + const spec = require.resolve(`@readme/oas-examples/3.0/${format}/petstore.${format}`); + + await expect( + convert.run({ + spec, + }) + ).rejects.toStrictEqual( + new Error("Sorry, this API definition is already an OpenAPI definition and doesn't need to be converted.") + ); + }); + }); +}); diff --git a/__tests__/lib/__snapshots__/commands.test.ts.snap b/__tests__/lib/__snapshots__/commands.test.ts.snap index 68cb4d75b..cd6a6744b 100644 --- a/__tests__/lib/__snapshots__/commands.test.ts.snap +++ b/__tests__/lib/__snapshots__/commands.test.ts.snap @@ -29,6 +29,11 @@ exports[`utils #listByCategory should list commands by category 1`] = ` "hidden": false, "name": "openapi", }, + { + "description": "Convert a Swagger or Postman Collection to OpenAPI.", + "hidden": false, + "name": "openapi:convert", + }, { "description": "Analyze an OpenAPI/Swagger definition for various OpenAPI and ReadMe feature usage.", "hidden": false, diff --git a/src/cmds/index.ts b/src/cmds/index.ts index f4d93b583..074ae5121 100644 --- a/src/cmds/index.ts +++ b/src/cmds/index.ts @@ -12,6 +12,7 @@ import LogoutCommand from './logout'; import OASCommand from './oas'; import OpenCommand from './open'; import OpenAPICommand from './openapi'; +import OpenAPIConvertCommand from './openapi/convert'; import OpenAPIInspectCommand from './openapi/inspect'; import OpenAPIReduceCommand from './openapi/reduce'; import OpenAPIValidateCommand from './openapi/validate'; @@ -47,6 +48,7 @@ const commands = { open: OpenCommand, openapi: OpenAPICommand, + 'openapi:convert': OpenAPIConvertCommand, 'openapi:inspect': OpenAPIInspectCommand, 'openapi:reduce': OpenAPIReduceCommand, 'openapi:validate': OpenAPIValidateCommand, diff --git a/src/cmds/openapi/convert.ts b/src/cmds/openapi/convert.ts new file mode 100644 index 000000000..a6928b780 --- /dev/null +++ b/src/cmds/openapi/convert.ts @@ -0,0 +1,90 @@ +import type { CommandOptions } from '../../lib/baseCommand'; + +import fs from 'fs'; +import path from 'path'; + +import chalk from 'chalk'; +import prompts from 'prompts'; + +import Command, { CommandCategories } from '../../lib/baseCommand'; +import { checkFilePath } from '../../lib/checkFile'; +import prepareOas from '../../lib/prepareOas'; +import promptTerminal from '../../lib/promptWrapper'; + +export interface Options { + spec?: string; + out?: string; + workingDirectory?: string; +} + +export default class OpenAPIConvertCommand extends Command { + constructor() { + super(); + + this.command = 'openapi:convert'; + this.usage = 'openapi:convert [file|url] [options]'; + this.description = 'Convert a Swagger or Postman Collection to OpenAPI.'; + this.cmdCategory = CommandCategories.APIS; + + this.hiddenArgs = ['spec']; + this.args = [ + { + name: 'spec', + type: String, + defaultOption: true, + }, + { + name: 'out', + type: String, + description: 'Output file path to write reduced file to', + }, + { + name: 'workingDirectory', + type: String, + description: 'Working directory (for usage with relative external references)', + }, + ]; + } + + async run(opts: CommandOptions) { + await super.run(opts); + + const { spec, workingDirectory } = opts; + + if (workingDirectory) { + process.chdir(workingDirectory); + } + + const { bundledSpec, specPath, specType } = await prepareOas(spec, 'openapi:convert', { convertToLatest: true }); + const parsedBundledSpec = JSON.parse(bundledSpec); + + if (specType === 'OpenAPI') { + throw new Error("Sorry, this API definition is already an OpenAPI definition and doesn't need to be converted."); + } + + prompts.override({ + outputPath: opts.out, + }); + + const promptResults = await promptTerminal([ + { + type: 'text', + name: 'outputPath', + message: 'Enter the path to save your converted API definition to:', + initial: () => { + const extension = path.extname(specPath); + return `${path.basename(specPath).split(extension)[0]}.openapi${extension}`; + }, + validate: value => checkFilePath(value), + }, + ]); + + Command.debug(`saving converted spec to ${promptResults.outputPath}`); + + fs.writeFileSync(promptResults.outputPath, JSON.stringify(parsedBundledSpec, null, 2)); + + Command.debug('converted spec saved'); + + return Promise.resolve(chalk.green(`Your converted API definition has been saved to ${promptResults.outputPath}!`)); + } +} diff --git a/src/lib/logger.ts b/src/lib/logger.ts index b2e2cdc69..13d496f50 100644 --- a/src/lib/logger.ts +++ b/src/lib/logger.ts @@ -13,9 +13,17 @@ const debugPackage = debugModule(config.get('cli')); /** * Wrapper for debug statements. */ -function debug(input: string) { +// eslint-disable-next-line @typescript-eslint/no-explicit-any +function debug(input: any) { /* istanbul ignore next */ - if (isGHA() && !isTest()) core.debug(`rdme: ${input}`); + if (isGHA() && !isTest()) { + if (typeof input === 'object') { + core.debug(`rdme: ${JSON.stringify(input)}`); + } else { + core.debug(`rdme: ${input}`); + } + } + return debugPackage(input); } diff --git a/src/lib/prepareOas.ts b/src/lib/prepareOas.ts index 4603f9ffd..8301a8450 100644 --- a/src/lib/prepareOas.ts +++ b/src/lib/prepareOas.ts @@ -1,3 +1,5 @@ +import type { OASDocument } from 'oas/dist/rmoas.types'; + import chalk from 'chalk'; import OASNormalize, { getAPIDefinitionType } from 'oas-normalize'; import ora from 'ora'; @@ -35,7 +37,7 @@ const capitalizeSpecType = (type: string) => */ export default async function prepareOas( path: string, - command: 'openapi' | 'openapi:inspect' | 'openapi:reduce' | 'openapi:validate', + command: 'openapi' | 'openapi:convert' | 'openapi:inspect' | 'openapi:reduce' | 'openapi:validate', opts: { /** * Optionally convert the supplied or discovered API definition to the latest OpenAPI release. @@ -65,13 +67,13 @@ export default async function prepareOas( const fileFindingSpinner = ora({ text: 'Looking for API definitions...', ...oraOptions() }).start(); - let action: 'inspect' | 'reduce' | 'upload' | 'validate'; + let action: 'convert' | 'inspect' | 'reduce' | 'upload' | 'validate'; switch (command) { case 'openapi': action = 'upload'; break; default: - action = command.split(':')[1] as 'inspect' | 'reduce' | 'validate'; + action = command.split(':')[1] as 'convert' | 'inspect' | 'reduce' | 'validate'; } const jsonAndYamlFiles = readdirRecursive('.', true).filter( @@ -164,7 +166,7 @@ export default async function prepareOas( }); // If we were supplied a Postman collection this will **always** convert it to OpenAPI 3.0. - const api = await oas.validate({ convertToLatest: opts.convertToLatest }).catch((err: Error) => { + const api: OASDocument = await oas.validate({ convertToLatest: opts.convertToLatest }).catch((err: Error) => { spinner.fail(); debug(`raw validation error object: ${JSON.stringify(err)}`); throw err; @@ -188,6 +190,10 @@ export default async function prepareOas( }); debug('spec bundled'); + } else if (command === 'openapi:convert') { + // As `openapi:convert` is purely for converting a spec to OpenAPI we don't need to do any + // bundling work as those'll be handled in other commands. + bundledSpec = JSON.stringify(api); } return { From b443b7cb41a1e998a14be5fd18fe3c86f135a646 Mon Sep 17 00:00:00 2001 From: Jon Ursenbach Date: Mon, 19 Dec 2022 12:26:05 -0800 Subject: [PATCH 2/9] fix: alex issues --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 6f3fb22c5..a2425a1ed 100644 --- a/README.md +++ b/README.md @@ -253,6 +253,7 @@ Similar to the `openapi` command, you can also [omit the file path](#omitting-th #### Converting an API definition + You can also convert any Swagger or Postman Collection to an OpenAPI 3.0 definition. ```sh From c5b9f0639f5d102978b5a452c53ca3ea4d3be161 Mon Sep 17 00:00:00 2001 From: Jon Ursenbach Date: Mon, 19 Dec 2022 12:28:54 -0800 Subject: [PATCH 3/9] fix: eslint issues --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index a2425a1ed..42ea46c14 100644 --- a/README.md +++ b/README.md @@ -254,6 +254,7 @@ Similar to the `openapi` command, you can also [omit the file path](#omitting-th #### Converting an API definition + You can also convert any Swagger or Postman Collection to an OpenAPI 3.0 definition. ```sh From 567ae8f397a1d5eaa0a43b7fcdc912818250f957 Mon Sep 17 00:00:00 2001 From: Jon Ursenbach Date: Mon, 19 Dec 2022 13:19:14 -0800 Subject: [PATCH 4/9] Update README.md Co-authored-by: Kanad Gupta <8854718+kanadgupta@users.noreply.github.com> --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 42ea46c14..9cb011d41 100644 --- a/README.md +++ b/README.md @@ -265,7 +265,7 @@ Similar to the `openapi` command, you can also [omit the file path](#omitting-th > **Note:** > -> All of our OpenAPI commands already do this conversion automatically, but incase you need to utilize this exclusive functionality outside of the context of those you can. +> All of our OpenAPI commands already do this conversion automatically, but in case you need to utilize this exclusive functionality outside of the context of those, you can. #### Reducing an API Definition From 53a9e6d85a63e3c0837dc88aa12eba23aa037d0f Mon Sep 17 00:00:00 2001 From: Jon Ursenbach Date: Mon, 19 Dec 2022 13:24:01 -0800 Subject: [PATCH 5/9] Update src/cmds/openapi/convert.ts Co-authored-by: Kanad Gupta <8854718+kanadgupta@users.noreply.github.com> --- src/cmds/openapi/convert.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cmds/openapi/convert.ts b/src/cmds/openapi/convert.ts index a6928b780..ce3c538d6 100644 --- a/src/cmds/openapi/convert.ts +++ b/src/cmds/openapi/convert.ts @@ -36,7 +36,7 @@ export default class OpenAPIConvertCommand extends Command { { name: 'out', type: String, - description: 'Output file path to write reduced file to', + description: 'Output file path to write converted file to', }, { name: 'workingDirectory', From 21a83444bad664962281727ff88618bd894b65fe Mon Sep 17 00:00:00 2001 From: Jon Ursenbach Date: Mon, 19 Dec 2022 13:23:40 -0800 Subject: [PATCH 6/9] chore: tweaking the reduce command a bit --- README.md | 4 ++-- src/cmds/openapi/reduce.ts | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 9cb011d41..cd04440c5 100644 --- a/README.md +++ b/README.md @@ -279,12 +279,12 @@ The command will ask you a couple questions about how you wish to reduce the fil - The input API definition is called `petstore.json` - The file is reduced to only the `/pet/{id}` path and the `GET` and `PUT` methods -- The output file is called `petstore-reduced.json` +- The output file is called `petstore.reduced.json` Here's what the resulting command looks like: ``` -rdme openapi:reduce petstore.json --path /pet/{id} --method get --method put --out petstore-reduced.json +rdme openapi:reduce petstore.json --path /pet/{id} --method get --method put --out petstore.reduced.json ``` As with the `openapi` command, you can also [omit the file path](#omitting-the-file-path). diff --git a/src/cmds/openapi/reduce.ts b/src/cmds/openapi/reduce.ts index de213e972..4e792c9f3 100644 --- a/src/cmds/openapi/reduce.ts +++ b/src/cmds/openapi/reduce.ts @@ -173,7 +173,7 @@ export default class OpenAPIReduceCommand extends Command { message: 'Enter the path to save your reduced API definition to:', initial: () => { const extension = path.extname(specPath); - return `${path.basename(specPath).split(extension)[0]}-reduced${extension}`; + return `${path.basename(specPath).split(extension)[0]}.reduced${extension}`; }, validate: value => checkFilePath(value), }, From 23e879eed16648d5397b131d928811caf6ad7e39 Mon Sep 17 00:00:00 2001 From: Jon Ursenbach Date: Mon, 19 Dec 2022 13:24:58 -0800 Subject: [PATCH 7/9] fix: pr feedback --- README.md | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index cd04440c5..518fcdc44 100644 --- a/README.md +++ b/README.md @@ -145,9 +145,9 @@ With `rdme`, you have access to a variety of tools to manage your API definition - [Syncing](#syncing-an-api-definition-to-readme) 🦉 - [Validation](#validating-an-api-definition) ✅ -- [Conversion](#converting-an-api-definition) ⏩ - [Reduction](#reducing-an-api-definition) 📉 - [Inspection](#inspecting-an-api-definition) 🔍 +- [Conversion](#converting-an-api-definition) ⏩ `rdme` supports [OpenAPI 3.1](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md), [OpenAPI 3.0](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md), and [Swagger 2.x](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/2.0.md). @@ -251,22 +251,6 @@ rdme openapi:validate [url-or-local-path-to-file] Similar to the `openapi` command, you can also [omit the file path](#omitting-the-file-path). -#### Converting an API definition - - - -You can also convert any Swagger or Postman Collection to an OpenAPI 3.0 definition. - -```sh -rdme openapi:convert [url-or-local-path-to-file] -``` - -Similar to the `openapi` command, you can also [omit the file path](#omitting-the-file-path). - -> **Note:** -> -> All of our OpenAPI commands already do this conversion automatically, but in case you need to utilize this exclusive functionality outside of the context of those, you can. - #### Reducing an API Definition We also offer a tool that allows you to reduce a large API definition down to a specific set of tags or paths (again, no ReadMe account required!). This can be useful if you're debugging a problematic schema somewhere, or if you have a file that is too big to maintain. @@ -305,6 +289,22 @@ rdme openapi:inspect [url-or-local-path-to-file] --feature circularRefs --featur As with the `openapi` command, you can also [omit the file path](#omitting-the-file-path). +#### Converting an API definition + + + +You can also convert any Swagger or Postman Collection to an OpenAPI 3.0 definition. + +```sh +rdme openapi:convert [url-or-local-path-to-file] +``` + +Similar to the `openapi` command, you can also [omit the file path](#omitting-the-file-path). + +> **Note:** +> +> All of our OpenAPI commands already do this conversion automatically, but in case you need to utilize this exclusive functionality outside of the context of those, you can. + ### Docs (a.k.a. Guides) 📖 The Markdown files will require YAML front matter with certain ReadMe documentation attributes. Check out [our docs](https://docs.readme.com/docs/rdme#markdown-file-setup) for more info on setting up your front matter. From 1c13d544f3cb725b26d0d44eed9ec7b9d14e9b1a Mon Sep 17 00:00:00 2001 From: Jon Ursenbach Date: Mon, 19 Dec 2022 13:29:44 -0800 Subject: [PATCH 8/9] fix: pr feedback --- package.json | 2 +- src/cmds/openapi/convert.ts | 6 +++--- src/cmds/openapi/index.ts | 4 ++-- src/cmds/openapi/inspect.ts | 6 +++--- src/cmds/openapi/reduce.ts | 12 ++++++------ src/lib/prepareOas.ts | 8 ++++---- 6 files changed, 19 insertions(+), 19 deletions(-) diff --git a/package.json b/package.json index 619e21286..7c0635031 100644 --- a/package.json +++ b/package.json @@ -100,7 +100,7 @@ "bump": "npm version -m 'build: %s release'", "debug": "ts-node src/cli.ts", "lint": "eslint . bin/rdme --ext .js,.ts", - "lint-docs": "npx alex . && npm run prettier:docs", + "lint-docs": "npx -y alex . && npm run prettier:docs", "postversion": "git tag $npm_package_version && ./bin/set-major-version-tag.js", "prebuild": "rm -rf dist/", "prepack": "npm run build", diff --git a/src/cmds/openapi/convert.ts b/src/cmds/openapi/convert.ts index ce3c538d6..a063a50c4 100644 --- a/src/cmds/openapi/convert.ts +++ b/src/cmds/openapi/convert.ts @@ -55,8 +55,8 @@ export default class OpenAPIConvertCommand extends Command { process.chdir(workingDirectory); } - const { bundledSpec, specPath, specType } = await prepareOas(spec, 'openapi:convert', { convertToLatest: true }); - const parsedBundledSpec = JSON.parse(bundledSpec); + const { preparedSpec, specPath, specType } = await prepareOas(spec, 'openapi:convert', { convertToLatest: true }); + const parsedPreparedSpec = JSON.parse(preparedSpec); if (specType === 'OpenAPI') { throw new Error("Sorry, this API definition is already an OpenAPI definition and doesn't need to be converted."); @@ -81,7 +81,7 @@ export default class OpenAPIConvertCommand extends Command { Command.debug(`saving converted spec to ${promptResults.outputPath}`); - fs.writeFileSync(promptResults.outputPath, JSON.stringify(parsedBundledSpec, null, 2)); + fs.writeFileSync(promptResults.outputPath, JSON.stringify(parsedPreparedSpec, null, 2)); Command.debug('converted spec saved'); diff --git a/src/cmds/openapi/index.ts b/src/cmds/openapi/index.ts index bfb920174..62b1f71fc 100644 --- a/src/cmds/openapi/index.ts +++ b/src/cmds/openapi/index.ts @@ -135,7 +135,7 @@ export default class OpenAPICommand extends Command { // Reason we're hardcoding in command here is because `swagger` command // relies on this and we don't want to use `swagger` in this function - const { bundledSpec, specPath, specType, specVersion } = await prepareOas(spec, 'openapi'); + const { preparedSpec, specPath, specType, specVersion } = await prepareOas(spec, 'openapi'); if (useSpecVersion) { Command.info( @@ -218,7 +218,7 @@ export default class OpenAPICommand extends Command { }); }; - const registryUUID = await streamSpecToRegistry(bundledSpec); + const registryUUID = await streamSpecToRegistry(preparedSpec); const options: RequestInit = { headers: cleanHeaders( diff --git a/src/cmds/openapi/inspect.ts b/src/cmds/openapi/inspect.ts index 13557c581..9ed88c409 100644 --- a/src/cmds/openapi/inspect.ts +++ b/src/cmds/openapi/inspect.ts @@ -242,9 +242,9 @@ export default class OpenAPIInspectCommand extends Command { process.chdir(workingDirectory); } - const { bundledSpec, definitionVersion } = await prepareOas(spec, 'openapi:inspect', { convertToLatest: true }); + const { preparedSpec, definitionVersion } = await prepareOas(spec, 'openapi:inspect', { convertToLatest: true }); this.definitionVersion = definitionVersion.version; - const parsedBundledSpec = JSON.parse(bundledSpec); + const parsedPreparedSpec = JSON.parse(preparedSpec); const spinner = ora({ ...oraOptions() }); if (features?.length) { @@ -257,7 +257,7 @@ export default class OpenAPIInspectCommand extends Command { spinner.start('Analyzing your API definition for OpenAPI and ReadMe feature usage...'); } - const analysis = await analyzeOas(parsedBundledSpec).catch(err => { + const analysis = await analyzeOas(parsedPreparedSpec).catch(err => { Command.debug(`analyzer err: ${err.message}`); spinner.fail(); throw err; diff --git a/src/cmds/openapi/reduce.ts b/src/cmds/openapi/reduce.ts index 4e792c9f3..a8bc5c360 100644 --- a/src/cmds/openapi/reduce.ts +++ b/src/cmds/openapi/reduce.ts @@ -80,8 +80,8 @@ export default class OpenAPIReduceCommand extends Command { process.chdir(workingDirectory); } - const { bundledSpec, specPath, specType } = await prepareOas(spec, 'openapi:reduce'); - const parsedBundledSpec = JSON.parse(bundledSpec); + const { preparedSpec, specPath, specType } = await prepareOas(spec, 'openapi:reduce'); + const parsedPreparedSpec = JSON.parse(preparedSpec); if (specType !== 'OpenAPI') { throw new Error('Sorry, this reducer feature in rdme only supports OpenAPI 3.0+ definitions.'); @@ -117,7 +117,7 @@ export default class OpenAPIReduceCommand extends Command { choices: () => { const tags: string[] = JSONPath({ path: '$..paths[*].tags', - json: parsedBundledSpec, + json: parsedPreparedSpec, resultType: 'value', }).flat(); @@ -133,7 +133,7 @@ export default class OpenAPIReduceCommand extends Command { message: 'Choose which paths to reduce by:', min: 1, choices: () => { - return Object.keys(parsedBundledSpec.paths).map(p => ({ + return Object.keys(parsedPreparedSpec.paths).map(p => ({ title: p, value: p, })); @@ -147,7 +147,7 @@ export default class OpenAPIReduceCommand extends Command { choices: (prev, values) => { const paths: string[] = values.paths; let methods = paths - .map((p: string) => Object.keys(parsedBundledSpec.paths[p] || {})) + .map((p: string) => Object.keys(parsedPreparedSpec.paths[p] || {})) .flat() .filter((method: string) => method.toLowerCase() !== 'parameters'); @@ -196,7 +196,7 @@ export default class OpenAPIReduceCommand extends Command { let reducedSpec; try { - reducedSpec = oasReducer(parsedBundledSpec, { + reducedSpec = oasReducer(parsedPreparedSpec, { tags: promptResults.tags || [], paths: (promptResults.paths || []).reduce((acc: Record, p: string) => { acc[p] = promptResults.methods; diff --git a/src/lib/prepareOas.ts b/src/lib/prepareOas.ts index 8301a8450..6b4e324bd 100644 --- a/src/lib/prepareOas.ts +++ b/src/lib/prepareOas.ts @@ -182,10 +182,10 @@ export default async function prepareOas( const specVersion: string = api.info.version; debug(`version in spec: ${specVersion}`); - let bundledSpec = ''; + let preparedSpec = ''; if (['openapi', 'openapi:inspect', 'openapi:reduce'].includes(command)) { - bundledSpec = await oas.bundle().then(res => { + preparedSpec = await oas.bundle().then(res => { return JSON.stringify(res); }); @@ -193,11 +193,11 @@ export default async function prepareOas( } else if (command === 'openapi:convert') { // As `openapi:convert` is purely for converting a spec to OpenAPI we don't need to do any // bundling work as those'll be handled in other commands. - bundledSpec = JSON.stringify(api); + preparedSpec = JSON.stringify(api); } return { - bundledSpec, + preparedSpec, specPath, specType, /** From 4ceafaa6416b18aac55d965810421b53d7197798 Mon Sep 17 00:00:00 2001 From: Jon Ursenbach Date: Mon, 19 Dec 2022 13:37:12 -0800 Subject: [PATCH 9/9] chore: revert change to package.json --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 7c0635031..619e21286 100644 --- a/package.json +++ b/package.json @@ -100,7 +100,7 @@ "bump": "npm version -m 'build: %s release'", "debug": "ts-node src/cli.ts", "lint": "eslint . bin/rdme --ext .js,.ts", - "lint-docs": "npx -y alex . && npm run prettier:docs", + "lint-docs": "npx alex . && npm run prettier:docs", "postversion": "git tag $npm_package_version && ./bin/set-major-version-tag.js", "prebuild": "rm -rf dist/", "prepack": "npm run build",