fix: update crud base

This commit is contained in:
riefive
2025-10-06 09:44:48 +07:00
parent 51ddb9d8b5
commit 45ea70d415
+15 -15
View File
@@ -1,6 +1,6 @@
import { xfetch } from '~/composables/useXfetch'
export async function create(path: string, data: any) {
export async function create(path: string, data: any, name: string = 'item') {
try {
const resp = await xfetch(path, 'POST', data)
const result: any = {}
@@ -8,12 +8,12 @@ export async function create(path: string, data: any) {
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')
console.error(`Error posting ${name}:`, error)
throw new Error(`Failed to post ${name}`)
}
}
export async function getList(path: string, params: any = null) {
export async function getList(path: string, params: any = null, name: string = 'item') {
try {
let url = path
if (params && typeof params === 'object' && Object.keys(params).length > 0) {
@@ -32,12 +32,12 @@ export async function getList(path: string, params: any = null) {
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')
console.error(`Error fetching ${name}s:`, error)
throw new Error(`Failed to fetch ${name}s`)
}
}
export async function getDetail(path: string, id: number | string) {
export async function getDetail(path: string, id: number | string, name: string = 'item') {
try {
const resp = await xfetch(`${path}/${id}`, 'GET')
const result: any = {}
@@ -45,12 +45,12 @@ export async function getDetail(path: string, id: number | string) {
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')
console.error(`Error fetching ${name} detail:`, error)
throw new Error(`Failed to get ${name} detail`)
}
}
export async function update(path: string, id: number | string, data: any) {
export async function update(path: string, id: number | string, data: any, name: string = 'item') {
try {
const resp = await xfetch(`${path}/${id}`, 'PATCH', data)
const result: any = {}
@@ -58,12 +58,12 @@ export async function update(path: string, id: number | string, data: any) {
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')
console.error(`Error putting ${name}:`, error)
throw new Error(`Failed to put ${name}`)
}
}
export async function remove(path: string, id: number | string) {
export async function remove(path: string, id: number | string, name: string = 'item') {
try {
const resp = await xfetch(`${path}/${id}`, 'DELETE')
const result: any = {}
@@ -71,7 +71,7 @@ export async function remove(path: string, id: number | string) {
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')
console.error(`Error deleting ${name}:`, error)
throw new Error(`Failed to delete ${name}`)
}
}