-
Notifications
You must be signed in to change notification settings - Fork 762
/
Copy pathrule-provider.ts
74 lines (64 loc) · 1.75 KB
/
rule-provider.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import { getURLAndInit } from 'src/misc/request-helper';
import { ClashAPIConfig } from 'src/types';
import { query, QueryCtx } from './fetch';
export type RuleProvider = RuleProviderAPIItem & { idx: number };
export type RuleProviderAPIItem = {
behavior: string;
name: string;
ruleCount: number;
type: 'Rule';
// example value "2020-06-30T16:23:01.44143802+08:00"
updatedAt: string;
vehicleType: 'HTTP' | 'File';
};
type RuleProviderAPIData = {
providers: Record<string, RuleProviderAPIItem>;
};
function normalizeAPIResponse(data: RuleProviderAPIData) {
const providers = data.providers;
const names = Object.keys(providers);
const byName: Record<string, RuleProvider> = {};
// attach an idx to each item
for (let i = 0; i < names.length; i++) {
const name = names[i];
byName[name] = { ...providers[name], idx: i };
}
return { byName, names };
}
export async function fetchRuleProviders(ctx: QueryCtx) {
const data = (await query(ctx)) || { providers: {} };
return normalizeAPIResponse(data);
}
export async function refreshRuleProviderByName({
name,
apiConfig,
}: {
name: string;
apiConfig: ClashAPIConfig;
}) {
const { url, init } = getURLAndInit(apiConfig);
try {
const res = await fetch(url + `/providers/rules/${name}`, {
method: 'PUT',
...init,
});
return res.ok;
} catch (err) {
// log and ignore
// eslint-disable-next-line no-console
console.log('failed to PUT /providers/rules/:name', err);
return false;
}
}
export async function updateRuleProviders({
names,
apiConfig,
}: {
names: string[];
apiConfig: ClashAPIConfig;
}) {
for (let i = 0; i < names.length; i++) {
// run in sequence
await refreshRuleProviderByName({ name: names[i], apiConfig });
}
}