46 lines
1.0 KiB
JavaScript
46 lines
1.0 KiB
JavaScript
export const API_BASE = (import.meta.env.VITE_API_BASE_URL || '').replace(/\/$/, '');
|
|
|
|
async function request(path, options = {}) {
|
|
const res = await fetch(API_BASE + path, options);
|
|
if (!res.ok) throw new Error(`${res.status} ${await res.text()}`);
|
|
return res;
|
|
}
|
|
|
|
export const apiClient = {
|
|
async json(path, options) {
|
|
const res = await request(path, options);
|
|
return await res.json();
|
|
},
|
|
async text(path, options) {
|
|
const res = await request(path, options);
|
|
return await res.text();
|
|
},
|
|
async raw(path, options) {
|
|
return await request(path, options);
|
|
}
|
|
};
|
|
|
|
export function jsonOptions(body) {
|
|
return {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify(body)
|
|
};
|
|
}
|
|
|
|
export function putJsonOptions(body) {
|
|
return {
|
|
method: 'PUT',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify(body)
|
|
};
|
|
}
|
|
|
|
export function textPutOptions(body) {
|
|
return {
|
|
method: 'PUT',
|
|
headers: { 'Content-Type': 'text/plain; charset=utf-8' },
|
|
body
|
|
};
|
|
}
|