mirror of
https://github.com/movie-web/extension.git
synced 2025-09-13 11:13:24 +00:00
improve typings
This commit is contained in:
@@ -1,9 +1,14 @@
|
|||||||
import type { PlasmoMessaging } from '@plasmohq/messaging';
|
import type { PlasmoMessaging } from '@plasmohq/messaging';
|
||||||
|
|
||||||
import type { BaseRequestBody } from '~types/request';
|
import type { BaseRequest } from '~types/request';
|
||||||
|
import type { BaseResponse } from '~types/response';
|
||||||
import { validateDomainWhiteList } from '~utils/storage';
|
import { validateDomainWhiteList } from '~utils/storage';
|
||||||
|
|
||||||
const handler: PlasmoMessaging.MessageHandler<BaseRequestBody> = async (req, res) => {
|
type Response = BaseResponse<{
|
||||||
|
version: string;
|
||||||
|
}>;
|
||||||
|
|
||||||
|
const handler: PlasmoMessaging.MessageHandler<BaseRequest, Response> = async (req, res) => {
|
||||||
try {
|
try {
|
||||||
await validateDomainWhiteList(req.body.requestDomain);
|
await validateDomainWhiteList(req.body.requestDomain);
|
||||||
|
|
||||||
|
@@ -1,16 +1,24 @@
|
|||||||
import type { PlasmoMessaging } from '@plasmohq/messaging';
|
import type { PlasmoMessaging } from '@plasmohq/messaging';
|
||||||
|
|
||||||
import type { BaseRequestBody } from '~types/request';
|
import type { BaseRequest } from '~types/request';
|
||||||
|
import type { BaseResponse } from '~types/response';
|
||||||
import { validateDomainWhiteList } from '~utils/storage';
|
import { validateDomainWhiteList } from '~utils/storage';
|
||||||
|
|
||||||
interface RequestBody extends BaseRequestBody {
|
interface Request extends BaseRequest {
|
||||||
url: string;
|
url: string;
|
||||||
method: string;
|
method: string;
|
||||||
headers?: Record<string, string>;
|
headers?: Record<string, string>;
|
||||||
body?: string | FormData | URLSearchParams;
|
body?: string | FormData | URLSearchParams;
|
||||||
}
|
}
|
||||||
|
|
||||||
const handler: PlasmoMessaging.MessageHandler<RequestBody> = async (req, res) => {
|
type Response = BaseResponse<{
|
||||||
|
status: number;
|
||||||
|
requestHeaders: Record<string, string>;
|
||||||
|
responseHeaders: Record<string, string>;
|
||||||
|
data: string | Record<string, unknown>;
|
||||||
|
}>;
|
||||||
|
|
||||||
|
const handler: PlasmoMessaging.MessageHandler<Request, Response> = async (req, res) => {
|
||||||
try {
|
try {
|
||||||
await validateDomainWhiteList(req.body.requestDomain);
|
await validateDomainWhiteList(req.body.requestDomain);
|
||||||
|
|
||||||
|
@@ -1,10 +1,11 @@
|
|||||||
import type { PlasmoMessaging } from '@plasmohq/messaging';
|
import type { PlasmoMessaging } from '@plasmohq/messaging';
|
||||||
|
|
||||||
|
import type { BaseRequest } from '~types/request';
|
||||||
|
import type { BaseResponse } from '~types/response';
|
||||||
import { validateDomainWhiteList } from '~utils/storage';
|
import { validateDomainWhiteList } from '~utils/storage';
|
||||||
|
|
||||||
interface RequestBody {
|
interface Request extends BaseRequest {
|
||||||
ruleId: number;
|
ruleId: number;
|
||||||
hostDomain: string;
|
|
||||||
targetDomains: [string, ...string[]];
|
targetDomains: [string, ...string[]];
|
||||||
requestHeaders?: Record<string, string>;
|
requestHeaders?: Record<string, string>;
|
||||||
responseHeaders?: Record<string, string>;
|
responseHeaders?: Record<string, string>;
|
||||||
@@ -20,9 +21,9 @@ const mapHeadersToDeclarativeNetRequestHeaders = (
|
|||||||
}));
|
}));
|
||||||
};
|
};
|
||||||
|
|
||||||
const handler: PlasmoMessaging.MessageHandler<RequestBody> = async (req, res) => {
|
const handler: PlasmoMessaging.MessageHandler<Request, BaseResponse> = async (req, res) => {
|
||||||
try {
|
try {
|
||||||
await validateDomainWhiteList(req.body.hostDomain);
|
await validateDomainWhiteList(req.body.requestDomain);
|
||||||
|
|
||||||
await chrome.declarativeNetRequest.updateDynamicRules({
|
await chrome.declarativeNetRequest.updateDynamicRules({
|
||||||
removeRuleIds: [req.body.ruleId],
|
removeRuleIds: [req.body.ruleId],
|
||||||
|
@@ -1,3 +1,3 @@
|
|||||||
export interface BaseRequestBody {
|
export interface BaseRequest {
|
||||||
requestDomain: string;
|
requestDomain: string;
|
||||||
}
|
}
|
||||||
|
8
src/types/response.ts
Normal file
8
src/types/response.ts
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
export type BaseResponse<T = object> =
|
||||||
|
| ({
|
||||||
|
success: true;
|
||||||
|
} & T)
|
||||||
|
| {
|
||||||
|
success: false;
|
||||||
|
error: string;
|
||||||
|
};
|
Reference in New Issue
Block a user