-
Notifications
You must be signed in to change notification settings - Fork 393
/
Copy pathapi.ts
53 lines (45 loc) · 1.75 KB
/
api.ts
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
import AsciiTable from 'ascii-table'
import type { OptionValues } from 'commander'
import { methods, type NetlifyAPI } from 'netlify'
import { ansis, logAndThrowError, exit, log, logJson } from '../../utils/command-helpers.js'
import type BaseCommand from '../base-command.js'
type ApiMethodName = keyof NetlifyAPI
const isValidApiMethod = (api: NetlifyAPI, apiMethod: string): apiMethod is ApiMethodName =>
Object.hasOwn(api, apiMethod)
export const apiCommand = async (apiMethodName: string, options: OptionValues, command: BaseCommand) => {
const { api } = command.netlify
if (options.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/#operation/${operationId}`)
})
log(table.toString())
log()
log('Above is a list of available API methods')
log(`To run a method use "${ansis.cyanBright('netlify api methodName')}"`)
exit()
}
if (!apiMethodName) {
return logAndThrowError(`You must provide an API method. Run "netlify api --list" to see available methods`)
}
if (!(isValidApiMethod(api, apiMethodName) && typeof api[apiMethodName] === 'function')) {
return logAndThrowError(
`"${apiMethodName}"" is not a valid api method. Run "netlify api --list" to see available methods`,
)
}
const apiMethod = api[apiMethodName].bind(api)
let payload
if (options.data) {
payload = typeof options.data === 'string' ? JSON.parse(options.data) : options.data
} else {
payload = {}
}
try {
const apiResponse = await apiMethod(payload)
logJson(apiResponse)
} catch (error_) {
return logAndThrowError(error_)
}
}