mirror of
https://github.com/movie-web/movie-web.git
synced 2024-12-28 14:31:50 +01:00
cleanup
This commit is contained in:
parent
bcd77093dd
commit
4202bf8ef3
@ -21,7 +21,7 @@ export type ExtensionMakeRequestBody =
|
|||||||
value: string;
|
value: string;
|
||||||
}
|
}
|
||||||
| {
|
| {
|
||||||
bodyType: "FormData" | "URLSearchParams" | "Object";
|
bodyType: "FormData" | "URLSearchParams" | "object";
|
||||||
value: Record<string, any>;
|
value: Record<string, any>;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
22
src/backend/extension/request.ts
Normal file
22
src/backend/extension/request.ts
Normal file
@ -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<string, any> = {};
|
||||||
|
for (const [key, value] of body.entries()) {
|
||||||
|
obj[key] = value;
|
||||||
|
}
|
||||||
|
return obj;
|
||||||
|
}
|
||||||
|
return body;
|
||||||
|
}
|
@ -4,7 +4,7 @@ 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 { ExtensionMakeRequestBodyType } from "../extension/plasmo";
|
import { convertBodyToObject, getBodyTypeFromBody } from "../extension/request";
|
||||||
|
|
||||||
function makeLoadbalancedList(getter: () => string[]) {
|
function makeLoadbalancedList(getter: () => string[]) {
|
||||||
let listIndex = -1;
|
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<string, any> {
|
|
||||||
if (body instanceof FormData) {
|
|
||||||
const obj: Record<string, any> = {};
|
|
||||||
for (const [key, value] of body.entries()) {
|
|
||||||
obj[key] = value;
|
|
||||||
}
|
|
||||||
return obj;
|
|
||||||
}
|
|
||||||
if (body instanceof URLSearchParams) {
|
|
||||||
const obj: Record<string, any> = {};
|
|
||||||
for (const [key, value] of body.entries()) {
|
|
||||||
obj[key] = value;
|
|
||||||
}
|
|
||||||
return obj;
|
|
||||||
}
|
|
||||||
return body;
|
|
||||||
}
|
|
||||||
|
|
||||||
export function makeExtensionFetcher() {
|
export function makeExtensionFetcher() {
|
||||||
const fetcher: Fetcher = async (url, ops) => {
|
const fetcher: Fetcher = async (url, ops) => {
|
||||||
const opsWithoutBody = { ...ops, body: undefined };
|
const opsWithoutBody = { ...ops, body: undefined };
|
||||||
const result = (await sendExtensionRequest<any>({
|
const result = await sendExtensionRequest<any>({
|
||||||
url,
|
url,
|
||||||
...opsWithoutBody,
|
...opsWithoutBody,
|
||||||
...(ops.body && {
|
...(ops.body && {
|
||||||
body: {
|
body: {
|
||||||
bodyType: getBodyTypeFromBody(ops.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}`);
|
if (!result?.success) throw new Error(`extension error: ${result?.error}`);
|
||||||
console.log(result);
|
|
||||||
const res = result.response;
|
const res = result.response;
|
||||||
return {
|
return {
|
||||||
body: res.body,
|
body: res.body,
|
||||||
|
Loading…
Reference in New Issue
Block a user