You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
99 lines
3.0 KiB
JavaScript
99 lines
3.0 KiB
JavaScript
5 years ago
|
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';
|
||
4 years ago
|
export const API_STATUS = '/api/v1/auth/status';
|
||
|
export const API_LOGIN = '/api/v1/auth/login';
|
||
|
export const API_GET_NAV = '/api/settings/nav';
|
||
|
export const API_NEW_PAGE = '/api/v1/page/write/new';
|
||
|
export const API_EDIT_PAGE = '/api/v1/page/write';
|
||
|
export const API_DELETE_PAGE = '/api/v1/page/delete';
|
||
|
export const API_IMAGE_UPLOAD = '/api/v1/page/add-post-image';
|
||
|
export const API_SETTINGS_SYNC = '/api/v1/settings/sync';
|
||
|
export const API_UPLOAD_AVATAR = '/api/v1/settings/add-avatar';
|
||
|
export const API_UPLOAD_BACKGROUND = '/api/v1/settings/add-feature-background';
|
||
|
export const API_PUBLISH_PAGES = '/api/v1/settings/publish-pages';
|
||
|
export const API_NAV_SYNC = '/api/v1/settings/nav-sync';
|
||
|
import * as DataEvent from '../com/events/DataEvent';
|
||
5 years ago
|
export default class APIUtils {
|
||
|
//--------------------------
|
||
|
// constructor
|
||
|
//--------------------------
|
||
|
constructor() {
|
||
|
this.percentComplete = 0;
|
||
|
this.token = null;
|
||
4 years ago
|
//checks backend to see if user is logged in
|
||
|
//and requests encrypted token for api calls
|
||
5 years ago
|
this.request('/api/v1/auth/status').then(r => {
|
||
|
let response = JSON.parse(r.request['response']);
|
||
|
if (response.type === DataEvent.API_REQUEST_GOOD) {
|
||
|
this.token = response.token;
|
||
|
} else {
|
||
|
//don't set token
|
||
|
}
|
||
|
});
|
||
|
}
|
||
4 years ago
|
//--------------------------
|
||
|
// methods
|
||
|
//--------------------------
|
||
5 years ago
|
request(
|
||
|
requestURL,
|
||
|
eventType,
|
||
|
requestType = REQUEST_TYPE_GET,
|
||
|
contentType = CONTENT_TYPE_JSON,
|
||
|
requestData = null
|
||
|
) {
|
||
|
var self = this;
|
||
5 years ago
|
return new Promise(function (resolve, reject) {
|
||
5 years ago
|
var request = new XMLHttpRequest();
|
||
|
request.upload.onprogress = self.handleLoadProgress;
|
||
|
request.open(requestType, requestURL, true);
|
||
|
request.onload = () => {
|
||
|
if (request.status == 200) {
|
||
|
resolve({
|
||
|
request,
|
||
|
eventType
|
||
|
});
|
||
|
} else {
|
||
|
reject({
|
||
|
request,
|
||
|
eventType
|
||
|
});
|
||
|
}
|
||
|
};
|
||
|
if (requestType == REQUEST_TYPE_PUT || requestType == REQUEST_TYPE_POST) {
|
||
5 years ago
|
if (
|
||
|
eventType === DataEvent.API_PAGE_WRITE ||
|
||
|
eventType === DataEvent.API_IMAGES_UPLOAD ||
|
||
5 years ago
|
eventType === DataEvent.API_SETTINGS_WRITE ||
|
||
5 years ago
|
eventType === DataEvent.API_PAGE_DELETE ||
|
||
|
eventType === DataEvent.API_RENDER_PAGES
|
||
5 years ago
|
)
|
||
|
request.setRequestHeader('x-access-token', self.token);
|
||
5 years ago
|
|
||
5 years ago
|
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);
|
||
|
//this.emitEvent(DataEvent.UPLOAD_PROGRESS);
|
||
|
}
|
||
|
}
|