import axios, { AxiosInstance, AxiosResponse } from 'axios'; import { DataTableParams } from '@/types/dataTable.type'; export interface ServiceOptions { headers?: Record; params?: Record; } export type AssetNameOptionsQueryParams = { nameOptions?: boolean; brandOptions?: boolean; }; export type GetAssetNameListQueryParams = { assetName?: string; tagType?: string; }; export interface AssetNameDropdownOption { _id: string; key: number; name: string; category: { _id: string; name: string; key: number; }; tagType: string; } export interface GetAssetNameDropdownResponse { data: AssetNameDropdownOption[]; } export const API = ({ headers = {}, params = {}, }: ServiceOptions = {}): AxiosInstance => { const user = JSON.parse(localStorage.getItem('user') ?? '{}'); const BASE_URL = import.meta.env.VITE_APP_ASSET_ATTRIBUTES_API; const instance = axios.create({ baseURL: `${BASE_URL}/v2/asset-name`, headers: { 'Content-type': 'application/json', 'Authorization': `Bearer ${user.token}`, ...headers, }, params, }); return instance; }; const getAssetNameDetail = (id: string): Promise => { return API().get(`/${id}`); }; const getAssetsByAssetName = ( id: string, params?: DataTableParams, ): Promise => { return API({ params }).get(`/${id}/list-asset`); }; const getAssetNameList = ( params: GetAssetNameListQueryParams, ): Promise => { return API({ params }).get('/'); }; const getUnpairedAssetName = ( params: GetAssetNameListQueryParams, ): Promise => { return API({ params }).get('/unpaired'); }; const getOptions = ( params: AssetNameOptionsQueryParams, ): Promise => { return API({ params }).get('/options'); }; const getAssetNameDropdown = (): Promise< AxiosResponse > => { return API().get('/dropdown'); }; export default { getAssetNameDetail, getAssetNameList, getAssetsByAssetName, getUnpairedAssetName, getAssetNameDropdown, getOptions, };