Skip to content

Commit

Permalink
Merge pull request #154 from Myongji-Graduate/migration-my-fetch-ax
Browse files Browse the repository at this point in the history
Migration Fetch ax in my page
  • Loading branch information
gahyuun authored Nov 4, 2024
2 parents 475558c + e73d642 commit dd8df38
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 56 deletions.
65 changes: 26 additions & 39 deletions app/business/services/lecture/taken-lecture.command.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
'use server';
import { FormState } from '@/app/ui/view/molecule/form/form-root';
import { API_PATH } from '../../api-path';
import { httpErrorHandler } from '@/app/utils/http/http-error-handler';
import { BadRequestError } from '@/app/utils/http/http-error';
import { revalidateTag } from 'next/cache';
import { TAG } from '@/app/utils/http/tag';
import { cookies } from 'next/headers';
import { redirect } from 'next/navigation';
import { getToken } from '../auth';
import { instance } from '@/app/utils/api/instance';

export const registerUserGrade = async (prevState: FormState, formData: FormData) => {
const parsingText = await parsePDFtoText(formData);
Expand Down Expand Up @@ -45,23 +42,13 @@ export const parsePDFtoText = async (formData: FormData) => {

export const deleteTakenLecture = async (lectureId: number) => {
try {
const response = await fetch(`${API_PATH.takenLectures}/${lectureId}`, {
method: 'DELETE',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${cookies().get('accessToken')?.value}`,
},
await instance.delete(`${API_PATH.takenLectures}/${lectureId}`, {
responseType: 'text',
});
if (response.ok) {
revalidateTag(TAG.GET_TAKEN_LECTURES);
return {
isSuccess: true,
};
} else {
return {
isSuccess: false,
};
}

return {
isSuccess: true,
};
} catch (error) {
if (error instanceof BadRequestError) {
return {
Expand All @@ -74,30 +61,30 @@ export const deleteTakenLecture = async (lectureId: number) => {
};

export const addTakenLecture = async (lectureId: number) => {
const token = await getToken();
const response = await fetch(API_PATH.takenLectures, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${token}`,
},
body: JSON.stringify({ lectureId }),
});
// delete taken lecture과 비슷한 이유로 코드 수정
if (response.ok) {
revalidateTag(TAG.GET_TAKEN_LECTURES);
try {
await instance.post(
API_PATH.takenLectures,
{ lectureId },
{
responseType: 'text',
},
);
return {
isSuccess: true,
isFailure: false,
validationError: {},
message: '과목 추가에 성공했습니다',
};
} else {
return {
isSuccess: false,
isFailure: true,
validationError: {},
message: '과목 추가에 실패했습니다',
};
} catch (error) {
if (error instanceof BadRequestError) {
return {
isSuccess: false,
isFailure: true,
validationError: {},
message: '과목 추가에 실패했습니다',
};
} else {
throw error;
}
}
};
15 changes: 4 additions & 11 deletions app/business/services/lecture/taken-lecture.query.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { instance } from '@/app/utils/api/instance';
import { API_PATH } from '../../api-path';
import { TAG } from '@/app/utils/http/tag';
import { cookies } from 'next/headers';

export interface TakenLecturesResponse {
totalCredit: number;
Expand All @@ -17,13 +16,7 @@ export interface TakenLectureInfoResponse {
credit: number;
}

export const fetchTakenLectures = async (): Promise<TakenLecturesResponse> => {
const response = await fetch(API_PATH.takenLectures, {
next: { tags: [TAG.GET_TAKEN_LECTURES] },
headers: {
Authorization: `Bearer ${cookies().get('accessToken')?.value}`,
},
});
const data = await response.json();
return data;
export const fetchTakenLectures = async () => {
const response = await instance.get<TakenLecturesResponse>(API_PATH.takenLectures);
return response.data;
};
11 changes: 5 additions & 6 deletions app/utils/api/instance.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import fetchAx, { FetchAxError } from 'fetch-ax';
import { cookies } from 'next/headers';
import { fetchAxErrorHandler } from '../http/http-error-handler';
import fetchAX from 'fetch-ax';

export const instance = fetchAx.create({
export const instance = fetchAX.create({
headers: {
'Content-Type': 'application/json',
},
Expand All @@ -11,11 +11,10 @@ export const instance = fetchAx.create({
},
requestInterceptor: (config) => {
const accessToken = cookies().get('accessToken')?.value;

if (accessToken) {
config.headers = {
...config.headers,
Authorization: `Bearer ${accessToken}`,
};
config.headers = new Headers(config.headers);
config.headers?.set('Authorization', `Bearer ${accessToken}`);
}
return config;
},
Expand Down

0 comments on commit dd8df38

Please sign in to comment.