Files
simrsx-fe/app/services/device.service.ts
2025-09-26 14:03:44 +07:00

80 lines
2.4 KiB
TypeScript

import { xfetch } from '~/composables/useXfetch'
const mainUrl = '/api/v1/device'
export async function getDevices(params: any = null) {
try {
let url = mainUrl
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
result.body = (resp.body as Record<string, any>) || {}
return result
} catch (error) {
console.error('Error fetching devices:', error)
throw new Error('Failed to fetch devices')
}
}
export async function getDeviceDetail(id: string | number) {
try {
const resp = await xfetch(`${mainUrl}/${id}`, 'GET')
const result: any = {}
result.success = resp.success
result.body = (resp.body as Record<string, any>) || {}
return result
} catch (error) {
console.error('Error fetching device detail:', error)
throw new Error('Failed to fetch device detail')
}
}
export async function postDevice(data: any) {
try {
const resp = await xfetch(mainUrl, 'POST', data)
const result: any = {}
result.success = resp.success
result.body = (resp.body as Record<string, any>) || {}
return result
} catch (error) {
console.error('Error creating device:', error)
throw new Error('Failed to create device')
}
}
export async function patchDevice(id: string | number, data: any) {
try {
const resp = await xfetch(`${mainUrl}/${id}`, 'PATCH', data)
const result: any = {}
result.success = resp.success
result.body = (resp.body as Record<string, any>) || {}
return result
} catch (error) {
console.error('Error updating device:', error)
throw new Error('Failed to update device')
}
}
export async function removeDevice(id: string | number) {
try {
const resp = await xfetch(`${mainUrl}/${id}`, 'DELETE')
const result: any = {}
result.success = resp.success
result.body = (resp.body as Record<string, any>) || {}
return result
} catch (error) {
console.error('Error deleting device:', error)
throw new Error('Failed to delete device')
}
}