Browse Source

reorganized API methods into new files

tags/b2.2.0^2
Ro 1 month ago
parent
commit
45fd8be25f
5 changed files with 322 additions and 164 deletions
  1. +4
    -4
      public/assets/scripts/dash.min.js
  2. +6
    -6
      src/com/Base.js
  3. +14
    -77
      src/libraries/FipamoAdminAPI.js
  4. +40
    -77
      src/libraries/FipamoContentAPI.js
  5. +258
    -0
      src/libraries/FipamoUtilityAPI.js

+ 4
- 4
public/assets/scripts/dash.min.js
File diff suppressed because it is too large
View File


+ 6
- 6
src/com/Base.js View File

@@ -1,5 +1,5 @@
import FipamoApi from "../libraries/FipamoAPI";
//import FipamoAdminAPI from "../libraries/FipamoAdminAPI";
import FipamoAdminAPI from "../libraries/FipamoAdminAPI";
import FipamoUtilityAPI from "../libraries/FipamoUtilityAPI";
import DataUitls from "./utils/DataUtils";
import * as DataEvent from "./events/DataEvent";
import DashManager from "./controllers/DashManager";
@@ -62,7 +62,7 @@ export default class Base {
e.preventDefault();
let authForm = data.formDataToJSON(document.getElementById("login"));
notify.alert("Looking, hold up", null);
let api = new FipamoApi();
let api = new FipamoAdminAPI();
this.processing = true;
api
.login(authForm)
@@ -90,7 +90,7 @@ export default class Base {
e.stopPropagation();
e.preventDefault();
let setUpForm = data.formDataToJSON(document.getElementById("init-form"));
let api = new FipamoApi();
let api = new FipamoUtilityAPI();
this.processing = true;
api
.init(setUpForm)
@@ -117,7 +117,7 @@ export default class Base {
let self = this;
e.stopPropagation();
e.preventDefault();
let api = new FipamoApi();
let api = new FipamoUtilityAPI();
var form = document.getElementById("init-restore");
this.processing = true;
api
@@ -144,7 +144,7 @@ export default class Base {
e.stopPropagation();
e.preventDefault();
let self = this;
let api = new FipamoApi();
let api = new FipamoUtilityAPI();
if (e.target.id == "get-secret-btn") {
let data = {
email: document.getElementById("email").value,


+ 14
- 77
src/libraries/FipamoAdminAPI.js View File

@@ -12,17 +12,14 @@ export const API_GET_NAV = "/api/settings/nav";
export const API_NEW_PAGE = "/api/v1/page/create";
export const API_EDIT_PAGE = "/api/v1/page/write";
export const API_DELETE_PAGE = "/api/v1/page/delete";
export const API_IMAGE_UPLOAD = "/api/v1/page/add-entry-image";
export const API_SETTINGS_SYNC = "/api/v1/settings/sync";
export const API_UPLOAD_AVATAR = "/api/v1/settings/add-avatar";
export const API_UPLOAD_BACKGROUND = "/api/v1/settings/add-feature-background";
export const API_PUBLISH_PAGES = "/api/v1/settings/publish";
export const API_NAV_SYNC = "/api/v1/settings/nav-sync";
export const API_REINDEX_PAGES = "/api/v1/settings/reindex";
export const API_CREATE_BACKUP = "/api/v1/backup";
export const API_DOWNLOAD_BACKUP = "/api/v1/backup/download";
export const API_RESTORE_BACKUP = "/api/v1/backup/restore";
export const API_SEND_MAIL = "/api/v1/mailer";
export const API_LOGIN = "/api/v1/login";
export const AUTH_STATUS = "getAuthStatus";

import * as DataEvent from "../com/events/DataEvent";
export default class APIUtils {
//--------------------------
@@ -44,11 +41,11 @@ export default class APIUtils {
//--------------------------
// public
//--------------------------
syncSettings(data) {
login(data) {
return new Promise((resolve, reject) => {
this._request(
API_SETTINGS_SYNC,
DataEvent.API_SETTINGS_WRITE,
this.baseURL ? this.baseURL + API_LOGIN : API_LOGIN,
AUTH_STATUS,
REQUEST_TYPE_POST,
CONTENT_TYPE_JSON,
data
@@ -61,50 +58,24 @@ export default class APIUtils {
});
});
}
imageUpload(type, files) {
syncSettings(data) {
return new Promise((resolve, reject) => {
let url = "";
switch (type) {
case "avatar-upload":
url = API_UPLOAD_AVATAR;
break;
case "background-upload":
url = API_UPLOAD_BACKGROUND;
break;
default:
url = API_IMAGE_UPLOAD;
break;
}
var imageData = new FormData();
for (var i = 0; i < files.length; i++) {
var file = files[i];
// Check the file type.
if (!file.type.match("image.*")) {
continue;
}
if (type === "avatar-upload") {
imageData.append("avatar_upload", file, file.name);
} else if (type === "background-upload") {
imageData.append("background_upload", file, file.name);
} else {
imageData.append("post_image", file, file.name);
}
}
this._request(
url,
DataEvent.API_IMAGES_UPLOAD,
API_SETTINGS_SYNC,
DataEvent.API_SETTINGS_WRITE,
REQUEST_TYPE_POST,
CONTENT_TYPE_FORM,
imageData
CONTENT_TYPE_JSON,
data
)
.then((r) => {
resolve(r);
.then((result) => {
resolve(result);
})
.catch((err) => {
reject(err);
});
});
}

publishSite(data) {
return new Promise((resolve, reject) => {
this._request(
@@ -192,40 +163,6 @@ export default class APIUtils {
});
});
}
handleBackup(id, files) {
return new Promise((resolve, reject) => {
var url, event, method, type, data;

if (id === "create-backup") {
url = API_CREATE_BACKUP;
event = DataEvent.API_BACKUP_CREATE;
method = REQUEST_TYPE_POST;
type = CONTENT_TYPE_JSON;
data = { task: "create_backup" };
} else {
url = API_RESTORE_BACKUP;
event = DataEvent.API_BACKUP_RESTORE;
method = REQUEST_TYPE_POST;
type = CONTENT_TYPE_FORM;
data = new FormData();
for (var i = 0; i < files.length; i++) {
var file = files[i];
// Check the file type.
if (!file.type.match("application.zip")) {
continue;
}
data.append("backup_upload", file, file.name);
}
}
this._request(url, event, method, type, data)
.then((result) => {
resolve(result);
})
.catch((err) => {
reject(err);
});
});
}

handleReindex(data) {
return new Promise((resolve, reject) => {


src/libraries/FipamoAPI.js → src/libraries/FipamoContentAPI.js View File

@@ -3,66 +3,52 @@ 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_FEATURED = "/api/v1/page/featured";
export const API_GET_MENU = "/api/v1/page/menu";
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";
export const API_GET_TAGS = "/api/v1/page/tags";
//** 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";

/**
* FipamoContentAPI
*/
export default class FipamoAPI {
//--------------------------
// constructor
//--------------------------
constructor(baseURL = null) {

/**
* @constructor
* @param {string} baseURL - url of site
* @param {string} key - user api key
*/
constructor(baseURL = null, key = null) {
this.baseURL = null;
if (baseURL) {
this.baseURL = baseURL;
}
this.key = null;
if (key) this.key = key;
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) {
getPages(key) {
return new Promise((resolve, reject) => {
this._request(
API_INIT,
TASK_SITE_INIT,
REQUEST_TYPE_POST,
CONTENT_TYPE_JSON,
data
this.baseURL
? this.baseURL + API_GET_PAGES + "?key=" + key
: API_GET_PAGES,
TASK_GET_CONTENT,
REQUEST_TYPE_GET
)
.then((result) => {
resolve(result);
@@ -73,33 +59,14 @@ export default class FipamoAPI {
});
}

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) {
getFeatured(key) {
return new Promise((resolve, reject) => {
this._request(
API_GET_SECRET,
TASK_GET_SECRET,
REQUEST_TYPE_POST,
CONTENT_TYPE_JSON,
data
this.baseURL
? this.baseURL + API_GET_FEATURED + "?key=" + key
: API_GET_FEATURED,
TASK_GET_CONTENT,
REQUEST_TYPE_GET
)
.then((result) => {
resolve(result);
@@ -110,14 +77,14 @@ export default class FipamoAPI {
});
}

setNewPass(data) {
getMenu(key) {
return new Promise((resolve, reject) => {
this._request(
API_RESET_PASS,
TASK_RESET_PASS,
REQUEST_TYPE_POST,
CONTENT_TYPE_JSON,
data
this.baseURL
? this.baseURL + API_GET_MENU + "?key=" + key
: API_GET_MENU,
TASK_GET_CONTENT,
REQUEST_TYPE_GET
)
.then((result) => {
resolve(result);
@@ -128,16 +95,12 @@ export default class FipamoAPI {
});
}

//** CONTENT API METHODS **//

getPages(num, key) {
let pageNum = num;
if (pageNum === null || pageNum === "" || !pageNum) pageNum = 1;
getTags(key) {
return new Promise((resolve, reject) => {
this._request(
this.baseURL
? this.baseURL + API_GET_PAGES + "?key=" + key
: API_GET_PAGES,
? this.baseURL + API_GET_TAGS + "?key=" + key
: API_GET_TAGS,
TASK_GET_CONTENT,
REQUEST_TYPE_GET
)

+ 258
- 0
src/libraries/FipamoUtilityAPI.js View File

@@ -0,0 +1,258 @@
//** 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_GET_SECRET = "/api/v1/get-secret";
export const API_RESET_PASS = "/api/v1/reset-password";
export const API_CREATE_BACKUP = "/api/v1/backup";
export const API_DOWNLOAD_BACKUP = "/api/v1/backup/download";
export const API_RESTORE_BACKUP = "/api/v1/backup/restore";
export const API_UPLOAD_AVATAR = "/api/v1/settings/add-avatar";
export const API_UPLOAD_BACKGROUND = "/api/v1/settings/add-feature-background";
export const API_IMAGE_UPLOAD = "/api/v1/page/add-entry-image";
//** 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";

import * as DataEvent from "../com/events/DataEvent";

/**
* FipamoUtilityAPI
*/
export default class FipamoUtilityAPI {
//--------------------------
// constructor
//--------------------------

/**
* @constructor
*/
constructor() {}
//--------------------------
// methods
//--------------------------

//** MEMBER AUTH METHODS **//

initNew(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);
});
});
}

initRestore(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);
});
});
}

backup(id, files) {
return new Promise((resolve, reject) => {
var url, event, method, type, data;

if (id === "create-backup") {
url = API_CREATE_BACKUP;
event = DataEvent.API_BACKUP_CREATE;
method = REQUEST_TYPE_POST;
type = CONTENT_TYPE_JSON;
data = { task: "create_backup" };
} else {
url = API_RESTORE_BACKUP;
event = DataEvent.API_BACKUP_RESTORE;
method = REQUEST_TYPE_POST;
type = CONTENT_TYPE_FORM;
data = new FormData();
for (var i = 0; i < files.length; i++) {
var file = files[i];
// Check the file type.
if (!file.type.match("application.zip")) {
continue;
}
data.append("backup_upload", file, file.name);
}
}
this._request(url, event, method, type, data)
.then((result) => {
resolve(result);
})
.catch((err) => {
reject(err);
});
});
}

secret(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);
});
});
}

newPass(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);
});
});
}

imageUpload(type, files) {
return new Promise((resolve, reject) => {
let url = "";
switch (type) {
case "avatar-upload":
url = API_UPLOAD_AVATAR;
break;
case "background-upload":
url = API_UPLOAD_BACKGROUND;
break;
default:
url = API_IMAGE_UPLOAD;
break;
}
var imageData = new FormData();
for (var i = 0; i < files.length; i++) {
var file = files[i];
// Check the file type.
if (!file.type.match("image.*")) {
continue;
}
if (type === "avatar-upload") {
imageData.append("avatar_upload", file, file.name);
} else if (type === "background-upload") {
imageData.append("background_upload", file, file.name);
} else {
imageData.append("post_image", file, file.name);
}
}
this._request(
url,
DataEvent.API_IMAGES_UPLOAD,
REQUEST_TYPE_POST,
CONTENT_TYPE_FORM,
imageData
)
.then((r) => {
resolve(r);
})
.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
}
}

Loading…
Cancel
Save