Files
portainer/app/react/docker/services/queries/useService.ts
T
2025-12-17 13:02:19 +02:00

36 lines
1.1 KiB
TypeScript

import { useQuery } from '@tanstack/react-query';
import { Service } from 'docker-types/generated/1.44';
import axios, { parseAxiosError } from '@CE/portainer/services/axios';
import { withGlobalError } from '@CE/react-tools/react-query';
import { ServiceId } from '@CE/react/docker/services/types';
import { queryKeys } from '@CE/react/docker/services/queries/query-keys';
import { EnvironmentId } from '@CE/react/portainer/environments/types';
import { buildUrl } from '@CE/react/docker/services/queries/build-url';
export function useService(environmentId: EnvironmentId, serviceId: ServiceId) {
return useQuery(
queryKeys.service(environmentId, serviceId),
() => getService(environmentId, serviceId),
{
enabled: !!serviceId,
...withGlobalError('Unable to retrieve service'),
}
);
}
export async function getService(
environmentId: EnvironmentId,
serviceId: ServiceId
) {
try {
const { data } = await axios.get<Service>(
buildUrl(environmentId, serviceId)
);
return data;
} catch (e) {
throw parseAxiosError(e, 'Unable to get service');
}
}