2019-12-02 22:07:16 +01: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 CONTENT_TYPE_JSON = 'json';
|
|
|
|
export const CONTENT_TYPE_FORM = 'x-www-form-urlencoded';
|
2020-06-18 05:47:18 +02:00
|
|
|
export const API_STATUS = '/api/v1/auth/status';
|
2020-06-21 03:13:23 +02:00
|
|
|
export const API_INIT = '/api/v1/auth/init';
|
2020-06-18 05:47:18 +02:00
|
|
|
export const API_LOGIN = '/api/v1/auth/login';
|
2020-07-12 23:25:35 +02:00
|
|
|
export const API_GET_PAGES = '/api/v1/page/published';
|
|
|
|
export const API_GET_PAGE = '/api/v1/page/single';
|
2020-06-18 05:47:18 +02:00
|
|
|
import * as DataEvent from '../com/events/DataEvent';
|
2019-12-02 22:07:16 +01:00
|
|
|
export default class APIUtils {
|
|
|
|
//--------------------------
|
|
|
|
// constructor
|
|
|
|
//--------------------------
|
2020-06-28 23:48:44 +02:00
|
|
|
constructor() {}
|
2020-06-18 01:45:55 +02:00
|
|
|
//--------------------------
|
2020-06-26 23:05:20 +02:00
|
|
|
// public
|
2020-06-18 01:45:55 +02:00
|
|
|
//--------------------------
|
2020-06-26 23:05:20 +02:00
|
|
|
login(data) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
this._request(
|
|
|
|
API_LOGIN,
|
|
|
|
DataEvent.AUTH_STATUS,
|
|
|
|
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, DataEvent.API_INIT, REQUEST_TYPE_POST, CONTENT_TYPE_JSON, data)
|
|
|
|
.then(result => {
|
|
|
|
resolve(result);
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
reject(err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2020-07-12 23:25:35 +02:00
|
|
|
getPages(num) {
|
|
|
|
let pageNum = num;
|
|
|
|
if (pageNum === null || pageNum === '' || !pageNum) pageNum = 1;
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
this._request(API_GET_PAGES + '/' + pageNum, DataEvent.API_GET_PAGES, REQUEST_TYPE_GET)
|
|
|
|
.then(result => {
|
|
|
|
resolve(result);
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
reject(err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
getPage(id) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
this._request(API_GET_PAGE + '/' + id, DataEvent.API_GET_PAGES, REQUEST_TYPE_GET)
|
|
|
|
.then(result => {
|
|
|
|
resolve(result);
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
reject(err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2020-06-26 23:05:20 +02:00
|
|
|
//--------------------------
|
|
|
|
// private
|
|
|
|
//--------------------------
|
|
|
|
_request(
|
2019-12-02 22:07:16 +01:00
|
|
|
requestURL,
|
|
|
|
eventType,
|
|
|
|
requestType = REQUEST_TYPE_GET,
|
|
|
|
contentType = CONTENT_TYPE_JSON,
|
|
|
|
requestData = null
|
|
|
|
) {
|
|
|
|
var self = this;
|
2020-05-05 22:44:45 +02:00
|
|
|
return new Promise(function (resolve, reject) {
|
2019-12-02 22:07:16 +01:00
|
|
|
var request = new XMLHttpRequest();
|
|
|
|
request.upload.onprogress = self.handleLoadProgress;
|
|
|
|
request.open(requestType, requestURL, true);
|
|
|
|
request.onload = () => {
|
|
|
|
if (request.status == 200) {
|
2020-06-24 23:14:17 +02:00
|
|
|
let response = JSON.parse(request['response']);
|
|
|
|
resolve(response);
|
2019-12-02 22:07:16 +01:00
|
|
|
} else {
|
2020-06-24 23:14:17 +02:00
|
|
|
let error = JSON.parse(request['response']);
|
|
|
|
reject(error);
|
2019-12-02 22:07:16 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------
|
|
|
|
// event handlers
|
|
|
|
//--------------------------
|
|
|
|
handleLoadProgress(e) {
|
|
|
|
this.percentComplete = Math.ceil((e.loaded / e.total) * 100);
|
2020-06-19 01:21:33 +02:00
|
|
|
//pass element to display request progress
|
2019-12-02 22:07:16 +01:00
|
|
|
}
|
|
|
|
}
|