Skip to content

Commit

Permalink
Merge pull request #379 from netlify/addAPICommand
Browse files Browse the repository at this point in the history
add “netlify api” command
  • Loading branch information
DavidWells authored Aug 7, 2019
2 parents 50f6ca5 + e89085b commit 8c4e155
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
"lodash.isequal": "^4.5.0",
"lodash.sample": "^4.2.1",
"log-symbols": "^2.2.0",
"netlify": "^2.4.7",
"netlify": "^2.4.8",
"netlify-dev-plugin": "^1.0.28",
"node-fetch": "^2.6.0",
"open": "^6.4.0",
Expand Down
73 changes: 73 additions & 0 deletions src/commands/api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
const Command = require('@netlify/cli-utils')
const AsciiTable = require('ascii-table')
const chalk = require('chalk')
const oclif = require('@oclif/command')
const { methods } = require('netlify')
const { isEmptyCommand } = require('../utils/check-command-inputs')

class APICommand extends Command {
async run() {
const { api } = this.netlify
const { args, flags } = this.parse(APICommand)

const { apiMethod } = args

if (isEmptyCommand(flags, args) || flags.list) {
const table = new AsciiTable(`Netlify API Methods`)
table.setHeading('API Method', 'Docs Link')
methods.forEach((method) => {
const { operationId } = method
table.addRow(operationId, `https://open-api.netlify.com/#/default/${operationId}`)
})
this.log(table.toString())
this.log()
this.log('Above is a list of available API methods')
this.log(`To run a method use "${chalk.cyanBright('netlify api methodName')}"`)
this.exit()
}

if (!apiMethod) {
this.error(`You must provider an API method. Run "netlify api --list" to see available methods`)
}

if (!api[apiMethod] || typeof api[apiMethod] !== 'function') {
this.error(`"${apiMethod}"" is not a valid api method. Run "netlify api --list" to see available methods`)
}

if (flags.data) {
const payload = (typeof flags.data === 'string') ? JSON.parse(flags.data) : flags.data
try {
const apiResponse = await api[apiMethod](payload)
this.log(JSON.stringify(apiResponse, null, 2))
} catch (e) {
this.error(e)
}
}
}
}

APICommand.description = `Run Netlify API Methods
For more information on available methods checkout https://open-api.netlify.com/#/default or run "netlify api --list"
`

APICommand.args = [
{
name: 'apiMethod',
description: 'Open API method to run'
}
]

APICommand.flags = {
data: oclif.flags.string({
char: 'd',
description: 'Data to use'
}),
list: oclif.flags.boolean({
description: 'List out available API methods'
}),
}

APICommand.strict = false

module.exports = APICommand

0 comments on commit 8c4e155

Please sign in to comment.