Skip to content

Commit 18afdce

Browse files
Travis Tranmrin9
authored andcommitted
add fetched-try event to support developers the ability to modify response (in case of using E2EE)
1 parent a6ee244 commit 18afdce

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

src/components/api-request.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1465,6 +1465,21 @@ export default class ApiRequest extends LitElement {
14651465
const startTime = performance.now();
14661466
fetchResponse = await fetch(fetchRequest, { signal });
14671467
const endTime = performance.now();
1468+
// Allow to modify response
1469+
let resolveModifiedResponse; // Create a promise that will be resolved from the event listener
1470+
const modifiedResponsePromise = new Promise((resolve) => {
1471+
resolveModifiedResponse = resolve;
1472+
});
1473+
this.dispatchEvent(new CustomEvent('fetched-try', {
1474+
bubbles: true,
1475+
composed: true,
1476+
detail: {
1477+
request: fetchRequest,
1478+
response: fetchResponse,
1479+
resolveModifiedResponse, // pass the resolver function
1480+
},
1481+
}));
1482+
fetchResponse = await modifiedResponsePromise; // Wait for the modified response
14681483
responseClone = fetchResponse.clone(); // create a response clone to allow reading response body again (response.json, response.text etc)
14691484
tryBtnEl.disabled = false;
14701485
this.responseMessage = html`${fetchResponse.statusText ? `${fetchResponse.statusText}:${fetchResponse.status}` : fetchResponse.status} <div style="color:var(--light-fg)"> Took ${Math.round(endTime - startTime)} milliseconds </div>`;

0 commit comments

Comments
 (0)