diff --git a/src/utils/api.js b/src/utils/api.js index ce417e6..0eb1293 100644 --- a/src/utils/api.js +++ b/src/utils/api.js @@ -16,6 +16,15 @@ async function request(endpoint, method = 'GET', data = null, headers = {}) { config.body = JSON.stringify(data); } + // 실제 배포시 주석처리 또는 제거해야합니다 + console.log( + `요청 메소드: ${method}\n + Endpont: ${API_URL}${endpoint}\n + Body: ${JSON.stringify(data)}\n + Headers: ${JSON.stringify(headers)}\n + Config: ${JSON.stringify(config)}`, + ); + try { const response = await fetch(`${API_URL}${endpoint}`, config); @@ -24,8 +33,14 @@ async function request(endpoint, method = 'GET', data = null, headers = {}) { throw new Error(`${response.status} ${response.statusText}`); } + let contentType = response.headers.get('Content-Type'); + // 응답 데이터를 JSON으로 반환합니다. - return await response.json(); + if (contentType.includes('application/json')) { + return await response.json(); + } else { + return await response.text(); + } } catch (error) { console.error('요청 실패:', error); throw error; // 오류를 호출한 곳으로 다시 던집니다. @@ -33,23 +48,23 @@ async function request(endpoint, method = 'GET', data = null, headers = {}) { } // GET 요청 -async function GET(endpoint) { - return await request(endpoint, 'GET'); +async function GET(endpoint, data = { body: {}, headers: {} }) { + return await request(endpoint, 'GET', data.body, data.headers); } // POST 요청 -async function POST(endpoint, data) { - return await request(endpoint, 'POST', data); +async function POST(endpoint, data = { body: {}, headers: {} }) { + return await request(endpoint, 'POST', data.body, data.headers); } // PUT 요청 -async function PUT(endpoint, data) { - return await request(endpoint, 'PUT', data); +async function PUT(endpoint, data = { body: {}, headers: {} }) { + return await request(endpoint, 'PUT', data.body, data.headers); } // DELETE 요청 -async function DELETE(endpoint) { - return await request(endpoint, 'DELETE'); +async function DELETE(endpoint, data = { body: {}, headers: {} }) { + return await request(endpoint, 'DELETE', data.body, data.headers); } export const API = {