wxt 0.12.4 → 0.13.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,15 +1,188 @@
1
- import { Driver, Storage } from 'unstorage';
2
- export * from 'unstorage';
3
-
1
+ declare const storage: WxtStorage;
2
+ interface WxtStorage {
3
+ /**
4
+ * Get an item from storage, or return `null` if it doesn't exist.
5
+ *
6
+ * @example
7
+ * await storage.getItem<number>("local:installDate");
8
+ */
9
+ getItem<T>(key: string, opts?: GetItemOptions<T>): Promise<T | null>;
10
+ /**
11
+ * Get multiple items from storage. There is no guarentee of order in the returned array.
12
+ *
13
+ * @example
14
+ * await storage.getItems(["local:installDate", "session:someCounter"]);
15
+ */
16
+ getItems(keys: Array<string | {
17
+ key: string;
18
+ options?: GetItemOptions<any>;
19
+ }>): Promise<Array<{
20
+ key: string;
21
+ value: any;
22
+ }>>;
23
+ /**
24
+ * Return an object containing metadata about the key. Object is stored at `key + "$"`. If value
25
+ * is not an object, it returns an empty object.
26
+ *
27
+ * @example
28
+ * await storage.getMeta("local:installDate");
29
+ */
30
+ getMeta<T extends Record<string, unknown>>(key: string): Promise<T>;
31
+ /**
32
+ * Set a value in storage. Setting a value to `null` or `undefined` is equivalent to calling
33
+ * `removeItem`.
34
+ *
35
+ * @example
36
+ * await storage.setItem<number>("local:installDate", Date.now());
37
+ */
38
+ setItem<T>(key: string, value: T | null): Promise<void>;
39
+ /**
40
+ * Set multiple values in storage. If a value is set to `null` or `undefined`, the key is removed.
41
+ *
42
+ * @example
43
+ * await storage.setItem([
44
+ * { key: "local:installDate", value: Date.now() },
45
+ * { key: "session:someCounter, value: 5 },
46
+ * ]);
47
+ */
48
+ setItems(values: Array<{
49
+ key: string;
50
+ value: any;
51
+ }>): Promise<void>;
52
+ /**
53
+ * Sets metadata properties. If some properties are already set, but are not included in the
54
+ * `properties` parameter, they will not be removed.
55
+ *
56
+ * @example
57
+ * await storage.setMeta("local:installDate", { appVersion });
58
+ */
59
+ setMeta<T extends Record<string, unknown>>(key: string, properties: T | null): Promise<void>;
60
+ /**
61
+ * Removes an item from storage.
62
+ *
63
+ * @example
64
+ * await storage.removeItem("local:installDate");
65
+ */
66
+ removeItem(key: string, opts?: RemoveItemOptions): Promise<void>;
67
+ /**
68
+ * Remove a list of keys from storage.
69
+ */
70
+ removeItems(keys: Array<string | {
71
+ key: string;
72
+ options?: RemoveItemOptions;
73
+ }>): Promise<void>;
74
+ /**
75
+ * Remove the entire metadata for a key, or specific properties by name.
76
+ *
77
+ * @example
78
+ * // Remove all metadata properties from the item
79
+ * await storage.removeMeta("local:installDate");
80
+ *
81
+ * // Remove only specific the "v" field
82
+ * await storage.removeMeta("local:installDate", "v")
83
+ */
84
+ removeMeta(key: string, properties?: string | string[]): Promise<void>;
85
+ /**
86
+ * Return all the items in storage.
87
+ */
88
+ snapshot(base: string, opts?: SnapshotOptions): Promise<Record<string, unknown>>;
89
+ /**
90
+ * Restores the results of `snapshot`. If new properties have been saved since the snapshot, they are
91
+ * not overridden. Only values existing in the snapshot are overritten.
92
+ */
93
+ restoreSnapshot(base: string, data: any): Promise<void>;
94
+ /**
95
+ * Watch for changes to a specific key in storage.
96
+ */
97
+ watch<T>(key: string, cb: WatchCallback<T>): Unwatch;
98
+ /**
99
+ * Remove all watch listeners.
100
+ */
101
+ unwatch(): void;
102
+ /**
103
+ * Define a constant with utilities for reading/writing to a single value in storage.
104
+ *
105
+ * @example
106
+ * export const installDate = storage.defineItem<number>("local:installDate");
107
+ */
108
+ defineItem<TValue, TMetadata extends Record<string, unknown> = {}>(key: string, options?: WxtStorageItemOptions<TValue>): WxtStorageItem<TValue, TMetadata>;
109
+ }
110
+ interface WxtStorageItem<TValue, TMetadata extends Record<string, unknown>> {
111
+ /**
112
+ * Get the latest value from storage.
113
+ */
114
+ getValue(): Promise<TValue>;
115
+ /**
116
+ * Get metadata.
117
+ */
118
+ getMeta(): Promise<NullablePartial<TMetadata>>;
119
+ /**
120
+ * Set the value in storage.
121
+ */
122
+ setValue(value: TValue | null): Promise<void>;
123
+ /**
124
+ * Set metadata properties.
125
+ */
126
+ setMeta(properties: NullablePartial<TMetadata>): Promise<void>;
127
+ /**
128
+ * Remove the value from storage.
129
+ */
130
+ removeValue(opts?: RemoveItemOptions): Promise<void>;
131
+ /**
132
+ * Remove all metadata or certain properties from metadata.
133
+ */
134
+ removeMeta(properties?: string[]): Promise<void>;
135
+ /**
136
+ * Listen for changes to the value in storage.
137
+ */
138
+ watch(cb: WatchCallback<TValue>): Unwatch;
139
+ }
140
+ interface GetItemOptions<T> {
141
+ /**
142
+ * Value returned from `getValue` when it would otherwise return null.
143
+ */
144
+ defaultValue?: T;
145
+ }
146
+ interface RemoveItemOptions {
147
+ /**
148
+ * Optionally remove metadata when deleting a key.
149
+ *
150
+ * @default false
151
+ */
152
+ removeMeta?: boolean;
153
+ }
154
+ interface SnapshotOptions {
155
+ /**
156
+ * Exclude a list of keys. The storage area prefix should be removed since the snapshot is for a
157
+ * specific storage area already.
158
+ */
159
+ excludeKeys?: string[];
160
+ }
161
+ interface WxtStorageItemOptions<T> extends GetItemOptions<T> {
162
+ /**
163
+ * Provide a version number for the storage item to enable migrations. When changing the version
164
+ * in the future, migration functions will be ran on application startup.
165
+ */
166
+ version?: number;
167
+ /**
168
+ * A map of version numbers to the functions used to migrate the data to that version.
169
+ */
170
+ migrations?: Record<number, (oldValue: any) => any>;
171
+ }
4
172
  /**
5
- * @module wxt/storage
173
+ * Same as `Partial`, but includes `| null`. It makes all the properties of an object optional and
174
+ * nullable.
6
175
  */
