entityDetailApi.js 2.0 KB

12345678910111213141516171819202122232425262728293031
  1. /** 统一实体详情只读 API。路径规划与响应正规化集中在此,组件/store 不认识后端信封。 */
  2. import { requestCockpitJson } from './cockpitApi.js'
  3. import { ENTITY_DETAIL_KIND, entityIdentity, normalizeEntityDetail } from '../domain/entityDetail.js'
  4. export const ENTITY_DETAIL_PATHS = Object.freeze({
  5. [ENTITY_DETAIL_KIND.FACILITY]: (id, target) => target.category
  6. ? `/infrastructure/entities/${encodeURIComponent(id)}/detail?category=${encodeURIComponent(target.category)}` : '',
  7. [ENTITY_DETAIL_KIND.EMERGENCY]: (id, target) => target.category
  8. ? `/infrastructure/entities/${encodeURIComponent(id)}/detail?category=${encodeURIComponent(target.category)}` : '',
  9. [ENTITY_DETAIL_KIND.EVENT]: (id, target) => target.family
  10. ? `/events/${encodeURIComponent(id)}/detail?family=${encodeURIComponent(target.family)}` : '',
  11. [ENTITY_DETAIL_KIND.WORK_ORDER]: (id) => `/work-orders/${encodeURIComponent(id)}/detail`,
  12. })
  13. export function entityDetailPath(identityInput) {
  14. const identity = entityIdentity(identityInput)
  15. const build = ENTITY_DETAIL_PATHS[identity.kind]
  16. return identity.valid && build ? build(identity.id, identityInput || {}) : ''
  17. }
  18. export async function fetchEntityDetail(identityInput, options = {}) {
  19. const identity = entityIdentity(identityInput)
  20. const path = options.path || entityDetailPath(identity)
  21. if (!identity.valid || !path) return { ok: false, identity, reason: 'invalid-identity', mode: '', data: null }
  22. const result = await requestCockpitJson(path, { config: options.config, fetchImpl: options.fetchImpl })
  23. if (!result.ok) return { ...result, identity, data: null }
  24. const detail = normalizeEntityDetail(identity, result.data, options.prefill)
  25. return detail
  26. ? { ok: true, identity, mode: result.mode || '', path: result.path || path, data: detail }
  27. : { ok: false, identity, mode: result.mode || '', path: result.path || path, reason: 'bad-payload', data: null }
  28. }