Comparing version 1.1.10 to 1.1.11
@@ -6,2 +6,3 @@ import { IJsonPropertiesMapper, TJsonaModel, TJsonaRelationships, TJsonApiBody, TJsonApiData, IJsonaModelBuilder } from '../JsonaTypes'; | ||
protected includedInObject: any; | ||
protected dataInObject: any; | ||
protected cachedModels: {}; | ||
@@ -14,3 +15,3 @@ constructor(propertiesMapper: any); | ||
buildRelationsByData(data: TJsonApiData, model: TJsonaModel): TJsonaRelationships | null; | ||
buildDataFromIncluded(id: string | number, type: string): TJsonApiData; | ||
buildDataFromIncludedOrData(id: string | number, type: string): TJsonApiData; | ||
buildIncludedInObject(): { | ||
@@ -17,0 +18,0 @@ [key: string]: TJsonApiData; |
@@ -22,5 +22,5 @@ "use strict"; | ||
var data = this.body.data; | ||
var staff; | ||
var stuff; | ||
if (Array.isArray(data)) { | ||
staff = []; | ||
stuff = []; | ||
var collectionLength = data.length; | ||
@@ -31,3 +31,3 @@ for (var i = 0; i < collectionLength; i++) { | ||
if (model) { | ||
staff.push(model); | ||
stuff.push(model); | ||
} | ||
@@ -38,5 +38,5 @@ } | ||
else if (data) { | ||
staff = this.buildModelByData(data); | ||
stuff = this.buildModelByData(data); | ||
} | ||
return staff; | ||
return stuff; | ||
}; | ||
@@ -46,3 +46,3 @@ JsonDeserializer.prototype.buildModelByData = function (data) { | ||
var model; | ||
if (entityKey) { | ||
if (entityKey && Object.keys(data).length === 2) { | ||
// checks for built model in cachedModels is a protection from creating models on recursive relationships | ||
@@ -84,4 +84,9 @@ model = this.cachedModels[entityKey]; | ||
var relationItemsLength = relation.data.length; | ||
var relationItem = void 0; | ||
for (var i = 0; i < relationItemsLength; i++) { | ||
var dataItem = this.buildDataFromIncluded(relation.data[i].id, relation.data[i].type); | ||
relationItem = relation.data[i]; | ||
if (!relationItem) { | ||
return; | ||
} | ||
var dataItem = this.buildDataFromIncludedOrData(relationItem.id, relationItem.type); | ||
readyRelations[k].push(this.buildModelByData(dataItem)); | ||
@@ -91,5 +96,8 @@ } | ||
else if (relation.data) { | ||
var dataItem = this.buildDataFromIncluded(relation.data.id, relation.data.type); | ||
var dataItem = this.buildDataFromIncludedOrData(relation.data.id, relation.data.type); | ||
readyRelations[k] = this.buildModelByData(dataItem); | ||
} | ||
else if (relation.data === null) { | ||
readyRelations[k] = null; | ||
} | ||
if (relation.links) { | ||
@@ -114,3 +122,3 @@ var setRelationshipLinks = this.pm.setRelationshipLinks; | ||
}; | ||
JsonDeserializer.prototype.buildDataFromIncluded = function (id, type) { | ||
JsonDeserializer.prototype.buildDataFromIncludedOrData = function (id, type) { | ||
var included = this.buildIncludedInObject(); | ||
@@ -117,0 +125,0 @@ var dataItem = included[type + id]; |
import { TJsonaModel, TJsonApiBody, TJsonApiData, TJsonaDenormalizedIncludeNames, TJsonaNormalizedIncludeNamesTree, TJsonaUniqueIncluded, IModelPropertiesMapper } from '../JsonaTypes'; | ||
declare class ModelsSerializer { | ||
protected propertiesMapper: IModelPropertiesMapper; | ||
protected staff: TJsonaModel | Array<TJsonaModel>; | ||
protected stuff: TJsonaModel | Array<TJsonaModel>; | ||
protected includeNamesTree: TJsonaNormalizedIncludeNamesTree; | ||
constructor(propertiesMapper?: IModelPropertiesMapper); | ||
setPropertiesMapper(propertiesMapper: IModelPropertiesMapper): void; | ||
setStuff(staff: any): void; | ||
setStuff(stuff: any): void; | ||
setIncludeNames(includeNames: TJsonaDenormalizedIncludeNames | TJsonaNormalizedIncludeNamesTree): void; | ||
@@ -10,0 +10,0 @@ build(): TJsonApiBody; |
@@ -11,4 +11,4 @@ "use strict"; | ||
}; | ||
ModelsSerializer.prototype.setStuff = function (staff) { | ||
this.staff = staff; | ||
ModelsSerializer.prototype.setStuff = function (stuff) { | ||
this.stuff = stuff; | ||
}; | ||
@@ -28,8 +28,8 @@ ModelsSerializer.prototype.setIncludeNames = function (includeNames) { | ||
ModelsSerializer.prototype.build = function () { | ||
var _a = this, staff = _a.staff, propertiesMapper = _a.propertiesMapper; | ||
var _a = this, stuff = _a.stuff, propertiesMapper = _a.propertiesMapper; | ||
if (!propertiesMapper || typeof propertiesMapper !== 'object') { | ||
throw new Error('ModelsSerializer cannot build, propertiesMapper is not set'); | ||
} | ||
else if (!staff || typeof staff !== 'object') { | ||
throw new Error('ModelsSerializer cannot build, staff is not set'); | ||
else if (!stuff || typeof stuff !== 'object') { | ||
throw new Error('ModelsSerializer cannot build, stuff is not set'); | ||
} | ||
@@ -39,16 +39,16 @@ var body = {}; | ||
var uniqueIncluded = {}; | ||
if (staff && Array.isArray(staff)) { | ||
var collectionLength = staff.length; | ||
if (stuff && Array.isArray(stuff)) { | ||
var collectionLength = stuff.length; | ||
var data = []; | ||
for (var i = 0; i < collectionLength; i++) { | ||
data.push(this.buildDataByModel(staff[i])); | ||
this.buildIncludedByModel(staff[i], this.includeNamesTree, uniqueIncluded); | ||
data.push(this.buildDataByModel(stuff[i])); | ||
this.buildIncludedByModel(stuff[i], this.includeNamesTree, uniqueIncluded); | ||
} | ||
body['data'] = data; | ||
} | ||
else if (staff) { | ||
body['data'] = this.buildDataByModel(staff); | ||
this.buildIncludedByModel(staff, this.includeNamesTree, uniqueIncluded); | ||
else if (stuff) { | ||
body['data'] = this.buildDataByModel(stuff); | ||
this.buildIncludedByModel(stuff, this.includeNamesTree, uniqueIncluded); | ||
} | ||
else if (staff === null) { | ||
else if (stuff === null) { | ||
body['data'] = null; | ||
@@ -55,0 +55,0 @@ } |
{ | ||
"name": "jsona", | ||
"description": "Provide data formatters (data model builder & json builder) to work with JSON API specification v1.0 in your JavaScript / TypeScript code", | ||
"version": "1.1.10", | ||
"version": "1.1.11", | ||
"keywords": [ | ||
@@ -6,0 +6,0 @@ "json-api", |
@@ -23,2 +23,3 @@ import { | ||
protected includedInObject; | ||
protected dataInObject; | ||
protected cachedModels = {}; | ||
@@ -40,6 +41,6 @@ | ||
const {data} = this.body; | ||
let staff; | ||
let stuff; | ||
if (Array.isArray(data)) { | ||
staff = []; | ||
stuff = []; | ||
const collectionLength = data.length; | ||
@@ -52,3 +53,3 @@ | ||
if (model) { | ||
staff.push(model); | ||
stuff.push(model); | ||
} | ||
@@ -58,6 +59,6 @@ } | ||
} else if (data) { | ||
staff = this.buildModelByData(data); | ||
stuff = this.buildModelByData(data); | ||
} | ||
return staff; | ||
return stuff; | ||
} | ||
@@ -71,3 +72,3 @@ | ||
if (entityKey) { | ||
if (entityKey && Object.keys(data).length === 2) { | ||
// checks for built model in cachedModels is a protection from creating models on recursive relationships | ||
@@ -123,6 +124,14 @@ model = this.cachedModels[entityKey]; | ||
const relationItemsLength = relation.data.length; | ||
let relationItem; | ||
for (let i = 0; i < relationItemsLength; i++) { | ||
let dataItem = this.buildDataFromIncluded( | ||
relation.data[i].id, | ||
relation.data[i].type | ||
relationItem = relation.data[i]; | ||
if (!relationItem) { | ||
return; | ||
} | ||
let dataItem = this.buildDataFromIncludedOrData( | ||
relationItem.id, | ||
relationItem.type | ||
); | ||
@@ -134,4 +143,6 @@ readyRelations[k].push( | ||
} else if (relation.data) { | ||
let dataItem = this.buildDataFromIncluded(relation.data.id, relation.data.type); | ||
let dataItem = this.buildDataFromIncludedOrData(relation.data.id, relation.data.type); | ||
readyRelations[k] = this.buildModelByData(dataItem); | ||
} else if (relation.data === null) { | ||
readyRelations[k] = null; | ||
} | ||
@@ -162,3 +173,3 @@ | ||
buildDataFromIncluded(id: string | number, type: string): TJsonApiData { | ||
buildDataFromIncludedOrData(id: string | number, type: string): TJsonApiData { | ||
const included = this.buildIncludedInObject(); | ||
@@ -189,4 +200,5 @@ const dataItem = included[type + id]; | ||
} | ||
} | ||
export default JsonDeserializer; |
@@ -16,3 +16,3 @@ import { | ||
protected propertiesMapper: IModelPropertiesMapper; | ||
protected staff: TJsonaModel | Array<TJsonaModel>; | ||
protected stuff: TJsonaModel | Array<TJsonaModel>; | ||
protected includeNamesTree: TJsonaNormalizedIncludeNamesTree; | ||
@@ -28,4 +28,4 @@ | ||
setStuff(staff) { | ||
this.staff = staff; | ||
setStuff(stuff) { | ||
this.stuff = stuff; | ||
} | ||
@@ -46,8 +46,8 @@ | ||
build(): TJsonApiBody { | ||
const {staff, propertiesMapper} = this; | ||
const {stuff, propertiesMapper} = this; | ||
if (!propertiesMapper || typeof propertiesMapper !== 'object') { | ||
throw new Error('ModelsSerializer cannot build, propertiesMapper is not set'); | ||
} else if (!staff || typeof staff !== 'object') { | ||
throw new Error('ModelsSerializer cannot build, staff is not set'); | ||
} else if (!stuff || typeof stuff !== 'object') { | ||
throw new Error('ModelsSerializer cannot build, stuff is not set'); | ||
} | ||
@@ -59,4 +59,4 @@ | ||
if (staff && Array.isArray(staff)) { | ||
const collectionLength = staff.length; | ||
if (stuff && Array.isArray(stuff)) { | ||
const collectionLength = stuff.length; | ||
const data = []; | ||
@@ -66,7 +66,7 @@ | ||
data.push( | ||
this.buildDataByModel(staff[i]) | ||
this.buildDataByModel(stuff[i]) | ||
); | ||
this.buildIncludedByModel( | ||
staff[i], | ||
stuff[i], | ||
this.includeNamesTree, | ||
@@ -79,11 +79,11 @@ uniqueIncluded | ||
} else if (staff) { | ||
body['data'] = this.buildDataByModel(staff); | ||
} else if (stuff) { | ||
body['data'] = this.buildDataByModel(stuff); | ||
this.buildIncludedByModel( | ||
staff, | ||
stuff, | ||
this.includeNamesTree, | ||
uniqueIncluded | ||
); | ||
} else if (staff === null) { | ||
} else if (stuff === null) { | ||
body['data'] = null; | ||
@@ -90,0 +90,0 @@ } |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Native code
Supply chain riskContains native code (e.g., compiled binaries or shared libraries). Including native code can obscure malicious behavior.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
125927
40
1991
1