-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: initial pass at preview command
- Loading branch information
1 parent
9fc7a0f
commit 371def7
Showing
3 changed files
with
72 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!`)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters