decorators.ts 763 B

12345678910111213141516171819
  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(apiResource: typeof ApiResource): PropertyDecorator {
  10. // We have to comply with the PropertyDecorator return type
  11. // eslint-disable-next-line @typescript-eslint/ban-types
  12. return (target: Object, propertyKey: string | symbol) => {
  13. // @ts-expect-error The object is an ApiResource
  14. const self = target.$self()
  15. self.addIriEncodedField(propertyKey as string, apiResource)
  16. }
  17. }