| 1234567891011121314151617181920212223242526272829303132 |
- 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]
- const targetEntity = relations[field].entity
- if (isArray(value)) {
- entity[field] = value.map((id: number) => {
- return UrlUtils.makeIRI(targetEntity, id)
- })
- } else {
- entity[field] = UrlUtils.makeIRI(targetEntity, value)
- }
- }
- return entity.$toJson()
- }
- }
- export default HydraNormalizer
|