hydraNormalizer__.ts 942 B

1234567891011121314151617181920212223242526272829303132
  1. import {AnyJson} from "~/types/data";
  2. import ApiResource from "~/models/ApiResource";
  3. import {isArray, isNumber} from "lodash";
  4. import UrlUtils from "~/services/utils/urlUtils";
  5. /**
  6. * Normalisation du format de données Hydra
  7. */
  8. class HydraNormalizer__ {
  9. public static normalizeEntity(entity: ApiResource): AnyJson {
  10. const prototype = Object.getPrototypeOf(entity)
  11. const relations = prototype.constructor.relations
  12. for (const field in relations) {
  13. const value = entity[field]
  14. const targetEntity = relations[field].entity
  15. if (isArray(value)) {
  16. entity[field] = value.map((id: number) => {
  17. return UrlUtils.makeIRI(targetEntity, id)
  18. })
  19. } else {
  20. entity[field] = UrlUtils.makeIRI(targetEntity, value)
  21. }
  22. }
  23. return entity.$toJson()
  24. }
  25. }
  26. export default HydraNormalizer