@schukai/monster 3.4.0 → 3.4.2
Sign up to get free protection for your applications and to get access to all the features.
- package/example/data/datasource/server/restapi.mjs +14 -0
- package/example/data/datasource/server/webconnect.mjs +9 -0
- package/example/net/webconnect.mjs +16 -0
- package/package.json +1 -1
- package/source/data/datasource/{restapi → server}/namespace.mjs +2 -2
- package/source/data/datasource/server/restapi/namespace.mjs +13 -0
- package/source/data/datasource/{restapi → server/restapi}/writeerror.mjs +3 -3
- package/source/data/datasource/server/restapi.mjs +47 -68
- package/source/data/datasource/server/webconnect.mjs +1 -3
- package/source/monster.mjs +1 -1
- package/source/net/namespace.mjs +15 -0
- package/source/net/webconnect/message.mjs +6 -0
- package/source/net/webconnect/namespace.mjs +15 -0
- package/source/net/webconnect.mjs +3 -3
- package/source/types/version.mjs +1 -1
- package/test/cases/data/datasource/server/restapi.mjs +12 -11
- package/test/cases/monster.mjs +1 -1
- package/test/web/test.html +2 -2
- package/test/web/tests.js +6 -6
- package/example/data/storage/restapi.mjs +0 -11
@@ -0,0 +1,14 @@
|
|
1
|
+
import {RestAPI} from '@schukai/monster/source/data/datasource/server/restapi.mjs';
|
2
|
+
|
3
|
+
const ds = new RestAPI({
|
4
|
+
write: {
|
5
|
+
url: 'https://httpbin.org/get'
|
6
|
+
},
|
7
|
+
read: {
|
8
|
+
url: 'https://httpbin.org/get'
|
9
|
+
}
|
10
|
+
});
|
11
|
+
|
12
|
+
ds.set({flag: true})
|
13
|
+
ds.write().then(() => console.log('done'));
|
14
|
+
ds.read().then(() => console.log('done'));
|
@@ -0,0 +1,9 @@
|
|
1
|
+
import {WebConnect} from '@schukai/monster/source/data/datasource/server/webconnect.mjs';
|
2
|
+
|
3
|
+
const ds = new WebConnect({
|
4
|
+
url: 'https://httpbin.org/get'
|
5
|
+
});
|
6
|
+
|
7
|
+
ds.set({flag: true})
|
8
|
+
ds.write().then(() => console.log('done'));
|
9
|
+
ds.read().then(() => console.log('done'));
|
@@ -0,0 +1,16 @@
|
|
1
|
+
import {WebConnect} from '@schukai/monster/source/net/webconnect.mjs';
|
2
|
+
|
3
|
+
const connection = new WebConnect({
|
4
|
+
url: 'https://httpbin.org/get'
|
5
|
+
});
|
6
|
+
|
7
|
+
connection.connect().then(()=>{
|
8
|
+
|
9
|
+
connection.send({message:"Hello World!"}).then((response)=>{
|
10
|
+
console.log(response);
|
11
|
+
});
|
12
|
+
|
13
|
+
const message = connection.poll()
|
14
|
+
|
15
|
+
|
16
|
+
})
|
package/package.json
CHANGED
@@ -0,0 +1,13 @@
|
|
1
|
+
/**
|
2
|
+
* Copyright 2022 schukai GmbH
|
3
|
+
* SPDX-License-Identifier: AGPL-3.0
|
4
|
+
*/
|
5
|
+
|
6
|
+
/**
|
7
|
+
* Namespace for storages
|
8
|
+
*
|
9
|
+
* @namespace Monster.Data.Datasource.Server.RestAPI
|
10
|
+
* @memberOf Monster.Data.Datasource.Server
|
11
|
+
* @author schukai GmbH
|
12
|
+
*/
|
13
|
+
const ns = {};
|
@@ -5,7 +5,7 @@
|
|
5
5
|
* License text available at https://www.gnu.org/licenses/agpl-3.0.en.html
|
6
6
|
*/
|
7
7
|
|
8
|
-
import {internalSymbol,instanceSymbol} from "
|
8
|
+
import {internalSymbol,instanceSymbol} from "../../../../constants.mjs";
|
9
9
|
|
10
10
|
export {WriteError}
|
11
11
|
|
@@ -15,7 +15,7 @@ export {WriteError}
|
|
15
15
|
* @license AGPLv3
|
16
16
|
* @since 1.24.0
|
17
17
|
* @copyright schukai GmbH
|
18
|
-
* @memberOf Monster.Data.Datasource.RestAPI
|
18
|
+
* @memberOf Monster.Data.Datasource.Server.RestAPI
|
19
19
|
* @summary the error is thrown by the rest api in case of error
|
20
20
|
*/
|
21
21
|
class WriteError extends Error {
|
@@ -38,7 +38,7 @@ class WriteError extends Error {
|
|
38
38
|
* @since 2.1.0
|
39
39
|
*/
|
40
40
|
static get [instanceSymbol]() {
|
41
|
-
return Symbol.for("@schukai/monster/data/datasource/restapi/writeerror");
|
41
|
+
return Symbol.for("@schukai/monster/data/datasource/server/restapi/writeerror");
|
42
42
|
}
|
43
43
|
|
44
44
|
/**
|
@@ -10,14 +10,14 @@ import {isObject} from "../../../types/is.mjs";
|
|
10
10
|
import {Server} from "../server.mjs";
|
11
11
|
import {Pathfinder} from "../../pathfinder.mjs";
|
12
12
|
import {Pipe} from "../../pipe.mjs";
|
13
|
-
import {WriteError} from "
|
13
|
+
import {WriteError} from "./restapi/writeerror.mjs";
|
14
14
|
|
15
15
|
export {RestAPI}
|
16
16
|
|
17
17
|
/**
|
18
18
|
* The RestAPI is a class that enables a REST API server.
|
19
19
|
*
|
20
|
-
* @externalExample
|
20
|
+
* @externalExample ../../../../example/data/datasource/server/restapi.mjs
|
21
21
|
* @license AGPLv3
|
22
22
|
* @since 1.22.0
|
23
23
|
* @copyright schukai GmbH
|
@@ -114,44 +114,17 @@ class RestAPI extends Server {
|
|
114
114
|
* @throws {Error} the data cannot be read
|
115
115
|
*/
|
116
116
|
read() {
|
117
|
+
|
117
118
|
const self = this;
|
118
|
-
let response;
|
119
119
|
|
120
120
|
let init = self.getOption('read.init');
|
121
121
|
if (!isObject(init)) init = {};
|
122
|
+
if (!init['method']) init['method'] = 'GET';
|
122
123
|
|
123
|
-
return
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
const acceptedStatus = self.getOption('read.acceptedStatus', [200]);
|
128
|
-
|
129
|
-
if (acceptedStatus.indexOf(resp.status) === -1) {
|
130
|
-
throw Error('the data cannot be read (response ' + resp.status + ')')
|
131
|
-
}
|
132
|
-
|
133
|
-
return resp.text()
|
134
|
-
}).then(body => {
|
135
|
-
|
136
|
-
let obj;
|
137
|
-
|
138
|
-
try {
|
139
|
-
obj = JSON.parse(body);
|
140
|
-
|
141
|
-
} catch (e) {
|
142
|
-
|
143
|
-
if (body.length > 100) {
|
144
|
-
body = body.substring(0, 97) + '...';
|
145
|
-
}
|
146
|
-
|
147
|
-
throw new Error('the response does not contain a valid json (actual: ' + body + ').');
|
148
|
-
}
|
149
|
-
|
150
|
-
self.set(self.transformServerPayload.call(self, obj));
|
151
|
-
resolve(response);
|
152
|
-
}).catch(reject);
|
124
|
+
return fetchData.call(this, 'read', (obj) => {
|
125
|
+
self.set(self.transformServerPayload.call(self, obj));
|
126
|
+
});
|
153
127
|
|
154
|
-
})
|
155
128
|
}
|
156
129
|
|
157
130
|
/**
|
@@ -159,6 +132,7 @@ class RestAPI extends Server {
|
|
159
132
|
* @throws {WriteError} the data cannot be written
|
160
133
|
*/
|
161
134
|
write() {
|
135
|
+
|
162
136
|
const self = this;
|
163
137
|
|
164
138
|
let init = self.getOption('write.init');
|
@@ -168,60 +142,65 @@ class RestAPI extends Server {
|
|
168
142
|
'Content-Type': 'application/json'
|
169
143
|
}
|
170
144
|
}
|
145
|
+
if (!init['method']) init['method'] = 'POST';
|
171
146
|
|
172
147
|
let obj = self.prepareServerPayload(self.get());
|
173
148
|
init['body'] = JSON.stringify(obj);
|
174
149
|
|
175
|
-
return
|
176
|
-
|
177
|
-
const acceptedStatus = self.getOption('write.acceptedStatus', [200, 201]);
|
150
|
+
return fetchData.call(this, init, 'write');
|
151
|
+
}
|
178
152
|
|
179
|
-
if (acceptedStatus.indexOf(response.status) > -1) {
|
180
|
-
reject(response);
|
181
|
-
return;
|
182
|
-
}
|
183
153
|
|
184
|
-
|
154
|
+
/**
|
155
|
+
* @return {RestAPI}
|
156
|
+
*/
|
157
|
+
getClone() {
|
158
|
+
const self = this;
|
159
|
+
return new RestAPI(self[internalSymbol].getRealSubject()['options'].read, self[internalSymbol].getRealSubject()['options'].write);
|
160
|
+
}
|
185
161
|
|
186
|
-
|
187
|
-
try {
|
188
|
-
obj = JSON.parse(body);
|
162
|
+
}
|
189
163
|
|
190
|
-
if (reportPath) {
|
191
|
-
validation = (new Pathfinder(obj)).getVia(reportPath);
|
192
|
-
}
|
193
164
|
|
165
|
+
function fetchData(init, key, callback) {
|
194
166
|
|
195
|
-
|
167
|
+
const self = this;
|
168
|
+
let response;
|
196
169
|
|
197
|
-
if (body.length > 100) {
|
198
|
-
body = body.substring(0, 97) + '...';
|
199
|
-
}
|
200
170
|
|
201
|
-
|
202
|
-
|
203
|
-
}
|
171
|
+
return fetch(self.getOption(key + '.url'), init).then(resp => {
|
172
|
+
response = resp;
|
204
173
|
|
205
|
-
|
206
|
-
return;
|
174
|
+
const acceptedStatus = self.getOption(key + '.acceptedStatus', [200]);
|
207
175
|
|
208
|
-
|
176
|
+
if (acceptedStatus.indexOf(resp.status) === -1) {
|
177
|
+
throw Error('the data cannot be ' + key + ' (response ' + resp.status + ')')
|
178
|
+
}
|
209
179
|
|
180
|
+
return resp.text()
|
181
|
+
}).then(body => {
|
210
182
|
|
211
|
-
|
183
|
+
let obj;
|
212
184
|
|
213
|
-
|
185
|
+
try {
|
186
|
+
obj = JSON.parse(body);
|
214
187
|
|
215
|
-
|
188
|
+
} catch (e) {
|
216
189
|
|
190
|
+
if (body.length > 100) {
|
191
|
+
body = body.substring(0, 97) + '...';
|
192
|
+
}
|
193
|
+
|
194
|
+
throw new Error('the response does not contain a valid json (actual: ' + body + ').');
|
195
|
+
}
|
196
|
+
|
197
|
+
if (callback && isFunction(callback)) {
|
198
|
+
callback(obj);
|
199
|
+
}
|
200
|
+
return response;
|
201
|
+
|
202
|
+
});
|
217
203
|
|
218
|
-
/**
|
219
|
-
* @return {RestAPI}
|
220
|
-
*/
|
221
|
-
getClone() {
|
222
|
-
const self = this;
|
223
|
-
return new RestAPI(self[internalSymbol].getRealSubject()['options'].read, self[internalSymbol].getRealSubject()['options'].write);
|
224
|
-
}
|
225
204
|
|
226
205
|
}
|
227
206
|
|
@@ -11,10 +11,8 @@ import {WebConnect as NetWebConnect} from "../../../net/webconnect.mjs";
|
|
11
11
|
import {Message} from "../../../net/webconnect/message.mjs";
|
12
12
|
import {Server} from "../server.mjs";
|
13
13
|
|
14
|
-
|
15
14
|
export {WebConnect}
|
16
15
|
|
17
|
-
|
18
16
|
/**
|
19
17
|
* @private
|
20
18
|
* @type {Symbol}
|
@@ -28,7 +26,7 @@ const webConnectSymbol = Symbol("connection");
|
|
28
26
|
/**
|
29
27
|
* The RestAPI is a class that enables a REST API server.
|
30
28
|
*
|
31
|
-
* @externalExample
|
29
|
+
* @externalExample ../../../../example/data/datasource/server/webconnect.mjs
|
32
30
|
* @license AGPLv3
|
33
31
|
* @since 3.1.0
|
34
32
|
* @copyright schukai GmbH
|
package/source/monster.mjs
CHANGED
@@ -192,7 +192,7 @@ export {SessionStorage} from "./data/datasource/storage/sessionstorage.mjs"
|
|
192
192
|
export {LocalStorage} from "./data/datasource/storage/localstorage.mjs"
|
193
193
|
export {RestAPI} from "./data/datasource/server/restapi.mjs"
|
194
194
|
export {WebConnect} from "./data/datasource/server/webconnect.mjs"
|
195
|
-
export {WriteError} from "./data/datasource/restapi/writeerror.mjs"
|
195
|
+
export {WriteError} from "./data/datasource/server/restapi/writeerror.mjs"
|
196
196
|
export {Storage, storageObjectSymbol} from "./data/datasource/storage.mjs"
|
197
197
|
export {random} from "./math/random.mjs"
|
198
198
|
|
@@ -0,0 +1,15 @@
|
|
1
|
+
/**
|
2
|
+
* Copyright 2022 schukai GmbH
|
3
|
+
* SPDX-License-Identifier: AGPL-3.0
|
4
|
+
*/
|
5
|
+
|
6
|
+
|
7
|
+
|
8
|
+
/**
|
9
|
+
* In this namespace you will find classes and methods for handling connections.
|
10
|
+
*
|
11
|
+
* @namespace Monster.Net
|
12
|
+
* @memberOf Monster
|
13
|
+
* @author schukai GmbH
|
14
|
+
*/
|
15
|
+
const ns = {};
|
@@ -14,6 +14,12 @@ const dataSymbol = Symbol("@@data");
|
|
14
14
|
|
15
15
|
/**
|
16
16
|
* This class represents a WebSocket message.
|
17
|
+
*
|
18
|
+
* @license AGPLv3
|
19
|
+
* @since 3.4.0
|
20
|
+
* @copyright schukai GmbH
|
21
|
+
* @memberOf Monster.Net.WebSocket
|
22
|
+
* @summary The Message class encapsulates a WebSocket message.
|
17
23
|
*/
|
18
24
|
class Message extends Base {
|
19
25
|
|
@@ -0,0 +1,15 @@
|
|
1
|
+
/**
|
2
|
+
* Copyright 2022 schukai GmbH
|
3
|
+
* SPDX-License-Identifier: AGPL-3.0
|
4
|
+
*/
|
5
|
+
|
6
|
+
|
7
|
+
|
8
|
+
/**
|
9
|
+
* In this namespace you will find classes and methods for handling data.
|
10
|
+
*
|
11
|
+
* @namespace Monster.Net.WebConnect
|
12
|
+
* @memberOf Monster.Net
|
13
|
+
* @author schukai GmbH
|
14
|
+
*/
|
15
|
+
const ns = {};
|
@@ -158,18 +158,18 @@ function connectServer(resolve, reject) {
|
|
158
158
|
/**
|
159
159
|
* The RestAPI is a class that enables a REST API server.
|
160
160
|
*
|
161
|
-
* @externalExample
|
161
|
+
* @externalExample ../../example/net/webconnect.mjs
|
162
162
|
* @license AGPLv3
|
163
163
|
* @since 3.1.0
|
164
164
|
* @copyright schukai GmbH
|
165
|
-
* @memberOf Monster.
|
165
|
+
* @memberOf Monster.Net
|
166
166
|
* @summary The LocalStorage class encapsulates the access to data objects.
|
167
167
|
*/
|
168
168
|
class WebConnect extends BaseWithOptions {
|
169
169
|
|
170
170
|
/**
|
171
171
|
*
|
172
|
-
* @param {Object} [options] options contains definitions for the
|
172
|
+
* @param {Object} [options] options contains definitions for the webconnect.
|
173
173
|
*/
|
174
174
|
constructor(options) {
|
175
175
|
|
package/source/types/version.mjs
CHANGED
@@ -7,7 +7,7 @@ import {validateObject} from "../../../../../../application/source/types/validat
|
|
7
7
|
|
8
8
|
describe('RestAPI', function () {
|
9
9
|
|
10
|
-
let fetchReference;
|
10
|
+
let fetchReference;
|
11
11
|
let returnStatus;
|
12
12
|
|
13
13
|
afterEach(() => {
|
@@ -28,9 +28,6 @@ describe('RestAPI', function () {
|
|
28
28
|
a: "test"
|
29
29
|
}));
|
30
30
|
});
|
31
|
-
|
32
|
-
|
33
|
-
|
34
31
|
},
|
35
32
|
status: returnStatus
|
36
33
|
});
|
@@ -55,19 +52,23 @@ describe('RestAPI', function () {
|
|
55
52
|
});
|
56
53
|
|
57
54
|
it('write should ', function (done) {
|
58
|
-
const ds = new RestAPI({
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
55
|
+
const ds = new RestAPI({
|
56
|
+
read: {
|
57
|
+
url: 'https://monsterjs.org/assets/world.json'
|
58
|
+
},
|
59
|
+
write: {
|
60
|
+
url: 'https://monsterjs.org/assets/world.json',
|
61
|
+
acceptedStatus: [99]
|
62
|
+
}
|
63
|
+
}
|
64
|
+
)
|
63
65
|
ds.write().then(data => {
|
64
66
|
done("should not be here");
|
65
67
|
}).catch(e => done());
|
66
68
|
});
|
69
|
+
});
|
67
70
|
|
68
71
|
|
69
|
-
})
|
70
|
-
|
71
72
|
describe('rw with errors', function () {
|
72
73
|
|
73
74
|
it('read should throw exception', function (done) {
|
package/test/cases/monster.mjs
CHANGED
package/test/web/test.html
CHANGED
@@ -14,8 +14,8 @@
|
|
14
14
|
</head>
|
15
15
|
<body>
|
16
16
|
<div id="headline" style="display: flex;align-items: center;justify-content: center;flex-direction: column;">
|
17
|
-
<h1 style='margin-bottom: 0.1em;'>Monster 3.
|
18
|
-
<div id="lastupdate" style='font-size:0.7em'>last update So 8. Jan 17:
|
17
|
+
<h1 style='margin-bottom: 0.1em;'>Monster 3.4.0</h1>
|
18
|
+
<div id="lastupdate" style='font-size:0.7em'>last update So 8. Jan 17:17:24 CET 2023</div>
|
19
19
|
</div>
|
20
20
|
<div id="mocks"></div>
|
21
21
|
<div id="mocha"></div>
|