mirror of
https://github.com/movie-web/movie-web.git
synced 2024-11-11 01:55:05 +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
@ -19,9 +19,12 @@ export interface ExtensionMakeRequest extends ExtensionBaseRequest {
|
|||||||
url: string;
|
url: string;
|
||||||
method: string;
|
method: string;
|
||||||
headers?: Record<string, 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<{
|
export type ExtensionMakeRequestResponse<T> = ExtensionBaseResponse<{
|
||||||
response: {
|
response: {
|
||||||
statusCode: number;
|
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 { getApiToken, setApiToken } from "@/backend/helpers/providerApi";
|
||||||
import { getProviderApiUrls, getProxyUrls } from "@/utils/proxyUrls";
|
import { getProviderApiUrls, getProxyUrls } from "@/utils/proxyUrls";
|
||||||
|
|
||||||
|
import { convertBodyToObject, getBodyTypeFromBody } from "../extension/request";
|
||||||
|
|
||||||
function makeLoadbalancedList(getter: () => string[]) {
|
function makeLoadbalancedList(getter: () => string[]) {
|
||||||
let listIndex = -1;
|
let listIndex = -1;
|
||||||
return () => {
|
return () => {
|
||||||
@ -67,10 +69,12 @@ function makeFinalHeaders(
|
|||||||
|
|
||||||
export function makeExtensionFetcher() {
|
export function makeExtensionFetcher() {
|
||||||
const fetcher: Fetcher = async (url, ops) => {
|
const fetcher: Fetcher = async (url, ops) => {
|
||||||
const result = (await sendExtensionRequest<any>({
|
const result = await sendExtensionRequest<any>({
|
||||||
url,
|
url,
|
||||||
...ops,
|
...ops,
|
||||||
})) as any;
|
body: convertBodyToObject(ops.body),
|
||||||
|
bodyType: getBodyTypeFromBody(ops.body),
|
||||||
|
});
|
||||||
if (!result?.success) throw new Error(`extension error: ${result?.error}`);
|
if (!result?.success) throw new Error(`extension error: ${result?.error}`);
|
||||||
const res = result.response;
|
const res = result.response;
|
||||||
return {
|
return {
|
||||||
|
Loading…
Reference in New Issue
Block a user