fix: clone response when reading the response for error (#2049)

This commit is contained in:
Joren Broekema 2023-08-01 10:45:27 +02:00 committed by GitHub
parent 16d95c624e
commit 9cdac8e947
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 11 additions and 3 deletions

View file

@ -0,0 +1,5 @@
---
'@lion/ajax': patch
---
Clone response before reading and parsing the body.

4
package-lock.json generated
View file

@ -22384,7 +22384,7 @@
}, },
"packages/ajax": { "packages/ajax": {
"name": "@lion/ajax", "name": "@lion/ajax",
"version": "1.1.4", "version": "1.2.0",
"license": "MIT" "license": "MIT"
}, },
"packages/singleton-manager": { "packages/singleton-manager": {
@ -22393,7 +22393,7 @@
}, },
"packages/ui": { "packages/ui": {
"name": "@lion/ui", "name": "@lion/ui",
"version": "0.3.4", "version": "0.3.5",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"@bundled-es-modules/message-format": "^6.0.4", "@bundled-es-modules/message-format": "^6.0.4",

View file

@ -203,7 +203,8 @@ export class Ajax {
* @returns {Promise<string|Object>} * @returns {Promise<string|Object>}
*/ */
async __parseBody(response) { async __parseBody(response) {
let responseText = await response.text(); // clone the response, so the consumer can also read it out manually as well
let responseText = await response.clone().text();
const { jsonPrefix } = this.__config; const { jsonPrefix } = this.__config;
if (typeof jsonPrefix === 'string' && responseText.startsWith(jsonPrefix)) { if (typeof jsonPrefix === 'string' && responseText.startsWith(jsonPrefix)) {

View file

@ -215,6 +215,8 @@ describe('Ajax', () => {
expect(_e.request).to.be.an.instanceOf(Request); expect(_e.request).to.be.an.instanceOf(Request);
expect(_e.response).to.be.an.instanceOf(Response); expect(_e.response).to.be.an.instanceOf(Response);
expect(_e.body).to.equal('my response'); expect(_e.body).to.equal('my response');
const bodyFromResponse = await _e.response.text();
expect(bodyFromResponse).to.equal('my response');
thrown = true; thrown = true;
} }
expect(thrown).to.be.true; expect(thrown).to.be.true;