81 lines
2.1 KiB
TypeScript
81 lines
2.1 KiB
TypeScript
import makeRequest from "@root/api/makeRequest";
|
||
|
||
import { parseAxiosError } from "@root/utils/parse-error";
|
||
|
||
import type { UserType } from "@root/api/roles";
|
||
|
||
export type UsersListResponse = {
|
||
totalPages: number;
|
||
users: UserType[];
|
||
};
|
||
|
||
const baseUrl = process.env.REACT_APP_DOMAIN + "/user";
|
||
|
||
const getUserInfo = async (id: string): Promise<[UserType | null, string?]> => {
|
||
try {
|
||
const userInfoResponse = await makeRequest<never, UserType>({
|
||
url: `${baseUrl}/${id}`,
|
||
method: "GET",
|
||
useToken: true,
|
||
});
|
||
|
||
return [userInfoResponse];
|
||
} catch (nativeError) {
|
||
const [error] = parseAxiosError(nativeError);
|
||
|
||
return [null, `Ошибка получения информации о пользователе. ${error}`];
|
||
}
|
||
};
|
||
|
||
const getUserList = async (page = 1, limit = 10): Promise<[UsersListResponse | null, string?]> => {
|
||
try {
|
||
const userResponse = await makeRequest<never, UsersListResponse>({
|
||
method: "get",
|
||
url: baseUrl + `/?page=${page}&limit=${limit}`,
|
||
});
|
||
|
||
return [userResponse];
|
||
} catch (nativeError) {
|
||
const [error] = parseAxiosError(nativeError);
|
||
|
||
return [null, `Ошибка при получении пользователей. ${error}`];
|
||
}
|
||
};
|
||
|
||
const getManagerList = async (page = 1, limit = 10): Promise<[UsersListResponse | null, string?]> => {
|
||
try {
|
||
const managerResponse = await makeRequest<never, UsersListResponse>({
|
||
method: "get",
|
||
url: baseUrl + `/?page=${page}&limit=${limit}`,
|
||
});
|
||
|
||
return [managerResponse];
|
||
} catch (nativeError) {
|
||
const [error] = parseAxiosError(nativeError);
|
||
|
||
return [null, `Ошибка при получении менеджеров. ${error}`];
|
||
}
|
||
};
|
||
|
||
const getAdminList = async (page = 1, limit = 10): Promise<[UsersListResponse | null, string?]> => {
|
||
try {
|
||
const adminResponse = await makeRequest<never, UsersListResponse>({
|
||
method: "get",
|
||
url: baseUrl + `/?page=${page}&limit=${limit}`,
|
||
});
|
||
|
||
return [adminResponse];
|
||
} catch (nativeError) {
|
||
const [error] = parseAxiosError(nativeError);
|
||
|
||
return [null, `Ошибка при получении админов. ${error}`];
|
||
}
|
||
};
|
||
|
||
export const userApi = {
|
||
getUserInfo,
|
||
getUserList,
|
||
getManagerList,
|
||
getAdminList,
|
||
};
|