|
|
@@ -1,7 +1,7 @@
|
|
|
import * as _ from 'lodash-es'
|
|
|
-import type { AnyJson, ApiResponse, HydraMetadata } from '~/types/data'
|
|
|
+import type {AnyJson, ApiResponse, HydraMetadata} from '~/types/data'
|
|
|
import UrlUtils from '~/services/utils/urlUtils'
|
|
|
-import { METADATA_TYPE } from '~/types/enum/data'
|
|
|
+import {METADATA_TYPE} from '~/types/enum/data'
|
|
|
import models from '~/models/models'
|
|
|
import ApiResource from '~/models/ApiResource'
|
|
|
|
|
|
@@ -76,7 +76,7 @@ class HydraNormalizer {
|
|
|
protected static getMetadata(data: AnyJson): HydraMetadata {
|
|
|
if (data['@type'] !== 'hydra:Collection') {
|
|
|
// A single item, no metadata
|
|
|
- return { type: METADATA_TYPE.ITEM }
|
|
|
+ return {type: METADATA_TYPE.ITEM}
|
|
|
}
|
|
|
|
|
|
const metadata: HydraMetadata = {
|
|
|
@@ -163,17 +163,16 @@ class HydraNormalizer {
|
|
|
|
|
|
throw new Error(
|
|
|
'De-normalization error : Could not determine the type of the entity ' +
|
|
|
- item['@id'] +
|
|
|
- ' (found: ' +
|
|
|
- entity +
|
|
|
- ')',
|
|
|
+ item['@id'] +
|
|
|
+ ' (found: ' +
|
|
|
+ entity +
|
|
|
+ ')',
|
|
|
)
|
|
|
}
|
|
|
|
|
|
protected static denormalizeEntity(model: typeof ApiResource, item: AnyJson) {
|
|
|
- if (item.id === undefined) {
|
|
|
- throw new Error('Missing id field for ' + model)
|
|
|
- }
|
|
|
+
|
|
|
+ item['id'] = this.getItemIdValue(model, item)
|
|
|
|
|
|
// eslint-disable-next-line new-cap
|
|
|
const instance = new model(item)
|
|
|
@@ -236,18 +235,36 @@ class HydraNormalizer {
|
|
|
iri: string,
|
|
|
expectedEntity: string,
|
|
|
): number | string {
|
|
|
- const { entity, id } = HydraNormalizer.parseEntityIRI(iri)
|
|
|
+ const {entity, id} = HydraNormalizer.parseEntityIRI(iri)
|
|
|
if (entity !== expectedEntity) {
|
|
|
throw new Error(
|
|
|
"IRI entity does not match the field's target entity (" +
|
|
|
- entity +
|
|
|
- ' != ' +
|
|
|
- expectedEntity +
|
|
|
- ')',
|
|
|
+ entity +
|
|
|
+ ' != ' +
|
|
|
+ expectedEntity +
|
|
|
+ ')',
|
|
|
)
|
|
|
}
|
|
|
return id
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Get the id value of an item
|
|
|
+ * @param model
|
|
|
+ * @param item
|
|
|
+ * @protected
|
|
|
+ */
|
|
|
+ protected static getItemIdValue(model: typeof ApiResource, item: AnyJson) {
|
|
|
+ if (item.id !== undefined) {
|
|
|
+ return item.id
|
|
|
+ }
|
|
|
+
|
|
|
+ if (model.getIdField() !== undefined && item[model.getIdField()] !== undefined) {
|
|
|
+ return item[model.getIdField()]
|
|
|
+ }
|
|
|
+
|
|
|
+ throw new Error('Missing id field or @IdField decorator for ' + model)
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
export default HydraNormalizer
|