Skip to content

Commit

Permalink
Fix ESLint warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
Ferri0 committed Feb 17, 2025
1 parent cdf6ddc commit 79277d6
Showing 1 changed file with 22 additions and 25 deletions.
47 changes: 22 additions & 25 deletions scripts/configs.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const ALLOWED_CONFIGS = ["prod", "stage", "dev"];
const ALLOWED_CONFIGS = ['prod', 'stage', 'dev'];

/**
* This function calculates the environment in which the site is running based on the URL.
Expand All @@ -9,19 +9,19 @@ const ALLOWED_CONFIGS = ["prod", "stage", "dev"];
*/
export const calcEnvironment = () => {
const { host, href } = window.location;
let environment = "prod";
if (href.includes(".aem.page") || host.includes("staging")) {
environment = "stage";
let environment = 'prod';
if (href.includes('.aem.page') || host.includes('staging')) {
environment = 'stage';
}
if (href.includes("localhost")) {
environment = "dev";
if (href.includes('localhost')) {
environment = 'dev';
}

const environmentFromConfig = window.sessionStorage.getItem("environment");
const environmentFromConfig = window.sessionStorage.getItem('environment');
if (
environmentFromConfig &&
ALLOWED_CONFIGS.includes(environmentFromConfig) &&
environment !== "prod"
environmentFromConfig
&& ALLOWED_CONFIGS.includes(environmentFromConfig)
&& environment !== 'prod'
) {
return environmentFromConfig;
}
Expand All @@ -31,8 +31,8 @@ export const calcEnvironment = () => {

function buildConfigURL(environment) {
const env = environment || calcEnvironment();
let fileName = "configs.json";
if (env !== "prod") {
let fileName = 'configs.json';
if (env !== 'prod') {
fileName = `configs-${env}.json`;
}
const configURL = new URL(`${window.location.origin}/${fileName}`);
Expand All @@ -45,15 +45,15 @@ const getConfigForEnvironment = async (environment) => {
try {
const configJSON = window.sessionStorage.getItem(`config:${env}`);
if (!configJSON) {
throw new Error("No config in session storage");
throw new Error('No config in session storage');
}

const parsedConfig = JSON.parse(configJSON);
if (
!parsedConfig[":expiry"] ||
parsedConfig[":expiry"] < Math.round(Date.now() / 1000)
!parsedConfig[':expiry']
|| parsedConfig[':expiry'] < Math.round(Date.now() / 1000)
) {
throw new Error("Config expired");
throw new Error('Config expired');
}

return parsedConfig;
Expand All @@ -63,7 +63,7 @@ const getConfigForEnvironment = async (environment) => {
throw new Error(`Failed to fetch config for ${env}`);
}
configJSON = await configJSON.json();
configJSON[":expiry"] = Math.round(Date.now() / 1000) + 7200;
configJSON[':expiry'] = Math.round(Date.now() / 1000) + 7200;
window.sessionStorage.setItem(`config:${env}`, JSON.stringify(configJSON));
return configJSON;
}
Expand Down Expand Up @@ -94,25 +94,23 @@ export const getConfigValue = async (configParam, environment) => {
export const getHeaders = async (scope, environment) => {
const env = environment || calcEnvironment();
const config = await getConfigForEnvironment(env);
const configElements = config.data.filter((el) =>
el?.key.includes(`headers.${scope}`)
);
const configElements = config.data.filter((el) => el?.key.includes(`headers.${scope}`));

return configElements.reduce((obj, item) => {
let { key } = item;
if (key.includes(`commerce.headers.${scope}.`)) {
key = key.replace(`commerce.headers.${scope}.`, "");
key = key.replace(`commerce.headers.${scope}.`, '');
}
return { ...obj, [key]: item.value };
}, {});
};

export const getCookie = (cookieName) => {
const cookies = document.cookie.split(";");
const cookies = document.cookie.split(';');
let foundValue;

cookies.forEach((cookie) => {
const [name, value] = cookie.trim().split("=");
const [name, value] = cookie.trim().split('=');
if (name === cookieName) {
foundValue = decodeURIComponent(value);
}
Expand All @@ -121,5 +119,4 @@ export const getCookie = (cookieName) => {
return foundValue;
};

export const checkIsAuthenticated = () =>
!!getCookie("auth_dropin_user_token") ?? false;
export const checkIsAuthenticated = () => !!getCookie('auth_dropin_user_token') ?? false;

0 comments on commit 79277d6

Please sign in to comment.