Skip to content

Commit

Permalink
Merge pull request #54 from KERT-core/fix/api
Browse files Browse the repository at this point in the history
fix: API 요청 시 body, headers 객체 분리
Village-GG-Water authored Oct 8, 2024
2 parents 82ca6fb + b6d5945 commit b9d25d4
Showing 1 changed file with 24 additions and 9 deletions.
33 changes: 24 additions & 9 deletions src/utils/api.js
Original file line number Diff line number Diff line change
@@ -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,32 +33,38 @@ 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; // 오류를 호출한 곳으로 다시 던집니다.
}
}

// 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 = {

0 comments on commit b9d25d4

Please sign in to comment.