mirror of
https://github.com/movie-web/movie-web.git
synced 2024-12-29 11:21:50 +01:00
serialize body
This commit is contained in:
parent
da841c65b3
commit
bcd77093dd
@ -15,11 +15,23 @@ export type ExtensionHelloResponse = ExtensionBaseResponse<{
|
|||||||
hasPermission: boolean;
|
hasPermission: boolean;
|
||||||
}>;
|
}>;
|
||||||
|
|
||||||
|
export type ExtensionMakeRequestBody =
|
||||||
|
| {
|
||||||
|
bodyType: "string";
|
||||||
|
value: string;
|
||||||
|
}
|
||||||
|
| {
|
||||||
|
bodyType: "FormData" | "URLSearchParams" | "Object";
|
||||||
|
value: Record<string, any>;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type ExtensionMakeRequestBodyType = ExtensionMakeRequestBody["bodyType"];
|
||||||
|
|
||||||
export interface ExtensionMakeRequest extends ExtensionBaseRequest {
|
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?: ExtensionMakeRequestBody;
|
||||||
}
|
}
|
||||||
|
|
||||||
export type ExtensionMakeRequestResponse<T> = ExtensionBaseResponse<{
|
export type ExtensionMakeRequestResponse<T> = ExtensionBaseResponse<{
|
||||||
|
@ -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 { ExtensionMakeRequestBodyType } from "../extension/plasmo";
|
||||||
|
|
||||||
function makeLoadbalancedList(getter: () => string[]) {
|
function makeLoadbalancedList(getter: () => string[]) {
|
||||||
let listIndex = -1;
|
let listIndex = -1;
|
||||||
return () => {
|
return () => {
|
||||||
@ -65,13 +67,47 @@ 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 result = (await sendExtensionRequest<any>({
|
const result = (await sendExtensionRequest<any>({
|
||||||
url,
|
url,
|
||||||
...ops,
|
...opsWithoutBody,
|
||||||
|
...(ops.body && {
|
||||||
|
body: {
|
||||||
|
bodyType: getBodyTypeFromBody(ops.body),
|
||||||
|
value: convertBodyToObject(ops.body) as any,
|
||||||
|
},
|
||||||
|
}),
|
||||||
})) as any;
|
})) 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