@schukai/monster 3.4.1 → 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/server/restapi.mjs +46 -67
- package/source/data/datasource/server/webconnect.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/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
@@ -17,7 +17,7 @@ export {RestAPI}
|
|
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
|
|
@@ -26,7 +26,7 @@ const webConnectSymbol = Symbol("connection");
|
|
26
26
|
/**
|
27
27
|
* The RestAPI is a class that enables a REST API server.
|
28
28
|
*
|
29
|
-
* @externalExample
|
29
|
+
* @externalExample ../../../../example/data/datasource/server/webconnect.mjs
|
30
30
|
* @license AGPLv3
|
31
31
|
* @since 3.1.0
|
32
32
|
* @copyright schukai GmbH
|
@@ -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
@@ -1,11 +0,0 @@
|
|
1
|
-
import {RestAPI} from '@schukai/monster/source/data/datasource/restapi.mjs';
|
2
|
-
|
3
|
-
const ds = new RestAPI({
|
4
|
-
url: 'https://httpbin.org/get'
|
5
|
-
}, {
|
6
|
-
url: 'https://httpbin.org/post'
|
7
|
-
});
|
8
|
-
|
9
|
-
ds.set({flag: true})
|
10
|
-
ds.write().then(() => console.log('done'));
|
11
|
-
ds.read().then(() => console.log('done'));
|