Skip to content

Commit bb7fe22

Browse files
Merge pull request #236 from CodeForPhilly/refactor-authFetch
2 parents afc9421 + 193d914 commit bb7fe22

File tree

7 files changed

+70
-56
lines changed

7 files changed

+70
-56
lines changed

builder-frontend/.env.example

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
VITE_API_KEY=AIzaSyDummyKeyForEmulator
2-
VITE_API_URL=http://localhost:8081/api
2+
VITE_API_URL=http://localhost:8081/
33
VITE_APP_ID=1:123456789:web:abcdef
44
VITE_AUTH_DOMAIN=localhost:9099
55
VITE_MEASUREMENT_ID=G-XXXXXXXXXX
66
VITE_MESSAGING_SENDER_ID=123456789
77
VITE_PROJECT_ID=demo-bdt-dev
8-
VITE_SCREENER_BASE_URL=http://localhost:5174/
98
VITE_STORAGE_BUCKET=demo-bdt-dev.appspot.com
109
DEV_SERVER_PORT=5173

builder-frontend/src/api/benefit.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,11 @@ import BenefitList from "@/components/project/manageBenefits/benefitList/Benefit
33

44
import { Benefit } from "@/types";
55

6-
const apiUrl = import.meta.env.VITE_API_URL;
7-
86
export const fetchScreenerBenefit = async (
97
srceenerId: string,
108
benefitId: string
119
): Promise<Benefit> => {
12-
const url = apiUrl + "/screener/" + srceenerId + "/benefit/" + benefitId;
10+
const url = `/api/screener/${srceenerId}/benefit/${benefitId}`;
1311
try {
1412
const response = await authFetch(url, {
1513
method: "GET",
@@ -33,7 +31,7 @@ export const updateScreenerBenefit = async (
3331
screenerId: string,
3432
benefitData: Benefit
3533
): Promise<Benefit> => {
36-
const url = apiUrl + "/screener/" + screenerId + "/benefit";
34+
const url = `/api/screener/${screenerId}/benefit`;
3735
try {
3836
const response = await authFetch(url, {
3937
method: "PUT",
@@ -56,7 +54,7 @@ export const updateScreenerBenefit = async (
5654
};
5755

5856
export const fetchPublicBenefits = async (): Promise<Benefit[]> => {
59-
const url = apiUrl + "/benefit";
57+
const url = "/api/benefit";
6058
try {
6159
const response = await authFetch(url, {
6260
method: "GET",

builder-frontend/src/api/check.ts

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@ import { authFetch } from "@/api/auth";
22

33
import type { EligibilityCheck, OptionalBoolean } from "@/types";
44

5-
const apiUrl = import.meta.env.VITE_API_URL;
6-
75
export const fetchPublicChecks = async (): Promise<EligibilityCheck[]> => {
8-
const url = apiUrl + "/library-checks";
6+
const url = "/api/library-checks";
97
try {
108
const response = await authFetch(url, {
119
method: "GET",
@@ -29,10 +27,8 @@ export const fetchPublicChecks = async (): Promise<EligibilityCheck[]> => {
2927
export const fetchCheck = async (
3028
checkId: string
3129
): Promise<EligibilityCheck> => {
32-
let url = apiUrl + `/custom-checks/${checkId}`;
33-
if (checkId.charAt(0) === "L") {
34-
url = apiUrl + `/library-checks/${checkId}`;
35-
}
30+
const checkResource = checkId[0] === "L" ? "library-checks" : "custom-checks";
31+
const url = `/api/${checkResource}/${checkId}`;
3632

3733
try {
3834
const response = await authFetch(url, {
@@ -55,7 +51,7 @@ export const fetchCheck = async (
5551
};
5652

5753
export const addCheck = async (check: EligibilityCheck) => {
58-
const url = apiUrl + "/custom-checks";
54+
const url = "/api/custom-checks";
5955
try {
6056
const response = await authFetch(url, {
6157
method: "POST",
@@ -78,7 +74,7 @@ export const addCheck = async (check: EligibilityCheck) => {
7874
};
7975

8076
export const updateCheck = async (check: EligibilityCheck) => {
81-
const url = apiUrl + "/custom-checks";
77+
const url = "/api/custom-checks";
8278
try {
8379
const response = await authFetch(url, {
8480
method: "PUT",
@@ -101,7 +97,7 @@ export const updateCheck = async (check: EligibilityCheck) => {
10197
};
10298

10399
export const saveCheckDmn = async (checkId: string, dmnModel: string) => {
104-
const url = apiUrl + "/save-check-dmn";
100+
const url = "/api/save-check-dmn";
105101
try {
106102
const response = await authFetch(url, {
107103
method: "POST",
@@ -125,7 +121,7 @@ export const validateCheckDmn = async (
125121
checkId: string,
126122
dmnModel: string
127123
): Promise<string[]> => {
128-
const url = apiUrl + "/validate-check-dmn";
124+
const url = "/api/validate-check-dmn";
129125
try {
130126
const response = await authFetch(url, {
131127
method: "POST",
@@ -151,8 +147,7 @@ export const validateCheckDmn = async (
151147
export const fetchUserDefinedChecks = async (
152148
working: boolean
153149
): Promise<EligibilityCheck[]> => {
154-
const workingQueryParam = working ? "true" : "false";
155-
let url: string = apiUrl + `/custom-checks?working=${workingQueryParam}`;
150+
const url = `/api/custom-checks?working=${working}`;
156151

157152
try {
158153
const response = await authFetch(url, {
@@ -178,7 +173,7 @@ export const evaluateWorkingCheck = async (
178173
checkConfig: any,
179174
inputData: Record<string, any>
180175
): Promise<OptionalBoolean> => {
181-
const url = apiUrl + `/decision/working-check?checkId=${checkId}`;
176+
const url = `/api/decision/working-check?checkId=${checkId}`;
182177
try {
183178
const response = await authFetch(url, {
184179
method: "POST",
@@ -203,7 +198,7 @@ export const evaluateWorkingCheck = async (
203198
export const getRelatedPublishedChecks = async (
204199
checkId: string
205200
): Promise<EligibilityCheck[]> => {
206-
const url = apiUrl + `/custom-checks/${checkId}/published-check-versions`;
201+
const url = `/api/custom-checks/${checkId}/published-check-versions`;
207202
try {
208203
const response = await authFetch(url, {
209204
method: "GET",
@@ -226,7 +221,7 @@ export const getRelatedPublishedChecks = async (
226221
export const publishCheck = async (
227222
checkId: string
228223
): Promise<OptionalBoolean> => {
229-
const url = apiUrl + `/publish-check/${checkId}`;
224+
const url = `/api/publish-check/${checkId}`;
230225
try {
231226
const response = await authFetch(url, {
232227
method: "POST",
@@ -247,7 +242,7 @@ export const publishCheck = async (
247242
};
248243

249244
export const archiveCheck = async (checkId: string): Promise<void> => {
250-
const url = apiUrl + `/custom-checks/${checkId}/archive`;
245+
const url = `/api/custom-checks/${checkId}/archive`;
251246
try {
252247
const response = await authFetch(url, {
253248
method: "POST",

builder-frontend/src/api/publishedScreener.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import type { PublishedScreener, ScreenerResult } from "@/types";
22

3-
const apiUrl = import.meta.env.VITE_API_URL;
4-
5-
export const fetchPublishedScreener = async (publishedScreenerId: string): Promise<PublishedScreener> => {
6-
const url = apiUrl + "/published/screener/" + publishedScreenerId;
3+
export const fetchPublishedScreener = async (
4+
publishedScreenerId: string
5+
): Promise<PublishedScreener> => {
6+
const url = `/api/published/screener/${publishedScreenerId}`;
77
try {
88
const response = await fetch(url, {
99
method: "GET",
@@ -23,9 +23,11 @@ export const fetchPublishedScreener = async (publishedScreenerId: string): Promi
2323
}
2424
};
2525

26-
27-
export const evaluatePublishedScreener = async (publishedScreenerId: string, inputData: any): Promise<ScreenerResult> => {
28-
const url = apiUrl + "/published/" + publishedScreenerId + "/evaluate";
26+
export const evaluatePublishedScreener = async (
27+
publishedScreenerId: string,
28+
inputData: any
29+
): Promise<ScreenerResult> => {
30+
const url = `/published/${publishedScreenerId}/evaluate`;
2931
try {
3032
const response = await fetch(url, {
3133
method: "POST",

builder-frontend/src/api/screener.ts

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@ import { authFetch } from "@/api/auth";
22

33
import type { BenefitDetail, ScreenerResult } from "@/types";
44

5-
const apiUrl = import.meta.env.VITE_API_URL;
6-
75
export const fetchProjects = async () => {
8-
const url = apiUrl + "/screeners";
6+
const url = "/api/screeners";
97
try {
108
const response = await authFetch(url, {
119
method: "GET",
@@ -25,8 +23,8 @@ export const fetchProjects = async () => {
2523
}
2624
};
2725

28-
export const fetchProject = async (screenerId) => {
29-
const url = apiUrl + "/screener/" + screenerId;
26+
export const fetchProject = async (screenerId: string) => {
27+
const url = `/api/screener/${screenerId}`;
3028
try {
3129
const response = await authFetch(url, {
3230
method: "GET",
@@ -47,7 +45,7 @@ export const fetchProject = async (screenerId) => {
4745
};
4846

4947
export const createNewScreener = async (screenerData) => {
50-
const url = apiUrl + "/screener";
48+
const url = "/api/screener";
5149
try {
5250
const response = await authFetch(url, {
5351
method: "POST",
@@ -70,7 +68,7 @@ export const createNewScreener = async (screenerData) => {
7068
};
7169

7270
export const updateScreener = async (screenerData) => {
73-
const url = apiUrl + "/screener";
71+
const url = "/api/screener";
7472
try {
7573
const response = await authFetch(url, {
7674
method: "PUT",
@@ -91,7 +89,7 @@ export const updateScreener = async (screenerData) => {
9189
};
9290

9391
export const deleteScreener = async (screenerData) => {
94-
const url = apiUrl + "/screener/delete?screenerId=" + screenerData.id;
92+
const url = "/api/screener/delete?screenerId=" + screenerData.id;
9593
try {
9694
const response = await authFetch(url, {
9795
method: "DELETE",
@@ -114,7 +112,7 @@ export const saveFormSchema = async (screenerId, schema) => {
114112
const requestData: any = {};
115113
requestData.screenerId = screenerId;
116114
requestData.schema = schema;
117-
const url = apiUrl + "/save-form-schema";
115+
const url = "/api/save-form-schema";
118116
try {
119117
const response = await authFetch(url, {
120118
method: "POST",
@@ -135,7 +133,7 @@ export const saveFormSchema = async (screenerId, schema) => {
135133
};
136134

137135
export const publishScreener = async (screenerId: string): Promise<void> => {
138-
const url = apiUrl + "/publish";
136+
const url = "/api/publish";
139137
try {
140138
const response = await authFetch(url, {
141139
method: "POST",
@@ -155,8 +153,11 @@ export const publishScreener = async (screenerId: string): Promise<void> => {
155153
}
156154
};
157155

158-
export const addCustomBenefit = async (screenerId: string, benefit: BenefitDetail) => {
159-
const url = apiUrl + "/screener/" + screenerId + "/benefit";
156+
export const addCustomBenefit = async (
157+
screenerId: string,
158+
benefit: BenefitDetail
159+
) => {
160+
const url = `/api/screener/${screenerId}/benefit`;
160161
try {
161162
const response = await authFetch(url, {
162163
method: "POST",
@@ -176,8 +177,11 @@ export const addCustomBenefit = async (screenerId: string, benefit: BenefitDetai
176177
}
177178
};
178179

179-
export const removeCustomBenefit = async (screenerId: string, benefitId: string) => {
180-
const url = apiUrl + "/screener/" + screenerId + "/benefit/" + benefitId;
180+
export const removeCustomBenefit = async (
181+
screenerId: string,
182+
benefitId: string
183+
) => {
184+
const url = `/screener/${screenerId}/benefit/${benefitId}`;
181185
try {
182186
const response = await authFetch(url, {
183187
method: "DELETE",
@@ -188,16 +192,21 @@ export const removeCustomBenefit = async (screenerId: string, benefitId: string)
188192
});
189193

190194
if (!response.ok) {
191-
throw new Error(`Delete of benefit failed with status: ${response.status}`);
195+
throw new Error(
196+
`Delete of benefit failed with status: ${response.status}`
197+
);
192198
}
193199
} catch (error) {
194200
console.error("Error deleting custom benefit:", error);
195201
throw error;
196202
}
197203
};
198204

199-
export const evaluateScreener = async (screenerId: string, inputData: any): Promise<ScreenerResult> => {
200-
const url = apiUrl + "/decision/v2?screenerId=" + screenerId;
205+
export const evaluateScreener = async (
206+
screenerId: string,
207+
inputData: any
208+
): Promise<ScreenerResult> => {
209+
const url = `/api/decision/v2?screenerId=${screenerId}`;
201210
try {
202211
const response = await authFetch(url, {
203212
method: "POST",
@@ -211,7 +220,7 @@ export const evaluateScreener = async (screenerId: string, inputData: any): Prom
211220
if (!response.ok) {
212221
throw new Error(`Evaluation failed with status: ${response.status}`);
213222
}
214-
223+
215224
const data = await response.json();
216225
return data;
217226
} catch (error) {

builder-frontend/vite.config.js

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,21 @@
1-
import { defineConfig } from "vite";
1+
import { defineConfig, loadEnv } from "vite";
22
import solid from "vite-plugin-solid";
3-
import tsconfigPaths from 'vite-tsconfig-paths'
3+
import tsconfigPaths from "vite-tsconfig-paths";
44

5-
export default defineConfig({
6-
plugins: [solid(), tsconfigPaths()],
7-
server: {
8-
port: process.env.DEV_SERVER_PORT || 5173
9-
}
5+
export default defineConfig(({ mode }) => {
6+
// Load variables from .env before vite.config.js finishes running
7+
const env = loadEnv(mode, process.cwd(), "");
8+
return {
9+
plugins: [solid(), tsconfigPaths()],
10+
server: {
11+
port: env.DEV_SERVER_PORT || 5173,
12+
// Proxy to connect to backend
13+
// https://vite.dev/config/server-options#server-proxy
14+
proxy: {
15+
"/api": {
16+
target: env.VITE_API_URL || "http://localhost:8081",
17+
},
18+
},
19+
},
20+
};
1021
});

0 commit comments

Comments
 (0)