forked from netlify/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.js
More file actions
85 lines (71 loc) · 2.31 KB
/
api.js
File metadata and controls
85 lines (71 loc) · 2.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
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
await this.config.runHook('analytics', {
eventName: 'command',
payload: {
command: "api",
},
});
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 any Netlify API method
For more information on available methods checkout https://open-api.netlify.com/ or run "netlify api --list"
`
APICommand.args = [
{
name: 'apiMethod',
description: 'Open API method to run'
}
]
APICommand.examples = [
'netlify api --list',
"netlify api getSite --data '{ \"site_id\": \"123456\"}'",
]
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