diff --git a/ui/actions/lighthouse/index.ts b/ui/actions/lighthouse/index.ts index 1a874ac7e6..c5bb97c05e 100644 --- a/ui/actions/lighthouse/index.ts +++ b/ui/actions/lighthouse/index.ts @@ -2,3 +2,4 @@ // export * from "./complianceframeworks"; export * from "./compliances"; export * from "./lighthouse"; +export * from "./resources"; diff --git a/ui/actions/lighthouse/resources.ts b/ui/actions/lighthouse/resources.ts new file mode 100644 index 0000000000..21beaabc19 --- /dev/null +++ b/ui/actions/lighthouse/resources.ts @@ -0,0 +1,76 @@ +import { apiBaseUrl, getAuthHeaders, parseStringify } from "@/lib/helper"; + +export async function getLighthouseResources( + page: number = 1, + query: string = "", + sort: string = "", + filters: any = {}, + fields: string[] = [], +) { + const headers = await getAuthHeaders({ contentType: false }); + + const url = new URL(`${apiBaseUrl}/resources`); + + if (page) { + url.searchParams.append("page[number]", page.toString()); + } + + if (sort) { + url.searchParams.append("sort", sort); + } + + if (query) { + url.searchParams.append("filter[search]", query); + } + + if (fields.length > 0) { + url.searchParams.append("fields[resources]", fields.join(",")); + } + + if (filters) { + for (const [key, value] of Object.entries(filters)) { + url.searchParams.append(`filter[${key}]`, value as string); + } + } + + try { + const response = await fetch(url.toString(), { + headers, + }); + const data = await response.json(); + const parsedData = parseStringify(data); + return parsedData; + } catch (error) { + console.error("Error fetching resources:", error); + return undefined; + } +} + +export async function getLighthouseResourceById( + id: string, + fields: string[] = [], + include: string[] = [], +) { + const headers = await getAuthHeaders({ contentType: false }); + const url = new URL(`${apiBaseUrl}/resources/${id}`); + + if (fields.length > 0) { + url.searchParams.append("fields", fields.join(",")); + } + + if (include.length > 0) { + url.searchParams.append("include", include.join(",")); + } + + try { + const response = await fetch(url.toString(), { + headers, + }); + const data = await response.json(); + const parsedData = parseStringify(data); + return parsedData; + } catch (error) { + console.error("Error fetching resource:", error); + return undefined; + } +}