import type ApiResource from '~/models/ApiResource' /** * Decorates an ApiResource's property to signal it as a field that is provided * as an IRI or an array of IRI by the API * * If the property is decorated, the HydraNormalizer will parse the IRI when de-normalizing * to get the id(s), then re-encode it as IRI(s) when re-normalizing. */ export function IriEncoded(apiResource: typeof ApiResource): PropertyDecorator { // We have to comply with the PropertyDecorator return type return (target: object, propertyKey: string | symbol) => { // @ts-expect-error The object is an ApiResource const self = target.$self() self.addIriEncodedField(propertyKey as string, apiResource) } } /** * Decorates an ApiResource's property to signal it as a field that is provided * as an unique identifier by the API * * If the property is decorated, the HydraNormalizer will get the item[idField] value to hydrate * the id property of the instance */ export function IdField(): PropertyDecorator { // We have to comply with the PropertyDecorator return type return (target: object, propertyKey: string | symbol) => { // @ts-expect-error The object is an ApiResource const self = target.$self() self.setIdField(propertyKey as string) } } export function Assert(assert: object): PropertyDecorator { // We have to comply with the PropertyDecorator return type return (target: object, propertyKey: string | symbol) => { // @ts-expect-error The object is an ApiResource const self = target.$self() const rules = self.getAsserts() rules[propertyKey] = assert self.setAsserts(rules) } } /** * * @constructor */ export function IdLess(): ClassDecorator { // We have to comply with the PropertyDecorator return type return (target) => { // @ts-expect-error The object is an ApiResource Class target.setIdLess() } }