mirror of
https://github.com/sussy-code/smov.git
synced 2025-01-01 16:37:39 +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";
|
} from "@plasmohq/messaging";
|
||||||
|
|
||||||
import { isAllowedExtensionVersion } from "@/backend/extension/compatibility";
|
import { isAllowedExtensionVersion } from "@/backend/extension/compatibility";
|
||||||
|
import { ExtensionMakeRequestResponse } from "@/backend/extension/plasmo";
|
||||||
|
|
||||||
let activeExtension = false;
|
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">,
|
ops: Omit<MessagesMetadata["makeRequest"]["req"], "requestDomain">,
|
||||||
): Promise<MessagesMetadata["makeRequest"]["res"] | null> {
|
): Promise<ExtensionMakeRequestResponse<T> | null> {
|
||||||
return sendMessage("makeRequest", {
|
return sendMessage("makeRequest", {
|
||||||
requestDomain: window.location.origin,
|
requestDomain: window.location.origin,
|
||||||
...ops,
|
...ops,
|
||||||
|
|
|
@ -22,11 +22,13 @@ export interface ExtensionMakeRequest extends ExtensionBaseRequest {
|
||||||
body?: string | FormData | URLSearchParams | Record<string, any>;
|
body?: string | FormData | URLSearchParams | Record<string, any>;
|
||||||
}
|
}
|
||||||
|
|
||||||
export type ExtensionMakeRequestResponse = ExtensionBaseResponse<{
|
export type ExtensionMakeRequestResponse<T> = ExtensionBaseResponse<{
|
||||||
status: number;
|
response: {
|
||||||
requestHeaders: Record<string, string>;
|
statusCode: number;
|
||||||
responseHeaders: Record<string, string>;
|
headers: Record<string, string>;
|
||||||
data: string | Record<string, unknown>;
|
finalUrl: string;
|
||||||
|
body: T;
|
||||||
|
};
|
||||||
}>;
|
}>;
|
||||||
|
|
||||||
export interface ExtensionPrepareStreamRequest extends ExtensionBaseRequest {
|
export interface ExtensionPrepareStreamRequest extends ExtensionBaseRequest {
|
||||||
|
@ -47,7 +49,7 @@ export interface MmMetadata {
|
||||||
};
|
};
|
||||||
makeRequest: {
|
makeRequest: {
|
||||||
req: ExtensionMakeRequest;
|
req: ExtensionMakeRequest;
|
||||||
res: ExtensionMakeRequestResponse;
|
res: ExtensionMakeRequestResponse<any>;
|
||||||
};
|
};
|
||||||
prepareStream: {
|
prepareStream: {
|
||||||
req: ExtensionPrepareStreamRequest;
|
req: ExtensionPrepareStreamRequest;
|
||||||
|
|
|
@ -53,12 +53,32 @@ export function makeLoadBalancedSimpleProxyFetcher() {
|
||||||
return fetcher;
|
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() {
|
export function makeExtensionFetcher() {
|
||||||
const fetcher: Fetcher = async (url, ops) => {
|
const fetcher: Fetcher = async (url, ops) => {
|
||||||
return sendExtensionRequest({
|
const result = await sendExtensionRequest<any>({
|
||||||
url,
|
url,
|
||||||
...ops,
|
...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;
|
return fetcher;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue