Enable TS strict mode and fix all associated errors

This commit is contained in:
William Oldham
2024-03-15 18:49:12 +00:00
parent a884c785f0
commit 8ba0544468
9 changed files with 28 additions and 21 deletions

View File

@@ -14,6 +14,8 @@ type Response = BaseResponse<{
const handler: PlasmoMessaging.MessageHandler<BaseRequest, Response> = async (req, res) => {
try {
if (!req.sender?.tab?.url) throw new Error('No tab URL found in the request.');
const version = getVersion();
res.send({
success: true,
@@ -21,7 +23,7 @@ const handler: PlasmoMessaging.MessageHandler<BaseRequest, Response> = async (re
allowed: await isDomainWhitelisted(req.sender.tab.url),
hasPermission: await hasPermission(),
});
} catch (err) {
} catch (err: any) {
res.send({
success: false,
error: err.message,

View File

@@ -32,7 +32,7 @@ type Response<T> = BaseResponse<{
const mapBodyToFetchBody = (body: Request['body'], bodyType: Request['bodyType']): BodyInit => {
if (bodyType === 'FormData') {
const formData = new FormData();
body.forEach(([key, value]) => {
body.forEach(([key, value]: [any, any]) => {
formData.append(key, value.toString());
});
}
@@ -50,6 +50,9 @@ const mapBodyToFetchBody = (body: Request['body'], bodyType: Request['bodyType']
const handler: PlasmoMessaging.MessageHandler<Request, Response<any>> = async (req, res) => {
try {
if (!req.sender?.tab?.url) throw new Error('No tab URL found in the request.');
if (!req.body) throw new Error('No request body found in the request.');
const url = makeFullUrl(req.body.url, req.body);
await assertDomainWhitelist(req.sender.tab.url);
@@ -87,7 +90,7 @@ const handler: PlasmoMessaging.MessageHandler<Request, Response<any>> = async (r
finalUrl: response.url,
},
});
} catch (err) {
} catch (err: any) {
console.error('failed request', err);
res.send({
success: false,

View File

@@ -11,6 +11,9 @@ type Request = BaseRequest & {
const handler: PlasmoMessaging.MessageHandler<Request, BaseResponse> = async (req, res) => {
try {
if (!req.sender?.tab?.id) throw new Error('No tab ID found in the request.');
if (!req.body) throw new Error('No body found in the request.');
const searchParams = new URLSearchParams();
searchParams.set('redirectUrl', req.body.redirectUrl);
const url = (chrome || browser).runtime.getURL(`/tabs/${req.body.page}.html?${searchParams.toString()}`);
@@ -26,7 +29,7 @@ const handler: PlasmoMessaging.MessageHandler<Request, BaseResponse> = async (re
res.send({
success: true,
});
} catch (err) {
} catch (err: any) {
res.send({
success: false,
error: err.message,

View File

@@ -15,12 +15,15 @@ interface Request extends BaseRequest {
const handler: PlasmoMessaging.MessageHandler<Request, BaseResponse> = async (req, res) => {
try {
if (!req.sender?.tab?.url) throw new Error('No tab URL found in the request.');
if (!req.body) throw new Error('No request body found in the request.');
await assertDomainWhitelist(req.sender.tab.url);
await setDynamicRules(req.body);
res.send({
success: true,
});
} catch (err) {
} catch (err: any) {
res.send({
success: false,
error: err.message,

View File

@@ -15,5 +15,5 @@ export function useDomain(): null | string {
};
}, []);
return makeUrlIntoDomain(domain);
return domain ? makeUrlIntoDomain(domain) : null;
}

View File

@@ -8,12 +8,12 @@ export function useDomainWhitelist() {
const removeDomain = useCallback((domain: string | null) => {
if (!domain) return;
setDomainWhitelist((s) => [...s.filter((v) => v !== domain)]);
setDomainWhitelist((s) => [...(s ?? []).filter((v) => v !== domain)]);
}, []);
const addDomain = useCallback((domain: string | null) => {
if (!domain) return;
setDomainWhitelist((s) => [...s.filter((v) => v !== domain), domain]);
setDomainWhitelist((s) => [...(s ?? []).filter((v) => v !== domain), domain]);
}, []);
return {

View File

@@ -123,5 +123,6 @@ export const removeDynamicRules = async (ruleIds: number[]) => {
await (chrome || browser).declarativeNetRequest.updateDynamicRules({
removeRuleIds: ruleIds,
});
if ((chrome || browser).runtime.lastError?.message) throw new Error((chrome || browser).runtime.lastError.message);
if ((chrome || browser).runtime.lastError?.message)
throw new Error((chrome || browser).runtime.lastError?.message ?? 'Unknown error');
};

View File

@@ -1,7 +1,7 @@
import { isChrome } from './extension';
export function queryCurrentDomain(cb: (domain: string | null) => void) {
const handle = (tabUrl: string | null) => {
const handle = (tabUrl: string | undefined) => {
if (!tabUrl) cb(null);
else cb(tabUrl);
};