From 03986050487a342f07e981469bc159567a4c70ba Mon Sep 17 00:00:00 2001 From: siyuniu-ms <123212536+siyuniu-ms@users.noreply.github.com> Date: Fri, 27 Oct 2023 13:52:46 -0700 Subject: [PATCH] [main] retrieve reponseText when fetch in HttpManager (#2185) --- channels/1ds-post-js/src/HttpManager.ts | 37 +++++++++++++++---------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/channels/1ds-post-js/src/HttpManager.ts b/channels/1ds-post-js/src/HttpManager.ts index e8a112482..d09466f46 100644 --- a/channels/1ds-post-js/src/HttpManager.ts +++ b/channels/1ds-post-js/src/HttpManager.ts @@ -470,6 +470,23 @@ export class HttpManager { requestInit.headers = payload.headers; } + const handleResponse = (status: number, headerMap: { [x: string]: string; }, responseText: string) => { + if (!responseHandled) { + responseHandled = true; + _doOnComplete(oncomplete, status, headerMap, responseText); + _handleCollectorResponse(responseText); + } + }; + + const handleError = () => { + // In case there is an error in the request. Set the status to 0 + // so that the events can be retried later. + if (!responseHandled) { + responseHandled = true; + _doOnComplete(oncomplete, 0, {}); + } + }; + fetch(theUrl, requestInit).then((response) => { let headerMap = {}; let responseText = STR_EMPTY; @@ -482,22 +499,12 @@ export class HttpManager { if (response.body) { response.text().then(function(text) { responseText = text; - }); - } - - if (!responseHandled) { - responseHandled = true; - _doOnComplete(oncomplete, response.status, headerMap, responseText); - _handleCollectorResponse(responseText); - } - }).catch((error) => { - // In case there is an error in the request. Set the status to 0 - // so that the events can be retried later. - if (!responseHandled) { - responseHandled = true; - _doOnComplete(oncomplete, 0, {}); + handleResponse(response.status, headerMap, responseText); + }, handleError); + } else { + handleResponse(response.status, headerMap, ""); } - }); + }).catch(handleError); if (ignoreResponse && !responseHandled) { // Assume success during unload processing