-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathfetch-security-policy.ts
45 lines (35 loc) · 1.33 KB
/
fetch-security-policy.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
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 fetchSecurityPolicy(
orgSlug: string
): Promise<SocketSdkReturnType<'getOrgSecurityPolicy'>['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 fetchSecurityPolicyWithToken(apiToken, orgSlug)
}
async function fetchSecurityPolicyWithToken(
apiToken: string,
orgSlug: string
): Promise<SocketSdkReturnType<'getOrgSecurityPolicy'>['data'] | undefined> {
// Lazily access constants.spinner.
const { spinner } = constants
const socketSdk = await setupSdk(apiToken)
spinner.start('Fetching organization quota...')
const result = await handleApiCall(
socketSdk.getOrgSecurityPolicy(orgSlug),
'looking up organization quota'
)
spinner?.successAndStop('Received organization quota response.')
if (!result.success) {
handleUnsuccessfulApiResponse('getOrgSecurityPolicy', result)
return
}
return result.data
}