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 타입 변경 #163

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 15 additions & 10 deletions client/src/service/base/api.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { IResponseRawData } from '@/types';
import axios from 'axios';

export const BASE_URL = process.env.NEXT_PUBLIC_API_URL;
Expand Down Expand Up @@ -26,15 +27,15 @@ export async function axios_get<Response = unknown>({
params = {},
}: Omit<AxiosProps<unknown>, 'data'>) {
try {
const response = await axios.get<Response>(suburl, {
const response = await axios.get<IResponseRawData<Response>>(suburl, {
headers,
params,
paramsSerializer,
});
//TODO: data: response.data.data로 바꾸기 (백엔드 변경 이후)
return {
isOkay: true,
data: response.data,
data: response.data.data,
};
} catch (error) {
if (axios.isAxiosError(error)) {
Expand All @@ -56,12 +57,16 @@ export async function axios_post<Response = unknown, Request = unknown>({
headers = {},
}: AxiosProps<Request>) {
try {
const response = await axios.post<Response>(suburl, data, {
headers,
});
const response = await axios.post<IResponseRawData<Response>>(
suburl,
data,
{
headers,
}
);
return {
isOkay: true,
data: response.data,
data: response.data.data,
};
} catch (error) {
if (axios.isAxiosError(error)) {
Expand All @@ -83,12 +88,12 @@ export async function axios_put<Response = unknown, Request = unknown>({
headers = {},
}: AxiosProps<Request>) {
try {
const response = await axios.put<Response>(suburl, data, {
const response = await axios.put<IResponseRawData<Response>>(suburl, data, {
headers,
});
return {
isOkay: true,
data: response.data,
data: response.data.data,
};
} catch (error) {
if (axios.isAxiosError(error)) {
Expand All @@ -109,12 +114,12 @@ export async function axios_delete<Response = unknown>({
headers = {},
}: Omit<AxiosProps<unknown>, 'data'>) {
try {
const response = await axios.delete<Response>(suburl, {
const response = await axios.delete<IResponseRawData<Response>>(suburl, {
headers,
});
return {
isOkay: true,
data: response.data,
data: response.data.data,
};
} catch (error) {
if (axios.isAxiosError(error)) {
Expand Down
8 changes: 3 additions & 5 deletions client/src/service/comment/getComments.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { axios_get } from '../base/api';

import { IComment, IResponseRawData } from '@/types';
import { IComment } from '@/types';

export default async function getComments({
postOriginId,
Expand All @@ -9,13 +9,11 @@ export default async function getComments({
}) {
const suburl = `/post/${postOriginId}/comments`;

const result = await axios_get({ suburl });
const result = await axios_get<IComment[]>({ suburl });

if (!result.isOkay) {
throw result.error;
}

const rawData = result.data as IResponseRawData<IComment[]>;

return rawData.data;
return result.data;
}
12 changes: 2 additions & 10 deletions client/src/service/follow/checkFollowStatus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,18 @@ import { axios_get } from '../base/api';

import { IFollowStatus } from '@/types';

interface IRawData {
data: IFollowStatus;
status: string;
message: string;
}

export default async function checkFollowStatus({
userId,
}: {
userId: string;
}) {
const suburl = `/follow/status/${userId}`;

const result = await axios_get<IRawData>({ suburl });
const result = await axios_get<IFollowStatus>({ suburl });

if (!result.isOkay) {
throw result.error;
}

const rawData = result.data as IRawData;

return rawData.data;
return result.data;
}
12 changes: 2 additions & 10 deletions client/src/service/follow/getFollower.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,14 @@ interface IFollowUser extends Pick<IUserState, 'userId' | 'userName'> {
profileImg: string;
}

interface IRawData {
data: IFollowUser[];
status: string;
message: string;
}

export default async function getFollower({ userId }: { userId: string }) {
const suburl = `/follow/followers/${userId}`;

const result = await axios_get({ suburl });
const result = await axios_get<IFollowUser[]>({ suburl });

if (!result.isOkay) {
throw new Error(result.error);
}

const rawData = result.data as IRawData;

return rawData.data;
return result.data;
}
12 changes: 2 additions & 10 deletions client/src/service/follow/getFollowing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,14 @@ interface IFollowUser extends Pick<IUserState, 'userId' | 'userName'> {
profileImg: string;
}

interface IRawData {
data: IFollowUser[];
status: string;
message: string;
}

export default async function getFollowing({ userId }: { userId: string }) {
const suburl = `/follow/followings/${userId}`;

const result = await axios_get({ suburl });
const result = await axios_get<IFollowUser[]>({ suburl });

if (!result.isOkay) {
throw new Error(result.error);
}

const rawData = result.data as IRawData;

return rawData.data;
return result.data;
}
10 changes: 3 additions & 7 deletions client/src/service/like/getIsLikePost.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
import { axios_get } from '../base/api';

import { IResponseRawData } from '@/types';

export default async function getIsLikePost({
postOriginId,
}: {
postOriginId: string;
}) {
const suburl = `like/posts/${postOriginId}/status`;

const result = await axios_get({ suburl });
const result = await axios_get<boolean>({ suburl });

if (!result.isOkay) {
if (!result.isOkay || result.data === undefined) {
throw result.error;
}

const rawData = result.data as IResponseRawData<boolean>;

return rawData.data;
return result.data;
}
10 changes: 3 additions & 7 deletions client/src/service/scrap/getIsScrapPost.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
import { axios_get } from '../base/api';

import { IResponseRawData } from '@/types';

export default async function getIsScrapPost({
postOriginId,
}: {
postOriginId: string;
}) {
const suburl = `scrap/posts/${postOriginId}/status`;

const result = await axios_get({ suburl });
const result = await axios_get<boolean>({ suburl });

if (!result.isOkay) {
if (!result.isOkay || result.data === undefined) {
throw result.error;
}

const rawData = result.data as IResponseRawData<boolean>;

return rawData.data;
return result.data;
}
12 changes: 5 additions & 7 deletions client/src/service/scrap/getScrapPosts.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
import { axios_get } from '../base/api';

import { IResponseRawData, IPostInformation } from '@/types';
import { IPostInformation } from '@/types';

export default async function getScrapPosts() {
const suburl = '/scrap/posts';

const result = await axios_get({ suburl });
const result = await axios_get<
Omit<IPostInformation, 'commentsCount' | 'hits' | 'likes'>
>({ suburl });

if (!result.isOkay) {
return result.error;
}

const rawData = result.data as IResponseRawData<
Omit<IPostInformation, 'commentsCount' | 'hits' | 'likes'>
>;

return rawData.data;
return result.data;
}
Loading