From 55113d654d2f809fd0a38273fc8c13a402bcd3a1 Mon Sep 17 00:00:00 2001 From: Aaron Date: Tue, 9 Jul 2024 12:29:56 -0500 Subject: [PATCH] Remove wxt/storage --- .../analytics/modules/analytics/client.ts | 28 +++++++++++-------- packages/analytics/package.json | 2 +- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/packages/analytics/modules/analytics/client.ts b/packages/analytics/modules/analytics/client.ts index 7bf6d3005..4d2ceb22b 100644 --- a/packages/analytics/modules/analytics/client.ts +++ b/packages/analytics/modules/analytics/client.ts @@ -1,16 +1,24 @@ import { defineWxtPlugin } from 'wxt/sandbox'; -import { storage } from 'wxt/storage'; import { useAppConfig } from 'wxt/client'; -import { browser } from 'wxt/browser'; import { Analytics, AnalyticsConfig, AnalyticsPageViewEvent, AnalyticsTrackEvent, BaseAnalyticsEvent, + AnalyticsStorageItemConfig, } from './types'; import uaParser from 'ua-parser-js'; +const defineStorageItem = ( + key: string, + defaultValue?: NonNullable, +): AnalyticsStorageItemConfig => ({ + getValue: async () => + (await chrome.storage.local.get(key))[key] ?? defaultValue, + setValue: (newValue) => chrome.storage.local.set({ [key]: newValue }), +}); + export let analytics: Analytics; const ANALYTICS_PORT = 'wxt-analytics'; const interactiveTags = new Set(['A', 'BUTTON', 'INPUT', 'SELECT', 'TEXTAREA']); @@ -31,7 +39,7 @@ export default defineWxtPlugin(() => { }); function createAnalyticsForwarder(): Analytics { - const port = browser.runtime.connect({ name: ANALYTICS_PORT }); + const port = chrome.runtime.connect({ name: ANALYTICS_PORT }); const sessionId = Date.now(); const getMetadata = (): ForwardMetadata => ({ sessionId, @@ -92,27 +100,25 @@ function createBackgroundAnalytics(): Analytics { // User properties storage const userIdStorage = - config?.userId ?? storage.defineItem('local:wxt-analytics:user-id'); + config?.userId ?? defineStorageItem('local:wxt-analytics:user-id'); const userPropertiesStorage = config?.userProperties ?? - storage.defineItem>( + defineStorageItem>( 'local:wxt-analytics:user-properties', - { defaultValue: {} }, + {}, ); const enabled = config?.enabled ?? - storage.defineItem('local:wxt-analytics:enabled', { - defaultValue: false, - }); + defineStorageItem('local:wxt-analytics:enabled', false); // Cached values - const platformInfo = browser.runtime.getPlatformInfo(); + const platformInfo = chrome.runtime.getPlatformInfo(); const userAgent = uaParser(); let userId = Promise.resolve(userIdStorage.getValue()).then( (id) => id ?? globalThis.crypto.randomUUID(), ); let userProperties = userPropertiesStorage.getValue(); - const manifest = browser.runtime.getManifest(); + const manifest = chrome.runtime.getManifest(); const getBaseEvent = async ( meta: ForwardMetadata = { diff --git a/packages/analytics/package.json b/packages/analytics/package.json index eb5a41e54..f13ae9b59 100644 --- a/packages/analytics/package.json +++ b/packages/analytics/package.json @@ -1,6 +1,6 @@ { "name": "@wxt-dev/analytics", - "version": "0.1.4", + "version": "0.1.5", "description": "Add analytics to your web extension", "repository": { "type": "git",