Skip to content

[정성현] sprint12 #138

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

Merged
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
1 change: 1 addition & 0 deletions next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const nextConfig = {
hostname: "sprint-fe-project.s3.ap-northeast-2.amazonaws.com",
pathname: "/Sprint_Mission/user/**",
},
{ protocol: "https", hostname: "image.hanatour.com" },
],
},
};
Expand Down
162 changes: 147 additions & 15 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 6 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,19 @@
"lint": "next lint"
},
"dependencies": {
"@tanstack/react-query": "^5.62.3",
"axios": "^1.7.9",
"next": "13.5.6",
"react": "^18",
"react-dom": "^18",
"next": "13.5.6"
"react-hook-form": "^7.54.0"
},
"devDependencies": {
"typescript": "^5",
"@types/node": "^20",
"@types/react": "^18",
"@types/react-dom": "^18",
"eslint": "^8",
"eslint-config-next": "13.5.6"
"eslint-config-next": "13.5.6",
"typescript": "^5"
}
}
36 changes: 36 additions & 0 deletions src/apis/auth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { instance } from "./instance";
import { PATH } from "@/constants/api";
import {
PostSignUpParams,
PostSignUpRes,
PostLogInParams,
PostLogInRes,
PostRefreshParams,
PostRefreshRes,
} from "@/types/api";

export const postSignUp = async ({
email,
nickname,
password,
passwordConfirmation,
}: PostSignUpParams) => {
const bodyObj = { email, nickname, password, passwordConfirmation };

const response = await instance.post<PostSignUpRes>(PATH.SIGNUP, bodyObj);
Copy link
Collaborator

Choose a reason for hiding this comment

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

P3:
타입 정의 좋습니당 👍

return response.data;
};

export const postLogIn = async ({ email, password }: PostLogInParams) => {
const bodyObj = { email, password };

const response = await instance.post<PostLogInRes>(PATH.LOGIN, bodyObj);
return response.data;
};

export const postRefresh = async ({ refreshToken }: PostRefreshParams) => {
const bodyObj = { refreshToken };

const response = await instance.post<PostRefreshRes>(PATH.REFRESH, bodyObj);
return response.data;
};
38 changes: 38 additions & 0 deletions src/apis/instance.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import axios, { AxiosError } from "axios";
import { postRefresh } from "./auth";
import { BASE_URL } from "@/constants/api";

export const instance = axios.create({
baseURL: BASE_URL,
headers: { "Content-Type": "application/json" },
});

instance.interceptors.request.use(async (config) => {
if (config.url === "auth/refresh-token") return config;

try {
const refresh = localStorage.getItem("refreshToken");
if (!refresh) throw new AxiosError("저장된 유저 정보가 없습니다.", "401");

const { accessToken } = await postRefresh({ refreshToken: refresh });
localStorage.setItem("accessToken", accessToken);
} catch {
localStorage.removeItem("accessToken");
localStorage.removeItem("refreshToken");
}

const access = localStorage.getItem("accessToken");
if (access) config.headers["Authorization"] = `Bearer ${access}`;
return config;
});

instance.interceptors.response.use(
(response) => response,
(error: AxiosError<{ message: string }>) => {
const res = error.response;
if (res)
console.log(`[${error.status}:${res.config.url}] ${res.data.message}`);

return Promise.reject(error);
}
);
17 changes: 17 additions & 0 deletions src/apis/product.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { instance } from "./instance";
import { PATH } from "@/constants/api";
import { GetProductsParams, GetProductsRes } from "@/types/api";

export const getProducts = async ({
page = 1,
pageSize = 10,
orderBy = "recent",
keyword,
}: GetProductsParams) => {
const paramObj = { page, pageSize, orderBy, ...(keyword && { keyword }) };

const response = await instance.get<GetProductsRes>(PATH.PRODUCTS, {
params: paramObj,
});
return response.data;
};
Loading
Loading