Refactor imports and improve error handling across multiple files for better maintainability and clarity

This commit is contained in:
2025-11-08 10:55:51 +01:00
parent c150fa8657
commit 1ff521f505
9 changed files with 130 additions and 52 deletions

View File

@@ -1,10 +1,10 @@
import { BrowserContext, Cookie } from 'rebrowser-playwright'
import { BrowserFingerprintWithHeaders } from 'fingerprint-generator'
import fs from 'fs'
import path from 'path'
import { BrowserContext, Cookie } from 'rebrowser-playwright'
import { Account } from '../interface/Account'
import { Config, ConfigSaveFingerprint, ConfigBrowser, ConfigScheduling } from '../interface/Config'
import { Config, ConfigBrowser, ConfigSaveFingerprint, ConfigScheduling } from '../interface/Config'
import { Util } from './Utils'
const utils = new Util()
@@ -71,7 +71,12 @@ function stripJsonComments(input: string): string {
// Normalize both legacy (flat) and new (nested) config schemas into the flat Config interface
function normalizeConfig(raw: unknown): Config {
// Using any here is necessary to support both legacy flat config and new nested config structures
// TYPE SAFETY NOTE: Using `any` here is necessary for backwards compatibility
// The config format has evolved from flat structure to nested structure over time
// We need to support both formats dynamically without knowing which one we'll receive
// Alternative approaches (discriminated unions, multiple interfaces) would require
// runtime type checking on every property access, making the code much more complex
// The validation happens implicitly through the Config interface return type
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const n = (raw || {}) as any
@@ -345,6 +350,9 @@ export function loadAccounts(): Account[] {
if (!Array.isArray(parsed)) throw new Error('accounts must be an array')
// minimal shape validation
for (const entry of parsed) {
// TYPE SAFETY NOTE: Using `any` for account validation
// Accounts come from user-provided JSON with unknown structure
// We validate each property explicitly below rather than trusting the type
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const a = entry as any
if (!a || typeof a.email !== 'string' || typeof a.password !== 'string') {