enumManager.ts 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. import type { VueI18n } from 'vue-i18n'
  2. import type ApiRequestService from './apiRequestService'
  3. import UrlUtils from '~/services/utils/urlUtils'
  4. import HydraNormalizer from '~/services/data/normalizer/hydraNormalizer'
  5. import type { AnyJson, Collection, Enum } from '~/types/data.d'
  6. import type { EnumChoice } from '~/types/interfaces'
  7. class EnumManager {
  8. private apiRequestService: ApiRequestService
  9. private i18n: VueI18n
  10. public constructor(apiRequestService: ApiRequestService, i18n: VueI18n) {
  11. this.apiRequestService = apiRequestService
  12. this.i18n = i18n
  13. }
  14. public async fetch(enumName: string): Promise<Enum> {
  15. const url = UrlUtils.join('api', 'enum', enumName)
  16. const response = await this.apiRequestService.get(url)
  17. // @ts-expect-error J'ignore pourquoi, mais response ici est bien de type AnyJson, et non Response...
  18. const { data } = HydraNormalizer.denormalize(response) as { data: Collection<Record<string>> }
  19. const enum_: Enum = []
  20. for (const key in data.items) {
  21. if (Object.prototype.hasOwnProperty.call(data.items, key)) {
  22. enum_.push({
  23. value: data.items[key],
  24. label: this.i18n.t(data.items[key]),
  25. })
  26. }
  27. }
  28. return enum_
  29. }
  30. }
  31. export default EnumManager