|
|
@@ -1,6 +1,7 @@
|
|
|
-import { AnyJson } from '~/types/interfaces'
|
|
|
+import {AnyJson, ApiResponse, HydraMetadata} from '~/types/interfaces'
|
|
|
import BaseDenormalizer from '~/services/serializer/denormalizer/baseDenormalizer'
|
|
|
-import { DENORMALIZER_TYPE } from '~/types/enums'
|
|
|
+import {DENORMALIZER_TYPE, METADATA_TYPE} from '~/types/enums'
|
|
|
+import {parseInt} from "lodash";
|
|
|
|
|
|
/**
|
|
|
* Classe permettant d'assurer la dénormalization d'un objet Hydra en JSON
|
|
|
@@ -25,27 +26,40 @@ class Hydra extends BaseDenormalizer {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
+ private static parseItem (hydraData: AnyJson): ApiResponse {
|
|
|
+ const itemResponse: ApiResponse = {
|
|
|
+ data: hydraData,
|
|
|
+ metadata: Hydra.definedMetadataForItem(hydraData)
|
|
|
+ }
|
|
|
+ return itemResponse
|
|
|
+ }
|
|
|
+ /**
|
|
|
* Méthode de parsing appelé si on est dans un GET
|
|
|
*
|
|
|
* @param {AnyJson} hydraData
|
|
|
*/
|
|
|
- private static parseItem (hydraData: AnyJson): AnyJson {
|
|
|
- if (hydraData['hydra:previous']) {
|
|
|
- const iriParts = hydraData['hydra:previous'].split('/')
|
|
|
- hydraData.previous = iriParts[iriParts.length - 1]
|
|
|
- }
|
|
|
- if (hydraData['hydra:next']) {
|
|
|
- const iriParts = hydraData['hydra:next'].split('/')
|
|
|
- hydraData.next = iriParts[iriParts.length - 1]
|
|
|
- }
|
|
|
- if (hydraData['hydra:totalItems']) {
|
|
|
- hydraData.totalItems = hydraData['hydra:totalItems']
|
|
|
- }
|
|
|
- if (hydraData['hydra:itemPosition']) {
|
|
|
- hydraData.itemPosition = hydraData['hydra:itemPosition']
|
|
|
- }
|
|
|
- return hydraData
|
|
|
+ private static definedMetadataForItem (hydraData: AnyJson): AnyJson {
|
|
|
+ const metadata:HydraMetadata = {}
|
|
|
+
|
|
|
+ // if (hydraData['hydra:previous']) {
|
|
|
+ // const iriParts = hydraData['hydra:previous'].split('/')
|
|
|
+ // hydraData.previous = iriParts[iriParts.length - 1]
|
|
|
+ // }
|
|
|
+ // if (hydraData['hydra:next']) {
|
|
|
+ // const iriParts = hydraData['hydra:next'].split('/')
|
|
|
+ // hydraData.next = iriParts[iriParts.length - 1]
|
|
|
+ // }
|
|
|
+ // if (hydraData['hydra:totalItems']) {
|
|
|
+ // hydraData.totalItems = hydraData['hydra:totalItems']
|
|
|
+ // }
|
|
|
+ // if (hydraData['hydra:itemPosition']) {
|
|
|
+ // hydraData.itemPosition = hydraData['hydra:itemPosition']
|
|
|
+ // }
|
|
|
+
|
|
|
+ metadata.type = METADATA_TYPE.ITEM
|
|
|
+
|
|
|
+ return metadata
|
|
|
+
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -53,40 +67,62 @@ class Hydra extends BaseDenormalizer {
|
|
|
*
|
|
|
* @param {AnyJson} hydraData
|
|
|
*/
|
|
|
- private static parseCollection (hydraData: AnyJson): AnyJson {
|
|
|
- const collectionResponse = hydraData['hydra:member']
|
|
|
- collectionResponse.metadata = {}
|
|
|
- collectionResponse.order = {}
|
|
|
- collectionResponse.search = {}
|
|
|
-
|
|
|
- // Put metadata in a property of the collection
|
|
|
- for (const key in hydraData) {
|
|
|
- const value = hydraData[key]
|
|
|
- if (key !== 'hydra:member') {
|
|
|
- collectionResponse.metadata[key] = value
|
|
|
- }
|
|
|
+ private static parseCollection (hydraData: AnyJson): ApiResponse {
|
|
|
+ const collectionResponse:ApiResponse = {
|
|
|
+ data:hydraData['hydra:member'],
|
|
|
+ metadata : Hydra.definedMetadataForCollection(hydraData)
|
|
|
}
|
|
|
|
|
|
+ // collectionResponse.order = {}
|
|
|
+ // collectionResponse.search = {}
|
|
|
+
|
|
|
+
|
|
|
// Populate href property for all elements of the collection
|
|
|
- for (const key in collectionResponse) {
|
|
|
- const value = collectionResponse[key]
|
|
|
+ for (const key in collectionResponse.data) {
|
|
|
+ const value = collectionResponse.data[key]
|
|
|
Hydra.populateAllData(value)
|
|
|
}
|
|
|
|
|
|
- if (typeof (hydraData['hydra:search']) !== 'undefined') {
|
|
|
- const collectionSearch = hydraData['hydra:search']['hydra:mapping']
|
|
|
- for (const key in collectionSearch) {
|
|
|
- const value = collectionSearch[key]
|
|
|
- if (value.variable.indexOf('filter[order]') === 0) {
|
|
|
- collectionResponse.order[value.property] = value
|
|
|
- } else if (value.variable.indexOf('filter[where]') === 0) {
|
|
|
- collectionResponse.search[value.property] = value
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
+ // if (typeof (hydraData['hydra:search']) !== 'undefined') {
|
|
|
+ // const collectionSearch = hydraData['hydra:search']['hydra:mapping']
|
|
|
+ // for (const key in collectionSearch) {
|
|
|
+ // const value = collectionSearch[key]
|
|
|
+ // if (value.variable.indexOf('filter[order]') === 0) {
|
|
|
+ // collectionResponse.order[value.property] = value
|
|
|
+ // } else if (value.variable.indexOf('filter[where]') === 0) {
|
|
|
+ // collectionResponse.search[value.property] = value
|
|
|
+ // }
|
|
|
+ // }
|
|
|
+ // }
|
|
|
+
|
|
|
return collectionResponse
|
|
|
}
|
|
|
|
|
|
+ private static definedMetadataForCollection(data:AnyJson){
|
|
|
+ const metadata:HydraMetadata = {
|
|
|
+ totalItems: data['hydra:totalItems']
|
|
|
+ }
|
|
|
+
|
|
|
+ if(data['hydra:view']){
|
|
|
+ metadata.firstPage = Hydra.getPageNumber(data['hydra:view']['hydra:first'])
|
|
|
+ metadata.lastPage = Hydra.getPageNumber(data['hydra:view']['hydra:last'])
|
|
|
+ metadata.nextPage = Hydra.getPageNumber(data['hydra:view']['hydra:next'])
|
|
|
+ metadata.previousPage = Hydra.getPageNumber(data['hydra:view']['hydra:previous'])
|
|
|
+ }
|
|
|
+
|
|
|
+ metadata.type = METADATA_TYPE.COLLECTION
|
|
|
+
|
|
|
+ return metadata
|
|
|
+ }
|
|
|
+
|
|
|
+ private static getPageNumber(uri:string):number {
|
|
|
+ if(uri){
|
|
|
+ const number = uri.split('page=').pop()
|
|
|
+ return number ? parseInt(number) : 0
|
|
|
+ }
|
|
|
+ return 0
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* Hydrate l'objet JSON de façon récursive (afin de gérer les objet nested)
|
|
|
*
|