Skip to content

Commit 88c6b70

Browse files
committed
Refactor to no longer use window.defaultConfig, instead create a module
1 parent 5228499 commit 88c6b70

File tree

115 files changed

+799
-760
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

115 files changed

+799
-760
lines changed

frontend/env.d.ts

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1 @@
11
/// <reference types="vite/client" />
2-
3-
export {};
4-
5-
declare global {
6-
interface Window {
7-
defaultConfig: {
8-
default_route: string;
9-
version: string;
10-
service_control_url: string;
11-
monitoring_urls: string[];
12-
showPendingRetry: boolean;
13-
};
14-
}
15-
}

frontend/src/defaultConfig.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
export interface DefaultConfig {
2+
default_route: string;
3+
version: string;
4+
service_control_url: string;
5+
monitoring_url: string;
6+
showPendingRetry: boolean;
7+
}
8+
9+
let config: DefaultConfig | null = null;
10+
11+
export function setDefaultConfig(defaultConfig: DefaultConfig): void {
12+
config = defaultConfig;
13+
}
14+
15+
export function getDefaultConfig(): DefaultConfig {
16+
if (!config) {
17+
throw new Error("defaultConfig has not been initialized");
18+
}
19+
return config;
20+
}

frontend/src/main.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import "vue-toastification/dist/index.css";
44
import "vue3-simple-typeahead/dist/vue3-simple-typeahead.css"; //Optional default CSS
55
import "./assets/main.css";
66
import "tippy.js/dist/tippy.css";
7+
import { setDefaultConfig } from "./defaultConfig";
78

