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-17 22:26:04 +02:00
|
|
|
|
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-17 22:26:04 +02:00
|
|
|
|
2021-06-02 19:04:04 +02:00
|
|
|
//** API URLS **//
|
2021-04-01 21:54:03 +02:00
|
|
|
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";
|
2021-04-01 21:54:03 +02:00
|
|
|
export const API_GET_PAGE = "/api/v1/page/single";
|
2021-06-21 23:36:06 +02:00
|
|
|
export const API_GET_MENU = "/api/v1/page/menu";
|
2021-06-17 22:26:04 +02:00
|
|
|
export const API_GET_TAGS = "/api/v1/page/tags";
|
|
|
|
|
2021-06-02 19:04:04 +02:00
|
|
|
//** API TASKS **//
|
|
|
|
export const TASK_GET_CONTENT = "retrieveContent";
|
|
|
|
|
2021-06-17 22:26:04 +02:00
|
|
|
/**
|
2021-07-02 23:45:35 +02:00
|
|
|
* A bag of methods for getting content from an install.
|
2021-06-17 22:26:04 +02:00
|
|
|
*/
|
2021-06-25 23:53:37 +02:00
|
|
|
class FipamoContentAPI {
|
2021-06-17 22:26:04 +02:00
|
|
|
/**
|
|
|
|
* @constructor
|
2021-07-02 23:45:35 +02:00
|
|
|
* @param {string} baseURL - url of install, defaults to local
|
|
|
|
* @param {string} key - user api key found in Settings
|
2021-06-25 23:53:37 +02:00
|
|
|
* @author Ro
|
2021-06-17 22:26:04 +02:00
|
|
|
*/
|
|
|
|
constructor(baseURL = null, key = null) {
|
2021-06-09 22:36:08 +02:00
|
|
|
this.baseURL = null;
|
2021-06-17 22:26:04 +02:00
|
|
|
this.key = null;
|
|
|
|
if (key) this.key = key;
|
|
|
|
if (baseURL) this.baseURL = baseURL;
|
2021-06-09 22:36:08 +02:00
|
|
|
}
|
2021-06-02 19:04:04 +02:00
|
|
|
|
2021-06-21 23:36:06 +02:00
|
|
|
/**
|
2021-07-02 23:45:35 +02:00
|
|
|
* *Promise method for retrieving page data*\
|
|
|
|
* **GET**`/api/v1/page/:type`
|
|
|
|
* @param {string} type - type of pages (`published | menu | featured`) being retrieved; null value defaults to `published`
|
2021-06-25 23:53:37 +02:00
|
|
|
* @example
|
|
|
|
* api.pages('published').then(pages=>{
|
|
|
|
* console.log("Pages Object", pages);
|
|
|
|
* })
|
|
|
|
* @returns {object} json object that contains pages of requested type
|
|
|
|
*
|
2021-07-02 23:45:35 +02:00
|
|
|
* *pages object example*
|
|
|
|
* ```
|
|
|
|
{
|
|
|
|
"pages":
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"id":1,
|
|
|
|
"uuid":"uuid-for-entry",
|
|
|
|
"title":"Entry Title",
|
|
|
|
"feature":"/path/to/image.jpg",
|
|
|
|
"path":"2020/09",
|
|
|
|
"layout":"page",
|
|
|
|
"tags":"these, are, tags",
|
|
|
|
"author":"your-name",
|
|
|
|
"created":"2020 Sep Tue 01",
|
|
|
|
"updated":"2020 Sep Tue 01",
|
|
|
|
"deleted":false,
|
|
|
|
"menu":false,
|
|
|
|
"featured":false,
|
|
|
|
"published":true,
|
|
|
|
"slug":"entry-title",
|
|
|
|
"content":"Premium Content"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"id":2,
|
|
|
|
"uuid":"uuid-for-entry",
|
|
|
|
"title":"Another Title",
|
|
|
|
"feature":"/path/to/image.jpg",
|
|
|
|
"path":"2020/09",
|
|
|
|
"layout":"page",
|
|
|
|
"tags":"these, are, tags",
|
|
|
|
"author":"your-name",
|
|
|
|
"created":"2020 Sep Tue 01",
|
|
|
|
"updated":"2020 Sep Tue 01",
|
|
|
|
"deleted":false,
|
|
|
|
"menu":false,
|
|
|
|
"featured":false,
|
|
|
|
"published":true,
|
|
|
|
"slug":"another-title",
|
|
|
|
"content":"Premium Content"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"totalItems":2
|
|
|
|
}
|
|
|
|
* ```
|
|
|
|
*
|
2021-06-21 23:36:06 +02:00
|
|
|
*/
|
|
|
|
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;
|
|
|
|
}
|
2021-04-01 21:54:03 +02:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
this._request(
|
2021-06-21 23:36:06 +02:00
|
|
|
this.baseURL ? this.baseURL + requestURL : requestURL,
|
|
|
|
TASK_GET_CONTENT
|
2021-04-01 21:54:03 +02:00
|
|
|
)
|
|
|
|
.then((result) => {
|
|
|
|
resolve(result);
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
reject(err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2021-06-21 23:36:06 +02:00
|
|
|
/**
|
2021-07-02 23:45:35 +02:00
|
|
|
* *Promise method for retrieving single page*\
|
|
|
|
* **GET** `/api/v1/page/single/:id`
|
2021-06-21 23:36:06 +02:00
|
|
|
* @param {string} id - uuid of desired page
|
2021-06-25 23:53:37 +02:00
|
|
|
* @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
|
2021-07-02 23:45:35 +02:00
|
|
|
*
|
|
|
|
* *page object example*
|
|
|
|
* ```
|
|
|
|
{
|
|
|
|
"id":1,
|
|
|
|
"uuid":"uuid-for-entry",
|
|
|
|
"title":"Entry Title",
|
|
|
|
"feature":"/path/to/image.jpg",
|
|
|
|
"path":"2020/09",
|
|
|
|
"layout":"page",
|
|
|
|
"tags":"these, are, tags",
|
|
|
|
"author":"your-name",
|
|
|
|
"created":"2020 Sep Tue 01",
|
|
|
|
"updated":"2020 Sep Tue 01",
|
|
|
|
"deleted":false,
|
|
|
|
"menu":false,
|
|
|
|
"featured":false,
|
|
|
|
"published":true,
|
|
|
|
"slug":"entry-title",
|
|
|
|
"content":"Premium Content"
|
|
|
|
}
|
|
|
|
* ```
|
2021-06-21 23:36:06 +02:00
|
|
|
*/
|
|
|
|
page(id) {
|
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
|
2021-06-21 23:36:06 +02:00
|
|
|
? this.baseURL + API_GET_PAGE + "/" + id + "?key=" + this.key
|
|
|
|
: API_GET_PAGE + "/" + id + "?key=" + this.key,
|
2021-06-17 22:26:04 +02:00
|
|
|
TASK_GET_CONTENT,
|
|
|
|
REQUEST_TYPE_GET
|
2021-05-06 21:45:39 +02:00
|
|
|
)
|
|
|
|
.then((result) => {
|
|
|
|
resolve(result);
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
reject(err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-06-21 23:36:06 +02:00
|
|
|
/**
|
2021-07-02 23:45:35 +02:00
|
|
|
* *Promise method for retrieving all tags used by pages*\
|
|
|
|
* **GET** `/api/v1/page/tags`
|
2021-06-25 23:53:37 +02:00
|
|
|
* @example
|
|
|
|
* api.tags().then(tags=>{
|
|
|
|
console.log("Tags Object", tags);
|
|
|
|
* })
|
2021-07-02 23:45:35 +02:00
|
|
|
* @returns {object} json object that contains site tags and page stubs associated with said tag
|
|
|
|
*
|
|
|
|
* *tags object example*
|
|
|
|
* ```
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"tag_name":"this is a tag",
|
|
|
|
"slug":"this-is-a-tag",
|
|
|
|
"pages":
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"title":"This is a title",
|
|
|
|
"slug":"this-is-a-title",
|
|
|
|
"path":"2021/04"
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
"title":"This is another title",
|
|
|
|
"slug":"this-is-another-title",
|
|
|
|
"path":"2020/10"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"tag_name":"this is another tag",
|
|
|
|
"slug":"this-is-another-tag",
|
|
|
|
"pages":
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"title":"This is a title",
|
|
|
|
"slug":"this-is-a-title",
|
|
|
|
"path":"2021/04"
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
"title":"This is another title",
|
|
|
|
"slug":"this-is-another-title",
|
|
|
|
"path":"2020/10"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
]
|
|
|
|
* ```
|
2021-06-21 23:36:06 +02:00
|
|
|
*/
|
|
|
|
tags() {
|
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-21 23:36:06 +02:00
|
|
|
? this.baseURL + API_GET_TAGS + "?key=" + this.key
|
|
|
|
: API_GET_TAGS + "?key=" + this.key,
|
2021-06-02 19:04:04 +02:00
|
|
|
TASK_GET_CONTENT,
|
|
|
|
REQUEST_TYPE_GET
|
2021-05-06 21:45:39 +02:00
|
|
|
)
|
|
|
|
.then((result) => {
|
|
|
|
resolve(result);
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
reject(err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-04-01 21:54:03 +02:00
|
|
|
//--------------------------
|
|
|
|
// 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
|
|
|
}
|
2021-06-25 23:53:37 +02:00
|
|
|
|
|
|
|
export { FipamoContentAPI as default };
|