Skip to content

Commit 5ad5f88

Browse files
authored
Merge pull request #176 from TheSoftwareHouse/feat/ZN-572/update-react-query
feat: update react query to V5
2 parents c829537 + 9fb6bae commit 5ad5f88

File tree

23 files changed

+232
-235
lines changed

23 files changed

+232
-235
lines changed

package-lock.json

Lines changed: 21 additions & 82 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@
5353
"dependencies": {
5454
"@sentry/browser": "8.8.0",
5555
"@sentry/integrations": "7.110.0",
56-
"@tanstack/react-query": "4.36.1",
57-
"@tanstack/react-query-devtools": "4.35.0",
56+
"@tanstack/react-query": "5.45.1",
57+
"@tanstack/react-query-devtools": "5.45.1",
5858
"@tanstack/react-router": "1.29.2",
5959
"axios": "1.6.8",
6060
"clsx": "2.1.0",
Lines changed: 41 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,47 @@
11
import { AxiosInstance } from 'axios';
22
import { stringify } from 'qs';
33

4-
import {
5-
GetMeQueryResponse,
6-
GetUsersInfiniteArgs,
7-
GetUsersListArgs,
8-
GetUsersResponse,
9-
// QUERY_TYPE_IMPORTS
10-
} from './auth.types';
4+
import { queryFactoryOptions, infiniteQueryFactoryOptions } from '../../utils/queryFactoryOptions';
5+
6+
import { GetMeQueryResponse, GetUsersInfiniteArgs, GetUsersListArgs, GetUsersResponse } from './auth.types';
7+
8+
const getCurrentUser = (client: AxiosInstance) => async () => {
9+
return (await client.get<GetMeQueryResponse>('/me')).data;
10+
};
11+
12+
const getUsersInfinite =
13+
(client: AxiosInstance, { count = '5' }: GetUsersInfiniteArgs) =>
14+
async ({ pageParam = '1' }) => {
15+
const queryParams = stringify({ page: pageParam, count }, { addQueryPrefix: true });
16+
return (await client.get<GetUsersResponse>(`/users/${queryParams}`)).data;
17+
};
18+
19+
const getUsersList =
20+
(client: AxiosInstance, { page = '1' }: GetUsersListArgs) =>
21+
async () => {
22+
const queryParams = stringify({ page, count: 5 }, { addQueryPrefix: true });
23+
return (await client.get<GetUsersResponse>(`/users/${queryParams}`)).data;
24+
};
1125

1226
export const authQueries = {
13-
getCurrentUser: (client: AxiosInstance) => async () => {
14-
return (await client.get<GetMeQueryResponse>('/me')).data;
15-
},
16-
getUsersInfinite:
17-
(client: AxiosInstance) =>
18-
async ({ pageParam = '1', count = '5' }: GetUsersInfiniteArgs) => {
19-
const queryParams = stringify({ page: pageParam, count: count }, { addQueryPrefix: true });
20-
return (await client.get<GetUsersResponse>(`/users/${queryParams}`)).data;
21-
},
22-
getUsersList:
23-
(client: AxiosInstance) =>
24-
async ({ page = '1' }: GetUsersListArgs) => {
25-
const queryParams = stringify({ page, count: 5 }, { addQueryPrefix: true });
26-
return (await client.get<GetUsersResponse>(`/users/${queryParams}`)).data;
27-
},
28-
// QUERY_FUNCTIONS_SETUP
27+
all: () => ['users'],
28+
me: () =>
29+
queryFactoryOptions({
30+
queryKey: [...authQueries.all(), 'me'],
31+
queryFn: getCurrentUser,
32+
}),
33+
lists: () => [...authQueries.all(), 'list'],
34+
list: (params: GetUsersListArgs) =>
35+
queryFactoryOptions({
36+
queryKey: [...authQueries.lists(), params],
37+
queryFn: (client) => getUsersList(client, params),
38+
}),
39+
listsInfinite: () => [...authQueries.lists(), 'infinite'],
40+
listInfinite: (params: GetUsersInfiniteArgs) =>
41+
infiniteQueryFactoryOptions({
42+
queryKey: [...authQueries.listsInfinite(), params],
43+
queryFn: (client) => getUsersInfinite(client, params),
44+
initialPageParam: '1',
45+
getNextPageParam: ({ nextPage }) => nextPage?.toString(),
46+
}),
2947
};

src/api/actions/auth/auth.types.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ export type GetUsersResponse = {
2727
};
2828

2929
export type GetUsersInfiniteArgs = {
30-
pageParam?: string;
3130
count?: string;
3231
};
3332

src/api/actions/index.ts

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,4 @@
11
import { authMutations } from './auth/auth.mutations';
2-
import { authQueries } from './auth/auth.queries';
3-
// API_COLLECTION_IMPORTS
4-
5-
export const queries = {
6-
...authQueries,
7-
// API_COLLECTION_QUERIES
8-
} as const;
9-
10-
export type AxiosQueriesType = typeof queries;
11-
12-
export type AxiosInfiniteQueriesType = Pick<AxiosQueriesType, 'getUsersInfinite'>;
132

143
export const mutations = {
154
...authMutations,

src/api/types/types.ts

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,10 @@
22
import { QueryMeta } from '@tanstack/react-query';
33
import { AxiosRequestConfig } from 'axios';
44

5-
import { AxiosQueriesType } from 'api/actions';
6-
75
export type MutationHTTPMethod = 'DELETE' | 'POST' | 'PUT' | 'PATCH';
86

97
export type Unwrap<T> = T extends PromiseLike<infer U> ? U : T;
108

11-
export type GetQueryParams<Key extends keyof AxiosQueriesType> = ReturnType<AxiosQueriesType[Key]> extends (
12-
value: infer Params,
13-
) => any
14-
? Params extends Parameters<ReturnType<AxiosQueriesType[keyof AxiosQueriesType]>>[0]
15-
? Params
16-
: any
17-
: never;
18-
19-
export type DataForQuery<TQueryKey extends keyof AxiosQueriesType> = Unwrap<
20-
ReturnType<ReturnType<AxiosQueriesType[TQueryKey]>>
21-
>;
22-
23-
export type ArgsForQuery<TQueryKey extends keyof AxiosQueriesType> = GetQueryParams<TQueryKey>;
24-
259
export type ExtendedQueryMeta = QueryMeta & {
2610
error: { excludedCodes: number[]; showGlobalError: boolean };
2711
};
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { StandardizedApiError } from 'context/apiClient/apiClientContextController/apiError/apiError.types';
2+
import { UseInfiniteQueryOptions } from 'hooks/useInfiniteQuery/useInfiniteQuery.types';
3+
import { UseQueryOptions } from 'hooks/useQuery/useQuery.types';
4+
5+
export const queryFactoryOptions = <TQueryFnData = unknown, TError = StandardizedApiError>(
6+
options: UseQueryOptions<TQueryFnData, TError>,
7+
) => options;
8+
9+
export const infiniteQueryFactoryOptions = <
10+
TQueryFnData = unknown,
11+
TPageParam = unknown,
12+
TError = StandardizedApiError,
13+
>(
14+
options: UseInfiniteQueryOptions<TQueryFnData, TError, TPageParam>,
15+
) => options;

src/context/apiClient/apiClientContextController/ApiClientContextController.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { useHandleQueryErrors } from 'hooks/useHandleQueryErrors/useHandleQueryE
88
import { ExtendedQueryMeta } from 'api/types/types';
99

1010
import { ApiClientControllerProps } from './ApiClientContextController.types';
11-
import { ApiError } from './apiError/apiError.types';
11+
import { StandardizedApiError } from './apiError/apiError.types';
1212

1313
const metaErrorConfig = { error: { showGlobalError: true, excludedCodes: [] } };
1414

@@ -17,14 +17,14 @@ export const ApiClientContextController = ({ children }: ApiClientControllerProp
1717

1818
const mutationCache = new MutationCache({
1919
onError: (err, variables, context, mutation) => {
20-
const error = err as ApiError;
20+
const error = err as StandardizedApiError;
2121
shouldHandleGlobalError((mutation.meta as ExtendedQueryMeta)?.error, error?.statusCode) && handleErrors(error);
2222
},
2323
});
2424

2525
const queryCache = new QueryCache({
2626
onError: (err, query) => {
27-
const error = err as ApiError;
27+
const error = err as StandardizedApiError;
2828

2929
shouldHandleGlobalError((query.meta as ExtendedQueryMeta)?.error, error?.statusCode) && handleErrors(error);
3030
},

src/context/apiClient/apiClientContextController/apiError/apiError.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ describe('getStandardizedApiError', () => {
1919
};
2020
const mockAxiosError = getMockAxiosError(errorData);
2121

22-
expect(getStandardizedApiError(mockAxiosError)).toEqual({
22+
expect(getStandardizedApiError(mockAxiosError)).toMatchObject({
2323
type: 'basic',
2424
statusCode: 400,
2525
data: errorData,
@@ -35,7 +35,7 @@ describe('getStandardizedApiError', () => {
3535
};
3636
const mockAxiosError = getMockAxiosError(errorData);
3737

38-
expect(getStandardizedApiError(mockAxiosError)).toEqual({
38+
expect(getStandardizedApiError(mockAxiosError)).toMatchObject({
3939
type: 'form',
4040
statusCode: 400,
4141
data: errorData,
@@ -51,7 +51,7 @@ describe('getStandardizedApiError', () => {
5151
};
5252
const mockAxiosError = getMockAxiosError(errorData);
5353

54-
expect(getStandardizedApiError(mockAxiosError)).toEqual({
54+
expect(getStandardizedApiError(mockAxiosError)).toMatchObject({
5555
statusCode: 400,
5656
type: 'unknown',
5757
data: errorData,

0 commit comments

Comments
 (0)