Files
simrsx-fe/app/services/_crud-base.ts
Munawwirul Jamal ce785f2092 dev: hotfix
+ data table
+ genCrudHandler
+ crud-base
2025-10-04 06:55:12 +07:00

78 lines
2.4 KiB
TypeScript

import { xfetch } from '~/composables/useXfetch'
export async function create(path: string, data: any) {
try {
const resp = await xfetch(path, 'POST', data)
const result: any = {}
result.success = resp.success
result.body = (resp.body as Record<string, any>) || {}
return result
} catch (error) {
console.error('Error posting division:', error)
throw new Error('Failed to post division')
}
}
export async function getList(path: string, params: any = null) {
try {
let url = path
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(path, 'GET')
const result: any = {}
result.success = resp.success
result.body = (resp.body as Record<string, any>) || {}
return result
} catch (error) {
console.error('Error fetching divisions:', error)
throw new Error('Failed to fetch divisions')
}
}
export async function getDetail(path: string, id: number | string) {
try {
const resp = await xfetch(`${path}/${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 division detail:', error)
throw new Error('Failed to get division detail')
}
}
export async function update(path: string, id: number | string, data: any) {
try {
const resp = await xfetch(`${path}/${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 putting division:', error)
throw new Error('Failed to put division')
}
}
export async function remove(path: string, id: number | string) {
try {
const resp = await xfetch(`${path}/${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 data:', error)
throw new Error('Failed to delete division')
}
}