mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Improve our wrapper to JSON.parse
- Only use it in some places Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
parent
cb9d978e5a
commit
1213e844d8
@ -16,7 +16,6 @@ import { EventEmitter } from "../../common/event-emitter";
|
|||||||
import type { Logger } from "../../common/logger";
|
import type { Logger } from "../../common/logger";
|
||||||
import type { Fetch } from "../fetch/fetch.injectable";
|
import type { Fetch } from "../fetch/fetch.injectable";
|
||||||
import type { Defaulted } from "../utils";
|
import type { Defaulted } from "../utils";
|
||||||
import { json } from "../utils";
|
|
||||||
|
|
||||||
export interface JsonApiData {}
|
export interface JsonApiData {}
|
||||||
|
|
||||||
@ -194,7 +193,7 @@ export class JsonApi<Data = JsonApiData, Params extends JsonApiParams<Data> = Js
|
|||||||
let data: any;
|
let data: any;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
data = text ? json.parse(text) : ""; // DELETE-requests might not have response-body
|
data = text ? JSON.parse(text) : ""; // DELETE-requests might not have response-body
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
data = text;
|
data = text;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -7,7 +7,7 @@
|
|||||||
|
|
||||||
import moment from "moment";
|
import moment from "moment";
|
||||||
import type { KubeJsonApiData, KubeJsonApiDataList, KubeJsonApiListMetadata } from "./kube-json-api";
|
import type { KubeJsonApiData, KubeJsonApiDataList, KubeJsonApiListMetadata } from "./kube-json-api";
|
||||||
import { autoBind, formatDuration, hasOptionalTypedProperty, hasTypedProperty, isObject, isString, isNumber, bindPredicate, isTypedArray, isRecord, json } from "../utils";
|
import { autoBind, formatDuration, hasOptionalTypedProperty, hasTypedProperty, isObject, isString, isNumber, bindPredicate, isTypedArray, isRecord } from "../utils";
|
||||||
import type { ItemObject } from "../item.store";
|
import type { ItemObject } from "../item.store";
|
||||||
import type { Patch } from "rfc6902";
|
import type { Patch } from "rfc6902";
|
||||||
import assert from "assert";
|
import assert from "assert";
|
||||||
@ -624,7 +624,7 @@ export class KubeObject<
|
|||||||
}
|
}
|
||||||
|
|
||||||
toPlainObject() {
|
toPlainObject() {
|
||||||
return json.parse(JSON.stringify(this)) as JsonObject;
|
return JSON.parse(JSON.stringify(this)) as JsonObject;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@ -3,8 +3,26 @@
|
|||||||
* 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 type { JsonValue } from "type-fest";
|
import type { Result } from "./result";
|
||||||
|
|
||||||
export function parse(input: string): JsonValue {
|
export interface JsonParseError {
|
||||||
return JSON.parse(input);
|
cause: SyntaxError;
|
||||||
|
text: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function parse(text: string): Result<unknown, JsonParseError> {
|
||||||
|
try {
|
||||||
|
return {
|
||||||
|
isOk: true,
|
||||||
|
value: JSON.parse(text),
|
||||||
|
};
|
||||||
|
} catch (error) {
|
||||||
|
return {
|
||||||
|
isOk: false,
|
||||||
|
error: {
|
||||||
|
cause: error as SyntaxError,
|
||||||
|
text,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -7,7 +7,6 @@
|
|||||||
// Docs: https://github.com/npm/node-tar
|
// Docs: https://github.com/npm/node-tar
|
||||||
import tar from "tar";
|
import tar from "tar";
|
||||||
import path from "path";
|
import path from "path";
|
||||||
import { parse } from "./json";
|
|
||||||
import type { JsonValue } from "type-fest";
|
import type { JsonValue } from "type-fest";
|
||||||
|
|
||||||
export type ReadFileFromTarOpts<ParseJson extends boolean> = {
|
export type ReadFileFromTarOpts<ParseJson extends boolean> = {
|
||||||
@ -43,7 +42,7 @@ export function readFileFromTar<ParseJson extends boolean>({ tarPath, filePath,
|
|||||||
});
|
});
|
||||||
entry.once("end", () => {
|
entry.once("end", () => {
|
||||||
const data = Buffer.concat(fileChunks);
|
const data = Buffer.concat(fileChunks);
|
||||||
const result = parseJson ? parse(data.toString("utf8")) : data;
|
const result = parseJson ? JSON.parse(data.toString("utf8")) : data;
|
||||||
|
|
||||||
resolve(result);
|
resolve(result);
|
||||||
});
|
});
|
||||||
|
|||||||
@ -4,7 +4,6 @@
|
|||||||
*/
|
*/
|
||||||
import { getInjectable } from "@ogre-tools/injectable";
|
import { getInjectable } from "@ogre-tools/injectable";
|
||||||
import type { JsonObject } from "type-fest";
|
import type { JsonObject } from "type-fest";
|
||||||
import { json } from "../../../../../common/utils";
|
|
||||||
import yaml from "js-yaml";
|
import yaml from "js-yaml";
|
||||||
import execFileWithInputInjectable from "./exec-file-with-input/exec-file-with-input.injectable";
|
import execFileWithInputInjectable from "./exec-file-with-input/exec-file-with-input.injectable";
|
||||||
import { getErrorMessage } from "../../../../../common/utils/get-error-message";
|
import { getErrorMessage } from "../../../../../common/utils/get-error-message";
|
||||||
@ -60,7 +59,7 @@ const callForKubeResourcesByManifestInjectable = getInjectable({
|
|||||||
throw new Error(errorMessage);
|
throw new Error(errorMessage);
|
||||||
}
|
}
|
||||||
|
|
||||||
const output = json.parse(result.response) as { items: JsonObject[] };
|
const output = JSON.parse(result.response) as { items: JsonObject[] };
|
||||||
|
|
||||||
return output.items;
|
return output.items;
|
||||||
};
|
};
|
||||||
|
|||||||
@ -22,7 +22,7 @@ const getHelmReleaseInjectable = getInjectable({
|
|||||||
|
|
||||||
logger.debug("Fetch release");
|
logger.debug("Fetch release");
|
||||||
|
|
||||||
const result = await execHelm([
|
const helmResult = await execHelm([
|
||||||
"status",
|
"status",
|
||||||
releaseName,
|
releaseName,
|
||||||
"--namespace",
|
"--namespace",
|
||||||
@ -33,15 +33,21 @@ const getHelmReleaseInjectable = getInjectable({
|
|||||||
"json",
|
"json",
|
||||||
]);
|
]);
|
||||||
|
|
||||||
if (!result.callWasSuccessful) {
|
if (!helmResult.callWasSuccessful) {
|
||||||
logger.warn(`Failed to exectute helm: ${result.error}`);
|
logger.warn(`Failed to exectute helm: ${helmResult.error}`);
|
||||||
|
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
const release = json.parse(result.response);
|
const { isOk, error, value } = json.parse(helmResult.response);
|
||||||
|
|
||||||
if (!isObject(release) || Array.isArray(release)) {
|
if (!isOk) {
|
||||||
|
logger.warn(`Failed to get helm release resources: ${error.cause}`);
|
||||||
|
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!isObject(value) || Array.isArray(value)) {
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -58,7 +64,7 @@ const getHelmReleaseInjectable = getInjectable({
|
|||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
...release,
|
...value,
|
||||||
resources: resourcesResult.response,
|
resources: resourcesResult.response,
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|||||||
@ -4,7 +4,7 @@
|
|||||||
*/
|
*/
|
||||||
import { getInjectable } from "@ogre-tools/injectable";
|
import { getInjectable } from "@ogre-tools/injectable";
|
||||||
import execHelmInjectable from "./exec-helm/exec-helm.injectable";
|
import execHelmInjectable from "./exec-helm/exec-helm.injectable";
|
||||||
import { isObject, json, toCamelCase } from "../../common/utils";
|
import { isObject, toCamelCase } from "../../common/utils";
|
||||||
|
|
||||||
export type ListHelmReleases = (pathToKubeconfig: string, namespace?: string) => Promise<Record<string, any>[]>;
|
export type ListHelmReleases = (pathToKubeconfig: string, namespace?: string) => Promise<Record<string, any>[]>;
|
||||||
|
|
||||||
@ -28,13 +28,13 @@ const listHelmReleasesInjectable = getInjectable({
|
|||||||
|
|
||||||
args.push("--kubeconfig", pathToKubeconfig);
|
args.push("--kubeconfig", pathToKubeconfig);
|
||||||
|
|
||||||
const result = await execHelm(args);
|
const helmResult = await execHelm(args);
|
||||||
|
|
||||||
if (!result.callWasSuccessful) {
|
if (!helmResult.callWasSuccessful) {
|
||||||
throw result.error;
|
throw helmResult.error;
|
||||||
}
|
}
|
||||||
|
|
||||||
const output = json.parse(result.response);
|
const output = JSON.parse(helmResult.response);
|
||||||
|
|
||||||
if (!Array.isArray(output) || output.length == 0) {
|
if (!Array.isArray(output) || output.length == 0) {
|
||||||
return [];
|
return [];
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user