From 2fe4535e781b1a5cfa0f3359c1afa5c360073b6a Mon Sep 17 00:00:00 2001 From: Chace Daniels Date: Thu, 14 Sep 2023 10:00:32 -0700 Subject: [PATCH] fix(http): add support for Request objects in fetch --- .../src/main/assets/native-bridge.js | 17 ++++++++-------- core/native-bridge.ts | 20 +++++++++---------- .../Capacitor/assets/native-bridge.js | 17 ++++++++-------- 3 files changed, 25 insertions(+), 29 deletions(-) diff --git a/android/capacitor/src/main/assets/native-bridge.js b/android/capacitor/src/main/assets/native-bridge.js index 9be99af24e..8c18f21317 100644 --- a/android/capacitor/src/main/assets/native-bridge.js +++ b/android/capacitor/src/main/assets/native-bridge.js @@ -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), diff --git a/core/native-bridge.ts b/core/native-bridge.ts index 89522d6212..8049030498 100644 --- a/core/native-bridge.ts +++ b/core/native-bridge.ts @@ -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); @@ -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: { diff --git a/ios/Capacitor/Capacitor/assets/native-bridge.js b/ios/Capacitor/Capacitor/assets/native-bridge.js index 9be99af24e..8c18f21317 100644 --- a/ios/Capacitor/Capacitor/assets/native-bridge.js +++ b/ios/Capacitor/Capacitor/assets/native-bridge.js @@ -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),