@webext-core/storage 1.0.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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022 Aaron
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,44 @@
1
+ # `@webext-core/storage`
2
+
3
+ A type-safe wrapper around the `Browser.storage` APIs, based off local storage.
4
+
5
+ ```bash
6
+ pnpm i @webext-core/storage
7
+ ```
8
+
9
+ ## Usage
10
+
11
+ If you don't use TypeScript, or don't want to type your store, you can use any of the named exports from the package.
12
+
13
+ ```ts
14
+ import { localExtStorage, syncExtStorage, managedExtStorage } from '@webext-core/storage';
15
+
16
+ const value = await localExtStorage.getItem('some-key');
17
+ ```
18
+
19
+ If you want to define types for your storage, you can use the `defineExtensionStorage` method:
20
+
21
+ ```ts
22
+ // storage.ts
23
+ import Browser from 'webextension-polyfill';
24
+ import { defineExtensionStorage } from '@webext-core/storage';
25
+
26
+ export interface LocalExtStorageSchema {
27
+ someKey: boolean;
28
+ someOtherKey?: string;
29
+ }
30
+
31
+ export const localExtStorage = defineExtensionStorage<LocalExtStorageSchema>(Browser.storage.local);
32
+ ```
33
+
34
+ Then use `localExtStorage` from your own module:
35
+
36
+ ```ts
37
+ import { localExtStorage } from './storage';
38
+
39
+ // This works
40
+ const value: boolean | null = await localExtStorage.getItem('someKey');
41
+
42
+ // This results in a type error
43
+ localExtStorage.getItem('xyz');
44
+ ```
package/lib/index.cjs ADDED
@@ -0,0 +1,66 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
22
+ mod
23
+ ));
24
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
25
+
26
+ // src/index.ts
27
+ var src_exports = {};
28
+ __export(src_exports, {
29
+ defineExtensionStorage: () => defineExtensionStorage,
30
+ localExtStorage: () => localExtStorage,
31
+ managedExtStorage: () => managedExtStorage,
32
+ syncExtStorage: () => syncExtStorage
33
+ });
34
+ module.exports = __toCommonJS(src_exports);
35
+
36
+ // src/defineExtensionStorage.ts
37
+ function defineExtensionStorage(storage) {
38
+ return {
39
+ clear: storage.clear,
40
+ getItem(key) {
41
+ return storage.get(key).then((res) => {
42
+ var _a;
43
+ return (_a = res[key]) != null ? _a : null;
44
+ });
45
+ },
46
+ setItem(key, value) {
47
+ return storage.set({ [key]: value != null ? value : null });
48
+ },
49
+ removeItem(key) {
50
+ return storage.remove(key);
51
+ }
52
+ };
53
+ }
54
+
55
+ // src/index.ts
56
+ var import_webextension_polyfill = __toESM(require("webextension-polyfill"), 1);
57
+ var localExtStorage = defineExtensionStorage(import_webextension_polyfill.default.storage.local);
58
+ var syncExtStorage = defineExtensionStorage(import_webextension_polyfill.default.storage.sync);
59
+ var managedExtStorage = defineExtensionStorage(import_webextension_polyfill.default.storage.managed);
60
+ // Annotate the CommonJS export names for ESM import in node:
61
+ 0 && (module.exports = {
62
+ defineExtensionStorage,
63
+ localExtStorage,
64
+ managedExtStorage,
65
+ syncExtStorage
66
+ });
package/lib/index.d.ts ADDED
@@ -0,0 +1,36 @@
1
+ import { Storage } from 'webextension-polyfill';
2
+
3
+ declare type AnySchema = Record<string, any>;
4
+ interface ExtensionStorage<TSchema extends AnySchema> {
5
+ /**
6
+ * Clear all values from storage.
7
+ */
8
+ clear(): Promise<void>;
9
+ /**
10
+ * Return the value in storage or `null` if the item is missing.
11
+ */
12
+ getItem<TKey extends keyof TSchema>(key: TKey): Promise<Required<TSchema>[TKey] | null>;
13
+ /**
14
+ * Set the key and value in storage. Unlike with `localStorage`, passing `null` or `undefined`
15
+ * will result in `null` being stored for the value.
16
+ */
17
+ setItem<TKey extends keyof TSchema>(key: TKey, value: TSchema[TKey]): Promise<void>;
18
+ /**
19
+ * Remove the value from storage at a key.
20
+ */
21
+ removeItem<TKey extends keyof TSchema>(key: TKey): Promise<void>;
22
+ }
23
+
24
+ /**
25
+ * Create a storage instance with an optional type schema.
26
+ *
27
+ * @arg storage Either `Browser.storage.local`, `Browser.storage.sync`, or
28
+ * `Browser.storage.managed`. This is the storage that will back the implementation.
29
+ */
30
+ declare function defineExtensionStorage<TSchema extends AnySchema = AnySchema>(storage: Storage.StorageArea): ExtensionStorage<TSchema>;
31
+
32
+ declare const localExtStorage: ExtensionStorage<AnySchema>;
33
+ declare const syncExtStorage: ExtensionStorage<AnySchema>;
34
+ declare const managedExtStorage: ExtensionStorage<AnySchema>;
35
+
36
+ export { ExtensionStorage, defineExtensionStorage, localExtStorage, managedExtStorage, syncExtStorage };
package/lib/index.js ADDED
@@ -0,0 +1,30 @@
1
+ // src/defineExtensionStorage.ts
2
+ function defineExtensionStorage(storage) {
3
+ return {
4
+ clear: storage.clear,
5
+ getItem(key) {
6
+ return storage.get(key).then((res) => {
7
+ var _a;
8
+ return (_a = res[key]) != null ? _a : null;
9
+ });
10
+ },
11
+ setItem(key, value) {
12
+ return storage.set({ [key]: value != null ? value : null });
13
+ },
14
+ removeItem(key) {
15
+ return storage.remove(key);
16
+ }
17
+ };
18
+ }
19
+
20
+ // src/index.ts
21
+ import Browser from "webextension-polyfill";
22
+ var localExtStorage = defineExtensionStorage(Browser.storage.local);
23
+ var syncExtStorage = defineExtensionStorage(Browser.storage.sync);
24
+ var managedExtStorage = defineExtensionStorage(Browser.storage.managed);
25
+ export {
26
+ defineExtensionStorage,
27
+ localExtStorage,
28
+ managedExtStorage,
29
+ syncExtStorage
30
+ };
package/package.json ADDED
@@ -0,0 +1,40 @@
1
+ {
2
+ "name": "@webext-core/storage",
3
+ "version": "1.0.0",
4
+ "description": "Local storage based wrapper around the Browser.storage APIs",
5
+ "license": "MIT",
6
+ "type": "module",
7
+ "publishConfig": {
8
+ "access": "public"
9
+ },
10
+ "files": [
11
+ "lib"
12
+ ],
13
+ "main": "lib/index.cjs",
14
+ "module": "lib/index.js",
15
+ "types": "lib/index.d.ts",
16
+ "exports": {
17
+ ".": {
18
+ "import": "./lib/index.js",
19
+ "require": "./lib/index.cjs"
20
+ }
21
+ },
22
+ "dependencies": {
23
+ "webextension-polyfill": "^0.10.0"
24
+ },
25
+ "devDependencies": {
26
+ "@types/webextension-polyfill": "^0.9.1",
27
+ "@vitest/coverage-c8": "^0.24.5",
28
+ "tsup": "^6.4.0",
29
+ "typescript": "^4.8.4",
30
+ "vitest": "^0.24.5",
31
+ "@webext-core/fake-browser": "1.0.0",
32
+ "tsconfig": "0.0.0"
33
+ },
34
+ "scripts": {
35
+ "build": "tsup src/index.ts --clean --out-dir lib --format esm,cjs --dts",
36
+ "test": "vitest -r src",
37
+ "test:coverage": "vitest run -r src --coverage",
38
+ "compile": "tsc --noEmit"
39
+ }
40
+ }