import axios, { AxiosInstance, AxiosResponse } from 'axios'; import { GetAllAssetsQueryParams, GetAssetDetailParams, GetAvailableAssetsQueryParams, GetLinkedAssetFamiliesResponse, } from '@/dto/assets.dto'; import { AssetOptionField } from '@/components/v2/DialogSelectAsset/DialogSelectAsset.vue.d'; export interface ServiceOptions { headers?: Record; params?: Record; } export const API = ({ headers = {}, params = {}, }: ServiceOptions = {}): AxiosInstance => { const user = JSON.parse(localStorage.getItem('user') ?? '{}'); const BASE_URL = import.meta.env.VITE_APP_ASSETS_V2_API; const instance = axios.create({ baseURL: `${BASE_URL}/v2/assets`, headers: { 'Content-type': 'application/json', 'Authorization': `Bearer ${user.token}`, ...headers, }, params, }); return instance; }; const getAllAssets = ( params: GetAllAssetsQueryParams, ): Promise => { return API({ params }).get('/'); }; const getAvailableAssets = ( params: GetAvailableAssetsQueryParams, ): Promise => { return API({ params }).get('/available'); }; const scanAsset = (tag: string): Promise => { return API({ params: { tag } }).get(''); }; const getAssetsById = ( _id: string, params: GetAvailableAssetsQueryParams, ): Promise => { return API({ params: { _id, ...params } }).get('/by-id'); }; const getOptions = ( endpoint?: 'unlinked' | 'by-id', params?: Partial> & { group?: string; excludeId?: string; }, ): Promise => { return API({ params }).get(endpoint ? `/${endpoint}/options` : '/options'); }; const getUnlinkedAssets = ( params: GetAvailableAssetsQueryParams, ): Promise => { return API({ params }).get('/unlinked'); }; const getAssetDetail = ( id: string, params?: GetAssetDetailParams, ): Promise => { return API({ params }).get(`/${id}`); }; const getLinkedAssetFamily = ( id?: string, ): Promise> => { return API({ params: { id } }).get('/family'); }; const matchAssetWithTag = ( id: string, tag?: string, ): Promise => { const params = { _id: JSON.stringify([id]), tag, }; return API({ params }).get('/by-id'); }; export default { getAllAssets, getAvailableAssets, getAssetsById, getAssetDetail, matchAssetWithTag, getLinkedAssetFamily, getUnlinkedAssets, getOptions, scanAsset, };