useBaseInfoApi-CB1ASC8-.mjs
3.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import { z as createSharedComposable, A as useApiGateway } from './server.mjs';
const DEVICE_TYPE_CATEGORY_VALUES = [
"robot",
"controller",
"vision",
"chassis",
"communication",
"power",
"sensor",
"actuator",
"other"
];
function isRecord(value) {
return typeof value === "object" && value !== null;
}
function isDeviceTypeCategory(value) {
return typeof value === "string" && DEVICE_TYPE_CATEGORY_VALUES.includes(value);
}
function normalizeDeviceTypes(payload) {
if (!Array.isArray(payload)) {
return [];
}
return payload.filter(isRecord).map((item) => {
const rawCategory = item.category;
return {
id: Number(item.id ?? 0),
name: typeof item.name === "string" ? item.name : "",
model: typeof item.model === "string" ? item.model : "",
category: isDeviceTypeCategory(rawCategory) ? rawCategory : "other",
lengthMm: Number(item.lengthMm ?? 0),
widthMm: Number(item.widthMm ?? 0),
heightMm: Number(item.heightMm ?? 0),
weightKg: Number(item.weightKg ?? 0),
hasBattery: Boolean(item.hasBattery),
batterySpec: typeof item.batterySpec === "string" ? item.batterySpec : void 0,
description: typeof item.description === "string" ? item.description : void 0,
updatedAt: typeof item.updatedAt === "string" ? item.updatedAt : ""
};
}).filter((item) => item.id > 0 && item.name.length > 0);
}
function normalizeMutation(payload, fallbackMessage) {
if (!isRecord(payload)) {
return {
success: false,
errorCode: "REQUEST_FAILED",
message: fallbackMessage
};
}
const message = typeof payload.message === "string" && payload.message.length > 0 ? payload.message : fallbackMessage;
return {
success: typeof payload.success === "boolean" ? payload.success : false,
errorCode: typeof payload.errorCode === "string" ? payload.errorCode : null,
message
};
}
const _useBaseInfoApi = () => {
const api = useApiGateway();
const getDeviceTypes = () => {
return api.useApiFetch("/api/base-info/device-types", {
transform: normalizeDeviceTypes,
default: () => []
});
};
const createDeviceType = async (payload) => {
try {
const result = await api.request("/api/base-info/device-types", {
method: "POST",
body: payload
});
return normalizeMutation(result, "创建设备类型失败。");
} catch {
return {
success: false,
errorCode: "REQUEST_FAILED",
message: "创建设备类型失败。"
};
}
};
const updateDeviceType = async (id, payload) => {
try {
const result = await api.request(`/api/base-info/device-types/${id}`, {
method: "PUT",
body: payload
});
return normalizeMutation(result, "更新设备类型失败。");
} catch {
return {
success: false,
errorCode: "REQUEST_FAILED",
message: "更新设备类型失败。"
};
}
};
const deleteDeviceType = async (id) => {
try {
const result = await api.request(`/api/base-info/device-types/${id}`, {
method: "DELETE"
});
return normalizeMutation(result, "删除设备类型失败。");
} catch {
return {
success: false,
errorCode: "REQUEST_FAILED",
message: "删除设备类型失败。"
};
}
};
return {
getDeviceTypes,
createDeviceType,
updateDeviceType,
deleteDeviceType
};
};
const useBaseInfoApi = createSharedComposable(_useBaseInfoApi);
export { DEVICE_TYPE_CATEGORY_VALUES as D, useBaseInfoApi as u };
//# sourceMappingURL=useBaseInfoApi-CB1ASC8-.mjs.map