170 lines
4.5 KiB
JavaScript
170 lines
4.5 KiB
JavaScript
export default class API {
|
|
constructor() {
|
|
this.loggedIn = false;
|
|
this.user = null;
|
|
this.session = null;
|
|
}
|
|
|
|
csrfToken() {
|
|
return this.loggedIn ? this.session.csrfToken : null;
|
|
}
|
|
|
|
async apiCall(method, params) {
|
|
params = params || { };
|
|
params.csrfToken = this.csrfToken();
|
|
let response = await fetch("/api/" + method, {
|
|
method: 'post',
|
|
headers: {'Content-Type': 'application/json'},
|
|
body: JSON.stringify(params)
|
|
});
|
|
|
|
let res = await response.json();
|
|
if (!res.success && res.msg === "You are not logged in.") {
|
|
this.loggedIn = false;
|
|
}
|
|
|
|
return res;
|
|
}
|
|
|
|
/** Info **/
|
|
async info() {
|
|
return this.apiCall("info");
|
|
}
|
|
|
|
/** UserAPI **/
|
|
async login(username, password, rememberMe=false) {
|
|
return this.apiCall("user/login", { username: username, password: password, stayLoggedIn: rememberMe })
|
|
}
|
|
|
|
async fetchUser() {
|
|
let response = await fetch("/api/user/info");
|
|
let data = await response.json();
|
|
if (data) {
|
|
this.loggedIn = data["loggedIn"];
|
|
if (this.loggedIn) {
|
|
this.session = data["session"];
|
|
this.user = data["user"];
|
|
} else {
|
|
this.session = null;
|
|
this.user = null;
|
|
}
|
|
}
|
|
return data;
|
|
}
|
|
|
|
async editUser(id, username, email, password, groups, confirmed) {
|
|
return this.apiCall("user/edit", {
|
|
id: id, username: username, email: email,
|
|
password: password, groups: groups, confirmed: confirmed
|
|
});
|
|
}
|
|
|
|
async logout() {
|
|
const res = await this.apiCall("user/logout");
|
|
if (res.success) {
|
|
this.loggedIn = false;
|
|
}
|
|
|
|
return res;
|
|
}
|
|
|
|
async getUser(id) {
|
|
return this.apiCall("user/get", { id: id });
|
|
}
|
|
|
|
async deleteUser(id) {
|
|
return this.apiCall("user/delete", { id: id });
|
|
}
|
|
|
|
async fetchUsers(pageNum = 1, count = 20, orderBy = 'id', sortOrder = 'asc') {
|
|
return this.apiCall("user/fetch", { page: pageNum, count: count, orderBy: orderBy, sortOrder: sortOrder });
|
|
}
|
|
|
|
async fetchGroups(pageNum = 1, count = 20, orderBy = 'id', sortOrder = 'asc') {
|
|
return this.apiCall("groups/fetch", { page: pageNum, count: count, orderBy: orderBy, sortOrder: sortOrder });
|
|
}
|
|
|
|
async getGroup(id) {
|
|
return this.apiCall("groups/get", { id: id });
|
|
}
|
|
|
|
async inviteUser(username, email) {
|
|
return this.apiCall("user/invite", { username: username, email: email });
|
|
}
|
|
|
|
async createUser(username, email, password, confirmPassword) {
|
|
return this.apiCall("user/create", { username: username, email: email, password: password, confirmPassword: confirmPassword });
|
|
}
|
|
|
|
/** Stats **/
|
|
async getStats() {
|
|
return this.apiCall("stats");
|
|
}
|
|
|
|
/** RoutesAPI **/
|
|
async getRoutes() {
|
|
return this.apiCall("routes/fetch");
|
|
}
|
|
|
|
async saveRoutes(routes) {
|
|
return this.apiCall("routes/save", { routes: routes });
|
|
}
|
|
|
|
/** GroupAPI **/
|
|
async createGroup(name, color) {
|
|
return this.apiCall("groups/create", { name: name, color: color });
|
|
}
|
|
|
|
async deleteGroup(id) {
|
|
return this.apiCall("groups/delete", { id: id });
|
|
}
|
|
|
|
/** SettingsAPI **/
|
|
async getSettings(key = "") {
|
|
return this.apiCall("settings/get", { key: key });
|
|
}
|
|
|
|
async saveSettings(settings) {
|
|
return this.apiCall("settings/set", { settings: settings });
|
|
}
|
|
|
|
/** MailAPI **/
|
|
async sendTestMail(receiver) {
|
|
return this.apiCall("mail/test", { receiver: receiver });
|
|
}
|
|
|
|
/** PermissionAPI **/
|
|
async fetchPermissions() {
|
|
return this.apiCall("permission/fetch");
|
|
}
|
|
|
|
async savePermissions(permissions) {
|
|
return this.apiCall("permission/save", { permissions: permissions });
|
|
}
|
|
|
|
/** VisitorsAPI **/
|
|
async getVisitors(type, date) {
|
|
return this.apiCall("visitors/stats", { type: type, date: date });
|
|
}
|
|
|
|
/** LanguageAPI **/
|
|
async getLanguages() {
|
|
return this.apiCall("language/get");
|
|
}
|
|
|
|
async setLanguage(params) {
|
|
return await this.apiCall("language/set", params);
|
|
}
|
|
|
|
async getLanguageEntries(modules, code=null, useCache=false) {
|
|
if (!Array.isArray(modules)) {
|
|
modules = [modules];
|
|
}
|
|
|
|
return this.apiCall("language/getEntries", {code: code, modules: modules});
|
|
}
|
|
|
|
/*
|
|
|
|
} */
|
|
}; |