fipamo/src/libraries/FipamoContentAPI.js

191 lines
4.7 KiB
JavaScript
Raw Normal View History

//** REQUEST TYPES **//
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-17 22:26:04 +02:00
//** POST CONTENT TYPES **//
export const CONTENT_TYPE_JSON = "json";
export const CONTENT_TYPE_FORM = "x-www-form-urlencoded";
2021-06-17 22:26:04 +02:00
//** API URLS **//
export const API_GET_PAGES = "/api/v1/page/published";
2021-06-17 22:26:04 +02:00
export const API_GET_FEATURED = "/api/v1/page/featured";
export const API_GET_MENU = "/api/v1/page/menu";
export const API_GET_PAGE = "/api/v1/page/single";
2021-06-17 22:26:04 +02:00
export const API_GET_TAGS = "/api/v1/page/tags";
//** API TASKS **//
export const TASK_GET_CONTENT = "retrieveContent";
2021-06-17 22:26:04 +02:00
/**
* FipamoContentAPI
*/
2021-04-15 03:50:19 +02:00
export default class FipamoAPI {
//--------------------------
// constructor
//--------------------------
2021-06-17 22:26:04 +02:00
/**
* @constructor
* @param {string} baseURL - url of site
* @param {string} key - user api key
*/
constructor(baseURL = null, key = null) {
this.baseURL = null;
2021-06-17 22:26:04 +02:00
this.key = null;
if (key) this.key = key;
if (baseURL) this.baseURL = baseURL;
}
//--------------------------
// methods
//--------------------------
2021-06-17 22:26:04 +02:00
getPages(key) {
return new Promise((resolve, reject) => {
this._request(
2021-06-17 22:26:04 +02:00
this.baseURL
? this.baseURL + API_GET_PAGES + "?key=" + key
: API_GET_PAGES,
TASK_GET_CONTENT,
REQUEST_TYPE_GET
)
.then((result) => {
resolve(result);
})
.catch((err) => {
reject(err);
});
});
}
2021-06-17 22:26:04 +02:00
getFeatured(key) {
return new Promise((resolve, reject) => {
this._request(
2021-06-17 22:26:04 +02:00
this.baseURL
? this.baseURL + API_GET_FEATURED + "?key=" + key
: API_GET_FEATURED,
TASK_GET_CONTENT,
REQUEST_TYPE_GET
)
.then((result) => {
resolve(result);
})
.catch((err) => {
reject(err);
});
});
}
2021-06-17 22:26:04 +02:00
getMenu(key) {
2021-05-06 21:45:39 +02:00
return new Promise((resolve, reject) => {
this._request(
2021-06-17 22:26:04 +02:00
this.baseURL
? this.baseURL + API_GET_MENU + "?key=" + key
: API_GET_MENU,
TASK_GET_CONTENT,
REQUEST_TYPE_GET
2021-05-06 21:45:39 +02:00
)
.then((result) => {
resolve(result);
})
.catch((err) => {
reject(err);
});
});
}
2021-06-17 22:26:04 +02:00
getTags(key) {
2021-05-06 21:45:39 +02:00
return new Promise((resolve, reject) => {
this._request(
2021-06-14 22:16:23 +02:00
this.baseURL
2021-06-17 22:26:04 +02:00
? this.baseURL + API_GET_TAGS + "?key=" + key
: API_GET_TAGS,
TASK_GET_CONTENT,
REQUEST_TYPE_GET
2021-05-06 21:45:39 +02:00
)
.then((result) => {
resolve(result);
})
.catch((err) => {
reject(err);
});
});
}
getPage(id, key) {
return new Promise((resolve, reject) => {
this._request(
2021-06-14 22:16:23 +02:00
//API_GET_PAGE + "/" + id + "?key=" + key,
this.baseURL
? this.baseURL + API_GET_PAGE + "/" + id + "?key=" + key
: API_GET_PAGE,
TASK_GET_CONTENT,
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.setRequestHeader(
"Access-Control-Allow-Origin",
self.baseURL
);
**/
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);
//pass element to display request progress
}
}