import { K8sResourceListOptions, K8sResourceReadOptions, K8sResourceListResult, createGetQueryOptions, queryClient, createListqueryOptions, createQueryKeys, k8sListResource, } from '../k8s'; import { TQueryOptions } from '../k8s/query/type'; import { K8sResourceCommon, QueryOptionsWithSelector, WatchK8sResource } from '../types/k8s'; import { KUBEARCHIVE_PATH_PREFIX } from './const'; function withKubearchivePathPrefix< T extends { fetchOptions?: { requestInit?: RequestInit & { wsPrefix?: string; pathPrefix?: string } }; }, >(opts: T): T { return { ...opts, fetchOptions: { ...opts.fetchOptions, requestInit: { ...opts.fetchOptions?.requestInit, pathPrefix: KUBEARCHIVE_PATH_PREFIX, }, }, }; } export function kubearchiveQueryGetResource( resourceInit: K8sResourceReadOptions, options?: TQueryOptions, ): Promise { return queryClient.ensureQueryData( createGetQueryOptions(withKubearchivePathPrefix(resourceInit), options), ); } export function kubearchiveQueryListResourceItems( resourceInit: K8sResourceListOptions, options?: TQueryOptions, ): Promise { return queryClient.ensureQueryData( createListqueryOptions(withKubearchivePathPrefix(resourceInit), options), ); } export function kubearchiveQueryListResource( resourceInit: K8sResourceListOptions, options?: TQueryOptions>, ): Promise> { return queryClient.ensureQueryData({ ...options, queryKey: createQueryKeys(withKubearchivePathPrefix(resourceInit)), queryFn: () => k8sListResource(withKubearchivePathPrefix(resourceInit)), }); } export const convertToKubearchiveQueryParams = ( resourceInit?: WatchK8sResource, ): QueryOptionsWithSelector | undefined => { if (!resourceInit) { return undefined; } const queryParams: QueryOptionsWithSelector['queryParams'] = {}; if (resourceInit.selector && Object.keys(resourceInit.selector).length > 0) { queryParams.labelSelector = resourceInit.selector; } // e.g. name=*e2e*,creationTimestampAfter=2023-01-01T12:00:00Z // // ['name=*e2e**', 'creationTimestampAfter=2023-01-01T12:00:00Z'] // // queryParams.name = '*e2e*' // queryParams.creationTimestampAfter = '2023-01-01T12:00:00Z' if (resourceInit.fieldSelector) { const fields = resourceInit.fieldSelector.split(','); fields.forEach((field) => { const [key, value] = field.split('='); if (key && value) { queryParams[key] = value; } }); } if (resourceInit.limit) { queryParams.limit = resourceInit.limit; } return { ns: resourceInit.namespace, name: resourceInit.name, queryParams, }; }; export { withKubearchivePathPrefix };