Comparing version 1.10.1 to 1.11.0
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.JsonDeserializer = void 0; | ||
var JsonDeserializer = /** @class */ (function () { | ||
var JsonDeserializer = (function () { | ||
function JsonDeserializer(propertiesMapper, deserializeCache, options) { | ||
@@ -46,3 +46,2 @@ this.preferNestedDataFromData = false; | ||
JsonDeserializer.prototype.buildModelByData = function (data, resourceIdObj) { | ||
if (resourceIdObj === void 0) { resourceIdObj = {}; } | ||
var cachedModel = this.dc.getCachedModel(data, resourceIdObj); | ||
@@ -53,3 +52,3 @@ if (cachedModel) { | ||
var model = this.pm.createModel(data.type); | ||
this.dc.handleModel(model, data, resourceIdObj); // should be called before this.pm.setRelationships(model, relationships); | ||
this.dc.handleModel(model, data, resourceIdObj); | ||
if (model) { | ||
@@ -66,3 +65,3 @@ this.pm.setId(model, data.id); | ||
} | ||
if (resourceIdObj.meta) { | ||
if (resourceIdObj === null || resourceIdObj === void 0 ? void 0 : resourceIdObj.meta) { | ||
this.pm.setResourceIdObjMeta(model, resourceIdObj.meta); | ||
@@ -89,3 +88,3 @@ } | ||
if (!resourceIdObj) { | ||
return; | ||
continue; | ||
} | ||
@@ -92,0 +91,0 @@ var dataItem = this.buildDataFromIncludedOrData(resourceIdObj.id, resourceIdObj.type); |
@@ -6,2 +6,3 @@ import { TJsonaModel, TJsonApiBody, TJsonApiData, TJsonaDenormalizedIncludeNames, TJsonaNormalizedIncludeNamesTree, TJsonaUniqueIncluded, IModelPropertiesMapper, IModelsSerializer } from '../JsonaTypes'; | ||
protected includeNamesTree: TJsonaNormalizedIncludeNamesTree; | ||
private buildIncludedIndex; | ||
constructor(propertiesMapper?: IModelPropertiesMapper); | ||
@@ -13,2 +14,6 @@ setPropertiesMapper(propertiesMapper: IModelPropertiesMapper): void; | ||
buildDataByModel(model: TJsonaModel | null): TJsonApiData; | ||
buildResourceObjectPart(relation: TJsonaModel): { | ||
id?: string | number; | ||
type: string; | ||
}; | ||
buildRelationshipsByModel(model: TJsonaModel): {}; | ||
@@ -15,0 +20,0 @@ buildIncludedByModel(model: TJsonaModel, includeTree: TJsonaNormalizedIncludeNamesTree, builtIncluded?: TJsonaUniqueIncluded): void; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.ModelsSerializer = void 0; | ||
var tslib_1 = require("tslib"); | ||
var utils_1 = require("../utils"); | ||
var ModelsSerializer = /** @class */ (function () { | ||
var ModelsSerializer = (function () { | ||
function ModelsSerializer(propertiesMapper) { | ||
propertiesMapper && this.setPropertiesMapper(propertiesMapper); | ||
this.buildIncludedIndex = 0; | ||
} | ||
@@ -19,3 +21,3 @@ ModelsSerializer.prototype.setPropertiesMapper = function (propertiesMapper) { | ||
includeNames.forEach(function (namesChain) { | ||
utils_1.createIncludeNamesTree(namesChain, includeNamesTree_1); | ||
(0, utils_1.createIncludeNamesTree)(namesChain, includeNamesTree_1); | ||
}); | ||
@@ -55,7 +57,3 @@ this.includeNamesTree = includeNamesTree_1; | ||
if (Object.keys(uniqueIncluded).length) { | ||
body['included'] = []; | ||
var includeUniqueKeys = Object.keys(uniqueIncluded); | ||
includeUniqueKeys.forEach(function (k) { | ||
body['included'].push(uniqueIncluded[k]); | ||
}); | ||
body['included'] = Object.values(uniqueIncluded); | ||
} | ||
@@ -65,7 +63,6 @@ return body; | ||
ModelsSerializer.prototype.buildDataByModel = function (model) { | ||
var data = { | ||
id: this.propertiesMapper.getId(model), | ||
type: this.propertiesMapper.getType(model), | ||
attributes: this.propertiesMapper.getAttributes(model), | ||
}; | ||
var id = this.propertiesMapper.getId(model); | ||
var type = this.propertiesMapper.getType(model); | ||
var attributes = this.propertiesMapper.getAttributes(model); | ||
var data = tslib_1.__assign(tslib_1.__assign({ type: type }, (typeof id !== 'undefined' ? { id: id } : {})), (typeof attributes !== 'undefined' ? { attributes: attributes } : {})); | ||
if (typeof data.type !== 'string' || !data.type) { | ||
@@ -81,2 +78,7 @@ console.warn('ModelsSerializer cannot buildDataByModel, type is not set or incorrect', model); | ||
}; | ||
ModelsSerializer.prototype.buildResourceObjectPart = function (relation) { | ||
var id = this.propertiesMapper.getId(relation); | ||
var type = this.propertiesMapper.getType(relation); | ||
return tslib_1.__assign({ type: type }, (typeof id === 'undefined' ? {} : { id: id })); | ||
}; | ||
ModelsSerializer.prototype.buildRelationshipsByModel = function (model) { | ||
@@ -93,13 +95,10 @@ var _this = this; | ||
var relationshipData = []; | ||
var relationLength = relation.length; | ||
for (var i = 0; i < relationLength; i++) { | ||
var item = { | ||
id: _this.propertiesMapper.getId(relation[i]), | ||
type: _this.propertiesMapper.getType(relation[i]) | ||
}; | ||
if (item.id && item.type) { | ||
relationshipData.push(item); | ||
for (var _i = 0, relation_1 = relation; _i < relation_1.length; _i++) { | ||
var relationItem = relation_1[_i]; | ||
var relationshipDataItem = _this.buildResourceObjectPart(relationItem); | ||
if ('type' in relationshipDataItem) { | ||
relationshipData.push(relationshipDataItem); | ||
} | ||
else { | ||
console.error("Can't create data item[" + i + "] for relationship " + k + ",\n it doesn't have 'id' or 'type', it was skipped", relation[i]); | ||
console.error("Can't create data item for relationship ".concat(k, ",\n it doesn't have 'id' or 'type', it was skipped"), relationItem); | ||
} | ||
@@ -112,7 +111,4 @@ } | ||
else if (relation) { | ||
var item = { | ||
id: _this.propertiesMapper.getId(relation), | ||
type: _this.propertiesMapper.getType(relation) | ||
}; | ||
if (item.type) { | ||
var item = _this.buildResourceObjectPart(relation); | ||
if ('type' in item) { | ||
relationships[k] = { | ||
@@ -123,3 +119,3 @@ data: item | ||
else { | ||
console.error("Can't create data for relationship " + k + ", it doesn't have 'type', it was skipped", relation); | ||
console.error("Can't create data for relationship ".concat(k, ", it doesn't have 'type', it was skipped"), relation); | ||
} | ||
@@ -164,5 +160,10 @@ } | ||
ModelsSerializer.prototype.buildIncludedItem = function (relationModel, subIncludeTree, builtIncluded) { | ||
var includeKey = this.propertiesMapper.getType(relationModel) + this.propertiesMapper.getId(relationModel); | ||
if (!builtIncluded[includeKey]) { | ||
// create data by current entity if such included is not yet created | ||
var id = this.propertiesMapper.getId(relationModel); | ||
var type = this.propertiesMapper.getType(relationModel); | ||
var includeKey = type + id; | ||
if (!id || !builtIncluded[includeKey]) { | ||
if (includeKey in builtIncluded) { | ||
includeKey += this.buildIncludedIndex; | ||
this.buildIncludedIndex += 1; | ||
} | ||
builtIncluded[includeKey] = this.buildDataByModel(relationModel); | ||
@@ -169,0 +170,0 @@ if (subIncludeTree) { |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var ReduxObjectDenormalizer = /** @class */ (function () { | ||
var ReduxObjectDenormalizer = (function () { | ||
function ReduxObjectDenormalizer(propertiesMapper) { | ||
@@ -66,4 +66,3 @@ this.cachedModels = {}; | ||
} | ||
// checks for built model in cachedModels is a protection from creating models on recursive relationships | ||
var entityKey = type + "-" + id; | ||
var entityKey = "".concat(type, "-").concat(id); | ||
var model = this.cachedModels[entityKey]; | ||
@@ -111,3 +110,3 @@ if (!model) { | ||
var setRelationshipMeta = _this.propertiesMapper.setRelationshipMeta; | ||
if (setRelationshipMeta) { // support was added in patch release | ||
if (setRelationshipMeta) { | ||
setRelationshipMeta(model, relationName, relation.meta); | ||
@@ -114,0 +113,0 @@ } |
@@ -1,8 +0,8 @@ | ||
import { IDeserializeCache, TAnyKeyValueObject } from './JsonaTypes'; | ||
import { IDeserializeCache, TAnyKeyValueObject, TJsonaModel, TJsonApiData, TResourceIdObj } from './JsonaTypes'; | ||
export declare function jsonStringify(json: TAnyKeyValueObject): string; | ||
export declare class DeserializeCache implements IDeserializeCache { | ||
protected cachedModels: {}; | ||
getCachedModel(data: any, resourceIdObject: any): any; | ||
handleModel(model: any, data: any, resourceIdObject: any): void; | ||
createCacheKey(data: any, resourceIdObject: any): string; | ||
getCachedModel(data: TJsonApiData, resourceIdObject: TResourceIdObj): any; | ||
handleModel(model: TJsonaModel, data: TJsonApiData, resourceIdObject: TResourceIdObj): void; | ||
createCacheKey(data: any, resourceIdObject?: TResourceIdObj): string; | ||
} |
@@ -16,3 +16,3 @@ "use strict"; | ||
exports.jsonStringify = jsonStringify; | ||
var DeserializeCache = /** @class */ (function () { | ||
var DeserializeCache = (function () { | ||
function DeserializeCache() { | ||
@@ -33,12 +33,13 @@ this.cachedModels = {}; | ||
DeserializeCache.prototype.createCacheKey = function (data, resourceIdObject) { | ||
// resourceIdObject.meta sets to model in simplePropertyMappers.ts, so it should be used here too | ||
// cache in this case probably will be redundant | ||
if (!data.id || !data.type || resourceIdObject.meta) { | ||
if (!data.id || !data.type) { | ||
return; | ||
} | ||
var resourcePart = resourceIdObject.type + "-" + resourceIdObject.id; | ||
var resourcePart = resourceIdObject ? "".concat(resourceIdObject.type, "-").concat(resourceIdObject.id) : ''; | ||
if (resourceIdObject === null || resourceIdObject === void 0 ? void 0 : resourceIdObject.meta) { | ||
resourcePart += "-".concat(jsonStringify(resourceIdObject.meta)); | ||
} | ||
if (data.meta) { | ||
return data.type + "-" + data.id + "-" + jsonStringify(data.meta) + "-" + resourcePart; | ||
return "".concat(data.type, "-").concat(data.id, "-").concat(jsonStringify(data.meta), "-").concat(resourcePart); | ||
} | ||
return data.type + "-" + data.id + "-" + resourcePart; | ||
return "".concat(data.type, "-").concat(data.id, "-").concat(resourcePart); | ||
}; | ||
@@ -45,0 +46,0 @@ return DeserializeCache; |
@@ -15,6 +15,2 @@ import { IModelPropertiesMapper, IModelsSerializerConstructor, IJsonDeserializerConstructor, IJsonPropertiesMapper, TJsonaDenormalizedIncludeNames, TJsonaNormalizedIncludeNamesTree, TJsonaModel, TJsonApiBody, TReduxObject, IDeserializeCacheConstructor, TDeserializeOptions } from './JsonaTypes'; | ||
}); | ||
/** | ||
* serialize | ||
* Creates JSON, compatible with json:api specification from Jsona model(s). | ||
*/ | ||
serialize({ stuff, includeNames }: { | ||
@@ -24,13 +20,3 @@ stuff: TJsonaModel | Array<TJsonaModel>; | ||
}): TJsonApiBody; | ||
/** | ||
* deserialize | ||
* Creates Jsona model(s) from JSON, compatible with json:api specification. | ||
*/ | ||
deserialize(body: TJsonApiBody | string, options?: TDeserializeOptions): TJsonaModel | Array<TJsonaModel>; | ||
/** | ||
* denormalizeReduxObject | ||
* Creates Jsona model(s) from ReduxObject, that creates by json-api-normalizer | ||
* https://github.com/yury-dymov/json-api-normalizer | ||
* | ||
*/ | ||
denormalizeReduxObject({ reduxObject, entityType, entityIds, returnBuilderInRelations }: { | ||
@@ -37,0 +23,0 @@ reduxObject: TReduxObject; |
@@ -9,3 +9,3 @@ "use strict"; | ||
var cache_1 = require("./cache"); | ||
var Jsona = /** @class */ (function () { | ||
var Jsona = (function () { | ||
function Jsona(params) { | ||
@@ -33,6 +33,2 @@ this.modelPropertiesMapper = new simplePropertyMappers_1.ModelPropertiesMapper(); | ||
} | ||
/** | ||
* serialize | ||
* Creates JSON, compatible with json:api specification from Jsona model(s). | ||
*/ | ||
Jsona.prototype.serialize = function (_a) { | ||
@@ -50,6 +46,2 @@ var stuff = _a.stuff, includeNames = _a.includeNames; | ||
}; | ||
/** | ||
* deserialize | ||
* Creates Jsona model(s) from JSON, compatible with json:api specification. | ||
*/ | ||
Jsona.prototype.deserialize = function (body, options) { | ||
@@ -62,3 +54,3 @@ if (!body) { | ||
if (typeof body === 'string') { | ||
modelBuilder.setJsonParsedObject(utils_1.jsonParse(body)); | ||
modelBuilder.setJsonParsedObject((0, utils_1.jsonParse)(body)); | ||
} | ||
@@ -70,8 +62,2 @@ else { | ||
}; | ||
/** | ||
* denormalizeReduxObject | ||
* Creates Jsona model(s) from ReduxObject, that creates by json-api-normalizer | ||
* https://github.com/yury-dymov/json-api-normalizer | ||
* | ||
*/ | ||
Jsona.prototype.denormalizeReduxObject = function (_a) { | ||
@@ -78,0 +64,0 @@ var reduxObject = _a.reduxObject, entityType = _a.entityType, entityIds = _a.entityIds, _b = _a.returnBuilderInRelations, returnBuilderInRelations = _b === void 0 ? false : _b; |
@@ -1,2 +0,2 @@ | ||
declare type AtLeastOneProperty<T, U = { | ||
type AtLeastOneProperty<T, U = { | ||
[K in keyof T]: Pick<T, K>; | ||
@@ -16,3 +16,3 @@ }> = Partial<T> & U[keyof U]; | ||
setLinks(model: TJsonaModel, links: TAnyKeyValueObject): void; | ||
setResourceIdObjMeta(model: TJsonaModel, meta: TResourceIdObj): void; | ||
setResourceIdObjMeta(model: TJsonaModel, meta: unknown): void; | ||
setRelationships(model: TJsonaModel, relationships: TJsonaRelationships): void; | ||
@@ -63,13 +63,13 @@ setRelationshipLinks(parentModel: TJsonaModel, relationName: string, links: TJsonApiLinks): void; | ||
} | ||
export declare type TDeserializeOptions = { | ||
export type TDeserializeOptions = { | ||
preferNestedDataFromData?: boolean; | ||
}; | ||
export declare type TAnyKeyValueObject = { | ||
export type TAnyKeyValueObject = { | ||
[key: string]: any; | ||
}; | ||
export declare type TJsonApiBody = { | ||
export type TJsonApiBody = { | ||
data?: TJsonApiData | TJsonApiData[]; | ||
included?: Array<TJsonApiData>; | ||
}; | ||
export declare type TJsonApiData = { | ||
export type TJsonApiData = { | ||
type: string; | ||
@@ -82,3 +82,3 @@ id?: string | number; | ||
}; | ||
export declare type TJsonApiRelationshipData = { | ||
export type TJsonApiRelationshipData = { | ||
type: string; | ||
@@ -88,3 +88,3 @@ id: string | number; | ||
}; | ||
declare type FullTJsonApiRelation = { | ||
type FullTJsonApiRelation = { | ||
data: TJsonApiRelationshipData | Array<TJsonApiRelationshipData> | null; | ||
@@ -94,57 +94,36 @@ links: TJsonApiLinks; | ||
}; | ||
export declare type TJsonApiRelation = AtLeastOneProperty<FullTJsonApiRelation>; | ||
declare type LinkKey = "self" | "related" | "first" | "prev" | "next" | "last"; | ||
declare type LinkObjectMember = string | { | ||
export type TJsonApiRelation = AtLeastOneProperty<FullTJsonApiRelation>; | ||
type LinkKey = "self" | "related" | "first" | "prev" | "next" | "last"; | ||
type LinkObjectMember = string | { | ||
href?: string; | ||
meta?: TAnyKeyValueObject; | ||
} | null; | ||
export declare type TJsonApiLinks = { | ||
export type TJsonApiLinks = { | ||
[key in LinkKey]?: LinkObjectMember; | ||
}; | ||
export declare type TJsonApiRelationships = { | ||
export type TJsonApiRelationships = { | ||
[relationName: string]: TJsonApiRelation; | ||
}; | ||
export declare type TJsonaUniqueIncluded = { | ||
export type TJsonaUniqueIncluded = { | ||
[entityTypeId: string]: TJsonApiData; | ||
}; | ||
/** | ||
* TJsonaDenormalizedIncludeNames example: | ||
* 'user.town.country' | ||
*/ | ||
export declare type TJsonaIncludeNamesChain = string; | ||
/** | ||
* TJsonaDenormalizedIncludeNames example: | ||
* ['user', 'user.town', 'user.town.country', 'comments', 'comments.author'] | ||
*/ | ||
export declare type TJsonaDenormalizedIncludeNames = Array<TJsonaIncludeNamesChain>; | ||
/** | ||
* TJsonaNormalizedIncludeNamesTree example: | ||
* { | ||
* user: { | ||
* town: { | ||
* country: null | ||
* } | ||
* comments: { | ||
* author: null | ||
* } | ||
*/ | ||
export declare type TJsonaNormalizedIncludeNamesTree = { | ||
export type TJsonaIncludeNamesChain = string; | ||
export type TJsonaDenormalizedIncludeNames = Array<TJsonaIncludeNamesChain>; | ||
export type TJsonaNormalizedIncludeNamesTree = { | ||
[relationName: string]: null | TJsonaNormalizedIncludeNamesTree; | ||
}; | ||
export declare type TJsonaModel = { | ||
export type TJsonaModel = { | ||
[propertyName: string]: any; | ||
}; | ||
export declare type TResourceIdObj = { | ||
export type TResourceIdObj = { | ||
id?: string | number; | ||
type?: string; | ||
meta?: { | ||
[propertyName: string]: any; | ||
}; | ||
type: string; | ||
meta?: unknown; | ||
[propertyName: string]: any; | ||
}; | ||
export declare type TJsonaRelationshipBuild = () => (TJsonaModel | Array<TJsonaModel>); | ||
export declare type TJsonaRelationships = { | ||
export type TJsonaRelationshipBuild = () => (TJsonaModel | Array<TJsonaModel>); | ||
export type TJsonaRelationships = { | ||
[relationName: string]: TJsonaRelationshipBuild | TJsonaModel | Array<TJsonaModel>; | ||
}; | ||
export declare type TReduxObject = { | ||
export type TReduxObject = { | ||
[entityType: string]: { | ||
@@ -154,3 +133,3 @@ [entityId: string]: TReduxObjectModel; | ||
}; | ||
export declare type TReduxObjectModel = { | ||
export type TReduxObjectModel = { | ||
id: number | string; | ||
@@ -160,3 +139,3 @@ attributes?: TAnyKeyValueObject; | ||
}; | ||
export declare type TReduxObjectRelation = { | ||
export type TReduxObjectRelation = { | ||
data: { | ||
@@ -167,3 +146,3 @@ id: string | Array<string>; | ||
}; | ||
export declare type SwitchCaseModelMapperOptionsType = { | ||
export type SwitchCaseModelMapperOptionsType = { | ||
switchAttributes?: boolean; | ||
@@ -174,3 +153,3 @@ switchRelationships?: boolean; | ||
}; | ||
export declare type SwitchCaseJsonMapperOptionsType = { | ||
export type SwitchCaseJsonMapperOptionsType = { | ||
camelizeAttributes?: boolean; | ||
@@ -177,0 +156,0 @@ camelizeRelationships?: boolean; |
"use strict"; | ||
var __spreadArray = (this && this.__spreadArray) || function (to, from) { | ||
for (var i = 0, il = from.length, j = to.length; i < il; i++, j++) | ||
to[j] = from[i]; | ||
return to; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.JsonPropertiesMapper = exports.defineRelationGetter = exports.ModelPropertiesMapper = exports.RELATIONSHIP_NAMES_PROP = void 0; | ||
var tslib_1 = require("tslib"); | ||
exports.RELATIONSHIP_NAMES_PROP = 'relationshipNames'; | ||
var ModelPropertiesMapper = /** @class */ (function () { | ||
var ModelPropertiesMapper = (function () { | ||
function ModelPropertiesMapper() { | ||
@@ -25,3 +21,3 @@ } | ||
else if (model[exports.RELATIONSHIP_NAMES_PROP]) { | ||
console.warn("Can't getAttributes correctly, '" + exports.RELATIONSHIP_NAMES_PROP + "' property of " + model.type + "-" + model.id + " model\n isn't array of relationship names", model[exports.RELATIONSHIP_NAMES_PROP]); | ||
console.warn("Can't getAttributes correctly, '".concat(exports.RELATIONSHIP_NAMES_PROP, "' property of ").concat(model.type, "-").concat(model.id, " model\n isn't array of relationship names"), model[exports.RELATIONSHIP_NAMES_PROP]); | ||
} | ||
@@ -42,3 +38,3 @@ var attributes = {}; | ||
else if (relationshipNames && !Array.isArray(relationshipNames)) { | ||
console.warn("Can't getRelationships correctly,\n '" + exports.RELATIONSHIP_NAMES_PROP + "' property of " + model.type + "-" + model.id + " model\n isn't array of relationship names", model[exports.RELATIONSHIP_NAMES_PROP]); | ||
console.warn("Can't getRelationships correctly,\n '".concat(exports.RELATIONSHIP_NAMES_PROP, "' property of ").concat(model.type, "-").concat(model.id, " model\n isn't array of relationship names"), model[exports.RELATIONSHIP_NAMES_PROP]); | ||
return; | ||
@@ -72,3 +68,3 @@ } | ||
exports.defineRelationGetter = defineRelationGetter; | ||
var JsonPropertiesMapper = /** @class */ (function () { | ||
var JsonPropertiesMapper = (function () { | ||
function JsonPropertiesMapper() { | ||
@@ -108,3 +104,3 @@ } | ||
if (currentNames && currentNames.length) { | ||
model[exports.RELATIONSHIP_NAMES_PROP] = __spreadArray(__spreadArray([], currentNames), newNames).filter(function (value, i, self) { return self.indexOf(value) === i; }); | ||
model[exports.RELATIONSHIP_NAMES_PROP] = tslib_1.__spreadArray(tslib_1.__spreadArray([], currentNames, true), newNames, true).filter(function (value, i, self) { return self.indexOf(value) === i; }); | ||
} | ||
@@ -116,6 +112,4 @@ else { | ||
JsonPropertiesMapper.prototype.setRelationshipLinks = function (parentModel, relationName, links) { | ||
// inherit your IJsonPropertiesMapper and overload this method, if you want to handle relationship links | ||
}; | ||
JsonPropertiesMapper.prototype.setRelationshipMeta = function (parentModel, relationName, links) { | ||
// inherit your IJsonPropertiesMapper and overload this method, if you want to handle relationship meta | ||
}; | ||
@@ -122,0 +116,0 @@ return JsonPropertiesMapper; |
@@ -8,6 +8,9 @@ import { IModelPropertiesMapper, IJsonPropertiesMapper, TAnyKeyValueObject, TJsonaModel, TJsonaRelationships, SwitchCaseModelMapperOptionsType, SwitchCaseJsonMapperOptionsType } from './JsonaTypes'; | ||
switchChar: string; | ||
regex: RegExp; | ||
constructor(options?: SwitchCaseModelMapperOptionsType); | ||
getType(model: TJsonaModel): any; | ||
getAttributes(model: TJsonaModel): {}; | ||
getRelationships(model: TJsonaModel): {}; | ||
getAttributes(model: TJsonaModel): any; | ||
getRelationships(model: TJsonaModel): any; | ||
private convertFromCamelCase; | ||
private convertFromCamelCaseString; | ||
} | ||
@@ -20,2 +23,3 @@ export declare class SwitchCaseJsonMapper extends JsonPropertiesMapper implements IJsonPropertiesMapper { | ||
switchChar: string; | ||
regex: RegExp; | ||
constructor(options?: SwitchCaseJsonMapperOptionsType); | ||
@@ -26,2 +30,4 @@ createModel(type: string): TJsonaModel; | ||
setRelationships(model: TJsonaModel, relationships: TJsonaRelationships): void; | ||
private convertToCamelCase; | ||
convertToCamelCaseString(notCamelCaseString: string): string; | ||
} |
"use strict"; | ||
var __extends = (this && this.__extends) || (function () { | ||
var extendStatics = function (d, b) { | ||
extendStatics = Object.setPrototypeOf || | ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || | ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; | ||
return extendStatics(d, b); | ||
}; | ||
return function (d, b) { | ||
if (typeof b !== "function" && b !== null) | ||
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); | ||
extendStatics(d, b); | ||
function __() { this.constructor = d; } | ||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); | ||
}; | ||
})(); | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.SwitchCaseJsonMapper = exports.SwitchCaseModelMapper = void 0; | ||
var tslib_1 = require("tslib"); | ||
var simplePropertyMappers_1 = require("./simplePropertyMappers"); | ||
var SwitchCaseModelMapper = /** @class */ (function (_super) { | ||
__extends(SwitchCaseModelMapper, _super); | ||
var utils_1 = require("./utils"); | ||
var SwitchCaseModelMapper = (function (_super) { | ||
tslib_1.__extends(SwitchCaseModelMapper, _super); | ||
function SwitchCaseModelMapper(options) { | ||
@@ -29,6 +16,6 @@ var _this = _super.call(this) || this; | ||
_this.switchChar = switchChar; | ||
_this.regex = new RegExp(/([a-z][A-Z0-9])/g); | ||
return _this; | ||
} | ||
SwitchCaseModelMapper.prototype.getType = function (model) { | ||
var _this = this; | ||
var type = _super.prototype.getType.call(this, model); | ||
@@ -38,6 +25,5 @@ if (!this.switchType || !type) { | ||
} | ||
return type.replace(/([a-z][A-Z0-9])/g, function (g) { return g[0] + _this.switchChar + g[1].toLowerCase(); }); | ||
return this.convertFromCamelCaseString(type); | ||
}; | ||
SwitchCaseModelMapper.prototype.getAttributes = function (model) { | ||
var _this = this; | ||
var camelCasedAttributes = _super.prototype.getAttributes.call(this, model); | ||
@@ -47,11 +33,5 @@ if (!this.switchAttributes || !camelCasedAttributes) { | ||
} | ||
var kebabAttributes = {}; | ||
Object.keys(camelCasedAttributes).forEach(function (name) { | ||
var kebabName = name.replace(/([a-z][A-Z0-9])/g, function (g) { return g[0] + _this.switchChar + g[1].toLowerCase(); }); | ||
kebabAttributes[kebabName] = camelCasedAttributes[name]; | ||
}); | ||
return kebabAttributes; | ||
return this.convertFromCamelCase(camelCasedAttributes); | ||
}; | ||
SwitchCaseModelMapper.prototype.getRelationships = function (model) { | ||
var _this = this; | ||
var camelCasedRelationships = _super.prototype.getRelationships.call(this, model); | ||
@@ -61,14 +41,29 @@ if (!this.switchRelationships || !camelCasedRelationships) { | ||
} | ||
var kebabRelationships = {}; | ||
Object.keys(camelCasedRelationships).forEach(function (name) { | ||
var kebabName = name.replace(/([a-z][A-Z0-9])/g, function (g) { return g[0] + _this.switchChar + g[1].toLowerCase(); }); | ||
kebabRelationships[kebabName] = camelCasedRelationships[name]; | ||
}); | ||
return kebabRelationships; | ||
return this.convertFromCamelCase(camelCasedRelationships); | ||
}; | ||
SwitchCaseModelMapper.prototype.convertFromCamelCase = function (stuff) { | ||
var _this = this; | ||
if (Array.isArray(stuff)) { | ||
return stuff.map(function (item) { return _this.convertFromCamelCase(item); }); | ||
} | ||
if ((0, utils_1.isPlainObject)(stuff)) { | ||
var converted_1 = {}; | ||
Object.entries(stuff).forEach(function (_a) { | ||
var propName = _a[0], value = _a[1]; | ||
var kebabName = _this.convertFromCamelCaseString(propName); | ||
converted_1[kebabName] = _this.convertFromCamelCase(value); | ||
}); | ||
return converted_1; | ||
} | ||
return stuff; | ||
}; | ||
SwitchCaseModelMapper.prototype.convertFromCamelCaseString = function (camelCaseString) { | ||
var _this = this; | ||
return camelCaseString.replace(this.regex, function (g) { return g[0] + _this.switchChar + g[1].toLowerCase(); }); | ||
}; | ||
return SwitchCaseModelMapper; | ||
}(simplePropertyMappers_1.ModelPropertiesMapper)); | ||
exports.SwitchCaseModelMapper = SwitchCaseModelMapper; | ||
var SwitchCaseJsonMapper = /** @class */ (function (_super) { | ||
__extends(SwitchCaseJsonMapper, _super); | ||
var SwitchCaseJsonMapper = (function (_super) { | ||
tslib_1.__extends(SwitchCaseJsonMapper, _super); | ||
function SwitchCaseJsonMapper(options) { | ||
@@ -82,2 +77,3 @@ var _this = _super.call(this) || this; | ||
_this.switchChar = switchChar; | ||
_this.regex = new RegExp("".concat(_this.switchChar, "([a-z0-9])"), 'g'); | ||
return _this; | ||
@@ -89,32 +85,19 @@ } | ||
} | ||
var regex = new RegExp(this.switchChar + "([a-z0-9])", 'g'); | ||
var camelizedType = type.replace(regex, function (g) { return g[1].toUpperCase(); }); | ||
var camelizedType = this.convertToCamelCaseString(type); | ||
return { type: camelizedType }; | ||
}; | ||
SwitchCaseJsonMapper.prototype.setAttributes = function (model, attributes) { | ||
var _this = this; | ||
if (!this.camelizeAttributes) { | ||
return _super.prototype.setAttributes.call(this, model, attributes); | ||
} | ||
Object.keys(attributes).forEach(function (propName) { | ||
var regex = new RegExp(_this.switchChar + "([a-z0-9])", 'g'); | ||
var camelName = propName.replace(regex, function (g) { return g[1].toUpperCase(); }); | ||
model[camelName] = attributes[propName]; | ||
}); | ||
Object.assign(model, this.convertToCamelCase(attributes)); | ||
}; | ||
SwitchCaseJsonMapper.prototype.setMeta = function (model, meta) { | ||
var _this = this; | ||
if (!this.camelizeMeta) { | ||
return _super.prototype.setMeta.call(this, model, meta); | ||
} | ||
model.meta = {}; | ||
Object.keys(meta).forEach(function (propName) { | ||
var regex = new RegExp(_this.switchChar + "([a-z0-9])", 'g'); | ||
var camelName = propName.replace(regex, function (g) { return g[1].toUpperCase(); }); | ||
model.meta[camelName] = meta[propName]; | ||
}); | ||
model.meta = this.convertToCamelCase(meta); | ||
}; | ||
SwitchCaseJsonMapper.prototype.setRelationships = function (model, relationships) { | ||
var _this = this; | ||
// call super.setRelationships first, just for not to copy paste setRelationships logic | ||
_super.prototype.setRelationships.call(this, model, relationships); | ||
@@ -124,6 +107,4 @@ if (!this.camelizeRelationships) { | ||
} | ||
// then change relationship names case if needed | ||
model[simplePropertyMappers_1.RELATIONSHIP_NAMES_PROP].forEach(function (kebabName, i) { | ||
var regex = new RegExp(_this.switchChar + "([a-z0-9])", 'g'); | ||
var camelName = kebabName.replace(regex, function (g) { return g[1].toUpperCase(); }); | ||
var camelName = _this.convertToCamelCaseString(kebabName); | ||
if (camelName !== kebabName) { | ||
@@ -136,2 +117,21 @@ model[camelName] = model[kebabName]; | ||
}; | ||
SwitchCaseJsonMapper.prototype.convertToCamelCase = function (stuff) { | ||
var _this = this; | ||
if (Array.isArray(stuff)) { | ||
return stuff.map(function (item) { return _this.convertToCamelCase(item); }); | ||
} | ||
if ((0, utils_1.isPlainObject)(stuff)) { | ||
var converted_2 = {}; | ||
Object.entries(stuff).forEach(function (_a) { | ||
var propName = _a[0], value = _a[1]; | ||
var camelName = _this.convertToCamelCaseString(propName); | ||
converted_2[camelName] = _this.convertToCamelCase(value); | ||
}); | ||
return converted_2; | ||
} | ||
return stuff; | ||
}; | ||
SwitchCaseJsonMapper.prototype.convertToCamelCaseString = function (notCamelCaseString) { | ||
return notCamelCaseString.replace(this.regex, function (g) { return g[1].toUpperCase(); }); | ||
}; | ||
return SwitchCaseJsonMapper; | ||
@@ -138,0 +138,0 @@ }(simplePropertyMappers_1.JsonPropertiesMapper)); |
import { TJsonaIncludeNamesChain, TJsonaNormalizedIncludeNamesTree } from './JsonaTypes'; | ||
export declare function createIncludeNamesTree(namesChain: TJsonaIncludeNamesChain, includeTree: TJsonaNormalizedIncludeNamesTree): void; | ||
export declare function jsonParse(stringified: string): Object; | ||
export declare const isPlainObject: (val: unknown) => val is Object; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.jsonParse = exports.createIncludeNamesTree = void 0; | ||
exports.isPlainObject = exports.jsonParse = exports.createIncludeNamesTree = void 0; | ||
function createIncludeNamesTree(namesChain, includeTree) { | ||
@@ -28,2 +28,4 @@ var namesArray = namesChain.split('.'); | ||
exports.jsonParse = jsonParse; | ||
var isPlainObject = function (val) { return !!val && typeof val === 'object' && val.constructor === Object; }; | ||
exports.isPlainObject = isPlainObject; | ||
//# sourceMappingURL=utils.js.map |
{ | ||
"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.10.1", | ||
"version": "1.11.0", | ||
"keywords": [ | ||
@@ -41,5 +41,8 @@ "json-api", | ||
"files": [ | ||
"lib" | ||
"lib", | ||
"src" | ||
], | ||
"dependencies": {}, | ||
"dependencies": { | ||
"tslib": "^2.4.1" | ||
}, | ||
"devDependencies": { | ||
@@ -50,5 +53,5 @@ "@types/chai": "^4.2.4", | ||
"chai": "^4.3.4", | ||
"mocha": "^8.3.2", | ||
"mocha": "^10.1.0", | ||
"ts-mocha": "^8.0.0", | ||
"typescript": "^4.2.4" | ||
"typescript": "^4.9.3" | ||
}, | ||
@@ -55,0 +58,0 @@ "scripts": { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
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
139114
43
2380
1
+ Addedtslib@^2.4.1
+ Addedtslib@2.8.1(transitive)