diff --git a/src/common/k8s-api/window-location.injectable.ts b/src/common/k8s-api/window-location.injectable.ts new file mode 100644 index 0000000000..80bcd44be6 --- /dev/null +++ b/src/common/k8s-api/window-location.injectable.ts @@ -0,0 +1,17 @@ +/** + * Copyright (c) OpenLens Authors. All rights reserved. + * Licensed under MIT License. See LICENSE in root directory for more information. + */ +import { getInjectable } from "@ogre-tools/injectable"; + +const windowLocationInjectable = getInjectable({ + id: "window-location", + instantiate: () => { + const { host, port } = window.location; + + return { host, port }; + }, + causesSideEffects: true, +}); + +export default windowLocationInjectable; diff --git a/src/renderer/k8s/api-base.injectable.ts b/src/renderer/k8s/api-base.injectable.ts index 44f2e6831e..9ce5ab5f98 100644 --- a/src/renderer/k8s/api-base.injectable.ts +++ b/src/renderer/k8s/api-base.injectable.ts @@ -5,6 +5,7 @@ import { getInjectable } from "@ogre-tools/injectable"; import { apiBaseInjectionToken } from "../../common/k8s-api/api-base"; import createJsonApiInjectable from "../../common/k8s-api/create-json-api.injectable"; +import windowLocationInjectable from "../../common/k8s-api/window-location.injectable"; import { apiPrefix } from "../../common/vars"; import isDebuggingInjectable from "../../common/vars/is-debugging.injectable"; import isDevelopmentInjectable from "../../common/vars/is-development.injectable"; @@ -14,16 +15,17 @@ const apiBaseInjectable = getInjectable({ instantiate: (di) => { const createJsonApi = di.inject(createJsonApiInjectable); const isDebugging = di.inject(isDebuggingInjectable); + const { port, host } = di.inject(windowLocationInjectable); return createJsonApi( { - serverAddress: `http://127.0.0.1:${window.location.port}`, + serverAddress: `http://127.0.0.1:${port}`, apiBase: apiPrefix, debug: di.inject(isDevelopmentInjectable) || isDebugging, }, { headers: { - "Host": window.location.host, + "Host": host, }, }, ); diff --git a/src/renderer/k8s/api-kube.injectable.ts b/src/renderer/k8s/api-kube.injectable.ts index 335c130ff3..c7c87194a5 100644 --- a/src/renderer/k8s/api-kube.injectable.ts +++ b/src/renderer/k8s/api-kube.injectable.ts @@ -10,6 +10,7 @@ import { storesAndApisCanBeCreatedInjectionToken } from "../../common/k8s-api/st import createKubeJsonApiInjectable from "../../common/k8s-api/create-kube-json-api.injectable"; import isDevelopmentInjectable from "../../common/vars/is-development.injectable"; import showErrorNotificationInjectable from "../components/notifications/show-error-notification.injectable"; +import windowLocationInjectable from "../../common/k8s-api/window-location.injectable"; const apiKubeInjectable = getInjectable({ id: "api-kube", @@ -18,14 +19,15 @@ const apiKubeInjectable = getInjectable({ const createKubeJsonApi = di.inject(createKubeJsonApiInjectable); const isDevelopment = di.inject(isDevelopmentInjectable); const showErrorNotification = di.inject(showErrorNotificationInjectable); + const { port, host } = di.inject(windowLocationInjectable); const apiKube = createKubeJsonApi({ - serverAddress: `http://127.0.0.1:${window.location.port}`, + serverAddress: `http://127.0.0.1:${port}`, apiBase: apiKubePrefix, debug: isDevelopment, }, { headers: { - "Host": window.location.host, + "Host": host, }, });