|
1 | 1 | import { AxiosInstance } from 'axios'; |
2 | 2 | import { stringify } from 'qs'; |
3 | 3 |
|
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 | + }; |
11 | 25 |
|
12 | 26 | 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 | + }), |
29 | 47 | }; |
0 commit comments