Skip to content

Commit

Permalink
Merge branch 'feat/api' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
eun-hak committed May 9, 2024
2 parents ce8d389 + 0ff6844 commit 4253f1f
Show file tree
Hide file tree
Showing 9 changed files with 337 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/api/auth/auth.delete.api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* 회원탈퇴 */
10 changes: 10 additions & 0 deletions src/api/auth/auth.get.api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// import { ICommon } from '../types/common';
import { getRequest } from '../request';
import { IUpdateProfile } from '../types/auth';

/* 회원 상세정보 */

export const userinfo = async () => {
const response = await getRequest<IUpdateProfile>('members');
return response;
};
1 change: 1 addition & 0 deletions src/api/auth/auth.patch.api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* 비밀번호 재설정 */
99 changes: 99 additions & 0 deletions src/api/auth/auth.post.api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
// import { ICommon } from '../types/common';
import { postRequest } from '../request';
import {
ISignIn,
ISignUp,
UserInfoType,
IEmail,
IEmailAuth,
IPhoneNumber,
IPhoneAuth
} from '../types/auth';

/* 회원가입 */

export const signup = async ({
memberEmail,
memberPassword,
memberName,
memberGender,
memberJob,
memberPhone,
memberBirthDate,
memberSmsAgree
}: ISignUp) => {
const response = await postRequest<null, ISignUp>('members', {
memberEmail,
memberPassword,
memberName,
memberGender,
memberJob,
memberPhone,
memberBirthDate,
memberSmsAgree
});
return response;
};

/* 로그인 */

export const signin = async ({ memberEmail, memberPassword }: ISignIn) => {
const response = await postRequest<UserInfoType, ISignIn>('login', {
memberEmail,
memberPassword
});

return response;
};

/* 이메일 인증 요청 */

export const emailauthrequest = async ({ emailAddress }: IEmail) => {
const response = await postRequest<null, IEmail>('auth/email', {
emailAddress
});

return response;
};

/* 이메일 코드 검증 */

export const emailauthverify = async ({ emailAddress, code }: IEmailAuth) => {
const response = await postRequest<null, IEmailAuth>('auth/email/verify', {
emailAddress,
code
});

return response;
};

/* 휴대전화 번호 인증 요청*/

export const phoneauthrequest = async ({ phoneNumber }: IPhoneNumber) => {
const response = await postRequest<null, IPhoneNumber>('/auth/phone', {
phoneNumber
});

return response;
};

/* 휴대전화 번호 코드 검증*/

export const phoneauthverify = async ({ phoneNumber, code }: IPhoneAuth) => {
const response = await postRequest<null, IPhoneAuth>('/auth/phone/verify', {
phoneNumber,
code
});

return response;
};

/* 로그아웃*/

export const logout = async () => {
const response = await postRequest<null, null>('/logout');

return response;
};

/* 토큰 리프레쉬*/
84 changes: 84 additions & 0 deletions src/api/instance.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import { toast } from 'react-toastify';
import axios, {
Axios,
InternalAxiosRequestConfig,
AxiosError,
AxiosResponse
} from 'axios';

import { getCookie } from '@utils/cookies';
import logOnDev from '@utils/logOnDev';

export const instance: Axios = axios.create({
baseURL: process.env.NEXT_PUBLIC_BASE_URL,
withCredentials: true,
headers: {
'Content-Type': 'application/json',
Accept: '*/*'
},
timeout: 3000
});

instance.interceptors.request.use(
(config: InternalAxiosRequestConfig): InternalAxiosRequestConfig => {
const token = getCookie('token') as string;
const id = getCookie('id') as string;
if (config && config.headers) {
if (token) {
config.headers.Authorization = token;
config.headers.id = id;
}
}
if (process.env.NODE_ENV === 'development') {
const { method, url } = config;
logOnDev(`🚀 [API] ${method?.toUpperCase()} ${url} | Request`);
}
return config;
},
(error: AxiosError | Error): Promise<AxiosError> => {
return Promise.reject(error);
}
);

