From a3af5b9e46fa68198f38b9e759e1ad48d21a4547 Mon Sep 17 00:00:00 2001 From: Sebastian Malton Date: Thu, 7 Oct 2021 09:05:55 -0400 Subject: [PATCH] Use built-in json parsing Signed-off-by: Sebastian Malton --- src/common/utils/app-version.ts | 8 ++++++-- src/main/k8s-request.ts | 13 +++++++------ .../components/+preferences/helm-charts.tsx | 5 +++-- 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/src/common/utils/app-version.ts b/src/common/utils/app-version.ts index 908f1f25da804..c5f4d95c70b7e 100644 --- a/src/common/utils/app-version.ts +++ b/src/common/utils/app-version.ts @@ -30,8 +30,12 @@ export function getBundledKubectlVersion(): string { return packageInfo.config.bundledKubectlVersion; } +interface AppVersion { + version: string; +} + export async function getAppVersionFromProxyServer(proxyPort: number): Promise { - const response = await got.get(`http://localhost:${proxyPort}/version`); + const { body } = await got(`http://localhost:${proxyPort}/version`, { responseType: "json" }); - return JSON.parse(response.body).version; + return body.version; } diff --git a/src/main/k8s-request.ts b/src/main/k8s-request.ts index 3cd374dd0e325..7dac620c3d645 100644 --- a/src/main/k8s-request.ts +++ b/src/main/k8s-request.ts @@ -35,23 +35,24 @@ export async function k8sRequest(cluster: Cluster, path: string, option options.timeout ??= 30000; options.headers ??= {}; options.headers.Host = `${cluster.id}.${new URL(kubeProxyUrl).host}`; // required in ClusterManager.getClusterForRequest() + options.responseType = "json"; - const res = await got.get(kubeProxyUrl + path, options); + const { body } = await got(kubeProxyUrl + path, options); - return JSON.parse(res.body); + return body; } export async function getMetrics(cluster: Cluster, prometheusPath: string, queryParams: IMetricsReqParams & { query: string }): Promise { const prometheusPrefix = cluster.preferences.prometheus?.prefix || ""; const kubeProxyUrl = getKubeProxyUrl(); const url = `${kubeProxyUrl}/api/v1/namespaces/${prometheusPath}/proxy${prometheusPrefix}/api/v1/query_range`; - - const response = await got.post(url, { + const { body } = await got.post(url, { form: queryParams, headers: { Host: `${cluster.id}.${new URL(kubeProxyUrl).host}`, - } + }, + responseType: "json" }); - return JSON.parse(response.body); + return body; } diff --git a/src/renderer/components/+preferences/helm-charts.tsx b/src/renderer/components/+preferences/helm-charts.tsx index 0c40fd1f8b5f4..0fddc102ba0e0 100644 --- a/src/renderer/components/+preferences/helm-charts.tsx +++ b/src/renderer/components/+preferences/helm-charts.tsx @@ -38,11 +38,12 @@ import got from "got"; import { orderBy } from "lodash"; async function loadAvailableHelmRepos(): Promise { - const { body } = await got.get("https://github.com/lensapp/artifact-hub-repositories/releases/download/latest/repositories.json", { + const { body } = await got.get("https://github.com/lensapp/artifact-hub-repositories/releases/download/latest/repositories.json", { timeout: 10_000, + responseType: "json" }); - return orderBy(JSON.parse(body), repo => repo.name); + return orderBy(body, repo => repo.name); } @observer