Merge pull request #827 from movie-web/feature/#820

Add FormData, URLSearchParams and User-Agent support for extension
This commit is contained in:
mrjvs 2024-01-25 20:39:04 +01:00 committed by GitHub
commit e071515d09
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 27 additions and 3 deletions

View File

@ -19,9 +19,12 @@ export interface ExtensionMakeRequest extends ExtensionBaseRequest {
url: string;
method: string;
headers?: Record<string, string>;
body?: string | FormData | URLSearchParams | Record<string, any>;
body?: string | Record<string, any>;
bodyType?: "string" | "FormData" | "URLSearchParams" | "object";
}
export type ExtensionMakeRequestBodyType = ExtensionMakeRequest["bodyType"];
export type ExtensionMakeRequestResponse<T> = ExtensionBaseResponse<{
response: {
statusCode: number;

View File

@ -0,0 +1,17 @@
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";
return "object";
}
export function convertBodyToObject(body: unknown): any {
if (body instanceof FormData || body instanceof URLSearchParams) {
return [...body];
}
return body;
}

View File

@ -4,6 +4,8 @@ import { sendExtensionRequest } from "@/backend/extension/messaging";
import { getApiToken, setApiToken } from "@/backend/helpers/providerApi";
import { getProviderApiUrls, getProxyUrls } from "@/utils/proxyUrls";
import { convertBodyToObject, getBodyTypeFromBody } from "../extension/request";
function makeLoadbalancedList(getter: () => string[]) {
let listIndex = -1;
return () => {
@ -67,10 +69,12 @@ function makeFinalHeaders(
export function makeExtensionFetcher() {
const fetcher: Fetcher = async (url, ops) => {
const result = (await sendExtensionRequest<any>({
const result = await sendExtensionRequest<any>({
url,
...ops,
})) as any;
body: convertBodyToObject(ops.body),
bodyType: getBodyTypeFromBody(ops.body),
});
if (!result?.success) throw new Error(`extension error: ${result?.error}`);
const res = result.response;
return {