-
Notifications
You must be signed in to change notification settings - Fork 199
/
Copy pathdeserializer-utils.js
188 lines (162 loc) · 5.41 KB
/
deserializer-utils.js
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
'use strict';
var isPlainObject = require('lodash/isPlainObject');
var isFunction = require('lodash/isFunction');
var _find = require('lodash/find');
var _extend = require('lodash/extend');
var _transform = require('lodash/transform');
var Inflector = require('./inflector');
module.exports = function (jsonapi, data, opts) {
var alreadyIncluded = [];
function isComplexType(obj) {
return Array.isArray(obj) || isPlainObject(obj);
}
function getValueForRelationship(relationshipData, included) {
if (opts && relationshipData && opts[relationshipData.type]) {
var valueForRelationshipFct = opts[relationshipData.type]
.valueForRelationship;
return valueForRelationshipFct(relationshipData, included);
} else {
return included;
}
}
function findIncluded(relationshipData, ancestry) {
return new Promise(function (resolve) {
if (!jsonapi.included || !relationshipData) { resolve(null); }
var included = _find(jsonapi.included, {
id: relationshipData.id,
type: relationshipData.type
});
if (included) {
// To prevent circular references, check if the record type
// has already been processed in this thread
if (ancestry.indexOf(included.type + included.id) > -1) {
return Promise
.all([extractAttributes(included)])
.then(function (results) {
var attributes = results[0];
var relationships = results[1];
resolve(_extend(attributes, relationships));
});
}
return Promise
.all([extractAttributes(included), extractRelationships(included, ancestry + ':' + included.type + included.id), extractLinks(included)])
.then(function (results) {
var attributes = results[0];
var relationships = results[1];
var links = results[2];
const record = _extend(attributes, relationships);
// Links
if (links) {
record.links = links;
}
resolve(record)
});
} else {
return resolve(null);
}
});
}
function keyForAttribute(attribute) {
if (isPlainObject(attribute)) {
return _transform(attribute, function (result, value, key) {
if (isComplexType(value)) {
result[keyForAttribute(key)] = keyForAttribute(value);
} else {
result[keyForAttribute(key)] = value;
}
});
} else if (Array.isArray(attribute)) {
return attribute.map(function (attr) {
if (isComplexType(attr)) {
return keyForAttribute(attr);
} else {
return attr;
}
});
} else {
if (isFunction(opts.keyForAttribute)) {
return opts.keyForAttribute(attribute);
} else {
return Inflector.caserize(attribute, opts);
}
}
}
function extractAttributes(from) {
var dest = keyForAttribute(from.attributes || {});
if ('id' in from) { dest[opts.id || 'id'] = from.id; }
if (opts.typeAsAttribute) {
if ('type' in from) { dest.type = from.type; }
}
if ('meta' in from) { dest.meta = keyForAttribute(from.meta || {}) }
return dest;
}
function extractRelationships(from, ancestry) {
if (!from.relationships) { return; }
var dest = {};
return Promise
.all(Object.keys(from.relationships).map(function (key) {
var relationship = from.relationships[key];
if (relationship.data === null) {
dest[keyForAttribute(key)] = null;
} else if (Array.isArray(relationship.data)) {
return Promise
.all(relationship.data.map(function (relationshipData) {
return extractIncludes(relationshipData, ancestry);
}))
.then(function (includes) {
if (includes) { dest[keyForAttribute(key)] = includes; }
});
} else {
return extractIncludes(relationship.data, ancestry)
.then(function (includes) {
if (includes) { dest[keyForAttribute(key)] = includes; }
});
}
}))
.then(function() {
return dest;
});
}
function extractIncludes(relationshipData, ancestry) {
return findIncluded(relationshipData, ancestry)
.then(function (included) {
var valueForRelationship = getValueForRelationship(relationshipData,
included);
if (valueForRelationship && isFunction(valueForRelationship.then)) {
return valueForRelationship.then(function (value) {
return value;
});
} else {
return valueForRelationship;
}
});
}
function extractLinks(from) {
if (from.links) {
return from.links;
}
if (jsonapi.links) {
return jsonapi.links;
}
return undefined;
}
this.perform = function () {
return Promise
.all([extractAttributes(data), extractRelationships(data, data.type + data.id), extractLinks(data)])
.then(function (results) {
var attributes = results[0];
var relationships = results[1];
var links = results[2];
var record = _extend(attributes, relationships);
// Links
if (links) {
record.links = links;
}
// If option is present, transform record
if (opts && opts.transform) {
record = opts.transform(record);
}
return record;
});
};
};