| 12345678910111213141516171819202122232425262728293031323334353637 |
- import {AnyJson} from "~/types/data";
- import ApiResource from "~/models/ApiResource";
- import {isArray, isNumber} from "lodash";
- import UrlUtils from "~/services/utils/urlUtils";
- /**
- * Normalisation du format de données Hydra
- */
- class HydraNormalizer {
- public static normalizeEntity(entity: ApiResource): AnyJson {
- const prototype = Object.getPrototypeOf(entity)
- const relations = prototype.constructor.relations
- for (const field in relations) {
- const value = entity[field]
- if (!isArray(value)) {
- console.warn("A model's relation is not an array")
- continue
- }
- const targetEntity = relations[field].entity
- entity[field] = value.map((id: number | string) => {
- if (!isNumber(id)) {
- throw Error(field + ' : invalid id')
- }
- return UrlUtils.makeIRI(targetEntity, id)
- })
- }
- return entity.$toJson()
- }
- }
- export default HydraNormalizer
|