Skip to content

Commit

Permalink
fix(http): add support for Request objects in fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
ItsChaceD authored Sep 14, 2023
1 parent 5cd3b2f commit 2fe4535
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 29 deletions.
17 changes: 8 additions & 9 deletions android/capacitor/src/main/assets/native-bridge.js
Original file line number Diff line number Diff line change
Expand Up @@ -424,22 +424,21 @@ var nativeBridge = (function (exports) {
if (doPatchHttp) {
// fetch patch
window.fetch = async (resource, options) => {
if (!(resource.toString().startsWith('http:') ||
resource.toString().startsWith('https:'))) {
const request = new Request(resource, options);
if (!(request.url.startsWith('http:') ||
request.url.startsWith('https:'))) {
return win.CapacitorWebFetch(resource, options);
}
const tag = `CapacitorHttp fetch ${Date.now()} ${resource}`;
console.time(tag);
try {
// intercept request & pass to the bridge
const { data: requestData, type, headers, } = await convertBody((options === null || options === void 0 ? void 0 : options.body) || undefined);
let optionHeaders = options === null || options === void 0 ? void 0 : options.headers;
if ((options === null || options === void 0 ? void 0 : options.headers) instanceof Headers) {
optionHeaders = Object.fromEntries(options.headers.entries());
}
const { body, method } = request;
const { data: requestData, type, headers, } = await convertBody(body || undefined);
const optionHeaders = Object.fromEntries(request.headers.entries());
const nativeResponse = await cap.nativePromise('CapacitorHttp', 'request', {
url: resource,
method: (options === null || options === void 0 ? void 0 : options.method) ? options.method : undefined,
url: request.url,
method: method,
data: requestData,
dataType: type,
headers: Object.assign(Object.assign({}, headers), optionHeaders),
Expand Down
20 changes: 9 additions & 11 deletions core/native-bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -472,10 +472,12 @@ const initBridge = (w: any): void => {
resource: RequestInfo | URL,
options?: RequestInit,
) => {
const request = new Request(resource, options);

if (
!(
resource.toString().startsWith('http:') ||
resource.toString().startsWith('https:')
request.url.startsWith('http:') ||
request.url.startsWith('https:')
)
) {
return win.CapacitorWebFetch(resource, options);
Expand All @@ -485,23 +487,19 @@ const initBridge = (w: any): void => {
console.time(tag);
try {
// intercept request & pass to the bridge
const { body, method } = request;
const {
data: requestData,
type,
headers,
} = await convertBody(options?.body || undefined);
let optionHeaders = options?.headers;
if (options?.headers instanceof Headers) {
optionHeaders = Object.fromEntries(
(options.headers as any).entries(),
);
}
} = await convertBody(body || undefined);
const optionHeaders = Object.fromEntries(request.headers.entries());
const nativeResponse: HttpResponse = await cap.nativePromise(
'CapacitorHttp',
'request',
{
url: resource,
method: options?.method ? options.method : undefined,
url: request.url,
method: method,
data: requestData,
dataType: type,
headers: {
Expand Down
17 changes: 8 additions & 9 deletions ios/Capacitor/Capacitor/assets/native-bridge.js
Original file line number Diff line number Diff line change
Expand Up @@ -424,22 +424,21 @@ var nativeBridge = (function (exports) {
if (doPatchHttp) {
// fetch patch
window.fetch = async (resource, options) => {
if (!(resource.toString().startsWith('http:') ||
resource.toString().startsWith('https:'))) {
const request = new Request(resource, options);
if (!(request.url.startsWith('http:') ||
request.url.startsWith('https:'))) {
return win.CapacitorWebFetch(resource, options);
}
const tag = `CapacitorHttp fetch ${Date.now()} ${resource}`;
console.time(tag);
try {
// intercept request & pass to the bridge
const { data: requestData, type, headers, } = await convertBody((options === null || options === void 0 ? void 0 : options.body) || undefined);
let optionHeaders = options === null || options === void 0 ? void 0 : options.headers;
if ((options === null || options === void 0 ? void 0 : options.headers) instanceof Headers) {
optionHeaders = Object.fromEntries(options.headers.entries());
}
const { body, method } = request;
const { data: requestData, type, headers, } = await convertBody(body || undefined);
const optionHeaders = Object.fromEntries(request.headers.entries());
const nativeResponse = await cap.nativePromise('CapacitorHttp', 'request', {
url: resource,
method: (options === null || options === void 0 ? void 0 : options.method) ? options.method : undefined,
url: request.url,
method: method,
data: requestData,
dataType: type,
headers: Object.assign(Object.assign({}, headers), optionHeaders),
Expand Down

0 comments on commit 2fe4535

Please sign in to comment.