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

fix: error in the status processing

parent 5e4df080
No related branches found
No related tags found
No related merge requests found
...@@ -114,44 +114,17 @@ class RestAPI extends Server { ...@@ -114,44 +114,17 @@ class RestAPI extends Server {
* @throws {Error} the data cannot be read * @throws {Error} the data cannot be read
*/ */
read() { read() {
const self = this; const self = this;
let response;
let init = self.getOption('read.init'); let init = self.getOption('read.init');
if (!isObject(init)) init = {}; if (!isObject(init)) init = {};
if (!init['method']) init['method'] = 'GET';
return new Promise((resolve, reject) => { return fetchData.call(this, 'read', (obj) => {
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 + ').');
}
self.set(self.transformServerPayload.call(self, obj)); self.set(self.transformServerPayload.call(self, obj));
resolve(response); });
}).catch(reject);
})
} }
/** /**
...@@ -159,6 +132,7 @@ class RestAPI extends Server { ...@@ -159,6 +132,7 @@ class RestAPI extends Server {
* @throws {WriteError} the data cannot be written * @throws {WriteError} the data cannot be written
*/ */
write() { write() {
const self = this; const self = this;
let init = self.getOption('write.init'); let init = self.getOption('write.init');
...@@ -168,60 +142,65 @@ class RestAPI extends Server { ...@@ -168,60 +142,65 @@ class RestAPI extends Server {
'Content-Type': 'application/json' 'Content-Type': 'application/json'
} }
} }
if (!init['method']) init['method'] = 'POST';
let obj = self.prepareServerPayload(self.get()); let obj = self.prepareServerPayload(self.get());
init['body'] = JSON.stringify(obj); init['body'] = JSON.stringify(obj);
return new Promise((resolve, reject) => { return fetchData.call(this, init, 'write');
fetch(self.getOption('write.url'), init).then(response => {
const acceptedStatus = self.getOption('write.acceptedStatus', [200, 201]);
if (acceptedStatus.indexOf(response.status) > -1) {
reject(response);
return;
} }
response.text().then((body) => {
let obj = {}, validation = {}; /**
try { * @return {RestAPI}
obj = JSON.parse(body); */
getClone() {
const self = this;
return new RestAPI(self[internalSymbol].getRealSubject()['options'].read, self[internalSymbol].getRealSubject()['options'].write);
}
if (reportPath) {
validation = (new Pathfinder(obj)).getVia(reportPath);
} }
} catch (e) { function fetchData(init, key, callback) {
if (body.length > 100) { const self = this;
body = body.substring(0, 97) + '...'; let response;
}
reject(new Error('the response does not contain a valid json (actual: ' + body + ').'));
return;
}
reject(new WriteError('the data cannot be written (response ' + response.status + ')', response, validation)) return fetch(self.getOption(key + '.url'), init).then(resp => {
return; response = resp;
const acceptedStatus = self.getOption(key + '.acceptedStatus', [200]);
if (acceptedStatus.indexOf(resp.status) === -1) {
throw Error('the data cannot be ' + key + ' (response ' + resp.status + ')')
}
}).catch(reject); return resp.text()
}).then(body => {
let obj;
}).catch(reject); 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 + ').');
}
/** if (callback && isFunction(callback)) {
* @return {RestAPI} callback(obj);
*/
getClone() {
const self = this;
return new RestAPI(self[internalSymbol].getRealSubject()['options'].read, self[internalSymbol].getRealSubject()['options'].write);
} }
return response;
});
} }
......
...@@ -28,9 +28,6 @@ describe('RestAPI', function () { ...@@ -28,9 +28,6 @@ describe('RestAPI', function () {
a: "test" a: "test"
})); }));
}); });
}, },
status: returnStatus status: returnStatus
}); });
...@@ -55,19 +52,23 @@ describe('RestAPI', function () { ...@@ -55,19 +52,23 @@ describe('RestAPI', function () {
}); });
it('write should ', function (done) { it('write should ', function (done) {
const ds = new RestAPI({url: 'https://monsterjs.org/assets/world.json'}, const ds = new RestAPI({
{ read: {
url: 'https://monsterjs.org/assets/world.json'
},
write: {
url: 'https://monsterjs.org/assets/world.json', url: 'https://monsterjs.org/assets/world.json',
acceptedStatus: [99] acceptedStatus: [99]
}) }
}
)
ds.write().then(data => { ds.write().then(data => {
done("should not be here"); done("should not be here");
}).catch(e => done()); }).catch(e => done());
}); });
});
})
describe('rw with errors', function () { describe('rw with errors', function () {
it('read should throw exception', function (done) { it('read should throw exception', function (done) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment