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

remove the explicit throw is new parseApi

Signed-off-by: Sebastian Malton <smalton@mirantis.com>
This commit is contained in:
Sebastian Malton 2020-06-18 10:37:10 -04:00
parent d55803d0c7
commit 1107ff3e43
3 changed files with 21 additions and 18 deletions

View File

@ -45,10 +45,23 @@ const tests: ParseAPITest[] = [
namespace: undefined,
},
},
{
url: "/apis/namespaces/default/es/gke-svc-vodka-1-app-1-w-l-1-9eea2bfe-4jnl",
expected: {
apiBase: "/apis",
apiPrefix: "/apis",
apiGroup: undefined,
apiVersion: undefined,
apiVersionWithGroup: "",
resource: undefined,
name: undefined,
namespace: undefined,
},
},
];
jest.mock('../kube-watch-api.ts', () => 'KubeWatchApi');
describe("parseAPI unit tests", () => {
describe("parseApi unit tests", () => {
for (const i in tests) {
const { url: tUrl, expected:tExpect} = tests[i];
test(`test #${parseInt(i)+1}`, () => {

View File

@ -18,23 +18,17 @@ export class ApiManager {
private views = observable.map<KubeApi, ApiComponents>();
getApi(pathOrCallback: string | ((api: KubeApi) => boolean)) {
const apis = this.apis;
if (typeof pathOrCallback === "string") {
let api = apis.get(pathOrCallback);
if (!api) {
const { apiBase } = KubeApi.parseApi(pathOrCallback);
api = apis.get(apiBase);
}
return api;
}
else {
return Array.from(apis.values()).find(pathOrCallback);
return this.apis.get(pathOrCallback) || this.apis.get(KubeApi.parseApi(pathOrCallback).apiBase);
}
return Array.from(this.apis.values()).find(pathOrCallback);
}
registerApi(apiBase: string, api: KubeApi) {
if (this.apis.has(apiBase)) return;
this.apis.set(apiBase, api);
if (!this.apis.has(apiBase)) {
this.apis.set(apiBase, api);
}
}
protected resolveApi(api: string | KubeApi): KubeApi {

View File

@ -48,11 +48,7 @@ export class KubeApi<T extends KubeObject = any> {
const [left, right, found] = split(parts, "namespaces");
let apiGroup, apiVersion, namespace, resource, name;
if (found) {
if (left.length == 0) {
throw new Error(`invalid apiPath: ${apiPath}`)
}
if (found && left.length > 0) {
apiVersion = left.pop();
apiGroup = left.join("/");
[namespace, resource, name] = right;