mirror of
https://github.com/sergeyyarkov/modbus-logger.git
synced 2026-10-11 02:18:51 +03:00
43 lines
1022 B
JavaScript
43 lines
1022 B
JavaScript
const urlPrefix = "/api";
|
|
|
|
/**
|
|
* Response handler
|
|
* @param {Response} response
|
|
*/
|
|
async function handle(response) {
|
|
const data = await response.json();
|
|
if (!response.ok) return Promise.reject(data.error || response.statusText);
|
|
return data;
|
|
}
|
|
|
|
export default {
|
|
async patch(path, data) {
|
|
const response = await fetch(`${urlPrefix}${path}`, {
|
|
method: "PATCH",
|
|
body: new URLSearchParams(data),
|
|
headers: {
|
|
"Content-Type": "application/x-www-form-urlencoded",
|
|
},
|
|
});
|
|
return handle(response);
|
|
},
|
|
|
|
async post(path, data) {
|
|
const response = await fetch(`${urlPrefix}${path}`, {
|
|
method: "POST",
|
|
body: new URLSearchParams(data),
|
|
headers: {
|
|
"Content-Type": "application/x-www-form-urlencoded",
|
|
},
|
|
});
|
|
return await handle(response);
|
|
},
|
|
|
|
async get(path) {
|
|
const response = await fetch(`${urlPrefix}${path}`, {
|
|
method: "GET",
|
|
});
|
|
return await handle(response);
|
|
},
|
|
};
|