-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.js
383 lines (353 loc) · 11.4 KB
/
index.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
'use strict';
const JSONAPISerializer = require('jsonapi-serializer').Serializer;
const crypto = require('crypto');
/**
* Converts a string to the `dasherized-key` format.
*
* @private
* @function dasherize
* @param {String} str - String to convert.
* @return {String}
*/
function dasherize(str) {
let newStr = str.substr(0, 1).toLowerCase() + str.substr(1);
newStr = newStr.replace(/([A-Z])/g, '-$1').toLowerCase();
return newStr;
}
/**
* Creates a unique ID for a given object, using a SHA-256 hash.
*
* @private
* @function generateFauxId
* @param {Object} data
* @return {String}
*/
function generateFauxId(data) {
const hash = crypto.createHash('sha256');
hash.update(JSON.stringify(data));
return hash.digest('hex');
}
/**
* Checks if Sequelize objects are available. If they're not, `serializePlainObject`
* will be used to serialize the result. No relationships will be available.
*
* @private
* @function mustParseAsSequelize
* @param {Hook} hook
* @return {Boolean}
*/
function mustParseAsSequelize(hook) {
return hook.service.Model;
}
/**
* Creates a filter helper to check if a given attribute name must be excluded or not.
*
* @private
* @function byExcluded
* @param {String[]} excluded
* @returns {Function}
*/
function byExcluded(excluded) {
return function(attribute) {
return excluded.indexOf(attribute) === -1;
};
}
/**
* Creates a filter helper to detect primary keys in a model.
*
* @private
* @function byPrimaryKey
* @param {Model} model
* @returns {Function}
*/
function byPrimaryKey(model) {
return function(attribute) {
return model.attributes[attribute].primaryKey;
};
}
/**
* Creates an object describing the relationship per JSON API specs.
* A relationship object has the form `{ "type": "<model>", "id": "<id>" }`.
*
* @private
* @function createRelationshipObject
* @param {Association} include - The association description
* @param {Object} item - The parent record.
* @return {Object}
*/
function createRelationshipObject(include, item) {
const relatedModelIdAttribute = include.association.targetKey;
const associationValue = item[include.as];
if (associationValue && Array.isArray(associationValue)) {
const results = [];
associationValue.forEach(function(record) {
const relatedItem = {};
relatedItem.type = include.model.name;
relatedItem[relatedModelIdAttribute] = record[relatedModelIdAttribute];
results.push(relatedItem);
});
return results;
} else if (associationValue && typeof associationValue === 'object') {
const relatedItem = {};
relatedItem.type = include.model.name;
relatedItem[relatedModelIdAttribute] = associationValue[relatedModelIdAttribute];
return relatedItem;
}
return null;
}
/**
* Returns a helper function that converts an embedded record to a related record per JSON API specs.
*
* @param {Object} data
* @param {Object[]} includedData
* @return {Function}
*/
function parseRelationships(data, includedData) {
return function(include) {
const relationship = {};
const relationshipName = include.association.options.underscored ? include.as.replace(/_/g, '-') : include.as;
relationship[relationshipName] = { data: createRelationshipObject(include, data) };
if (data[include.as] !== null) {
const serializedRelationship = jsonapify(data[include.as], include.model, include.model.name + '/' + data[include.as].id, { include: [] });
if (Array.isArray(serializedRelationship.document)) {
Array.prototype.push.apply(includedData, [...serializedRelationship.document.map(function(item) {
return Object.assign(item, { links: { self: '/' + include.model.name + '/' + item.id }});
})]);
} else {
includedData.push(Object.assign({}, serializedRelationship.document, { links: serializedRelationship.links }));
}
delete data[include.as][include.association.foreignKey];
}
delete data[include.association.foreignKey];
delete data[include.as];
if (relationship[relationshipName].data !== null) {
data.relationships = Object.assign({}, data.relationships, relationship);
}
};
}
/**
* Clears any array of records of duplicates by checking its `id`.
*
* @private
* @function
* @param {Object[]} collection
* @return {Object[]}
*/
function removeDuplicateRecords(collection) {
const newCollection = [];
const uniqueRecords = {};
collection.forEach(function(item) {
uniqueRecords[item.id] = item;
});
Object.keys(uniqueRecords).forEach(function(uniqueKey) {
newCollection.push(uniqueRecords[uniqueKey]);
});
return newCollection;
}
/**
* Creates a JSON API document derived from the REST provider response.
*
* @private
* @function jsonapify
* @param {Object|Object[]} data - The original response.
* @param {Model} model - The Sequelize model to work with.
* @param {String} selfUrl - The hook's path to create the `self` link.
* @param {Object} context - The contents of `result.$options`.
* @return {Object}
*/
function jsonapify(data, model, selfUrl, context) {
const includedData = [];
const idAttribute = Object.keys(model.attributes).filter(byPrimaryKey(model))[0];
const excluded = [idAttribute];
if (context && context.include && context.include.length) {
context.include.forEach(parseRelationships(data, includedData, model, selfUrl));
}
const attributes = Object.keys(model.attributes).filter(byExcluded(excluded)).concat(['relationships']);
const json = new JSONAPISerializer(model.name, data, {
topLevelLinks: {
self: '/' + selfUrl
},
attributes: attributes
});
if (json.data.attributes && json.data.attributes.relationships) {
json.data.relationships = json.data.attributes.relationships;
delete json.data.attributes.relationships;
}
const result = { document: json.data, links: json.links };
if (includedData.length) {
result.related = includedData;
}
return result;
}
/**
* Moves any non-JSON-API top-level key as metadata.
*
* @private
* @method createMetadata
* @param {Hook} hook
*/
function createMetadata(hook) {
const metaKeys = Object.keys(hook.result).filter(function(key) {
return ['data', 'included', 'meta', 'links'].indexOf(key) === -1;
});
if (metaKeys.length) {
const meta = {};
metaKeys.forEach(function(key) {
meta[dasherize(key)] = hook.result[key];
delete hook.result[key];
});
hook.result.meta = meta;
}
}
/**
* Creates links to follow the pagination context included by the REST provider.
*
* @private
* @method createPagination
* @param {Hook} hook
*/
function createPagination(hook) {
if (hook.result.skip !== undefined && hook.result.total !== undefined && hook.result.limit !== undefined) {
hook.result.links = {};
if (hook.result.skip >= hook.result.limit) {
hook.result.links.first = '/' + hook.path;
}
if (hook.result.skip + hook.result.limit < hook.result.total) {
hook.result.links.next = '/' + hook.path + '?$skip=' + (hook.result.skip + hook.result.limit);
}
if (hook.result.skip + hook.result.limit > hook.result.limit) {
hook.result.links.prev = '/' + hook.path + '?$skip=' + (hook.result.skip - hook.result.limit);
}
if (hook.result.skip + hook.result.limit < hook.result.total) {
hook.result.links.last = '/' + hook.path + '?$skip=' + (Math.floor(hook.result.total / hook.result.limit) * hook.result.limit);
}
}
}
/**
* Converts a plain Object instance into a JSON-API-compliant object.
*
* @private
* @function serializePlainObject
* @param {Object} item
* @param {Object} options
* @param {Hook} hook
* @return {Object}
*/
function serializePlainObject(item, options, hook) {
const newItem = {};
if (options.identifierKey) {
newItem.id = item[options.identifierKey];
} else {
newItem.id = generateFauxId(item);
}
if (options.typeKey) {
newItem.type = item[options.typeKey];
} else {
newItem.type = hook.service.options.name;
}
newItem.attributes = {};
Object.keys(item).filter(function(key) {
return key !== options.identifierKey && key !== options.typeKey;
}).forEach(function(key) {
newItem.attributes[dasherize(key)] = item[key];
});
newItem.links = {
self: '/' + hook.path + '/' + newItem.id
};
return newItem;
}
/**
* Creates a JSON API document with multiple records.
*
* @private
* @method jsonapifyViaFind
* @param {Hook} hook
*/
function jsonapifyViaFind(hook, options) {
let serialized = {};
if (mustParseAsSequelize(hook)) {
hook.result.included = [];
hook.result.data.forEach(function(data, index) {
const jsonItem = data;
serialized = jsonapify(jsonItem, hook.service.Model, hook.path + '/' + jsonItem.id, data.$options);
hook.result.data[index] = serialized.document;
if (serialized.related) {
hook.result.included = hook.result.included.concat(serialized.related);
}
if (serialized.links) {
hook.result.data[index].links = serialized.links;
createPagination(hook);
}
});
if (!hook.result.included.length) {
delete hook.result.included;
} else {
hook.result.included = removeDuplicateRecords(hook.result.included);
}
createMetadata(hook);
} else {
const newResult = {};
if (Array.isArray(hook.result) && hook.result.length > 1) {
newResult.data = hook.result.map(function(item) {
return serializePlainObject(item, options, hook);
});
} else if (!Array.isArray(hook.result) && Object.keys(hook.result).length) {
newResult.data = [serializePlainObject(hook.result, options, hook)];
} else {
newResult.data = [];
}
hook.result = newResult;
}
}
/**
* Creates a JSON API document for a single record.
*
* @private
* @method jsonapifyViaGet
* @param {Hook} hook
*/
function jsonapifyViaGet(hook, options) {
let serialized = {};
if (mustParseAsSequelize(hook)) {
const jsonItem = hook.result;
serialized = jsonapify(jsonItem, hook.service.Model, hook.path + '/' + jsonItem.id, hook.result.$options);
hook.result = { data: serialized.document, included: serialized.related };
if (hook.result.included && !hook.result.included.length) {
delete hook.result.included;
}
if (serialized.links) {
hook.result.data.links = serialized.links;
hook.result.data.links.parent = '/' + hook.service.Model.name;
}
createMetadata(hook);
} else {
hook.result.data = serializePlainObject(hook.result, options, hook);
}
}
/**
* Maps hook methods to jsonapify functions.
*
* @private
* @constant entrypoints
*/
const entrypoints = { find: jsonapifyViaFind, get: jsonapifyViaGet };
/**
* This hook allows to serialize the REST provider response as a JSON API response.
* It is used as an `after` hook. Bindable with `find` and `get` hooks.
*
* @function jsonapify
* @param {Object} options - Define settings for the JSONAPIficiation process.
* Available options:
* - identifierKey: (String) Used by `serializePlainObject` to determine
* which key will be used as `id`.
* - typeKey: (String) Used by `serializePlainObject` to determine
* which key will be used as `type`.
*/
module.exports = function (options = {}) { // eslint-disable-line no-unused-vars
return function (hook) {
if (hook.method === 'find' || hook.method === 'get') {
entrypoints[hook.method](hook, options);
}
return Promise.resolve(hook);
};
};