decorators.ts 725 B

123456789101112131415161718192021222324
  1. import ApiResource from "~/models/ApiResource";
  2. /**
  3. * Decorates an ApiResource's property to signal it as a field that is provided
  4. * as an IRI or an array of IRI by the API
  5. *
  6. * If the property is decorated, the HydraNormalizer will parse the IRI when de-normalizing
  7. * to get the id(s), then re-encode it as IRI(s) when re-normalizing.
  8. */
  9. export function IriEncoded (
  10. apiResource: typeof ApiResource
  11. ): PropertyDecorator {
  12. return (target: Object, propertyKey: string | symbol) => {
  13. //@ts-ignore
  14. const self = target.$self()
  15. if (!self.hasOwnProperty("relations")) {
  16. self.relations = {}
  17. }
  18. //@ts-ignore
  19. self.relations[propertyKey] = apiResource
  20. }
  21. }