useBaseInfoApi-CB1ASC8-.mjs 3.57 KB
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