diff --git a/builder-frontend/src/api/auth.js b/builder-frontend/src/api/auth.js deleted file mode 100644 index f44572f9..00000000 --- a/builder-frontend/src/api/auth.js +++ /dev/null @@ -1,17 +0,0 @@ -import { auth } from "../firebase/firebase.js"; - - -export async function authFetch(input, init = {}) { - const user = auth.currentUser; - - // If no user is logged in, you can handle it accordingly - if (!user) { - throw new Error("User not authenticated"); - } - - const token = await user.getIdToken(); - const headers = new Headers(init.headers || {}); - - headers.set("Authorization", `Bearer ${token}`); - return fetch(input, { ...init, headers }); -} diff --git a/builder-frontend/src/api/auth.ts b/builder-frontend/src/api/auth.ts new file mode 100644 index 00000000..e9112332 --- /dev/null +++ b/builder-frontend/src/api/auth.ts @@ -0,0 +1,36 @@ +import { auth } from "../firebase/firebase.js"; + +type RestMethod = "GET" | "PATCH" | "POST" | "PUT" | "DELETE"; + +export const authFetch = + (method: RestMethod) => async (url: string, body?: any) => { + const user = auth.currentUser; + + // If no user is logged in, you can handle it accordingly + if (!user) { + throw new Error("User not authenticated"); + } + + const token = await user.getIdToken(); + const headers = new Headers(); + headers.set("Authorization", `Bearer ${token}`); + // set headers based on method + if (method === "GET") { + headers.set("Accept", "application/json"); + } else if (method === "POST" || method === "PUT") { + headers.set("Accept", "application/json"); + headers.set("Content-Type", "application/json"); + } + + return fetch(url, { + method, + headers, + ...(body && { body: JSON.stringify(body) }), + }); + }; + +export const authGet = authFetch("GET"); +export const authPost = authFetch("POST"); +export const authPut = authFetch("PUT"); +export const authPatch = authFetch("PATCH"); +export const authDelete = authFetch("DELETE"); diff --git a/builder-frontend/src/api/benefit.ts b/builder-frontend/src/api/benefit.ts index 37e33c27..cd7568ef 100644 --- a/builder-frontend/src/api/benefit.ts +++ b/builder-frontend/src/api/benefit.ts @@ -1,5 +1,4 @@ -import { authFetch } from "@/api/auth"; -import BenefitList from "@/components/project/manageBenefits/benefitList/BenefitList"; +import { authGet, authPut } from "@/api/auth"; import { Benefit } from "@/types"; @@ -7,16 +6,11 @@ const apiUrl = import.meta.env.VITE_API_URL; export const fetchScreenerBenefit = async ( srceenerId: string, - benefitId: string + benefitId: string, ): Promise => { const url = apiUrl + "/screener/" + srceenerId + "/benefit/" + benefitId; try { - const response = await authFetch(url, { - method: "GET", - headers: { - Accept: "application/json", - }, - }); + const response = await authGet(url); if (!response.ok) { throw new Error(`Fetch failed with status: ${response.status}`); @@ -31,18 +25,11 @@ export const fetchScreenerBenefit = async ( export const updateScreenerBenefit = async ( screenerId: string, - benefitData: Benefit + benefitData: Benefit, ): Promise => { const url = apiUrl + "/screener/" + screenerId + "/benefit"; try { - const response = await authFetch(url, { - method: "PUT", - headers: { - "Content-Type": "application/json", - Accept: "application/json", - }, - body: JSON.stringify(benefitData), - }); + const response = await authPut(url, benefitData); if (!response.ok) { throw new Error(`Update failed with status: ${response.status}`); @@ -58,12 +45,7 @@ export const updateScreenerBenefit = async ( export const fetchPublicBenefits = async (): Promise => { const url = apiUrl + "/benefit"; try { - const response = await authFetch(url, { - method: "GET", - headers: { - Accept: "application/json", - }, - }); + const response = await authGet(url); if (!response.ok) { throw new Error(`Fetch failed with status: ${response.status}`); diff --git a/builder-frontend/src/api/check.ts b/builder-frontend/src/api/check.ts index f80bfa65..75dfe8b0 100644 --- a/builder-frontend/src/api/check.ts +++ b/builder-frontend/src/api/check.ts @@ -1,18 +1,18 @@ -import { authFetch } from "@/api/auth"; +import { authGet, authPatch, authPost, authPut } from "@/api/auth"; -import type { EligibilityCheck, OptionalBoolean, CreateCheckRequest, UpdateCheckRequest } from "@/types"; +import type { + EligibilityCheck, + OptionalBoolean, + CreateCheckRequest, + UpdateCheckRequest, +} from "@/types"; const apiUrl = import.meta.env.VITE_API_URL; export const fetchPublicChecks = async (): Promise => { const url = apiUrl + "/library-checks"; try { - const response = await authFetch(url, { - method: "GET", - headers: { - Accept: "application/json", - }, - }); + const response = await authGet(url); if (!response.ok) { throw new Error(`Fetch failed with status: ${response.status}`); @@ -27,7 +27,7 @@ export const fetchPublicChecks = async (): Promise => { }; export const fetchCheck = async ( - checkId: string + checkId: string, ): Promise => { let url = apiUrl + `/custom-checks/${checkId}`; if (checkId.charAt(0) === "L") { @@ -35,12 +35,7 @@ export const fetchCheck = async ( } try { - const response = await authFetch(url, { - method: "GET", - headers: { - Accept: "application/json", - }, - }); + const response = await authGet(url); if (!response.ok) { throw new Error(`Fetch failed with status: ${response.status}`); @@ -54,21 +49,16 @@ export const fetchCheck = async ( } }; -export const addCheck = async (check: CreateCheckRequest): Promise => { +export const addCheck = async ( + check: CreateCheckRequest, +): Promise => { const url = apiUrl + "/custom-checks"; try { - const response = await authFetch(url, { - method: "POST", - headers: { - "Content-Type": "application/json", - Accept: "application/json", - }, - body: JSON.stringify({ - name: check.name, - module: check.module, - description: check.description, - parameterDefinitions: check.parameterDefinitions, - }), + const response = await authPost(url, { + name: check.name, + module: check.module, + description: check.description, + parameterDefinitions: check.parameterDefinitions, }); if (!response.ok) { @@ -82,22 +72,20 @@ export const addCheck = async (check: CreateCheckRequest): Promise => { +export const updateCheck = async ( + checkId: string, + updates: UpdateCheckRequest, +): Promise => { const url = apiUrl + `/custom-checks/${checkId}`; try { // Build request body with only non-undefined fields (partial update) const body: UpdateCheckRequest = {}; - if (updates.description !== undefined) body.description = updates.description; - if (updates.parameterDefinitions !== undefined) body.parameterDefinitions = updates.parameterDefinitions; + if (updates.description !== undefined) + body.description = updates.description; + if (updates.parameterDefinitions !== undefined) + body.parameterDefinitions = updates.parameterDefinitions; - const response = await authFetch(url, { - method: "PATCH", - headers: { - "Content-Type": "application/json", - Accept: "application/json", - }, - body: JSON.stringify(body), - }); + const response = await authPatch(url, body); if (!response.ok) { throw new Error(`Update failed with status: ${response.status}`); @@ -113,14 +101,7 @@ export const updateCheck = async (checkId: string, updates: UpdateCheckRequest): export const saveCheckDmn = async (checkId: string, dmnModel: string) => { const url = apiUrl + `/custom-checks/${checkId}/dmn`; try { - const response = await authFetch(url, { - method: "PUT", - headers: { - "Content-Type": "application/json", - Accept: "application/json", - }, - body: JSON.stringify({ dmnModel: dmnModel }), - }); + const response = await authPut(url, { dmnModel: dmnModel }); if (!response.ok) { throw new Error(`DMN save failed with status: ${response.status}`); @@ -133,18 +114,11 @@ export const saveCheckDmn = async (checkId: string, dmnModel: string) => { export const validateCheckDmn = async ( checkId: string, - dmnModel: string + dmnModel: string, ): Promise => { const url = apiUrl + `/custom-checks/${checkId}/dmn/validate`; try { - const response = await authFetch(url, { - method: "POST", - headers: { - "Content-Type": "application/json", - Accept: "application/json", - }, - body: JSON.stringify({ dmnModel: dmnModel }), - }); + const response = await authPost(url, { dmnModel: dmnModel }); if (!response.ok) { throw new Error(`Validation failed with status: ${response.status}`); @@ -159,18 +133,13 @@ export const validateCheckDmn = async ( }; export const fetchUserDefinedChecks = async ( - working: boolean + working: boolean, ): Promise => { const workingQueryParam = working ? "true" : "false"; let url: string = apiUrl + `/custom-checks?working=${workingQueryParam}`; try { - const response = await authFetch(url, { - method: "GET", - headers: { - Accept: "application/json", - }, - }); + const response = await authGet(url); if (!response.ok) { throw new Error(`Fetch failed with status: ${response.status}`); @@ -186,17 +155,13 @@ export const fetchUserDefinedChecks = async ( export const evaluateWorkingCheck = async ( checkId: string, checkConfig: any, - inputData: Record + inputData: Record, ): Promise => { const url = apiUrl + `/decision/working-check?checkId=${checkId}`; try { - const response = await authFetch(url, { - method: "POST", - headers: { - "Content-Type": "application/json", - Accept: "application/json", - }, - body: JSON.stringify({ checkConfig: checkConfig, inputData: inputData }), + const response = await authPost(url, { + checkConfig: checkConfig, + inputData: inputData, }); if (!response.ok) { @@ -211,16 +176,11 @@ export const evaluateWorkingCheck = async ( }; export const getRelatedPublishedChecks = async ( - checkId: string + checkId: string, ): Promise => { const url = apiUrl + `/custom-checks/${checkId}/versions`; try { - const response = await authFetch(url, { - method: "GET", - headers: { - Accept: "application/json", - }, - }); + const response = await authGet(url); if (!response.ok) { throw new Error(`Fetch failed with status: ${response.status}`); @@ -234,16 +194,11 @@ export const getRelatedPublishedChecks = async ( }; export const publishCheck = async ( - checkId: string + checkId: string, ): Promise => { const url = apiUrl + `/custom-checks/${checkId}/publish`; try { - const response = await authFetch(url, { - method: "POST", - headers: { - Accept: "application/json", - }, - }); + const response = await authPost(url); if (!response.ok) { throw new Error(`Publish failed with status: ${response.status}`); @@ -259,12 +214,7 @@ export const publishCheck = async ( export const archiveCheck = async (checkId: string): Promise => { const url = apiUrl + `/custom-checks/${checkId}/archive`; try { - const response = await authFetch(url, { - method: "POST", - headers: { - Accept: "application/json", - }, - }); + const response = await authPost(url); if (!response.ok) { throw new Error(`Archive failed with status: ${response.status}`); diff --git a/builder-frontend/src/api/screener.ts b/builder-frontend/src/api/screener.ts index 8c3fb8f4..81a19353 100644 --- a/builder-frontend/src/api/screener.ts +++ b/builder-frontend/src/api/screener.ts @@ -1,4 +1,4 @@ -import { authFetch } from "@/api/auth"; +import { authDelete, authGet, authPost, authPut } from "@/api/auth"; import type { BenefitDetail, ScreenerResult } from "@/types"; @@ -7,12 +7,7 @@ const apiUrl = import.meta.env.VITE_API_URL; export const fetchProjects = async () => { const url = apiUrl + "/screeners"; try { - const response = await authFetch(url, { - method: "GET", - headers: { - Accept: "application/json", - }, - }); + const response = await authGet(url); if (!response.ok) { throw new Error(`Fetch failed with status: ${response.status}`); @@ -28,12 +23,7 @@ export const fetchProjects = async () => { export const fetchProject = async (screenerId) => { const url = apiUrl + "/screener/" + screenerId; try { - const response = await authFetch(url, { - method: "GET", - headers: { - Accept: "application/json", - }, - }); + const response = await authGet(url); if (!response.ok) { throw new Error(`Fetch failed with status: ${response.status}`); @@ -49,14 +39,7 @@ export const fetchProject = async (screenerId) => { export const createNewScreener = async (screenerData) => { const url = apiUrl + "/screener"; try { - const response = await authFetch(url, { - method: "POST", - headers: { - "Content-Type": "application/json", - Accept: "application/json", - }, - body: JSON.stringify(screenerData), - }); + const response = await authPost(url, screenerData); if (!response.ok) { throw new Error(`Post failed with status: ${response.status}`); @@ -72,14 +55,7 @@ export const createNewScreener = async (screenerData) => { export const updateScreener = async (screenerData) => { const url = apiUrl + "/screener"; try { - const response = await authFetch(url, { - method: "PUT", - headers: { - "Content-Type": "application/json", - Accept: "application/json", - }, - body: JSON.stringify(screenerData), - }); + const response = await authPut(url, screenerData); if (!response.ok) { throw new Error(`Update failed with status: ${response.status}`); @@ -93,13 +69,7 @@ export const updateScreener = async (screenerData) => { export const deleteScreener = async (screenerData) => { const url = apiUrl + "/screener/delete?screenerId=" + screenerData.id; try { - const response = await authFetch(url, { - method: "DELETE", - headers: { - "Content-Type": "application/json", - Accept: "application/json", - }, - }); + const response = await authDelete(url); if (!response.ok) { throw new Error(`Update failed with status: ${response.status}`); @@ -116,14 +86,7 @@ export const saveFormSchema = async (screenerId, schema) => { requestData.schema = schema; const url = apiUrl + "/save-form-schema"; try { - const response = await authFetch(url, { - method: "POST", - headers: { - "Content-Type": "application/json", - Accept: "application/json", - }, - body: JSON.stringify(requestData), - }); + const response = await authPost(url, requestData); if (!response.ok) { throw new Error(`Post failed with status: ${response.status}`); @@ -137,14 +100,7 @@ export const saveFormSchema = async (screenerId, schema) => { export const publishScreener = async (screenerId: string): Promise => { const url = apiUrl + "/publish"; try { - const response = await authFetch(url, { - method: "POST", - headers: { - "Content-Type": "application/json", - Accept: "application/json", - }, - body: JSON.stringify({ screenerId: screenerId }), - }); + const response = await authPost(url, { screenerId: screenerId }); if (!response.ok) { throw new Error(`Submit failed with status: ${response.status}`); @@ -155,17 +111,13 @@ export const publishScreener = async (screenerId: string): Promise => { } }; -export const addCustomBenefit = async (screenerId: string, benefit: BenefitDetail) => { +export const addCustomBenefit = async ( + screenerId: string, + benefit: BenefitDetail, +) => { const url = apiUrl + "/screener/" + screenerId + "/benefit"; try { - const response = await authFetch(url, { - method: "POST", - headers: { - "Content-Type": "application/json", - Accept: "application/json", - }, - body: JSON.stringify(benefit), - }); + const response = await authPost(url, benefit); if (!response.ok) { throw new Error(`Create benefit failed with status: ${response.status}`); @@ -176,19 +128,18 @@ export const addCustomBenefit = async (screenerId: string, benefit: BenefitDetai } }; -export const removeCustomBenefit = async (screenerId: string, benefitId: string) => { +export const removeCustomBenefit = async ( + screenerId: string, + benefitId: string, +) => { const url = apiUrl + "/screener/" + screenerId + "/benefit/" + benefitId; try { - const response = await authFetch(url, { - method: "DELETE", - headers: { - "Content-Type": "application/json", - Accept: "application/json", - }, - }); + const response = await authDelete(url); if (!response.ok) { - throw new Error(`Delete of benefit failed with status: ${response.status}`); + throw new Error( + `Delete of benefit failed with status: ${response.status}`, + ); } } catch (error) { console.error("Error deleting custom benefit:", error); @@ -196,22 +147,18 @@ export const removeCustomBenefit = async (screenerId: string, benefitId: string) } }; -export const evaluateScreener = async (screenerId: string, inputData: any): Promise => { +export const evaluateScreener = async ( + screenerId: string, + inputData: any, +): Promise => { const url = apiUrl + "/decision/v2?screenerId=" + screenerId; try { - const response = await authFetch(url, { - method: "POST", - headers: { - "Content-Type": "application/json", - Accept: "application/json", - }, - body: JSON.stringify(inputData), - }); + const response = await authPost(url, inputData); if (!response.ok) { throw new Error(`Evaluation failed with status: ${response.status}`); } - + const data = await response.json(); return data; } catch (error) {