mirror of
https://github.com/sussy-code/smov.git
synced 2024-12-29 16:07:40 +01:00
Request messaging
This commit is contained in:
parent
4bdb95ed0f
commit
e1be30dde9
3 changed files with 33 additions and 10 deletions
|
@ -4,6 +4,7 @@ import {
|
|||
} from "@plasmohq/messaging";
|
||||
|
||||
import { isAllowedExtensionVersion } from "@/backend/extension/compatibility";
|
||||
import { ExtensionMakeRequestResponse } from "@/backend/extension/plasmo";
|
||||
|
||||
let activeExtension = false;
|
||||
|
||||
|
@ -32,9 +33,9 @@ function sendMessage<MessageKey extends keyof MessagesMetadata>(
|
|||
});
|
||||
}
|
||||
|
||||
export async function sendExtensionRequest(
|
||||
export async function sendExtensionRequest<T>(
|
||||
ops: Omit<MessagesMetadata["makeRequest"]["req"], "requestDomain">,
|
||||
): Promise<MessagesMetadata["makeRequest"]["res"] | null> {
|
||||
): Promise<ExtensionMakeRequestResponse<T> | null> {
|
||||
return sendMessage("makeRequest", {
|
||||
requestDomain: window.location.origin,
|
||||
...ops,
|
||||
|
|
|
@ -22,11 +22,13 @@ export interface ExtensionMakeRequest extends ExtensionBaseRequest {
|
|||
body?: string | FormData | URLSearchParams | Record<string, any>;
|
||||
}
|
||||
|
||||
export type ExtensionMakeRequestResponse = ExtensionBaseResponse<{
|
||||
status: number;
|
||||
requestHeaders: Record<string, string>;
|
||||
responseHeaders: Record<string, string>;
|
||||
data: string | Record<string, unknown>;
|
||||
export type ExtensionMakeRequestResponse<T> = ExtensionBaseResponse<{
|
||||
response: {
|
||||
statusCode: number;
|
||||
headers: Record<string, string>;
|
||||
finalUrl: string;
|
||||
body: T;
|
||||
};
|
||||
}>;
|
||||
|
||||
export interface ExtensionPrepareStreamRequest extends ExtensionBaseRequest {
|
||||
|
@ -47,7 +49,7 @@ export interface MmMetadata {
|
|||
};
|
||||
makeRequest: {
|
||||
req: ExtensionMakeRequest;
|
||||
res: ExtensionMakeRequestResponse;
|
||||
res: ExtensionMakeRequestResponse<any>;
|
||||
};
|
||||
prepareStream: {
|
||||
req: ExtensionPrepareStreamRequest;
|
||||
|
|
|
@ -53,12 +53,32 @@ export function makeLoadBalancedSimpleProxyFetcher() {
|
|||
return fetcher;
|
||||
}
|
||||
|
||||
function makeFinalHeaders(
|
||||
readHeaders: string[],
|
||||
headers: Record<string, string>,
|
||||
): Headers {
|
||||
const lowercasedHeaders = readHeaders.map((v) => v.toLowerCase());
|
||||
return new Headers(
|
||||
Object.entries(headers).filter((entry) =>
|
||||
lowercasedHeaders.includes(entry[0].toLowerCase()),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
export function makeExtensionFetcher() {
|
||||
const fetcher: Fetcher = async (url, ops) => {
|
||||
return sendExtensionRequest({
|
||||
const result = await sendExtensionRequest<any>({
|
||||
url,
|
||||
...ops,
|
||||
}) as any;
|
||||
});
|
||||
if (!result?.success) throw new Error(`extension error: ${result?.error}`);
|
||||
const res = result.response;
|
||||
return {
|
||||
body: res.body,
|
||||
finalUrl: res.finalUrl,
|
||||
statusCode: res.statusCode,
|
||||
headers: makeFinalHeaders(ops.readHeaders, res.headers),
|
||||
};
|
||||
};
|
||||
return fetcher;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue