added backup apis and tests

This commit is contained in:
Gani Georgiev
2023-05-13 22:10:14 +03:00
parent 3b0f60fe15
commit e8b4a7eb26
104 changed files with 3192 additions and 1017 deletions
+2 -2
View File
@@ -31,7 +31,7 @@ PocketBase.prototype.logout = function(redirect = true) {
* @param {Boolean} notify Whether to add a toast notification.
* @param {String} defaultMsg Default toast notification message if the error doesn't have one.
*/
PocketBase.prototype.errorResponseHandler = function(err, notify = true, defaultMsg = "") {
PocketBase.prototype.error = function(err, notify = true, defaultMsg = "") {
if (!err || !(err instanceof Error) || err.isAbort) {
return;
}
@@ -88,7 +88,7 @@ PocketBase.prototype.getAdminFileToken = async function(collectionId = "") {
let token = localStorage.getItem(adminFileTokenKey) || "";
// request a new token only if the previous one is missing or will expire soon
if (!token || isTokenExpired(token, 15)) {
if (!token || isTokenExpired(token, 10)) {
// remove previously stored token (if any)
token && localStorage.removeItem(adminFileTokenKey);
+80 -56
View File
@@ -285,7 +285,7 @@ export default class CommonHelper {
const result = JSON.parse(JSON.stringify(obj || {}));
for (let prop in result) {
if (typeof result[prop] === 'object' && result[prop] !== null) {
if (typeof result[prop] === "object" && result[prop] !== null) {
result[prop] = CommonHelper.filterRedactedProps(result[prop], mask)
} else if (result[prop] === mask) {
delete result[prop];
@@ -313,7 +313,7 @@ export default class CommonHelper {
*/
static getNestedVal(data, path, defaultVal = null, delimiter = ".") {
let result = data || {};
let parts = (path || '').split(delimiter);
let parts = (path || "").split(delimiter);
for (const part of parts) {
if (
@@ -343,7 +343,7 @@ export default class CommonHelper {
* @param {String} delimiter
*/
static setByPath(data, path, newValue, delimiter = ".") {
if (data === null || typeof data !== 'object') {
if (data === null || typeof data !== "object") {
console.warn("setByPath: data not an object or array.");
return
}
@@ -382,7 +382,7 @@ export default class CommonHelper {
*/
static deleteByPath(data, path, delimiter = ".") {
let result = data || {};
let parts = (path || '').split(delimiter);
let parts = (path || "").split(delimiter);
let lastPart = parts.pop();
for (const part of parts) {
@@ -533,7 +533,7 @@ export default class CommonHelper {
for (let key in obj) {
let value = obj[key];
if (typeof value === 'string') {
if (typeof value === "string") {
value = CommonHelper.truncate(value, 150, true);
}
@@ -551,47 +551,47 @@ export default class CommonHelper {
* @param {Array} [preserved]
* @return {String}
*/
static slugify(str, delimiter = '_', preserved = ['.', '=', '-']) {
if (str === '') {
return '';
static slugify(str, delimiter = "_", preserved = [".", "=", "-"]) {
if (str === "") {
return "";
}
// special characters
const specialCharsMap = {
'a': /а|à|á|å|â/gi,
'b': /б/gi,
'c': /ц|ç/gi,
'd': /д/gi,
'e': /е|è|é|ê|ẽ|ë/gi,
'f': /ф/gi,
'g': /г/gi,
'h': /х/gi,
'i': /й|и|ì|í|î/gi,
'j': /ж/gi,
'k': /к/gi,
'l': /л/gi,
'm': /м/gi,
'n': /н|ñ/gi,
'o': /о|ò|ó|ô|ø/gi,
'p': /п/gi,
'q': /я/gi,
'r': /р/gi,
's': /с/gi,
't': /т/gi,
'u': /ю|ù|ú|ů|û/gi,
'v': /в/gi,
'w': /в/gi,
'x': /ь/gi,
'y': /ъ/gi,
'z': /з/gi,
'ae': /ä|æ/gi,
'oe': /ö/gi,
'ue': /ü/gi,
'Ae': /Ä/gi,
'Ue': /Ü/gi,
'Oe': /Ö/gi,
'ss': /ß/gi,
'and': /&/gi
"a": /а|à|á|å|â/gi,
"b": /б/gi,
"c": /ц|ç/gi,
"d": /д/gi,
"e": /е|è|é|ê|ẽ|ë/gi,
"f": /ф/gi,
"g": /г/gi,
"h": /х/gi,
"i": /й|и|ì|í|î/gi,
"j": /ж/gi,
"k": /к/gi,
"l": /л/gi,
"m": /м/gi,
"n": /н|ñ/gi,
"o": /о|ò|ó|ô|ø/gi,
"p": /п/gi,
"q": /я/gi,
"r": /р/gi,
"s": /с/gi,
"t": /т/gi,
"u": /ю|ù|ú|ů|û/gi,
"v": /в/gi,
"w": /в/gi,
"x": /ь/gi,
"y": /ъ/gi,
"z": /з/gi,
"ae": /ä|æ/gi,
"oe": /ö/gi,
"ue": /ü/gi,
"Ae": /Ä/gi,
"Ue": /Ü/gi,
"Oe": /Ö/gi,
"ss": /ß/gi,
"and": /&/gi
};
// replace special characters
@@ -600,8 +600,8 @@ export default class CommonHelper {
}
return str
.replace(new RegExp('[' + preserved.join('') + ']', 'g'), ' ') // replace preserved characters with spaces
.replace(/[^\w\ ]/gi, '') // replaces all non-alphanumeric with empty string
.replace(new RegExp('[' + preserved.join("") + ']', 'g'), ' ') // replace preserved characters with spaces
.replace(/[^\w\ ]/gi, "") // replaces all non-alphanumeric with empty string
.replace(/\s+/g, delimiter); // collapse whitespaces and replace with `delimiter`
}
@@ -666,7 +666,7 @@ export default class CommonHelper {
* @return {String}
*/
static getInitials(str) {
str = (str || '').split('@')[0].trim();
str = (str || "").split("@")[0].trim();
if (str.length <= 2) {
return str.toUpperCase();
@@ -681,6 +681,18 @@ export default class CommonHelper {
return str[0].toUpperCase();
}
/**
* Returns a human readable file size string from size in bytes.
*
* @param {Number} size s
* @return {String}
*/
static formattedFileSize(size) {
const i = size ? Math.floor(Math.log(size) / Math.log(1024)) : 0;
return (size / Math.pow(1024, i)).toFixed(2) * 1 + " " + ["B", "KB", "MB", "GB", "TB"][i];
}
/**
* Returns a DateTime instance from a date object/string.
*
@@ -688,7 +700,7 @@ export default class CommonHelper {
* @return {DateTime}
*/
static getDateTime(date) {
if (typeof date === 'string') {
if (typeof date === "string") {
const formats = {
19: "yyyy-MM-dd HH:mm:ss",
23: "yyyy-MM-dd HH:mm:ss.SSS",
@@ -696,7 +708,7 @@ export default class CommonHelper {
24: "yyyy-MM-dd HH:mm:ss.SSS'Z'",
}
const format = formats[date.length] || formats[19];
return DateTime.fromFormat(date, format, { zone: 'UTC' });
return DateTime.fromFormat(date, format, { zone: "UTC" });
}
return DateTime.fromJSDate(date);
@@ -709,7 +721,7 @@ export default class CommonHelper {
* @param {String} [format] The result format (see https://moment.github.io/luxon/#/parsing?id=table-of-tokens)
* @return {String}
*/
static formatToUTCDate(date, format = 'yyyy-MM-dd HH:mm:ss') {
static formatToUTCDate(date, format = "yyyy-MM-dd HH:mm:ss") {
return CommonHelper.getDateTime(date).toUTC().toFormat(format);
}
@@ -720,7 +732,7 @@ export default class CommonHelper {
* @param {String} [format] The result format (see https://moment.github.io/luxon/#/parsing?id=table-of-tokens)
* @return {String}
*/
static formatToLocalDate(date, format = 'yyyy-MM-dd HH:mm:ss') {
static formatToLocalDate(date, format = "yyyy-MM-dd HH:mm:ss") {
return CommonHelper.getDateTime(date).toLocal().toFormat(format);
}
@@ -742,20 +754,32 @@ export default class CommonHelper {
})
}
/**
* Forces the browser to start downloading the specified url.
*
* @param {String} url The url of the file to download.
* @param {String} name The result file name.
*/
static download(url, name) {
const tempLink = document.createElement("a");
tempLink.setAttribute("href", url);
tempLink.setAttribute("download", name);
tempLink.click();
tempLink.remove();
}
/**
* Downloads a json file created from the provide object.
*
* @param {mixed} obj The JS object to download.
* @param {String} name The result file name.
* @param {String} name The result file name.
*/
static downloadJson(obj, name) {
const encodedObj = "data:text/json;charset=utf-8," + encodeURIComponent(JSON.stringify(obj, null, 2));
const tempLink = document.createElement('a');
tempLink.setAttribute("href", encodedObj);
tempLink.setAttribute("download", name + ".json");
tempLink.click();
tempLink.remove();
name = name.endsWith(".json") ? name : (name + ".json");
download(encodedObj, name)
}
/**
@@ -765,7 +789,7 @@ export default class CommonHelper {
* @return {Object}
*/
static getJWTPayload(jwt) {
const raw = (jwt || '').split(".")[1] || '';
const raw = (jwt || "").split(".")[1] || "";
if (raw === "") {
return {};
}