-
Notifications
You must be signed in to change notification settings - Fork 196
Expand file tree
/
Copy pathdeserializer-utils.js
More file actions
168 lines (146 loc) · 5 KB
/
deserializer-utils.js
File metadata and controls
168 lines (146 loc) · 5 KB
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
'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.find((aIncluded) => aIncluded === included.type)) {
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])])
.then(function (results) {
var attributes = results[0];
var relationships = results[1];
resolve(_extend(attributes, relationships));
});
} 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;
}
});
}
this.perform = function () {
return Promise
.all([extractAttributes(data), extractRelationships(data, [data.type])])
.then(function (results) {
var attributes = results[0];
var relationships = results[1];
var record = _extend(attributes, relationships);
// Links
if (jsonapi.links) {
record.links = jsonapi.links;
}
// If option is present, transform record
if (opts && opts.transform) {
record = opts.transform(record);
}
return record;
});
};
};