-
Notifications
You must be signed in to change notification settings - Fork 1
Feat/axios interceptor #12
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| import axios from "axios"; | ||
|
|
||
| // 1. 공통 설정 (URL, 헤더 등) | ||
| const axiosConfig = { | ||
| baseURL: process.env.NEXT_PUBLIC_API_URL, | ||
| headers: { "Content-Type": "application/json" }, | ||
| withCredentials: true, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 클라이언트 환경에서 |
||
| }; | ||
|
|
||
| // 2. 클라이언트용(CSR) 인스턴스 생성 및 export | ||
| export const api = axios.create(axiosConfig); | ||
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,46 @@ | ||||||||
| import { cookies } from "next/headers"; | ||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
| import { api } from "./axios"; | ||||||||
| import type { AxiosRequestConfig, AxiosResponse } from "axios"; | ||||||||
|
|
||||||||
| // 서버 환경에서 사용할 API 클라이언트입니다. | ||||||||
| // next/headers의 cookies()를 사용하여 요청에 자동으로 쿠키를 포함시킵니다. | ||||||||
| async function request<T>( | ||||||||
| config: AxiosRequestConfig, | ||||||||
| ): Promise<AxiosResponse<T>> { | ||||||||
| const cookieStore = await cookies(); | ||||||||
| const cookieHeader = cookieStore.toString(); | ||||||||
|
Comment on lines
+10
to
+11
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 안녕하세요.
const cookieStore = await cookies();
const cookieHeader = cookieStore
.getAll()
.map((cookie) => `${cookie.name}=${cookie.value}`)
.join("; "); |
||||||||
|
|
||||||||
| return api({ | ||||||||
| ...config, | ||||||||
| headers: { | ||||||||
| ...config.headers, | ||||||||
| ...(cookieHeader && { Cookie: cookieHeader }), | ||||||||
| }, | ||||||||
| }); | ||||||||
| } | ||||||||
|
Comment on lines
+1
to
+20
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 현재 import { cookies } from "next/headers";
import { api } from "./axios";
import type { AxiosRequestConfig, AxiosResponse } from "axios";
// 서버 환경에서 사용할 API 클라이언트입니다.
// next/headers의 cookies()를 사용하여 요청에 자동으로 쿠키를 포함시킵니다.
async function request<T>(config: AxiosRequestConfig): Promise<AxiosResponse<T>> {
const cookieStore = await cookies();
const cookieHeader = cookieStore.toString();
return api({
...config,
headers: {
...config.headers,
...(cookieHeader && { Cookie: cookieHeader }),
},
});
}
export const serverApi = {
get: <T = any>(url: string, config?: AxiosRequestConfig) =>
request<T>({ ...config, method: "GET", url }),
post: <T = any>(url: string, data?: any, config?: AxiosRequestConfig) =>
request<T>({ ...config, method: "POST", url, data }),
put: <T = any>(url: string, data?: any, config?: AxiosRequestConfig) =>
request<T>({ ...config, method: "PUT", url, data }),
delete: <T = any>(url: string, config?: AxiosRequestConfig) =>
request<T>({ ...config, method: "DELETE", url }),
patch: <T = any>(url: string, data?: any, config?: AxiosRequestConfig) =>
request<T>({ ...config, method: "PATCH", url, data }),
};References
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 반영함 |
||||||||
|
|
||||||||
| export const serverApi = { | ||||||||
| get: <T = unknown>(url: string, config?: AxiosRequestConfig) => | ||||||||
| request<T>({ ...config, method: "GET", url }), | ||||||||
|
|
||||||||
| post: <T = unknown>( | ||||||||
| url: string, | ||||||||
| data?: unknown, | ||||||||
| config?: AxiosRequestConfig, | ||||||||
| ) => request<T>({ ...config, method: "POST", url, data }), | ||||||||
|
Comment on lines
+26
to
+30
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 타입 안정성 및 개발자 경험 향상을 위해 post: <T = unknown, D = unknown>(
url: string,
data?: D,
config?: AxiosRequestConfig,
) => request<T>({ ...config, method: "POST", url, data }), |
||||||||
|
|
||||||||
| put: <T = unknown>( | ||||||||
| url: string, | ||||||||
| data?: unknown, | ||||||||
| config?: AxiosRequestConfig, | ||||||||
| ) => request<T>({ ...config, method: "PUT", url, data }), | ||||||||
|
Comment on lines
+32
to
+36
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 타입 안정성 및 개발자 경험 향상을 위해 put: <T = unknown, D = unknown>(
url: string,
data?: D,
config?: AxiosRequestConfig,
) => request<T>({ ...config, method: "PUT", url, data }), |
||||||||
|
|
||||||||
| delete: <T = unknown>(url: string, config?: AxiosRequestConfig) => | ||||||||
| request<T>({ ...config, method: "DELETE", url }), | ||||||||
|
|
||||||||
| patch: <T = unknown>( | ||||||||
| url: string, | ||||||||
| data?: unknown, | ||||||||
| config?: AxiosRequestConfig, | ||||||||
| ) => request<T>({ ...config, method: "PATCH", url, data }), | ||||||||
|
Comment on lines
+41
to
+45
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 타입 안정성 및 개발자 경험 향상을 위해 patch: <T = unknown, D = unknown>(
url: string,
data?: D,
config?: AxiosRequestConfig,
) => request<T>({ ...config, method: "PATCH", url, data }), |
||||||||
| }; | ||||||||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
File Walkthrough
axios.ts (+11/-0)
Axios 클라이언트 인스턴스 설정lib/axios.ts
server-api.ts (+46/-0)
서버 환경 API 클라이언트 구현lib/server-api.ts
package.json (+3/-0)
React Query 및 Axios 의존성 추가package.json
pnpm-lock.yaml (+113/-0)
패키지 잠금 파일 업데이트pnpm-lock.yaml