Skip to content

Commit

Permalink
refactor: extracted resource from endpoint url
Browse files Browse the repository at this point in the history
  • Loading branch information
AlbertoBaroso committed Jun 3, 2024
1 parent 47d71e8 commit 1b7e588
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 12 deletions.
7 changes: 4 additions & 3 deletions frontend/src/services/API.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type HttpRequestOptions = {

export class Api {
private static getUrl = (endpoint: string) =>
`${process.env.API_ENDPOINT}/${endpoint}/`;
`${process.env.REACT_APP_API_ENDPOINT}/v1/${endpoint}`;

static async httpRequest(
endpoint: string,
Expand All @@ -46,14 +46,15 @@ export class Api {
if (options.body) init["body"] = JSON.stringify(options.body);
/* Send the request */
response = await fetch(new URL(this.getUrl(endpoint)), init);
} catch {
} catch (error) {
console.log(error);
throw new Error("Something went wrong, try again later.");
}

return await this.handleResponse(response);
}

static async get(endpoint: string, authRequired: boolean = false) {
static async get(endpoint: string, authRequired: boolean = true) {
return Api.httpRequest(endpoint, "GET", { authRequired });
}

Expand Down
4 changes: 3 additions & 1 deletion frontend/src/services/applications.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { Api } from "./api";

const resource = "applications";

const applications = {
getApplications: async () => {
return await Api.get("applications");
return await Api.get(`${resource}`);
},
};

Expand Down
4 changes: 3 additions & 1 deletion frontend/src/services/availabilities.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { Api } from "./api";

const resource = "availabilities";

const availabilities = {
getAvailabilities: async () => {
return await Api.get("availabilities");
return await Api.get(`${resource}`);
},
};

Expand Down
6 changes: 4 additions & 2 deletions frontend/src/services/interviews.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { Api } from "./api";

const resource = "interviews";

const interviews = {
getInterviewsByDates: async (startDate: string, endDate: string) => {
return await Api.get(
`interviews?startDate=${startDate}&endDate=${endDate}}`
`${resource}?startDate=${startDate}&endDate=${endDate}}`
);
},
getInterviewsByDate: async (date: string) => {
return await Api.get(`interviews?date=${date}`);
return await Api.get(`${resource}?date=${date}`);
},
};

Expand Down
4 changes: 3 additions & 1 deletion frontend/src/services/recruitmentSessions.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { Api } from "./api";

const resource = "recruitment-session";

const recruitmentSessions = {
getActive: async () => {
return await Api.get("recruitmentSessions");
return await Api.get(`${resource}`);
},
};

Expand Down
4 changes: 3 additions & 1 deletion frontend/src/services/timeslots.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { Api } from "./api";

const resource = "timeslots";

const timeslots = {
getApplicants: async () => {
return await Api.get("timeslots");
return await Api.get(`${resource}`);
},
};

Expand Down
4 changes: 3 additions & 1 deletion frontend/src/services/users.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { Api } from "./api";

const resource = "users";

const users = {
getUsers: async () => {
return await Api.get("users");
return await Api.get(`${resource}`);
},
};

Expand Down
6 changes: 4 additions & 2 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 1b7e588

Please sign in to comment.