chore: adjust type

This commit is contained in:
GyDi
2022-11-19 17:22:29 +08:00
parent e19fe5ce1c
commit 6eafb15cf9
26 changed files with 274 additions and 283 deletions

View File

@@ -50,11 +50,11 @@ export async function getVersion() {
/// Get current base configs
export async function getClashConfig() {
const instance = await getAxios();
return instance.get("/configs") as Promise<ApiType.ConfigData>;
return instance.get("/configs") as Promise<IConfigData>;
}
/// Update current configs
export async function updateConfigs(config: Partial<ApiType.ConfigData>) {
export async function updateConfigs(config: Partial<IConfigData>) {
const instance = await getAxios();
return instance.patch("/configs", config);
}
@@ -63,7 +63,7 @@ export async function updateConfigs(config: Partial<ApiType.ConfigData>) {
export async function getRules() {
const instance = await getAxios();
const response = await instance.get<any, any>("/rules");
return response?.rules as ApiType.RuleItem[];
return response?.rules as IRuleItem[];
}
/// Get Proxy delay
@@ -90,7 +90,7 @@ export async function updateProxy(group: string, proxy: string) {
async function getProxiesInner() {
const instance = await getAxios();
const response = await instance.get<any, any>("/proxies");
return (response?.proxies || {}) as Record<string, ApiType.ProxyItem>;
return (response?.proxies || {}) as Record<string, IProxyItem>;
}
/// Get the Proxy information
@@ -116,7 +116,7 @@ export async function getProxies() {
const { GLOBAL: global, DIRECT: direct, REJECT: reject } = proxyRecord;
let groups: ApiType.ProxyGroupItem[] = [];
let groups: IProxyGroupItem[] = [];
if (global?.all) {
groups = global.all
@@ -142,7 +142,7 @@ export async function getProxies() {
)
);
const _global: ApiType.ProxyGroupItem = {
const _global: IProxyGroupItem = {
...global,
all: global?.all?.map((item) => generateItem(item)) || [],
};
@@ -155,10 +155,7 @@ export async function getProviders() {
const instance = await getAxios();
const response = await instance.get<any, any>("/providers/proxies");
const providers = (response.providers || {}) as Record<
string,
ApiType.ProviderItem
>;
const providers = (response.providers || {}) as Record<string, IProviderItem>;
return Object.fromEntries(
Object.entries(providers).filter(([key, item]) => {
@@ -179,7 +176,7 @@ export async function providerHealthCheck(name: string) {
export async function getConnections() {
const instance = await getAxios();
const result = await instance.get("/connections");
return result as any as ApiType.Connections;
return result as any as IConnections;
}
// Close specific connection

View File

@@ -21,23 +21,23 @@ export async function getClashLogs() {
}
return null;
})
.filter(Boolean) as ApiType.LogItem[];
.filter(Boolean) as ILogItem[];
}
export async function getProfiles() {
return invoke<CmdType.ProfilesConfig>("get_profiles");
return invoke<IProfilesConfig>("get_profiles");
}
export async function enhanceProfiles() {
return invoke<void>("enhance_profiles");
}
export async function patchProfilesConfig(profiles: CmdType.ProfilesConfig) {
export async function patchProfilesConfig(profiles: IProfilesConfig) {
return invoke<void>("patch_profiles_config", { profiles });
}
export async function createProfile(
item: Partial<CmdType.ProfileItem>,
item: Partial<IProfileItem>,
fileData?: string | null
) {
return invoke<void>("create_profile", { item, fileData });
@@ -62,10 +62,7 @@ export async function importProfile(url: string) {
});
}
export async function updateProfile(
index: string,
option?: CmdType.ProfileOption
) {
export async function updateProfile(index: string, option?: IProfileOption) {
return invoke<void>("update_profile", { index, option });
}
@@ -75,13 +72,13 @@ export async function deleteProfile(index: string) {
export async function patchProfile(
index: string,
profile: Partial<CmdType.ProfileItem>
profile: Partial<IProfileItem>
) {
return invoke<void>("patch_profile", { index, profile });
}
export async function getClashInfo() {
return invoke<CmdType.ClashInfo | null>("get_clash_info");
return invoke<IClashInfo | null>("get_clash_info");
}
export async function getRuntimeConfig() {
@@ -100,15 +97,15 @@ export async function getRuntimeLogs() {
return invoke<Record<string, [string, string][]>>("get_runtime_logs");
}
export async function patchClashConfig(payload: Partial<ApiType.ConfigData>) {
export async function patchClashConfig(payload: Partial<IConfigData>) {
return invoke<void>("patch_clash_config", { payload });
}
export async function getVergeConfig() {
return invoke<CmdType.VergeConfig>("get_verge_config");
return invoke<IVergeConfig>("get_verge_config");
}
export async function patchVergeConfig(payload: CmdType.VergeConfig) {
export async function patchVergeConfig(payload: IVergeConfig) {
return invoke<void>("patch_verge_config", { payload });
}

View File

@@ -56,7 +56,7 @@ class DelayManager {
}
/// 暂时修复provider的节点延迟排序的问题
getDelayFix(proxy: ApiType.ProxyItem, group: string) {
getDelayFix(proxy: IProxyItem, group: string) {
if (!proxy.provider) return this.getDelay(proxy.name, group);
if (proxy.history.length > 0) {

View File

@@ -10,7 +10,7 @@ export const atomClashPort = atom<number>({
default: 0,
});
export const atomLogData = atom<ApiType.LogItem[]>({
export const atomLogData = atom<ILogItem[]>({
key: "atomLogData",
default: [],
});
@@ -50,10 +50,10 @@ export const atomConnectionSetting = atom<IConnectionSetting>({
try {
const value = localStorage.getItem(key);
const data = value == null ? { layout: "list" } : JSON.parse(value);
const data = value == null ? { layout: "table" } : JSON.parse(value);
setSelf(data);
} catch {
setSelf({ layout: "list" });
setSelf({ layout: "table" });
}
onSet((newValue) => {

View File

@@ -1,243 +1,240 @@
/**
* Some interface for clash api
*/
declare namespace ApiType {
interface ConfigData {
port: number;
mode: string;
ipv6: boolean;
"socket-port": number;
"allow-lan": boolean;
"log-level": string;
"mixed-port": number;
"redir-port": number;
"socks-port": number;
"tproxy-port": number;
"external-controller": string;
secret: string;
}
interface IConfigData {
port: number;
mode: string;
ipv6: boolean;
"socket-port": number;
"allow-lan": boolean;
"log-level": string;
"mixed-port": number;
"redir-port": number;
"socks-port": number;
"tproxy-port": number;
"external-controller": string;
secret: string;
}
interface RuleItem {
interface IRuleItem {
type: string;
payload: string;
proxy: string;
}
interface IProxyItem {
name: string;
type: string;
udp: boolean;
history: {
time: string;
delay: number;
}[];
all?: string[];
now?: string;
provider?: string; // 记录是否来自provider
}
type IProxyGroupItem = Omit<IProxyItem, "all"> & {
all: IProxyItem[];
};
interface IProviderItem {
name: string;
type: string;
proxies: IProxyItem[];
updatedAt: string;
vehicleType: string;
}
interface ITrafficItem {
up: number;
down: number;
}
interface ILogItem {
type: string;
time?: string;
payload: string;
}
interface IConnectionsItem {
id: string;
metadata: {
network: string;
type: string;
payload: string;
proxy: string;
}
interface ProxyItem {
name: string;
type: string;
udp: boolean;
history: {
time: string;
delay: number;
}[];
all?: string[];
now?: string;
provider?: string; // 记录是否来自provider
}
type ProxyGroupItem = Omit<ProxyItem, "all"> & {
all: ProxyItem[];
host: string;
sourceIP: string;
sourcePort: string;
destinationPort: string;
destinationIP?: string;
process?: string;
processPath?: string;
};
upload: number;
download: number;
start: string;
chains: string[];
rule: string;
rulePayload: string;
curUpload?: number; // upload speed, calculate at runtime
curDownload?: number; // download speed, calculate at runtime
}
interface ProviderItem {
name: string;
type: string;
proxies: ProxyItem[];
updatedAt: string;
vehicleType: string;
}
interface TrafficItem {
up: number;
down: number;
}
interface LogItem {
type: string;
time?: string;
payload: string;
}
interface ConnectionsItem {
id: string;
metadata: {
network: string;
type: string;
host: string;
sourceIP: string;
sourcePort: string;
destinationPort: string;
destinationIP?: string;
process?: string;
processPath?: string;
};
upload: number;
download: number;
start: string;
chains: string[];
rule: string;
rulePayload: string;
curUpload?: number; // upload speed, calculate at runtime
curDownload?: number; // download speed, calculate at runtime
}
interface Connections {
downloadTotal: number;
uploadTotal: number;
connections: ConnectionsItem[];
}
interface IConnections {
downloadTotal: number;
uploadTotal: number;
connections: IConnectionsItem[];
}
/**
* Some interface for command
*/
declare namespace CmdType {
type ProfileType = "local" | "remote" | "merge" | "script";
interface ClashInfo {
status: string;
port?: string; // clash mixed port
server?: string; // external-controller
secret?: string;
}
type IProfileType = "local" | "remote" | "merge" | "script";
interface ProfileItem {
uid: string;
type?: ProfileType | string;
name?: string;
desc?: string;
file?: string;
url?: string;
updated?: number;
selected?: {
name?: string;
now?: string;
}[];
extra?: {
upload: number;
download: number;
total: number;
expire: number;
};
option?: ProfileOption;
}
interface ProfileOption {
user_agent?: string;
with_proxy?: boolean;
self_proxy?: boolean;
update_interval?: number;
}
interface ProfilesConfig {
current?: string;
chain?: string[];
valid?: string[];
items?: ProfileItem[];
}
interface VergeConfig {
language?: string;
clash_core?: string;
theme_mode?: "light" | "dark" | "system";
theme_blur?: boolean;
traffic_graph?: boolean;
enable_tun_mode?: boolean;
enable_auto_launch?: boolean;
enable_service_mode?: boolean;
enable_silent_start?: boolean;
enable_system_proxy?: boolean;
enable_proxy_guard?: boolean;
proxy_guard_duration?: number;
system_proxy_bypass?: string;
web_ui_list?: string[];
hotkeys?: string[];
theme_setting?: {
primary_color?: string;
secondary_color?: string;
primary_text?: string;
secondary_text?: string;
info_color?: string;
error_color?: string;
warning_color?: string;
success_color?: string;
font_family?: string;
css_injection?: string;
};
auto_close_connection?: boolean;
default_latency_test?: string;
}
type ClashConfigValue = any;
interface ProfileMerge {
// clash config fields (default supports)
rules?: ClashConfigValue;
proxies?: ClashConfigValue;
"proxy-groups"?: ClashConfigValue;
"proxy-providers"?: ClashConfigValue;
"rule-providers"?: ClashConfigValue;
// clash config fields (use flag)
tun?: ClashConfigValue;
dns?: ClashConfigValue;
hosts?: ClashConfigValue;
script?: ClashConfigValue;
profile?: ClashConfigValue;
payload?: ClashConfigValue;
"interface-name"?: ClashConfigValue;
"routing-mark"?: ClashConfigValue;
// functional fields
use?: string[];
"prepend-rules"?: any[];
"append-rules"?: any[];
"prepend-proxies"?: any[];
"append-proxies"?: any[];
"prepend-proxy-groups"?: any[];
"append-proxy-groups"?: any[];
// fix
ebpf?: any;
experimental?: any;
iptables?: any;
sniffer?: any;
authentication?: any;
"bind-address"?: any;
"external-ui"?: any;
"auto-redir"?: any;
"socks-port"?: any;
"redir-port"?: any;
"tproxy-port"?: any;
"geodata-mode"?: any;
"tcp-concurrent"?: any;
}
// partial of the clash config
type ProfileData = Partial<{
rules: any[];
proxies: any[];
"proxy-groups": any[];
"proxy-providers": any[];
"rule-providers": any[];
[k: string]: any;
}>;
interface ChainItem {
item: ProfileItem;
merge?: ProfileMerge;
script?: string;
}
interface EnhancedPayload {
chain: ChainItem[];
valid: string[];
current: ProfileData;
callback: string;
}
interface EnhancedResult {
data: ProfileData;
status: string;
error?: string;
}
interface IClashInfo {
status: string;
port?: string; // clash mixed port
server?: string; // external-controller
secret?: string;
}
interface IProfileItem {
uid: string;
type?: IProfileType | string;
name?: string;
desc?: string;
file?: string;
url?: string;
updated?: number;
selected?: {
name?: string;
now?: string;
}[];
extra?: {
upload: number;
download: number;
total: number;
expire: number;
};
option?: IProfileOption;
}
interface IProfileOption {
user_agent?: string;
with_proxy?: boolean;
self_proxy?: boolean;
update_interval?: number;
}
interface IProfilesConfig {
current?: string;
chain?: string[];
valid?: string[];
items?: IProfileItem[];
}
interface IVergeConfig {
language?: string;
clash_core?: string;
theme_mode?: "light" | "dark" | "system";
theme_blur?: boolean;
traffic_graph?: boolean;
enable_tun_mode?: boolean;
enable_auto_launch?: boolean;
enable_service_mode?: boolean;
enable_silent_start?: boolean;
enable_system_proxy?: boolean;
enable_proxy_guard?: boolean;
proxy_guard_duration?: number;
system_proxy_bypass?: string;
web_ui_list?: string[];
hotkeys?: string[];
theme_setting?: {
primary_color?: string;
secondary_color?: string;
primary_text?: string;
secondary_text?: string;
info_color?: string;
error_color?: string;
warning_color?: string;
success_color?: string;
font_family?: string;
css_injection?: string;
};
auto_close_connection?: boolean;
default_latency_test?: string;
}
type IClashConfigValue = any;
interface IProfileMerge {
// clash config fields (default supports)
rules?: IClashConfigValue;
proxies?: IClashConfigValue;
"proxy-groups"?: IClashConfigValue;
"proxy-providers"?: IClashConfigValue;
"rule-providers"?: IClashConfigValue;
// clash config fields (use flag)
tun?: IClashConfigValue;
dns?: IClashConfigValue;
hosts?: IClashConfigValue;
script?: IClashConfigValue;
profile?: IClashConfigValue;
payload?: IClashConfigValue;
"interface-name"?: IClashConfigValue;
"routing-mark"?: IClashConfigValue;
// functional fields
use?: string[];
"prepend-rules"?: any[];
"append-rules"?: any[];
"prepend-proxies"?: any[];
"append-proxies"?: any[];
"prepend-proxy-groups"?: any[];
"append-proxy-groups"?: any[];
// fix
ebpf?: any;
experimental?: any;
iptables?: any;
sniffer?: any;
authentication?: any;
"bind-address"?: any;
"external-ui"?: any;
"auto-redir"?: any;
"socks-port"?: any;
"redir-port"?: any;
"tproxy-port"?: any;
"geodata-mode"?: any;
"tcp-concurrent"?: any;
}
// partial of the clash config
type IProfileData = Partial<{
rules: any[];
proxies: any[];
"proxy-groups": any[];
"proxy-providers": any[];
"rule-providers": any[];
[k: string]: any;
}>;
interface IChainItem {
item: IProfileItem;
merge?: IProfileMerge;
script?: string;
}
interface IEnhancedPayload {
chain: IChainItem[];
valid: string[];
current: IProfileData;
callback: string;
}
interface IEnhancedResult {
data: IProfileData;
status: string;
error?: string;
}