2021-06-22 22:18:16 +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-22 22:18:16 +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-22 22:18:16 +02:00
|
|
|
//** API URLS **//
|
2021-04-01 21:54:03 +02:00
|
|
|
export const API_STATUS = "/api/v1/status";
|
|
|
|
export const API_GET_NAV = "/api/settings/nav";
|
2021-04-14 00:20:57 +02:00
|
|
|
export const API_NEW_PAGE = "/api/v1/page/create";
|
2021-04-01 21:54:03 +02:00
|
|
|
export const API_EDIT_PAGE = "/api/v1/page/write";
|
|
|
|
export const API_DELETE_PAGE = "/api/v1/page/delete";
|
|
|
|
export const API_SETTINGS_SYNC = "/api/v1/settings/sync";
|
2021-04-16 07:19:59 +02:00
|
|
|
export const API_PUBLISH_PAGES = "/api/v1/settings/publish";
|
2021-04-01 21:54:03 +02:00
|
|
|
export const API_NAV_SYNC = "/api/v1/settings/nav-sync";
|
|
|
|
export const API_REINDEX_PAGES = "/api/v1/settings/reindex";
|
|
|
|
export const API_SEND_MAIL = "/api/v1/mailer";
|
2021-06-17 22:26:04 +02:00
|
|
|
export const API_LOGIN = "/api/v1/login";
|
2021-06-22 22:18:16 +02:00
|
|
|
//** API TASKS **//
|
2021-06-17 22:26:04 +02:00
|
|
|
export const AUTH_STATUS = "getAuthStatus";
|
2021-06-22 22:18:16 +02:00
|
|
|
export const TASK_SETTINGS_WRITE = "writeSettings";
|
|
|
|
export const TASK_PUBLISH_SITE = "publishSite";
|
|
|
|
export const TASK_PAGE_CREATE = "createNewPage";
|
|
|
|
export const TASK_PAGE_EDIT = "editPage";
|
|
|
|
export const TASK_PAGE_DELETE = "deletePage";
|
|
|
|
export const TASK_SEND_MAIL = "sendMail";
|
|
|
|
export const TASK_REINDEX_PAGE = "reIndexPages";
|
2021-06-24 20:58:55 +02:00
|
|
|
//** API STATUS **//
|
2021-06-28 22:47:06 +02:00
|
|
|
export const API_ACCESS_GOOD = "apiUseAuthorized";
|
|
|
|
export const API_ACCESS_BAD = "apiUseNotAuthorized";
|
2021-06-17 22:26:04 +02:00
|
|
|
|
2021-06-22 22:18:16 +02:00
|
|
|
/**
|
2021-06-27 22:48:17 +02:00
|
|
|
* A can of methods used to edit install settings, navigation pages.
|
2021-06-22 22:18:16 +02:00
|
|
|
*/
|
|
|
|
|
2021-06-25 23:53:37 +02:00
|
|
|
class FipamoAdminAPI {
|
2021-06-22 22:18:16 +02:00
|
|
|
/**
|
|
|
|
* @constructor
|
2021-06-27 22:48:17 +02:00
|
|
|
* @param {string} baseURL - url of site; uses local when empty
|
|
|
|
* @param {string} key - user api key
|
2021-06-22 22:18:16 +02:00
|
|
|
*/
|
2021-06-25 23:53:37 +02:00
|
|
|
constructor(baseURL = null, key = null) {
|
|
|
|
this.percentComplete = 0; //for later
|
2021-04-01 21:54:03 +02:00
|
|
|
this.token = null;
|
2021-06-25 23:53:37 +02:00
|
|
|
this.baseURL = null;
|
|
|
|
this.key = null;
|
|
|
|
if (key) this.key = key;
|
|
|
|
if (baseURL) this.baseURL = baseURL;
|
2021-06-28 22:47:06 +02:00
|
|
|
//if key is valid, checks to see if a session is active and returns
|
|
|
|
this._request(
|
|
|
|
this.baseURL
|
|
|
|
? this.baseURL + API_STATUS + "?key=" + this.key
|
|
|
|
: API_STATUS + "?key=" + this.key
|
|
|
|
).then((response) => {
|
2021-06-22 22:18:16 +02:00
|
|
|
if (response.type === API_ACCESS_GOOD) {
|
2021-04-01 21:54:03 +02:00
|
|
|
this.token = response.token;
|
|
|
|
} else {
|
|
|
|
//don't set token
|
2021-06-28 22:47:06 +02:00
|
|
|
//console.log("NO TOKEN");
|
2021-04-01 21:54:03 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2021-06-22 22:18:16 +02:00
|
|
|
/**
|
2021-06-28 22:47:06 +02:00
|
|
|
* Promise method for checking credentials. Must login to use Admin API.
|
2021-06-27 22:48:17 +02:00
|
|
|
* @param {object} data - json object that contains data for set up
|
2021-06-22 22:18:16 +02:00
|
|
|
* @property {string} handle - handle for site user
|
|
|
|
* @property {string} password - password for site user
|
|
|
|
*/
|
2021-06-17 22:26:04 +02:00
|
|
|
login(data) {
|
2021-04-01 21:54:03 +02:00
|
|
|
return new Promise((resolve, reject) => {
|
2021-06-28 22:47:06 +02:00
|
|
|
this.baseURL ? (data.remote = true) : (data.remote = false);
|
|
|
|
this.key ? (data.key = this.key) : (data.key = null);
|
2021-04-01 21:54:03 +02:00
|
|
|
this._request(
|
2021-06-17 22:26:04 +02:00
|
|
|
this.baseURL ? this.baseURL + API_LOGIN : API_LOGIN,
|
|
|
|
AUTH_STATUS,
|
2021-04-01 21:54:03 +02:00
|
|
|
REQUEST_TYPE_POST,
|
|
|
|
CONTENT_TYPE_JSON,
|
|
|
|
data
|
|
|
|
)
|
|
|
|
.then((result) => {
|
|
|
|
resolve(result);
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
reject(err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2021-06-22 22:18:16 +02:00
|
|
|
/**
|
|
|
|
* Method for saving settings
|
|
|
|
* @param {object} data - json object that contains settings data set on the front-end
|
|
|
|
* @property {string} global.base_url - base url for site
|
|
|
|
* @property {string} global.title - site title
|
|
|
|
* @property {string} global.descriptions - brief site summary
|
|
|
|
* @property {string} global.background - url for site feature image for header
|
|
|
|
* @property {boolean} global.private - privacy state for site [disabled]
|
|
|
|
* @property {boolean} global.renderOnSave - property for publishing site when page saved [disabled]
|
|
|
|
* @property {string} global.theme - current theme for site
|
|
|
|
* @property {boolean} global.externalAPI - toggle for external API access
|
|
|
|
* @property {string} member.handle - current member handle
|
|
|
|
* @property {string} member.email - current member email
|
|
|
|
* @property {string} email.active - current email protocol being used
|
|
|
|
* @property {string} email.smtp.domain - url of smtp service being
|
|
|
|
* @property {string} email.smtp.email - email account of smtp service
|
|
|
|
* @property {string} email.smtp.password - password for email of smtp service
|
|
|
|
* @property {string} email.mailgun.domain - mailgun domain url
|
|
|
|
* @property {string} email.mailgun.key - mailgun key
|
|
|
|
*/
|
2021-06-17 22:26:04 +02:00
|
|
|
syncSettings(data) {
|
2021-04-01 21:54:03 +02:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
this._request(
|
2021-06-28 22:47:06 +02:00
|
|
|
this.baseURL
|
|
|
|
? this.baseURL + API_SETTINGS_SYNC + "?key=" + this.key
|
|
|
|
: API_SETTINGS_SYNC + "?key=" + this.key,
|
2021-06-22 22:18:16 +02:00
|
|
|
TASK_SETTINGS_WRITE,
|
2021-04-01 21:54:03 +02:00
|
|
|
REQUEST_TYPE_POST,
|
2021-06-17 22:26:04 +02:00
|
|
|
CONTENT_TYPE_JSON,
|
|
|
|
data
|
2021-04-01 21:54:03 +02:00
|
|
|
)
|
2021-06-17 22:26:04 +02:00
|
|
|
.then((result) => {
|
|
|
|
resolve(result);
|
2021-04-01 21:54:03 +02:00
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
reject(err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2021-06-17 22:26:04 +02:00
|
|
|
|
2021-06-22 22:18:16 +02:00
|
|
|
/**
|
|
|
|
* Method for retrieving user authorizing user login
|
|
|
|
* @param {object} data - json object that contains task
|
|
|
|
* @property {string} task - publishing task [deprecated]
|
|
|
|
*/
|
2021-04-01 21:54:03 +02:00
|
|
|
publishSite(data) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
this._request(
|
2021-06-27 22:48:17 +02:00
|
|
|
//API_PUBLISH_PAGES,
|
|
|
|
this.baseURL ? this.baseURL + API_PUBLISH_PAGES : API_PUBLISH_PAGES,
|
2021-06-22 22:18:16 +02:00
|
|
|
TASK_PUBLISH_SITE,
|
2021-04-01 21:54:03 +02:00
|
|
|
REQUEST_TYPE_POST,
|
|
|
|
CONTENT_TYPE_JSON,
|
|
|
|
data
|
|
|
|
)
|
|
|
|
.then((result) => {
|
|
|
|
resolve(result);
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
reject(err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2020-06-28 23:48:44 +02:00
|
|
|
|
2021-06-22 22:18:16 +02:00
|
|
|
/**
|
|
|
|
* Method for handling page creating and editing
|
|
|
|
* @param {string} task - current page action
|
|
|
|
* @param {object} data - form object that contains info for current page being edited/created
|
|
|
|
* @property {string} id - sequence id for page
|
|
|
|
* @property {string} uuid - unique identifier for page
|
|
|
|
* @property {string} layout - current page layout
|
|
|
|
* @property {string} current_title - saved url save title for persistence when changing title
|
|
|
|
* @property {string} content - markdown body of page
|
|
|
|
* @property {string} title - current title of page
|
|
|
|
* @property {string} created - date page was created
|
|
|
|
* @property {string} slug - url safe string of page title
|
|
|
|
* @property {string} tags - comma separated list of tags
|
|
|
|
* @property {boolean} menu - property that indicates page is included in site menu
|
|
|
|
* @property {boolean} featured - property that indicates page is featured
|
|
|
|
* @property {boolean} published - property that indicates page is public
|
|
|
|
* @property {input} feature_image - main image for page
|
|
|
|
*/
|
2021-04-01 21:54:03 +02:00
|
|
|
pageActions(task, data) {
|
|
|
|
let url, event, content;
|
|
|
|
switch (task) {
|
|
|
|
case TASK_PAGE_CREATE:
|
|
|
|
url = API_NEW_PAGE;
|
2021-06-22 22:18:16 +02:00
|
|
|
event = TASK_PAGE_CREATE;
|
2021-04-01 21:54:03 +02:00
|
|
|
content = CONTENT_TYPE_FORM;
|
|
|
|
break;
|
|
|
|
case TASK_PAGE_EDIT:
|
|
|
|
url = API_EDIT_PAGE;
|
2021-06-22 22:18:16 +02:00
|
|
|
event = TASK_PAGE_EDIT;
|
2021-04-01 21:54:03 +02:00
|
|
|
content = CONTENT_TYPE_FORM;
|
|
|
|
break;
|
2020-06-28 23:48:44 +02:00
|
|
|
|
2021-04-01 21:54:03 +02:00
|
|
|
case TASK_PAGE_DELETE:
|
|
|
|
url = API_DELETE_PAGE;
|
2021-06-22 22:18:16 +02:00
|
|
|
event = TASK_PAGE_DELETE;
|
2021-04-01 21:54:03 +02:00
|
|
|
content = CONTENT_TYPE_JSON;
|
|
|
|
break;
|
2020-06-28 23:48:44 +02:00
|
|
|
|
2021-04-01 21:54:03 +02:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2020-06-28 23:48:44 +02:00
|
|
|
|
2021-06-28 22:47:06 +02:00
|
|
|
if (this.baseURL) {
|
|
|
|
data.key = this.key;
|
|
|
|
data.remote = true;
|
|
|
|
} else {
|
|
|
|
data.remote = false;
|
|
|
|
}
|
|
|
|
|
2021-04-01 21:54:03 +02:00
|
|
|
return new Promise((resolve, reject) => {
|
2021-06-27 22:48:17 +02:00
|
|
|
this._request(
|
|
|
|
this.baseURL ? this.baseURL + url : url,
|
|
|
|
event,
|
|
|
|
REQUEST_TYPE_POST,
|
|
|
|
content,
|
|
|
|
data
|
|
|
|
)
|
2021-04-01 21:54:03 +02:00
|
|
|
.then((result) => {
|
|
|
|
resolve(result);
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
reject(err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2020-06-28 23:48:44 +02:00
|
|
|
|
2021-06-22 22:18:16 +02:00
|
|
|
/**
|
|
|
|
* Method for saving pages to be included in main site navigation
|
|
|
|
* @param {object} data - json object that contains items to be included in main site navigation
|
|
|
|
* @property {string} item.title - page title
|
|
|
|
* @property {string} item.slug - url safe title
|
|
|
|
* @property {string} item.uuid - unique identifier
|
|
|
|
* @property {string} item.path - directory path to associated markdown file
|
|
|
|
*/
|
2021-04-01 21:54:03 +02:00
|
|
|
syncNav(data) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
this._request(
|
2021-06-27 22:48:17 +02:00
|
|
|
this.baseURL ? this.baseURL + API_NAV_SYNC : API_NAV_SYNC,
|
2021-06-22 22:18:16 +02:00
|
|
|
TASK_SETTINGS_WRITE,
|
2021-04-01 21:54:03 +02:00
|
|
|
REQUEST_TYPE_POST,
|
|
|
|
CONTENT_TYPE_JSON,
|
|
|
|
data
|
|
|
|
)
|
|
|
|
.then((result) => {
|
|
|
|
resolve(result);
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
reject(err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2021-06-22 22:18:16 +02:00
|
|
|
/**
|
|
|
|
* Method for sending mail
|
|
|
|
* @param {object} message - json object that contains items to be included in main site navigation
|
|
|
|
* @property {string} content - message to send
|
|
|
|
*/
|
2021-04-01 21:54:03 +02:00
|
|
|
sendMail(message) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
this._request(
|
2021-06-27 22:48:17 +02:00
|
|
|
this.baseURL ? this.baseURL + API_SEND_MAIL : API_SEND_MAIL,
|
2021-06-22 22:18:16 +02:00
|
|
|
TASK_SEND_MAIL,
|
2021-04-01 21:54:03 +02:00
|
|
|
REQUEST_TYPE_POST,
|
|
|
|
CONTENT_TYPE_JSON,
|
|
|
|
message
|
|
|
|
)
|
|
|
|
.then((result) => {
|
|
|
|
resolve(result);
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
reject(err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2021-06-22 22:18:16 +02:00
|
|
|
/**
|
|
|
|
* Maintenance method to clean up page sequencing [disabled]
|
|
|
|
* @param {object} data - json object that contains items to be included in main site navigation
|
|
|
|
*/
|
|
|
|
reindexPages(data) {
|
2021-04-01 21:54:03 +02:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
this._request(
|
2021-06-27 22:48:17 +02:00
|
|
|
this.baseURL ? this.baseURL + API_REINDEX_PAGES : API_REINDEX_PAGES,
|
2021-06-22 22:18:16 +02:00
|
|
|
TASK_REINDEX_PAGE,
|
2021-04-01 21:54:03 +02:00
|
|
|
REQUEST_TYPE_POST,
|
|
|
|
CONTENT_TYPE_JSON,
|
|
|
|
data
|
|
|
|
)
|
|
|
|
.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) {
|
|
|
|
if (
|
2021-06-22 22:18:16 +02:00
|
|
|
eventType === TASK_SETTINGS_WRITE ||
|
|
|
|
eventType === TASK_PAGE_EDIT ||
|
2021-06-30 23:06:13 +02:00
|
|
|
eventType === TASK_PAGE_CREATE ||
|
2021-06-22 22:18:16 +02:00
|
|
|
eventType === TASK_PAGE_DELETE ||
|
|
|
|
eventType === TASK_PUBLISH_SITE ||
|
|
|
|
eventType === TASK_REINDEX_PAGE
|
2021-04-01 21:54:03 +02:00
|
|
|
)
|
2021-04-14 00:20:57 +02:00
|
|
|
request.setRequestHeader("fipamo-access-token", self.token);
|
2020-06-28 23:48:44 +02:00
|
|
|
|
2021-04-01 21:54:03 +02:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2020-06-28 23:48:44 +02: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
|
|
|
|
}
|
2020-06-28 23:48:44 +02:00
|
|
|
}
|
2021-06-25 23:53:37 +02:00
|
|
|
|
|
|
|
export { FipamoAdminAPI as default };
|