import axios, { AxiosInstance, AxiosResponse } from 'axios'; import { SystemRoleAttribute, TransactionAttribute } from '@/utils/role.util'; import { TreeNode } from 'primevue/treenode'; export interface ServiceOptions { type?: 'Group' | 'Category'; headers?: Record; params?: TreeQueryParams; } export interface TreeListData { data: TreeNode[]; } export type RoleType = 'Manager' | 'Monitoring' | 'Approval' | 'Staff'; export interface TreeRoleParams { systemRole?: SystemRoleAttribute[]; transactionAttribute?: TransactionAttribute[]; roleType?: RoleType[]; } export interface TreeQueryParams { systemRole?: string; transactionAttribute?: string; roleType?: string; } export const API = ({ type = 'Group', headers = {}, params = {}, }: ServiceOptions = {}): AxiosInstance => { const user = JSON.parse(localStorage.getItem('user') ?? '{}'); const BASE_URL = type === 'Group' ? import.meta.env.VITE_APP_GROUPS_API : import.meta.env.VITE_APP_ASSET_ATTRIBUTES_API; const instance = axios.create({ baseURL: `${BASE_URL}/v2/`, headers: { 'Content-type': 'application/json', 'Authorization': `Bearer ${user.token}`, ...headers, }, params, }); return instance; }; const TreeServices = { getTreeList: ( type: ServiceOptions['type'], params?: TreeQueryParams, ): Promise> => { return API({ type, params }).get( type === 'Group' ? '/tree' : '/category/tree', ); }, }; export default TreeServices;