메인페이지 원복시키기
This commit is contained in:
1 parent
c07ef255d4
commit
5f7e0051cd
9 files changed
+178
-123
No files matched your search
+6
-12
@@ -8,16 +8,7 @@
|
||||
*/
|
||||
import ky, { type KyInstance } from 'ky'
|
||||
|
||||
// Vite 가 빌드 시 치환하는 환경변수 (개발 서버에서도 동일).
|
||||
// import.meta 를 모듈 최상단에서 읽으면 일부 번들러가 cjs 변환 시 경고를 내므로
|
||||
// 함수 내부에서 lazy 하게 접근.
|
||||
function getApiKey(): string | undefined {
|
||||
try {
|
||||
return (import.meta as { env?: { VITE_API_KEY?: string } }).env?.VITE_API_KEY;
|
||||
} catch {
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
const API_BASE_URL = (import.meta.env.VITE_API_BASE_URL ?? '').replace(/\/$/, '')
|
||||
|
||||
export const kyInstance: KyInstance = ky.create({
|
||||
timeout: 10_000,
|
||||
@@ -27,7 +18,7 @@ export const kyInstance: KyInstance = ky.create({
|
||||
hooks: {
|
||||
beforeRequest: [
|
||||
(request) => {
|
||||
const apiKey = getApiKey();
|
||||
const apiKey = import.meta.env.VITE_API_KEY;
|
||||
if (apiKey) request.headers.set('x-api-key', apiKey);
|
||||
},
|
||||
// TODO: 인증 토큰 주입
|
||||
@@ -46,7 +37,10 @@ export const customFetcher = async <T>(
|
||||
url: string,
|
||||
init?: RequestInit,
|
||||
): Promise<T> => {
|
||||
const response = await kyInstance(url, init as Parameters<KyInstance>[1])
|
||||
// orval 이 생성하는 url 은 `/api/...` 상대경로라서, 그대로 두면 dev 서버 origin 으로 감.
|
||||
// VITE_API_BASE_URL 이 있으면 절대 URL 로 만들어서 백엔드로 직접 요청.
|
||||
const fullUrl = /^https?:\/\//.test(url) ? url : `${API_BASE_URL}${url}`
|
||||
const response = await kyInstance(fullUrl, init as Parameters<KyInstance>[1])
|
||||
let data: unknown = null
|
||||
try {
|
||||
data = await response.json()
|
||||
|
||||
Reference in new issue
Block a user