instance.interceptors.response.use(
(response: AxiosResponse): AxiosResponse => {
return response;
},
(error: AxiosError | Error): Promise<AxiosError> => {
if (process.env.NODE_ENV === 'development') {
if (axios.isAxiosError(error)) {
const { message } = error;
const { method, url } = error.config as InternalAxiosRequestConfig;
const { status, statusText } = error.response as AxiosResponse;
logOnDev(
`🚨 [API] ${method?.toUpperCase()} ${url} | Error ${status} ${statusText} | ${message}`
);
switch (status) {
case 401: {
toast.error('로그인이 필요합니다');
break;
}
case 403: {
toast.error('잘못된 권한입니다');
break;
}
case 404: {
toast.error('잘못된 요청입니다');
break;
}
case 500: {
toast.error('서버 에러 발생');
break;
}
default: {
toast.error('알 수 없는 오류 발생');
break;
}
}
} else {
logOnDev(`🚨 [API] | Error ${error.message}`);
}
}
return Promise.reject(error);
}
);
60 changes: 60 additions & 0 deletions src/api/request.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { AxiosRequestConfig, InternalAxiosRequestConfig } from 'axios';

import { instance } from './instance';

/* get 요청 */
export const getRequest = async <T>(
url: string,
config?: AxiosRequestConfig
): Promise<T> => {
const response = await instance.get<T>(url, config as InternalAxiosRequestConfig);
return response.data;
};

/* post 요청 */
export const postRequest = async <T, D>(
url: string,
data?: D,
config?: AxiosRequestConfig
): Promise<T> => {
const response = await instance.post<T>(
url,
data,
config as InternalAxiosRequestConfig
);
return response.data;
};

/* delete 요청 */
export const deleteRequest = async <T>(
url: string,
config?: AxiosRequestConfig
): Promise<T> => {
const response = await instance.delete<T>(url, config as InternalAxiosRequestConfig);
return response.data;
};

/* put 요청 */
export const putRequest = async <T, D>(
url: string,
data: D,
config?: AxiosRequestConfig
): Promise<T> => {
const response = await instance.put<T>(url, data, config as InternalAxiosRequestConfig);
return response.data;
};

/* patch 요청 */
export const patchRequest = async <T, D>(
url: string,
data: D,
config?: AxiosRequestConfig
): Promise<T> => {
const response = await instance.patch<T>(
url,
data,
config as InternalAxiosRequestConfig
);
return response.data;
};
57 changes: 57 additions & 0 deletions src/api/types/auth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { ICommon } from './common';

export interface ISignIn {
memberEmail: string;
memberPassword: string;
}

export interface ISignUp extends ISignIn {
memberName: string;
memberGender: string;
memberJob: string;
memberPhone: string;
memberBirthDate: string;
memberSmsAgree: boolean;
}

export interface IUpdateProfile {
memberEmail: string;
memberName: string;
memberJob: string;
memberPhone: string;
memberBirthDate: string;
}

export interface IPhoneNumber {
phoneNumber: string;
}

export interface IPhoneAuth extends IPhoneNumber {
code: number;
}

export interface IUserInfo {
memberEmail: string;
memberName: string;
memberJob: string;
memberPhone: string;
memberBirthDate: string;
}

export interface IWithdraw {
memberPassword: string;
}

export interface IEmail {
emailAddress: string;
}
export interface IEmailAuth extends IEmail {
code: number;
}

export interface ITokenRefresh {
// 아직 타입 안들어옴
}
export type FindPassword = Pick<ISignUp, 'memberPassword'>;
export type ChangePassword = Pick<ISignUp, 'memberPassword'>;
export type UserInfoType = ICommon<IUserInfo>;
6 changes: 6 additions & 0 deletions src/api/types/common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export interface ICommon<T> {
status: number;
code: string;
message: string;
value: T;
}
19 changes: 19 additions & 0 deletions src/types/cookies.type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export interface CookieGetOptions {
doNotParse?: boolean;
doNotUpdate?: boolean;
}
export interface CookieSetOptions {
path?: string;
expires?: Date;
maxAge?: number;
domain?: string;
secure?: boolean;
httpOnly?: boolean;
sameSite?: boolean | 'none' | 'lax' | 'strict';
}
export interface CookieChangeOptions {
name: string;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
value?: any;
options?: CookieSetOptions;
}

0 comments on commit 4253f1f

Please sign in to comment.