89
async function conditionallyEnableMocking() {
910
if (process.env.NODE_ENV !== "dev-mocks") {
@@ -27,7 +28,7 @@ conditionallyEnableMocking()
2728
// eslint-disable-next-line promise/always-return
2829
if (response.ok) {
2930
const appConstants = await response.json();
30-
window.defaultConfig = appConstants;
31+
setDefaultConfig(appConstants);
3132
} else {
3233
console.error("Failed to load app constants");
3334
}

frontend/src/router/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { createRouter, createWebHashHistory, type RouteRecordRaw, RouteRecordSingleViewWithChildren } from "vue-router";
22
import config, { RouteItem } from "./config";
3+
import { getDefaultConfig } from "@/defaultConfig";
34

45
function meta(item: { title: string }) {
56
return { title: `${item.title} • ServicePulse` };
@@ -43,7 +44,7 @@ export default function makeRouter() {
4344
return result;
4445
});
4546

46-
const defaultRoute = window.defaultConfig.default_route;
47+
const defaultRoute = getDefaultConfig().default_route;
4748
if (!!defaultRoute && defaultRoute !== "/") {
4849
routes.push({
4950
path: "/",

frontend/src/stores/EnvironmentAndVersionsStore.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { useMemoize } from "@vueuse/core";
55
import { acceptHMRUpdate, defineStore } from "pinia";
66
import { computed, reactive } from "vue";
77
import { useServiceControlStore } from "./ServiceControlStore";
8+
import { getDefaultConfig } from "@/defaultConfig";
89

910
export const useEnvironmentAndVersionsStore = defineStore("EnvironmentAndVersionsStore", () => {
1011
const serviceControlStore = useServiceControlStore();
@@ -14,7 +15,7 @@ export const useEnvironmentAndVersionsStore = defineStore("EnvironmentAndVersion
1415
sc_version: "",
1516
minimum_supported_sc_version: "6.6.0",
1617
is_compatible_with_sc: true,
17-
sp_version: window.defaultConfig && window.defaultConfig.version ? window.defaultConfig.version : "1.2.0",
18+
sp_version: getDefaultConfig().version,
1819
supportsArchiveGroups: false,
1920
endpoints_error_url: "",
2021
known_endpoints_url: "",

frontend/src/stores/ServiceControlStore.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { acceptHMRUpdate, defineStore } from "pinia";
22
import { computed, ref } from "vue";
3+
import { getDefaultConfig } from "@/defaultConfig";
34

45
export const useServiceControlStore = defineStore("ServiceControlStore", () => {
56
const serviceControlUrl = ref<string | null>();
@@ -26,10 +27,11 @@ export const useServiceControlStore = defineStore("ServiceControlStore", () => {
2627
function refresh() {
2728
const params = new URLSearchParams(window.location.search);
2829
const mu = params.get("mu");
30+
const config = getDefaultConfig();
2931

30-
if (window.defaultConfig && window.defaultConfig.service_control_url) {
31-
serviceControlUrl.value = window.defaultConfig.service_control_url;
32-
console.debug(`setting ServiceControl Url to its default value: ${window.defaultConfig.service_control_url}`);
32+
if (config.service_control_url) {
33+
serviceControlUrl.value = config.service_control_url;
34+
console.debug(`setting ServiceControl Url to its default value: ${config.service_control_url}`);
3335
} else {
3436
console.warn("ServiceControl Url is not defined.");
3537
}
@@ -41,9 +43,9 @@ export const useServiceControlStore = defineStore("ServiceControlStore", () => {
4143
} else if (window.localStorage.getItem("mu")) {
4244
monitoringUrl.value = window.localStorage.getItem("mu");
4345
console.debug(`Monitoring Url, not in QS, found in local storage: ${monitoringUrl.value}`);
44-
} else if (window.defaultConfig && window.defaultConfig.monitoring_urls && window.defaultConfig.monitoring_urls.length) {
45-
monitoringUrl.value = window.defaultConfig.monitoring_urls[0];
46-
console.debug(`setting Monitoring Url to its default value: ${window.defaultConfig.monitoring_urls[0]}`);
46+
} else if (config.monitoring_url) {
47+
monitoringUrl.value = config.monitoring_url;
48+
console.debug(`setting Monitoring Url to its default value: ${config.monitoring_url}`);
4749
} else {
4850
console.warn("Monitoring Url is not defined.");
4951
}

frontend/src/views/FailedMessagesView.vue

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ import routeLinks from "@/router/routeLinks";
55
import isRouteSelected from "@/composables/isRouteSelected";
66
import { storeToRefs } from "pinia";
77
import useConnectionsAndStatsAutoRefresh from "@/composables/useConnectionsAndStatsAutoRefresh";
8+
import { getDefaultConfig } from "@/defaultConfig";
89
9-
const showPendingRetry = window.defaultConfig.showPendingRetry;
10+
const showPendingRetry = getDefaultConfig().showPendingRetry;
1011
const { store: connectionsAndStatsStore } = useConnectionsAndStatsAutoRefresh();
1112
connectionsAndStatsStore.requiresFullFailureDetails();
1213
const connectionState = connectionsAndStatsStore.connectionState;

frontend/src/views/ThroughputReportView.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import Toast from "vue-toastification";
1212
import { serviceControlWithThroughput } from "@/views/throughputreport/serviceControlWithThroughput";
1313
import { useServiceControlStore } from "@/stores/ServiceControlStore";
1414
import { setActivePinia } from "pinia";
15+
import { getDefaultConfig } from "@/defaultConfig";
1516

1617
describe("EndpointsView tests", () => {
1718
async function setup() {
@@ -100,7 +101,7 @@ describe("EndpointsView tests", () => {
100101
await renderComponent(Transport.AmazonSQS, async (driver) => {
101102
await driver.setUp(precondition.hasLicensingReportAvailable());
102103
await driver.setUp(precondition.hasLicensingEndpoints([{ name: "foo", is_known_endpoint: false, user_indicator: "something", max_daily_throughput: 0 }]));
103-
driver.mockEndpoint(`${window.defaultConfig.service_control_url}licensing/report/file`, {
104+
driver.mockEndpoint(`${getDefaultConfig().service_control_url}licensing/report/file`, {
104105
body: {},
105106
headers: {
106107
"Content-Disposition": `attachment; filename="${fileName}"`,

frontend/src/views/throughputreport/endpoints/DetectedListView.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { serviceControlWithThroughput } from "@/views/throughputreport/serviceCo
1313
import { flushPromises } from "@vue/test-utils";
1414
import { useServiceControlStore } from "@/stores/ServiceControlStore";
1515
import { setActivePinia } from "pinia";
16+
import { getDefaultConfig } from "@/defaultConfig";
1617
import { createTestingPinia } from "@pinia/testing";
1718

1819
describe("DetectedListView tests", () => {
@@ -283,7 +284,7 @@ describe("DetectedListView tests", () => {
283284
]
284285
) =>
285286
({ driver }: SetupFactoryOptions) => {
286-
driver.mockEndpoint(`${window.defaultConfig.service_control_url}licensing/endpoints/update`, {
287+
driver.mockEndpoint(`${getDefaultConfig().service_control_url}licensing/endpoints/update`, {
287288
body,
288289
method: "post",
289290
status: 200,

frontend/src/views/throughputreport/setup/MasksView.spec.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { flushPromises } from "@vue/test-utils";
99
import { createTestingPinia } from "@pinia/testing";
1010
import { useServiceControlStore } from "@/stores/ServiceControlStore";
1111
import { setActivePinia } from "pinia";
12+
import { getDefaultConfig } from "@/defaultConfig";
1213

1314
describe("MaskView tests", () => {
1415
async function setup() {
@@ -26,7 +27,7 @@ describe("MaskView tests", () => {
2627

2728
async function renderComponent(body: string[] = []) {
2829
const driver = await setup();
29-
driver.mockEndpoint(`${window.defaultConfig.service_control_url}licensing/settings/masks`, { body });
30+
driver.mockEndpoint(`${getDefaultConfig().service_control_url}licensing/settings/masks`, { body });
3031
setActivePinia(createTestingPinia({ stubActions: false }));
3132

3233
useServiceControlStore();
@@ -68,7 +69,7 @@ describe("MaskView tests", () => {
6869
const use = userEvent.setup();
6970
await use.type(getTextAreaElement(), "\nthree\nfour\nfive");
7071

71-
driver.mockEndpoint(`${window.defaultConfig.service_control_url}licensing/settings/masks/update`, { body: undefined, method: "post" });
72+
driver.mockEndpoint(`${getDefaultConfig().service_control_url}licensing/settings/masks/update`, { body: undefined, method: "post" });
7273
await use.click(screen.getByRole("button", { name: /Save/i }));
7374

7475
expect(screen.queryAllByText(/Masks Saved/i).length).toBeGreaterThanOrEqual(1);

0 commit comments

Comments
 (0)