Skip to content

Commit

Permalink
feat: initial pass at preview command
Browse files Browse the repository at this point in the history
  • Loading branch information
kanadgupta committed May 23, 2023
1 parent 9fc7a0f commit 371def7
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/cmds/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -50,6 +51,7 @@ const commands = {
openapi: OpenAPICommand,
'openapi:convert': OpenAPIConvertCommand,
'openapi:inspect': OpenAPIInspectCommand,
'openapi:preview': OpenAPIPreviewCommand,
'openapi:reduce': OpenAPIReduceCommand,
'openapi:validate': OpenAPIValidateCommand,

Expand Down
61 changes: 61 additions & 0 deletions src/cmds/openapi/preview.ts
Original file line number Diff line number Diff line change
@@ -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<Options>) {
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!`));
}
}
12 changes: 9 additions & 3 deletions src/lib/prepareOas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 371def7

Please sign in to comment.