1
0
Fork 0
mirror of https://github.com/sussy-code/smov.git synced 2024-12-20 14:37:43 +01:00

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 {