v0.1.12 - 서버 저장 연결 시작
This commit is contained in:
51
src/lib/plannerApi.js
Normal file
51
src/lib/plannerApi.js
Normal file
@@ -0,0 +1,51 @@
|
||||
const API_BASE_URL = import.meta.env.VITE_API_BASE_URL ?? 'http://localhost:3001'
|
||||
|
||||
function buildHeaders(token) {
|
||||
return {
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: `Bearer ${token}`,
|
||||
}
|
||||
}
|
||||
|
||||
async function request(path, { method = 'GET', token, body } = {}) {
|
||||
const response = await fetch(`${API_BASE_URL}${path}`, {
|
||||
method,
|
||||
headers: buildHeaders(token),
|
||||
body: body ? JSON.stringify(body) : undefined,
|
||||
})
|
||||
|
||||
const data = await response.json().catch(() => ({}))
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(data.message || '플래너 데이터를 처리하지 못했습니다.')
|
||||
}
|
||||
|
||||
return data
|
||||
}
|
||||
|
||||
export async function fetchPlannerEntries(token, range = {}) {
|
||||
const searchParams = new URLSearchParams()
|
||||
|
||||
if (range.from) {
|
||||
searchParams.set('from', range.from)
|
||||
}
|
||||
|
||||
if (range.to) {
|
||||
searchParams.set('to', range.to)
|
||||
}
|
||||
|
||||
const query = searchParams.toString()
|
||||
return request(`/api/planner${query ? `?${query}` : ''}`, {
|
||||
token,
|
||||
})
|
||||
}
|
||||
|
||||
export async function savePlannerEntry(token, entryDate, payload) {
|
||||
return request(`/api/planner/${entryDate}`, {
|
||||
method: 'PUT',
|
||||
token,
|
||||
body: {
|
||||
payload,
|
||||
},
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user