1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00
lens/packages/kube-object/src/kube-status.ts
Sebastian Malton 1bf24db797 chore: Extract @k8slens/kube-object package
Signed-off-by: Sebastian Malton <sebastian@malton.name>
2023-05-02 10:11:52 -07:00

54 lines
1.5 KiB
TypeScript

/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { isObject, hasTypedProperty, hasOptionalTypedProperty, isString, isNumber } from "@k8slens/utilities";
import type { KubeStatusData } from "./api-types";
/**
* Is the shape of {@link object} correct for {@link KubeStatusData}
* @param object Some object
* @returns
*/
export function isKubeStatusData(object: unknown): object is KubeStatusData {
return (
isObject(object) &&
hasTypedProperty(object, "kind", isString) &&
hasTypedProperty(object, "apiVersion", isString) &&
hasTypedProperty(object, "code", isNumber) &&
(hasOptionalTypedProperty(object, "message", isString) ||
hasOptionalTypedProperty(object, "reason", isString) ||
hasOptionalTypedProperty(object, "status", isString)) &&
object.kind === "Status"
);
}
export class KubeStatus {
public readonly kind = "Status";
public readonly apiVersion: string;
public readonly code: number;
public readonly message: string;
public readonly reason: string;
public readonly status: string;
constructor(data: KubeStatusData) {
this.apiVersion = data.apiVersion;
this.code = data.code;
this.message = data.message || "";
this.reason = data.reason || "";
this.status = data.status || "";
}
getExplanation(): string {
const { code, message, reason, status } = this;
return `${code}: ${message || reason || status}`;
}
}