mirror of
https://github.com/movie-web/movie-web.git
synced 2024-12-27 23:21:51 +01:00
cleanup
This commit is contained in:
parent
f9a70b196b
commit
ad8c6709a1
@ -15,25 +15,16 @@ export type ExtensionHelloResponse = ExtensionBaseResponse<{
|
||||
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 {
|
||||
url: string;
|
||||
method: string;
|
||||
headers?: Record<string, string>;
|
||||
body?: ExtensionMakeRequestBody;
|
||||
body?: string | Record<string, any>;
|
||||
bodyType?: "string" | "FormData" | "URLSearchParams" | "object";
|
||||
}
|
||||
|
||||
export type ExtensionMakeRequestBodyType = ExtensionMakeRequest["bodyType"];
|
||||
|
||||
export type ExtensionMakeRequestResponse<T> = ExtensionBaseResponse<{
|
||||
response: {
|
||||
statusCode: number;
|
||||
|
@ -7,7 +7,7 @@ export function getBodyTypeFromBody(
|
||||
if (body instanceof FormData) return "FormData";
|
||||
if (body instanceof URLSearchParams) return "URLSearchParams";
|
||||
if (typeof body === "object") return "object";
|
||||
return "string";
|
||||
return undefined;
|
||||
}
|
||||
|
||||
export function convertBodyToObject(body: unknown): any {
|
||||
|
@ -69,14 +69,11 @@ function makeFinalHeaders(
|
||||
|
||||
export function makeExtensionFetcher() {
|
||||
const fetcher: Fetcher = async (url, ops) => {
|
||||
const opsWithoutBody = { ...ops, body: undefined };
|
||||
const result = await sendExtensionRequest<any>({
|
||||
url,
|
||||
...opsWithoutBody,
|
||||
...(ops.body && {
|
||||
body: convertBodyToObject(ops.body),
|
||||
bodyType: getBodyTypeFromBody(ops.body),
|
||||
}),
|
||||
...ops,
|
||||
body: convertBodyToObject(ops.body),
|
||||
bodyType: getBodyTypeFromBody(ops.body),
|
||||
});
|
||||
if (!result?.success) throw new Error(`extension error: ${result?.error}`);
|
||||
const res = result.response;
|
||||
|
Loading…
Reference in New Issue
Block a user