forked from projects/fipamo
184 lines
5 KiB
JavaScript
184 lines
5 KiB
JavaScript
//** 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";
|
|
|
|
//** POST CONTENT TYPES **//
|
|
export const CONTENT_TYPE_JSON = "json";
|
|
export const CONTENT_TYPE_FORM = "x-www-form-urlencoded";
|
|
|
|
//** API URLS **//
|
|
export const API_GET_PAGES = "/api/v1/page/published";
|
|
export const API_GET_FEATURED = "/api/v1/page/featured";
|
|
export const API_GET_PAGE = "/api/v1/page/single";
|
|
export const API_GET_MENU = "/api/v1/page/menu";
|
|
export const API_GET_TAGS = "/api/v1/page/tags";
|
|
|
|
//** API TASKS **//
|
|
export const TASK_GET_CONTENT = "retrieveContent";
|
|
|
|
/**
|
|
* Fipamo Content API
|
|
* A bag of methods for getting page info from an install.
|
|
* To use remotely, include url of install and user key found in settings.
|
|
*/
|
|
class FipamoContentAPI {
|
|
/**
|
|
* @constructor
|
|
* @param {string} baseURL - url of site; uses local when empty
|
|
* @param {string} key - user api key
|
|
* @author Ro
|
|
*/
|
|
constructor(baseURL = null, key = null) {
|
|
this.baseURL = null;
|
|
this.key = null;
|
|
if (key) this.key = key;
|
|
if (baseURL) this.baseURL = baseURL;
|
|
}
|
|
|
|
/**
|
|
* Promise method for retrieving page data
|
|
* @param {string} type - type of pages being retrieved; null value defaults to published
|
|
* @example
|
|
* api.pages('published').then(pages=>{
|
|
* console.log("Pages Object", pages);
|
|
* })
|
|
* @returns {object} json object that contains pages of requested type
|
|
*
|
|
*/
|
|
pages(type = null) {
|
|
//set url based on request type
|
|
let requestURL = "";
|
|
switch (type) {
|
|
default:
|
|
case "published":
|
|
requestURL = API_GET_PAGES + "?key=" + this.key;
|
|
break;
|
|
case "featured":
|
|
requestURL = API_GET_FEATURED + "?key=" + this.key;
|
|
break;
|
|
case "menu":
|
|
requestURL = API_GET_MENU + "?key=" + this.key;
|
|
break;
|
|
}
|
|
return new Promise((resolve, reject) => {
|
|
this._request(
|
|
this.baseURL ? this.baseURL + requestURL : requestURL,
|
|
TASK_GET_CONTENT
|
|
)
|
|
.then((result) => {
|
|
resolve(result);
|
|
})
|
|
.catch((err) => {
|
|
reject(err);
|
|
});
|
|
});
|
|
}
|
|
/**
|
|
* Promise method for retrieving single page
|
|
* @param {string} id - uuid of desired page
|
|
* @example
|
|
* api.page("a-uuid-for-a-page").then(page=>{
|
|
console.log("Page Object", page);
|
|
* })
|
|
* @returns {object} json object that contains data for requested page
|
|
*/
|
|
page(id) {
|
|
return new Promise((resolve, reject) => {
|
|
this._request(
|
|
this.baseURL
|
|
? this.baseURL + API_GET_PAGE + "/" + id + "?key=" + this.key
|
|
: API_GET_PAGE + "/" + id + "?key=" + this.key,
|
|
TASK_GET_CONTENT,
|
|
REQUEST_TYPE_GET
|
|
)
|
|
.then((result) => {
|
|
resolve(result);
|
|
})
|
|
.catch((err) => {
|
|
reject(err);
|
|
});
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Promise method for retrieving all tags used by pages
|
|
* @example
|
|
* api.tags().then(tags=>{
|
|
console.log("Tags Object", tags);
|
|
* })
|
|
* @returns {object} json object that contains tags used by pages
|
|
*/
|
|
tags() {
|
|
return new Promise((resolve, reject) => {
|
|
this._request(
|
|
this.baseURL
|
|
? this.baseURL + API_GET_TAGS + "?key=" + this.key
|
|
: API_GET_TAGS + "?key=" + this.key,
|
|
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.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
|
|
}
|
|
}
|
|
|
|
export { FipamoContentAPI as default };
|