Skip to content

Commit

Permalink
Refactor callApi into direct usage of apiService
Browse files Browse the repository at this point in the history
  • Loading branch information
Tamir198 committed Aug 7, 2023
1 parent 7f89fed commit f763db7
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 51 deletions.
11 changes: 6 additions & 5 deletions dashboard/src/API/k8s.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
import { type UseQueryOptions, useQuery } from "@tanstack/react-query";
import { callApi } from "./releases";
import { K8sResource, K8sResourceList, KubectlContexts } from "./interfaces";
import apiService from "./apiService";

// Get list of kubectl contexts configured locally
function useGetKubectlContexts(options?: UseQueryOptions<KubectlContexts>) {
return useQuery<KubectlContexts>(
["k8s", "contexts"],
() => callApi<KubectlContexts>("/api/k8s/contexts"),
() => apiService.fetchWithDefaults<KubectlContexts>("/api/k8s/contexts"),
options
);
}
Expand All @@ -22,7 +22,7 @@ function useGetK8sResource(
return useQuery<K8sResource>(
["k8s", kind, "get", name, namespace],
() =>
callApi<K8sResource>(
apiService.fetchWithDefaults<K8sResource>(
`/api/k8s/${kind}/get?name=${name}&namespace=${namespace}`
),
options
Expand All @@ -36,7 +36,8 @@ function useGetK8sResourceList(
) {
return useQuery<K8sResourceList>(
["k8s", kind, "list"],
() => callApi<K8sResourceList>(`/api/k8s/${kind}/list`),
() =>
apiService.fetchWithDefaults<K8sResourceList>(`/api/k8s/${kind}/list`),
options
);
}
Expand All @@ -51,7 +52,7 @@ function useGetK8sResourceDescribe(
return useQuery<string>(
["k8s", kind, "describe", name, namespace],
() =>
callApi<string>(
apiService.fetchWithDefaults<string>(
`/api/k8s/${kind}/describe?name=${name}&namespace=${namespace}`,
{
headers: {
Expand Down
6 changes: 3 additions & 3 deletions dashboard/src/API/other.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ import {
useMutation,
useQuery,
} from "@tanstack/react-query";
import { callApi } from "./releases";
import { ApplicationStatus } from "./interfaces";
import apiService from "./apiService";

// Shuts down the Helm Dashboard application
export function useShutdownHelmDashboard(
options?: UseMutationOptions<void, Error>
) {
return useMutation<void, Error>(
() =>
callApi("/", {
apiService.fetchWithDefaults("/", {
method: "DELETE",
}),
options
Expand All @@ -26,7 +26,7 @@ export function useGetApplicationStatus(
) {
return useQuery<ApplicationStatus>(
["status"],
() => callApi<ApplicationStatus>("/status"),
() => apiService.fetchWithDefaults<ApplicationStatus>("/status"),
{
...options,
}
Expand Down
53 changes: 25 additions & 28 deletions dashboard/src/API/releases.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export function useGetInstalledReleases(
return useQuery<Release[]>(
["installedReleases", context],
() =>
callApi<Release[]>("/api/helm/releases", {
apiService.fetchWithDefaults<Release[]>("/api/helm/releases", {
headers: {
"X-Kubecontext": context,
},
Expand All @@ -38,7 +38,9 @@ export function useGetReleaseManifest({
return useQuery<any>(

Check warning on line 38 in dashboard/src/API/releases.ts

View workflow job for this annotation

GitHub Actions / build

Unexpected any. Specify a different type

Check warning on line 38 in dashboard/src/API/releases.ts

View workflow job for this annotation

GitHub Actions / static_and_lint

Unexpected any. Specify a different type
["manifest", namespace, chartName],
() =>
callApi<any>(`/api/helm/releases/${namespace}/${chartName}/manifests`),
apiService.fetchWithDefaults<any>(

Check warning on line 41 in dashboard/src/API/releases.ts

View workflow job for this annotation

GitHub Actions / build

Unexpected any. Specify a different type

Check warning on line 41 in dashboard/src/API/releases.ts

View workflow job for this annotation

GitHub Actions / static_and_lint

Unexpected any. Specify a different type
`/api/helm/releases/${namespace}/${chartName}/manifests`
),
options
);
}
Expand All @@ -52,7 +54,7 @@ export function useGetResources(
const { data, ...rest } = useQuery<StructuredResources[]>(
["resources", ns, name],
() =>
callApi<StructuredResources[]>(
apiService.fetchWithDefaults<StructuredResources[]>(
`/api/helm/releases/${ns}/${name}/resources?health=true`
),
options
Expand Down Expand Up @@ -89,7 +91,7 @@ export function useGetResourceDescription(
return useQuery<string>(
["describe", type, ns, name],
() =>
callApi<string>(
apiService.fetchWithDefaults<string>(
`/api/k8s/${type}/describe?name=${name}&namespace=${ns}`,
{
headers: { "Content-Type": "text/plain; charset=utf-8" },
Expand All @@ -105,7 +107,7 @@ export function useGetLatestVersion(
return useQuery<ChartVersion[]>(
["latestver", chartName],
() =>
callApi<ChartVersion[]>(
apiService.fetchWithDefaults<ChartVersion[]>(
`/api/helm/repositories/latestver?name=${chartName}`
),
options
Expand All @@ -118,7 +120,7 @@ export function useGetVersions(
return useQuery<LatestChartVersion[]>(
["versions", chartName],
() =>
callApi<LatestChartVersion[]>(
apiService.fetchWithDefaults<LatestChartVersion[]>(
`/api/helm/repositories/versions?name=${chartName}`
),
options
Expand All @@ -134,7 +136,7 @@ export function useGetReleaseInfoByType(
return useQuery<string>(
[tab, namespace, chart, revision, additionalParams],
() =>
callApi<string>(
apiService.fetchWithDefaults<string>(
`/api/helm/releases/${namespace}/${chart}/${tab}?revision=${revision}${additionalParams}`,
{
headers: { "Content-Type": "text/plain; charset=utf-8" },
Expand All @@ -151,7 +153,7 @@ export function useGetDiff(
return useQuery<string>(
["diff", formData],
() => {
return callApi<string>("/diff", {
return apiService.fetchWithDefaults<string>("/diff", {
body: formData,

method: "POST",
Expand All @@ -177,10 +179,13 @@ export function useRollbackRelease(
const formData = new FormData();
formData.append("revision", revision.toString());

return callApi<void>(`/api/helm/releases/${ns}/${name}/rollback`, {
method: "POST",
body: formData,
});
return apiService.fetchWithDefaults<void>(
`/api/helm/releases/${ns}/${name}/rollback`,
{
method: "POST",
body: formData,
}
);
}, options);
}

Expand All @@ -190,9 +195,12 @@ export function useTestRelease(
) {
return useMutation<void, unknown, { ns: string; name: string }>(
({ ns, name }) => {
return callApi<void>(`/api/helm/releases/${ns}/${name}/test`, {
method: "POST",
});
return apiService.fetchWithDefaults<void>(
`/api/helm/releases/${ns}/${name}/test`,
{
method: "POST",
}
);
},
options
);
Expand All @@ -216,7 +224,7 @@ export function useChartReleaseValues({
return useQuery<any>(

Check warning on line 224 in dashboard/src/API/releases.ts

View workflow job for this annotation

GitHub Actions / build

Unexpected any. Specify a different type

Check warning on line 224 in dashboard/src/API/releases.ts

View workflow job for this annotation

GitHub Actions / static_and_lint

Unexpected any. Specify a different type
["values", namespace, release, userDefinedValue, version],
() =>
callApi<any>(
apiService.fetchWithDefaults<any>(

Check warning on line 227 in dashboard/src/API/releases.ts

View workflow job for this annotation

GitHub Actions / build

Unexpected any. Specify a different type

Check warning on line 227 in dashboard/src/API/releases.ts

View workflow job for this annotation

GitHub Actions / static_and_lint

Unexpected any. Specify a different type
`/api/helm/releases/${namespace}/${release}/values?${"userDefined=true"}${
revision ? `&revision=${revision}` : ""
}`,
Expand Down Expand Up @@ -270,7 +278,7 @@ export const useVersionData = ({
namespace ? namespace : "[empty]"
}${`/${releaseName}`}`;

const data = await callApi(fetchUrl, {
const data = await apiService.fetchWithDefaults(fetchUrl, {
method: "post",
body: formData,
});
Expand Down Expand Up @@ -326,14 +334,3 @@ export interface Condition {
reason: string;
message: string;
}

// TODO: there is no need at this anymore, we can use apiService.fetchWithDefaults directly
// so this function can be removed
export async function callApi<T>(
url: string,
options?: RequestInit
): Promise<T> {
const data = await apiService.fetchWithDefaults(url, options);

return data;
}
10 changes: 5 additions & 5 deletions dashboard/src/API/repositories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ import {
useMutation,
useQuery,
} from "@tanstack/react-query";
import { callApi } from "./releases";
import { HelmRepositories } from "./interfaces";
import apiService from "./apiService";

// Get list of Helm repositories
export function useGetRepositories(
options?: UseQueryOptions<HelmRepositories>
) {
return useQuery<HelmRepositories>(
["helm", "repositories"],
() => callApi<HelmRepositories>("/api/helm/repositories"),
() => apiService.fetchWithDefaults<HelmRepositories>("/api/helm/repositories"),
options
);
}
Expand All @@ -24,7 +24,7 @@ export function useUpdateRepo(
options?: UseMutationOptions<void, unknown, void>
) {
return useMutation<void, unknown, void>(() => {
return callApi<void>(`/api/helm/repositories/${repo}`, {
return apiService.fetchWithDefaults<void>(`/api/helm/repositories/${repo}`, {
method: "POST",
});
}, options);
Expand All @@ -36,7 +36,7 @@ export function useDeleteRepo(
options?: UseMutationOptions<void, unknown, void>
) {
return useMutation<void, unknown, void>(() => {
return callApi<void>(`/api/helm/repositories/${repo}`, {
return apiService.fetchWithDefaults<void>(`/api/helm/repositories/${repo}`, {
method: "DELETE",
});
}, options);
Expand All @@ -52,7 +52,7 @@ export function useChartRepoValues({
return useQuery<any>(
["helm", "repositories", "values", chart, version],
() =>
callApi<any>(
apiService.fetchWithDefaults<any>(
`/api/helm/repositories/values?chart=${chart}&version=${version}`,
{
headers: { "Content-Type": "text/plain; charset=utf-8" },
Expand Down
8 changes: 4 additions & 4 deletions dashboard/src/API/scanners.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ import {
useMutation,
useQuery,
} from "@tanstack/react-query";
import { callApi } from "./releases";
import { ScanResult, ScanResults, ScannersList } from "./interfaces";
import apiService from "./apiService";

// Get list of discovered scanners
function useGetDiscoveredScanners(options?: UseQueryOptions<ScannersList>) {
return useQuery<ScannersList>(
["scanners"],
() => callApi<ScannersList>("/api/scanners"),
() => apiService.fetchWithDefaults<ScannersList>("/api/scanners"),
options
);
}
Expand All @@ -28,7 +28,7 @@ function useScanManifests(
formData.append("manifest", manifest);
return useMutation<ScanResults, Error, string>(
() =>
callApi<ScanResults>("/api/scanners/manifests", {
apiService.fetchWithDefaults<ScanResults>("/api/scanners/manifests", {
method: "POST",
body: formData,
}),
Expand All @@ -46,7 +46,7 @@ function useScanK8sResource(
return useQuery<ScanResults>(
["scanners", "resource", kind, namespace, name],
() =>
callApi<ScanResults>(
apiService.fetchWithDefaults<ScanResults>(
`/api/scanners/resource/${kind}?namespace=${namespace}&name=${name}`
),
options
Expand Down
4 changes: 2 additions & 2 deletions dashboard/src/components/modal/AddRepositoryModal.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { useEffect, useState } from "react";
import Modal from "./Modal";
import { callApi } from "../../API/releases";
import Spinner from "../Spinner";
import useAlertError from "../../hooks/useAlertError";
import useCustomSearchParams from "../../hooks/useCustomSearchParams";
import { useAppContext } from "../../context/AppContext";
import { useQueryClient } from "@tanstack/react-query";
import { useNavigate, useParams } from "react-router-dom";
import apiService from "../../API/apiService";

interface FormKeys {
name: string;
Expand Down Expand Up @@ -45,7 +45,7 @@ function AddRepositoryModal({ isOpen, onClose }: AddRepositoryModalProps) {

setIsLoading(true);

callApi<void>("/api/helm/repositories", {
apiService.fetchWithDefaults<void>("/api/helm/repositories", {
method: "POST",
body,
})
Expand Down
10 changes: 6 additions & 4 deletions dashboard/src/components/repository/RepositoryViewer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { useQuery, useQueryClient } from "@tanstack/react-query";
import apiService from "../../API/apiService";
import Spinner from "../Spinner";
import { useUpdateRepo } from "../../API/repositories";
import { callApi } from "../../API/releases";
import { useEffect, useMemo, useState } from "react";
import { useNavigate, useParams } from "react-router-dom";
import { useAppContext } from "../../context/AppContext";
Expand Down Expand Up @@ -49,9 +48,12 @@ function RepositoryViewer({ repository }: RepositoryViewerProps) {
try {
setIsRemove(true);
const repo = repository?.name || "";
await callApi<void>(`/api/helm/repositories/${repo}`, {
method: "DELETE",
});
await apiService.fetchWithDefaults<void>(
`/api/helm/repositories/${repo}`,
{
method: "DELETE",
}
);
navigate(`/${context}/repository`, { replace: true });
setSelectedRepo("");
queryClient.invalidateQueries({
Expand Down

0 comments on commit f763db7

Please sign in to comment.