Skip to content

Commit b476309

Browse files
committed
adding common files
1 parent 246f60f commit b476309

File tree

2 files changed

+345
-0
lines changed

2 files changed

+345
-0
lines changed
Lines changed: 271 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,271 @@
1+
import { fetchCloudflareApi } from '../cloudflare-api'
2+
import {
3+
AssetCategoriesResponse,
4+
AssetDetail,
5+
AssetsResponse,
6+
IntegrationResponse,
7+
IntegrationsResponse,
8+
zReturnedAssetCategoriesResult,
9+
zReturnedAssetsResult,
10+
zReturnedIntegrationResult,
11+
zReturnedIntegrationsResult,
12+
} from '../schemas/cf1-integrations'
13+
import { V4Schema } from '../v4-api'
14+
15+
/**
16+
* Fetches integration by ID for a specified Cloudflare One Integration
17+
* @param intgerationIdParam ID of the CF1 Integration to get
18+
* @param accountId Cloudflare account ID
19+
* @param apiToken Cloudflare API token
20+
* @returns The integration result or null
21+
*/
22+
export async function handleIntegrationById({
23+
accountId,
24+
apiToken,
25+
integrationIdParam,
26+
}: {
27+
accountId: string
28+
apiToken: string
29+
integrationIdParam: string
30+
}): Promise<{ integration: zReturnedIntegrationResult | null }> {
31+
const data = await fetchCloudflareApi({
32+
endpoint: `/casb/integrations/${integrationIdParam}`,
33+
accountId,
34+
apiToken,
35+
responseSchema: V4Schema(IntegrationResponse),
36+
options: {
37+
method: 'GET',
38+
headers: {
39+
'Content-Type': 'application/json',
40+
},
41+
},
42+
})
43+
44+
return { integration: data.result }
45+
}
46+
47+
/**
48+
* Fetches all integrations in a given CF1 Account.
49+
* @param accountId Cloudflare account ID
50+
* @param apiToken Cloudflare API token
51+
* @returns The logs analysis result with filtered relevant information
52+
*/
53+
export async function handleIntegrations({
54+
accountId,
55+
apiToken,
56+
}: {
57+
accountId: string
58+
apiToken: string
59+
}): Promise<{ integrations: zReturnedIntegrationsResult | null }> {
60+
const data = await fetchCloudflareApi({
61+
endpoint: `/casb/integrations`,
62+
accountId,
63+
apiToken,
64+
responseSchema: V4Schema(IntegrationsResponse),
65+
options: {
66+
method: 'GET',
67+
headers: { 'Content-Type': 'application/json' },
68+
},
69+
})
70+
71+
return { integrations: data.result }
72+
}
73+
74+
export async function handleAssetCategories({
75+
accountId,
76+
apiToken,
77+
78+
type,
79+
vendor,
80+
}: {
81+
accountId: string
82+
apiToken: string
83+
84+
type?: string
85+
vendor?: string
86+
}): Promise<{ categories: zReturnedAssetCategoriesResult | null }> {
87+
const params = new URLSearchParams()
88+
if (vendor) params.append('vendor', vendor)
89+
if (type) params.append('type', type)
90+
91+
console.log('\n\n\n', params, '\n\n\n')
92+
93+
const data = await fetchCloudflareApi({
94+
endpoint: `/casb/asset_categories?${params.toString()}`,
95+
accountId,
96+
apiToken,
97+
responseSchema: V4Schema(AssetCategoriesResponse),
98+
options: {
99+
method: 'GET',
100+
headers: { 'Content-Type': 'application/json' },
101+
},
102+
})
103+
104+
return { categories: data.result }
105+
}
106+
107+
// Base handler for asset-related operations
108+
async function handleAssetRequest({
109+
accountId,
110+
apiToken,
111+
endpoint,
112+
params,
113+
}: {
114+
accountId: string
115+
apiToken: string
116+
endpoint: string
117+
params?: URLSearchParams
118+
}): Promise<{ assets: zReturnedAssetsResult }> {
119+
const fullEndpoint = `/casb${endpoint}?${params?.toString()}`
120+
console.log('ENDPOINT: ', fullEndpoint)
121+
const data = await fetchCloudflareApi({
122+
endpoint: fullEndpoint,
123+
accountId,
124+
apiToken,
125+
responseSchema: V4Schema(AssetsResponse),
126+
options: {
127+
method: 'GET',
128+
headers: { 'Content-Type': 'application/json' },
129+
},
130+
})
131+
132+
console.log('ASSETS DATA: ', data)
133+
134+
return { assets: data?.result || [] }
135+
}
136+
137+
// Get all assets with optional pagination and filters
138+
export async function handleAssets({
139+
accountId,
140+
apiToken,
141+
page,
142+
pageSize,
143+
}: {
144+
accountId: string
145+
apiToken: string
146+
page?: number
147+
pageSize?: number
148+
}) {
149+
const params = new URLSearchParams()
150+
if (page) params.append('page', String(page))
151+
if (pageSize) params.append('page_size', String(pageSize))
152+
153+
const { assets } = await handleAssetRequest({
154+
accountId,
155+
apiToken,
156+
endpoint: '/assets',
157+
params,
158+
})
159+
160+
return { assets }
161+
}
162+
163+
// Get assets by integration ID
164+
export async function handleAssetsByIntegrationId({
165+
accountId,
166+
apiToken,
167+
integrationId,
168+
page,
169+
pageSize,
170+
}: {
171+
accountId: string
172+
apiToken: string
173+
integrationId: string
174+
page?: number
175+
pageSize?: number
176+
}) {
177+
const params = new URLSearchParams({ integration_id: integrationId })
178+
if (page) params.append('page', String(page))
179+
if (pageSize) params.append('page_size', String(pageSize))
180+
181+
const { assets } = await handleAssetRequest({
182+
accountId,
183+
apiToken,
184+
endpoint: '/assets',
185+
params,
186+
})
187+
188+
return { assets }
189+
}
190+
191+
// Get single asset by ID
192+
export async function handleAssetById({
193+
accountId,
194+
apiToken,
195+
assetId,
196+
}: {
197+
accountId: string
198+
apiToken: string
199+
assetId: string
200+
}) {
201+
const data = await fetchCloudflareApi({
202+
endpoint: `/casb/assets/${assetId}`,
203+
accountId,
204+
apiToken,
205+
responseSchema: V4Schema(AssetDetail),
206+
options: {
207+
method: 'GET',
208+
headers: { 'Content-Type': 'application/json' },
209+
},
210+
})
211+
212+
console.log('RAW DATA: ', data)
213+
214+
return { asset: data.result }
215+
}
216+
217+
// Get assets by category ID
218+
export async function handleAssetsByAssetCategoryId({
219+
accountId,
220+
apiToken,
221+
categoryId,
222+
page,
223+
pageSize,
224+
}: {
225+
accountId: string
226+
apiToken: string
227+
categoryId: string
228+
page?: number
229+
pageSize?: number
230+
}) {
231+
const params = new URLSearchParams({ category_id: categoryId })
232+
if (page) params.append('page', String(page))
233+
if (pageSize) params.append('page_size', String(pageSize))
234+
235+
const { assets } = await handleAssetRequest({
236+
accountId,
237+
apiToken,
238+
endpoint: '/assets',
239+
params,
240+
})
241+
242+
return { assets }
243+
}
244+
245+
// Search assets
246+
export async function handleAssetsSearch({
247+
accountId,
248+
apiToken,
249+
searchTerm,
250+
page,
251+
pageSize,
252+
}: {
253+
accountId: string
254+
apiToken: string
255+
searchTerm: string
256+
page?: number
257+
pageSize?: number
258+
}) {
259+
const params = new URLSearchParams({ search: searchTerm })
260+
if (page) params.append('page', String(page))
261+
if (pageSize) params.append('page_size', String(pageSize))
262+
263+
const { assets } = await handleAssetRequest({
264+
accountId,
265+
apiToken,
266+
endpoint: '/assets',
267+
params,
268+
})
269+
270+
return { assets }
271+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import { z } from 'zod'
2+
3+
const Vendor = z.object({
4+
id: z.string(),
5+
name: z.string(),
6+
display_name: z.string(),
7+
description: z.string().nullable(),
8+
logo: z.string().nullable(),
9+
static_logo: z.string().nullable(),
10+
})
11+
12+
const Policy = z.object({
13+
id: z.string(),
14+
name: z.string(),
15+
permissions: z.array(z.string()),
16+
link: z.string().nullable(),
17+
dlp_enabled: z.boolean(),
18+
})
19+
20+
// Base Integration schema
21+
export const Integration = z.object({
22+
id: z.string(),
23+
name: z.string(),
24+
status: z.enum(['Healthy', 'Unhealthy', 'Initializing', 'Paused']),
25+
upgradable: z.boolean(),
26+
permissions: z.array(z.string()),
27+
28+
vendor: Vendor,
29+
policy: Policy,
30+
31+
created: z.string(),
32+
updated: z.string(),
33+
credentials_expiry: z.string().nullable(),
34+
last_hydrated: z.string().nullable(),
35+
})
36+
37+
// Schema for output: a single integration
38+
export const IntegrationResponse = Integration
39+
export type zReturnedIntegrationResult = z.infer<typeof IntegrationResponse>
40+
41+
// Schema for output: multiple integrations
42+
export const IntegrationsResponse = z.array(Integration)
43+
export type zReturnedIntegrationsResult = z.infer<typeof IntegrationsResponse>
44+
45+
export const AssetCategory = z.object({
46+
id: z.string().uuid(),
47+
type: z.string(),
48+
vendor: z.string(),
49+
service: z.string().nullable(),
50+
})
51+
52+
export const AssetDetail = z.object({
53+
id: z.string().uuid(),
54+
external_id: z.string(),
55+
name: z.string(),
56+
link: z.string().nullable(),
57+
fields: z.array(
58+
z.object({
59+
link: z.string().nullable(),
60+
name: z.string(),
61+
value: z.any(),
62+
})
63+
),
64+
category: AssetCategory,
65+
integration: Integration,
66+
})
67+
68+
export type zReturnedAssetResult = z.infer<typeof AssetDetail>
69+
70+
export const AssetsResponse = z.array(AssetDetail)
71+
export type zReturnedAssetsResult = z.infer<typeof AssetsResponse>
72+
73+
export const AssetCategoriesResponse = z.array(AssetCategory)
74+
export type zReturnedAssetCategoriesResult = z.infer<typeof AssetCategoriesResponse>

0 commit comments

Comments
 (0)