From 4177d82351f4fac01647798323fc4e2d29b6dd7e Mon Sep 17 00:00:00 2001 From: joshwanf <17016446+joshwanf@users.noreply.github.com> Date: Fri, 19 Dec 2025 14:14:15 -0500 Subject: [PATCH] Curried authFetch into authGet, authPost, authPut, authDelete, set headers and stringify body in authFetch --- builder-frontend/src/api/auth.ts | 40 +++++++++---- builder-frontend/src/api/benefit.ts | 26 ++------ builder-frontend/src/api/check.ts | 89 ++++------------------------ builder-frontend/src/api/screener.ts | 86 ++++----------------------- 4 files changed, 56 insertions(+), 185 deletions(-) diff --git a/builder-frontend/src/api/auth.ts b/builder-frontend/src/api/auth.ts index f44572f9..7a77990c 100644 --- a/builder-frontend/src/api/auth.ts +++ b/builder-frontend/src/api/auth.ts @@ -1,17 +1,35 @@ import { auth } from "../firebase/firebase.js"; +type RestMethod = "GET" | "POST" | "PUT" | "DELETE"; -export async function authFetch(input, init = {}) { - const user = auth.currentUser; +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"); - } + // 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 || {}); + 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"); + } - headers.set("Authorization", `Bearer ${token}`); - return fetch(input, { ...init, headers }); -} + 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 authDelete = authFetch("DELETE"); diff --git a/builder-frontend/src/api/benefit.ts b/builder-frontend/src/api/benefit.ts index 1f40f8ef..772da087 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 { authFetch, authGet, authPost, authPut } from "@/api/auth"; import { Benefit } from "@/types"; @@ -9,12 +8,7 @@ export const fetchScreenerBenefit = async ( ): Promise => { const url = `/api/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}`); @@ -33,14 +27,7 @@ export const updateScreenerBenefit = async ( ): Promise => { const url = `/api/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}`); @@ -56,12 +43,7 @@ export const updateScreenerBenefit = async ( export const fetchPublicBenefits = async (): Promise => { const url = "/api/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 a53cebf6..666842fd 100644 --- a/builder-frontend/src/api/check.ts +++ b/builder-frontend/src/api/check.ts @@ -1,16 +1,11 @@ -import { authFetch } from "@/api/auth"; +import { authFetch, authGet, authPost, authPut } from "@/api/auth"; import type { EligibilityCheck, OptionalBoolean } from "@/types"; export const fetchPublicChecks = async (): Promise => { const url = "/api/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}`); @@ -31,12 +26,7 @@ export const fetchCheck = async ( const url = `/api/${checkResource}/${checkId}`; 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}`); @@ -53,14 +43,7 @@ export const fetchCheck = async ( export const addCheck = async (check: EligibilityCheck) => { const url = "/api/custom-checks"; try { - const response = await authFetch(url, { - method: "POST", - headers: { - "Content-Type": "application/json", - Accept: "application/json", - }, - body: JSON.stringify(check), - }); + const response = await authPost(url, check); if (!response.ok) { throw new Error(`Post failed with status: ${response.status}`); @@ -76,14 +59,7 @@ export const addCheck = async (check: EligibilityCheck) => { export const updateCheck = async (check: EligibilityCheck) => { const url = "/api/custom-checks"; try { - const response = await authFetch(url, { - method: "PUT", - headers: { - "Content-Type": "application/json", - Accept: "application/json", - }, - body: JSON.stringify(check), - }); + const response = await authPut(url, check); if (!response.ok) { throw new Error(`Update failed with status: ${response.status}`); @@ -99,14 +75,7 @@ export const updateCheck = async (check: EligibilityCheck) => { export const saveCheckDmn = async (checkId: string, dmnModel: string) => { const url = "/api/save-check-dmn"; try { - const response = await authFetch(url, { - method: "POST", - headers: { - "Content-Type": "application/json", - Accept: "application/json", - }, - body: JSON.stringify({ id: checkId, dmnModel: dmnModel }), - }); + const response = await authPost(url, { id: checkId, dmnModel }); if (!response.ok) { throw new Error(`DMN save failed with status: ${response.status}`); @@ -123,14 +92,7 @@ export const validateCheckDmn = async ( ): Promise => { const url = "/api/validate-check-dmn"; try { - const response = await authFetch(url, { - method: "POST", - headers: { - "Content-Type": "application/json", - Accept: "application/json", - }, - body: JSON.stringify({ id: checkId, dmnModel: dmnModel }), - }); + const response = await authPost(url, { id: checkId, dmnModel }); if (!response.ok) { throw new Error(`Validation failed with status: ${response.status}`); @@ -150,12 +112,7 @@ export const fetchUserDefinedChecks = async ( const url = `/api/custom-checks?working=${working}`; 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}`); @@ -175,14 +132,7 @@ export const evaluateWorkingCheck = async ( ): Promise => { const url = `/api/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, inputData }); if (!response.ok) { throw new Error(`Test check failed with status: ${response.status}`); @@ -200,12 +150,7 @@ export const getRelatedPublishedChecks = async ( ): Promise => { const url = `/api/custom-checks/${checkId}/published-check-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}`); @@ -223,12 +168,7 @@ export const publishCheck = async ( ): Promise => { const url = `/api/publish-check/${checkId}`; 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}`); @@ -244,12 +184,7 @@ export const publishCheck = async ( export const archiveCheck = async (checkId: string): Promise => { const url = `/api/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 fdebd13f..cf41f13f 100644 --- a/builder-frontend/src/api/screener.ts +++ b/builder-frontend/src/api/screener.ts @@ -1,16 +1,11 @@ -import { authFetch } from "@/api/auth"; +import { authDelete, authFetch, authGet, authPost, authPut } from "@/api/auth"; import type { BenefitDetail, ScreenerResult } from "@/types"; export const fetchProjects = async () => { const url = "/api/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}`); @@ -26,12 +21,7 @@ export const fetchProjects = async () => { export const fetchProject = async (screenerId: string) => { const url = `/api/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}`); @@ -47,14 +37,7 @@ export const fetchProject = async (screenerId: string) => { export const createNewScreener = async (screenerData) => { const url = "/api/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}`); @@ -70,14 +53,7 @@ export const createNewScreener = async (screenerData) => { export const updateScreener = async (screenerData) => { const url = "/api/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}`); @@ -91,13 +67,7 @@ export const updateScreener = async (screenerData) => { export const deleteScreener = async (screenerData) => { const url = "/api/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}`); @@ -114,14 +84,7 @@ export const saveFormSchema = async (screenerId, schema) => { requestData.schema = schema; const url = "/api/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}`); @@ -135,14 +98,7 @@ export const saveFormSchema = async (screenerId, schema) => { export const publishScreener = async (screenerId: string): Promise => { const url = "/api/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 }); if (!response.ok) { throw new Error(`Submit failed with status: ${response.status}`); @@ -159,14 +115,7 @@ export const addCustomBenefit = async ( ) => { const url = `/api/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}`); @@ -183,13 +132,7 @@ export const removeCustomBenefit = async ( ) => { const url = `/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( @@ -208,14 +151,7 @@ export const evaluateScreener = async ( ): Promise => { const url = `/api/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}`);