2021-06-02 19:04:04 +02:00
|
|
|
//** REQUEST TYPES **//
|
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";
|
2021-06-02 19:04:04 +02:00
|
|
|
//** POST CONTENT TYPES **//
|
2021-04-01 21:54:03 +02:00
|
|
|
export const CONTENT_TYPE_JSON = "json";
|
|
|
|
export const CONTENT_TYPE_FORM = "x-www-form-urlencoded";
|
2021-06-02 19:04:04 +02:00
|
|
|
//** API URLS **//
|
2021-04-01 21:54:03 +02:00
|
|
|
export const API_STATUS = "/api/v1/status";
|
|
|
|
export const API_INIT = "/api/v1/init";
|
2021-04-25 01:37:25 +02:00
|
|
|
export const API_RESTORE = "/api/v1/restore";
|
2021-04-01 21:54:03 +02:00
|
|
|
export const API_LOGIN = "/api/v1/login";
|
|
|
|
export const API_GET_PAGES = "/api/v1/page/published";
|
|
|
|
export const API_GET_PAGE = "/api/v1/page/single";
|
2021-05-06 21:45:39 +02:00
|
|
|
export const API_GET_SECRET = "/api/v1/get-secret";
|
|
|
|
export const API_RESET_PASS = "/api/v1/reset-password";
|
2021-06-02 19:04:04 +02:00
|
|
|
//** API TASKS **//
|
|
|
|
export const AUTH_STATUS = "getAuthStatus";
|
|
|
|
export const TASK_SITE_INIT = "blogInit";
|
|
|
|
export const TASK_BACKUP_RESTORE = "downloadBackup";
|
|
|
|
export const TASK_GET_SECRET = "retrieveSecret";
|
|
|
|
export const TASK_RESET_PASS = "resetPassword";
|
|
|
|
export const TASK_GET_CONTENT = "retrieveContent";
|
|
|
|
|
2021-04-15 03:50:19 +02:00
|
|
|
export default class FipamoAPI {
|
2021-04-01 21:54:03 +02:00
|
|
|
//--------------------------
|
|
|
|
// constructor
|
|
|
|
//--------------------------
|
|
|
|
constructor() {}
|
|
|
|
//--------------------------
|
2021-06-02 19:04:04 +02:00
|
|
|
// methods
|
2021-04-01 21:54:03 +02:00
|
|
|
//--------------------------
|
2021-06-02 19:04:04 +02:00
|
|
|
|
|
|
|
//** MEMBER AUTH METHODS **//
|
2021-04-01 21:54:03 +02:00
|
|
|
login(data) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
this._request(
|
|
|
|
API_LOGIN,
|
2021-06-02 19:04:04 +02:00
|
|
|
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);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
init(data) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
this._request(
|
|
|
|
API_INIT,
|
2021-06-02 19:04:04 +02:00
|
|
|
TASK_SITE_INIT,
|
2021-04-01 21:54:03 +02:00
|
|
|
REQUEST_TYPE_POST,
|
|
|
|
CONTENT_TYPE_JSON,
|
|
|
|
data
|
|
|
|
)
|
|
|
|
.then((result) => {
|
|
|
|
resolve(result);
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
reject(err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2021-04-25 01:37:25 +02:00
|
|
|
|
|
|
|
handleInitRestore(form) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
var url, event, method, type, data;
|
|
|
|
|
|
|
|
url = API_RESTORE;
|
2021-06-02 19:04:04 +02:00
|
|
|
event = TASK_BACKUP_RESTORE;
|
2021-04-25 01:37:25 +02:00
|
|
|
method = REQUEST_TYPE_POST;
|
|
|
|
type = CONTENT_TYPE_FORM;
|
|
|
|
data = new FormData(form);
|
|
|
|
this._request(url, event, method, type, data)
|
|
|
|
.then((result) => {
|
|
|
|
resolve(result);
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
reject(err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2021-06-02 19:04:04 +02:00
|
|
|
|
|
|
|
getSecret(data) {
|
2021-04-01 21:54:03 +02:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
this._request(
|
2021-06-02 19:04:04 +02:00
|
|
|
API_GET_SECRET,
|
|
|
|
TASK_GET_SECRET,
|
|
|
|
REQUEST_TYPE_POST,
|
|
|
|
CONTENT_TYPE_JSON,
|
|
|
|
data
|
2021-04-01 21:54:03 +02:00
|
|
|
)
|
|
|
|
.then((result) => {
|
|
|
|
resolve(result);
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
reject(err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2020-07-12 23:25:35 +02:00
|
|
|
|
2021-06-02 19:04:04 +02:00
|
|
|
setNewPass(data) {
|
2021-05-06 21:45:39 +02:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
this._request(
|
2021-06-02 19:04:04 +02:00
|
|
|
API_RESET_PASS,
|
|
|
|
TASK_RESET_PASS,
|
2021-05-06 21:45:39 +02:00
|
|
|
REQUEST_TYPE_POST,
|
|
|
|
CONTENT_TYPE_JSON,
|
|
|
|
data
|
|
|
|
)
|
|
|
|
.then((result) => {
|
|
|
|
resolve(result);
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
reject(err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-06-02 19:04:04 +02:00
|
|
|
//** CONTENT API METHODS **//
|
|
|
|
|
|
|
|
getPages(num, key) {
|
|
|
|
let pageNum = num;
|
|
|
|
if (pageNum === null || pageNum === "" || !pageNum) pageNum = 1;
|
2021-05-06 21:45:39 +02:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
this._request(
|
2021-06-02 19:04:04 +02:00
|
|
|
API_GET_PAGES + "/" + pageNum + "?key=" + key,
|
|
|
|
TASK_GET_CONTENT,
|
|
|
|
REQUEST_TYPE_GET
|
2021-05-06 21:45:39 +02:00
|
|
|
)
|
|
|
|
.then((result) => {
|
|
|
|
resolve(result);
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
reject(err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-06-02 19:04:04 +02:00
|
|
|
getPage(id, key) {
|
2021-04-01 21:54:03 +02:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
this._request(
|
2021-06-02 19:04:04 +02:00
|
|
|
API_GET_PAGE + "/" + id + "?key=" + key,
|
|
|
|
TASK_GET_CONTENT,
|
2021-04-01 21:54:03 +02:00
|
|
|
REQUEST_TYPE_GET
|
|
|
|
)
|
|
|
|
.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) {
|
|
|
|
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) {
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2019-12-02 22:07:16 +01: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
|
|
|
|
}
|
2019-12-02 22:07:16 +01:00
|
|
|
}
|