This commit is contained in:
Jorrin 2024-01-25 19:30:28 +01:00
parent f9a70b196b
commit ad8c6709a1
3 changed files with 8 additions and 20 deletions

View File

@ -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;

View File

@ -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 {

View File

@ -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 && {
...ops,
body: convertBodyToObject(ops.body),
bodyType: getBodyTypeFromBody(ops.body),
}),
});
if (!result?.success) throw new Error(`extension error: ${result?.error}`);
const res = result.response;