summaryrefslogtreecommitdiff
path: root/src/api
diff options
context:
space:
mode:
authorLarvan2 <[email protected]>2026-07-05 17:36:35 +0800
committerLarvan2 <[email protected]>2026-07-05 17:36:35 +0800
commit30dfb51a61a6d6083640d411da8c63cb076318ab (patch)
tree457f95f9dafc6cf1196d35fdec5eb0d677667b23 /src/api
parenta7a842ee25a5b7fc5aa0cd20e9a3086d747332bc (diff)
feat(rules): support enabling/disabling rules and show hit/entry countsHEADmaster
Wire up the mihomo rules API's extra fields (disabled, hitCount, hitAt) and the /rules/disable endpoint so each rule can be toggled on/off and shows its hit count. Also surface the rule provider's ruleCount for RuleSet-type rules, since their own size field is always -1.
Diffstat (limited to 'src/api')
-rw-r--r--src/api/rules.ts43
1 files changed, 38 insertions, 5 deletions
diff --git a/src/api/rules.ts b/src/api/rules.ts
index 5537c31..4e65e9d 100644
--- a/src/api/rules.ts
+++ b/src/api/rules.ts
@@ -5,13 +5,23 @@ import { ClashAPIConfig } from '~/types';
// const endpoint = '/rules';
-type RuleItem = RuleAPIItem & { id: number };
+export type RuleExtra = {
+ disabled: boolean;
+ hitCount: number;
+ hitAt: string;
+ missCount: number;
+ missAt: string;
+};
+
+export type RuleItem = RuleAPIItem & { id: number };
-type RuleAPIItem = {
+export type RuleAPIItem = {
+ index?: number;
type: string;
payload: string;
proxy: string;
size: number;
+ extra?: RuleExtra;
};
function normalizeAPIResponse(json: { rules: Array<RuleAPIItem> }): Array<RuleItem> {
@@ -20,8 +30,11 @@ function normalizeAPIResponse(json: { rules: Array<RuleAPIItem> }): Array<RuleIt
'there is no valid rules list in the rules API response'
);
- // attach an id
- return json.rules.map((r: RuleAPIItem, i: number) => ({ ...r, id: i }));
+ // attach an id, preferring the backend-provided index over array position
+ return json.rules.map((r: RuleAPIItem, i: number) => ({
+ ...r,
+ id: typeof r.index === 'number' ? r.index : i,
+ }));
}
export async function fetchRules(endpoint: string, apiConfig: ClashAPIConfig) {
@@ -34,8 +47,28 @@ export async function fetchRules(endpoint: string, apiConfig: ClashAPIConfig) {
}
} catch (err) {
// log and ignore
-
+
console.log('failed to fetch rules', err);
}
return normalizeAPIResponse(json);
}
+
+export async function updateRuleDisabledStatus(
+ apiConfig: ClashAPIConfig,
+ updates: Record<number, boolean>
+) {
+ const { url, init } = getURLAndInit(apiConfig);
+ try {
+ const res = await fetch(url + '/rules/disable', {
+ method: 'PATCH',
+ ...init,
+ body: JSON.stringify(updates),
+ });
+ return res.ok;
+ } catch (err) {
+ // log and ignore
+
+ console.log('failed to PATCH /rules/disable', err);
+ return false;
+ }
+}