1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

Register alternate API base URL if preferred version changes.

Signed-off-by: Trevor Nichols <trevor@nichols.id.au>
This commit is contained in:
Trevor Nichols 2020-09-03 08:39:56 +10:00
parent 42f352b73a
commit 23f38984f7

View File

@ -1,16 +1,35 @@
import { stringify } from "querystring"; import { stringify } from "querystring";
import { KubeObject } from "./kube-object"; import { KubeObject } from "./kube-object";
import { createKubeApiURL } from "./kube-api-parse"; import { createKubeApiURL } from "./kube-api-parse";
import { KubeApi, IKubeApiQueryParams } from "./kube-api"; import { KubeApi, IKubeApiQueryParams, IKubeApiOptions } from "./kube-api";
import { apiManager } from "./api-manager";
export class VersionedKubeApi<T extends KubeObject = any> extends KubeApi<T> { export class VersionedKubeApi<T extends KubeObject = any> extends KubeApi<T> {
private preferredVersion?: string; private preferredVersion?: string;
constructor(opts: IKubeApiOptions<T>) {
super(opts);
this.getPreferredVersion().then(() => {
if (this.apiBase != opts.apiBase)
apiManager.registerApi(this.apiBase, this);
});
}
// override this property to make read-write
apiBase: string
async getPreferredVersion() { async getPreferredVersion() {
if (this.preferredVersion) return; if (this.preferredVersion) return;
const apiGroupVersion = await this.request.get<{ preferredVersion: { version: string; }; }>(`${this.apiPrefix}/${this.apiGroup}`); const apiGroupVersion = await this.request.get<{ preferredVersion?: { version: string; }; }>(`${this.apiPrefix}/${this.apiGroup}`);
if (!apiGroupVersion?.preferredVersion) return;
this.preferredVersion = apiGroupVersion.preferredVersion.version; this.preferredVersion = apiGroupVersion.preferredVersion.version;
// update apiBase
this.apiBase = this.getUrl();
} }
async list({ namespace = "" } = {}, query?: IKubeApiQueryParams): Promise<T[]> { async list({ namespace = "" } = {}, query?: IKubeApiQueryParams): Promise<T[]> {