176
+ type NullablePartial<T> = {
177
+ [key in keyof T]+?: T[key] | undefined | null;
178
+ };
179
+ /**
180
+ * Callback called when a value in storage is changed.
181
+ */
182
+ type WatchCallback<T> = (newValue: T | null, oldValue: T | null) => void;
183
+ /**
184
+ * Call to remove a watch listener
185
+ */
186
+ type Unwatch = () => void;
7
187
 
8
- interface WebExtensionDriverOptions {
9
- storageArea: 'sync' | 'local' | 'managed' | 'session';
10
- }
11
- declare const webExtensionDriver: (opts: WebExtensionDriverOptions) => Driver;
12
- type StorageValue = null | string | number | boolean | object;
13
- declare const storage: Storage<StorageValue>;
14
-
15
- export { type StorageValue, type WebExtensionDriverOptions, storage, webExtensionDriver };
188
+ export { type GetItemOptions, type NullablePartial, type RemoveItemOptions, type SnapshotOptions, type Unwatch, type WatchCallback, type WxtStorage, type WxtStorageItem, type WxtStorageItemOptions, storage };
package/dist/storage.d.ts CHANGED
@@ -1,15 +1,188 @@
1
- import { Driver, Storage } from 'unstorage';
2
- export * from 'unstorage';
3
-
1
+ declare const storage: WxtStorage;
2
+ interface WxtStorage {
3
+ /**
4
+ * Get an item from storage, or return `null` if it doesn't exist.
5
+ *
6
+ * @example
7
+ * await storage.getItem<number>("local:installDate");
8
+ */
9
+ getItem<T>(key: string, opts?: GetItemOptions<T>): Promise<T | null>;
10
+ /**
11
+ * Get multiple items from storage. There is no guarentee of order in the returned array.
12
+ *
13
+ * @example
14
+ * await storage.getItems(["local:installDate", "session:someCounter"]);
15
+ */
16
+ getItems(keys: Array<string | {
17
+ key: string;
18
+ options?: GetItemOptions<any>;
19
+ }>): Promise<Array<{
20
+ key: string;
21
+ value: any;
22
+ }>>;
23
+ /**
24
+ * Return an object containing metadata about the key. Object is stored at `key + "$"`. If value
25
+ * is not an object, it returns an empty object.
26
+ *
27
+ * @example
28
+ * await storage.getMeta("local:installDate");
29
+ */
30
+ getMeta<T extends Record<string, unknown>>(key: string): Promise<T>;
31
+ /**
32
+ * Set a value in storage. Setting a value to `null` or `undefined` is equivalent to calling
33
+ * `removeItem`.
34
+ *
35
+ * @example
36
+ * await storage.setItem<number>("local:installDate", Date.now());
37
+ */
38
+ setItem<T>(key: string, value: T | null): Promise<void>;
39
+ /**
40
+ * Set multiple values in storage. If a value is set to `null` or `undefined`, the key is removed.
41
+ *
42
+ * @example
43
+ * await storage.setItem([
44
+ * { key: "local:installDate", value: Date.now() },
45
+ * { key: "session:someCounter, value: 5 },
46
+ * ]);
47
+ */
48
+ setItems(values: Array<{
49
+ key: string;
50
+ value: any;
51
+ }>): Promise<void>;
52
+ /**
53
+ * Sets metadata properties. If some properties are already set, but are not included in the
54
+ * `properties` parameter, they will not be removed.
55
+ *
56
+ * @example
57
+ * await storage.setMeta("local:installDate", { appVersion });
58
+ */
59
+ setMeta<T extends Record<string, unknown>>(key: string, properties: T | null): Promise<void>;
60
+ /**
61
+ * Removes an item from storage.
62
+ *
63
+ * @example
64
+ * await storage.removeItem("local:installDate");
65
+ */
66
+ removeItem(key: string, opts?: RemoveItemOptions): Promise<void>;
67
+ /**
68
+ * Remove a list of keys from storage.
69
+ */
70
+ removeItems(keys: Array<string | {
71
+ key: string;
72
+ options?: RemoveItemOptions;
73
+ }>): Promise<void>;
74
+ /**
75
+ * Remove the entire metadata for a key, or specific properties by name.
76
+ *
77
+ * @example
78
+ * // Remove all metadata properties from the item
79
+ * await storage.removeMeta("local:installDate");
80
+ *
81
+ * // Remove only specific the "v" field
82
+ * await storage.removeMeta("local:installDate", "v")
83
+ */
84
+ removeMeta(key: string, properties?: string | string[]): Promise<void>;
85
+ /**
86
+ * Return all the items in storage.
87
+ */
88
+ snapshot(base: string, opts?: SnapshotOptions): Promise<Record<string, unknown>>;
89
+ /**
90
+ * Restores the results of `snapshot`. If new properties have been saved since the snapshot, they are
91
+ * not overridden. Only values existing in the snapshot are overritten.
92
+ */
93
+ restoreSnapshot(base: string, data: any): Promise<void>;
94
+ /**
95
+ * Watch for changes to a specific key in storage.
96
+ */
97
+ watch<T>(key: string, cb: WatchCallback<T>): Unwatch;
98
+ /**
99
+ * Remove all watch listeners.
100
+ */
101
+ unwatch(): void;
102
+ /**
103
+ * Define a constant with utilities for reading/writing to a single value in storage.
104
+ *
105
+ * @example
106
+ * export const installDate = storage.defineItem<number>("local:installDate");
107
+ */
108
+ defineItem<TValue, TMetadata extends Record<string, unknown> = {}>(key: string, options?: WxtStorageItemOptions<TValue>): WxtStorageItem<TValue, TMetadata>;
109
+ }
110
+ interface WxtStorageItem<TValue, TMetadata extends Record<string, unknown>> {
111
+ /**
112
+ * Get the latest value from storage.
113
+ */
114
+ getValue(): Promise<TValue>;
115
+ /**
116
+ * Get metadata.
117
+ */
118
+ getMeta(): Promise<NullablePartial<TMetadata>>;
119
+ /**
120
+ * Set the value in storage.
121
+ */
122
+ setValue(value: TValue | null): Promise<void>;
123
+ /**
124
+ * Set metadata properties.
125
+ */
126
+ setMeta(properties: NullablePartial<TMetadata>): Promise<void>;
127
+ /**
128
+ * Remove the value from storage.
129
+ */
130
+ removeValue(opts?: RemoveItemOptions): Promise<void>;
131
+ /**
132
+ * Remove all metadata or certain properties from metadata.
133
+ */
134
+ removeMeta(properties?: string[]): Promise<void>;
135
+ /**
136
+ * Listen for changes to the value in storage.
137
+ */
138
+ watch(cb: WatchCallback<TValue>): Unwatch;
139
+ }
140
+ interface GetItemOptions<T> {
141
+ /**
142
+ * Value returned from `getValue` when it would otherwise return null.
143
+ */
144
+ defaultValue?: T;
145
+ }
146
+ interface RemoveItemOptions {
147
+ /**
148
+ * Optionally remove metadata when deleting a key.
149
+ *
150
+ * @default false
151
+ */
152
+ removeMeta?: boolean;
153
+ }
154
+ interface SnapshotOptions {
155
+ /**
156
+ * Exclude a list of keys. The storage area prefix should be removed since the snapshot is for a
157
+ * specific storage area already.
158
+ */
159
+ excludeKeys?: string[];
160
+ }
161
+ interface WxtStorageItemOptions<T> extends GetItemOptions<T> {
162
+ /**
163
+ * Provide a version number for the storage item to enable migrations. When changing the version
164
+ * in the future, migration functions will be ran on application startup.
165
+ */
166
+ version?: number;
167
+ /**
168
+ * A map of version numbers to the functions used to migrate the data to that version.
169
+ */
170
+ migrations?: Record<number, (oldValue: any) => any>;
171
+ }
4
172
  /**
5
- * @module wxt/storage
173
+ * Same as `Partial`, but includes `| null`. It makes all the properties of an object optional and
174
+ * nullable.
6
175
  */
176
+ type NullablePartial<T> = {
177
+ [key in keyof T]+?: T[key] | undefined | null;
178
+ };
179
+ /**
180
+ * Callback called when a value in storage is changed.
181
+ */
182
+ type WatchCallback<T> = (newValue: T | null, oldValue: T | null) => void;
183
+ /**
184
+ * Call to remove a watch listener
185
+ */
186
+ type Unwatch = () => void;
7
187
 
8
- interface WebExtensionDriverOptions {
9
- storageArea: 'sync' | 'local' | 'managed' | 'session';
10
- }
11
- declare const webExtensionDriver: (opts: WebExtensionDriverOptions) => Driver;
12
- type StorageValue = null | string | number | boolean | object;
13
- declare const storage: Storage<StorageValue>;
14
-
15
- export { type StorageValue, type WebExtensionDriverOptions, storage, webExtensionDriver };
188
+ export { type GetItemOptions, type NullablePartial, type RemoveItemOptions, type SnapshotOptions, type Unwatch, type WatchCallback, type WxtStorage, type WxtStorageItem, type WxtStorageItemOptions, storage };