fleet/frontend/services/entities/software.ts
Martavis Parker 384c987389
Removed all traces of Redux from the app! (#5287)
* clean up routes and useless components

* component clean up

* removed redux from routes

* rename file

* moved useDeepEffect hook with others

* removed redux, fleet, app_constants dirs; added types to utilities

* style cleanup

* typo fix

* removed unused ts-ignore comments

* removed redux packages!!!

* formatting

* fixed typing for simple search function

* updated frontend readme
2022-04-22 09:45:35 -07:00

87 lines
2 KiB
TypeScript

import { snakeCase } from "lodash";
import sendRequest from "services";
import endpoints from "utilities/endpoints";
import { ISoftware } from "interfaces/software";
interface IGetSoftwareProps {
page?: number;
perPage?: number;
orderKey?: string;
orderDir?: "asc" | "desc";
query?: string;
vulnerable?: boolean;
teamId?: number;
}
export interface ISoftwareResponse {
counts_updated_at: string;
software: ISoftware[];
}
export interface ISoftwareCountResponse {
count: number;
}
type ISoftwareParams = Partial<IGetSoftwareProps>;
const ORDER_KEY = "name";
const ORDER_DIRECTION = "asc";
const buildQueryStringFromParams = (params: ISoftwareParams) => {
const filteredParams = Object.entries(params).filter(
([key, value]) => !!value
);
if (!filteredParams.length) {
return "";
}
return `?${filteredParams
.map(
([key, value]) =>
`${encodeURIComponent(snakeCase(key))}=${encodeURIComponent(value)}`
)
.join("&")}`;
};
export default {
load: async ({
page,
perPage,
orderKey = ORDER_KEY,
orderDir = ORDER_DIRECTION,
query,
vulnerable,
teamId,
}: ISoftwareParams): Promise<ISoftwareResponse> => {
const { SOFTWARE } = endpoints;
const pagination = perPage ? `page=${page}&per_page=${perPage}` : "";
const sort = `order_key=${orderKey}&order_direction=${orderDir}`;
let path = `${SOFTWARE}?${pagination}&${sort}`;
if (teamId) {
path += `&team_id=${teamId}`;
}
if (query) {
path += `&query=${encodeURIComponent(query)}`;
}
if (vulnerable) {
path += `&vulnerable=${vulnerable}`;
}
try {
return sendRequest("GET", path);
} catch (error) {
throw error;
}
},
count: async (params: ISoftwareParams): Promise<ISoftwareCountResponse> => {
const { SOFTWARE } = endpoints;
const path = `${SOFTWARE}/count`;
const queryString = buildQueryStringFromParams(params);
return sendRequest("GET", path.concat(queryString));
},
};