45 lines
1.3 KiB
JavaScript
45 lines
1.3 KiB
JavaScript
const RsvDataSource = {
|
|
create_rsv_resource(base_url, { nonce } = {}) {
|
|
function request(url, method, body) {
|
|
const headers = { 'Content-Type': 'application/json' };
|
|
if (nonce) headers['X-WP-Nonce'] = nonce;
|
|
else headers['X-WP-Nonce'] = ReservairServiceAPI.nonce;
|
|
|
|
return fetch(url, {
|
|
method,
|
|
credentials: 'same-origin',
|
|
headers,
|
|
...(body !== undefined ? { body: JSON.stringify(body) } : {}),
|
|
}).then(r => {
|
|
if (!r.ok) throw new Error(`${method} ${url} failed: ${r.status}`);
|
|
return r.status === 204 ? null : r.json();
|
|
});
|
|
}
|
|
|
|
return {
|
|
base_url: base_url,
|
|
get_page(skip = 0, limit = 20, params = {}) {
|
|
const url = new URL(base_url);
|
|
url.searchParams.set('skip', skip);
|
|
url.searchParams.set('limit', limit);
|
|
for (const [k, v] of Object.entries(params)) {
|
|
url.searchParams.set(k, v);
|
|
}
|
|
return request(url, 'GET');
|
|
},
|
|
get(id) {
|
|
return request(`${base_url}/${id}`, 'GET');
|
|
},
|
|
post(data) {
|
|
return request(base_url, 'POST', data);
|
|
},
|
|
put(id, data) {
|
|
return request(`${base_url}/${id}`, 'PUT', data);
|
|
},
|
|
delete(id) {
|
|
return request(`${base_url}/${id}`, 'DELETE');
|
|
},
|
|
};
|
|
}
|
|
};
|