Skip to content

Commit a60cf9c

Browse files
authored
Merge pull request #115 from Team-Senifit/release-1.0.1
fix inconsistent url logic
2 parents 86b18dc + 1a496bb commit a60cf9c

2 files changed

Lines changed: 28 additions & 12 deletions

File tree

src/apis/axiosClient.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,20 @@ export const axiosClient = axios.create({
66
withCredentials: true, // 쿠키 인증이면 필수
77
});
88

9+
axiosClient.interceptors.request.use((config) => {
10+
const url = config.url;
11+
if (typeof url === "string" && !url.startsWith("http")) {
12+
if (!url.startsWith("/api/")) {
13+
const base = url.startsWith("/") ? "" : "/";
14+
if (!config.baseURL) {
15+
// baseURL이 비어있을 때만 /api를 강제로 붙임
16+
config.url = `/api${base}${url}`;
17+
}
18+
}
19+
}
20+
return config;
21+
});
22+
923
axiosClient.interceptors.response.use(
1024
(res) => res,
1125
(error) => {

src/apis/createAxiosServer.ts

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,32 @@ import { headers as nextHeaders } from "next/headers";
55
import { AuthError } from "./errors";
66

77
const API_PREFIX = normalizePrefix(process.env.NEXT_PUBLIC_API_BASE ?? "/api");
8-
const SITE_URL = ensureOrigin(process.env.NEXT_PUBLIC_SITE_URL);
8+
const ENV_SITE_URL = ensureOrigin(process.env.NEXT_PUBLIC_SITE_URL);
99

1010
const devHttpsAgent =
1111
process.env.NODE_ENV !== "production"
1212
? new https.Agent({ rejectUnauthorized: false })
1313
: undefined;
1414

1515
function ensureOrigin(v?: string) {
16-
if (!v) {
17-
throw new Error(
18-
"Missing NEXT_PUBLIC_SITE_URL. Set a valid origin like https://senifit.co.kr",
19-
);
20-
}
16+
if (!v) return undefined;
2117
// 'localhost:3000' 처럼 스킴이 빠진 값이 와도 보정
2218
const normalized = /^https?:\/\//i.test(v) ? v : `https://${v}`;
2319
try {
2420
const url = new URL(normalized);
2521
if (!url.hostname) throw new Error("Missing hostname");
2622
return url.origin;
2723
} catch {
28-
throw new Error(
29-
`Invalid NEXT_PUBLIC_SITE_URL: "${v}". Set a valid origin like https://senifit.co.kr`,
30-
);
24+
return undefined;
3125
}
3226
}
27+
28+
function originFromHeaders(h: Headers) {
29+
const proto = h.get("x-forwarded-proto") || "https";
30+
const host = h.get("x-forwarded-host") || h.get("host");
31+
if (!host) return undefined;
32+
return `${proto}://${host}`;
33+
}
3334
function normalizePrefix(p: string) {
3435
return p.startsWith("/") ? p : `/${p}`;
3536
}
@@ -38,11 +39,12 @@ export async function createAxiosServer(opts?: {
3839
forwardCookies?: boolean; // default: true
3940
extraHeaders?: Record<string, string | number | boolean | undefined>;
4041
}): Promise<AxiosInstance> {
41-
// 헤더 의존 대신 env 기반으로 절대 baseURL 생성
42-
const baseURL = new URL(API_PREFIX, SITE_URL).toString(); // e.g. https://localhost:3000/api
43-
4442
// 쿠키 전달 (SSR 세션 유지용)
4543
const h = await nextHeaders();
44+
// env 우선, 없으면 요청 헤더 기반 origin 사용
45+
const siteOrigin =
46+
ENV_SITE_URL || originFromHeaders(h) || "https://localhost:3000";
47+
const baseURL = new URL(API_PREFIX, siteOrigin).toString();
4648
const cookie = opts?.forwardCookies === false ? "" : (h.get("cookie") ?? "");
4749

4850
const instance = axios.create({

0 commit comments

Comments
 (0)