Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 0 additions & 17 deletions builder-frontend/src/api/auth.js

This file was deleted.

36 changes: 36 additions & 0 deletions builder-frontend/src/api/auth.ts
Original file line number Diff line number Diff line change
@@ -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");
30 changes: 6 additions & 24 deletions builder-frontend/src/api/benefit.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,16 @@
import { authFetch } from "@/api/auth";
import BenefitList from "@/components/project/manageBenefits/benefitList/BenefitList";
import { authGet, authPut } from "@/api/auth";

import { Benefit } from "@/types";

const apiUrl = import.meta.env.VITE_API_URL;

export const fetchScreenerBenefit = async (
srceenerId: string,
benefitId: string
benefitId: string,
): Promise<Benefit> => {
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}`);
Expand All @@ -31,18 +25,11 @@ export const fetchScreenerBenefit = async (

export const updateScreenerBenefit = async (
screenerId: string,
benefitData: Benefit
benefitData: Benefit,
): Promise<Benefit> => {
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}`);
Expand All @@ -58,12 +45,7 @@ export const updateScreenerBenefit = async (
export const fetchPublicBenefits = async (): Promise<Benefit[]> => {
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}`);
Expand Down
132 changes: 41 additions & 91 deletions builder-frontend/src/api/check.ts
Original file line number Diff line number Diff line change
@@ -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<EligibilityCheck[]> => {
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}`);
Expand All @@ -27,20 +27,15 @@ export const fetchPublicChecks = async (): Promise<EligibilityCheck[]> => {
};

export const fetchCheck = async (
checkId: string
checkId: string,
): Promise<EligibilityCheck> => {
let url = apiUrl + `/custom-checks/${checkId}`;
if (checkId.charAt(0) === "L") {
url = apiUrl + `/library-checks/${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}`);
Expand All @@ -54,21 +49,16 @@ export const fetchCheck = async (
}
};

export const addCheck = async (check: CreateCheckRequest): Promise<EligibilityCheck> => {
export const addCheck = async (
check: CreateCheckRequest,
): Promise<EligibilityCheck> => {
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) {
Expand All @@ -82,22 +72,20 @@ export const addCheck = async (check: CreateCheckRequest): Promise<EligibilityCh
}
};

export const updateCheck = async (checkId: string, updates: UpdateCheckRequest): Promise<EligibilityCheck> => {
export const updateCheck = async (
checkId: string,
updates: UpdateCheckRequest,
): Promise<EligibilityCheck> => {
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}`);
Expand All @@ -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}`);
Expand All @@ -133,18 +114,11 @@ export const saveCheckDmn = async (checkId: string, dmnModel: string) => {

export const validateCheckDmn = async (
checkId: string,
dmnModel: string
dmnModel: string,
): Promise<string[]> => {
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}`);
Expand All @@ -159,18 +133,13 @@ export const validateCheckDmn = async (
};

export const fetchUserDefinedChecks = async (
working: boolean
working: boolean,
): Promise<EligibilityCheck[]> => {
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}`);
Expand All @@ -186,17 +155,13 @@ export const fetchUserDefinedChecks = async (
export const evaluateWorkingCheck = async (
checkId: string,
checkConfig: any,
inputData: Record<string, any>
inputData: Record<string, any>,
): Promise<OptionalBoolean> => {
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) {
Expand All @@ -211,16 +176,11 @@ export const evaluateWorkingCheck = async (
};

export const getRelatedPublishedChecks = async (
checkId: string
checkId: string,
): Promise<EligibilityCheck[]> => {
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}`);
Expand All @@ -234,16 +194,11 @@ export const getRelatedPublishedChecks = async (
};

export const publishCheck = async (
checkId: string
checkId: string,
): Promise<OptionalBoolean> => {
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}`);
Expand All @@ -259,12 +214,7 @@ export const publishCheck = async (
export const archiveCheck = async (checkId: string): Promise<void> => {
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}`);
Expand Down
Loading
Loading