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

refactor to better name

Signed-off-by: Sebastian Malton <smalton@mirantis.com>
This commit is contained in:
Sebastian Malton 2020-06-22 11:03:37 -04:00
parent f68b96efe3
commit e20d2f361b

View File

@ -41,16 +41,31 @@ export interface IKubeApiLinkBase extends IKubeApiLinkRef {
} }
export class KubeApi<T extends KubeObject = any> { export class KubeApi<T extends KubeObject = any> {
static parseApi(apiPath = "") { static parseApi(apiPath = ""): IKubeApiLinkBase {
apiPath = new URL(apiPath, location.origin).pathname; apiPath = new URL(apiPath, location.origin).pathname;
const [, prefix, ...parts] = apiPath.split("/"); const [, prefix, ...parts] = apiPath.split("/");
const apiPrefix = `/${prefix}`; const apiPrefix = `/${prefix}`;
const [left, right, found] = split(parts, "namespaces"); const [left, right, namespaced] = split(parts, "namespaces");
let apiGroup, apiVersion, namespace, resource, name; let apiGroup, apiVersion, namespace, resource, name;
if (!found) { if (namespaced) {
switch (right.length) {
case 0:
resource = "namespaces"; // special case this due to `split` removing namespaces
break;
case 1:
resource = right[0];
break;
default:
[namespace, resource, name] = right;
break;
}
apiVersion = left.pop();
apiGroup = left.join("/");
} else {
switch (left.length) { switch (left.length) {
case 2: case 2:
resource = left.pop(); resource = left.pop();
@ -85,21 +100,6 @@ export class KubeApi<T extends KubeObject = any> {
} }
break; break;
} }
} else {
switch (right.length) {
case 0:
resource = "namespaces"; // special case this due to `split` removing namespaces
break;
case 1:
resource = right[0];
break;
default:
[namespace, resource, name] = right;
break;
}
apiVersion = left.pop();
apiGroup = left.join("/");
} }
const apiVersionWithGroup = [apiGroup, apiVersion].filter(v => v).join("/"); const apiVersionWithGroup = [apiGroup, apiVersion].filter(v => v).join("/");