Skip to content

Commit

Permalink
fix(http): add support for defining xhr and angular http response types
Browse files Browse the repository at this point in the history
  • Loading branch information
ItsChaceD authored Sep 14, 2023
1 parent 2fe4535 commit 09bd040
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 12 deletions.
18 changes: 14 additions & 4 deletions android/capacitor/src/main/assets/native-bridge.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
19 changes: 15 additions & 4 deletions core/native-bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
18 changes: 14 additions & 4 deletions ios/Capacitor/Capacitor/assets/native-bridge.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 09bd040

Please sign in to comment.