2021-04-01 21:54:03 +02:00
|
|
|
export const REQUEST_TYPE_POST = "POST";
|
|
|
|
export const REQUEST_TYPE_GET = "GET";
|
|
|
|
export const REQUEST_TYPE_PUT = "PUT";
|
|
|
|
export const REQUEST_TYPE_DELETE = "DELETE";
|
|
|
|
export const TASK_PAGE_CREATE = "createNewPage";
|
|
|
|
export const TASK_PAGE_EDIT = "editPage";
|
|
|
|
export const TASK_PAGE_DELETE = "deletePage";
|
|
|
|
export const CONTENT_TYPE_JSON = "json";
|
|
|
|
export const CONTENT_TYPE_FORM = "x-www-form-urlencoded";
|
|
|
|
export const API_STATUS = "/api/v1/status";
|
|
|
|
export const API_GET_NAV = "/api/settings/nav";
|
2021-04-14 00:20:57 +02:00
|
|
|
export const API_NEW_PAGE = "/api/v1/page/create";
|
2021-04-01 21:54:03 +02:00
|
|
|
export const API_EDIT_PAGE = "/api/v1/page/write";
|
|
|
|
export const API_DELETE_PAGE = "/api/v1/page/delete";
|
|
|
|
export const API_SETTINGS_SYNC = "/api/v1/settings/sync";
|
2021-04-16 07:19:59 +02:00
|
|
|
export const API_PUBLISH_PAGES = "/api/v1/settings/publish";
|
2021-04-01 21:54:03 +02:00
|
|
|
export const API_NAV_SYNC = "/api/v1/settings/nav-sync";
|
|
|
|
export const API_REINDEX_PAGES = "/api/v1/settings/reindex";
|
|
|
|
export const API_SEND_MAIL = "/api/v1/mailer";
|
2021-06-17 22:26:04 +02:00
|
|
|
export const API_LOGIN = "/api/v1/login";
|
|
|
|
export const AUTH_STATUS = "getAuthStatus";
|
|
|
|
|
2021-04-01 21:54:03 +02:00
|
|
|
import * as DataEvent from "../com/events/DataEvent";
|
2020-06-28 23:48:44 +02:00
|
|
|
export default class APIUtils {
|
2021-04-01 21:54:03 +02:00
|
|
|
//--------------------------
|
|
|
|
// constructor
|
|
|
|
//--------------------------
|
|
|
|
constructor() {
|
|
|
|
this.percentComplete = 0;
|
|
|
|
this.token = null;
|
|
|
|
//checks backend to see if user is logged in
|
|
|
|
//and requests encrypted token for api calls
|
|
|
|
this._request(API_STATUS).then((response) => {
|
|
|
|
if (response.type === DataEvent.API_REQUEST_GOOD) {
|
|
|
|
this.token = response.token;
|
|
|
|
} else {
|
|
|
|
//don't set token
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
//--------------------------
|
|
|
|
// public
|
|
|
|
//--------------------------
|
2021-06-17 22:26:04 +02:00
|
|
|
login(data) {
|
2021-04-01 21:54:03 +02:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
this._request(
|
2021-06-17 22:26:04 +02:00
|
|
|
this.baseURL ? this.baseURL + API_LOGIN : API_LOGIN,
|
|
|
|
AUTH_STATUS,
|
2021-04-01 21:54:03 +02:00
|
|
|
REQUEST_TYPE_POST,
|
|
|
|
CONTENT_TYPE_JSON,
|
|
|
|
data
|
|
|
|
)
|
|
|
|
.then((result) => {
|
|
|
|
resolve(result);
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
reject(err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2021-06-17 22:26:04 +02:00
|
|
|
syncSettings(data) {
|
2021-04-01 21:54:03 +02:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
this._request(
|
2021-06-17 22:26:04 +02:00
|
|
|
API_SETTINGS_SYNC,
|
|
|
|
DataEvent.API_SETTINGS_WRITE,
|
2021-04-01 21:54:03 +02:00
|
|
|
REQUEST_TYPE_POST,
|
2021-06-17 22:26:04 +02:00
|
|
|
CONTENT_TYPE_JSON,
|
|
|
|
data
|
2021-04-01 21:54:03 +02:00
|
|
|
)
|
2021-06-17 22:26:04 +02:00
|
|
|
.then((result) => {
|
|
|
|
resolve(result);
|
2021-04-01 21:54:03 +02:00
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
reject(err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2021-06-17 22:26:04 +02:00
|
|
|
|
2021-04-01 21:54:03 +02:00
|
|
|
publishSite(data) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
this._request(
|
|
|
|
API_PUBLISH_PAGES,
|
|
|
|
DataEvent.API_RENDER_PAGES,
|
|
|
|
REQUEST_TYPE_POST,
|
|
|
|
CONTENT_TYPE_JSON,
|
|
|
|
data
|
|
|
|
)
|
|
|
|
.then((result) => {
|
|
|
|
resolve(result);
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
reject(err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2020-06-28 23:48:44 +02:00
|
|
|
|
2021-04-01 21:54:03 +02:00
|
|
|
pageActions(task, data) {
|
|
|
|
let url, event, content;
|
|
|
|
switch (task) {
|
|
|
|
case TASK_PAGE_CREATE:
|
|
|
|
url = API_NEW_PAGE;
|
|
|
|
event = DataEvent.API_PAGE_WRITE;
|
|
|
|
content = CONTENT_TYPE_FORM;
|
|
|
|
break;
|
|
|
|
case TASK_PAGE_EDIT:
|
|
|
|
url = API_EDIT_PAGE;
|
|
|
|
event = DataEvent.API_PAGE_WRITE;
|
|
|
|
content = CONTENT_TYPE_FORM;
|
|
|
|
break;
|
2020-06-28 23:48:44 +02:00
|
|
|
|
2021-04-01 21:54:03 +02:00
|
|
|
case TASK_PAGE_DELETE:
|
|
|
|
url = API_DELETE_PAGE;
|
|
|
|
event = DataEvent.API_PAGE_DELETE;
|
|
|
|
content = CONTENT_TYPE_JSON;
|
|
|
|
break;
|
2020-06-28 23:48:44 +02:00
|
|
|
|
2021-04-01 21:54:03 +02:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2020-06-28 23:48:44 +02:00
|
|
|
|
2021-04-01 21:54:03 +02:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
this._request(url, event, REQUEST_TYPE_POST, content, data)
|
|
|
|
.then((result) => {
|
|
|
|
resolve(result);
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
reject(err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2020-06-28 23:48:44 +02:00
|
|
|
|
2021-04-01 21:54:03 +02:00
|
|
|
syncNav(data) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
this._request(
|
|
|
|
API_NAV_SYNC,
|
|
|
|
DataEvent.API_SETTINGS_WRITE,
|
|
|
|
REQUEST_TYPE_POST,
|
|
|
|
CONTENT_TYPE_JSON,
|
|
|
|
data
|
|
|
|
)
|
|
|
|
.then((result) => {
|
|
|
|
resolve(result);
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
reject(err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
sendMail(message) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
this._request(
|
|
|
|
API_SEND_MAIL,
|
|
|
|
DataEvent.SEND_MAIL,
|
|
|
|
REQUEST_TYPE_POST,
|
|
|
|
CONTENT_TYPE_JSON,
|
|
|
|
message
|
|
|
|
)
|
|
|
|
.then((result) => {
|
|
|
|
resolve(result);
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
reject(err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2020-09-30 00:26:30 +02:00
|
|
|
|
2021-04-01 21:54:03 +02:00
|
|
|
handleReindex(data) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
this._request(
|
|
|
|
API_REINDEX_PAGES,
|
|
|
|
DataEvent.API_REINDEX_PAGES,
|
|
|
|
REQUEST_TYPE_POST,
|
|
|
|
CONTENT_TYPE_JSON,
|
|
|
|
data
|
|
|
|
)
|
|
|
|
.then((result) => {
|
|
|
|
resolve(result);
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
reject(err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
//--------------------------
|
|
|
|
// private
|
|
|
|
//--------------------------
|
|
|
|
_request(
|
|
|
|
requestURL,
|
|
|
|
eventType,
|
|
|
|
requestType = REQUEST_TYPE_GET,
|
|
|
|
contentType = CONTENT_TYPE_JSON,
|
|
|
|
requestData = null
|
|
|
|
) {
|
|
|
|
var self = this;
|
|
|
|
return new Promise(function (resolve, reject) {
|
|
|
|
var request = new XMLHttpRequest();
|
|
|
|
request.upload.onprogress = self.handleLoadProgress;
|
|
|
|
request.open(requestType, requestURL, true);
|
|
|
|
request.onload = () => {
|
|
|
|
if (request.status == 200) {
|
2021-04-14 23:51:06 +02:00
|
|
|
//console.log("RESPONSE", request);
|
2021-04-01 21:54:03 +02:00
|
|
|
let response = JSON.parse(request["response"]);
|
|
|
|
resolve(response);
|
|
|
|
} else {
|
|
|
|
let error = JSON.parse(request["response"]);
|
|
|
|
reject(error);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
if (requestType == REQUEST_TYPE_PUT || requestType == REQUEST_TYPE_POST) {
|
|
|
|
if (
|
|
|
|
eventType === DataEvent.API_PAGE_WRITE ||
|
|
|
|
eventType === DataEvent.API_IMAGES_UPLOAD ||
|
|
|
|
eventType === DataEvent.API_SETTINGS_WRITE ||
|
|
|
|
eventType === DataEvent.API_PAGE_DELETE ||
|
|
|
|
eventType === DataEvent.API_RENDER_PAGES ||
|
|
|
|
eventType === DataEvent.API_BACKUP_CREATE ||
|
|
|
|
eventType === DataEvent.API_BACKUP_RESTORE ||
|
|
|
|
eventType === DataEvent.API_REINDEX_PAGES
|
|
|
|
)
|
2021-04-14 00:20:57 +02:00
|
|
|
request.setRequestHeader("fipamo-access-token", self.token);
|
2020-06-28 23:48:44 +02:00
|
|
|
|
2021-04-01 21:54:03 +02:00
|
|
|
switch (contentType) {
|
|
|
|
case CONTENT_TYPE_JSON:
|
|
|
|
request.setRequestHeader(
|
|
|
|
"Content-type",
|
|
|
|
"application/" + contentType
|
|
|
|
);
|
|
|
|
request.send(JSON.stringify(requestData));
|
|
|
|
break;
|
|
|
|
case CONTENT_TYPE_FORM:
|
|
|
|
request.send(requestData);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
request.send();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2020-06-28 23:48:44 +02:00
|
|
|
|
2021-04-01 21:54:03 +02:00
|
|
|
//--------------------------
|
|
|
|
// event handlers
|
|
|
|
//--------------------------
|
|
|
|
handleLoadProgress(e) {
|
|
|
|
this.percentComplete = Math.ceil((e.loaded / e.total) * 100);
|
|
|
|
//pass element to display request progress
|
|
|
|
}
|
2020-06-28 23:48:44 +02:00
|
|
|
}
|