Skip to content
Snippets Groups Projects
Verified Commit 0f105d32 authored by Volker Schukai's avatar Volker Schukai :alien:
Browse files

chore: code format rome

parent 7556b994
No related branches found
No related tags found
No related merge requests found
Showing
with 256 additions and 353 deletions
......@@ -8,11 +8,7 @@
* @author schukai GmbH
*/
export {
internalSymbol,
internalStateSymbol,
instanceSymbol
}
export { internalSymbol, internalStateSymbol, instanceSymbol };
/**
* @private
......@@ -21,7 +17,7 @@ export {
* @license AGPLv3
* @since 1.24.0
*/
const internalSymbol = Symbol.for('@schukai/monster/internal');
const internalSymbol = Symbol.for("@schukai/monster/internal");
/**
* @private
......@@ -30,11 +26,10 @@ const internalSymbol = Symbol.for('@schukai/monster/internal');
* @license AGPLv3
* @since 1.25.0
*/
const internalStateSymbol = Symbol.for('@schukai/monster/state');
const internalStateSymbol = Symbol.for("@schukai/monster/state");
/**
* @private
* @type {symbol}
*/
const instanceSymbol = Symbol.for('@schukai/monster/instance');
const instanceSymbol = Symbol.for("@schukai/monster/instance");
......@@ -5,10 +5,10 @@
* License text available at https://www.gnu.org/licenses/agpl-3.0.en.html
*/
import {Base} from '../types/base.mjs';
import {instanceSymbol} from '../constants.mjs';
import { Base } from "../types/base.mjs";
import { instanceSymbol } from "../constants.mjs";
export {AbstractConstraint}
export { AbstractConstraint };
/**
* Constraints are used to define conditions that must be met by the value of a variable.
......@@ -26,7 +26,6 @@ export {AbstractConstraint}
* @summary The abstract constraint
*/
class AbstractConstraint extends Base {
/**
*
*/
......@@ -52,5 +51,4 @@ class AbstractConstraint extends Base {
static get [instanceSymbol]() {
return Symbol.for("@schukai/monster/constraints/abstract-constraint");
}
}
......@@ -6,8 +6,8 @@
*/
import { AbstractConstraint } from "./abstract.mjs";
import {instanceSymbol} from '../constants.mjs';
export {AbstractOperator}
import { instanceSymbol } from "../constants.mjs";
export { AbstractOperator };
/**
* Constraints are used to define conditions that must be met by the value of a variable.
......@@ -24,7 +24,6 @@ export {AbstractOperator}
* @summary The abstract operator constraint
*/
class AbstractOperator extends AbstractConstraint {
/**
*
* @param {AbstractConstraint} operantA
......@@ -34,13 +33,12 @@ class AbstractOperator extends AbstractConstraint {
constructor(operantA, operantB) {
super();
if (!((operantA instanceof AbstractConstraint) && (operantB instanceof AbstractConstraint))) {
throw new TypeError("parameters must be from type AbstractConstraint")
if (!(operantA instanceof AbstractConstraint && operantB instanceof AbstractConstraint)) {
throw new TypeError("parameters must be from type AbstractConstraint");
}
this.operantA = operantA;
this.operantB = operantB;
}
/**
......@@ -51,8 +49,4 @@ class AbstractOperator extends AbstractConstraint {
static get [instanceSymbol]() {
return Symbol.for("@schukai/monster/constraints/abstract-operator");
}
}
......@@ -6,8 +6,8 @@
*/
import { AbstractOperator } from "./abstractoperator.mjs";
import {instanceSymbol} from '../constants.mjs';
export {AndOperator}
import { instanceSymbol } from "../constants.mjs";
export { AndOperator };
/**
* Constraints are used to define conditions that must be met by the value of a variable.
......@@ -24,7 +24,6 @@ export {AndOperator}
* @summary A and operator constraint
*/
class AndOperator extends AbstractOperator {
/**
* this method return a promise containing the result of the check.
*
......@@ -43,6 +42,4 @@ class AndOperator extends AbstractOperator {
static get [instanceSymbol]() {
return Symbol.for("@schukai/monster/constraints/and-operator");
}
}
......@@ -6,8 +6,8 @@
*/
import { AbstractConstraint } from "./abstract.mjs";
import {instanceSymbol} from '../constants.mjs';
export {Invalid}
import { instanceSymbol } from "../constants.mjs";
export { Invalid };
/**
* Constraints are used to define conditions that must be met by the value of a variable.
......@@ -24,7 +24,6 @@ export {Invalid}
* @summary A constraint that always invalid
*/
class Invalid extends AbstractConstraint {
/**
* this method return a rejected promise
*
......@@ -43,6 +42,4 @@ class Invalid extends AbstractConstraint {
static get [instanceSymbol]() {
return Symbol.for("@schukai/monster/constraints/invalid");
}
}
......@@ -7,8 +7,8 @@
import { isArray } from "../types/is.mjs";
import { AbstractConstraint } from "./abstract.mjs";
import {instanceSymbol} from '../constants.mjs';
export {IsArray}
import { instanceSymbol } from "../constants.mjs";
export { IsArray };
/**
* Constraints are used to define conditions that must be met by the value of a variable.
......@@ -23,7 +23,6 @@ export {IsArray}
* @summary A constraint to check if a value is an array
*/
class IsArray extends AbstractConstraint {
/**
* this method return a promise containing the result of the check.
*
......@@ -46,6 +45,4 @@ class IsArray extends AbstractConstraint {
static get [instanceSymbol]() {
return Symbol.for("@schukai/monster/constraint/is-array");
}
}
......@@ -7,8 +7,8 @@
import { isObject } from "../types/is.mjs";
import { AbstractConstraint } from "./abstract.mjs";
import {instanceSymbol} from '../constants.mjs';
export {IsObject}
import { instanceSymbol } from "../constants.mjs";
export { IsObject };
/**
* Constraints are used to define conditions that must be met by the value of a variable.
......@@ -23,7 +23,6 @@ export {IsObject}
* @summary A constraint to check if a value is an object
*/
class IsObject extends AbstractConstraint {
/**
* this method return a promise containing the result of the check.
*
......@@ -46,6 +45,4 @@ class IsObject extends AbstractConstraint {
static get [instanceSymbol]() {
return Symbol.for("@schukai/monster/constraint/is-object");
}
}
......@@ -3,8 +3,6 @@
* SPDX-License-Identifier: AGPL-3.0
*/
/**
* Constraints are used to define conditions that must be met by the value of a variable so that the value can be transferred to the system.
*
......
......@@ -6,8 +6,8 @@
*/
import { AbstractOperator } from "./abstractoperator.mjs";
import {instanceSymbol} from '../constants.mjs';
export {OrOperator}
import { instanceSymbol } from "../constants.mjs";
export { OrOperator };
/**
* Constraints are used to define conditions that must be met by the value of a variable.
......@@ -24,7 +24,6 @@ export {OrOperator}
* @summary A or operator
*/
class OrOperator extends AbstractOperator {
/**
* this method return a promise containing the result of the check.
*
......@@ -38,10 +37,12 @@ class OrOperator extends AbstractOperator {
let a;
let b;
self.operantA.isValid(value)
self.operantA
.isValid(value)
.then(function () {
resolve();
}).catch(function () {
})
.catch(function () {
a = false;
/** b has already been evaluated and was not true */
if (b === false) {
......@@ -49,10 +50,12 @@ class OrOperator extends AbstractOperator {
}
});
self.operantB.isValid(value)
self.operantB
.isValid(value)
.then(function () {
resolve();
}).catch(function () {
})
.catch(function () {
b = false;
/** b has already been evaluated and was not true */
if (a === false) {
......@@ -70,7 +73,4 @@ class OrOperator extends AbstractOperator {
static get [instanceSymbol]() {
return Symbol.for("@schukai/monster/constraints/or-operator");
}
}
......@@ -6,8 +6,8 @@
*/
import { AbstractConstraint } from "./abstract.mjs";
import {instanceSymbol} from '../constants.mjs';
export {Valid}
import { instanceSymbol } from "../constants.mjs";
export { Valid };
/**
* Constraints are used to define conditions that must be met by the value of a variable.
......@@ -24,7 +24,6 @@ export {Valid}
* @summary A constraint that always valid
*/
class Valid extends AbstractConstraint {
/**
* this method return a promise containing the result of the check.
*
......@@ -43,6 +42,4 @@ class Valid extends AbstractConstraint {
static get [instanceSymbol]() {
return Symbol.for("@schukai/monster/constraints/valid");
}
}
......@@ -10,14 +10,13 @@ import {validateString} from "../types/validate.mjs";
import { clone } from "../util/clone.mjs";
import { DELIMITER, Pathfinder, WILDCARD } from "./pathfinder.mjs";
export {buildMap, PARENT, assembleParts}
export { buildMap, PARENT, assembleParts };
/**
* @type {string}
* @memberOf Monster.Data
*/
const PARENT = '^';
const PARENT = "^";
/**
* With the help of the function `buildMap()`, maps can be easily created from data objects.
......@@ -47,10 +46,8 @@ function buildMap(subject, selector, valueTemplate, keyTemplate, filter) {
v = build(v, valueTemplate);
this.set(k, v);
});
}
/**
* @private
* @param {*} subject
......@@ -61,20 +58,19 @@ function buildMap(subject, selector, valueTemplate, keyTemplate, filter) {
* @throws {TypeError} selector is neither a string nor a function
*/
function assembleParts(subject, selector, filter, callback) {
const result = new Map();
let map;
if (isFunction(selector)) {
map = selector(subject)
map = selector(subject);
if (!(map instanceof Map)) {
throw new TypeError('the selector callback must return a map');
throw new TypeError("the selector callback must return a map");
}
} else if (isString(selector)) {
map = new Map;
map = new Map();
buildFlatMap.call(map, subject, selector);
} else {
throw new TypeError('selector is neither a string nor a function')
throw new TypeError("selector is neither a string nor a function");
}
if (!(map instanceof Map)) {
......@@ -87,7 +83,6 @@ function assembleParts(subject, selector, filter, callback) {
}
callback.call(result, v, k, m);
});
return result;
......@@ -102,9 +97,8 @@ function assembleParts(subject, selector, filter, callback) {
* @return {*}
*/
function buildFlatMap(subject, selector, key, parentMap) {
const result = this;
const currentMap = new Map;
const currentMap = new Map();
const resultLength = result.size;
......@@ -114,12 +108,10 @@ function buildFlatMap(subject, selector, key, parentMap) {
let current = "";
let currentPath = [];
do {
current = parts.shift();
currentPath.push(current);
if (current === WILDCARD) {
let finder = new Pathfinder(subject);
let map;
......@@ -131,12 +123,11 @@ function buildFlatMap(subject, selector, key, parentMap) {
}
for (const [k, o] of map) {
let copyKey = clone(key);
currentPath.map((a) => {
copyKey.push((a === WILDCARD) ? k : a)
})
copyKey.push(a === WILDCARD ? k : a);
});
let kk = copyKey.join(DELIMITER);
let sub = buildFlatMap.call(result, o, parts.join(DELIMITER), copyKey, o);
......@@ -147,10 +138,7 @@ function buildFlatMap(subject, selector, key, parentMap) {
currentMap.set(kk, sub);
}
}
} while (parts.length > 0);
// no set in child run
......@@ -161,10 +149,8 @@ function buildFlatMap(subject, selector, key, parentMap) {
}
return subject;
}
/**
* With the help of this filter callback, values can be filtered out. Only if the filter function returns true, the value is taken for the map.
*
......@@ -319,7 +305,7 @@ function build(subject, definition, defaultValue) {
if (definition === undefined) return defaultValue ? defaultValue : subject;
validateString(definition);
const regexp = /(?<placeholder>\${(?<path>[a-z\^A-Z.\-_0-9]*)})/gm
const regexp = /(?<placeholder>\${(?<path>[a-z\^A-Z.\-_0-9]*)})/gm;
const array = [...definition.matchAll(regexp)];
let finder = new Pathfinder(subject);
......@@ -329,20 +315,17 @@ function build(subject, definition, defaultValue) {
}
array.forEach((a) => {
let groups = a?.['groups'];
let placeholder = groups?.['placeholder']
let groups = a?.["groups"];
let placeholder = groups?.["placeholder"];
if (placeholder === undefined) return;
let path = groups?.['path']
let path = groups?.["path"];
let v = finder.getVia(path);
if (v === undefined) v = defaultValue;
definition = definition.replaceAll(placeholder, v);
})
});
return definition;
}
......@@ -11,19 +11,19 @@ import {NodeList} from "../types/nodelist.mjs";
import { assembleParts } from "./buildmap.mjs";
import { extend } from "./extend.mjs";
export {buildTree}
export { buildTree };
/**
* @private
* @type {symbol}
*/
const parentSymbol = Symbol('parent');
const parentSymbol = Symbol("parent");
/**
* @private
* @type {symbol}
*/
const rootSymbol = Symbol('root');
const rootSymbol = Symbol("root");
/**
* @typedef {Object} buildTreeOptions
......@@ -49,17 +49,20 @@ const rootSymbol = Symbol('root');
* @since 1.26.0
*/
function buildTree(subject, selector, idKey, parentIDKey, options) {
const nodes = new Map;
const nodes = new Map();
if (!isObject(options)) {
options = {}
options = {};
}
options = extend({}, {
options = extend(
{},
{
rootReferences: [null, undefined],
filter: undefined
}, options)
filter: undefined,
},
options,
);
const filter = options?.filter;
let rootReferences = options.rootReferences;
......@@ -68,13 +71,12 @@ function buildTree(subject, selector, idKey, parentIDKey, options) {
}
const childMap = assembleParts(subject, selector, filter, function (o, k, m) {
const key = o?.[idKey]
let ref = o?.[parentIDKey]
const key = o?.[idKey];
let ref = o?.[parentIDKey];
if (rootReferences.indexOf(ref) !== -1) ref = rootSymbol;
if (key === undefined) {
throw new Error('the object has no value for the specified id')
throw new Error("the object has no value for the specified id");
}
o[parentSymbol] = ref;
......@@ -82,28 +84,26 @@ function buildTree(subject, selector, idKey, parentIDKey, options) {
const node = new Node(o);
this.has(ref) ? this.get(ref).add(node) : this.set(ref, new NodeList().add(node));
nodes.set(key, node);
});
})
nodes.forEach(node => {
let id = node?.['value']?.[idKey];
nodes.forEach((node) => {
let id = node?.["value"]?.[idKey];
if (childMap.has(id)) {
node.childNodes = childMap.get(id);
childMap.delete(id)
childMap.delete(id);
}
})
});
const list = new NodeList;
const list = new NodeList();
childMap.forEach((s) => {
if (s instanceof Set) {
s.forEach((n) => {
list.add(n);
})
});
}
})
});
return list;
}
......@@ -6,7 +6,7 @@
*/
import { internalSymbol } from "../constants.mjs";
import {instanceSymbol} from '../constants.mjs';
import { instanceSymbol } from "../constants.mjs";
import { Base } from "../types/base.mjs";
import { parseDataURL } from "../types/dataurl.mjs";
import { isString } from "../types/is.mjs";
......@@ -15,8 +15,7 @@ import {validateObject} from "../types/validate.mjs";
import { extend } from "./extend.mjs";
import { Pathfinder } from "./pathfinder.mjs";
export {Datasource}
export { Datasource };
/**
* This callback can be passed to a datasource and is used to adapt data structures.
......@@ -28,7 +27,6 @@ export {Datasource}
* @see Monster.Data.Datasource
*/
/**
* @private
* @type {symbol}
......@@ -36,7 +34,7 @@ export {Datasource}
* @license AGPLv3
* @since 1.24.0
*/
const internalDataSymbol = Symbol.for('@schukai/monster/data/datasource/@@data');
const internalDataSymbol = Symbol.for("@schukai/monster/data/datasource/@@data");
/**
* The datasource class is the basis for dealing with different data sources.
......@@ -49,7 +47,6 @@ const internalDataSymbol = Symbol.for('@schukai/monster/data/datasource/@@data')
* @summary The datasource class encapsulates the access to data objects.
*/
class Datasource extends Base {
/**
* creates a new datasource
*
......@@ -57,14 +54,10 @@ class Datasource extends Base {
constructor() {
super();
this[internalSymbol] = new ProxyObserver({
'options': extend({}, this.defaults)
options: extend({}, this.defaults),
});
this[internalDataSymbol] = new ProxyObserver({
});
this[internalDataSymbol] = new ProxyObserver({});
}
/**
......@@ -74,7 +67,7 @@ class Datasource extends Base {
* @returns {Datasource}
*/
attachObserver(observer) {
this[internalDataSymbol].attachObserver(observer)
this[internalDataSymbol].attachObserver(observer);
return this;
}
......@@ -85,7 +78,7 @@ class Datasource extends Base {
* @returns {Datasource}
*/
detachObserver(observer) {
this[internalDataSymbol].detachObserver(observer)
this[internalDataSymbol].detachObserver(observer);
return this;
}
......@@ -120,7 +113,7 @@ class Datasource extends Base {
* @return {Datasource}
*/
setOption(path, value) {
new Pathfinder(this[internalSymbol].getSubject()['options']).setVia(path, value);
new Pathfinder(this[internalSymbol].getSubject()["options"]).setVia(path, value);
return this;
}
......@@ -130,13 +123,12 @@ class Datasource extends Base {
* @throws {Error} the options does not contain a valid json definition
*/
setOptions(options) {
if (isString(options)) {
options = parseOptionsJSON(options)
options = parseOptionsJSON(options);
}
const self = this;
extend(self[internalSymbol].getSubject()['options'], self.defaults, options);
extend(self[internalSymbol].getSubject()["options"], self.defaults, options);
return self;
}
......@@ -152,10 +144,8 @@ class Datasource extends Base {
let value;
try {
value = new Pathfinder(this[internalSymbol].getRealSubject()['options']).getVia(path);
} catch (e) {
}
value = new Pathfinder(this[internalSymbol].getRealSubject()["options"]).getVia(path);
} catch (e) {}
if (value === undefined) return defaultValue;
return value;
......@@ -166,7 +156,7 @@ class Datasource extends Base {
* @return {Promise}
*/
read() {
throw new Error("this method must be implemented by derived classes")
throw new Error("this method must be implemented by derived classes");
}
/**
......@@ -174,10 +164,9 @@ class Datasource extends Base {
* @return {Promise}
*/
write() {
throw new Error("this method must be implemented by derived classes")
throw new Error("this method must be implemented by derived classes");
}
/**
* Returns real object
*
......@@ -206,7 +195,6 @@ class Datasource extends Base {
static get [instanceSymbol]() {
return Symbol.for("@schukai/monster/data/datasource");
}
}
/**
......@@ -217,15 +205,11 @@ class Datasource extends Base {
*/
function parseOptionsJSON(data) {
if (isString(data)) {
// the configuration can be specified as a data url.
try {
let dataUrl = parseDataURL(data);
data = dataUrl.content;
} catch (e) {
}
} catch (e) {}
try {
let obj = JSON.parse(data);
......
......@@ -11,7 +11,7 @@ import {Datasource} from "../datasource.mjs";
import { Pathfinder } from "../pathfinder.mjs";
import { Pipe } from "../pipe.mjs";
export {Server}
export { Server };
/**
* Base class for all server datasources
......@@ -23,7 +23,6 @@ export {Server}
* @summary The Server class encapsulates the access to a server datasource
*/
class Server extends Datasource {
/**
* This method is called by the `instanceof` operator.
* @returns {symbol}
......@@ -32,7 +31,6 @@ class Server extends Datasource {
return Symbol.for("@schukai/monster/data/datasource/server");
}
/**
* This prepares the data that comes from the server.
* Should not be called directly.
......@@ -43,11 +41,11 @@ class Server extends Datasource {
*/
transformServerPayload(payload) {
const self = this;
payload = doTransform.call(self, 'read', payload);
payload = doTransform.call(self, "read", payload);
const dataPath = self.getOption('read.path');
const dataPath = self.getOption("read.path");
if (dataPath) {
payload = (new Pathfinder(payload)).getVia(dataPath);
payload = new Pathfinder(payload).getVia(dataPath);
}
return payload;
......@@ -63,23 +61,21 @@ class Server extends Datasource {
prepareServerPayload(payload) {
const self = this;
payload = doTransform.call(self, 'write', payload);
payload = doTransform.call(self, "write", payload);
let sheathingObject = self.getOption('write.sheathing.object');
let sheathingPath = self.getOption('write.sheathing.path');
let sheathingObject = self.getOption("write.sheathing.object");
let sheathingPath = self.getOption("write.sheathing.path");
if (sheathingObject && sheathingPath) {
const sub = payload;
payload = sheathingObject;
(new Pathfinder(payload)).setVia(sheathingPath, sub);
new Pathfinder(payload).setVia(sheathingPath, sub);
}
return payload;
}
}
/**
* @private
* @param self
......@@ -91,11 +87,11 @@ function doTransform(type, obj) {
let transformation = self.getOption(`${type}.mapping.transformer`);
if (transformation !== undefined) {
const pipe = new Pipe(transformation);
const callbacks = self.getOption(`${type}.mapping.callbacks`)
const callbacks = self.getOption(`${type}.mapping.callbacks`);
if (isObject(callbacks)) {
for (const key in callbacks) {
if (callbacks.hasOwnProperty(key) && typeof callbacks[key] === 'function') {
if (callbacks.hasOwnProperty(key) && typeof callbacks[key] === "function") {
pipe.setCallback(key, callbacks[key]);
}
}
......
......@@ -12,7 +12,7 @@ import {Pathfinder} from "../../pathfinder.mjs";
import { Pipe } from "../../pipe.mjs";
import { WriteError } from "./restapi/writeerror.mjs";
export {RestAPI}
export { RestAPI };
/**
* The RestAPI is a class that enables a REST API server.
......@@ -25,7 +25,6 @@ export {RestAPI}
* @summary The RestAPI is a class that binds a REST API server.
*/
class RestAPI extends Server {
/**
*
* @param {Object} [options] options contains definitions for the datasource.
......@@ -36,7 +35,6 @@ class RestAPI extends Server {
if (isObject(options)) {
this.setOptions(options);
}
}
/**
......@@ -76,34 +74,33 @@ class RestAPI extends Server {
return Object.assign({}, super.defaults, {
write: {
init: {
method: 'POST',
method: "POST",
},
acceptedStatus: [200, 201],
url: undefined,
mapping: {
transformer: undefined,
callbacks: []
callbacks: [],
},
sheathing: {
object: undefined,
path: undefined,
},
report: {
path: undefined
}
path: undefined,
},
},
read: {
init: {
method: 'GET'
method: "GET",
},
acceptedStatus: [200],
url: undefined,
mapping: {
transformer: undefined,
callbacks: []
callbacks: [],
},
},
});
}
......@@ -114,17 +111,15 @@ class RestAPI extends Server {
* @throws {Error} the data cannot be read
*/
read() {
const self = this;
let init = self.getOption('read.init');
let init = self.getOption("read.init");
if (!isObject(init)) init = {};
if (!init['method']) init['method'] = 'GET';
if (!init["method"]) init["method"] = "GET";
return fetchData.call(this, 'read', (obj) => {
return fetchData.call(this, "read", (obj) => {
self.set(self.transformServerPayload.call(self, obj));
});
}
/**
......@@ -132,61 +127,57 @@ class RestAPI extends Server {
* @throws {WriteError} the data cannot be written
*/
write() {
const self = this;
let init = self.getOption('write.init');
let init = self.getOption("write.init");
if (!isObject(init)) init = {};
if (typeof init['headers'] !== 'object') {
init['headers'] = {
'Content-Type': 'application/json'
if (typeof init["headers"] !== "object") {
init["headers"] = {
"Content-Type": "application/json",
};
}
}
if (!init['method']) init['method'] = 'POST';
if (!init["method"]) init["method"] = "POST";
let obj = self.prepareServerPayload(self.get());
init['body'] = JSON.stringify(obj);
init["body"] = JSON.stringify(obj);
return fetchData.call(this, init, 'write');
return fetchData.call(this, init, "write");
}
/**
* @return {RestAPI}
*/
getClone() {
const self = this;
return new RestAPI(self[internalSymbol].getRealSubject()['options'].read, self[internalSymbol].getRealSubject()['options'].write);
return new RestAPI(
self[internalSymbol].getRealSubject()["options"].read,
self[internalSymbol].getRealSubject()["options"].write,
);
}
}
function fetchData(init, key, callback) {
const self = this;
let response;
return fetch(self.getOption(`${key}.url`), init).then(resp => {
return fetch(self.getOption(`${key}.url`), init)
.then((resp) => {
response = resp;
const acceptedStatus = self.getOption(`${key}.acceptedStatus`, [200]);
if (acceptedStatus.indexOf(resp.status) === -1) {
throw Error(`the data cannot be ${key} (response ${resp.status})`)
throw Error(`the data cannot be ${key} (response ${resp.status})`);
}
return resp.text()
}).then(body => {
return resp.text();
})
.then((body) => {
let obj;
try {
obj = JSON.parse(body);
} catch (e) {
if (body.length > 100) {
body = `${body.substring(0, 97)}...`;
}
......@@ -198,10 +189,5 @@ function fetchData(init, key, callback) {
callback(obj);
}
return response;
});
}
......@@ -7,7 +7,7 @@
import { internalSymbol, instanceSymbol } from "../../../../constants.mjs";
export {WriteError}
export { WriteError };
/**
* Error message for API requests with extension of request and validation.
......@@ -28,7 +28,7 @@ class WriteError extends Error {
super(message);
this[internalSymbol] = {
response: response,
validation: validation
validation: validation,
};
}
......@@ -45,13 +45,13 @@ class WriteError extends Error {
* @return {Response}
*/
getResponse() {
return this[internalSymbol]['response']
return this[internalSymbol]["response"];
}
/**
* @return {Object}
*/
getValidation() {
return this[internalSymbol]['validation']
return this[internalSymbol]["validation"];
}
}
......@@ -11,7 +11,7 @@ import {WebConnect as NetWebConnect} from "../../../net/webconnect.mjs";
import { Message } from "../../../net/webconnect/message.mjs";
import { Server } from "../server.mjs";
export {WebConnect}
export { WebConnect };
/**
* @private
......@@ -21,8 +21,6 @@ export {WebConnect}
*/
const webConnectSymbol = Symbol("connection");
/**
* The RestAPI is a class that enables a REST API server.
*
......@@ -34,7 +32,6 @@ const webConnectSymbol = Symbol("connection");
* @summary The LocalStorage class encapsulates the access to data objects.
*/
class WebConnect extends Server {
/**
*
* @param {Object} [options] options contains definitions for the datasource.
......@@ -51,15 +48,15 @@ class WebConnect extends Server {
if (!isObject(options)) options = {};
this.setOptions(options);
this[webConnectSymbol] = new NetWebConnect({
url: self.getOption('url'),
url: self.getOption("url"),
connection: {
timeout: self.getOption('connection.timeout'),
timeout: self.getOption("connection.timeout"),
reconnect: {
timeout: self.getOption('connection.reconnect.timeout'),
attempts: self.getOption('connection.reconnect.attempts'),
enabled: self.getOption('connection.reconnect.enabled')
}
}
timeout: self.getOption("connection.reconnect.timeout"),
attempts: self.getOption("connection.reconnect.attempts"),
enabled: self.getOption("connection.reconnect.enabled"),
},
},
});
}
......@@ -112,7 +109,7 @@ class WebConnect extends Server {
write: {
mapping: {
transformer: undefined,
callbacks: {}
callbacks: {},
},
sheathing: {
object: undefined,
......@@ -122,7 +119,7 @@ class WebConnect extends Server {
read: {
mapping: {
transformer: undefined,
callbacks: {}
callbacks: {},
},
path: undefined,
},
......@@ -132,8 +129,8 @@ class WebConnect extends Server {
timeout: 1000,
attempts: 1,
enabled: false,
}
}
},
},
});
}
......@@ -153,16 +150,15 @@ class WebConnect extends Server {
const self = this;
return new Promise((resolve, reject) => {
while (this[webConnectSymbol].dataReceived() === true) {
let obj = this[webConnectSymbol].poll();
if (!isObject(obj)) {
reject(new Error('The received data is not an object.'));
reject(new Error("The received data is not an object."));
return;
}
if (!(obj instanceof Message)) {
reject(new Error('The received data is not a Message.'));
reject(new Error("The received data is not a Message."));
return;
}
......@@ -172,11 +168,8 @@ class WebConnect extends Server {
}
resolve(self.get());
})
};
});
}
/**
* @return {Promise}
......@@ -184,7 +177,7 @@ class WebConnect extends Server {
write() {
const self = this;
let obj = self.prepareServerPayload(self.get());
return self[webConnectSymbol].send(obj)
return self[webConnectSymbol].send(obj);
}
/**
......@@ -192,8 +185,6 @@ class WebConnect extends Server {
*/
getClone() {
const self = this;
return new WebConnect(self[internalSymbol].getRealSubject()['options']);
return new WebConnect(self[internalSymbol].getRealSubject()["options"]);
}
}
......@@ -9,13 +9,13 @@ import {internalSymbol,instanceSymbol} from "../../constants.mjs";
import { validateString } from "../../types/validate.mjs";
import { Datasource } from "../datasource.mjs";
export {Storage, storageObjectSymbol}
export { Storage, storageObjectSymbol };
/**
* @private
* @type {symbol}
*/
const storageObjectSymbol = Symbol.for ('@schukai/monster/data/datasource/storage/@@storageObject')
const storageObjectSymbol = Symbol.for("@schukai/monster/data/datasource/storage/@@storageObject");
/**
* The class represents a record.
......@@ -27,7 +27,6 @@ const storageObjectSymbol = Symbol.for ('@schukai/monster/data/datasource/stora
* @summary The Storage class encapsulates the access to data objects over WebStorageAPI.
*/
class Storage extends Datasource {
/**
*
* @param {string} key LocalStorage Key
......@@ -35,7 +34,7 @@ class Storage extends Datasource {
*/
constructor(key) {
super();
this.setOption('key', validateString(key));
this.setOption("key", validateString(key));
}
/**
......@@ -62,7 +61,7 @@ class Storage extends Datasource {
* @private
*/
[storageObjectSymbol]() {
throw new Error("this method must be implemented by derived classes")
throw new Error("this method must be implemented by derived classes");
}
/**
......@@ -77,11 +76,10 @@ class Storage extends Datasource {
const storage = self[storageObjectSymbol]();
return new Promise(function (resolve) {
const data = JSON.parse(storage.getItem(self.getOption('key')));
const data = JSON.parse(storage.getItem(self.getOption("key")));
self.set(data ?? {});
resolve();
})
});
}
/**
......@@ -94,16 +92,15 @@ class Storage extends Datasource {
const storage = self[storageObjectSymbol]();
return new Promise(function (resolve) {
const data = self.get();
if (data === undefined) {
storage.removeItem(self.getOption('key'));
storage.removeItem(self.getOption("key"));
} else {
storage.setItem(self.getOption('key'), JSON.stringify(data));
storage.setItem(self.getOption("key"), JSON.stringify(data));
}
resolve();
})
});
}
/**
......@@ -111,7 +108,6 @@ class Storage extends Datasource {
*/
getClone() {
const self = this;
return new Storage(self[internalSymbol].getRealSubject()['options'].key);
return new Storage(self[internalSymbol].getRealSubject()["options"].key);
}
}
......@@ -10,7 +10,7 @@ import {getGlobalObject} from "../../../types/global.mjs";
import { Datasource } from "../../datasource.mjs";
import { Storage, storageObjectSymbol } from "../storage.mjs";
export {LocalStorage}
export { LocalStorage };
/**
* The LocalStorage Datasource provides a data store in the browser localStorage.
......@@ -22,14 +22,13 @@ export {LocalStorage}
* @summary The LocalStorage class encapsulates the access to data objects.
*/
class LocalStorage extends Storage {
/**
* @throws {Error} this method must be implemented by derived classes.
* @return {external:localStorage}
* @private
*/
[storageObjectSymbol]() {
return getGlobalObject('localStorage');
return getGlobalObject("localStorage");
}
/**
......@@ -47,8 +46,6 @@ class LocalStorage extends Storage {
*/
getClone() {
const self = this;
return new LocalStorage(self[internalSymbol].getRealSubject()['options'].key);
return new LocalStorage(self[internalSymbol].getRealSubject()["options"].key);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment