diff --git a/src/common/k8s-api/api-base.ts b/src/common/k8s-api/api-base.ts index e511dd8454..8b7b5272be 100644 --- a/src/common/k8s-api/api-base.ts +++ b/src/common/k8s-api/api-base.ts @@ -3,38 +3,15 @@ * Licensed under MIT License. See LICENSE in root directory for more information. */ -import { JsonApi } from "./json-api"; -import { apiPrefix, isDebugging, isDevelopment } from "../vars"; -import { appEventBus } from "../app-event-bus/event-bus"; +import type { JsonApi } from "./json-api"; +import { getInjectionToken } from "@ogre-tools/injectable"; +import { asLegacyGlobalForExtensionApi } from "../../extensions/as-legacy-globals-for-extension-api/as-legacy-global-object-for-extension-api"; -export let apiBase: JsonApi; +export const apiBaseInjectionToken = getInjectionToken({ + id: "api-base-token", +}); -if (typeof window === "undefined") { - appEventBus.addListener((event) => { - if (event.name !== "lens-proxy" && event.action !== "listen") return; - - const params = event.params as { port?: number }; - - if (!params.port) return; - - apiBase = new JsonApi({ - serverAddress: `http://127.0.0.1:${params.port}`, - apiBase: apiPrefix, - debug: isDevelopment || isDebugging, - }, { - headers: { - "Host": `localhost:${params.port}`, - }, - }); - }); -} else { - apiBase = new JsonApi({ - serverAddress: `http://127.0.0.1:${window.location.port}`, - apiBase: apiPrefix, - debug: isDevelopment || isDebugging, - }, { - headers: { - "Host": window.location.host, - }, - }); -} +/** + * @deprecated switch to using apiBaseInjectionToken instead + */ +export const apiBase = asLegacyGlobalForExtensionApi(apiBaseInjectionToken); diff --git a/src/common/k8s-api/api-kube.ts b/src/common/k8s-api/api-kube.ts index 4154ec2f00..51b04e6cfc 100644 --- a/src/common/k8s-api/api-kube.ts +++ b/src/common/k8s-api/api-kube.ts @@ -11,4 +11,7 @@ export const apiKubeInjectionToken = getInjectionToken({ id: "api-kube-injection-token", }); +/** + * @deprecated Switch to using apiKubeInjectionToken instead + */ export const apiKube = asLegacyGlobalForExtensionApi(apiKubeInjectionToken); diff --git a/src/main/k8s/api-base.injectable.ts b/src/main/k8s/api-base.injectable.ts new file mode 100644 index 0000000000..99b4cb9729 --- /dev/null +++ b/src/main/k8s/api-base.injectable.ts @@ -0,0 +1,30 @@ +/** + * 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"; +import { apiBaseInjectionToken } from "../../common/k8s-api/api-base"; +import { JsonApi } from "../../common/k8s-api/json-api"; +import { apiPrefix, isDebugging } from "../../common/vars"; +import isDevelopmentInjectable from "../../common/vars/is-development.injectable"; +import lensProxyPortInjectable from "../lens-proxy/lens-proxy-port.injectable"; + +const apiBaseInjectable = getInjectable({ + id: "api-base", + instantiate: (di) => { + const proxyPort = di.inject(lensProxyPortInjectable); + + return new JsonApi({ + serverAddress: `http://127.0.0.1:${proxyPort.get()}`, + apiBase: apiPrefix, + debug: di.inject(isDevelopmentInjectable) || isDebugging, + }, { + headers: { + "Host": `localhost:${proxyPort.get()}`, + }, + }); + }, + injectionToken: apiBaseInjectionToken, +}); + +export default apiBaseInjectable; diff --git a/src/renderer/api/index.ts b/src/renderer/api/index.ts index 379d1e292b..897a452d03 100644 --- a/src/renderer/api/index.ts +++ b/src/renderer/api/index.ts @@ -3,4 +3,9 @@ * Licensed under MIT License. See LICENSE in root directory for more information. */ -export { apiBase, apiKube } from "../../common/k8s-api"; +import { apiBaseInjectionToken } from "../../common/k8s-api/api-base"; +import { apiKubeInjectionToken } from "../../common/k8s-api/api-kube"; +import { asLegacyGlobalForExtensionApi } from "../../extensions/as-legacy-globals-for-extension-api/as-legacy-global-object-for-extension-api"; + +export const apiBase = asLegacyGlobalForExtensionApi(apiBaseInjectionToken); +export const apiKube = asLegacyGlobalForExtensionApi(apiKubeInjectionToken); diff --git a/src/renderer/k8s/api-base.injectable.ts b/src/renderer/k8s/api-base.injectable.ts new file mode 100644 index 0000000000..f48a1d8fa2 --- /dev/null +++ b/src/renderer/k8s/api-base.injectable.ts @@ -0,0 +1,25 @@ +/** + * 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"; +import { apiBaseInjectionToken } from "../../common/k8s-api/api-base"; +import { JsonApi } from "../../common/k8s-api/json-api"; +import { apiPrefix, isDebugging } from "../../common/vars"; +import isDevelopmentInjectable from "../../common/vars/is-development.injectable"; + +const apiBaseInjectable = getInjectable({ + id: "api-base", + instantiate: (di) => new JsonApi({ + serverAddress: `http://127.0.0.1:${window.location.port}`, + apiBase: apiPrefix, + debug: di.inject(isDevelopmentInjectable) || isDebugging, + }, { + headers: { + "Host": window.location.host, + }, + }), + injectionToken: apiBaseInjectionToken, +}); + +export default apiBaseInjectable;