diff --git a/packages/manager/apps/hub-react/src/data/api/apiHubSupport.ts b/packages/manager/apps/hub-react/src/data/api/apiHubSupport.ts index e81963f021e9..7f1f7ee469be 100644 --- a/packages/manager/apps/hub-react/src/data/api/apiHubSupport.ts +++ b/packages/manager/apps/hub-react/src/data/api/apiHubSupport.ts @@ -1,12 +1,11 @@ import { aapi } from '@ovh-ux/manager-core-api'; -import { AxiosResponse } from 'axios'; import { SupportResponse } from '@/types/support.type'; import { ApiEnvelope } from '@/types/apiEnvelope.type'; export const getHubSupport: ( cached: boolean, -) => Promise>> = (cached: boolean) => - aapi.get( +) => Promise> = async (cached: boolean) => { + const { data } = await aapi.get>( '/hub/support', !cached && { headers: { @@ -14,3 +13,8 @@ export const getHubSupport: ( }, }, ); + if (data?.data?.support?.status === 'ERROR') { + throw new Error(); + } + return data; +}; diff --git a/packages/manager/apps/hub-react/src/data/hooks/apiHubSupport/useHubSupport.spec.tsx b/packages/manager/apps/hub-react/src/data/hooks/apiHubSupport/useHubSupport.spec.tsx index ca354e099103..c44438878d76 100644 --- a/packages/manager/apps/hub-react/src/data/hooks/apiHubSupport/useHubSupport.spec.tsx +++ b/packages/manager/apps/hub-react/src/data/hooks/apiHubSupport/useHubSupport.spec.tsx @@ -1,7 +1,6 @@ import { QueryClientProvider } from '@tanstack/react-query'; import { act, renderHook, waitFor } from '@testing-library/react'; import { PropsWithChildren } from 'react'; -import { AxiosResponse } from 'axios'; import { describe, it, vi } from 'vitest'; import { useFetchHubSupport } from '@/data/hooks/apiHubSupport/useHubSupport'; import { SupportResponse } from '@/types/support.type'; @@ -29,8 +28,9 @@ describe('useFetchHubSupport', () => { .spyOn(hubSupportApi, 'getHubSupport') .mockReturnValue( Promise.resolve({ - data: { data: supportDataResponse, status: 'OK' }, - } as AxiosResponse), + data: supportDataResponse, + status: 'OK', + }), ); const { result } = renderHook(() => useFetchHubSupport(), { @@ -52,8 +52,9 @@ describe('useFetchHubSupport', () => { .spyOn(hubSupportApi, 'getHubSupport') .mockReturnValue( Promise.resolve({ - data: { data: supportDataResponse, status: 'OK' }, - } as AxiosResponse), + data: supportDataResponse, + status: 'OK', + }), ); const { result } = renderHook(() => useFetchHubSupport(), { diff --git a/packages/manager/apps/hub-react/src/data/hooks/apiHubSupport/useHubSupport.tsx b/packages/manager/apps/hub-react/src/data/hooks/apiHubSupport/useHubSupport.tsx index 956da412b36d..e24f2ca7f37c 100644 --- a/packages/manager/apps/hub-react/src/data/hooks/apiHubSupport/useHubSupport.tsx +++ b/packages/manager/apps/hub-react/src/data/hooks/apiHubSupport/useHubSupport.tsx @@ -1,5 +1,4 @@ import { DefinedInitialDataOptions, useQuery } from '@tanstack/react-query'; -import { AxiosResponse } from 'axios'; import { SupportDataResponse, SupportResponse } from '@/types/support.type'; import { getHubSupport } from '@/data/api/apiHubSupport'; import queryClient from '@/queryClient'; @@ -7,8 +6,6 @@ import { ApiEnvelope } from '@/types/apiEnvelope.type'; const queryKey = ['get-hub-support']; -type ApiResponse = AxiosResponse>; - const DEFAULT_RESULT: SupportDataResponse = { data: [], count: 0, @@ -16,7 +13,11 @@ const DEFAULT_RESULT: SupportDataResponse = { export const useFetchHubSupport = ( options?: Partial< - DefinedInitialDataOptions + DefinedInitialDataOptions< + ApiEnvelope, + any, + SupportDataResponse + > >, ) => useQuery({ @@ -25,7 +26,7 @@ export const useFetchHubSupport = ( const hasBeenFetched = Boolean(queryClient.getQueryData(queryKey)); return getHubSupport(!hasBeenFetched); }, - select: ({ data }: ApiResponse) => - data?.data?.support?.data ?? DEFAULT_RESULT, + select: ({ data }: ApiEnvelope) => + data?.support?.data ?? DEFAULT_RESULT, ...(options ?? {}), });