-
Notifications
You must be signed in to change notification settings - Fork 367
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #379 from netlify/addAPICommand
add “netlify api” command
- Loading branch information
Showing
2 changed files
with
74 additions
and
1 deletion.
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,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 |