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:
commit
e071515d09
3 changed files with 27 additions and 3 deletions
|
@ -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;
|
||||
|
|
17
src/backend/extension/request.ts
Normal file
17
src/backend/extension/request.ts
Normal 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;
|
||||
}
|
|
@ -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 {
|
||||
|
|
Loading…
Reference in a new issue