-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathfetch-organization-list.mts
More file actions
71 lines (58 loc) · 1.78 KB
/
fetch-organization-list.mts
File metadata and controls
71 lines (58 loc) · 1.78 KB
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
import { logger } from '@socketsecurity/registry/lib/logger'
import { handleApiCall } from '../../utils/api.mts'
import { setupSdk } from '../../utils/sdk.mts'
import type { CResult } from '../../types.mts'
import type { SetupSdkOptions } from '../../utils/sdk.mts'
import type { SocketSdk, SocketSdkSuccessResult } from '@socketsecurity/sdk'
export type FetchOrganizationOptions = {
description?: string | undefined
sdk?: SocketSdk | undefined
sdkOpts?: SetupSdkOptions | undefined
silence?: boolean | undefined
}
export type EnterpriseOrganization = Omit<Organization, 'plan'> & {
plan: `enterprise${string}`
}
export type EnterpriseOrganizations = EnterpriseOrganization[]
export type Organization =
SocketSdkSuccessResult<'getOrganizations'>['data']['organizations'][string]
export type Organizations = Organization[]
export type OrganizationsData = { organizations: Organizations }
export type OrganizationsCResult = CResult<OrganizationsData>
export async function fetchOrganization(
options?: FetchOrganizationOptions | undefined,
): Promise<OrganizationsCResult> {
const {
description = 'organization list',
sdk,
sdkOpts,
silence = false,
} = {
__proto__: null,
...options,
} as FetchOrganizationOptions
let sockSdk = sdk
if (!sockSdk) {
const sockSdkCResult = await setupSdk(sdkOpts)
if (!sockSdkCResult.ok) {
return sockSdkCResult
}
sockSdk = sockSdkCResult.data
}
const orgsCResult = await handleApiCall(sockSdk.getOrganizations(), {
description,
silence,
})
if (!orgsCResult.ok) {
if (!silence) {
logger.fail(orgsCResult.message, orgsCResult.cause)
}
return orgsCResult
}
return {
...orgsCResult,
data: {
organizations: Object.values(orgsCResult.data.organizations),
},
}
}