Skip to content

Commit

Permalink
feat: add services for handling request to API
Browse files Browse the repository at this point in the history
TODO: The endpoints for getting list of items needs to be
updated after RSN-57. Addtionally, the structures for events
endpoints should also be uploaded to be compliant with current
state.
  • Loading branch information
raczu committed Jun 22, 2024
1 parent d04da8a commit 8cac908
Show file tree
Hide file tree
Showing 10 changed files with 492 additions and 46 deletions.
28 changes: 18 additions & 10 deletions Client/reasn-client/apps/web/lib/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,15 @@ export type ProblemDetails = {
instance?: string;
};

export type RequestOptions = {
method: HttpMethod;
body?: Object | FormData;
authRequired?: boolean;
};

export const sendRequest = async <T>(
url: string,
httpMethod: HttpMethod,
body: Object = {},
authRequired: boolean = false,
url: string | URL,
{ method, body = {}, authRequired = false }: RequestOptions,
): Promise<T> => {
try {
let headers: HeadersInit = new Headers();
Expand All @@ -37,13 +41,17 @@ export const sendRequest = async <T>(
}

const fetchOptions: RequestInit = {
method: httpMethod,
method: method,
headers,
};

if (httpMethod == HttpMethod.POST || httpMethod == HttpMethod.PUT) {
headers.set("Content-Type", "application/json");
fetchOptions.body = JSON.stringify(body);
if (method == HttpMethod.POST || method == HttpMethod.PUT) {
if (body instanceof FormData) {
fetchOptions.body = body;
} else {
headers.set("Content-Type", "application/json");
fetchOptions.body = JSON.stringify(body);
}
}

const response = await fetch(url, fetchOptions);
Expand All @@ -61,10 +69,10 @@ export const sendRequest = async <T>(
);
}

return (await response.json()) as T;
return response.json().catch(() => {}) as T;
} catch (error) {
console.error(
`Error while sending request to ${url} with method ${httpMethod}: ${error}`,
`Error while sending request to ${url} with method ${method}: ${error}`,
);
throw Error;
}
Expand Down
3 changes: 2 additions & 1 deletion Client/reasn-client/apps/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@
"dependencies": {
"@reasn/common": "*",
"@reasn/ui": "*",
"@types/url-search-params": "^1.1.2",
"jwt-decode": "^4.0.0",
"next": "^14.0.4",
"next": "^14.1.1",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-native-web": "^0.19.10"
Expand Down
34 changes: 34 additions & 0 deletions Client/reasn-client/apps/web/services/auth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { sendRequest, HttpMethod } from "@/lib/request";
import {
TokenPayload,
TokenPayloadMapper,
} from "@reasn/common/src/schemas/TokenPayload";
import { LoginRequest } from "@reasn/common/src/schemas/LoginRequest";
import { RegisterRequest } from "@reasn/common/src/schemas/RegisterRequest";
import { UserDto, UserDtoMapper } from "@reasn/common/src/schemas/UserDto";

const baseUrl = `${process.env.REASN_API_URL}/api/v1/auth`;

export const login = async (
loginRequest: LoginRequest,
): Promise<TokenPayload> => {
const url = new URL(`${baseUrl}/login`);

const response = await sendRequest<TokenPayload>(url, {
method: HttpMethod.POST,
body: loginRequest,
});
return TokenPayloadMapper.fromObject(response);
};

export const register = async (
registerRequest: RegisterRequest,
): Promise<UserDto> => {
const url = new URL(`${baseUrl}/register`);

const response = await sendRequest<UserDto>(url, {
method: HttpMethod.POST,
body: registerRequest,
});
return UserDtoMapper.fromObject(response);
};
165 changes: 165 additions & 0 deletions Client/reasn-client/apps/web/services/event.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
import { sendRequest, HttpMethod } from "@/lib/request";

const baseUrl = `${process.env.REASN_API_URL}/api/v1/events`;

export const getEvents = async (
params: Record<string, string> = {},
): Promise<any> => {
const url = new URL(baseUrl);
url.search = new URLSearchParams(params).toString();

const response = await sendRequest<any>(url, { method: HttpMethod.GET });
return response;
};

export const createEvent = async (event: any): Promise<any> => {
const url = new URL(baseUrl);

const response = await sendRequest<any>(url, {
method: HttpMethod.POST,
body: event,
authRequired: true,
});
return response;
};

export const getEventBySlug = async (slug: string): Promise<any> => {
const url = new URL(`${baseUrl}/${slug}`);

const response = await sendRequest<any>(url, { method: HttpMethod.GET });
return response;
};

export const updateEvent = async (slug: string, event: any): Promise<any> => {
const url = new URL(`${baseUrl}/${slug}`);

const response = await sendRequest<any>(url, {
method: HttpMethod.PUT,
body: event,
authRequired: true,
});
return response;
};

export const getEventsRequests = async (): Promise<any> => {
const url = new URL(`${baseUrl}/requests`);

const response = await sendRequest<any>(url, {
method: HttpMethod.GET,
authRequired: true,
});
return response;
};

export const approveEventRequest = async (slug: string): Promise<any> => {
const url = new URL(`${baseUrl}/requests/${slug}`);

const response = await sendRequest<any>(url, {
method: HttpMethod.POST,
authRequired: true,
});
return response;
};

export const addEventImage = async (
slug: string,
images: Blob[],
): Promise<any> => {
const url = new URL(`${baseUrl}/${slug}/images`);

const formData = new FormData();
images.forEach((image) => {
formData.append("images", image);
});

const response = await sendRequest<any>(url, {
method: HttpMethod.POST,
body: formData,
authRequired: true,
});
return response;
};

export const updateEventImage = async (
slug: string,
images: Blob[],
): Promise<any> => {
const url = new URL(`${baseUrl}/${slug}/images`);

const formData = new FormData();
images.forEach((image) => {
formData.append("images", image);
});

const response = await sendRequest<any>(url, {
method: HttpMethod.PUT,
body: formData,
authRequired: true,
});
return response;
};

export const getEventImages = async (slug: string): Promise<any> => {
const url = new URL(`${baseUrl}/${slug}/images`);

const response = await sendRequest<any>(url, { method: HttpMethod.GET });
return response;
};

export const getEventParticipants = async (slug: string): Promise<any> => {
const url = new URL(`${baseUrl}/${slug}/participants`);

const response = await sendRequest<any>(url, { method: HttpMethod.GET });
return response;
};

export const getEventComments = async (slug: string): Promise<any> => {
const url = new URL(`${baseUrl}/${slug}/comments`);

const response = await sendRequest<any>(url, { method: HttpMethod.GET });
return response;
};

export const addEventComment = async (
slug: string,
comment: any,
): Promise<any> => {
const url = new URL(`${baseUrl}/${slug}/comments`);

const response = await sendRequest<any>(url, {
method: HttpMethod.POST,
body: comment,
authRequired: true,
});
return response;
};

export const getEventsParameters = async (): Promise<any> => {
const url = new URL(`${baseUrl}/parameters`);

const response = await sendRequest<any>(url, {
method: HttpMethod.GET,
authRequired: true,
});
return response;
};

export const getEventsTags = async (): Promise<any> => {
const url = new URL(`${baseUrl}/tags`);

const response = await sendRequest<any>(url, {
method: HttpMethod.GET,
authRequired: true,
});
return response;
};

export const deleteEventsTag = async (tagId: number): Promise<any> => {
const url = new URL(`${baseUrl}/tags/${tagId}`);

const response = await sendRequest<any>(url, {
method: HttpMethod.DELETE,
authRequired: true,
});
return response;
};
Loading

0 comments on commit 8cac908

Please sign in to comment.