From baaf3fea133dde5c579800c7796b01fe55ba520c Mon Sep 17 00:00:00 2001 From: Chandrapal Badshah <12944530+Chan9390@users.noreply.github.com> Date: Wed, 4 Jun 2025 16:40:41 +0530 Subject: [PATCH] Add resources file --- ui/actions/lighthouse/index.ts | 1 + ui/actions/lighthouse/resources.ts | 76 ++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 ui/actions/lighthouse/resources.ts 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; + } +}