Skip to content
Open
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
40 changes: 29 additions & 11 deletions builder-frontend/src/api/auth.ts
Original file line number Diff line number Diff line change
@@ -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");
26 changes: 4 additions & 22 deletions builder-frontend/src/api/benefit.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand All @@ -9,12 +8,7 @@ export const fetchScreenerBenefit = async (
): Promise<Benefit> => {
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}`);
Expand All @@ -33,14 +27,7 @@ export const updateScreenerBenefit = async (
): Promise<Benefit> => {
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}`);
Expand All @@ -56,12 +43,7 @@ export const updateScreenerBenefit = async (
export const fetchPublicBenefits = async (): Promise<Benefit[]> => {
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}`);
Expand Down
89 changes: 12 additions & 77 deletions builder-frontend/src/api/check.ts
Original file line number Diff line number Diff line change
@@ -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<EligibilityCheck[]> => {
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}`);
Expand All @@ -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}`);
Expand All @@ -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}`);
Expand All @@ -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}`);
Expand All @@ -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}`);
Expand All @@ -123,14 +92,7 @@ export const validateCheckDmn = async (
): Promise<string[]> => {
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}`);
Expand All @@ -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}`);
Expand All @@ -175,14 +132,7 @@ export const evaluateWorkingCheck = async (
): Promise<OptionalBoolean> => {
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}`);
Expand All @@ -200,12 +150,7 @@ export const getRelatedPublishedChecks = async (
): Promise<EligibilityCheck[]> => {
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}`);
Expand All @@ -223,12 +168,7 @@ export const publishCheck = async (
): Promise<OptionalBoolean> => {
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}`);
Expand All @@ -244,12 +184,7 @@ export const publishCheck = async (
export const archiveCheck = async (checkId: string): Promise<void> => {
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}`);
Expand Down
Loading