UNPKG

@coder/backstage-plugin-coder

Version:

Create and manage Coder workspaces from Backstage

76 lines (73 loc) 2.98 kB
import { useQueryClient, useQuery } from '@tanstack/react-query'; import { DEFAULT_TANSTACK_QUERY_RETRY_COUNT } from '../typesConstants.esm.js'; import { useEndUserCoderAuth } from '../components/CoderProvider/CoderAuthProvider.esm.js'; import '../components/CoderProvider/CoderAppConfigProvider.esm.js'; import '../components/CoderProvider/CoderProvider.esm.js'; import { CODER_QUERY_KEY_PREFIX } from '../api/queryOptions.esm.js'; import { useCoderApi } from './useCoderApi.esm.js'; function useCoderQuery(queryOptions) { const queryClient = useQueryClient(); const { isAuthenticated } = useEndUserCoderAuth(); const coderApi = useCoderApi(); let patchedQueryKey = queryOptions.queryKey; if (patchedQueryKey === void 0 || patchedQueryKey[0] !== CODER_QUERY_KEY_PREFIX) { const baseKey = queryOptions.queryKey ?? queryClient.defaultQueryOptions().queryKey; if (baseKey === void 0) { throw new Error("No queryKey value provided to useCoderQuery"); } patchedQueryKey = [ CODER_QUERY_KEY_PREFIX, ...baseKey ]; } const patchedOptions = { ...queryOptions, queryKey: patchedQueryKey, enabled: isAuthenticated && (queryOptions.enabled ?? true), keepPreviousData: isAuthenticated && (queryOptions.keepPreviousData ?? false), refetchIntervalInBackground: isAuthenticated && (queryOptions.refetchIntervalInBackground ?? false), queryFn: async (context) => { if (!isAuthenticated) { throw new Error("Cannot complete request - user is not authenticated"); } return queryOptions.queryFn({ ...context, coderApi }); }, refetchInterval: (data, query) => { if (!isAuthenticated) { return false; } const externalRefetchInterval = queryOptions.refetchInterval; if (typeof externalRefetchInterval !== "function") { return externalRefetchInterval ?? false; } return externalRefetchInterval(data, query); }, refetchOnMount: (query) => { if (!isAuthenticated) { return false; } const externalRefetchOnMount = queryOptions.refetchOnMount; if (typeof externalRefetchOnMount !== "function") { return externalRefetchOnMount ?? true; } return externalRefetchOnMount(query); }, retry: (failureCount, error) => { if (!isAuthenticated) { return false; } const externalRetry = queryOptions.retry; if (typeof externalRetry === "number") { const normalized = Number.isInteger(externalRetry) ? Math.max(1, externalRetry) : DEFAULT_TANSTACK_QUERY_RETRY_COUNT; return failureCount < normalized; } if (typeof externalRetry !== "function") { return externalRetry ? externalRetry : failureCount < DEFAULT_TANSTACK_QUERY_RETRY_COUNT; } return externalRetry(failureCount, error); } }; return useQuery(patchedOptions); } export { useCoderQuery }; //# sourceMappingURL=reactQueryWrappers.esm.js.map