51 lines
No EOL
1.3 KiB
JavaScript
51 lines
No EOL
1.3 KiB
JavaScript
const endpoint = '/api'
|
|
|
|
const request = async (url, method, body) => {
|
|
|
|
let response;
|
|
|
|
if (method == 'GET') {
|
|
response = await fetch(endpoint + url, {
|
|
method,
|
|
headers: {
|
|
'Content-Type': 'application/json'
|
|
}
|
|
});
|
|
} else {
|
|
response = await fetch(endpoint + url, {
|
|
method,
|
|
body: JSON.stringify(body),
|
|
headers: {
|
|
'Content-Type': 'application/json'
|
|
}
|
|
});
|
|
}
|
|
|
|
if (response.status == 401) {
|
|
location.href = '/login'
|
|
}
|
|
const contentType = response.headers.get("content-type");
|
|
if (contentType && contentType.indexOf("application/json") !== -1) {
|
|
const json = await response.json()
|
|
return { status: response.status, msg: json.msg, json }
|
|
} else {
|
|
const msg = await response.text();
|
|
return { status: response.status, msg }
|
|
}
|
|
}
|
|
|
|
export const login = async (user, pass) => {
|
|
return await request('/login', 'POST', {user, pass})
|
|
}
|
|
|
|
export const domains = async () => {
|
|
return await request('/domains', 'GET')
|
|
}
|
|
|
|
export const del_domain = async (domain) => {
|
|
return await request('/domains', 'DELETE', {domain})
|
|
}
|
|
|
|
export const records = async (domain) => {
|
|
return await request(`/records?domain=${domain}`, 'GET')
|
|
} |