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 { Fetch } from "../fetch/fetch.injectable";
|
||||
import type { Defaulted } from "../utils";
|
||||
import { json } from "../utils";
|
||||
|
||||
export interface JsonApiData {}
|
||||
|
||||
@ -194,7 +193,7 @@ export class JsonApi<Data = JsonApiData, Params extends JsonApiParams<Data> = Js
|
||||
let data: any;
|
||||
|
||||
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) {
|
||||
data = text;
|
||||
}
|
||||
|
||||
@ -7,7 +7,7 @@
|
||||
|
||||
import moment from "moment";
|
||||
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 { Patch } from "rfc6902";
|
||||
import assert from "assert";
|
||||
@ -624,7 +624,7 @@ export class KubeObject<
|
||||
}
|
||||
|
||||
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.
|
||||
*/
|
||||
|
||||
import type { JsonValue } from "type-fest";
|
||||
import type { Result } from "./result";
|
||||
|
||||
export function parse(input: string): JsonValue {
|
||||
return JSON.parse(input);
|
||||
export interface JsonParseError {
|
||||
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
|
||||
import tar from "tar";
|
||||
import path from "path";
|
||||
import { parse } from "./json";
|
||||
import type { JsonValue } from "type-fest";
|
||||
|
||||
export type ReadFileFromTarOpts<ParseJson extends boolean> = {
|
||||
@ -43,7 +42,7 @@ export function readFileFromTar<ParseJson extends boolean>({ tarPath, filePath,
|
||||
});
|
||||
entry.once("end", () => {
|
||||
const data = Buffer.concat(fileChunks);
|
||||
const result = parseJson ? parse(data.toString("utf8")) : data;
|
||||
const result = parseJson ? JSON.parse(data.toString("utf8")) : data;
|
||||
|
||||
resolve(result);
|
||||
});
|
||||
|
||||
@ -4,7 +4,6 @@
|
||||
*/
|
||||
import { getInjectable } from "@ogre-tools/injectable";
|
||||
import type { JsonObject } from "type-fest";
|
||||
import { json } from "../../../../../common/utils";
|
||||
import yaml from "js-yaml";
|
||||
import execFileWithInputInjectable from "./exec-file-with-input/exec-file-with-input.injectable";
|
||||
import { getErrorMessage } from "../../../../../common/utils/get-error-message";
|
||||
@ -60,7 +59,7 @@ const callForKubeResourcesByManifestInjectable = getInjectable({
|
||||
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;
|
||||
};
|
||||
|
||||
@ -22,7 +22,7 @@ const getHelmReleaseInjectable = getInjectable({
|
||||
|
||||
logger.debug("Fetch release");
|
||||
|
||||
const result = await execHelm([
|
||||
const helmResult = await execHelm([
|
||||
"status",
|
||||
releaseName,
|
||||
"--namespace",
|
||||
@ -33,15 +33,21 @@ const getHelmReleaseInjectable = getInjectable({
|
||||
"json",
|
||||
]);
|
||||
|
||||
if (!result.callWasSuccessful) {
|
||||
logger.warn(`Failed to exectute helm: ${result.error}`);
|
||||
if (!helmResult.callWasSuccessful) {
|
||||
logger.warn(`Failed to exectute helm: ${helmResult.error}`);
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
@ -58,7 +64,7 @@ const getHelmReleaseInjectable = getInjectable({
|
||||
}
|
||||
|
||||
return {
|
||||
...release,
|
||||
...value,
|
||||
resources: resourcesResult.response,
|
||||
};
|
||||
};
|
||||
|
||||
@ -4,7 +4,7 @@
|
||||
*/
|
||||
import { getInjectable } from "@ogre-tools/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>[]>;
|
||||
|
||||
@ -28,13 +28,13 @@ const listHelmReleasesInjectable = getInjectable({
|
||||
|
||||
args.push("--kubeconfig", pathToKubeconfig);
|
||||
|
||||
const result = await execHelm(args);
|
||||
const helmResult = await execHelm(args);
|
||||
|
||||
if (!result.callWasSuccessful) {
|
||||
throw result.error;
|
||||
if (!helmResult.callWasSuccessful) {
|
||||
throw helmResult.error;
|
||||
}
|
||||
|
||||
const output = json.parse(result.response);
|
||||
const output = JSON.parse(helmResult.response);
|
||||
|
||||
if (!Array.isArray(output) || output.length == 0) {
|
||||
return [];
|
||||
|
||||
Loading…
Reference in New Issue
Block a user