Files
simrsx-fe/app/services/device.service.ts
2025-09-22 13:39:36 +07:00

73 lines
2.1 KiB
TypeScript

import { xfetch } from '~/composables/useXfetch'
export async function getSourceDevices(params: any = null) {
try {
let url = '/api/v1/device'
if (params && typeof params === 'object' && Object.keys(params).length > 0) {
const searchParams = new URLSearchParams()
for (const key in params) {
if (params[key] !== null && params[key] !== undefined && params[key] !== '') {
searchParams.append(key, params[key])
}
}
const queryString = searchParams.toString()
if (queryString) url += `?${queryString}`
}
const resp = await xfetch(url, 'GET')
const result: any = {}
result.success = resp.success
if (resp.success) {
result.data = (resp.body as Record<string, any>).data
}
return result
} catch (error) {
console.error('Error fetching source devices:', error)
throw new Error('Failed to fetch source devices')
}
}
export async function getSourceDeviceDetail(id: string | number) {
try {
const resp = await xfetch(`/api/v1/device/${id}`, 'GET')
const result: any = {}
result.success = resp.success
if (resp.success) {
result.data = (resp.body as Record<string, any>).data
}
return result
} catch (error) {
console.error('Error fetching device detail:', error)
throw new Error('Failed to fetch device detail')
}
}
export async function postSourceDevice(data: any) {
try {
const resp = await xfetch('/api/v1/device', 'POST', data)
return resp
} catch (error) {
console.error('Error creating device:', error)
throw new Error('Failed to create device')
}
}
export async function putSourceDevice(id: string | number, data: any) {
try {
const resp = await xfetch(`/api/v1/device/${id}`, 'PUT', data)
return resp
} catch (error) {
console.error('Error updating device:', error)
throw new Error('Failed to update device')
}
}
export async function removeSourceDevice(id: string | number) {
try {
const resp = await xfetch(`/api/v1/device/${id}`, 'DELETE')
return resp
} catch (error) {
console.error('Error deleting device:', error)
throw new Error('Failed to delete device')
}
}