Skip to content

Commit 1865b19

Browse files
authored
🤖 Merge PR DefinitelyTyped#71719 [Chrome] add testMatchOutcome in declarativeNetRequest (Chrome 103 and 129) by @erwanjugand
1 parent 1f2ee9e commit 1865b19

File tree

2 files changed

+162
-55
lines changed

2 files changed

+162
-55
lines changed

‎types/chrome/index.d.ts‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13873,6 +13873,31 @@ declare namespace chrome {
1387313873
rulesMatchedInfo: MatchedRuleInfo[];
1387413874
}
1387513875

13876+
/** @since Chrome 103 */
13877+
export interface TestMatchOutcomeResult {
13878+
/** The rules (if any) that match the hypothetical request. */
13879+
matchedRules: MatchedRule[];
13880+
}
13881+
13882+
/** @since Chrome 103 */
13883+
export interface TestMatchRequestDetails {
13884+
/** The initiator URL (if any) for the hypothetical request. */
13885+
initiator?: string;
13886+
/** Standard HTTP method of the hypothetical request. Defaults to "get" for HTTP requests and is ignored for non-HTTP requests. */
13887+
method?: `${RequestMethod}`;
13888+
/**
13889+
* The headers provided by a hypothetical response if the request does not get blocked or redirected before it is sent. Represented as an object which maps a header name to a list of string values. If not specified, the hypothetical response would return empty response headers, which can match rules which match on the non-existence of headers. E.g. `{"content-type": ["text/html; charset=utf-8", "multipart/form-data"]}`
13890+
* @since Chrome 129
13891+
*/
13892+
responseHeaders?: { [name: string]: unknown };
13893+
/** The ID of the tab in which the hypothetical request takes place. Does not need to correspond to a real tab ID. Default is -1, meaning that the request isn't related to a tab. */
13894+
tabId?: number;
13895+
/** The resource type of the hypothetical request. */
13896+
type: `${ResourceType}`;
13897+
/** The URL of the hypothetical request. */
13898+
url: string;
13899+
}
13900+
1387613901
/** Returns the number of static rules an extension can enable before the global static rule limit is reached. */
1387713902
export function getAvailableStaticRuleCount(callback: (count: number) => void): void;
1387813903

