@schukai/monster 3.100.17 → 3.100.18
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +11 -0
- package/package.json +1 -1
- package/source/data/datasource/server/restapi.mjs +207 -207
- package/source/types/version.mjs +1 -1
- package/test/cases/monster.mjs +1 -1
- package/test/web/test.html +2 -2
- package/test/web/tests.js +229 -155
package/CHANGELOG.md
CHANGED
package/package.json
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"author":"schukai GmbH","dependencies":{"@floating-ui/dom":"^1.6.13","@popperjs/core":"^2.11.8"},"description":"Monster is a simple library for creating fast, robust and lightweight websites.","homepage":"https://monsterjs.org/","keywords":["framework","web","dom","css","sass","mobile-first","app","front-end","templates","schukai","core","shopcloud","alvine","monster","buildmap","stack","observer","observable","uuid","node","nodelist","css-in-js","logger","log","theme"],"license":"AGPL 3.0","main":"source/monster.mjs","module":"source/monster.mjs","name":"@schukai/monster","repository":{"type":"git","url":"https://gitlab.schukai.com/oss/libraries/javascript/monster.git"},"type":"module","version":"3.100.
|
1
|
+
{"author":"schukai GmbH","dependencies":{"@floating-ui/dom":"^1.6.13","@popperjs/core":"^2.11.8"},"description":"Monster is a simple library for creating fast, robust and lightweight websites.","homepage":"https://monsterjs.org/","keywords":["framework","web","dom","css","sass","mobile-first","app","front-end","templates","schukai","core","shopcloud","alvine","monster","buildmap","stack","observer","observable","uuid","node","nodelist","css-in-js","logger","log","theme"],"license":"AGPL 3.0","main":"source/monster.mjs","module":"source/monster.mjs","name":"@schukai/monster","repository":{"type":"git","url":"https://gitlab.schukai.com/oss/libraries/javascript/monster.git"},"type":"module","version":"3.100.18"}
|
@@ -12,15 +12,15 @@
|
|
12
12
|
* SPDX-License-Identifier: AGPL-3.0
|
13
13
|
*/
|
14
14
|
|
15
|
-
import {
|
16
|
-
import {
|
17
|
-
import {
|
18
|
-
import {
|
19
|
-
import {
|
20
|
-
import {
|
21
|
-
import {
|
15
|
+
import {internalSymbol, instanceSymbol} from "../../../constants.mjs";
|
16
|
+
import {isObject, isFunction, isArray} from "../../../types/is.mjs";
|
17
|
+
import {diff} from "../../diff.mjs";
|
18
|
+
import {Server} from "../server.mjs";
|
19
|
+
import {WriteError} from "./restapi/writeerror.mjs";
|
20
|
+
import {DataFetchError} from "./restapi/data-fetch-error.mjs";
|
21
|
+
import {clone} from "../../../util/clone.mjs";
|
22
22
|
|
23
|
-
export {
|
23
|
+
export {RestAPI};
|
24
24
|
|
25
25
|
/**
|
26
26
|
* @type {symbol}
|
@@ -28,7 +28,7 @@ export { RestAPI };
|
|
28
28
|
* @since 3.12.0
|
29
29
|
*/
|
30
30
|
const rawDataSymbol = Symbol.for(
|
31
|
-
|
31
|
+
"@schukai/monster/data/datasource/server/restapi/rawdata",
|
32
32
|
);
|
33
33
|
|
34
34
|
/**
|
@@ -41,159 +41,159 @@ const rawDataSymbol = Symbol.for(
|
|
41
41
|
* @summary The RestAPI is a class that binds a REST API server.
|
42
42
|
*/
|
43
43
|
class RestAPI extends Server {
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
44
|
+
/**
|
45
|
+
*
|
46
|
+
* @param {Object} [options] options contains definitions for the datasource.
|
47
|
+
*/
|
48
|
+
constructor(options) {
|
49
|
+
super();
|
50
|
+
|
51
|
+
if (isObject(options)) {
|
52
|
+
this.setOptions(options);
|
53
|
+
}
|
54
|
+
}
|
55
|
+
|
56
|
+
/**
|
57
|
+
* This method is called by the `instanceof` operator.
|
58
|
+
* @return {symbol}
|
59
|
+
* @since 2.1.0
|
60
|
+
*/
|
61
|
+
static get [instanceSymbol]() {
|
62
|
+
return Symbol.for("@schukai/monster/data/datasource/server/restapi");
|
63
|
+
}
|
64
|
+
|
65
|
+
/**
|
66
|
+
* @property {Object} write={} Options
|
67
|
+
* @property {Object} write.init={} An option object, containing any custom settings that you want to apply to the request. The parameters are identical to those of the {@link https://developer.mozilla.org/en-US/docs/Web/API/Request/Request|Request constructor}
|
68
|
+
* @property {string} write.init.method=POST
|
69
|
+
* @property {Headers} write.init.headers Object containing any custom headers that you want to apply to the request.
|
70
|
+
* @property {string} write.responseCallback Callback function to be executed after the request has been completed.
|
71
|
+
* @property {string} write.acceptedStatus=[200,201]
|
72
|
+
* @property {string} write.url URL
|
73
|
+
* @property {Object} write.mapping the mapping is applied before writing.
|
74
|
+
* @property {String} write.mapping.transformer Transformer to select the appropriate entries
|
75
|
+
* @property {exampleCallback[]} write.mapping.callback with the help of the callback, the structures can be adjusted before writing.
|
76
|
+
* @property {Object} write.report
|
77
|
+
* @property {String} write.report.path Path to validations
|
78
|
+
* @property {Object} write.partial
|
79
|
+
* @property {Function} write.partial.callback Callback function to be executed after the request has been completed. (obj, diffResult) => obj
|
80
|
+
* @property {Object} write.sheathing
|
81
|
+
* @property {Object} write.sheathing.object Object to be wrapped
|
82
|
+
* @property {string} write.sheathing.path Path to the data
|
83
|
+
* @property {Object} read={} Options
|
84
|
+
* @property {Object} read.init={} An option object containing any custom settings that you want to apply to the request. The parameters are identical to those of the {@link https://developer.mozilla.org/en-US/docs/Web/API/Request/Request|Request constructor}
|
85
|
+
* @property {string} read.init.method=GET
|
86
|
+
* @property {array} read.acceptedStatus=[200]
|
87
|
+
* @property {string} read.url URL
|
88
|
+
* @property {Object} read.mapping the mapping is applied after reading.
|
89
|
+
* @property {String} read.mapping.transformer Transformer to select the appropriate entries
|
90
|
+
* @property {exampleCallback[]} read.mapping.callback with the help of the callback, the structures can be adjusted after reading.
|
91
|
+
*/
|
92
|
+
get defaults() {
|
93
|
+
return Object.assign({}, super.defaults, {
|
94
|
+
write: {
|
95
|
+
init: {
|
96
|
+
method: "POST",
|
97
|
+
headers: null,
|
98
|
+
},
|
99
|
+
responseCallback: null,
|
100
|
+
acceptedStatus: [200, 201],
|
101
|
+
url: null,
|
102
|
+
mapping: {
|
103
|
+
transformer: null,
|
104
|
+
callbacks: [],
|
105
|
+
},
|
106
|
+
sheathing: {
|
107
|
+
object: null,
|
108
|
+
path: null,
|
109
|
+
},
|
110
|
+
report: {
|
111
|
+
path: null,
|
112
|
+
},
|
113
|
+
|
114
|
+
partial: {
|
115
|
+
callback: null,
|
116
|
+
},
|
117
|
+
},
|
118
|
+
read: {
|
119
|
+
init: {
|
120
|
+
method: "GET",
|
121
|
+
headers: null,
|
122
|
+
},
|
123
|
+
path: null,
|
124
|
+
responseCallback: null,
|
125
|
+
acceptedStatus: [200],
|
126
|
+
url: null,
|
127
|
+
mapping: {
|
128
|
+
transformer: null,
|
129
|
+
callbacks: [],
|
130
|
+
},
|
131
|
+
},
|
132
|
+
});
|
133
|
+
}
|
134
|
+
|
135
|
+
/**
|
136
|
+
* @return {Promise}
|
137
|
+
* @throws {Error} the options does not contain a valid json definition
|
138
|
+
* @throws {TypeError} value is not a object
|
139
|
+
* @throws {Error} the data cannot be read
|
140
|
+
*/
|
141
|
+
read() {
|
142
|
+
let init = this.getOption("read.init");
|
143
|
+
if (!isObject(init)) init = {};
|
144
|
+
if (!(init["headers"] instanceof Headers)) {
|
145
|
+
init["headers"] = new Headers();
|
146
|
+
init["headers"].append("Accept", "application/json");
|
147
|
+
init["headers"].append("X-Requested-With", "XMLHttpRequest");
|
148
|
+
}
|
149
|
+
|
150
|
+
if (!init["method"]) init["method"] = "GET";
|
151
|
+
|
152
|
+
let callback = this.getOption("read.responseCallback");
|
153
|
+
if (!callback) {
|
154
|
+
callback = (obj) => {
|
155
|
+
this.set(this.transformServerPayload.call(this, obj));
|
156
|
+
};
|
157
|
+
}
|
158
|
+
|
159
|
+
return fetchData.call(this, init, "read", callback);
|
160
|
+
}
|
161
|
+
|
162
|
+
/**
|
163
|
+
* @return {Promise}
|
164
|
+
* @throws {WriteError} the data cannot be written
|
165
|
+
*/
|
166
|
+
write() {
|
167
|
+
let init = this.getOption("write.init");
|
168
|
+
if (!isObject(init)) init = {};
|
169
|
+
if (!(init["headers"] instanceof Headers)) {
|
170
|
+
init["headers"] = new Headers();
|
171
|
+
init["headers"].append("Accept", "application/json");
|
172
|
+
init["headers"].append("Content-Type", "application/json");
|
173
|
+
}
|
174
|
+
if (!init["method"]) init["method"] = "POST";
|
175
|
+
|
176
|
+
const obj = this.prepareServerPayload(this.get());
|
177
|
+
init["body"] = JSON.stringify(obj);
|
178
|
+
|
179
|
+
const callback = this.getOption("write.responseCallback");
|
180
|
+
return fetchData.call(this, init, "write", callback);
|
181
|
+
}
|
182
|
+
|
183
|
+
/**
|
184
|
+
* @return {RestAPI}
|
185
|
+
*/
|
186
|
+
getClone() {
|
187
|
+
const api = new RestAPI();
|
188
|
+
|
189
|
+
const read = clone(this[internalSymbol].getRealSubject()["options"].read);
|
190
|
+
const write = clone(this[internalSymbol].getRealSubject()["options"].write);
|
191
|
+
|
192
|
+
api.setOption("read", read);
|
193
|
+
api.setOption("write", write);
|
194
|
+
|
195
|
+
return api;
|
196
|
+
}
|
197
197
|
}
|
198
198
|
|
199
199
|
/**
|
@@ -204,49 +204,49 @@ class RestAPI extends Server {
|
|
204
204
|
* @return {Promise<string>}
|
205
205
|
*/
|
206
206
|
function fetchData(init, key, callback) {
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
207
|
+
let response;
|
208
|
+
|
209
|
+
return fetch(this.getOption(`${key}.url`), init)
|
210
|
+
.then((resp) => {
|
211
|
+
response = resp;
|
212
|
+
|
213
|
+
const acceptedStatus = this.getOption(`${key}.acceptedStatus`, [200]).map(
|
214
|
+
Number,
|
215
|
+
);
|
216
|
+
|
217
|
+
if (acceptedStatus.indexOf(resp.status) === -1) {
|
218
|
+
throw new DataFetchError(
|
219
|
+
`the response does not contain an accepted status (actual: ${resp.status}).`,
|
220
|
+
response,
|
221
|
+
);
|
222
|
+
}
|
223
|
+
|
224
|
+
return resp.text();
|
225
|
+
})
|
226
|
+
.then((body) => {
|
227
|
+
let obj;
|
228
|
+
|
229
|
+
try {
|
230
|
+
obj = JSON.parse(body);
|
231
|
+
|
232
|
+
response[rawDataSymbol] = obj;
|
233
|
+
} catch (e) {
|
234
|
+
if (body.length > 100) {
|
235
|
+
body = `${body.substring(0, 97)}...`;
|
236
|
+
}
|
237
|
+
|
238
|
+
throw new DataFetchError(
|
239
|
+
`the response does not contain a valid json (actual: ${body}).`,
|
240
|
+
response,
|
241
|
+
);
|
242
|
+
}
|
243
|
+
|
244
|
+
if (callback && isFunction(callback)) {
|
245
|
+
callback(obj);
|
246
|
+
}
|
247
|
+
return response;
|
248
|
+
})
|
249
|
+
.catch((e) => {
|
250
|
+
throw e;
|
251
|
+
});
|
252
252
|
}
|
package/source/types/version.mjs
CHANGED
package/test/cases/monster.mjs
CHANGED
package/test/web/test.html
CHANGED
@@ -9,8 +9,8 @@
|
|
9
9
|
</head>
|
10
10
|
<body>
|
11
11
|
<div id="headline" style="display: flex;align-items: center;justify-content: center;flex-direction: column;">
|
12
|
-
<h1 style='margin-bottom: 0.1em;'>Monster 3.
|
13
|
-
<div id="lastupdate" style='font-size:0.7em'>last update
|
12
|
+
<h1 style='margin-bottom: 0.1em;'>Monster 3.100.17</h1>
|
13
|
+
<div id="lastupdate" style='font-size:0.7em'>last update Di 21. Jan 00:44:02 CET 2025</div>
|
14
14
|
</div>
|
15
15
|
<div id="mocha-errors"
|
16
16
|
style="color: red;font-weight: bold;display: flex;align-items: center;justify-content: center;flex-direction: column;margin:20px;"></div>
|