feat: add settings normalization and status storage
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 5.5
parent
1d03ee4054
commit
1b3c4c929f
@@ -0,0 +1,34 @@
|
||||
export const DEFAULT_SETTINGS = { apiKey: '', threshold: 5, minViews: 10000 };
|
||||
|
||||
const toNumber = (value) => (value === null || value === undefined || value === '' ? NaN : Number(value));
|
||||
|
||||
export function normalizeSettings(raw = {}) {
|
||||
const threshold = toNumber(raw.threshold);
|
||||
const minViews = toNumber(raw.minViews);
|
||||
return {
|
||||
apiKey: typeof raw.apiKey === 'string' ? raw.apiKey.trim() : DEFAULT_SETTINGS.apiKey,
|
||||
threshold: Number.isFinite(threshold) && threshold > 0 ? threshold : DEFAULT_SETTINGS.threshold,
|
||||
minViews: Number.isFinite(minViews) && minViews >= 0 ? Math.floor(minViews) : DEFAULT_SETTINGS.minViews,
|
||||
};
|
||||
}
|
||||
|
||||
export async function getSettings() {
|
||||
const { settings } = await chrome.storage.local.get('settings');
|
||||
return normalizeSettings(settings);
|
||||
}
|
||||
|
||||
export async function saveSettings(raw) {
|
||||
const settings = normalizeSettings(raw);
|
||||
await chrome.storage.local.set({ settings });
|
||||
return settings;
|
||||
}
|
||||
|
||||
// Status is { state: 'ok' | 'no-key' | 'invalid-key' | 'quota', message?, until? }.
|
||||
export async function getStatus() {
|
||||
const { status } = await chrome.storage.local.get('status');
|
||||
return status ?? { state: 'ok' };
|
||||
}
|
||||
|
||||
export async function setStatus(status) {
|
||||
await chrome.storage.local.set({ status });
|
||||
}
|
||||
Reference in New Issue
Block a user