-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathfetch-dependencies.ts
58 lines (48 loc) · 1.45 KB
/
fetch-dependencies.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
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 fetchDependencies({
limit,
offset
}: {
limit: number
offset: number
}): Promise<SocketSdkReturnType<'searchDependencies'>['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 fetchDependenciesWithToken(apiToken, {
limit,
offset
})
}
async function fetchDependenciesWithToken(
apiToken: string,
{
limit,
offset
}: {
limit: number
offset: number
}
): Promise<SocketSdkReturnType<'searchDependencies'>['data'] | undefined> {
// Lazily access constants.spinner.
const { spinner } = constants
spinner.start('Fetching organization dependencies...')
const socketSdk = await setupSdk(apiToken)
const result = await handleApiCall(
socketSdk.searchDependencies({ limit, offset }),
'Searching dependencies'
)
spinner?.successAndStop('Received organization dependencies response.')
if (!result.success) {
handleUnsuccessfulApiResponse('searchDependencies', result)
return
}
return result.data
}