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

#116

parent c3049ee8
No related branches found
No related tags found
No related merge requests found
Showing
with 25 additions and 20 deletions
...@@ -2,13 +2,18 @@ ...@@ -2,13 +2,18 @@
All notable changes to this project will be documented in this file. All notable changes to this project will be documented in this file.
## [1.31.0] - 2022-02-07
## Added
- [new promise domReady and windowReady](https://gitlab.schukai.com/oss/libraries/javascript/monster/-/issues/116)
## [1.30.0] - 2022-02-05 ## [1.30.0] - 2022-02-05
## Added ## Added
- [new class DeadMansSwitch](https://gitlab.schukai.com/oss/libraries/javascript/monster/-/issues/115) - [new class DeadMansSwitch](https://gitlab.schukai.com/oss/libraries/javascript/monster/-/issues/115)
## [1.29.3] - 2022-01-23 ## [1.29.3] - 2022-01-23
## Fixed ## Fixed
......
/** Monster 1.30.1, © 2021 schukai GmbH, Released under the AGPL 3.0 License. */ /** Monster 1.30.1, © 2021 schukai GmbH, Released under the AGPL 3.0 License. */
"use strict";import{Monster}from"./namespace.js";const internalSymbol=Symbol("internalData"),internalStateSymbol=Symbol("state");export{Monster,internalSymbol,internalStateSymbol}; 'use strict';import{Monster}from"./namespace.js";const internalSymbol=Symbol("internalData");const internalStateSymbol=Symbol("state");export{Monster,internalSymbol,internalStateSymbol};
\ No newline at end of file
/** Monster 1.30.1, © 2021 schukai GmbH, Released under the AGPL 3.0 License. */ /** Monster 1.30.1, © 2021 schukai GmbH, Released under the AGPL 3.0 License. */
"use strict";import{assignToNamespace,Monster}from"../namespace.js";import{Base}from"../types/base.js";class AbstractConstraint extends Base{constructor(){super()}isValid(s){return Promise.reject(s)}}assignToNamespace("Monster.Constraints",AbstractConstraint);export{Monster,AbstractConstraint}; 'use strict';import{assignToNamespace,Monster}from"../namespace.js";import{Base}from"../types/base.js";class AbstractConstraint extends Base{constructor(){super()}isValid(value){return Promise.reject(value)}}assignToNamespace("Monster.Constraints",AbstractConstraint);export{Monster,AbstractConstraint};
\ No newline at end of file
/** Monster 1.30.1, © 2021 schukai GmbH, Released under the AGPL 3.0 License. */ /** Monster 1.30.1, © 2021 schukai GmbH, Released under the AGPL 3.0 License. */
"use strict";import{assignToNamespace,Monster}from"../namespace.js";import{AbstractConstraint}from"./abstract.js";class AbstractOperator extends AbstractConstraint{constructor(t,r){if(super(),!(t instanceof AbstractConstraint&&r instanceof AbstractConstraint))throw new TypeError("parameters must be from type AbstractConstraint");this.operantA=t,this.operantB=r}}assignToNamespace("Monster.Constraints",AbstractOperator);export{Monster,AbstractOperator}; 'use strict';import{assignToNamespace,Monster}from"../namespace.js";import{AbstractConstraint}from"./abstract.js";class AbstractOperator extends AbstractConstraint{constructor(operantA,operantB){super();if(!(operantA instanceof AbstractConstraint)||!(operantB instanceof AbstractConstraint)){throw new TypeError("parameters must be from type AbstractConstraint")}this.operantA=operantA;this.operantB=operantB}}assignToNamespace("Monster.Constraints",AbstractOperator);export{Monster,AbstractOperator};
\ No newline at end of file
/** Monster 1.30.1, © 2021 schukai GmbH, Released under the AGPL 3.0 License. */ /** Monster 1.30.1, © 2021 schukai GmbH, Released under the AGPL 3.0 License. */
"use strict";import{assignToNamespace,Monster}from"../namespace.js";import{AbstractOperator}from"./abstractoperator.js";class AndOperator extends AbstractOperator{isValid(r){return Promise.all([this.operantA.isValid(r),this.operantB.isValid(r)])}}assignToNamespace("Monster.Constraints",AndOperator);export{Monster,AndOperator}; 'use strict';import{assignToNamespace,Monster}from"../namespace.js";import{AbstractOperator}from"./abstractoperator.js";class AndOperator extends AbstractOperator{isValid(value){return Promise.all([this.operantA.isValid(value),this.operantB.isValid(value)])}}assignToNamespace("Monster.Constraints",AndOperator);export{Monster,AndOperator};
\ No newline at end of file
/** Monster 1.30.1, © 2021 schukai GmbH, Released under the AGPL 3.0 License. */ /** Monster 1.30.1, © 2021 schukai GmbH, Released under the AGPL 3.0 License. */
"use strict";import{assignToNamespace,Monster}from"../namespace.js";import{AbstractConstraint}from"./abstract.js";class Invalid extends AbstractConstraint{isValid(s){return Promise.reject(s)}}assignToNamespace("Monster.Constraints",Invalid);export{Monster,Invalid}; 'use strict';import{assignToNamespace,Monster}from"../namespace.js";import{AbstractConstraint}from"./abstract.js";class Invalid extends AbstractConstraint{isValid(value){return Promise.reject(value)}}assignToNamespace("Monster.Constraints",Invalid);export{Monster,Invalid};
\ No newline at end of file
/** Monster 1.30.1, © 2021 schukai GmbH, Released under the AGPL 3.0 License. */ /** Monster 1.30.1, © 2021 schukai GmbH, Released under the AGPL 3.0 License. */
"use strict";import{assignToNamespace,Monster}from"../namespace.js";import{isArray}from"../types/is.js";import{AbstractConstraint}from"./abstract.js";class IsArray extends AbstractConstraint{isValid(s){return isArray(s)?Promise.resolve(s):Promise.reject(s)}}assignToNamespace("Monster.Constraints",IsArray);export{Monster,IsArray}; 'use strict';import{assignToNamespace,Monster}from"../namespace.js";import{isArray}from"../types/is.js";import{AbstractConstraint}from"./abstract.js";class IsArray extends AbstractConstraint{isValid(value){if(isArray(value)){return Promise.resolve(value)}return Promise.reject(value)}}assignToNamespace("Monster.Constraints",IsArray);export{Monster,IsArray};
\ No newline at end of file
/** Monster 1.30.1, © 2021 schukai GmbH, Released under the AGPL 3.0 License. */ /** Monster 1.30.1, © 2021 schukai GmbH, Released under the AGPL 3.0 License. */
"use strict";import{assignToNamespace,Monster}from"../namespace.js";import{isObject}from"../types/is.js";import{AbstractConstraint}from"./abstract.js";class IsObject extends AbstractConstraint{isValid(s){return isObject(s)?Promise.resolve(s):Promise.reject(s)}}assignToNamespace("Monster.Constraints",IsObject);export{Monster,IsObject}; 'use strict';import{assignToNamespace,Monster}from"../namespace.js";import{isObject}from"../types/is.js";import{AbstractConstraint}from"./abstract.js";class IsObject extends AbstractConstraint{isValid(value){if(isObject(value)){return Promise.resolve(value)}return Promise.reject(value)}}assignToNamespace("Monster.Constraints",IsObject);export{Monster,IsObject};
\ No newline at end of file
/** Monster 1.30.1, © 2021 schukai GmbH, Released under the AGPL 3.0 License. */ /** Monster 1.30.1, © 2021 schukai GmbH, Released under the AGPL 3.0 License. */
"use strict";const namespace="Monster.Constraints";export{namespace}; 'use strict';export const namespace="Monster.Constraints";
\ No newline at end of file
/** Monster 1.30.1, © 2021 schukai GmbH, Released under the AGPL 3.0 License. */ /** Monster 1.30.1, © 2021 schukai GmbH, Released under the AGPL 3.0 License. */
"use strict";import{assignToNamespace,Monster}from"../namespace.js";import{AbstractOperator}from"./abstractoperator.js";class OrOperator extends AbstractOperator{isValid(s){var o=this;return new Promise(function(t,r){let a,e;o.operantA.isValid(s).then(function(){t()}).catch(function(){(a=!1)===e&&r()}),o.operantB.isValid(s).then(function(){t()}).catch(function(){(e=!1)===a&&r()})})}}assignToNamespace("Monster.Constraints",OrOperator);export{Monster,OrOperator}; 'use strict';import{assignToNamespace,Monster}from"../namespace.js";import{AbstractOperator}from"./abstractoperator.js";class OrOperator extends AbstractOperator{isValid(value){var self=this;return new Promise(function(resolve,reject){let a,b;self.operantA.isValid(value).then(function(){resolve()}).catch(function(){a=false;if(b===false){reject()}});self.operantB.isValid(value).then(function(){resolve()}).catch(function(){b=false;if(a===false){reject()}})})}}assignToNamespace("Monster.Constraints",OrOperator);export{Monster,OrOperator};
\ No newline at end of file
/** Monster 1.30.1, © 2021 schukai GmbH, Released under the AGPL 3.0 License. */ /** Monster 1.30.1, © 2021 schukai GmbH, Released under the AGPL 3.0 License. */
"use strict";import{assignToNamespace,Monster}from"../namespace.js";import{AbstractConstraint}from"./abstract.js";class Valid extends AbstractConstraint{isValid(s){return Promise.resolve(s)}}assignToNamespace("Monster.Constraints",Valid);export{Monster,Valid}; 'use strict';import{assignToNamespace,Monster}from"../namespace.js";import{AbstractConstraint}from"./abstract.js";class Valid extends AbstractConstraint{isValid(value){return Promise.resolve(value)}}assignToNamespace("Monster.Constraints",Valid);export{Monster,Valid};
\ No newline at end of file
/** Monster 1.30.1, © 2021 schukai GmbH, Released under the AGPL 3.0 License. */ /** Monster 1.30.1, © 2021 schukai GmbH, Released under the AGPL 3.0 License. */
"use strict";import{assignToNamespace,Monster}from"../namespace.js";import{isFunction,isObject,isString}from"../types/is.js";import{validateString}from"../types/validate.js";import{clone}from"../util/clone.js";import{DELIMITER,Pathfinder,WILDCARD}from"./pathfinder.js";const PARENT="^";function buildMap(t,e,a,n,i){return assembleParts(t,e,i,function(t,e,i){e=build(t,n,e),t=build(t,a),this.set(e,t)})}function assembleParts(t,e,a,n){const r=new Map;let i;if(isFunction(e)){if(i=e(t),!(i instanceof Map))throw new TypeError("the selector callback must return a map")}else{if(!isString(e))throw new TypeError("selector is neither a string nor a function");i=new Map,buildFlatMap.call(i,t,e)}return i instanceof Map&&i.forEach((t,e,i)=>{isFunction(a)&&!0!==a.call(i,t,e)||n.call(r,t,e,i)}),r}function buildFlatMap(i,e,a,n){var r=this;const s=new Map;var f=r.size;void 0===a&&(a=[]);let o=e.split(DELIMITER),t,l=[];do{if(t=o.shift(),l.push(t),t===WILDCARD){let t=new Pathfinder(i),e;try{e=t.getVia(l.join(DELIMITER))}catch(t){e=new Map}for(const[c,p]of e){let e=clone(a);l.map(t=>{e.push(t===WILDCARD?c:t)});var u=e.join(DELIMITER);let t=buildFlatMap.call(r,p,o.join(DELIMITER),e,p);isObject(t)&&void 0!==n&&(t[PARENT]=n),s.set(u,t)}}}while(0<o.length);if(f===r.size)for(var[d,h]of s)r.set(d,h);return i}function build(t,a,n){if(void 0===a)return n||t;validateString(a);const e=[...a.matchAll(/(?<placeholder>\${(?<path>[a-z\^A-Z.\-_0-9]*)})/gm)];let r=new Pathfinder(t);return 0===e.length?r.getVia(a):(e.forEach(e=>{var e=e?.groups,i=e?.placeholder;if(void 0!==i){e=e?.path;let t=r.getVia(e);void 0===t&&(t=n),a=a.replaceAll(i,t)}}),a)}assignToNamespace("Monster.Data",buildMap);export{PARENT,Monster,buildMap,assembleParts}; 'use strict';import{assignToNamespace,Monster}from"../namespace.js";import{isFunction,isObject,isString}from"../types/is.js";import{validateString}from"../types/validate.js";import{clone}from"../util/clone.js";import{DELIMITER,Pathfinder,WILDCARD}from"./pathfinder.js";export const PARENT="^";function buildMap(subject,selector,valueTemplate,keyTemplate,filter){return assembleParts(subject,selector,filter,function(v,k,m){k=build(v,keyTemplate,k);v=build(v,valueTemplate);this.set(k,v)})}function assembleParts(subject,selector,filter,callback){const result=new Map;let map;if(isFunction(selector)){map=selector(subject);if(!(map instanceof Map)){throw new TypeError("the selector callback must return a map")}}else if(isString(selector)){map=new Map;buildFlatMap.call(map,subject,selector)}else{throw new TypeError("selector is neither a string nor a function")}if(!(map instanceof Map)){return result}map.forEach((v,k,m)=>{if(isFunction(filter)){if(filter.call(m,v,k)!==true)return}callback.call(result,v,k,m)});return result}function buildFlatMap(subject,selector,key,parentMap){const result=this;const currentMap=new Map;const resultLength=result.size;if(key===undefined)key=[];let parts=selector.split(DELIMITER);let current="",currentPath=[];do{current=parts.shift();currentPath.push(current);if(current===WILDCARD){let finder=new Pathfinder(subject);let map;try{map=finder.getVia(currentPath.join(DELIMITER))}catch(e){let a=e;map=new Map}for(const[k,o]of map){let copyKey=clone(key);currentPath.map(a=>{copyKey.push(a===WILDCARD?k:a)});let kk=copyKey.join(DELIMITER);let sub=buildFlatMap.call(result,o,parts.join(DELIMITER),copyKey,o);if(isObject(sub)&&parentMap!==undefined){sub[PARENT]=parentMap}currentMap.set(kk,sub)}}}while(parts.length>0);if(resultLength===result.size){for(const[k,o]of currentMap){result.set(k,o)}}return subject}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 array=[...definition.matchAll(regexp)];let finder=new Pathfinder(subject);if(array.length===0){return finder.getVia(definition)}array.forEach(a=>{let groups=a?.["groups"];let placeholder=groups?.["placeholder"];if(placeholder===undefined)return;let path=groups?.["path"];let v=finder.getVia(path);if(v===undefined)v=defaultValue;definition=definition.replaceAll(placeholder,v)});return definition}assignToNamespace("Monster.Data",buildMap);export{Monster,buildMap,assembleParts};
\ No newline at end of file
/** Monster 1.30.1, © 2021 schukai GmbH, Released under the AGPL 3.0 License. */ /** Monster 1.30.1, © 2021 schukai GmbH, Released under the AGPL 3.0 License. */
"use strict";import{assignToNamespace,Monster}from"../namespace.js";import{isArray,isObject}from"../types/is.js";import{Node}from"../types/node.js";import{NodeList}from"../types/nodelist.js";import{assembleParts}from"./buildmap.js";import{extend}from"./extend.js";const parentSymbol=Symbol("parent"),rootSymbol=Symbol("root");function buildTree(e,o,i,a,t){const n=new Map;isObject(t)||(t={});var s=(t=extend({},{rootReferences:[null,void 0],filter:void 0},t))?.filter;let d=t.rootReferences;isArray(d)||(d=[d]);const r=assembleParts(e,o,s,function(e,o,t){var s=e?.[i];let r=e?.[a];if(-1!==d.indexOf(r)&&(r=rootSymbol),void 0===s)throw new Error("the object has no value for the specified id");e[parentSymbol]=r;e=new Node(e);this.has(r)?this.get(r).add(e):this.set(r,(new NodeList).add(e)),n.set(s,e)}),m=(n.forEach(e=>{var o=e?.value?.[i];r.has(o)&&(e.childNodes=r.get(o),r.delete(o))}),new NodeList);return r.forEach(e=>{e instanceof Set&&e.forEach(e=>{m.add(e)})}),m}assignToNamespace("Monster.Data",buildTree);export{Monster,buildTree}; 'use strict';import{assignToNamespace,Monster}from"../namespace.js";import{isArray,isObject}from"../types/is.js";import{Node}from"../types/node.js";import{NodeList}from"../types/nodelist.js";import{assembleParts}from"./buildmap.js";import{extend}from"./extend.js";const parentSymbol=Symbol("parent");const rootSymbol=Symbol("root");function buildTree(subject,selector,idKey,parentIDKey,options){const nodes=new Map;if(!isObject(options)){options={}}options=extend({},{rootReferences:[null,undefined],filter:undefined},options);const filter=options?.filter;let rootReferences=options.rootReferences;if(!isArray(rootReferences)){rootReferences=[rootReferences]}const childMap=assembleParts(subject,selector,filter,function(o,k,m){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")}o[parentSymbol]=ref;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];if(childMap.has(id)){node.childNodes=childMap.get(id);childMap.delete(id)}});const list=new NodeList;childMap.forEach(s=>{if(s instanceof Set){s.forEach(n=>{list.add(n)})}});return list}assignToNamespace("Monster.Data",buildTree);export{Monster,buildTree};
\ No newline at end of file
/** Monster 1.30.1, © 2021 schukai GmbH, Released under the AGPL 3.0 License. */ /** Monster 1.30.1, © 2021 schukai GmbH, Released under the AGPL 3.0 License. */
"use strict";import{internalSymbol}from"../constants.js";import{assignToNamespace,Monster}from"../namespace.js";import{Base}from"../types/base.js";import{parseDataURL}from"../types/dataurl.js";import{isString}from"../types/is.js";import{ProxyObserver}from"../types/proxyobserver.js";import{validateObject}from"../types/validate.js";import{extend}from"./extend.js";import{Pathfinder}from"./pathfinder.js";const internalDataSymbol=Symbol("internalData");class Datasource extends Base{constructor(){super(),this[internalSymbol]=new ProxyObserver({options:extend({},this.defaults)}),this[internalDataSymbol]=new ProxyObserver({})}attachObserver(t){return this[internalDataSymbol].attachObserver(t),this}detachObserver(t){return this[internalDataSymbol].detachObserver(t),this}containsObserver(t){return this[internalDataSymbol].containsObserver(t)}get defaults(){return{}}setOption(t,e){return new Pathfinder(this[internalSymbol].getSubject().options).setVia(t,e),this}setOptions(t){isString(t)&&(t=parseOptionsJSON(t));var e=this;return extend(e[internalSymbol].getSubject().options,e.defaults,t),e}getOption(t,e){let r;try{r=new Pathfinder(this[internalSymbol].getRealSubject().options).getVia(t)}catch(t){}return void 0===r?e:r}read(){throw new Error("this method must be implemented by derived classes")}write(){throw new Error("this method must be implemented by derived classes")}get(){return this[internalDataSymbol].getRealSubject()}set(t){return this[internalDataSymbol].setSubject(t),this}}function parseOptionsJSON(e){if(isString(e)){try{e=parseDataURL(e).content}catch(t){}try{var t=JSON.parse(e);return validateObject(t),t}catch(t){throw new Error("the options does not contain a valid json definition (actual: "+e+").")}}return{}}assignToNamespace("Monster.Data",Datasource);export{Monster,Datasource}; 'use strict';import{internalSymbol}from"../constants.js";import{assignToNamespace,Monster}from"../namespace.js";import{Base}from"../types/base.js";import{parseDataURL}from"../types/dataurl.js";import{isString}from"../types/is.js";import{ProxyObserver}from"../types/proxyobserver.js";import{validateObject}from"../types/validate.js";import{extend}from"./extend.js";import{Pathfinder}from"./pathfinder.js";const internalDataSymbol=Symbol("internalData");class Datasource extends Base{constructor(){super();this[internalSymbol]=new ProxyObserver({"options":extend({},this.defaults)});this[internalDataSymbol]=new ProxyObserver({})}attachObserver(observer){this[internalDataSymbol].attachObserver(observer);return this}detachObserver(observer){this[internalDataSymbol].detachObserver(observer);return this}containsObserver(observer){return this[internalDataSymbol].containsObserver(observer)}get defaults(){return{}}setOption(path,value){new Pathfinder(this[internalSymbol].getSubject()["options"]).setVia(path,value);return this}setOptions(options){if(isString(options)){options=parseOptionsJSON(options)}const self=this;extend(self[internalSymbol].getSubject()["options"],self.defaults,options);return self}getOption(path,defaultValue){let value;try{value=new Pathfinder(this[internalSymbol].getRealSubject()["options"]).getVia(path)}catch(e){}if(value===undefined)return defaultValue;return value}read(){throw new Error("this method must be implemented by derived classes")}write(){throw new Error("this method must be implemented by derived classes")}get(){const self=this;return self[internalDataSymbol].getRealSubject()}set(data){const self=this;self[internalDataSymbol].setSubject(data);return self}}function parseOptionsJSON(data){if(isString(data)){try{let dataUrl=parseDataURL(data);data=dataUrl.content}catch(e){}try{let obj=JSON.parse(data);validateObject(obj);return obj}catch(e){throw new Error("the options does not contain a valid json definition (actual: "+data+").")}}return{}}assignToNamespace("Monster.Data",Datasource);export{Monster,Datasource};
\ No newline at end of file
/** Monster 1.30.1, © 2021 schukai GmbH, Released under the AGPL 3.0 License. */ /** Monster 1.30.1, © 2021 schukai GmbH, Released under the AGPL 3.0 License. */
"use strict";const namespace="Monster.Data.Datasource";export{namespace}; 'use strict';export const namespace="Monster.Data.Datasource";
\ No newline at end of file
/** Monster 1.30.1, © 2021 schukai GmbH, Released under the AGPL 3.0 License. */ /** Monster 1.30.1, © 2021 schukai GmbH, Released under the AGPL 3.0 License. */
"use strict";import{internalSymbol}from"../../constants.js";import{assignToNamespace,Monster}from"../../namespace.js";import{isObject}from"../../types/is.js";import{Datasource}from"../datasource.js";import{Pathfinder}from"../pathfinder.js";import{Pipe}from"../pipe.js";import{WriteError}from"./restapi/writeerror.js";class RestAPI extends Datasource{constructor(t,e){super();const r={};isObject(t)&&(r.read=t),isObject(e)&&(r.write=e),this.setOptions(r)}get defaults(){return Object.assign({},super.defaults,{write:{init:{method:"POST"},acceptedStatus:[200,201],url:void 0,mapping:{transformer:void 0,callbacks:[]},report:{path:void 0}},read:{init:{method:"GET"},acceptedStatus:[200],url:void 0,mapping:{transformer:void 0,callbacks:[]}}})}read(){const s=this;let n,t=s.getOption("read.init");return isObject(t)||(t={}),fetch(s.getOption("read.url"),t).then(t=>{n=t;const e=s.getOption("read.acceptedStatus",[200]);if(-1===e.indexOf(t.status))throw Error("the data cannot be read (response "+t.status+")");return t.text()}).then(e=>{let t;try{t=JSON.parse(e)}catch(t){throw 100<e.length&&(e=e.substring(0,97)+"..."),new Error("the response does not contain a valid json (actual: "+e+").")}e=s.getOption("read.mapping.transformer");if(void 0!==e){const r=new Pipe(e);t=r.run(t)}return s.set(t),n})}write(){const e=this;let t=e.getOption("write.init"),r=(isObject(t)||(t={}),"object"!=typeof t.headers&&(t.headers={"Content-Type":"application/json"}),e.get());var s=e.getOption("write.mapping.transformer");if(void 0!==s){const a=new Pipe(s);r=a.run(r)}var n,s=e.getOption("write.sheathing.object"),i=e.getOption("write.sheathing.path");let o=e.getOption("write.report.path");return s&&i&&(n=r,r=s,new Pathfinder(r).setVia(i,n)),t.body=JSON.stringify(r),fetch(e.getOption("write.url"),t).then(s=>{const t=e.getOption("write.acceptedStatus",[200,2001]);return-1===t.indexOf(s.status)?s.text().then(e=>{let t,r;try{t=JSON.parse(e),r=new Pathfinder(t).getVia(o)}catch(t){throw 100<e.length&&(e=e.substring(0,97)+"..."),new Error("the response does not contain a valid json (actual: "+e+").")}throw new WriteError("the data cannot be written (response "+s.status+")",s,r)}):s})}getClone(){return new RestAPI(this[internalSymbol].getRealSubject().options.read,this[internalSymbol].getRealSubject().options.write)}}assignToNamespace("Monster.Data.Datasource",RestAPI);export{Monster,RestAPI}; 'use strict';import{internalSymbol}from"../../constants.js";import{assignToNamespace,Monster}from"../../namespace.js";import{isObject}from"../../types/is.js";import{Datasource}from"../datasource.js";import{Pathfinder}from"../pathfinder.js";import{Pipe}from"../pipe.js";import{WriteError}from"./restapi/writeerror.js";class RestAPI extends Datasource{constructor(readDefinition,writeDefinition){super();const options={};if(isObject(readDefinition))options.read=readDefinition;if(isObject(writeDefinition))options.write=writeDefinition;this.setOptions(options)}get defaults(){return Object.assign({},super.defaults,{write:{init:{method:"POST"},acceptedStatus:[200,201],url:undefined,mapping:{transformer:undefined,callbacks:[]},report:{path:undefined}},read:{init:{method:"GET"},acceptedStatus:[200],url:undefined,mapping:{transformer:undefined,callbacks:[]}}})}read(){const self=this;let response;let init=self.getOption("read.init");if(!isObject(init))init={};return fetch(self.getOption("read.url"),init).then(resp=>{response=resp;const acceptedStatus=self.getOption("read.acceptedStatus",[200]);if(acceptedStatus.indexOf(resp.status)===-1){throw Error("the data cannot be read (response "+resp.status+")")}return resp.text()}).then(body=>{let obj;try{obj=JSON.parse(body)}catch(e){if(body.length>100){body=body.substring(0,97)+"..."}throw new Error("the response does not contain a valid json (actual: "+body+").")}let transformation=self.getOption("read.mapping.transformer");if(transformation!==undefined){const pipe=new Pipe(transformation);obj=pipe.run(obj)}self.set(obj);return response})}write(){const self=this;let init=self.getOption("write.init");if(!isObject(init))init={};if(typeof init["headers"]!=="object"){init["headers"]={"Content-Type":"application/json"}}let obj=self.get();let transformation=self.getOption("write.mapping.transformer");if(transformation!==undefined){const pipe=new Pipe(transformation);obj=pipe.run(obj)}let sheathingObject=self.getOption("write.sheathing.object");let sheathingPath=self.getOption("write.sheathing.path");let reportPath=self.getOption("write.report.path");if(sheathingObject&&sheathingPath){const sub=obj;obj=sheathingObject;new Pathfinder(obj).setVia(sheathingPath,sub)}init["body"]=JSON.stringify(obj);return fetch(self.getOption("write.url"),init).then(response=>{const acceptedStatus=self.getOption("write.acceptedStatus",[200,2001]);if(acceptedStatus.indexOf(response.status)===-1){return response.text().then(body=>{let obj,validation;try{obj=JSON.parse(body);validation=new Pathfinder(obj).getVia(reportPath)}catch(e){if(body.length>100){body=body.substring(0,97)+"..."}throw new Error("the response does not contain a valid json (actual: "+body+").")}throw new WriteError("the data cannot be written (response "+response.status+")",response,validation)})}return response})}getClone(){const self=this;return new RestAPI(self[internalSymbol].getRealSubject()["options"].read,self[internalSymbol].getRealSubject()["options"].write)}}assignToNamespace("Monster.Data.Datasource",RestAPI);export{Monster,RestAPI};
\ No newline at end of file
/** Monster 1.30.1, © 2021 schukai GmbH, Released under the AGPL 3.0 License. */ /** Monster 1.30.1, © 2021 schukai GmbH, Released under the AGPL 3.0 License. */
"use strict";import{internalSymbol}from"../../../constants.js";import{assignToNamespace,Monster}from"../../../namespace.js";class WriteError extends Error{constructor(r,e,t){super(r),this[internalSymbol]={response:e,validation:t}}getResponse(){return this[internalSymbol].response}getValidation(){return this[internalSymbol].validation}}assignToNamespace("Monster.Data.Datasource.RestAPI",WriteError);export{Monster,WriteError}; 'use strict';import{internalSymbol}from"../../../constants.js";import{assignToNamespace,Monster}from"../../../namespace.js";class WriteError extends Error{constructor(message,response,validation){super(message);this[internalSymbol]={response:response,validation:validation}}getResponse(){return this[internalSymbol]["response"]}getValidation(){return this[internalSymbol]["validation"]}}assignToNamespace("Monster.Data.Datasource.RestAPI",WriteError);export{Monster,WriteError};
\ No newline at end of file
/** Monster 1.30.1, © 2021 schukai GmbH, Released under the AGPL 3.0 License. */ /** Monster 1.30.1, © 2021 schukai GmbH, Released under the AGPL 3.0 License. */
"use strict";import{internalSymbol}from"../../constants.js";import{assignToNamespace,Monster}from"../../namespace.js";import{validateString}from"../../types/validate.js";import{Datasource}from"../datasource.js";const storageObjectSymbol=Symbol("storageObject");class Storage extends Datasource{constructor(e){super(),this.setOption("key",validateString(e))}get defaults(){return Object.assign({},super.defaults,{key:void 0})}[storageObjectSymbol](){throw new Error("this method must be implemented by derived classes")}read(){const o=this,s=o[storageObjectSymbol]();return new Promise(function(e){var t=JSON.parse(s.getItem(o.getOption("key")));o.set(t??{}),e()})}write(){const o=this,s=o[storageObjectSymbol]();return new Promise(function(e){var t=o.get();void 0===t?s.removeItem(o.getOption("key")):s.setItem(o.getOption("key"),JSON.stringify(t)),e()})}getClone(){return new Storage(this[internalSymbol].getRealSubject().options.key)}}assignToNamespace("Monster.Data.Datasource",Storage);export{Monster,Storage,storageObjectSymbol}; 'use strict';import{internalSymbol}from"../../constants.js";import{assignToNamespace,Monster}from"../../namespace.js";import{validateString}from"../../types/validate.js";import{Datasource}from"../datasource.js";const storageObjectSymbol=Symbol("storageObject");class Storage extends Datasource{constructor(key){super();this.setOption("key",validateString(key))}get defaults(){return Object.assign({},super.defaults,{key:undefined})}[storageObjectSymbol](){throw new Error("this method must be implemented by derived classes")}read(){const self=this;const storage=self[storageObjectSymbol]();return new Promise(function(resolve){const data=JSON.parse(storage.getItem(self.getOption("key")));self.set(data??{});resolve()})}write(){const self=this;const storage=self[storageObjectSymbol]();return new Promise(function(resolve){const data=self.get();if(data===undefined){storage.removeItem(self.getOption("key"))}else{storage.setItem(self.getOption("key"),JSON.stringify(data))}resolve()})}getClone(){const self=this;return new Storage(self[internalSymbol].getRealSubject()["options"].key)}}assignToNamespace("Monster.Data.Datasource",Storage);export{Monster,Storage,storageObjectSymbol};
\ No newline at end of file
/** Monster 1.30.1, © 2021 schukai GmbH, Released under the AGPL 3.0 License. */ /** Monster 1.30.1, © 2021 schukai GmbH, Released under the AGPL 3.0 License. */
"use strict";import{internalSymbol}from"../../../constants.js";import{assignToNamespace,Monster}from"../../../namespace.js";import{getGlobalObject}from"../../../types/global.js";import{Datasource}from"../../datasource.js";import{Storage,storageObjectSymbol}from"../storage.js";class LocalStorage extends Storage{[storageObjectSymbol](){return getGlobalObject("localStorage")}getClone(){return new LocalStorage(this[internalSymbol].getRealSubject().options.key)}}assignToNamespace("Monster.Data.Datasource.Storage",LocalStorage);export{Monster,LocalStorage}; 'use strict';import{internalSymbol}from"../../../constants.js";import{assignToNamespace,Monster}from"../../../namespace.js";import{getGlobalObject}from"../../../types/global.js";import{Datasource}from"../../datasource.js";import{Storage,storageObjectSymbol}from"../storage.js";class LocalStorage extends Storage{[storageObjectSymbol](){return getGlobalObject("localStorage")}getClone(){const self=this;return new LocalStorage(self[internalSymbol].getRealSubject()["options"].key)}}assignToNamespace("Monster.Data.Datasource.Storage",LocalStorage);export{Monster,LocalStorage};
\ No newline at end of file
/** Monster 1.30.1, © 2021 schukai GmbH, Released under the AGPL 3.0 License. */ /** Monster 1.30.1, © 2021 schukai GmbH, Released under the AGPL 3.0 License. */
"use strict";const namespace="Monster.Data.Datasource.Storage";export{namespace}; 'use strict';export const namespace="Monster.Data.Datasource.Storage";
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment