diff options
| author | joonhoekim <26rote@gmail.com> | 2025-10-17 18:46:22 +0900 |
|---|---|---|
| committer | joonhoekim <26rote@gmail.com> | 2025-10-17 18:46:22 +0900 |
| commit | 7349504c6a92fdc115cf59d9deef76880a8287c0 (patch) | |
| tree | b6a2e4fb8936aaf1a9e917c31556723378816367 /lib/soap/sender.ts | |
| parent | ab8b726fee9a82ba98c550a151988c86dc096efd (diff) | |
(김준회) MDG 벤더 송신 테스트페이지 개선, SWP 페이지 텍스트 수정
Diffstat (limited to 'lib/soap/sender.ts')
| -rw-r--r-- | lib/soap/sender.ts | 58 |
1 files changed, 38 insertions, 20 deletions
diff --git a/lib/soap/sender.ts b/lib/soap/sender.ts index 1dfc8730..d12665cb 100644 --- a/lib/soap/sender.ts +++ b/lib/soap/sender.ts @@ -2,6 +2,7 @@ import { withSoapLogging } from "@/lib/soap/utils"; import { XMLBuilder } from 'fast-xml-parser'; +import { debugLog, debugError, debugWarn, debugSuccess } from '@/lib/debug-utils'; // 기본 인증 정보 타입 export interface SoapAuthConfig { @@ -97,6 +98,7 @@ export async function sendSoapXml( logInfo: SoapLogInfo, auth?: SoapAuthConfig ): Promise<SoapSendResult> { + let xmlData: string | undefined; try { // 인증 정보 설정 (기본값 사용) const authConfig = auth || getDefaultAuth(); @@ -109,10 +111,10 @@ export async function sendSoapXml( config.prefix ); - const xmlData = await generateSoapXml(soapEnvelope); + xmlData = await generateSoapXml(soapEnvelope); - console.log('📤 SOAP XML 전송 시작'); - console.log('🔍 전송 XML (첫 500자):', xmlData.substring(0, 500)); + debugLog('📤 SOAP XML 전송 시작'); + debugLog('🔍 전송 XML (첫 500자):', xmlData.substring(0, 500)); // 요청 헤더 및 fetch 옵션을 사전에 구성 const requestHeaders: Record<string, string> = { @@ -124,9 +126,9 @@ export async function sendSoapXml( if (authConfig.username && authConfig.password) { const credentials = Buffer.from(`${authConfig.username}:${authConfig.password}`).toString('base64'); requestHeaders['Authorization'] = `Basic ${credentials}`; - console.log('🔐 Basic Authentication 헤더 추가 완료'); + debugSuccess('🔐 Basic Authentication 헤더 추가 완료'); } else { - console.warn('⚠️ SOAP 인증 정보가 설정되지 않았습니다.'); + debugWarn('⚠️ SOAP 인증 정보가 설정되지 않았습니다.'); } const fetchOptions: RequestInit = { @@ -136,14 +138,18 @@ export async function sendSoapXml( }; // Body 루트 요소(p1:MT_...)에 기본 네임스페이스를 부여하여 하위 무접두사 요소들도 동일 네임스페이스로 인식되도록 처리 - const envelopeObj = soapEnvelope as Record<string, any>; - const bodyObj = envelopeObj['soap:Envelope']?.['soap:Body'] as Record<string, any> | undefined; + const envelopeObj = soapEnvelope as Record<string, unknown>; + const bodyObj = envelopeObj['soap:Envelope'] as Record<string, unknown> | undefined; if (bodyObj && typeof bodyObj === 'object') { - const rootKeys = Object.keys(bodyObj); - if (rootKeys.length > 0) { - const rootKey = rootKeys[0]; - if (bodyObj[rootKey] && typeof bodyObj[rootKey] === 'object') { - bodyObj[rootKey]['@_xmlns'] = namespace; + const soapBody = bodyObj['soap:Body'] as Record<string, unknown> | undefined; + if (soapBody && typeof soapBody === 'object') { + const rootKeys = Object.keys(soapBody); + if (rootKeys.length > 0) { + const rootKey = rootKeys[0]; + const rootValue = soapBody[rootKey]; + if (rootValue && typeof rootValue === 'object') { + (rootValue as Record<string, unknown>)['@_xmlns'] = namespace; + } } } } @@ -183,8 +189,19 @@ export async function sendSoapXml( const response = result as Response; const responseText = await response.text(); - console.log('📥 SOAP 응답 수신:', response.status, response.statusText); - console.log('🔍 응답 XML (첫 500자):', responseText.substring(0, 500)); + // 응답 헤더 수집 (디버깅용) + const responseHeadersDebug: Record<string, string> = {}; + response.headers.forEach((value, key) => { + responseHeadersDebug[key] = value; + }); + + debugLog('📥 SOAP 응답 수신:', { + status: response.status, + statusText: response.statusText, + headers: responseHeadersDebug, + bodyLength: responseText.length + }); + debugLog('🔍 응답 바디 (전체):', responseText); // HTTP 상태 코드가 비정상이거나 SOAP Fault 포함 시 실패로 처리하되 본문을 그대로 반환 if (!response.ok || responseText.includes('soap:Fault') || responseText.includes('SOAP:Fault')) { @@ -222,10 +239,11 @@ export async function sendSoapXml( }; } catch (error) { - console.error('❌ SOAP XML 전송 실패:', error); + debugError('❌ SOAP XML 전송 실패:', error); return { success: false, - message: error instanceof Error ? error.message : 'Unknown error' + message: error instanceof Error ? error.message : 'Unknown error', + requestXml: xmlData // 에러가 발생해도 생성된 XML이 있다면 반환 }; } } @@ -241,7 +259,7 @@ export async function sendSoapXmlWithRetry( for (let attempt = 1; attempt <= maxRetries; attempt++) { try { - console.log(`🔄 SOAP 전송 시도 ${attempt}/${maxRetries}`); + debugLog(`🔄 SOAP 전송 시도 ${attempt}/${maxRetries}`); const result = await sendSoapXml(config, logInfo, auth); @@ -251,12 +269,12 @@ export async function sendSoapXmlWithRetry( // 마지막 시도가 아니면 재시도 if (attempt < maxRetries) { - console.log(`⏳ ${retryDelay}ms 후 재시도...`); + debugLog(`⏳ ${retryDelay}ms 후 재시도...`); await new Promise(resolve => setTimeout(resolve, retryDelay)); } } catch (error) { - console.error(`❌ SOAP 전송 시도 ${attempt} 실패:`, error); + debugError(`❌ SOAP 전송 시도 ${attempt} 실패:`, error); if (attempt === maxRetries) { return { @@ -266,7 +284,7 @@ export async function sendSoapXmlWithRetry( } // 마지막 시도가 아니면 재시도 - console.log(`⏳ ${retryDelay}ms 후 재시도...`); + debugLog(`⏳ ${retryDelay}ms 후 재시도...`); await new Promise(resolve => setTimeout(resolve, retryDelay)); } } |
