From 09bd040dfe4b8808d7499b6ee592005420406cac Mon Sep 17 00:00:00 2001 From: Chace Daniels Date: Thu, 14 Sep 2023 11:45:16 -0700 Subject: [PATCH] fix(http): add support for defining xhr and angular http response types --- .../src/main/assets/native-bridge.js | 18 ++++++++++++++---- core/native-bridge.ts | 19 +++++++++++++++---- .../Capacitor/assets/native-bridge.js | 18 ++++++++++++++---- 3 files changed, 43 insertions(+), 12 deletions(-) diff --git a/android/capacitor/src/main/assets/native-bridge.js b/android/capacitor/src/main/assets/native-bridge.js index 8c18f21317..1f72392a6a 100644 --- a/android/capacitor/src/main/assets/native-bridge.js +++ b/android/capacitor/src/main/assets/native-bridge.js @@ -586,12 +586,22 @@ var nativeBridge = (function (exports) { } this._headers = nativeResponse.headers; this.status = nativeResponse.status; + const responseString = typeof nativeResponse.data !== 'string' + ? JSON.stringify(nativeResponse.data) + : nativeResponse.data; if (this.responseType === '' || this.responseType === 'text') { - this.response = - typeof nativeResponse.data !== 'string' - ? JSON.stringify(nativeResponse.data) - : nativeResponse.data; + this.response = responseString; + } + else if (this.responseType === 'blob') { + this.response = new Blob([responseString], { + type: "application/json", + }); + } + else if (this.responseType === 'arraybuffer') { + const encoder = new TextEncoder(); + const uint8Array = encoder.encode(responseString); + this.response = uint8Array.buffer; } else { this.response = nativeResponse.data; diff --git a/core/native-bridge.ts b/core/native-bridge.ts index 8049030498..aad89fa4a8 100644 --- a/core/native-bridge.ts +++ b/core/native-bridge.ts @@ -694,14 +694,25 @@ const initBridge = (w: any): void => { } this._headers = nativeResponse.headers; this.status = nativeResponse.status; + + const responseString = + typeof nativeResponse.data !== 'string' + ? JSON.stringify(nativeResponse.data) + : nativeResponse.data; + if ( this.responseType === '' || this.responseType === 'text' ) { - this.response = - typeof nativeResponse.data !== 'string' - ? JSON.stringify(nativeResponse.data) - : nativeResponse.data; + this.response = responseString; + } else if (this.responseType === 'blob') { + this.response = new Blob([responseString], { + type: 'application/json', + }); + } else if (this.responseType === 'arraybuffer') { + const encoder = new TextEncoder(); + const uint8Array = encoder.encode(responseString); + this.response = uint8Array.buffer; } else { this.response = nativeResponse.data; } diff --git a/ios/Capacitor/Capacitor/assets/native-bridge.js b/ios/Capacitor/Capacitor/assets/native-bridge.js index 8c18f21317..1f72392a6a 100644 --- a/ios/Capacitor/Capacitor/assets/native-bridge.js +++ b/ios/Capacitor/Capacitor/assets/native-bridge.js @@ -586,12 +586,22 @@ var nativeBridge = (function (exports) { } this._headers = nativeResponse.headers; this.status = nativeResponse.status; + const responseString = typeof nativeResponse.data !== 'string' + ? JSON.stringify(nativeResponse.data) + : nativeResponse.data; if (this.responseType === '' || this.responseType === 'text') { - this.response = - typeof nativeResponse.data !== 'string' - ? JSON.stringify(nativeResponse.data) - : nativeResponse.data; + this.response = responseString; + } + else if (this.responseType === 'blob') { + this.response = new Blob([responseString], { + type: "application/json", + }); + } + else if (this.responseType === 'arraybuffer') { + const encoder = new TextEncoder(); + const uint8Array = encoder.encode(responseString); + this.response = uint8Array.buffer; } else { this.response = nativeResponse.data;