mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-06-05 20:54:14 +00:00
refactor(frontend): drop eslint-disable from models/dbinbound
Replace `any` with explicit domain types: - `coerceInboundJsonField` returns `Record<string, unknown>` (settings/streamSettings/sniffing are always objects). - Add `RawJsonField`, `ClientStats`, `FallbackParentRef`, `DBInboundInit` types. - `_cachedInbound: Inbound | null`, `toInbound(): Inbound`. - `getClientStats(email): ClientStats | undefined`. - `genInboundLinks(): string` (matches actual return from Inbound.genInboundLinks). - Constructor now accepts `DBInboundInit`.
This commit is contained in:
parent
2a5bf835b8
commit
dd0477a839
1 changed files with 66 additions and 18 deletions
|
|
@ -1,16 +1,62 @@
|
||||||
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
||||||
import dayjs, { type Dayjs } from 'dayjs';
|
import dayjs, { type Dayjs } from 'dayjs';
|
||||||
import { ObjectUtil, NumberFormatter, SizeFormatter } from '@/utils';
|
import { ObjectUtil, NumberFormatter, SizeFormatter } from '@/utils';
|
||||||
import { Inbound, Protocols } from './inbound';
|
import { Inbound, Protocols } from './inbound';
|
||||||
|
|
||||||
export function coerceInboundJsonField(value: unknown): any {
|
export type RawJsonField = string | Record<string, unknown> | unknown[];
|
||||||
|
|
||||||
|
export interface ClientStats {
|
||||||
|
email: string;
|
||||||
|
up: number;
|
||||||
|
down: number;
|
||||||
|
total: number;
|
||||||
|
expiryTime: number;
|
||||||
|
enable?: boolean;
|
||||||
|
inboundId?: number;
|
||||||
|
reset?: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface FallbackParentRef {
|
||||||
|
masterId: number;
|
||||||
|
path: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export type DBInboundInit = Partial<{
|
||||||
|
id: number;
|
||||||
|
userId: number;
|
||||||
|
up: number;
|
||||||
|
down: number;
|
||||||
|
total: number;
|
||||||
|
remark: string;
|
||||||
|
enable: boolean;
|
||||||
|
expiryTime: number;
|
||||||
|
trafficReset: string;
|
||||||
|
lastTrafficResetTime: number;
|
||||||
|
listen: string;
|
||||||
|
port: number;
|
||||||
|
protocol: string;
|
||||||
|
settings: RawJsonField;
|
||||||
|
streamSettings: RawJsonField;
|
||||||
|
tag: string;
|
||||||
|
sniffing: RawJsonField;
|
||||||
|
clientStats: ClientStats[] | string;
|
||||||
|
nodeId: number | null;
|
||||||
|
fallbackParent: FallbackParentRef | null;
|
||||||
|
}>;
|
||||||
|
|
||||||
|
export function coerceInboundJsonField(value: unknown): Record<string, unknown> {
|
||||||
if (value == null) return {};
|
if (value == null) return {};
|
||||||
if (typeof value === 'object') return value;
|
if (typeof value === 'object' && !Array.isArray(value)) {
|
||||||
|
return value as Record<string, unknown>;
|
||||||
|
}
|
||||||
if (typeof value !== 'string') return {};
|
if (typeof value !== 'string') return {};
|
||||||
const trimmed = value.trim();
|
const trimmed = value.trim();
|
||||||
if (trimmed === '') return {};
|
if (trimmed === '') return {};
|
||||||
try {
|
try {
|
||||||
return JSON.parse(trimmed);
|
const parsed = JSON.parse(trimmed);
|
||||||
|
if (parsed && typeof parsed === 'object' && !Array.isArray(parsed)) {
|
||||||
|
return parsed as Record<string, unknown>;
|
||||||
|
}
|
||||||
|
return {};
|
||||||
} catch {
|
} catch {
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
@ -31,18 +77,18 @@ export class DBInbound {
|
||||||
listen: string;
|
listen: string;
|
||||||
port: number;
|
port: number;
|
||||||
protocol: string;
|
protocol: string;
|
||||||
settings: any;
|
settings: RawJsonField;
|
||||||
streamSettings: any;
|
streamSettings: RawJsonField;
|
||||||
tag: string;
|
tag: string;
|
||||||
sniffing: any;
|
sniffing: RawJsonField;
|
||||||
clientStats: any;
|
clientStats: ClientStats[] | string;
|
||||||
nodeId: number | null;
|
nodeId: number | null;
|
||||||
fallbackParent: { masterId: number; path: string } | null;
|
fallbackParent: FallbackParentRef | null;
|
||||||
|
|
||||||
private _cachedInbound: any = null;
|
private _cachedInbound: Inbound | null = null;
|
||||||
private _clientStatsMap: Map<string, any> | null = null;
|
private _clientStatsMap: Map<string, ClientStats> | null = null;
|
||||||
|
|
||||||
constructor(data?: any) {
|
constructor(data?: DBInboundInit) {
|
||||||
this.id = 0;
|
this.id = 0;
|
||||||
this.userId = 0;
|
this.userId = 0;
|
||||||
this.up = 0;
|
this.up = 0;
|
||||||
|
|
@ -142,7 +188,7 @@ export class DBInbound {
|
||||||
this._clientStatsMap = null;
|
this._clientStatsMap = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
toInbound(): any {
|
toInbound(): Inbound {
|
||||||
if (this._cachedInbound) {
|
if (this._cachedInbound) {
|
||||||
return this._cachedInbound;
|
return this._cachedInbound;
|
||||||
}
|
}
|
||||||
|
|
@ -166,15 +212,17 @@ export class DBInbound {
|
||||||
return this._cachedInbound;
|
return this._cachedInbound;
|
||||||
}
|
}
|
||||||
|
|
||||||
getClientStats(email: string): any {
|
getClientStats(email: string): ClientStats | undefined {
|
||||||
if (!this._clientStatsMap) {
|
if (!this._clientStatsMap) {
|
||||||
this._clientStatsMap = new Map();
|
this._clientStatsMap = new Map();
|
||||||
if (this.clientStats && Array.isArray(this.clientStats)) {
|
if (Array.isArray(this.clientStats)) {
|
||||||
for (const stats of this.clientStats) {
|
for (const stats of this.clientStats) {
|
||||||
|
if (stats && stats.email) {
|
||||||
this._clientStatsMap.set(stats.email, stats);
|
this._clientStatsMap.set(stats.email, stats);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return this._clientStatsMap.get(email);
|
return this._clientStatsMap.get(email);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -205,7 +253,7 @@ export class DBInbound {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
genInboundLinks(remarkModel: string, hostOverride: string = ''): any[] {
|
genInboundLinks(remarkModel: string, hostOverride: string = ''): string {
|
||||||
const inbound = this.toInbound();
|
const inbound = this.toInbound();
|
||||||
return inbound.genInboundLinks(this.remark, remarkModel, hostOverride);
|
return inbound.genInboundLinks(this.remark, remarkModel, hostOverride);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue