added FipamoAPI class

This commit is contained in:
Ro 2021-06-15 12:48:17 -07:00
parent 321903643a
commit 46691b454d
2 changed files with 230 additions and 39 deletions

42
.gitignore vendored
View file

@ -41,42 +41,6 @@ config/tags.json
config.codekit3 config.codekit3
/config/backups /config/backups
/src /src/com
/src/com/Base.js /src/style
/src/com/controllers/SettingsIndex.js /src/node_modules
/src/styles/main/_colors.sass
/src/styles/main/_forms.sass
/src/styles/main/_index.sass
/src/styles/main/_navigation.sass
/src/styles/main/_posts.sass
/src/styles/main/_settings.sass
/src/styles/main/_structure.sass
/src/com/Base.js
/src/com/controllers/SettingsIndex.js
/src/styles/main/_colors.sass
/src/styles/main/_forms.sass
/src/styles/main/_index.sass
/src/styles/main/_navigation.sass
/src/styles/main/_posts.sass
/src/styles/main/_settings.sass
/src/styles/main/_structure.sass
/src/com/Base.js
/src/com/controllers/SettingsIndex.js
/src/styles/main/_colors.sass
/src/styles/main/_forms.sass
/src/styles/main/_index.sass
/src/styles/main/_navigation.sass
/src/styles/main/_posts.sass
/src/styles/main/_settings.sass
/src/styles/main/_structure.sass
/src/com/Base.js
/src/com/actions/Mailer.js
/src/com/actions/NavActions.js
/src/com/actions/SettingsActions.js
/src/com/controllers/NavIndex.js
/src/com/controllers/PageEditor.js
/src/com/controllers/SettingsIndex.js
/src/com/ui/TextEditor.js
/src/libraries/FipamoAPI.js
/src/styles/main/_settings.sass
/src/libraries/FipamoAPI.js

227
src/libraries/FipamoAPI.js Normal file
View file

@ -0,0 +1,227 @@
//** 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_STATUS = "/api/v1/status";
export const API_INIT = "/api/v1/init";
export const API_RESTORE = "/api/v1/restore";
export const API_LOGIN = "/api/v1/login";
export const API_GET_PAGES = "/api/v1/page/published";
export const API_GET_PAGE = "/api/v1/page/single";
export const API_GET_SECRET = "/api/v1/get-secret";
export const API_RESET_PASS = "/api/v1/reset-password";
//** API TASKS **//
export const AUTH_STATUS = "getAuthStatus";
export const TASK_SITE_INIT = "blogInit";
export const TASK_BACKUP_RESTORE = "downloadBackup";
export const TASK_GET_SECRET = "retrieveSecret";
export const TASK_RESET_PASS = "resetPassword";
export const TASK_GET_CONTENT = "retrieveContent";
export default class FipamoAPI {
//--------------------------
// constructor
//--------------------------
constructor(baseURL = null) {
this.baseURL = null;
if (baseURL) {
this.baseURL = baseURL;
}
}
//--------------------------
// methods
//--------------------------
//** MEMBER AUTH METHODS **//
login(data) {
return new Promise((resolve, reject) => {
this._request(
this.baseURL ? this.baseURL + API_LOGIN : API_LOGIN,
AUTH_STATUS,
REQUEST_TYPE_POST,
CONTENT_TYPE_JSON,
data
)
.then((result) => {
resolve(result);
})
.catch((err) => {
reject(err);
});
});
}
init(data) {
return new Promise((resolve, reject) => {
this._request(
API_INIT,
TASK_SITE_INIT,
REQUEST_TYPE_POST,
CONTENT_TYPE_JSON,
data
)
.then((result) => {
resolve(result);
})
.catch((err) => {
reject(err);
});
});
}
handleInitRestore(form) {
return new Promise((resolve, reject) => {
var url, event, method, type, data;
url = API_RESTORE;
event = TASK_BACKUP_RESTORE;
method = REQUEST_TYPE_POST;
type = CONTENT_TYPE_FORM;
data = new FormData(form);
this._request(url, event, method, type, data)
.then((result) => {
resolve(result);
})
.catch((err) => {
reject(err);
});
});
}
getSecret(data) {
return new Promise((resolve, reject) => {
this._request(
API_GET_SECRET,
TASK_GET_SECRET,
REQUEST_TYPE_POST,
CONTENT_TYPE_JSON,
data
)
.then((result) => {
resolve(result);
})
.catch((err) => {
reject(err);
});
});
}
setNewPass(data) {
return new Promise((resolve, reject) => {
this._request(
API_RESET_PASS,
TASK_RESET_PASS,
REQUEST_TYPE_POST,
CONTENT_TYPE_JSON,
data
)
.then((result) => {
resolve(result);
})
.catch((err) => {
reject(err);
});
});
}
//** CONTENT API METHODS **//
getPages(num, key) {
let pageNum = num;
if (pageNum === null || pageNum === "" || !pageNum) pageNum = 1;
return new Promise((resolve, reject) => {
this._request(
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);
});
});
}
getPage(id, key) {
return new Promise((resolve, reject) => {
this._request(
//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
}
}