π¬ A browser extension and desktop app that turns your clicks into step-by-step guides with annotated screenshots π
TypeScript browser-extensionchrome-extensiondocumentationfirefox-extensionguide-generatorlocal-firstmanifest-v3privacy-firstproductivityreactscreenshotstep-by-step-guidetutorialtypescriptworkflowwxt
fix(storage): check every settings read and write against the declared shape
Luis Ortizf3388add07e6
diff --git a/src/core/guides/types.ts b/src/core/guides/types.tsindex a903889..3a34bb2 100644--- a/src/core/guides/types.ts+++ b/src/core/guides/types.ts@@ -1,6 +1,9 @@import type { AIApiKeys } from '@/core/capture/ai/keys';import type { AIProviderKey } from '@/core/capture/ai/models';import type { VoiceProvider } from '@/core/capture/voice/transcribe';+import type { BrandLogo } from '@/core/export/branding';+import type { ExportOptions } from '@/core/export/options';+import type { GuideMeSession } from '@/core/guideme/session';import type { ScreenshotEdits } from '@/core/screenshot/types';export interface Guide {@@ -61,16 +64,32 @@ export interface Screenshot {export interface Settings {aiApiKey: string;- aiApiKeys?: AIApiKeys;+ aiApiKeys: AIApiKeys;aiProvider: AIProviderKey;aiModel: string;- aiBaseUrl?: string;+ aiBaseUrl: string;+ aiLanguage: string;voiceEnabled: boolean;voiceProvider: VoiceProvider;voiceApiKey: string;voiceMicrophoneId: string;+ voiceLanguage: string;+ blurPresets: Record<string, boolean>;+ targetColor: string;+ brandLogo: BrandLogo | null;+ brandFooter: string;+ brandAttribution: boolean;+ exportOptions: ExportOptions;+ guideMeSession: GuideMeSession | null;+ guideMeStep: Step | null;+ guideMeBlocked: number | null;+ guideMeManual: boolean;+ mimikBlurMode: boolean;+ onboardingCompleted: boolean;}+export type SettingsKey = keyof Settings;+export interface ElementMeta {tag: string;cssSelector: string;diff --git a/src/lib/__tests__/storage-typing.test.ts b/src/lib/__tests__/storage-typing.test.tsnew file mode 100644index 0000000..73123d7--- /dev/null+++ b/src/lib/__tests__/storage-typing.test.ts@@ -0,0 +1,25 @@+import { describe, expectTypeOf, it } from 'vitest';+import type { Settings } from '@/core/guides/types';+import { localStorage } from '@/lib/browser-api';++describe('storage access is checked against the declared settings', () => {+ it('returns the declared type for a key, not unknown', async () => {+ const stored = await localStorage.get(['aiProvider', 'aiApiKeys']);+ expectTypeOf(stored.aiProvider).toEqualTypeOf<Settings['aiProvider'] | undefined>();+ expectTypeOf(stored.aiApiKeys).toEqualTypeOf<Settings['aiApiKeys'] | undefined>();+ });++ it('rejects a key that is not declared', () => {+ // @ts-expect-error an undeclared storage key must not compile+ void localStorage.get(['aiApiKeyTypo']);+ // @ts-expect-error an undeclared storage key must not compile+ void localStorage.set({ aiApiKeyTypo: 'x' });+ });++ it('rejects the wrong type for a declared key', () => {+ // @ts-expect-error aiProvider is a provider key, not an arbitrary string+ void localStorage.set({ aiProvider: 'not-a-provider' });+ // @ts-expect-error voiceEnabled is a boolean+ void localStorage.set({ voiceEnabled: 'yes' });+ });+});diff --git a/src/lib/browser-api.ts b/src/lib/browser-api.tsindex c696d2c..c68ca8e 100644--- a/src/lib/browser-api.ts+++ b/src/lib/browser-api.ts@@ -1,5 +1,6 @@import type { PublicPath } from 'wxt/browser';import { type Browser, browser } from '#imports';+import type { Settings, SettingsKey } from '@/core/guides/types';type HtmlPublicPath = Extract<PublicPath, `${string}.html`>;type ScriptPath = Extract<PublicPath, `${string}.js`>;@@ -104,8 +105,9 @@ export const sessionStorage = {};export const localStorage = {- get: (keys: string[]) => browser.storage.local.get(keys),- set: (items: Record<string, unknown>) => browser.storage.local.set(items),+ get: <K extends SettingsKey>(keys: readonly K[]) =>+ browser.storage.local.get(keys as unknown as K) as Promise<Partial<Pick<Settings, K>>>,+ set: (items: Partial<Settings>) => browser.storage.local.set(items),};export function setSidePanelBehavior(openOnActionClick: boolean): void {