Refactor network utils directory
Simplify the structure of the network utils directory for better maintainability.
This commit is contained in:
@@ -1,45 +0,0 @@
|
||||
|
||||
import { isCorsProxyEnabled, getProxyType, getSupabaseUrl, getOriginalSupabaseUrl } from '@/lib/supabase/config';
|
||||
|
||||
/**
|
||||
* CORS 문제 확인
|
||||
*/
|
||||
export const hasCorsIssue = (error: any): boolean => {
|
||||
if (!error) return false;
|
||||
|
||||
const errorMessage = error.message || '';
|
||||
return (
|
||||
errorMessage.includes('Failed to fetch') ||
|
||||
errorMessage.includes('CORS') ||
|
||||
errorMessage.includes('Network') ||
|
||||
errorMessage.includes('프록시')
|
||||
);
|
||||
};
|
||||
|
||||
/**
|
||||
* HTTP URL이 프록시 없이 사용되고 있는지 확인하고 처리
|
||||
*/
|
||||
export const handleHttpUrlWithoutProxy = (): boolean => {
|
||||
// HTTP URL을 사용하는데 프록시가 비활성화된 경우
|
||||
const originalUrl = getOriginalSupabaseUrl();
|
||||
const usingProxy = isCorsProxyEnabled();
|
||||
|
||||
if (originalUrl.startsWith('http:') && !usingProxy) {
|
||||
// 자동으로 프록시 활성화
|
||||
localStorage.setItem('use_cors_proxy', 'true');
|
||||
localStorage.setItem('proxy_type', 'cloudflare');
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
|
||||
/**
|
||||
* 프록시 정보 로깅
|
||||
*/
|
||||
export const logProxyInfo = (): void => {
|
||||
const usingProxy = isCorsProxyEnabled();
|
||||
const proxyType = getProxyType();
|
||||
const supabaseUrl = getSupabaseUrl();
|
||||
|
||||
console.log(`연결 테스트 - CORS 프록시: ${usingProxy ? '사용 중' : '미사용'}, 타입: ${proxyType}, URL: ${supabaseUrl}`);
|
||||
};
|
||||
@@ -1,80 +0,0 @@
|
||||
|
||||
import { getSupabaseUrl, isCorsProxyEnabled, getProxyType, getOriginalSupabaseUrl } from '@/lib/supabase/config';
|
||||
import { logProxyInfo, handleHttpUrlWithoutProxy } from './corsUtils';
|
||||
|
||||
/**
|
||||
* 강화된 서버 연결 검사: 다양한 경로로 시도
|
||||
*/
|
||||
export const verifySupabaseConnection = async (): Promise<{
|
||||
connected: boolean;
|
||||
message: string;
|
||||
statusCode?: number;
|
||||
details?: string;
|
||||
}> => {
|
||||
const supabaseUrl = getSupabaseUrl();
|
||||
if (!supabaseUrl) {
|
||||
return {
|
||||
connected: false,
|
||||
message: 'Supabase URL이 설정되지 않았습니다'
|
||||
};
|
||||
}
|
||||
|
||||
// 프록시 정보 로깅
|
||||
const usingProxy = isCorsProxyEnabled();
|
||||
const proxyType = getProxyType();
|
||||
console.log(`강화된 연결 테스트 - 프록시: ${usingProxy ? '사용 중' : '미사용'}, 타입: ${proxyType}, URL: ${supabaseUrl}`);
|
||||
|
||||
// 무작위 쿼리 파라미터를 추가하여 캐시 방지
|
||||
const cacheParam = `?_nocache=${Date.now()}`;
|
||||
|
||||
// 다양한 경로를 순차적으로 시도
|
||||
const paths = [
|
||||
'/auth/v1/',
|
||||
'/',
|
||||
'/rest/v1/',
|
||||
'/storage/v1/'
|
||||
];
|
||||
|
||||
for (const path of paths) {
|
||||
try {
|
||||
console.log(`경로 시도: ${path}`);
|
||||
const response = await fetch(`${supabaseUrl}${path}${cacheParam}`, {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'apikey': localStorage.getItem('supabase_key') || ''
|
||||
},
|
||||
signal: AbortSignal.timeout(15000) // 타임아웃 시간 증가
|
||||
});
|
||||
|
||||
console.log(`경로 ${path} 응답 상태:`, response.status);
|
||||
|
||||
// 어떤 응답이든 서버가 살아있다는 신호로 간주
|
||||
return {
|
||||
connected: true,
|
||||
message: `서버 연결 성공 (${path})`,
|
||||
statusCode: response.status,
|
||||
details: `${response.status} ${response.statusText}`
|
||||
};
|
||||
} catch (error) {
|
||||
console.warn(`${path} 경로 연결 실패:`, error);
|
||||
// 계속 다음 경로 시도
|
||||
}
|
||||
}
|
||||
|
||||
// HTTP URL을 사용하는데 프록시가 비활성화된 경우 처리
|
||||
if (handleHttpUrlWithoutProxy()) {
|
||||
return {
|
||||
connected: false,
|
||||
message: 'HTTP URL에 직접 접근할 수 없어 CORS 프록시를 자동으로 활성화했습니다. 페이지를 새로고침하고 다시 시도하세요.',
|
||||
details: 'CORS 제한으로 인해 HTTP URL에 직접 접근할 수 없습니다'
|
||||
};
|
||||
}
|
||||
|
||||
// 모든 경로 시도 실패
|
||||
return {
|
||||
connected: false,
|
||||
message: '모든 Supabase 경로에 대한 연결 시도 실패',
|
||||
details: '네트워크 연결 또는 서버 주소를 확인하세요'
|
||||
};
|
||||
};
|
||||
@@ -1,5 +1,9 @@
|
||||
|
||||
// 네트워크 유틸리티 모듈
|
||||
export { hasCorsIssue, handleHttpUrlWithoutProxy, logProxyInfo } from './corsUtils';
|
||||
export { verifyServerConnection } from './basicConnectionCheck';
|
||||
export { verifySupabaseConnection } from './enhancedConnectionCheck';
|
||||
export {
|
||||
hasCorsIssue,
|
||||
handleHttpUrlWithoutProxy,
|
||||
logProxyInfo,
|
||||
verifyServerConnection,
|
||||
verifySupabaseConnection
|
||||
} from './networkUtils';
|
||||
|
||||
@@ -1,6 +1,48 @@
|
||||
|
||||
import { getSupabaseUrl, isCorsProxyEnabled, getProxyType, getOriginalSupabaseUrl } from '@/lib/supabase/config';
|
||||
import { logProxyInfo, handleHttpUrlWithoutProxy } from './corsUtils';
|
||||
|
||||
/**
|
||||
* CORS 문제 확인
|
||||
*/
|
||||
export const hasCorsIssue = (error: any): boolean => {
|
||||
if (!error) return false;
|
||||
|
||||
const errorMessage = error.message || '';
|
||||
return (
|
||||
errorMessage.includes('Failed to fetch') ||
|
||||
errorMessage.includes('CORS') ||
|
||||
errorMessage.includes('Network') ||
|
||||
errorMessage.includes('프록시')
|
||||
);
|
||||
};
|
||||
|
||||
/**
|
||||
* HTTP URL이 프록시 없이 사용되고 있는지 확인하고 처리
|
||||
*/
|
||||
export const handleHttpUrlWithoutProxy = (): boolean => {
|
||||
// HTTP URL을 사용하는데 프록시가 비활성화된 경우
|
||||
const originalUrl = getOriginalSupabaseUrl();
|
||||
const usingProxy = isCorsProxyEnabled();
|
||||
|
||||
if (originalUrl.startsWith('http:') && !usingProxy) {
|
||||
// 자동으로 프록시 활성화
|
||||
localStorage.setItem('use_cors_proxy', 'true');
|
||||
localStorage.setItem('proxy_type', 'cloudflare');
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
|
||||
/**
|
||||
* 프록시 정보 로깅
|
||||
*/
|
||||
export const logProxyInfo = (): void => {
|
||||
const usingProxy = isCorsProxyEnabled();
|
||||
const proxyType = getProxyType();
|
||||
const supabaseUrl = getSupabaseUrl();
|
||||
|
||||
console.log(`연결 테스트 - CORS 프록시: ${usingProxy ? '사용 중' : '미사용'}, 타입: ${proxyType}, URL: ${supabaseUrl}`);
|
||||
};
|
||||
|
||||
/**
|
||||
* 서버 연결 상태 검사
|
||||
@@ -136,3 +178,80 @@ export const verifyServerConnection = async (): Promise<{
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* 강화된 서버 연결 검사: 다양한 경로로 시도
|
||||
*/
|
||||
export const verifySupabaseConnection = async (): Promise<{
|
||||
connected: boolean;
|
||||
message: string;
|
||||
statusCode?: number;
|
||||
details?: string;
|
||||
}> => {
|
||||
const supabaseUrl = getSupabaseUrl();
|
||||
if (!supabaseUrl) {
|
||||
return {
|
||||
connected: false,
|
||||
message: 'Supabase URL이 설정되지 않았습니다'
|
||||
};
|
||||
}
|
||||
|
||||
// 프록시 정보 로깅
|
||||
const usingProxy = isCorsProxyEnabled();
|
||||
const proxyType = getProxyType();
|
||||
console.log(`강화된 연결 테스트 - 프록시: ${usingProxy ? '사용 중' : '미사용'}, 타입: ${proxyType}, URL: ${supabaseUrl}`);
|
||||
|
||||
// 무작위 쿼리 파라미터를 추가하여 캐시 방지
|
||||
const cacheParam = `?_nocache=${Date.now()}`;
|
||||
|
||||
// 다양한 경로를 순차적으로 시도
|
||||
const paths = [
|
||||
'/auth/v1/',
|
||||
'/',
|
||||
'/rest/v1/',
|
||||
'/storage/v1/'
|
||||
];
|
||||
|
||||
for (const path of paths) {
|
||||
try {
|
||||
console.log(`경로 시도: ${path}`);
|
||||
const response = await fetch(`${supabaseUrl}${path}${cacheParam}`, {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'apikey': localStorage.getItem('supabase_key') || ''
|
||||
},
|
||||
signal: AbortSignal.timeout(15000) // 타임아웃 시간 증가
|
||||
});
|
||||
|
||||
console.log(`경로 ${path} 응답 상태:`, response.status);
|
||||
|
||||
// 어떤 응답이든 서버가 살아있다는 신호로 간주
|
||||
return {
|
||||
connected: true,
|
||||
message: `서버 연결 성공 (${path})`,
|
||||
statusCode: response.status,
|
||||
details: `${response.status} ${response.statusText}`
|
||||
};
|
||||
} catch (error) {
|
||||
console.warn(`${path} 경로 연결 실패:`, error);
|
||||
// 계속 다음 경로 시도
|
||||
}
|
||||
}
|
||||
|
||||
// HTTP URL을 사용하는데 프록시가 비활성화된 경우 처리
|
||||
if (handleHttpUrlWithoutProxy()) {
|
||||
return {
|
||||
connected: false,
|
||||
message: 'HTTP URL에 직접 접근할 수 없어 CORS 프록시를 자동으로 활성화했습니다. 페이지를 새로고침하고 다시 시도하세요.',
|
||||
details: 'CORS 제한으로 인해 HTTP URL에 직접 접근할 수 없습니다'
|
||||
};
|
||||
}
|
||||
|
||||
// 모든 경로 시도 실패
|
||||
return {
|
||||
connected: false,
|
||||
message: '모든 Supabase 경로에 대한 연결 시도 실패',
|
||||
details: '네트워크 연결 또는 서버 주소를 확인하세요'
|
||||
};
|
||||
};
|
||||
@@ -4,5 +4,7 @@
|
||||
export {
|
||||
hasCorsIssue,
|
||||
verifyServerConnection,
|
||||
verifySupabaseConnection
|
||||
} from './network';
|
||||
verifySupabaseConnection,
|
||||
handleHttpUrlWithoutProxy,
|
||||
logProxyInfo
|
||||
} from './network/networkUtils';
|
||||
|
||||
Reference in New Issue
Block a user