fleet/frontend/services/entities/software.ts
2023-12-12 15:03:33 -06:00

163 lines
4.0 KiB
TypeScript

import { snakeCase, reduce } from "lodash";
import sendRequest from "services";
import endpoints from "utilities/endpoints";
import {
ISoftwareResponse,
ISoftwareCountResponse,
IGetSoftwareByIdResponse,
ISoftwareVersion,
ISoftwareTitle,
} from "interfaces/software";
import { buildQueryStringFromParams, QueryParams } from "utilities/url";
export interface ISoftwareApiParams {
page?: number;
perPage?: number;
orderKey?: string;
orderDirection?: "asc" | "desc";
query?: string;
vulnerable?: boolean;
teamId?: number;
}
export interface ISoftwareTitlesResponse {
counts_updated_at: string | null;
count: number;
software_titles: ISoftwareTitle[];
meta: {
has_next_results: boolean;
has_previous_results: boolean;
};
}
export interface ISoftwareVersionsResponse {
counts_updated_at: string | null;
count: number;
software: ISoftwareVersion[];
meta: {
has_next_results: boolean;
has_previous_results: boolean;
};
}
export interface ISoftwareTitleResponse {
software_title: ISoftwareTitle;
}
export interface ISoftwareVersionResponse {
software: ISoftwareVersion;
}
export interface ISoftwareQueryKey extends ISoftwareApiParams {
scope: "software";
}
export interface ISoftwareCountQueryKey
extends Pick<ISoftwareApiParams, "query" | "vulnerable" | "teamId"> {
scope: "softwareCount";
}
const ORDER_KEY = "name";
const ORDER_DIRECTION = "asc";
const convertParamsToSnakeCase = (params: ISoftwareApiParams) => {
return reduce<typeof params, QueryParams>(
params,
(result, val, key) => {
result[snakeCase(key)] = val;
return result;
},
{}
);
};
export default {
load: async ({
page,
perPage,
orderKey = ORDER_KEY,
orderDirection: orderDir = ORDER_DIRECTION,
query,
vulnerable,
teamId,
}: ISoftwareApiParams): Promise<ISoftwareResponse> => {
const { SOFTWARE } = endpoints;
const queryParams = {
page,
perPage,
orderKey,
orderDirection: orderDir,
teamId,
query,
vulnerable,
};
const snakeCaseParams = convertParamsToSnakeCase(queryParams);
const queryString = buildQueryStringFromParams(snakeCaseParams);
const path = `${SOFTWARE}?${queryString}`;
try {
return sendRequest("GET", path);
} catch (error) {
throw error;
}
},
getCount: async ({
query,
teamId,
vulnerable,
}: Pick<
ISoftwareApiParams,
"query" | "teamId" | "vulnerable"
>): Promise<ISoftwareCountResponse> => {
const { SOFTWARE } = endpoints;
const path = `${SOFTWARE}/count`;
const queryParams = {
query,
teamId,
vulnerable,
};
const snakeCaseParams = convertParamsToSnakeCase(queryParams);
const queryString = buildQueryStringFromParams(snakeCaseParams);
return sendRequest("GET", path.concat(`?${queryString}`));
},
getSoftwareById: async (
softwareId: string
): Promise<IGetSoftwareByIdResponse> => {
const { SOFTWARE } = endpoints;
const path = `${SOFTWARE}/${softwareId}`;
return sendRequest("GET", path);
},
getSoftwareTitles: (params: ISoftwareApiParams) => {
const { SOFTWARE_TITLES } = endpoints;
const snakeCaseParams = convertParamsToSnakeCase(params);
const queryString = buildQueryStringFromParams(snakeCaseParams);
const path = `${SOFTWARE_TITLES}?${queryString}`;
return sendRequest("GET", path);
},
getSoftwareTitle: (id: number) => {
const { SOFTWARE_TITLE } = endpoints;
return sendRequest("GET", SOFTWARE_TITLE(id));
},
getSoftwareVersions: (params: ISoftwareApiParams) => {
const { SOFTWARE_VERSIONS } = endpoints;
const snakeCaseParams = convertParamsToSnakeCase(params);
const queryString = buildQueryStringFromParams(snakeCaseParams);
const path = `${SOFTWARE_VERSIONS}?${queryString}`;
return sendRequest("GET", path);
},
getSoftwareVersion: (id: number) => {
const { SOFTWARE_VERSION } = endpoints;
return sendRequest("GET", SOFTWARE_VERSION(id));
},
};