@@ -13976,6 +14001,17 @@ declare namespace chrome {
1397614001
*/
1397714002
export function setExtensionActionOptions(options: ExtensionActionOptions): Promise<void>;
1397814003

14004+
/**
14005+
* Checks if any of the extension's declarativeNetRequest rules would match a hypothetical request. Note: Only available for unpacked extensions as this is only intended to be used during extension development.
14006+
* @param request
14007+
* @since Chrome 103
14008+
*/
14009+
export function testMatchOutcome(request: TestMatchRequestDetails): Promise<TestMatchOutcomeResult>;
14010+
export function testMatchOutcome(
14011+
request: TestMatchRequestDetails,
14012+
callback: (result: TestMatchOutcomeResult) => void,
14013+
): void;
14014+
1397914015
/** Modifies the current set of dynamic rules for the extension.
1398014016
* The rules with IDs listed in options.removeRuleIds are first removed, and then the rules given in options.addRules are added.
1398114017
*

‎types/chrome/test/index.ts‎

Lines changed: 126 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -2323,72 +2323,143 @@ async function testDeclarativeNetRequest() {
23232323
chrome.declarativeNetRequest.UnsupportedRegexReason.MEMORY_LIMIT_EXCEEDED === "memoryLimitExceeded";
23242324
chrome.declarativeNetRequest.UnsupportedRegexReason.SYNTAX_ERROR === "syntaxError";
23252325

2326-
await chrome.declarativeNetRequest.getAvailableStaticRuleCount();
2327-
chrome.declarativeNetRequest.getAvailableStaticRuleCount(count => {
2328-
// $ExpectType number
2329-
count;
2326+
chrome.declarativeNetRequest.getAvailableStaticRuleCount(); // $ExpectType Promise<number>
2327+
chrome.declarativeNetRequest.getAvailableStaticRuleCount((count) => { // $ExpectType void
2328+
count; // $ExpectType number
23302329
});
2331-
await chrome.declarativeNetRequest.getDynamicRules();
2332-
chrome.declarativeNetRequest.getDynamicRules(rules => {
2333-
// $ExpectType Rule[]
2334-
rules;
2330+
// @ts-expect-error
2331+
chrome.declarativeNetRequest.getAvailableStaticRuleCount(() => {}).then(() => {});
23352332

2336-
const rule = rules[0];
2333+
chrome.declarativeNetRequest.getDynamicRules(); // $ExpectType Promise<Rule[]>
2334+
chrome.declarativeNetRequest.getDynamicRules(([rule]) => { // $ExpectType void
23372335
rule.action; // $ExpectType RuleAction
23382336
rule.condition; // $ExpectType RuleCondition
23392337
rule.id; // $ExpectType number
23402338
rule.priority; // $ExpectType number | undefined
23412339
rule.condition.excludedResponseHeaders; // $ExpectType HeaderInfo[] | undefined
23422340
rule.condition.responseHeaders; // $ExpectType HeaderInfo[] | undefined
23432341
});
2342+
// @ts-expect-error
2343+
chrome.declarativeNetRequest.getDynamicRules(() => {}).then(() => {});
23442344

2345-
await chrome.declarativeNetRequest.getEnabledRulesets();
2346-
chrome.declarativeNetRequest.getEnabledRulesets(sets => {
2347-
// $ExpectType string[]
2348-
sets;
2349-
});
2350-
await chrome.declarativeNetRequest.getMatchedRules({});
2351-
await chrome.declarativeNetRequest.getMatchedRules();
2352-
await chrome.declarativeNetRequest.getSessionRules();
2353-
await chrome.declarativeNetRequest.isRegexSupported({ regex: "regex1" });
2354-
await chrome.declarativeNetRequest.setExtensionActionOptions({});
2355-
await chrome.declarativeNetRequest.updateDynamicRules({});
2356-
await chrome.declarativeNetRequest.updateEnabledRulesets({});
2357-
await chrome.declarativeNetRequest.updateSessionRules({});
2358-
2359-
await chrome.declarativeNetRequest.updateDynamicRules({});
2360-
await chrome.declarativeNetRequest.updateDynamicRules({
2361-
addRules: [{
2362-
action: {
2363-
type: chrome.declarativeNetRequest.RuleActionType.ALLOW,
2364-
},
2365-
condition: {
2366-
initiatorDomains: ["www.example.com"],
2367-
tabIds: [2, 3, 76],
2368-
},
2369-
id: 2,
2370-
priority: 3,
2371-
}],
2372-
});
2373-
2374-
await chrome.declarativeNetRequest.updateDynamicRules({
2375-
addRules: [{
2376-
action: {
2377-
type: chrome.declarativeNetRequest.RuleActionType.MODIFY_HEADERS,
2378-
requestHeaders: [{
2379-
header: "X-Test-Header",
2380-
operation: chrome.declarativeNetRequest.HeaderOperation.SET,
2381-
value: "test-value",
2382-
}],
2383-
},
2384-
condition: {
2385-
resourceTypes: [chrome.declarativeNetRequest.ResourceType.MAIN_FRAME],
2386-
domains: ["www.example.com"],
2387-
},
2388-
id: 2,
2389-
priority: 3,
2390-
}],
2345+
chrome.declarativeNetRequest.getEnabledRulesets(); // $ExpectType Promise<string[]>
2346+
chrome.declarativeNetRequest.getEnabledRulesets((rulesetIds) => { // $ExpectType void
2347+
rulesetIds; // $ExpectType string[]
2348+
});
2349+
// @ts-expect-error
2350+
chrome.declarativeNetRequest.getEnabledRulesets(() => {}).then(() => {});
2351+
2352+
const matchedRulesFilter: chrome.declarativeNetRequest.MatchedRulesFilter = {
2353+
tabId: 0,
2354+
minTimeStamp: 0,
2355+
};
2356+
2357+
chrome.declarativeNetRequest.getMatchedRules(); // $ExpectType Promise<RulesMatchedDetails>
2358+
chrome.declarativeNetRequest.getMatchedRules(matchedRulesFilter); // $ExpectType Promise<RulesMatchedDetails>
2359+
chrome.declarativeNetRequest.getMatchedRules((details) => { // $ExpectType void
2360+
details; // $ExpectType RulesMatchedDetails
2361+
});
2362+
chrome.declarativeNetRequest.getMatchedRules(matchedRulesFilter, (details) => { // $ExpectType void
2363+
details; // $ExpectType RulesMatchedDetails
2364+
});
2365+
// @ts-expect-error
2366+
chrome.declarativeNetRequest.getMatchedRules(() => {}).then(() => {});
2367+
2368+
chrome.declarativeNetRequest.getSessionRules(); // $ExpectType Promise<Rule[]>
2369+
chrome.declarativeNetRequest.getSessionRules((rules) => { // $ExpectType void
2370+
rules; // $ExpectType Rule[]
2371+
});
2372+
// @ts-expect-error
2373+
chrome.declarativeNetRequest.getSessionRules(() => {}).then(() => {});
2374+
2375+
const regexOptions: chrome.declarativeNetRequest.RegexOptions = {
2376+
regex: "regex1",
2377+
isCaseSensitive: true,
2378+
requireCapturing: true,
2379+
};
2380+
2381+
chrome.declarativeNetRequest.isRegexSupported(regexOptions); // $ExpectType Promise<IsRegexSupportedResult>
2382+
chrome.declarativeNetRequest.isRegexSupported(regexOptions, (result) => { // $ExpectType void
2383+
result; // $ExpectType IsRegexSupportedResult
2384+
});
2385+
// @ts-expect-error
2386+
chrome.declarativeNetRequest.isRegexSupported(() => {}).then(() => {});
2387+
2388+
const extensionActionOptions: chrome.declarativeNetRequest.ExtensionActionOptions = {
2389+
displayActionCountAsBadgeText: true,
2390+
tabUpdate: {
2391+
increment: 1,
2392+
tabId: 1,
2393+
},
2394+
};
2395+
2396+
chrome.declarativeNetRequest.setExtensionActionOptions(extensionActionOptions); // $ExpectType Promise<void>
2397+
chrome.declarativeNetRequest.setExtensionActionOptions(extensionActionOptions, () => {}); // $ExpectType void
2398+
// @ts-expect-error
2399+
chrome.declarativeNetRequest.setExtensionActionOptions(extensionActionOptions, () => {}).then(() => {});
2400+
2401+
const testMatchRequestDetails: chrome.declarativeNetRequest.TestMatchRequestDetails = {
2402+
type: "image",
2403+
url: "https://example.com",
2404+
tabId: 1,
2405+
initiator: "https://example.com",
2406+
method: "get",
2407+
responseHeaders: {},
2408+
};
2409+
2410+
chrome.declarativeNetRequest.testMatchOutcome(testMatchRequestDetails); // $ExpectType Promise<TestMatchOutcomeResult>
2411+
chrome.declarativeNetRequest.testMatchOutcome(testMatchRequestDetails, (result) => { // $ExpectType void
2412+
result.matchedRules; // $ExpectType MatchedRule[]
2413+
});
2414+
// @ts-expect-error
2415+
chrome.declarativeNetRequest.testMatchOutcome(testMatchRequestDetails, () => {}).then(() => {});
2416+
2417+
const updateRuleOptions: chrome.declarativeNetRequest.UpdateRuleOptions = {
2418+
addRules: [],
2419+
removeRuleIds: [1, 2, 3],
2420+
};
2421+
2422+
chrome.declarativeNetRequest.updateDynamicRules(updateRuleOptions); // $ExpectType Promise<void>
2423+
chrome.declarativeNetRequest.updateDynamicRules(updateRuleOptions, () => {}); // $ExpectType void
2424+
// @ts-expect-error
2425+
chrome.declarativeNetRequest.updateDynamicRules(updateRuleOptions, () => {}).then(() => {});
2426+
2427+
const updateRulesetOptions: chrome.declarativeNetRequest.UpdateRulesetOptions = {
2428+
disableRulesetIds: ["ruleset1"],
2429+
enableRulesetIds: ["ruleset2"],
2430+
};
2431+
2432+
chrome.declarativeNetRequest.updateEnabledRulesets(updateRulesetOptions); // $ExpectType Promise<void>
2433+
chrome.declarativeNetRequest.updateEnabledRulesets(updateRulesetOptions, () => {}); // $ExpectType void
2434+
// @ts-expect-error
2435+
chrome.declarativeNetRequest.updateEnabledRulesets(updateRulesetOptions, () => {}).then(() => {});
2436+
2437+
chrome.declarativeNetRequest.updateSessionRules(updateRuleOptions); // $ExpectType Promise<void>
2438+
chrome.declarativeNetRequest.updateSessionRules(updateRuleOptions, () => {}); // $ExpectType void
2439+
// @ts-expect-error
2440+
chrome.declarativeNetRequest.updateSessionRules(updateRuleOptions, () => {}).then(() => {});
2441+
2442+
const updateStaticRulesOptions: chrome.declarativeNetRequest.UpdateStaticRulesOptions = {
2443+
disableRuleIds: [1, 2, 3],
2444+
enableRuleIds: [1, 2, 3],
2445+
rulesetId: "ruleset1",
2446+
};
2447+
2448+
chrome.declarativeNetRequest.updateStaticRules(updateStaticRulesOptions); // $ExpectType Promise<void>
2449+
chrome.declarativeNetRequest.updateStaticRules(updateStaticRulesOptions, () => {}); // $ExpectType void
2450+
// @ts-expect-error
2451+
chrome.declarativeNetRequest.updateStaticRules(updateStaticRulesOptions, () => {}).then(() => {});
2452+
2453+
chrome.declarativeNetRequest.onRuleMatchedDebug.addListener((info) => {
2454+
info; // $ExpectType MatchedRuleInfoDebug
2455+
});
2456+
chrome.declarativeNetRequest.onRuleMatchedDebug.removeListener((info) => {
2457+
info; // $ExpectType MatchedRuleInfoDebug
2458+
});
2459+
chrome.declarativeNetRequest.onRuleMatchedDebug.hasListener((info) => {
2460+
info; // $ExpectType MatchedRuleInfoDebug
23912461
});
2462+
chrome.declarativeNetRequest.onRuleMatchedDebug.hasListeners(); // $ExpectType boolean
23922463
}
23932464

23942465
// https://developer.chrome.com/docs/extensions/reference/storage

0 commit comments

Comments
 (0)