Skip to content

Commit

Permalink
Use built-in json parsing
Browse files Browse the repository at this point in the history
Signed-off-by: Sebastian Malton <sebastian@malton.name>
  • Loading branch information
Nokel81 committed Oct 7, 2021
1 parent 21cc0f1 commit a3af5b9
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
8 changes: 6 additions & 2 deletions src/common/utils/app-version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,12 @@ export function getBundledKubectlVersion(): string {
return packageInfo.config.bundledKubectlVersion;
}

interface AppVersion {
version: string;
}

export async function getAppVersionFromProxyServer(proxyPort: number): Promise<string> {
const response = await got.get(`http://localhost:${proxyPort}/version`);
const { body } = await got<AppVersion>(`http://localhost:${proxyPort}/version`, { responseType: "json" });

return JSON.parse(response.body).version;
return body.version;
}
13 changes: 7 additions & 6 deletions src/main/k8s-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,24 @@ export async function k8sRequest<T = any>(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<string>(kubeProxyUrl + path, options);
const { body } = await got<T>(kubeProxyUrl + path, options);

return JSON.parse(res.body);
return body;
}

export async function getMetrics(cluster: Cluster, prometheusPath: string, queryParams: IMetricsReqParams & { query: string }): Promise<any> {
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<any>(url, {
form: queryParams,
headers: {
Host: `${cluster.id}.${new URL(kubeProxyUrl).host}`,
}
},
responseType: "json"
});

return JSON.parse(response.body);
return body;
}
5 changes: 3 additions & 2 deletions src/renderer/components/+preferences/helm-charts.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,12 @@ import got from "got";
import { orderBy } from "lodash";

async function loadAvailableHelmRepos(): Promise<HelmRepo[]> {
const { body } = await got.get("https://github.com/lensapp/artifact-hub-repositories/releases/download/latest/repositories.json", {
const { body } = await got.get<HelmRepo[]>("https://github.com/lensapp/artifact-hub-repositories/releases/download/latest/repositories.json", {
timeout: 10_000,
responseType: "json"
});

return orderBy<HelmRepo>(JSON.parse(body), repo => repo.name);
return orderBy(body, repo => repo.name);
}

@observer
Expand Down

0 comments on commit a3af5b9

Please sign in to comment.