Skip to content

Commit

Permalink
Fix Signature Error and Refactor Configuration.js (#20)
Browse files Browse the repository at this point in the history
* Fix get config error

* Fix get config error & refactor

* Refactor configuration

* Put default config outside

Having the config outside of the getConfiguration() function allows exporting down the line if needed.

---------

Co-authored-by: Fivefold <38764218+Fivefold@users.noreply.github.com>
  • Loading branch information
Sevichecc and Fivefold authored Jun 4, 2023
1 parent 0ab847d commit 9e0bf7b
Showing 1 changed file with 21 additions and 33 deletions.
54 changes: 21 additions & 33 deletions src/configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,32 @@ import { getStorage } from "./browser";

const CONFIG_KEY = "ld_ext_config";

export async function getConfiguration() {
let config = "";

const configJson = await getStorage().get(CONFIG_KEY);
const DEFAULT_CONFIG = {
baseUrl: "",
token: "",
resultNum: 10,
openLinkType: "newTab",
themeGoogle: "auto",
themeDuckduckgo: "auto",
themeBrave: "auto",
themeSearx: "auto",
};

if (
// if there is no saved configuration, save a default configuration
configJson &&
Object.keys(configJson).length === 0 &&
Object.getPrototypeOf(configJson) === Object.prototype
) {
config = {
baseUrl: "",
token: "",
resultNum: 10,
openLinkType: "newTab",
themeGoogle: "auto",
themeDuckduckgo: "auto",
themeBrave: "auto",
themeSearx: "auto",
};
} else {
config = JSON.parse(configJson[CONFIG_KEY]);
}
return config;
}
export async function getConfiguration() {
return new Promise((resolve) => {
getStorage().get(CONFIG_KEY, (data) => {
const config = JSON.parse(data[CONFIG_KEY] || DEFAULT_CONFIG);
resolve(config);
});
});
};

export function saveConfiguration(config) {
const configJson = JSON.stringify(config);
getStorage().set({ [CONFIG_KEY]: configJson });
}

export async function isConfigurationComplete() {
const config = await getConfiguration();

if (config.baseUrl === "" || config.token === "") {
return false;
} else {
return true;
}
}
const { baseUrl, token } = await getConfiguration();
return !!baseUrl && !!token;
};

0 comments on commit 9e0bf7b

Please sign in to comment.