refactor(utils): simplify maskApiKey to show only 2 chars at each end

This commit is contained in:
Supra4E8C
2026-01-03 15:42:34 +08:00
parent 1d8729ec53
commit fae4fb0fed

View File

@@ -4,16 +4,17 @@
*/ */
/** /**
* 隐藏 API Key 中间部分 * 隐藏 API Key 中间部分,仅保留前后两位
*/ */
export function maskApiKey(key: string, visibleChars: number = 4): string { export function maskApiKey(key: string): string {
if (!key || key.length <= visibleChars * 2) { if (!key) {
return key; return '';
} }
const visibleChars = 2;
const start = key.slice(0, visibleChars); const start = key.slice(0, visibleChars);
const end = key.slice(-visibleChars); const end = key.slice(-visibleChars);
const maskedLength = Math.min(key.length - visibleChars * 2, 20); const maskedLength = Math.max(key.length - visibleChars * 2, 1);
const masked = '*'.repeat(maskedLength); const masked = '*'.repeat(maskedLength);
return `${start}${masked}${end}`; return `${start}${masked}${end}`;