mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Making apiBase injectable
Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
parent
ed073d6562
commit
2f57644801
@ -3,38 +3,15 @@
|
|||||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { JsonApi } from "./json-api";
|
import type { JsonApi } from "./json-api";
|
||||||
import { apiPrefix, isDebugging, isDevelopment } from "../vars";
|
import { getInjectionToken } from "@ogre-tools/injectable";
|
||||||
import { appEventBus } from "../app-event-bus/event-bus";
|
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<JsonApi>({
|
||||||
|
id: "api-base-token",
|
||||||
|
});
|
||||||
|
|
||||||
if (typeof window === "undefined") {
|
/**
|
||||||
appEventBus.addListener((event) => {
|
* @deprecated switch to using apiBaseInjectionToken instead
|
||||||
if (event.name !== "lens-proxy" && event.action !== "listen") return;
|
*/
|
||||||
|
export const apiBase = asLegacyGlobalForExtensionApi(apiBaseInjectionToken);
|
||||||
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,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|||||||
@ -11,4 +11,7 @@ export const apiKubeInjectionToken = getInjectionToken<KubeJsonApi>({
|
|||||||
id: "api-kube-injection-token",
|
id: "api-kube-injection-token",
|
||||||
});
|
});
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @deprecated Switch to using apiKubeInjectionToken instead
|
||||||
|
*/
|
||||||
export const apiKube = asLegacyGlobalForExtensionApi(apiKubeInjectionToken);
|
export const apiKube = asLegacyGlobalForExtensionApi(apiKubeInjectionToken);
|
||||||
|
|||||||
30
src/main/k8s/api-base.injectable.ts
Normal file
30
src/main/k8s/api-base.injectable.ts
Normal file
@ -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;
|
||||||
@ -3,4 +3,9 @@
|
|||||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
* 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);
|
||||||
|
|||||||
25
src/renderer/k8s/api-base.injectable.ts
Normal file
25
src/renderer/k8s/api-base.injectable.ts
Normal file
@ -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;
|
||||||
Loading…
Reference in New Issue
Block a user