@schukai/monster 3.10.1 → 3.12.0
Sign up to get free protection for your applications and to get access to all the features.
package/package.json
CHANGED
@@ -12,6 +12,16 @@ import { WriteError } from "./restapi/writeerror.mjs";
|
|
12
12
|
|
13
13
|
export { RestAPI };
|
14
14
|
|
15
|
+
|
16
|
+
|
17
|
+
/**
|
18
|
+
* @type {symbol}
|
19
|
+
* @memberOf Monster
|
20
|
+
* @license AGPLv3
|
21
|
+
* @since 3.12.0
|
22
|
+
*/
|
23
|
+
const rawDataSymbol = Symbol.for("@schukai/monster/data/datasource/server/restapi/rawdata");
|
24
|
+
|
15
25
|
/**
|
16
26
|
* The RestAPI is a class that enables a REST API server.
|
17
27
|
*
|
@@ -49,6 +59,7 @@ class RestAPI extends Server {
|
|
49
59
|
* @property {Object} write.init={} An options 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}
|
50
60
|
* @property {string} write.init.method=POST
|
51
61
|
* @property {Object} write.init.headers Object containing any custom headers that you want to apply to the request.
|
62
|
+
* @property {string} write.responseCallback Callback function to be executed after the request has been completed.
|
52
63
|
* @property {string} write.acceptedStatus=[200,201]
|
53
64
|
* @property {string} write.url URL
|
54
65
|
* @property {Object} write.mapping the mapping is applied before writing.
|
@@ -74,6 +85,7 @@ class RestAPI extends Server {
|
|
74
85
|
init: {
|
75
86
|
method: "POST",
|
76
87
|
},
|
88
|
+
responseCallback: undefined,
|
77
89
|
acceptedStatus: [200, 201],
|
78
90
|
url: undefined,
|
79
91
|
mapping: {
|
@@ -92,6 +104,7 @@ class RestAPI extends Server {
|
|
92
104
|
init: {
|
93
105
|
method: "GET",
|
94
106
|
},
|
107
|
+
responseCallback: undefined,
|
95
108
|
acceptedStatus: [200],
|
96
109
|
url: undefined,
|
97
110
|
mapping: {
|
@@ -115,9 +128,12 @@ class RestAPI extends Server {
|
|
115
128
|
if (!isObject(init)) init = {};
|
116
129
|
if (!init["method"]) init["method"] = "GET";
|
117
130
|
|
118
|
-
|
131
|
+
let callback = self.getOption("read.responseCallback");
|
132
|
+
if(!callback) callback = (obj) => {
|
119
133
|
self.set(self.transformServerPayload.call(self, obj));
|
120
|
-
}
|
134
|
+
};
|
135
|
+
|
136
|
+
return fetchData.call(this, "read", callback);
|
121
137
|
}
|
122
138
|
|
123
139
|
/**
|
@@ -139,7 +155,8 @@ class RestAPI extends Server {
|
|
139
155
|
let obj = self.prepareServerPayload(self.get());
|
140
156
|
init["body"] = JSON.stringify(obj);
|
141
157
|
|
142
|
-
|
158
|
+
let callback = self.getOption("write.responseCallback");
|
159
|
+
return fetchData.call(this, init, "write", callback);
|
143
160
|
}
|
144
161
|
|
145
162
|
/**
|
@@ -175,6 +192,9 @@ function fetchData(init, key, callback) {
|
|
175
192
|
|
176
193
|
try {
|
177
194
|
obj = JSON.parse(body);
|
195
|
+
|
196
|
+
response[rawDataSymbol] = obj;
|
197
|
+
|
178
198
|
} catch (e) {
|
179
199
|
if (body.length > 100) {
|
180
200
|
body = `${body.substring(0, 97)}...`;
|
@@ -186,6 +206,7 @@ function fetchData(init, key, callback) {
|
|
186
206
|
if (callback && isFunction(callback)) {
|
187
207
|
callback(obj);
|
188
208
|
}
|
209
|
+
|
189
210
|
return response;
|
190
211
|
});
|
191
212
|
}
|
package/source/types/version.mjs
CHANGED
package/test/cases/monster.mjs
CHANGED