Cosmos-Server/client/src/api/index.jsx

184 lines
4.3 KiB
React
Raw Normal View History

2023-05-01 17:33:58 +00:00
import * as _auth from './authentication';
import * as _users from './users';
import * as _config from './config';
import * as _docker from './docker';
2023-06-09 18:48:31 +00:00
import * as _market from './market';
2023-05-01 17:33:58 +00:00
import * as authDemo from './authentication.demo';
import * as usersDemo from './users.demo';
import * as configDemo from './config.demo';
import * as dockerDemo from './docker.demo';
import * as indexDemo from './index.demo';
2023-06-09 18:48:31 +00:00
import * as marketDemo from './market.demo';
2023-03-29 20:38:50 +00:00
import wrap from './wrap';
2023-05-01 17:33:58 +00:00
let getStatus = () => {
2023-03-29 20:38:50 +00:00
return wrap(fetch('/cosmos/api/status', {
method: 'GET',
headers: {
'Content-Type': 'application/json'
}
}))
}
2023-05-01 17:33:58 +00:00
let isOnline = () => {
2023-04-28 18:28:01 +00:00
return fetch('/cosmos/api/status', {
method: 'GET',
headers: {
'Content-Type': 'application/json'
}
}).then(async (response) => {
let rep;
try {
rep = await response.json();
} catch {
throw new Error('Server error');
}
if (response.status == 200) {
return rep;
}
const e = new Error(rep.message);
e.status = response.status;
throw e;
});
}
2023-05-19 15:23:05 +00:00
let newInstall = (req, onProgress) => {
if(req.step == '2') {
const requestOptions = {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(req)
};
return fetch('/cosmos/api/newInstall', requestOptions)
.then(response => {
if (!response.ok) {
throw new Error(response.statusText);
}
// The response body is a ReadableStream. This code reads the stream and passes chunks to the callback.
const reader = response.body.getReader();
// Read the stream and pass chunks to the callback as they arrive
return new ReadableStream({
start(controller) {
function read() {
return reader.read().then(({ done, value }) => {
if (done) {
controller.close();
return;
}
// Decode the UTF-8 text
let text = new TextDecoder().decode(value);
// Split by lines in case there are multiple lines in one chunk
let lines = text.split('\n');
for (let line of lines) {
if (line) {
// Call the progress callback
onProgress(line);
}
}
controller.enqueue(value);
return read();
});
}
return read();
}
});
}).catch((e) => {
alert(e);
});
} else {
return wrap(fetch('/cosmos/api/newInstall', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(req)
}))
}
2023-03-29 20:38:50 +00:00
}
2023-06-09 18:48:31 +00:00
let checkHost = (host) => {
2023-06-04 14:41:26 +00:00
return fetch('/cosmos/api/dns-check?url=' + host, {
method: 'GET',
headers: {
'Content-Type': 'application/json'
}
}).then(async (response) => {
let rep;
try {
rep = await response.json();
} catch {
throw new Error('Server error');
}
if (response.status == 200) {
return rep;
}
const e = new Error(rep.message);
e.status = response.status;
e.message = rep.message;
throw e;
});
}
2023-06-09 18:48:31 +00:00
let getDNS = (host) => {
2023-06-04 14:41:26 +00:00
return fetch('/cosmos/api/dns?url=' + host, {
method: 'GET',
headers: {
'Content-Type': 'application/json'
}
}).then(async (response) => {
let rep;
try {
rep = await response.json();
} catch {
throw new Error('Server error');
}
if (response.status == 200) {
return rep;
}
const e = new Error(rep.message);
e.status = response.status;
e.message = rep.message;
throw e;
});
}
2023-05-01 17:33:58 +00:00
const isDemo = import.meta.env.MODE === 'demo';
let auth = _auth;
let users = _users;
let config = _config;
let docker = _docker;
2023-06-09 18:48:31 +00:00
let market = _market;
2023-05-01 17:33:58 +00:00
if(isDemo) {
auth = authDemo;
users = usersDemo;
config = configDemo;
docker = dockerDemo;
2023-06-09 18:48:31 +00:00
market = marketDemo;
2023-05-01 17:33:58 +00:00
getStatus = indexDemo.getStatus;
newInstall = indexDemo.newInstall;
isOnline = indexDemo.isOnline;
2023-06-09 18:48:31 +00:00
checkHost = indexDemo.checkHost;
getDNS = indexDemo.getDNS;
2023-05-01 17:33:58 +00:00
}
2023-03-12 18:17:28 +00:00
export {
2023-03-13 00:49:27 +00:00
auth,
2023-03-16 18:56:36 +00:00
users,
2023-03-25 20:15:00 +00:00
config,
2023-03-29 20:38:50 +00:00
docker,
2023-06-09 18:48:31 +00:00
market,
2023-03-29 20:38:50 +00:00
getStatus,
newInstall,
2023-06-04 14:41:26 +00:00
isOnline,
checkHost,
getDNS
2023-03-12 18:17:28 +00:00
};