From 43e6cfc8f31b4f917b14fa5daee75582d542f7a8 Mon Sep 17 00:00:00 2001 From: Serraniel Date: Mon, 28 Dec 2020 22:08:50 +0100 Subject: [PATCH] #48 First types --- package-lock.json | 37 ++++++++++++++++++++ package.json | 4 ++- src/javascript/browserApi/storageProvider.ts | 31 +++++++++------- tsconfig.json | 6 ++++ 4 files changed, 65 insertions(+), 13 deletions(-) create mode 100644 tsconfig.json diff --git a/package-lock.json b/package-lock.json index 497b45e..fff28cd 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1488,12 +1488,37 @@ "dev": true, "optional": true }, + "@types/chrome": { + "version": "0.0.127", + "resolved": "https://registry.npmjs.org/@types/chrome/-/chrome-0.0.127.tgz", + "integrity": "sha512-hBB9EApLYKKn2GvklVkTxVP6vZvxsH9okyIRUinNtMzZHIgIKWQk/ESbX+O5g4Bihfy38+aFGn7Kl7Cxou5JUg==", + "dev": true, + "requires": { + "@types/filesystem": "*", + "@types/har-format": "*" + } + }, "@types/color-name": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/@types/color-name/-/color-name-1.1.1.tgz", "integrity": "sha512-rr+OQyAjxze7GgWrSaJwydHStIhHq2lvY3BOC2Mj7KnzI7XK0Uw1TOOdI9lDoajEbSWLiYgoo4f1R51erQfhPQ==", "dev": true }, + "@types/filesystem": { + "version": "0.0.29", + "resolved": "https://registry.npmjs.org/@types/filesystem/-/filesystem-0.0.29.tgz", + "integrity": "sha512-85/1KfRedmfPGsbK8YzeaQUyV1FQAvMPMTuWFQ5EkLd2w7szhNO96bk3Rh/SKmOfd9co2rCLf0Voy4o7ECBOvw==", + "dev": true, + "requires": { + "@types/filewriter": "*" + } + }, + "@types/filewriter": { + "version": "0.0.28", + "resolved": "https://registry.npmjs.org/@types/filewriter/-/filewriter-0.0.28.tgz", + "integrity": "sha1-wFTor02d11205jq8dviFFocU1LM=", + "dev": true + }, "@types/glob": { "version": "7.1.3", "resolved": "https://registry.npmjs.org/@types/glob/-/glob-7.1.3.tgz", @@ -1504,6 +1529,12 @@ "@types/node": "*" } }, + "@types/har-format": { + "version": "1.2.5", + "resolved": "https://registry.npmjs.org/@types/har-format/-/har-format-1.2.5.tgz", + "integrity": "sha512-IG8AE1m2pWtPqQ7wXhFhy6Q59bwwnLwO36v5Rit2FrbXCIp8Sk8E2PfUCreyrdo17STwFSKDAkitVuVYbpEHvQ==", + "dev": true + }, "@types/minimatch": { "version": "3.0.3", "resolved": "https://registry.npmjs.org/@types/minimatch/-/minimatch-3.0.3.tgz", @@ -13115,6 +13146,12 @@ "integrity": "sha512-2ham8XPWTONajOR0ohOKOHXkm3+gaBmGut3SRuu75xLd/RRaY6vqgh8NBYYk7+RW3u5AtzPQZG8F10LHkl0lAQ==", "dev": true }, + "web-ext-types": { + "version": "3.2.1", + "resolved": "https://registry.npmjs.org/web-ext-types/-/web-ext-types-3.2.1.tgz", + "integrity": "sha512-oQZYDU3W8X867h8Jmt3129kRVKklz70db40Y6OzoTTuzOJpF/dB2KULJUf0txVPyUUXuyzV8GmT3nVvRHoG+Ew==", + "dev": true + }, "which": { "version": "1.3.1", "resolved": "https://registry.npmjs.org/which/-/which-1.3.1.tgz", diff --git a/package.json b/package.json index 6ec0880..d32c98f 100644 --- a/package.json +++ b/package.json @@ -44,6 +44,7 @@ "@babel/plugin-proposal-private-methods": "^7.12.1", "@babel/preset-env": "^7.12.11", "@babel/register": "^7.12.10", + "@types/chrome": "0.0.127", "babelify": "^10.0.0", "browserify": "^17.0.0", "cross-env": "^7.0.3", @@ -74,6 +75,7 @@ "tsify": "^5.0.2", "typescript": "^4.1.3", "vinyl-buffer": "^1.0.1", - "vinyl-source-stream": "^2.0.0" + "vinyl-source-stream": "^2.0.0", + "web-ext-types": "^3.2.1" } } diff --git a/src/javascript/browserApi/storageProvider.ts b/src/javascript/browserApi/storageProvider.ts index 0f84b1d..ff53fa8 100644 --- a/src/javascript/browserApi/storageProvider.ts +++ b/src/javascript/browserApi/storageProvider.ts @@ -1,15 +1,20 @@ const { assigned } = require("../utils/helpers") -class StorageProviderChromium { +export interface ICustomBrowserStorageProvider { + setData(key: string, value: string): void; + getData(key: string, defaultValue: string, callback: (x: string) => void): void; +} - setData(key, value) { +class StorageProviderChromium implements ICustomBrowserStorageProvider { + + setData(key: string, value: string): void { let obj = {}; obj[key] = value; this.getStorage().set(obj); } - getData(key, defaultValue, callback) { + getData(key: string, defaultValue: string, callback: (x: string) => void): void { this.getStorage().get(key, items => { if (assigned(items) && assigned(items[key])) { callback(items[key]); @@ -20,7 +25,7 @@ class StorageProviderChromium { }) } - getStorage() { + private getStorage(): chrome.storage.StorageArea { if (assigned(chrome.storage.sync)) { return chrome.storage.sync; } @@ -30,21 +35,21 @@ class StorageProviderChromium { } -class StorageProviderFirefox { +class StorageProviderFirefox implements ICustomBrowserStorageProvider { - setData(key, value) { + setData(key: string, value: string): void { let obj = {}; obj[key] = value; this.getStorage().set(obj); } - getData(key, defaultValue, callback) { + getData(key: string, defaultValue: string, callback: (x: string) => void): void { let promise = this.getStorage().get(key); promise.then(items => { if (assigned(items) && assigned(items[key])) { - callback(items[key]); + callback(items[key] as string); } else { callback(defaultValue); @@ -52,7 +57,7 @@ class StorageProviderFirefox { }); } - getStorage() { + private getStorage(): browser.storage.StorageArea { if (assigned(browser.storage.sync)) { return browser.storage.sync; } @@ -61,11 +66,13 @@ class StorageProviderFirefox { } } -let __storageProvieder; +let __storageProvieder: ICustomBrowserStorageProvider; function createStorageProvider() { // chrome based browser - if (assigned(chrome?.app)) { + // TODO: chrome.app? + // if (assigned(chrome?.app)) { + if (true) { __storageProvieder = new StorageProviderChromium(); } // firefox @@ -75,7 +82,7 @@ function createStorageProvider() { } -export function getGlobalStorageProvider() { +export function getGlobalStorageProvider(): ICustomBrowserStorageProvider { if (!assigned(__storageProvieder)) { createStorageProvider(); } diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..05d6614 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,6 @@ +{ + "compilerOptions": { + // You have to explicitly set @types to get DefinitelyTyped type definitions + "typeRoots": ["node_modules/@types", "node_modules/web-ext-types"] + } + } \ No newline at end of file