From 6fd2ea22f1ddf97251cb86628eac75f42003c0e3 Mon Sep 17 00:00:00 2001 From: Biplob Sutradhar Date: Thu, 25 Apr 2024 19:39:41 +0600 Subject: [PATCH] refactored storage codes --- .prettierrc | 3 + package-lock.json | 91 ++++++ package.json | 2 + src/background/helper.ts | 19 ++ src/components/Footer.svelte | 13 +- src/components/Header.svelte | 16 +- src/components/Main.svelte | 13 +- src/components/Theme_Switch.svelte | 18 +- src/components/Update_XPath.svelte | 8 +- src/components/api/Data.svelte | 11 +- src/components/api/Item.svelte | 30 +- src/components/api/Select_Account.svelte | 84 ++---- src/components/data/Zip_Reader.svelte | 8 +- src/components/pages/API.svelte | 144 ++++------ src/components/pages/Home.svelte | 64 ++--- src/components/setting/Mode_Switch.svelte | 20 +- src/components/setting/api/API_Delay.svelte | 20 +- .../setting/xpath/Update_Xpath.svelte | 113 ++++---- src/popup/helper.ts | 4 +- src/utils/core.ts | 82 ++++++ src/utils/default.ts | 31 +- src/utils/helper.ts | 29 +- src/utils/schema.ts | 22 +- src/utils/storage.ts | 271 +++--------------- src/utils/types.ts | 2 +- 25 files changed, 555 insertions(+), 563 deletions(-) create mode 100644 .prettierrc create mode 100644 src/utils/core.ts diff --git a/.prettierrc b/.prettierrc new file mode 100644 index 0000000..d0b3f16 --- /dev/null +++ b/.prettierrc @@ -0,0 +1,3 @@ +{ + "plugins": ["prettier-plugin-tailwindcss"] +} \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 2c8902e..b8af766 100644 --- a/package-lock.json +++ b/package-lock.json @@ -26,6 +26,8 @@ "pino": "^9.0.0", "pino-pretty": "^11.0.0", "postcss": "^8.4.38", + "prettier": "^3.2.5", + "prettier-plugin-tailwindcss": "^0.5.14", "qs": "^6.12.1", "svelte": "^4.2.15", "svelte-check": "3.6.9", @@ -3008,6 +3010,95 @@ "integrity": "sha512-1NNCs6uurfkVbeXG4S8JFT9t19m45ICnif8zWLd5oPSZ50QnwMfK+H3jv408d4jw/7Bttv5axS5IiHoLaVNHeQ==", "dev": true }, + "node_modules/prettier": { + "version": "3.2.5", + "resolved": "https://registry.npmjs.org/prettier/-/prettier-3.2.5.tgz", + "integrity": "sha512-3/GWa9aOC0YeD7LUfvOG2NiDyhOWRvt1k+rcKhOuYnMY24iiCphgneUfJDyFXd6rZCAnuLBv6UeAULtrhT/F4A==", + "dev": true, + "bin": { + "prettier": "bin/prettier.cjs" + }, + "engines": { + "node": ">=14" + }, + "funding": { + "url": "https://github.com/prettier/prettier?sponsor=1" + } + }, + "node_modules/prettier-plugin-tailwindcss": { + "version": "0.5.14", + "resolved": "https://registry.npmjs.org/prettier-plugin-tailwindcss/-/prettier-plugin-tailwindcss-0.5.14.tgz", + "integrity": "sha512-Puaz+wPUAhFp8Lo9HuciYKM2Y2XExESjeT+9NQoVFXZsPPnc9VYss2SpxdQ6vbatmt8/4+SN0oe0I1cPDABg9Q==", + "dev": true, + "engines": { + "node": ">=14.21.3" + }, + "peerDependencies": { + "@ianvs/prettier-plugin-sort-imports": "*", + "@prettier/plugin-pug": "*", + "@shopify/prettier-plugin-liquid": "*", + "@trivago/prettier-plugin-sort-imports": "*", + "@zackad/prettier-plugin-twig-melody": "*", + "prettier": "^3.0", + "prettier-plugin-astro": "*", + "prettier-plugin-css-order": "*", + "prettier-plugin-import-sort": "*", + "prettier-plugin-jsdoc": "*", + "prettier-plugin-marko": "*", + "prettier-plugin-organize-attributes": "*", + "prettier-plugin-organize-imports": "*", + "prettier-plugin-sort-imports": "*", + "prettier-plugin-style-order": "*", + "prettier-plugin-svelte": "*" + }, + "peerDependenciesMeta": { + "@ianvs/prettier-plugin-sort-imports": { + "optional": true + }, + "@prettier/plugin-pug": { + "optional": true + }, + "@shopify/prettier-plugin-liquid": { + "optional": true + }, + "@trivago/prettier-plugin-sort-imports": { + "optional": true + }, + "@zackad/prettier-plugin-twig-melody": { + "optional": true + }, + "prettier-plugin-astro": { + "optional": true + }, + "prettier-plugin-css-order": { + "optional": true + }, + "prettier-plugin-import-sort": { + "optional": true + }, + "prettier-plugin-jsdoc": { + "optional": true + }, + "prettier-plugin-marko": { + "optional": true + }, + "prettier-plugin-organize-attributes": { + "optional": true + }, + "prettier-plugin-organize-imports": { + "optional": true + }, + "prettier-plugin-sort-imports": { + "optional": true + }, + "prettier-plugin-style-order": { + "optional": true + }, + "prettier-plugin-svelte": { + "optional": true + } + } + }, "node_modules/process": { "version": "0.11.10", "resolved": "https://registry.npmjs.org/process/-/process-0.11.10.tgz", diff --git a/package.json b/package.json index ddb4fdd..b939b4e 100644 --- a/package.json +++ b/package.json @@ -30,6 +30,8 @@ "pino": "^9.0.0", "pino-pretty": "^11.0.0", "postcss": "^8.4.38", + "prettier": "^3.2.5", + "prettier-plugin-tailwindcss": "^0.5.14", "qs": "^6.12.1", "svelte": "^4.2.15", "svelte-check": "3.6.9", diff --git a/src/background/helper.ts b/src/background/helper.ts index 044f22f..aa8d6d9 100644 --- a/src/background/helper.ts +++ b/src/background/helper.ts @@ -1,4 +1,7 @@ import { runtime } from "src/utils/communication"; +import { XPATH_URL } from "src/utils/constants"; +import { addDate } from "src/utils/helper"; +import { XPathModelSchema, type XPathModel } from "src/utils/xpaths"; export async function readySignalSend() { // Ready signal @@ -10,3 +13,19 @@ export async function readySignalSend() { }, }); } + +export async function fetchXPathUpdate( + signal?: AbortSignal, +): Promise { + try { + const resJson = await (await fetch(XPATH_URL, { signal })).json(); + + const xPathValueValidated = await XPathModelSchema.parseAsync(resJson); + + const xpathValues = addDate(xPathValueValidated); + + return xpathValues; + } catch (e) { + return undefined; + } +} diff --git a/src/components/Footer.svelte b/src/components/Footer.svelte index 83acc48..f5369ac 100644 --- a/src/components/Footer.svelte +++ b/src/components/Footer.svelte @@ -1,19 +1,10 @@
@@ -26,7 +17,7 @@ > Github logo Learn more diff --git a/src/components/Header.svelte b/src/components/Header.svelte index f77131a..4d2451c 100644 --- a/src/components/Header.svelte +++ b/src/components/Header.svelte @@ -3,18 +3,8 @@ import icon48 from "src/assets/icons/icon128.png"; import ThemeSwitch from "./Theme_Switch.svelte"; - import { modeWritable, type MODE } from "src/utils/storage"; - import { onMount } from "svelte"; import { blur } from "svelte/transition"; - import { MODE_DEFAULT } from "src/utils/default"; - - let localMode: MODE = MODE_DEFAULT; - - onMount(() => { - modeWritable.subscribe((mode) => { - localMode = mode; - }); - }); + import { workingModeWritable } from "src/utils/storage";
@@ -24,12 +14,12 @@ - {#key localMode} + {#key $workingModeWritable}
- {localMode} + {$workingModeWritable}
{/key}
diff --git a/src/components/Main.svelte b/src/components/Main.svelte index a638f3e..e20067f 100644 --- a/src/components/Main.svelte +++ b/src/components/Main.svelte @@ -4,19 +4,10 @@ import About from "src/components/pages/About.svelte"; import type { TabName } from "../utils/types"; import { slide, blur } from "svelte/transition"; - import { type MODE, modeWritable } from "src/utils/storage"; - import { onMount } from "svelte"; import Api from "./pages/API.svelte"; - import { MODE_DEFAULT } from "src/utils/default"; + import { workingModeWritable } from "src/utils/storage"; let tabName: TabName = "Home"; - let localMode: MODE = MODE_DEFAULT; - - onMount(() => { - modeWritable.subscribe((mode) => { - localMode = mode; - }); - });
@@ -41,7 +32,7 @@
{#if tabName === "Home"}
- {#if localMode === "xpath"} + {#if $workingModeWritable === "xpath"} {:else} diff --git a/src/components/Theme_Switch.svelte b/src/components/Theme_Switch.svelte index 53299ce..6dc5125 100644 --- a/src/components/Theme_Switch.svelte +++ b/src/components/Theme_Switch.svelte @@ -1,25 +1,33 @@
- {#if user} + {#if user?.given_name}
{user.given_name}
{:else}
diff --git a/src/components/api/Select_Account.svelte b/src/components/api/Select_Account.svelte index 8892aa1..e1643c4 100644 --- a/src/components/api/Select_Account.svelte +++ b/src/components/api/Select_Account.svelte @@ -3,33 +3,25 @@ import { runtime } from "src/utils/communication"; import { delay } from "src/utils/helper"; import log from "src/utils/logger"; - import type { User } from "src/utils/schema"; import { blur } from "svelte/transition"; import { - channel0OAuthTokenWritable, - channel1OAuthTokenWritable, + firstOAuthKeyWritable, + secondOAuthKeyWritable, firstUserWritable, secondUserWritable, + primaryChannelWritable, } from "src/utils/storage"; - import { onMount } from "svelte"; import Item from "./Item.svelte"; import axios from "axios"; - import type { PrimaryChannel } from "src/utils/types"; import DocsLink from "../Docs_Link.svelte"; import { docs } from "src/utils/docs"; + import { SETTINGS_DEFAULT as ud } from "src/utils/default"; - export let primaryChannel: PrimaryChannel; export let isRunning: boolean; export let isReady: boolean; export let isStop: boolean; export let setStatus: (msg: string, isError?: boolean) => void; - let channel0OAuthToken: string | null = null; - let channel1OAuthToken: string | null = null; - - let firstUser: User | null = null; - let secondUser: User | null = null; - let primaryChannelName: string; async function waitingForResponse(msg: string, sec: number, ms: number) { @@ -77,22 +69,22 @@ } } - async function connectDisconnect(btnNo: 0 | 1) { + async function connectDisconnect(btnNo: "0" | "1") { isRunning = true; - primaryChannel = btnNo; + primaryChannelWritable.set(btnNo); try { - if (btnNo === 0) { - if (channel0OAuthToken) { - await revokeToken(channel0OAuthToken); - channel0OAuthTokenWritable.set(null); - firstUserWritable.set(null); + if (btnNo === "0") { + if ($firstOAuthKeyWritable) { + await revokeToken($firstOAuthKeyWritable); + firstOAuthKeyWritable.set(""); + firstUserWritable.set(ud.firstUser); return; } } else { - if (channel1OAuthToken) { - await revokeToken(channel1OAuthToken); - channel1OAuthTokenWritable.set(null); - secondUserWritable.set(null); + if ($secondOAuthKeyWritable) { + await revokeToken($secondOAuthKeyWritable); + secondOAuthKeyWritable.set(""); + secondUserWritable.set(ud.secondUser); return; } } @@ -105,34 +97,24 @@ await waitingForResponseReady(`Waiting for the OAuth Token `, 30); } catch (error) { log.info(error); - primaryChannel = -1; + $primaryChannelWritable = "-1"; } finally { isRunning = false; } } - onMount(() => { - firstUserWritable.subscribe((value) => (firstUser = value)); - secondUserWritable.subscribe((value) => (secondUser = value)); - - channel0OAuthTokenWritable.subscribe( - (value) => (channel0OAuthToken = value) - ); - - channel1OAuthTokenWritable.subscribe( - (value) => (channel1OAuthToken = value) - ); - }); - $: { - if (primaryChannel === 0 && firstUser) { - primaryChannelName = firstUser.given_name; - } else if (primaryChannel === 1 && secondUser) { - primaryChannelName = secondUser.given_name; + if ($primaryChannelWritable === "0" && $firstUserWritable?.given_name) { + primaryChannelName = $firstUserWritable.given_name; + } else if ( + $primaryChannelWritable === "1" && + $secondUserWritable?.given_name + ) { + primaryChannelName = $secondUserWritable.given_name; } else { primaryChannelName = "Choose an account"; if (!isRunning) { - primaryChannel = -1; + primaryChannelWritable.set("-1"); } } } @@ -164,22 +146,8 @@ - - + +
diff --git a/src/components/data/Zip_Reader.svelte b/src/components/data/Zip_Reader.svelte index 1b38d8e..6863050 100644 --- a/src/components/data/Zip_Reader.svelte +++ b/src/components/data/Zip_Reader.svelte @@ -3,7 +3,7 @@ import axios, { AxiosError } from "axios"; import { csv_async_iter } from "csv-iter-parse"; import { API_KEY, CHANNEL_API_URL } from "src/utils/constants"; - import { API_REQ_DELAY_DEFAULT } from "src/utils/default"; + import { SETTINGS_DEFAULT as sd } from "src/utils/default"; import { delay } from "src/utils/helper"; import log from "src/utils/logger"; import { ChannelRawSchema } from "src/utils/schema"; @@ -42,12 +42,12 @@ switch (errors.response?.status) { case 401: console.error( - "Reconnect your account. OAuth token might be expired!" + "Reconnect your account. OAuth token might be expired!", ); return customUrls; case 404: console.error( - "The subscriber identified with the request cannot be found." + "The subscriber identified with the request cannot be found.", ); return customUrls; } @@ -62,7 +62,7 @@ break; } customUrls.push(...raw.data); - await delay(API_REQ_DELAY_DEFAULT); + await delay(sd.apiReqDelay); } return customUrls; } diff --git a/src/components/pages/API.svelte b/src/components/pages/API.svelte index 64a6a3b..7dc238e 100644 --- a/src/components/pages/API.svelte +++ b/src/components/pages/API.svelte @@ -9,14 +9,13 @@ } from "src/utils/communication"; import { apiReqDelayWritable, - channel0OAuthTokenWritable, - channel1OAuthTokenWritable, + firstOAuthKeyWritable, + secondOAuthKeyWritable, firstUserWritable, - primaryChannelWritable, secondUserWritable, - subscriptionsWritable, + subscriptionsListWritable, + primaryChannelWritable, } from "src/utils/storage"; - import { get } from "svelte/store"; import { blur, slide } from "svelte/transition"; import log from "src/utils/logger"; import Timer from "../Timer.svelte"; @@ -35,15 +34,11 @@ SUBSCRIPTIONS_API_URL, USERINFO_API_URL, } from "src/utils/constants"; - import type { PrimaryChannel } from "src/utils/types"; - import { API_REQ_DELAY_DEFAULT } from "src/utils/default.js"; + import { SETTINGS_DEFAULT as ud } from "src/utils/default"; - let subscriptionsList: SubscriptionsList = []; - let subscriptionCount: number = 0; - let channel0OAuthToken: string | null = null; - let channel1OAuthToken: string | null = null; + let subscriptionsList = $subscriptionsListWritable; + let subscriptionCount = $subscriptionsListWritable.length; - let primaryChannel: PrimaryChannel = -1; let lastStatusData: RuntimeMessage | undefined = undefined; let isRunning = true; @@ -55,7 +50,12 @@ let failedCount = 0; let successCount = 0; let actionName = ""; - let apiReqDelay = API_REQ_DELAY_DEFAULT; + + function getAccessToken() { + return $primaryChannelWritable === "0" + ? $firstOAuthKeyWritable + : $secondOAuthKeyWritable; + } async function stop() { isStop = true; @@ -84,7 +84,7 @@ } setStatus( "Unable to get the subscriptions list from the content client", - true + true, ); return false; } @@ -99,24 +99,24 @@ if (mode) { notFoundList = currentSubs.filter( (elem) => - !subscriptionsList.map((y) => y.channelId).includes(elem.channelId) + !subscriptionsList.map((y) => y.channelId).includes(elem.channelId), ); if (notFoundList.length === 0) { setStatus( - "All those channels have already been subscribed to! There's no need to subscribe again." + "All those channels have already been subscribed to! There's no need to subscribe again.", ); } } else { notFoundList = currentSubs.filter((elem) => - subscriptionsList.map((y) => y.channelId).includes(elem.channelId) + subscriptionsList.map((y) => y.channelId).includes(elem.channelId), ); if (notFoundList.length === 0) { setStatus( - "No channel match with your current subscriptions channel list! NO need to unsubscribe" + "No channel match with your current subscriptions channel list! NO need to unsubscribe", ); } } - subscriptionsWritable.set(notFoundList); + subscriptionsListWritable.set(notFoundList); subscriptionsList = notFoundList; subscriptionCount = notFoundList.length; } @@ -161,7 +161,7 @@ setStatus(`${un}subscribe to the ${title} unsuccessful`, true); failedCount++; } - await delay(apiReqDelay); + await delay($apiReqDelayWritable); } if (!isStop) { setStatus("Done"); @@ -173,7 +173,7 @@ isSubRunning = false; isRunning = false; // console.log(subscriptionsList); - subscriptionsWritable.set(subscriptionsList); + subscriptionsListWritable.set(subscriptionsList); isStop = false; } } @@ -193,9 +193,8 @@ async function parseData(dataLocal: RuntimeMessage) { setStatus("..."); - const validationResult = await runtimeMessageSchema.safeParseAsync( - dataLocal - ); + const validationResult = + await runtimeMessageSchema.safeParseAsync(dataLocal); if (!validationResult.success) { setStatus("Error when parsing data", true); @@ -222,31 +221,31 @@ setStatus(status.msg, true); isRunning = false; isReady = true; - primaryChannel = -1; + primaryChannelWritable.set("-1"); return; default: return; } } else if (dataLocal.type === "dataOptionAuthToken") { - if (primaryChannel === -1) { + if ($primaryChannelWritable === "-1") { setStatus( "Received OAuth token, but it was rejected due to not arriving on time", - true + true, ); return; } setStatus("OAuth token receive. Getting user information"); - switch (primaryChannel) { - case 0: - channel0OAuthTokenWritable.set(dataLocal.authToken); + switch ($primaryChannelWritable) { + case "0": + firstOAuthKeyWritable.set(dataLocal.authToken); const userData0 = await getUserInfo(); if (userData0) { firstUserWritable.set(userData0); } break; - case 1: - channel1OAuthTokenWritable.set(dataLocal.authToken); + case "1": + secondOAuthKeyWritable.set(dataLocal.authToken); const userData1 = await getUserInfo(); if (userData1) { secondUserWritable.set(userData1); @@ -263,10 +262,8 @@ } async function deleteSubscription(id: string) { - const oAuthToken = - primaryChannel === 0 ? channel0OAuthToken : channel1OAuthToken; const headers = { - Authorization: "Bearer " + oAuthToken, + Authorization: "Bearer " + getAccessToken(), "Content-Type": "application/json", }; @@ -288,7 +285,7 @@ case 401: setStatus( "Reconnect your account. OAuth token might be expired!", - true + true, ); resetAccount(); isStop = true; @@ -296,7 +293,7 @@ case 404: setStatus( "The subscriber identified with the request cannot be found.", - true + true, ); return false; @@ -311,10 +308,8 @@ } async function insertSubscription(channelId: string) { - const oAuthToken = - primaryChannel === 0 ? channel0OAuthToken : channel1OAuthToken; const headers = { - Authorization: "Bearer " + oAuthToken, + Authorization: "Bearer " + getAccessToken(), "Content-Type": "application/json", }; @@ -335,7 +330,7 @@ key: API_KEY, }, headers, - } + }, ); return true; } catch (err) { @@ -347,7 +342,7 @@ case 401: setStatus( "Reconnect your account. OAuth token might be expired!", - true + true, ); resetAccount(); isStop = true; @@ -355,14 +350,14 @@ case 400: setStatus( "You have reached your maximum number of subscriptions.", - true + true, ); isStop = true; return false; case 404: setStatus( "The subscriber identified with the request cannot be found.", - true + true, ); return false; @@ -377,10 +372,8 @@ } async function getChannelsList() { - const oAuthToken = - primaryChannel === 0 ? channel0OAuthToken : channel1OAuthToken; const headers = { - Authorization: "Bearer " + oAuthToken, + Authorization: "Bearer " + getAccessToken(), "Content-Type": "application/json", }; @@ -409,14 +402,14 @@ case 401: setStatus( "Reconnect your account. OAuth token might be expired!", - true + true, ); resetAccount(); return; case 404: setStatus( "The subscriber identified with the request cannot be found.", - true + true, ); return; } @@ -450,24 +443,24 @@ pageToken = data.nextPageToken; - await delay(apiReqDelay); + await delay($apiReqDelayWritable); } log.info(subscriptionsList); - subscriptionsWritable.set(subscriptionsList); + subscriptionsListWritable.set(subscriptionsList); subscriptionCount = subscriptionsList.length; setStatus("Subscription list collected successful"); } function resetAccount() { - switch (primaryChannel) { - case 0: - channel0OAuthTokenWritable.set(null); - firstUserWritable.set(null); + switch ($primaryChannelWritable) { + case "0": + firstOAuthKeyWritable.set(""); + firstUserWritable.set(ud.firstUser); break; - case 1: - channel1OAuthTokenWritable.set(null); - secondUserWritable.set(null); + case "1": + secondOAuthKeyWritable.set(""); + secondUserWritable.set(ud.secondUser); break; } } @@ -476,8 +469,7 @@ try { const res = await axios.get(USERINFO_API_URL, { params: { - access_token: - primaryChannel === 0 ? channel0OAuthToken : channel1OAuthToken, + access_token: getAccessToken(), }, }); @@ -494,21 +486,6 @@ } onMount(async () => { - channel0OAuthTokenWritable.subscribe( - (value) => (channel0OAuthToken = value) - ); - - channel1OAuthTokenWritable.subscribe( - (value) => (channel1OAuthToken = value) - ); - - primaryChannelWritable.subscribe((value) => (primaryChannel = value)); - - apiReqDelayWritable.subscribe((value) => (apiReqDelay = value)); - - subscriptionsList = get(subscriptionsWritable); - subscriptionCount = subscriptionsList.length; - storageRemoveListener = runtime.addListener(parseData); await readySignalSend(); }); @@ -519,13 +496,7 @@
- +