diff --git a/package.json b/package.json index ec6460e4622..c801aa18039 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/commands/api.js b/src/commands/api.js new file mode 100644 index 00000000000..3364dddc721 --- /dev/null +++ b/src/commands/api.js @@ -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