-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.ts
105 lines (89 loc) · 2.86 KB
/
index.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
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/**
* Copyright Zendesk, Inc.
*
* Use of this source code is governed under the Apache License, Version 2.0
* found at http://www.apache.org/licenses/LICENSE-2.0.
*/
import { siteId as getSiteId, token as getToken } from '../index.js';
import { handleErrorMessage, handleSuccessMessage } from '../../utils/index.js';
import { Command } from 'commander';
import { NetlifyAPI } from 'netlify';
import { Ora } from 'ora';
interface INetlifyBandwidthArgs {
token?: string;
siteId?: string;
spinner?: Ora;
}
type RETVAL = {
available: number;
used: number;
};
/**
* Execute the `netlify-bandwidth` command.
*
* @param {string} [args.token] Netlify personal access token.
* @param {string} [args.siteId] Netlify site API ID.
* @param {Ora} [args.spinner] Terminal spinner.
*
* @returns {object} The Netlify available and used bandwidth byte counts.
*/
export const execute = async (args: INetlifyBandwidthArgs = {}): Promise<RETVAL | undefined> => {
let retVal: RETVAL | undefined;
try {
const token = args.token || (await getToken(args.spinner));
const client = new NetlifyAPI(token);
const siteId = args.siteId || (await getSiteId(args.spinner));
/* https://open-api.netlify.com/#operation/getSite */
let response = await client.getSite({ siteId });
const url = `${client.basePath}/accounts/${response.account_slug}/bandwidth`;
/* bandwidth API call not yet supported by Netlify */
response = await fetch(url, { headers: client.defaultHeaders });
if (response.ok) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
const data = (await response.json()) as {
included: number;
additional: number;
used: number;
};
retVal = {
available: data.included + data.additional,
used: data.used
};
} else {
throw new Error(`HTTP Error: ${response.status} ${response.statusText}`);
}
} catch (error: unknown) {
handleErrorMessage(error, 'netlify-bandwidth', args.spinner);
throw error;
}
return retVal;
};
export default (spinner: Ora): Command => {
const command = new Command('netlify-bandwidth');
return command
.description('output remaining Netlify bandwidth')
.option('-i, --id <id>', 'site API ID')
.option('-t, --token <token>', 'access token')
.action(async () => {
try {
spinner.start();
const options = command.opts();
const result = await execute({
token: options.token,
siteId: options.id,
spinner
});
if (result) {
const bandwidth = `${result.available - result.used}`;
handleSuccessMessage(bandwidth, spinner);
} else {
throw new Error();
}
} catch {
spinner.fail('Bandwidth not found');
process.exitCode = 1;
} finally {
spinner.stop();
}
});
};