Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

다락방에 종속된 api를 연결 #390

Merged
merged 7 commits into from
Aug 20, 2024
105 changes: 90 additions & 15 deletions frontend/src/apis/apiClient.ts
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getWithLastDarakbangId()와 같이 함수명이 변경되었는데 원래는 isRequiredAuth값에 따라 뒤어 with, without을 붙였는데 LastDarakbangId만 있으니 사용하는 입작에서 알 수 없는 것이 큽니다

Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ApiError } from '@_utils/customError/ApiError';
import { getLastDarakbangId } from '@_common/lastDarakbangManager';
import { getToken } from '@_utils/tokenManager';

type Method = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE';
Expand All @@ -9,8 +10,10 @@ const DEFAULT_HEADERS = {

const BASE_URL = `${process.env.BASE_URL}/v1`;

function addBaseUrl(endpoint: string) {
function addBaseUrl(endpoint: string, isNeedLastDarakbang: boolean = false) {
if (endpoint[0] !== '/') endpoint = '/' + endpoint;
if (isNeedLastDarakbang)
endpoint = '/darakbang/' + (getLastDarakbangId() || 0) + endpoint;
return BASE_URL + endpoint;
}

Expand All @@ -29,8 +32,9 @@ async function request(
data: object = {},
config: RequestInit = {},
isRequiredAuth: boolean = false,
isRequiredLastDarakbang: boolean = false,
) {
const url = addBaseUrl(endpoint);
const url = addBaseUrl(endpoint, isRequiredLastDarakbang);

const options: RequestInit = {
method,
Expand All @@ -56,35 +60,67 @@ async function get(
endpoint: string,
config: RequestInit = {},
isRequiredAuth: boolean = false,
isRequiredLastDarakbang: boolean = false,
) {
return request('GET', endpoint, {}, config, isRequiredAuth);
return request(
'GET',
endpoint,
{},
config,
isRequiredAuth,
isRequiredLastDarakbang,
);
}

async function post(
endpoint: string,
data: object = {},
config: RequestInit = {},
isRequiredAuth: boolean = false,
isRequiredLastDarakbang: boolean = false,
) {
return request('POST', endpoint, data, config, isRequiredAuth);
return request(
'POST',
endpoint,
data,
config,
isRequiredAuth,
isRequiredLastDarakbang,
);
}

async function put(
endpoint: string,
data: object = {},
config: RequestInit = {},
isRequiredAuth: boolean = false,
isRequiredLastDarakbang: boolean = false,
) {
return request('PUT', endpoint, data, config, isRequiredAuth);
return request(
'PUT',
endpoint,
data,
config,
isRequiredAuth,
isRequiredLastDarakbang,
);
}

async function patch(
endpoint: string,
data: object = {},
config: RequestInit = {},
isRequiredAuth: boolean = false,
isRequiredLastDarakbang: boolean = false,
) {
return request('PATCH', endpoint, data, config, isRequiredAuth);
return request(
'PATCH',
endpoint,
data,
config,
isRequiredAuth,
isRequiredLastDarakbang,
);
}

/**
Expand All @@ -95,8 +131,16 @@ async function deleteMethod(
data: object = {},
config: RequestInit = {},
isRequiredAuth: boolean = false,
isRequiredLastDarakbang: boolean = false,
) {
return request('DELETE', endpoint, data, config, isRequiredAuth);
return request(
'DELETE',
endpoint,
data,
config,
isRequiredAuth,
isRequiredLastDarakbang,
);
}

const ApiClient = {
Expand All @@ -106,65 +150,96 @@ const ApiClient = {
async getWithAuth(endpoint: string, config: RequestInit = {}) {
return get(endpoint, config, true);
},
async getWithLastDarakbangId(endpoint: string, config: RequestInit = {}) {
return get(endpoint, config, true, true);
},

async postWithoutAuth(
endpoint: string,
data: object = {},
config: RequestInit = {},
) {
return post(endpoint, data, config, false);
},
async postWithAuth(
endpoint: string,
data: object = {},
config: RequestInit = {},
) {
return post(endpoint, data, config, true);
},
async postWithoutAuth(
async postWithLastDarakbangId(
endpoint: string,
data: object = {},
config: RequestInit = {},
) {
return post(endpoint, data, config, false);
return post(endpoint, data, config, true, true);
},

async putWithoutAuth(
endpoint: string,
data: object = {},
config: RequestInit = {},
) {
return put(endpoint, data, config, false);
},
async putWithAuth(
endpoint: string,
data: object = {},
config: RequestInit = {},
) {
return put(endpoint, data, config, true);
},
async putWithoutAuth(
async putWithLastDarakbangId(
endpoint: string,
data: object = {},
config: RequestInit = {},
) {
return put(endpoint, data, config, false);
return put(endpoint, data, config, true, true);
},

async patchWithoutAuth(
endpoint: string,
data: object = {},
config: RequestInit = {},
) {
return patch(endpoint, data, config, false);
},
async patchWithAuth(
endpoint: string,
data: object = {},
config: RequestInit = {},
) {
return patch(endpoint, data, config, true);
},
async patchWithoutAuth(
async patchWithLastDarakbangId(
endpoint: string,
data: object = {},
config: RequestInit = {},
) {
return patch(endpoint, data, config, false);
return patch(endpoint, data, config, true, true);
},

async deleteWithoutAuth(
endpoint: string,
data: object = {},
config: RequestInit = {},
) {
return deleteMethod(endpoint, data, config, false);
},
async deleteWithAuth(
endpoint: string,
data: object = {},
config: RequestInit = {},
) {
return deleteMethod(endpoint, data, config, true);
},
async deleteWithoutAuth(
async deleteWithLastDarakbangId(
endpoint: string,
data: object = {},
config: RequestInit = {},
) {
return deleteMethod(endpoint, data, config, false);
return deleteMethod(endpoint, data, config, true, true);
},
};

Expand Down
2 changes: 1 addition & 1 deletion frontend/src/apis/deletes.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import ApiClient from './apiClient';

export const deleteCancelChamyo = async (moimId: number) => {
await ApiClient.deleteWithAuth(`chamyo`, {
await ApiClient.deleteWithLastDarakbangId(`chamyo`, {
moimId,
});
};
52 changes: 27 additions & 25 deletions frontend/src/apis/gets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import ApiClient from './apiClient';
import { Filter } from '@_components/MyMoimListFilters/MyMoimListFilters';

export const getMoims = async (): Promise<MoimInfo[]> => {
const response = await ApiClient.getWithAuth('moim');
const response = await ApiClient.getWithLastDarakbangId('moim');

const json: GetMoims = await response.json();
return json.data.moims;
Expand All @@ -36,28 +36,30 @@ export const getMoims = async (): Promise<MoimInfo[]> => {
export const getMyFilteredMoims = async (
filter: Filter['api'],
): Promise<MoimInfo[]> => {
const response = await ApiClient.getWithAuth(`moim/mine?filter=${filter}`);
const response = await ApiClient.getWithLastDarakbangId(
`moim/mine?filter=${filter}`,
);

const json: GetMoims = await response.json();
return json.data.moims;
};

export const getMyZzimMoims = async (): Promise<MoimInfo[]> => {
const response = await ApiClient.getWithAuth('moim/zzim');
const response = await ApiClient.getWithLastDarakbangId('moim/zzim');

const json: GetMoims = await response.json();
return json.data.moims;
};

export const getMoim = async (moimId: number): Promise<MoimInfo> => {
const response = await ApiClient.getWithAuth(`moim/${moimId}`);
const response = await ApiClient.getWithLastDarakbangId(`moim/${moimId}`);

const json: GetMoim = await response.json();
return json.data;
};

export const getChatPreview = async (): Promise<ChattingPreview[]> => {
const response = await ApiClient.getWithAuth(`chat/preview`);
const response = await ApiClient.getWithLastDarakbangId(`chat/preview`);

const json: GetChattingPreview = await response.json();
return json.data.chatPreviewResponses;
Expand All @@ -67,7 +69,7 @@ export const getChat = async (
moimId: number,
recentChatId?: number,
): Promise<Chat[]> => {
const response = await ApiClient.getWithAuth(
const response = await ApiClient.getWithLastDarakbangId(
`chat?moimId=${moimId}&recentChatId=${recentChatId || 0}`,
);

Expand All @@ -76,21 +78,25 @@ export const getChat = async (
};

export const getMyMoims = async (): Promise<MoimInfo[]> => {
const response = await ApiClient.getWithAuth(`moim/mine`);
const response = await ApiClient.getWithLastDarakbangId(`moim/mine`);

const json: GetMoims = await response.json();
return json.data.moims;
};

export const getChamyoMine = async (moimId: number): Promise<Role> => {
const response = await ApiClient.getWithAuth(`chamyo/mine?moimId=${moimId}`);
const response = await ApiClient.getWithLastDarakbangId(
`chamyo/mine?moimId=${moimId}`,
);

const json: GetChamyoMine = await response.json();
return json.data.role;
};

export const getZzimMine = async (moimId: number): Promise<boolean> => {
const response = await ApiClient.getWithAuth(`zzim/mine?moimId=${moimId}`);
const response = await ApiClient.getWithLastDarakbangId(
`zzim/mine?moimId=${moimId}`,
);

const json: GetZzimMine = await response.json();
return json.data.isZzimed;
Expand All @@ -99,28 +105,30 @@ export const getZzimMine = async (moimId: number): Promise<boolean> => {
export const getChamyoAll = async (
moimId: number,
): Promise<Participation[]> => {
const response = await ApiClient.getWithAuth(`chamyo/all?moimId=${moimId}`);
const response = await ApiClient.getWithLastDarakbangId(
`chamyo/all?moimId=${moimId}`,
);

const json: GetChamyoAll = await response.json();
return json.data.chamyos;
};

export const getPleases = async () => {
const response = await ApiClient.getWithAuth('please');
const response = await ApiClient.getWithLastDarakbangId('please');

const json: GetPleases = await response.json();
return json.data.pleases;
};

export const getMyInfo = async () => {
const response = await ApiClient.getWithAuth('member/mine');
const response = await ApiClient.getWithLastDarakbangId('member/mine');

const json: GetMyInfo = await response.json();
return json.data;
};

export const getNotifications = async () => {
const response = await ApiClient.getWithAuth('notification/mine');
const response = await ApiClient.getWithLastDarakbangId('notification/mine');

const json: GetNotifications = await response.json();
return json.data.notifications;
Expand All @@ -133,28 +141,22 @@ export const getMyDarakbangs = async () => {
return json.data.darakbangResponses;
};

export const getMyRoleInDarakbang = async (darakbangId: number) => {
const response = await ApiClient.getWithAuth(
'darakbang/' + darakbangId + '/role',
);
export const getMyRoleInDarakbang = async () => {
const response = await ApiClient.getWithLastDarakbangId('/role');

const json: GetMyRoleInDarakbang = await response.json();
return json.data.role;
};

export const getDarakbangMembers = async (darakbangId: number) => {
const response = await ApiClient.getWithAuth(
'darakbang/' + darakbangId + '/members',
);
export const getDarakbangMembers = async () => {
const response = await ApiClient.getWithLastDarakbangId('/members');

const json: GetDarakbangMembers = await response.json();
return json.data.darakbangMemberResponses;
};

export const getDarakbangInviteCode = async (darakbangId: number) => {
const response = await ApiClient.getWithAuth(
'darakbang/' + darakbangId + '/code',
);
export const getDarakbangInviteCode = async () => {
const response = await ApiClient.getWithLastDarakbangId('/code');

const json: GetDarakbangInviteCode = await response.json();
return json.data.code;
Expand Down
Loading
Loading