-
Notifications
You must be signed in to change notification settings - Fork 1
axiosClient 메서드 사용하기
hwisaac edited this page Feb 21, 2023
·
5 revisions
코드는
src/libs/axiosClient.tsx
에 있습니다.
- 해당 파일에서
ax
라는 axios 인스턴스를 생성하고export
하고 있습니다. - 이
ax
인스턴스를import
하면, 안에 정의된 메서드들을 사용할 수 있습니다.
예시:
import { ax } from './libs/axiosClient';
const handleTest = async () => {
const { email, password } = getValues(); // useForm 에서 입력받은 email과 password를 가져옵니다.
const token = await ax.postLogin({email, password}); // 해당 메서드를 이용하면 api 요청을 간단히 할 수 있습니다.
console.log(token);
};
ax.postLogin : ({ email, password }: ILoginInput) => Promise<IToken>
interface ILoginInput {
email: string;
password: string;
}
interface IToken {
accessToken: string;
refreshToken: string;
}
ax.patchUserEdit : (
accessToken: string,
payload: IUserEditPayload
) => Promise<IPatchUserEditReturn>
interface IUserEditPayload {
oldPassword: string;
newPassword?: string;
phone?: string;
salary?: number;
job?: string;
}
interface IPatchUserEditReturn {
oldPassword: string;
newPassword: string;
phone: string;
salary: number;
job: string;
}
ax.getUser : (accessToken: string) => Promise<IGetUserReturn>
interface IGetUserReturn {
email: string;
password: string;
name: string;
birth: string;
phone: string;
salary: number;
job: string;
availableAmount: number;
}
ax.postRefresh : (refreshToken: string) => Promise<IPostRefreshReturn>
interface IPostRefreshReturn {
accessToken: string,
refreshToken: string
}
ax.postLogout : (accessToken) => Promise<void>
{
"code": 200,
"message": "로그아웃 성공",
"data": "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJGYXN0Q2FtcHVzIiwiaWF0IjoxNjc2OTcxMjkyLCJleHAiOjE2NzY5NzMwOTIsImVtYWlsIjoidGVzdHQifQ.RWsntbwHaURNqyg9jrS_lDx8CDuCWjbIctHcAsH6WYg"
}
ax.postRegister : (payload: IRegisterInput) => Promise<IPostRegisterReturn>
interface IRegisterInput {
birth?: string;
email: string;
job?: string;
name?: string;
password: string;
phone?: string;
salary: number;
}
interface IPostRegisterReturn {
code: number;
message: string;
data: string;
}
리턴 예시
{
"code": 200,
"message": "요청에 성공하였습니다.",
"data": "SignupReqDTO(email=t2, password={bcrypt}$2a$10$1EkAozx4V5A9Xtyl0kk.3u2RlZDpIcbq2961qAMUo2oVLAH0c8GJS, name=null, birth=null, phone=null, salary=1234, job=null)"
}
ax.deleteUser : (accessToken, { email, password }) => Promise<IDeleteUserReturn>
interface IDeleteUserReturn {
memberId: number;
email: string;
password: string;
name: string;
birth: string;
phone: string;
salary: number;
job: string;
deleteCheck: string;
created_date: number[];
updated_date: number[];
}
리턴 예시
{
"memberId": 32,
"email": "t3",
"password": "{bcrypt}$2a$10$DlG.zG36zkURqyVJuIdcjOVfV7FnqIm7WfoVePqKfWKSXBD6Q3.Xy",
"name": "Anonymouse",
"birth": "12314",
"phone": "011111111111",
"salary": 313123,
"job": "feee",
"deleteCheck": "withdraw",
"created_date": [
2023,
2,
21,
3,
19,
8,
65628000
],
"updated_date": [
2023,
2,
21,
3,
31,
27,
590384000
]
}
ax.getProducts : (accessToken, page) => Promise<IGetProductsReturn>
interface IGetProductsReturn {
content : {
price: number,
brand: string,
logo: string;
name: string;
rate: number;
detail: string;
productId: number
}[],
totalPages: number,
totalElements: number,
pageNumber: number,
size: number
}
리턴 예시
{
"content": [
{
"price": 1000,
"brand": "우리은행",
"logo": "https://ai.esmplus.com/heehyohoo/project/wori.png",
"name": "부동산담보대출",
"rate": 7.7,
"detail": "02-123-1234",
"productId": 1
},
{
"price": 2000,
"brand": "국민은행",
"logo": "https://ai.esmplus.com/heehyohoo/project/kb.png",
"name": "부동산담보대출",
"rate": 7.7,
"detail": "02-123-1234",
"productId": 2
}
],
"totalPages": 15,
"totalElements": 150,
"pageNumber": 1,
"size": 10
}
ax.getRecommendsProducts : (accessToken, page) => Promise<IGetProductsReturn>
interface IGetProductsReturn {
content: {
price: number;
brand: string;
logo: string;
name: string;
rate: number;
detail: string;
productId: number;
}[];
totalPages: number;
totalElements: number;
pageNumber: number;
size: number;
}
리턴값 예시는 전체상품 가져오기와 동일
ax.getProductsDetails : (products_id) => Promise<IProduct>
interface IProduct {
brand: string;
detail: string;
logo: string;
name: string;
price: number;
productId: number;
rate: number;
}
리턴값은 없습니다.(void)
ax.postOrders(
accessToken,
products_id_list: (number|string)[]
): Promise<void>
ax.getOrders : (accessToken) => Promise<IGetOrders>
type IGetOrders = {
orderId: number;
purchaseDate: number[];
purchasedProductList: purchasedProduct[];
}[]; // 배열을 리턴해줍니다.
리턴 예시
[
{
"orderId": 52,
"purchaseDate": [2023, 2, 21, 9, 43, 45, 197054000],
"purchasedProductList": [
{
"purchasedProductId": 75,
"purchasedProductPrice": 1000,
"purchasedProductBrand": "우리은행",
"purchasedProductLogo": "https://ai.esmplus.com/heehyohoo/project/wori.png",
"purchasedProductName": "부동산담보대출",
"purchasedProductRate": 7.7,
"purchasedProductDetail": "02-123-1234",
"originalProductId": 1
},
{
"purchasedProductId": 76,
"purchasedProductPrice": 2000,
"purchasedProductBrand": "국민은행",
"purchasedProductLogo": "https://ai.esmplus.com/heehyohoo/project/kb.png",
"purchasedProductName": "부동산담보대출",
"purchasedProductRate": 7.7,
"purchasedProductDetail": "02-123-1234",
"originalProductId": 2
},
{
"purchasedProductId": 77,
"purchasedProductPrice": 3000,
"purchasedProductBrand": "하나은행",
"purchasedProductLogo": "https://ai.esmplus.com/heehyohoo/project/hana.png",
"purchasedProductName": "부동산담보대출",
"purchasedProductRate": 7.7,
"purchasedProductDetail": "02-123-1234",
"originalProductId": 3
}
]
},
{
"orderId": 53,
"purchaseDate": [2023, 2, 21, 9, 43, 56, 653965000],
"purchasedProductList": [
{
"purchasedProductId": 78,
"purchasedProductPrice": 4000,
"purchasedProductBrand": "신한은행",
"purchasedProductLogo": "https://ai.esmplus.com/heehyohoo/project/shinhan.png",
"purchasedProductName": "부동산담보대출",
"purchasedProductRate": 7.7,
"purchasedProductDetail": "02-123-1234",
"originalProductId": 4
},
{
"purchasedProductId": 79,
"purchasedProductPrice": 5000,
"purchasedProductBrand": "카카오뱅크",
"purchasedProductLogo": "https://ai.esmplus.com/heehyohoo/project/kakao.png",
"purchasedProductName": "부동산담보대출",
"purchasedProductRate": 7.7,
"purchasedProductDetail": "02-123-1234",
"originalProductId": 5
},
{
"purchasedProductId": 80,
"purchasedProductPrice": 5000,
"purchasedProductBrand": "기업은행",
"purchasedProductLogo": "https://ai.esmplus.com/heehyohoo/project/ibk.png",
"purchasedProductName": "부동산담보대출",
"purchasedProductRate": 7.7,
"purchasedProductDetail": "02-123-1234",
"originalProductId": 6
}
]
}
]
ax.deleteOrders : (accessToken: string, orderId: number | string ) => Promise<void>
getSearch(
accessToken,
{ name, page }
): Promise<ISearchedData>
interface ISearchedData {
content: {
price: number;
brand: string;
logo: string;
name: string;
rate: number;
detail: string;
productId: number;
}[];
totalPages: number;
totalElements: number;
pageNumber: number;
size: number;
}
ax.getWishlists : (accessToken) => Promise<IWishlistsData>
ax.getBaskets : (accessToken) => Promise<IBasketsData>
type IWishlistsData = {
wishlistId: number;
productId: number;
brand: string;
logo: string;
name: string;
price: number;
detail: string;
rate: number;
}[];
type IBasketsData = {
basketId: number;
brand: string;
detail: string;
logo: string;
name: string;
price: number;
productId: number;
rate: number;
}[];
ax.postWishlists : (accessToken: string, productId: number | string ) => Promise<void>
ax.postBaskets: (accessToken: string, basketId: number | string ) => Promise<void>
ax.deleteWishlists : ( accessToken: string, wishlistId: number | string ) => Promise<void>
ax.deleteBaskets(
accessToken: string,
basketId: number | string
): Promise<void>