-
Notifications
You must be signed in to change notification settings - Fork 27
/
simplePropertyMappers.ts
149 lines (122 loc) · 4.59 KB
/
simplePropertyMappers.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import {
IModelPropertiesMapper,
IJsonPropertiesMapper,
TAnyKeyValueObject,
TJsonaModel,
TJsonaRelationships, TJsonaRelationshipBuild, TJsonApiLinks, TResourceIdObj
} from './JsonaTypes';
export const RELATIONSHIP_NAMES_PROP = 'relationshipNames';
export class ModelPropertiesMapper implements IModelPropertiesMapper {
getId(model: TJsonaModel) {
return model.id;
}
getType(model: TJsonaModel) {
return model.type;
}
getAttributes(model: TJsonaModel) {
let exceptProps = ['id', 'type', RELATIONSHIP_NAMES_PROP];
if (Array.isArray(model[RELATIONSHIP_NAMES_PROP])) {
exceptProps.push(...model[RELATIONSHIP_NAMES_PROP]);
} else if (model[RELATIONSHIP_NAMES_PROP]) {
console.warn(
`Can't getAttributes correctly, '${RELATIONSHIP_NAMES_PROP}' property of ${model.type}-${model.id} model
isn't array of relationship names`,
model[RELATIONSHIP_NAMES_PROP]
);
}
const attributes = {};
Object.keys(model).forEach((attrName) => {
if (exceptProps.indexOf(attrName) === -1) {
attributes[attrName] = model[attrName];
}
});
return attributes;
}
getRelationships(model: TJsonaModel) {
const relationshipNames = model[RELATIONSHIP_NAMES_PROP];
if (!relationshipNames || Array.isArray(relationshipNames) && !relationshipNames.length) {
return;
} else if (relationshipNames && !Array.isArray(relationshipNames)) {
console.warn(
`Can't getRelationships correctly,
'${RELATIONSHIP_NAMES_PROP}' property of ${model.type}-${model.id} model
isn't array of relationship names`,
model[RELATIONSHIP_NAMES_PROP]
);
return;
}
const relationships = {};
relationshipNames.forEach((relationName) => {
if (model[relationName] !== undefined) {
relationships[relationName] = model[relationName];
}
});
return relationships;
}
}
export function defineRelationGetter(
model,
relationName,
buildRelation: TJsonaRelationshipBuild
) {
Object.defineProperty(
model,
relationName,
{
enumerable: true,
configurable: true,
set: (value) => {
delete model[relationName];
model[relationName] = value;
},
get: () => {
delete model[relationName];
return model[relationName] = buildRelation();
},
},
);
}
export class JsonPropertiesMapper implements IJsonPropertiesMapper {
createModel(type: string): TJsonaModel {
return {type};
}
setId(model: TJsonaModel, id: string | number) {
model.id = id;
}
setAttributes(model: TJsonaModel, attributes: TAnyKeyValueObject) {
Object.keys(attributes).forEach((propName) => {
model[propName] = attributes[propName];
});
}
setMeta(model: TJsonaModel, meta: TAnyKeyValueObject) {
model.meta = meta;
}
setLinks(model: TJsonaModel, links: TJsonApiLinks) {
model.links = links;
}
setResourceIdObjMeta(model: TJsonaModel, meta: TResourceIdObj) {
model.resourceIdObjMeta = meta;
}
setRelationships(model: TJsonaModel, relationships: TJsonaRelationships) {
Object.keys(relationships).forEach((propName) => {
if (typeof relationships[propName] === 'function') {
defineRelationGetter(model, propName, <TJsonaRelationshipBuild> relationships[propName]);
} else {
model[propName] = relationships[propName];
}
});
const newNames = Object.keys(relationships);
const currentNames = model[RELATIONSHIP_NAMES_PROP];
if (currentNames && currentNames.length) {
model[RELATIONSHIP_NAMES_PROP] = [...currentNames, ...newNames].filter((value, i, self) => self.indexOf(value) === i);
} else {
model[RELATIONSHIP_NAMES_PROP] = newNames;
}
}
setRelationshipLinks(parentModel: TJsonaModel, relationName: string, links: TJsonApiLinks) {
// inherit your IJsonPropertiesMapper and overload this method, if you want to handle relationship links
}
setRelationshipMeta(parentModel: TJsonaModel, relationName: string, links: TAnyKeyValueObject) {
// inherit your IJsonPropertiesMapper and overload this method, if you want to handle relationship meta
}
}