Skip to content

Commit

Permalink
Has all routes in src/api/emergencies.ts depend on .env file
Browse files Browse the repository at this point in the history
  • Loading branch information
r800360 committed Jun 4, 2024
1 parent 0b0b946 commit c3db5d9
Showing 1 changed file with 26 additions and 5 deletions.
31 changes: 26 additions & 5 deletions admin-portal-frontend/src/app/api/emergencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,13 @@ export async function createEmergency(
emergency: CreateEmergencyRequest,
): Promise<APIResult<Emergency>> {
try {
const response = await post("/api/emergencies", emergency);
if (!process.env.API_URL) {
throw new Error("API URL is not defined");
}

const url = `${process.env.API_URL}/emergencies`;

const response = await post(url, emergency);
const json = (await response.json()) as Emergency;
return { success: true, data: json };
} catch (error) {
Expand All @@ -75,7 +81,13 @@ export async function createEmergency(

export async function getEmergency(id: string): Promise<APIResult<Emergency>> {
try {
const response = await get(`/api/emergencies/${id}`);
if (!process.env.API_URL) {
throw new Error("API URL is not defined");
}

const url = `${process.env.API_URL}/emergencies/${id}`;

const response = await get(url);
const json = (await response.json()) as Emergency;
return { success: true, data: json };
} catch (error) {
Expand All @@ -85,7 +97,12 @@ export async function getEmergency(id: string): Promise<APIResult<Emergency>> {

export async function getAllEmergencies(): Promise<APIResult<Emergency[]>> {
try {
const response = await get(`/api/emergencies/`);
if (!process.env.API_URL) {
throw new Error("API URL is not defined");
}

const url = `${process.env.API_URL}/emergencies`;
const response = await get(url);
const json = (await response.json()) as Emergency[];
// const parsedJson = json.map((element) => (element));
return { success: true, data: json };
Expand All @@ -99,8 +116,12 @@ export async function updateEmergency(
emergency: UpdateEmergencyRequest,
): Promise<APIResult<Emergency>> {
try {
// your code here
const response = await put(`/api/emergencies/${emergency._id}`, emergency);
if (!process.env.API_URL) {
throw new Error("API URL is not defined");
}

const url = `${process.env.API_URL}/emergencies/${emergency._id}`;
const response = await put(url, emergency);
const json = (await response.json()) as Emergency;
return { success: true, data: json };
} catch (error) {
Expand Down

0 comments on commit c3db5d9

Please sign in to comment.