Skip to content

Commit

Permalink
fix: missing jsona types
Browse files Browse the repository at this point in the history
  • Loading branch information
Андрис Цилдерманис authored and olosegres committed Sep 23, 2023
1 parent 0390772 commit 3710b27
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 33 deletions.
31 changes: 18 additions & 13 deletions src/JsonaTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,26 +45,26 @@ export interface IJsonaDeserializer extends IJsonaModelBuilder {
}

export interface IJsonDeserializerConstructor {
new(propertiesMapper: IJsonPropertiesMapper, deserializeCache: IDeserializeCache, options);
new(propertiesMapper: IJsonPropertiesMapper, deserializeCache: IDeserializeCache, options?: TDeserializeOptions): IJsonaDeserializer;
}

export interface IModelsSerializer {
setPropertiesMapper(propertiesMapper: IModelPropertiesMapper);
setStuff(stuff);
setIncludeNames(includeNames: TJsonaDenormalizedIncludeNames | TJsonaNormalizedIncludeNamesTree);
setPropertiesMapper(propertiesMapper: IModelPropertiesMapper): void;
setStuff(stuff: TJsonaModel | TJsonaModel[]): void;
setIncludeNames(includeNames: TJsonaDenormalizedIncludeNames | TJsonaNormalizedIncludeNamesTree): void;
build(): TJsonApiBody;
buildDataByModel(model: TJsonaModel | null): TJsonApiData;
buildRelationshipsByModel(model: TJsonaModel);
buildRelationshipsByModel(model: TJsonaModel): TJsonaRelationshipDataItem[] | TJsonaModel | TJsonaRelationshipBuild;
buildIncludedByModel(
model: TJsonaModel,
includeTree: TJsonaNormalizedIncludeNamesTree,
builtIncluded: TJsonaUniqueIncluded
model: TJsonaModel,
includeTree: TJsonaNormalizedIncludeNamesTree,
builtIncluded: TJsonaUniqueIncluded
): void;
buildIncludedItem(
relationModel: TJsonaModel,
subIncludeTree: TJsonaNormalizedIncludeNamesTree,
builtIncluded: TJsonaUniqueIncluded
);
relationModel: TJsonaModel,
subIncludeTree: TJsonaNormalizedIncludeNamesTree,
builtIncluded: TJsonaUniqueIncluded
): void;
}

export interface IModelsSerializerConstructor {
Expand Down Expand Up @@ -113,7 +113,7 @@ type LinkKey = "self" | "related" | "first" | "prev" | "next" | "last";
type LinkObjectMember = string | { href?: string; meta?: TAnyKeyValueObject } | null;

export type TJsonApiLinks = {
[key in LinkKey]?: LinkObjectMember;
[key in LinkKey]?: LinkObjectMember;
};

export type TJsonApiRelationships = {
Expand Down Expand Up @@ -155,6 +155,11 @@ export type TJsonaModel = {
[propertyName: string]: any
};

export type TJsonaRelationshipDataItem = {
id?: number | string,
type: string;
}

export type TResourceIdObj = {
id?: string | number,
type: string,
Expand Down
41 changes: 21 additions & 20 deletions src/builders/ModelsSerializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import {
TJsonaNormalizedIncludeNamesTree,
TJsonaUniqueIncluded,
IModelPropertiesMapper,
IModelsSerializer
IModelsSerializer,
TJsonaRelationshipDataItem,
} from '../JsonaTypes';

import {createIncludeNamesTree} from '../utils';
Expand All @@ -27,7 +28,7 @@ export class ModelsSerializer implements IModelsSerializer {
this.propertiesMapper = propertiesMapper;
}

setStuff(stuff) {
setStuff(stuff: TJsonaModel | TJsonaModel[]) {
this.stuff = stuff;
}

Expand Down Expand Up @@ -61,13 +62,13 @@ export class ModelsSerializer implements IModelsSerializer {

for (let i = 0; i < collectionLength; i++) {
data.push(
this.buildDataByModel(stuff[i])
this.buildDataByModel(stuff[i])
);

this.buildIncludedByModel(
stuff[i],
this.includeNamesTree,
uniqueIncluded
stuff[i],
this.includeNamesTree,
uniqueIncluded
);
}

Expand All @@ -77,9 +78,9 @@ export class ModelsSerializer implements IModelsSerializer {
body['data'] = this.buildDataByModel(stuff);

this.buildIncludedByModel(
stuff,
this.includeNamesTree,
uniqueIncluded
stuff,
this.includeNamesTree,
uniqueIncluded
);
} else if (stuff === null) {
body['data'] = null;
Expand Down Expand Up @@ -115,7 +116,7 @@ export class ModelsSerializer implements IModelsSerializer {
return data;
}

buildResourceObjectPart(relation: TJsonaModel) {
buildResourceObjectPart(relation: TJsonaModel): TJsonaRelationshipDataItem {
const id = this.propertiesMapper.getId(relation);
const type = this.propertiesMapper.getType(relation);

Expand Down Expand Up @@ -147,9 +148,9 @@ export class ModelsSerializer implements IModelsSerializer {
relationshipData.push(relationshipDataItem);
} else {
console.error(
`Can't create data item for relationship ${k},
`Can't create data item for relationship ${k},
it doesn't have 'id' or 'type', it was skipped`,
relationItem
relationItem
);
}
}
Expand All @@ -166,8 +167,8 @@ export class ModelsSerializer implements IModelsSerializer {
};
} else {
console.error(
`Can't create data for relationship ${k}, it doesn't have 'type', it was skipped`,
relation
`Can't create data for relationship ${k}, it doesn't have 'type', it was skipped`,
relation
);
}
} else {
Expand All @@ -181,9 +182,9 @@ export class ModelsSerializer implements IModelsSerializer {
}

buildIncludedByModel(
model: TJsonaModel,
includeTree: TJsonaNormalizedIncludeNamesTree,
builtIncluded: TJsonaUniqueIncluded = {}
model: TJsonaModel,
includeTree: TJsonaNormalizedIncludeNamesTree,
builtIncluded: TJsonaUniqueIncluded = {}
): void {
if (!includeTree || !Object.keys(includeTree).length) {
return;
Expand Down Expand Up @@ -217,9 +218,9 @@ export class ModelsSerializer implements IModelsSerializer {
}

buildIncludedItem(
relationModel: TJsonaModel,
subIncludeTree: TJsonaNormalizedIncludeNamesTree,
builtIncluded: TJsonaUniqueIncluded
relationModel: TJsonaModel,
subIncludeTree: TJsonaNormalizedIncludeNamesTree,
builtIncluded: TJsonaUniqueIncluded
) {
const id = this.propertiesMapper.getId(relationModel);
const type = this.propertiesMapper.getType(relationModel);
Expand Down

0 comments on commit 3710b27

Please sign in to comment.