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 1/5] 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, From 4202bf8ef39f02c521cdd5ac63d1a7fff8fdd11c Mon Sep 17 00:00:00 2001 From: Jorrin <43169049+JorrinKievit@users.noreply.github.com> Date: Thu, 25 Jan 2024 16:38:35 +0100 Subject: [PATCH 2/5] cleanup --- src/backend/extension/plasmo.ts | 2 +- src/backend/extension/request.ts | 22 +++++++++++++++++++ src/backend/providers/fetchers.ts | 35 ++++--------------------------- 3 files changed, 27 insertions(+), 32 deletions(-) create mode 100644 src/backend/extension/request.ts diff --git a/src/backend/extension/plasmo.ts b/src/backend/extension/plasmo.ts index cc07ae24..4a7aaad3 100644 --- a/src/backend/extension/plasmo.ts +++ b/src/backend/extension/plasmo.ts @@ -21,7 +21,7 @@ export type ExtensionMakeRequestBody = value: string; } | { - bodyType: "FormData" | "URLSearchParams" | "Object"; + bodyType: "FormData" | "URLSearchParams" | "object"; value: Record; }; diff --git a/src/backend/extension/request.ts b/src/backend/extension/request.ts new file mode 100644 index 00000000..3e3f9b8b --- /dev/null +++ b/src/backend/extension/request.ts @@ -0,0 +1,22 @@ +import { ExtensionMakeRequestBodyType } from "./plasmo"; + +export function getBodyTypeFromBody( + body: unknown, +): 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"; +} + +export function convertBodyToObject(body: unknown): any { + if (body instanceof FormData || body instanceof URLSearchParams) { + const obj: Record = {}; + for (const [key, value] of body.entries()) { + obj[key] = value; + } + return obj; + } + return body; +} diff --git a/src/backend/providers/fetchers.ts b/src/backend/providers/fetchers.ts index dc9682c3..bcb8f087 100644 --- a/src/backend/providers/fetchers.ts +++ b/src/backend/providers/fetchers.ts @@ -4,7 +4,7 @@ import { sendExtensionRequest } from "@/backend/extension/messaging"; import { getApiToken, setApiToken } from "@/backend/helpers/providerApi"; import { getProviderApiUrls, getProxyUrls } from "@/utils/proxyUrls"; -import { ExtensionMakeRequestBodyType } from "../extension/plasmo"; +import { convertBodyToObject, getBodyTypeFromBody } from "../extension/request"; function makeLoadbalancedList(getter: () => string[]) { let listIndex = -1; @@ -67,47 +67,20 @@ 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({ + const result = await sendExtensionRequest({ url, ...opsWithoutBody, ...(ops.body && { body: { bodyType: getBodyTypeFromBody(ops.body), - value: convertBodyToObject(ops.body) as any, + value: convertBodyToObject(ops.body), }, }), - })) as any; + }); if (!result?.success) throw new Error(`extension error: ${result?.error}`); - console.log(result); const res = result.response; return { body: res.body, From f9a70b196ba575b11c1caa92d9b6dfba09b6f462 Mon Sep 17 00:00:00 2001 From: Jorrin <43169049+JorrinKievit@users.noreply.github.com> Date: Thu, 25 Jan 2024 19:25:56 +0100 Subject: [PATCH 3/5] mr feedback --- src/backend/providers/fetchers.ts | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/backend/providers/fetchers.ts b/src/backend/providers/fetchers.ts index bcb8f087..0c04013d 100644 --- a/src/backend/providers/fetchers.ts +++ b/src/backend/providers/fetchers.ts @@ -74,10 +74,8 @@ export function makeExtensionFetcher() { url, ...opsWithoutBody, ...(ops.body && { - body: { - bodyType: getBodyTypeFromBody(ops.body), - value: convertBodyToObject(ops.body), - }, + body: convertBodyToObject(ops.body), + bodyType: getBodyTypeFromBody(ops.body), }), }); if (!result?.success) throw new Error(`extension error: ${result?.error}`); From ad8c6709a1a0190b5a4a57667b0300d3c3ed5dda Mon Sep 17 00:00:00 2001 From: Jorrin <43169049+JorrinKievit@users.noreply.github.com> Date: Thu, 25 Jan 2024 19:30:28 +0100 Subject: [PATCH 4/5] cleanup --- src/backend/extension/plasmo.ts | 17 ++++------------- src/backend/extension/request.ts | 2 +- src/backend/providers/fetchers.ts | 9 +++------ 3 files changed, 8 insertions(+), 20 deletions(-) diff --git a/src/backend/extension/plasmo.ts b/src/backend/extension/plasmo.ts index 4a7aaad3..14a5481f 100644 --- a/src/backend/extension/plasmo.ts +++ b/src/backend/extension/plasmo.ts @@ -15,25 +15,16 @@ 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?: ExtensionMakeRequestBody; + body?: string | Record; + bodyType?: "string" | "FormData" | "URLSearchParams" | "object"; } +export type ExtensionMakeRequestBodyType = ExtensionMakeRequest["bodyType"]; + export type ExtensionMakeRequestResponse = ExtensionBaseResponse<{ response: { statusCode: number; diff --git a/src/backend/extension/request.ts b/src/backend/extension/request.ts index 3e3f9b8b..96f3b61c 100644 --- a/src/backend/extension/request.ts +++ b/src/backend/extension/request.ts @@ -7,7 +7,7 @@ export function getBodyTypeFromBody( if (body instanceof FormData) return "FormData"; if (body instanceof URLSearchParams) return "URLSearchParams"; if (typeof body === "object") return "object"; - return "string"; + return undefined; } export function convertBodyToObject(body: unknown): any { diff --git a/src/backend/providers/fetchers.ts b/src/backend/providers/fetchers.ts index 0c04013d..5cb6f6ba 100644 --- a/src/backend/providers/fetchers.ts +++ b/src/backend/providers/fetchers.ts @@ -69,14 +69,11 @@ function makeFinalHeaders( export function makeExtensionFetcher() { const fetcher: Fetcher = async (url, ops) => { - const opsWithoutBody = { ...ops, body: undefined }; const result = await sendExtensionRequest({ url, - ...opsWithoutBody, - ...(ops.body && { - body: convertBodyToObject(ops.body), - bodyType: getBodyTypeFromBody(ops.body), - }), + ...ops, + body: convertBodyToObject(ops.body), + bodyType: getBodyTypeFromBody(ops.body), }); if (!result?.success) throw new Error(`extension error: ${result?.error}`); const res = result.response; From b5cd297ad88e4a9f8c1ad97045b1eaebef310e2d Mon Sep 17 00:00:00 2001 From: Jorrin <43169049+JorrinKievit@users.noreply.github.com> Date: Thu, 25 Jan 2024 19:37:15 +0100 Subject: [PATCH 5/5] mr feedback --- src/backend/extension/request.ts | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/src/backend/extension/request.ts b/src/backend/extension/request.ts index 96f3b61c..5885225e 100644 --- a/src/backend/extension/request.ts +++ b/src/backend/extension/request.ts @@ -6,17 +6,12 @@ export function getBodyTypeFromBody( 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 undefined; + return "object"; } export function convertBodyToObject(body: unknown): any { if (body instanceof FormData || body instanceof URLSearchParams) { - const obj: Record = {}; - for (const [key, value] of body.entries()) { - obj[key] = value; - } - return obj; + return [...body]; } return body; }