diff --git a/src/cmds/index.ts b/src/cmds/index.ts index 074ae5121..0e688b0d4 100644 --- a/src/cmds/index.ts +++ b/src/cmds/index.ts @@ -14,6 +14,7 @@ import OpenCommand from './open'; import OpenAPICommand from './openapi'; import OpenAPIConvertCommand from './openapi/convert'; import OpenAPIInspectCommand from './openapi/inspect'; +import OpenAPIPreviewCommand from './openapi/preview'; import OpenAPIReduceCommand from './openapi/reduce'; import OpenAPIValidateCommand from './openapi/validate'; import SwaggerCommand from './swagger'; @@ -50,6 +51,7 @@ const commands = { openapi: OpenAPICommand, 'openapi:convert': OpenAPIConvertCommand, 'openapi:inspect': OpenAPIInspectCommand, + 'openapi:preview': OpenAPIPreviewCommand, 'openapi:reduce': OpenAPIReduceCommand, 'openapi:validate': OpenAPIValidateCommand, diff --git a/src/cmds/openapi/preview.ts b/src/cmds/openapi/preview.ts new file mode 100644 index 000000000..128a0e6d2 --- /dev/null +++ b/src/cmds/openapi/preview.ts @@ -0,0 +1,61 @@ +import type { CommandOptions } from '../../lib/baseCommand'; + +import chalk from 'chalk'; +// eslint-disable-next-line no-restricted-imports +import nodeFetch from 'node-fetch'; + +import pkg from '../../../package.json'; +import Command, { CommandCategories } from '../../lib/baseCommand'; +import prepareOas from '../../lib/prepareOas'; + +export interface Options { + spec?: string; + workingDirectory?: string; +} + +export default class OpenAPIValidateCommand extends Command { + constructor() { + super(); + + this.command = 'openapi:preview'; + this.usage = 'openapi:preview [file|url] [options]'; + this.description = 'Preview your OpenAPI/Swagger definition on bin.readme.com.'; + this.cmdCategory = CommandCategories.APIS; + + this.hiddenArgs = ['spec']; + this.args = [ + { + name: 'spec', + type: String, + defaultOption: true, + }, + this.getWorkingDirArg(), + this.getGitHubArg(), + ]; + } + + async run(opts: CommandOptions) { + const binBaseUrl = 'http://localhost:3675'; + await super.run(opts); + + const { spec, workingDirectory } = opts; + + if (workingDirectory) { + process.chdir(workingDirectory); + } + + const { preparedSpec, specPath, specType } = await prepareOas(spec, 'openapi:preview'); + + const res = await nodeFetch(binBaseUrl, { + body: JSON.stringify({ swagger: preparedSpec }), + method: 'post', + headers: { 'Content-Type': 'application/json', 'rdme-version': pkg.version }, + }); + + const jsonRes = await res.json(); + + console.log('jsonRes:', `${binBaseUrl}${jsonRes.url}`); + + return Promise.resolve(chalk.green(`${specPath} is a valid ${specType} API definition!`)); + } +} diff --git a/src/lib/prepareOas.ts b/src/lib/prepareOas.ts index 3a3460025..7e49f2bc5 100644 --- a/src/lib/prepareOas.ts +++ b/src/lib/prepareOas.ts @@ -39,7 +39,13 @@ const capitalizeSpecType = (type: string) => */ export default async function prepareOas( path: string, - command: 'openapi' | 'openapi:convert' | 'openapi:inspect' | 'openapi:reduce' | 'openapi:validate', + command: + | 'openapi' + | 'openapi:convert' + | 'openapi:inspect' + | 'openapi:preview' + | 'openapi:reduce' + | 'openapi:validate', opts: { /** * Optionally convert the supplied or discovered API definition to the latest OpenAPI release. @@ -74,13 +80,13 @@ export default async function prepareOas( const fileFindingSpinner = ora({ text: 'Looking for API definitions...', ...oraOptions() }).start(); - let action: 'convert' | 'inspect' | 'reduce' | 'upload' | 'validate'; + let action: 'convert' | 'inspect' | 'preview' | 'reduce' | 'upload' | 'validate'; switch (command) { case 'openapi': action = 'upload'; break; default: - action = command.split(':')[1] as 'convert' | 'inspect' | 'reduce' | 'validate'; + action = command.split(':')[1] as 'convert' | 'inspect' | 'preview' | 'reduce' | 'validate'; } const jsonAndYamlFiles = readdirRecursive('.', true).filter(