-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathfetch-quota.ts
43 lines (34 loc) · 1.21 KB
/
fetch-quota.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
import constants from '../../constants'
import { handleApiCall, handleUnsuccessfulApiResponse } from '../../utils/api'
import { AuthError } from '../../utils/errors'
import { getDefaultToken, setupSdk } from '../../utils/sdk'
import type { SocketSdkReturnType } from '@socketsecurity/sdk'
export async function fetchQuota(): Promise<
SocketSdkReturnType<'getQuota'>['data'] | undefined
> {
const apiToken = getDefaultToken()
if (!apiToken) {
throw new AuthError(
'User must be authenticated to run this command. To log in, run the command `socket login` and enter your API key.'
)
}
return await fetchQuotaWithToken(apiToken)
}
async function fetchQuotaWithToken(
apiToken: string
): Promise<SocketSdkReturnType<'getQuota'>['data'] | undefined> {
// Lazily access constants.spinner.
const { spinner } = constants
const socketSdk = await setupSdk(apiToken)
spinner.start('Fetching organization quota...')
const result = await handleApiCall(
socketSdk.getQuota(),
'looking up organization quota'
)
spinner?.successAndStop('Recieved organization quota response.')
if (!result.success) {
handleUnsuccessfulApiResponse('getQuota', result)
return
}
return result.data
}