useEntity.ts 878 B

123456789101112131415161718192021222324252627282930313233
  1. // Copyright (C) 2012-2025 Zammad Foundation, https://zammad-foundation.org/
  2. import { keyBy } from 'lodash-es'
  3. import type { EnumObjectManagerObjects } from '#shared/graphql/types.ts'
  4. import type { EntityObject } from '#shared/types/entity.ts'
  5. export type EntityName = string | EnumObjectManagerObjects
  6. export interface EntityPlugin<T = EntityObject> {
  7. name: EntityName
  8. display: (object: T) => string
  9. }
  10. const entityModules: Record<string, EntityPlugin> = import.meta.glob(
  11. ['./*/entity.ts'],
  12. {
  13. eager: true,
  14. import: 'default',
  15. },
  16. )
  17. export const entities = Object.values(entityModules)
  18. export const entitiesByName = keyBy(entities, 'name')
  19. export const useEntity = (entity: EntityName) => {
  20. // TODO: only log or really an error?
  21. if (!entitiesByName[entity]) {
  22. throw new Error(`Entity "${entity}" not found`)
  23. }
  24. return entitiesByName[entity]
  25. }