From bcd77093dd018b703ccae412ca03ac256d068ddd Mon Sep 17 00:00:00 2001 From: Jorrin <43169049+JorrinKievit@users.noreply.github.com> Date: Wed, 24 Jan 2024 19:40:48 +0100 Subject: [PATCH] serialize body --- src/backend/extension/plasmo.ts | 14 +++++++++++- src/backend/providers/fetchers.ts | 38 ++++++++++++++++++++++++++++++- 2 files changed, 50 insertions(+), 2 deletions(-) diff --git a/src/backend/extension/plasmo.ts b/src/backend/extension/plasmo.ts index c13898be..cc07ae24 100644 --- a/src/backend/extension/plasmo.ts +++ b/src/backend/extension/plasmo.ts @@ -15,11 +15,23 @@ export type ExtensionHelloResponse = ExtensionBaseResponse<{ hasPermission: boolean; }>; +export type ExtensionMakeRequestBody = + | { + bodyType: "string"; + value: string; + } + | { + bodyType: "FormData" | "URLSearchParams" | "Object"; + value: Record; + }; + +export type ExtensionMakeRequestBodyType = ExtensionMakeRequestBody["bodyType"]; + export interface ExtensionMakeRequest extends ExtensionBaseRequest { url: string; method: string; headers?: Record; - body?: string | FormData | URLSearchParams | Record; + body?: ExtensionMakeRequestBody; } export type ExtensionMakeRequestResponse = ExtensionBaseResponse<{ diff --git a/src/backend/providers/fetchers.ts b/src/backend/providers/fetchers.ts index 95267595..dc9682c3 100644 --- a/src/backend/providers/fetchers.ts +++ b/src/backend/providers/fetchers.ts @@ -4,6 +4,8 @@ import { sendExtensionRequest } from "@/backend/extension/messaging"; import { getApiToken, setApiToken } from "@/backend/helpers/providerApi"; import { getProviderApiUrls, getProxyUrls } from "@/utils/proxyUrls"; +import { ExtensionMakeRequestBodyType } from "../extension/plasmo"; + function makeLoadbalancedList(getter: () => string[]) { let listIndex = -1; return () => { @@ -65,13 +67,47 @@ function makeFinalHeaders( ); } +function getBodyTypeFromBody(body: any): ExtensionMakeRequestBodyType { + if (typeof body === "string") return "string"; + if (body instanceof FormData) return "FormData"; + if (body instanceof URLSearchParams) return "URLSearchParams"; + if (typeof body === "object") return "Object"; + return "string"; +} + +function convertBodyToObject(body: any): Record { + if (body instanceof FormData) { + const obj: Record = {}; + for (const [key, value] of body.entries()) { + obj[key] = value; + } + return obj; + } + if (body instanceof URLSearchParams) { + const obj: Record = {}; + for (const [key, value] of body.entries()) { + obj[key] = value; + } + return obj; + } + return body; +} + export function makeExtensionFetcher() { const fetcher: Fetcher = async (url, ops) => { + const opsWithoutBody = { ...ops, body: undefined }; const result = (await sendExtensionRequest({ url, - ...ops, + ...opsWithoutBody, + ...(ops.body && { + body: { + bodyType: getBodyTypeFromBody(ops.body), + value: convertBodyToObject(ops.body) as any, + }, + }), })) as any; if (!result?.success) throw new Error(`extension error: ${result?.error}`); + console.log(result); const res = result.response; return { body: res.body,