|
@@ -10,7 +10,13 @@ import type ApiRequestService from './apiRequestService'
|
|
|
import UrlUtils from '~/services/utils/urlUtils'
|
|
import UrlUtils from '~/services/utils/urlUtils'
|
|
|
import type ApiModel from '~/models/ApiModel'
|
|
import type ApiModel from '~/models/ApiModel'
|
|
|
import type ApiResource from '~/models/ApiResource'
|
|
import type ApiResource from '~/models/ApiResource'
|
|
|
-import type { AnyJson, AssociativeArray, Collection } from '~/types/data.d'
|
|
|
|
|
|
|
+import type {
|
|
|
|
|
+ AnyJson,
|
|
|
|
|
+ ApiCollection,
|
|
|
|
|
+ ApiResponse,
|
|
|
|
|
+ AssociativeArray,
|
|
|
|
|
+ Collection,
|
|
|
|
|
+} from '~/types/data.d'
|
|
|
import modelsIndex from '~/models/models'
|
|
import modelsIndex from '~/models/models'
|
|
|
import HydraNormalizer from '~/services/data/normalizer/hydraNormalizer'
|
|
import HydraNormalizer from '~/services/data/normalizer/hydraNormalizer'
|
|
|
import ObjectUtils from '~/services/utils/objectUtils'
|
|
import ObjectUtils from '~/services/utils/objectUtils'
|
|
@@ -69,7 +75,7 @@ class EntityManager {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public getModel(instance: ApiResource): typeof ApiResource {
|
|
public getModel(instance: ApiResource): typeof ApiResource {
|
|
|
- return instance.constructor as typeof ApiModel
|
|
|
|
|
|
|
+ return instance.constructor as typeof ApiResource
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -181,6 +187,7 @@ class EntityManager {
|
|
|
const response = await this.apiRequestService.get(url)
|
|
const response = await this.apiRequestService.get(url)
|
|
|
|
|
|
|
|
// deserialize the response
|
|
// deserialize the response
|
|
|
|
|
+ // @ts-expect-error Response here is a Json Hydra response
|
|
|
const attributes = HydraNormalizer.denormalize(response, model)
|
|
const attributes = HydraNormalizer.denormalize(response, model)
|
|
|
.data as object
|
|
.data as object
|
|
|
|
|
|
|
@@ -196,13 +203,13 @@ class EntityManager {
|
|
|
*/
|
|
*/
|
|
|
public async fetchCollection <T extends typeof ApiResource>(
|
|
public async fetchCollection <T extends typeof ApiResource>(
|
|
|
model: T,
|
|
model: T,
|
|
|
- parent: T | null,
|
|
|
|
|
|
|
+ parent: InstanceType<T> | null,
|
|
|
query: Query | null = null,
|
|
query: Query | null = null,
|
|
|
): Promise<ComputedRef<Collection<InstanceType<T>>>> {
|
|
): Promise<ComputedRef<Collection<InstanceType<T>>>> {
|
|
|
let url
|
|
let url
|
|
|
|
|
|
|
|
if (parent !== null) {
|
|
if (parent !== null) {
|
|
|
- url = UrlUtils.join('api', parent.entity, '' + parent.id, model.entity)
|
|
|
|
|
|
|
+ url = UrlUtils.join('api', parent.$entity(), '' + String(parent.id), model.entity)
|
|
|
} else {
|
|
} else {
|
|
|
url = UrlUtils.join('api', model.entity)
|
|
url = UrlUtils.join('api', model.entity)
|
|
|
}
|
|
}
|
|
@@ -214,7 +221,8 @@ class EntityManager {
|
|
|
const response = await this.apiRequestService.get(url)
|
|
const response = await this.apiRequestService.get(url)
|
|
|
|
|
|
|
|
// deserialize the response
|
|
// deserialize the response
|
|
|
- const apiCollection = HydraNormalizer.denormalize(response, model)
|
|
|
|
|
|
|
+ // @ts-expect-error Response here is a Json Hydra response
|
|
|
|
|
+ const apiCollection = HydraNormalizer.denormalize(response, model) as ApiCollection
|
|
|
|
|
|
|
|
apiCollection.data.map((attributes: object) => {
|
|
apiCollection.data.map((attributes: object) => {
|
|
|
return this.newInstance(model, attributes)
|
|
return this.newInstance(model, attributes)
|
|
@@ -252,7 +260,7 @@ class EntityManager {
|
|
|
*
|
|
*
|
|
|
* @param instance
|
|
* @param instance
|
|
|
*/
|
|
*/
|
|
|
- public async persist<T extends ApiModel>(instance: T): Promise<T> {
|
|
|
|
|
|
|
+ public async persist<T extends ApiResource>(instance: T): Promise<T> {
|
|
|
const model = this.getModel(instance)
|
|
const model = this.getModel(instance)
|
|
|
|
|
|
|
|
let url = UrlUtils.join('api', model.entity)
|
|
let url = UrlUtils.join('api', model.entity)
|
|
@@ -284,7 +292,7 @@ class EntityManager {
|
|
|
this.removeTempAfterPersist(model, instance.id)
|
|
this.removeTempAfterPersist(model, instance.id)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- return newInstance
|
|
|
|
|
|
|
+ return newInstance as T
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -304,6 +312,7 @@ class EntityManager {
|
|
|
const body = JSON.stringify(data)
|
|
const body = JSON.stringify(data)
|
|
|
const response = await this.apiRequestService.patch(url, body)
|
|
const response = await this.apiRequestService.patch(url, body)
|
|
|
|
|
|
|
|
|
|
+ // @ts-expect-error Response here is a Json Hydra response
|
|
|
const hydraResponse = HydraNormalizer.denormalize(response, model)
|
|
const hydraResponse = HydraNormalizer.denormalize(response, model)
|
|
|
|
|
|
|
|
return this.newInstance(model, hydraResponse.data)
|
|
return this.newInstance(model, hydraResponse.data)
|