-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathJSONAPISerializer.js
872 lines (757 loc) · 29.6 KB
/
JSONAPISerializer.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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
'use strict';
const _get = require('lodash/get');
const _set = require('lodash/set');
const _pick = require('lodash/pick');
const _difference = require('lodash/difference');
const _isPlainObject = require('lodash/isPlainObject');
const _find = require('lodash/find');
const _isEmpty = require('lodash/isEmpty');
const _uniqWith = require('lodash/uniqWith');
const _isEqual = require('lodash/isEqual');
const _isObjectLike = require('lodash/isObjectLike');
const _omitBy = require('lodash/omitBy');
const _isUndefined = require('lodash/isUndefined');
const _transform = require('lodash/transform');
const _snakeCase = require('lodash/snakeCase');
const _kebabCase = require('lodash/kebabCase');
const _camelCase = require('lodash/camelCase');
const joi = require('joi');
const intoStream = require('into-stream');
const through = require('through2');
const dedupeStream = require('unique-stream');
const streamToArray = require('stream-to-array');
/**
* JSONAPISerializer class.
*
* @example
* const JSONAPISerializer = require('json-api-serializer');
*
* // Create an instance of JSONAPISerializer with default settings
* const Serializer = new JSONAPISerializer();
*
* @class JSONAPISerializer
* @param {Object} [opts] Configuration options.
*/
module.exports = class JSONAPISerializer {
constructor(opts) {
this.opts = opts || {};
this.schemas = {};
}
/**
* Validate and apply default values to resource's configuration options.
*
* @method JSONAPISerializer#validateOptions
* @private
* @param {Object} options Configuration options.
* @return {Object}
*/
validateOptions(options) {
const optionsSchema = joi.object({
id: joi.string().default('id'),
blacklist: joi.array().items(joi.string()).single().default([]),
whitelist: joi.array().items(joi.string()).single().default([]),
links: joi.alternatives([joi.func(), joi.object()]).default({}),
relationships: joi.object().pattern(/.+/, joi.object({
type: joi.alternatives([joi.func(), joi.string()]).required(),
alternativeKey: joi.string(),
schema: joi.string().default('default'),
links: joi.alternatives([joi.func(), joi.object()]).default({}),
meta: joi.alternatives([joi.func(), joi.object()]).default({}),
})).default({}),
topLevelLinks: joi.alternatives([joi.func(), joi.object()]).default({}),
topLevelMeta: joi.alternatives([joi.func(), joi.object()]).default({}),
convertCase: joi.string().valid('kebab-case', 'snake_case', 'camelCase'),
// Deserialization options
unconvertCase: joi.string().valid('kebab-case', 'snake_case', 'camelCase'),
blacklistOnDeserialize: joi.array().items(joi.string()).single().default([]),
whitelistOnDeserialize: joi.array().items(joi.string()).single().default([]),
jsonapiObject: joi.boolean().default(true),
}).required();
const validated = joi.validate(options, optionsSchema);
if (validated.error) {
throw new Error(validated.error);
}
return validated.value;
}
/**
* Validate and apply default values to the dynamic type object option.
*
* @method JSONAPISerializer#validateDynamicTypeOptions
* @private
* @param {Object} options dynamic type object option.
* @return {Object}
*/
validateDynamicTypeOptions(options) {
const schema = joi.object({
type: joi.alternatives([joi.func(), joi.string()]).required(),
topLevelLinks: joi.alternatives([joi.func(), joi.object()]).default({}),
topLevelMeta: joi.alternatives([joi.func(), joi.object()]).default({}),
jsonapiObject: joi.boolean().default(true),
}).required();
const validated = joi.validate(options, schema);
if (validated.error) {
throw new Error(validated.error);
}
return validated.value;
}
/**
* Validate a JSONAPI error object
*
* @method JSONAPISerializer#validateError
* @private
* @param {Object} err a JSONAPI error object
* @return {Object}
*/
validateError(err) {
const errorSchema = joi.object({
id: joi.string(),
links: joi.object({
about: joi.alternatives([
joi.string(),
joi.object({
href: joi.string(),
meta: joi.object(),
})]),
}),
status: joi.string(),
code: joi.string(),
title: joi.string(),
detail: joi.string(),
source: joi.object({
pointer: joi.string(),
parameter: joi.string(),
}),
meta: joi.object(),
}).required();
const validated = joi.validate(err, errorSchema, { convert: true });
if (validated.error) {
throw new Error(validated.error);
}
return validated.value;
}
/**
* Register a resource with its type, schema name, and configuration options.
*
* @method JSONAPISerializer#register
* @param {string} type resource's type.
* @param {string} [schema=default] schema name.
* @param {Object} [options] Configuration options.
*/
register(type, schema, options) {
if (typeof schema === 'object') {
options = schema;
schema = 'default';
}
schema = schema || 'default';
options = Object.assign({}, this.opts, options);
_set(this.schemas, [type, schema].join('.'), this.validateOptions(options));
}
/**
* Serialze input data to a JSON API compliant response.
* Input data can be a simple object or an array of objects.
*
* @see {@link http://jsonapi.org/format/#document-top-level}
* @method JSONAPISerializer#serialize
* @param {string|Object} type resource's type as string or a dynamic type options as object.
* @param {Object|Object[]} data input data.
* @param {string} [schema=default] resource's schema name.
* @param {Object} [extraData] additional data that can be used in topLevelMeta options.
* @return {Object} serialized data.
*/
serialize(type, data, schema, extraData) {
// Support optional arguments (schema)
if (arguments.length === 3) {
if (_isPlainObject(schema)) {
extraData = schema;
schema = 'default';
}
}
schema = schema || 'default';
extraData = extraData || {};
const included = [];
let serializedData;
let options;
if (_isPlainObject(type)) { // Serialize data with the dynamic type
options = this.validateDynamicTypeOptions(type);
// Override top level data
serializedData = this.serializeMixedData(options, data, included, extraData);
} else { // Serialize data with the defined type
if (!this.schemas[type]) {
throw new Error(`No type registered for ${type}`);
}
if (schema && !this.schemas[type][schema]) {
throw new Error(`No schema ${schema} registered for ${type}`);
}
options = this.schemas[type][schema];
serializedData = this.serializeData(type, data, options, included, extraData);
}
return {
jsonapi: options.jsonapiObject ? { version: '1.0' } : undefined,
meta: this.processOptionsValues(data, extraData, options.topLevelMeta, 'extraData'),
links: this.processOptionsValues(data, extraData, options.topLevelLinks, 'extraData'),
data: serializedData,
included: this.serializeIncluded(included),
};
}
/**
* Asynchronously serialize input data to a JSON API compliant response.
* Input data can be a simple object or an array of objects.
*
* @see {@link http://jsonapi.org/format/#document-top-level}
* @method JSONAPISerializer#serializeAsync
* @param {string} type resource's type.
* @param {Object|Object[]} data input data.
* @param {string} [schema=default] resource's schema name.
* @param {Object} [extraData] additional data that can be used in topLevelMeta options.
* @return {Promise} resolves with serialized data.
*/
serializeAsync(type, data, schema, extraData) {
// Support optional arguments (schema)
if (arguments.length === 3) {
if (_isPlainObject(schema)) {
extraData = schema;
schema = 'default';
}
}
schema = schema || 'default';
extraData = extraData || {};
const included = [];
const isDataArray = Array.isArray(data);
const isDynamicType = _isPlainObject(type);
let serializedData;
let serializedIncludes;
let options;
if (isDynamicType) {
options = this.validateDynamicTypeOptions(type);
} else {
if (!this.schemas[type]) {
throw new Error(`No type registered for ${type}`);
}
if (schema && !this.schemas[type][schema]) {
throw new Error(`No schema ${schema} registered for ${type}`);
}
options = this.schemas[type][schema];
}
// Convert data into stream with serialization-transform. Single objects
// will be converted to an array to unify the serialization process. They
// will be converted back to a single object at the end.
const dataStream = intoStream.obj(isDataArray ? data : [data])
.pipe(through.obj((item, enc, callback) => {
try {
// Serialize a single item of the data-array.
const serializedItem = isDynamicType
? this.serializeMixedData(type, item, included, extraData)
: this.serializeData(type, item, options, included, extraData);
// If the serialized item is null, we won't push it to the stream,
// as pushing a null-value causes streams to end.
if (serializedItem === null) {
return callback();
}
return callback(null, serializedItem);
} catch (e) {
return callback(e);
}
}));
// Concat the processed stream back to an array and return promise-chain.
return streamToArray(dataStream)
.then((result) => {
serializedData = result;
// After the serialization of the dataStream is finished, the included
// objects (side-loaded relations) need to be serialized as well.
return this.serializeIncludedAsync(included);
})
.then((result) => {
serializedIncludes = result;
return {
jsonapi: options.jsonapiObject ? { version: '1.0' } : undefined,
meta: this.processOptionsValues(data, extraData, options.topLevelMeta, 'extraData'),
links: this.processOptionsValues(data, extraData, options.topLevelLinks, 'extraData'),
// If the source data was an array, we just pass the serialized data array.
// Otherwise we try to take the first (and only) item of it or pass null.
data: isDataArray ? serializedData : (serializedData[0] || null),
included: serializedIncludes,
};
});
}
/**
* Deserialize JSON API document data.
* Input data can be a simple object or an array of objects.
*
* @method JSONAPISerializer#deserialize
* @param {string|Object} type resource's type as string or an object with a dynamic type resolved from data.
* @param {Object} data JSON API input data.
* @param {string} [schema=default] resource's schema name.
* @return {Object} deserialized data.
*/
deserialize(type, data, schema) {
schema = schema || 'default';
if (!_isPlainObject(type)) {
if (!this.schemas[type]) {
throw new Error(`No type registered for ${type}`);
}
if (schema && !this.schemas[type][schema]) {
throw new Error(`No schema ${schema} registered for ${type}`);
}
} else {
type = this.validateDynamicTypeOptions(type);
}
let deserializedData = {};
if (data.data) {
if (Array.isArray(data.data)) {
deserializedData = data.data.map(resource => this.deserializeResource(type, resource, schema, data.included));
} else {
deserializedData = this.deserializeResource(type, data.data, schema, data.included);
}
}
return deserializedData;
}
/**
* Asynchronously Deserialize JSON API document data.
* Input data can be a simple object or an array of objects.
*
* @method JSONAPISerializer#deserializeAsync
* @param {string|Object} type resource's type as string or an object with a dynamic type resolved from data.
* @param {Object} data JSON API input data.
* @param {string} [schema=default] resource's schema name.
* @return {Promise} resolves with serialized data.
*/
deserializeAsync(type, data, schema) {
schema = schema || 'default';
if (!_isPlainObject(type)) {
if (!this.schemas[type]) {
throw new Error(`No type registered for ${type}`);
}
if (schema && !this.schemas[type][schema]) {
throw new Error(`No schema ${schema} registered for ${type}`);
}
} else {
type = this.validateDynamicTypeOptions(type);
}
return new Promise((resolve, reject) => { // eslint-disable-line consistent-return
if (Array.isArray(data.data)) {
const deserializedData = [];
const stream = intoStream.obj(data.data);
stream.on('data', (item) => {
deserializedData.push(this.deserializeResource(type, item, schema, data.included));
});
stream.on('end', () => resolve(deserializedData));
stream.on('error', reject);
} else {
return resolve(this.deserializeResource(type, data.data, schema, data.included));
}
});
}
/**
* Serialize any error into a JSON API error document.
* Input data can be:
* - An Error or an array of Error.
* - A JSON API error object or an array of JSON API error object.
*
* @see {@link http://jsonapi.org/format/#errors}
* @method JSONAPISerializer#serializeError
* @param {Error|Error[]|Object|Object[]} error an Error, an array of Error, a JSON API error object, an array of JSON API error object
* @return {Promise} resolves with serialized error.
*/
serializeError(error) {
function convertToError(err) {
let serializedError;
if (err instanceof Error) {
const status = err.status || err.statusCode;
serializedError = {
status: status && status.toString(),
code: err.code,
detail: err.message,
};
} else {
serializedError = this.validateError(err);
}
return serializedError;
}
const convertError = convertToError.bind(this);
if (Array.isArray(error)) {
return {
errors: error.map(err => convertError(err)),
};
}
return {
errors: [convertError(error)],
};
}
/**
* Deserialize a single JSON API resource.
* Input data must be a simple object.
*
* @method JSONAPISerializer#deserializeResource
* @param {string|Object} type resource's type as string or an object with a dynamic type resolved from data.
* @param {Object} data JSON API resource data.
* @param {string} [schema=default] resource's schema name.
* @param {Object[]} included.
* @return {Object} deserialized data.
*/
deserializeResource(type, data, schema, included) {
if (_isPlainObject(type)) {
type = (typeof type.type === 'function') ? type.type(data) : _get(data, type.type);
if (!type) {
throw new Error(`No type can be resolved from data: ${JSON.stringify(data)}`);
}
if (!this.schemas[type]) {
throw new Error(`No type registered for ${type}`);
}
schema = 'default';
}
const resourceOpts = this.schemas[type][schema];
let deserializedData = {};
// Deserialize id
deserializedData[resourceOpts.id] = data.id || undefined;
// whitelistOnDeserialize option
if (data.attributes && resourceOpts.whitelistOnDeserialize.length > 0) {
data.attributes = _pick(data.attributes, resourceOpts.whitelistOnDeserialize);
}
// Remove unwanted keys (blacklistOnDeserialize option)
if (data.attributes && resourceOpts.blacklistOnDeserialize.length > 0) {
data.attributes = _pick(data.attributes, _difference(Object.keys(data.attributes), resourceOpts.blacklistOnDeserialize));
}
Object.assign(deserializedData, data.attributes);
// Deserialize relationships
if (data.relationships) {
Object.keys(data.relationships).forEach((relationshipProperty) => {
const relationship = data.relationships[relationshipProperty];
// Support alternativeKey options for relationships
let relationshipKey = relationshipProperty;
if (resourceOpts.relationships[relationshipKey] && resourceOpts.relationships[relationshipKey].alternativeKey) {
relationshipKey = resourceOpts.relationships[relationshipKey].alternativeKey;
}
if (relationship.data !== undefined) {
if (Array.isArray(relationship.data)) {
// Array data
_set(deserializedData, relationshipKey, relationship.data.map(d => (included
? this.deserializeIncluded(d.type, d.id, resourceOpts.relationships[relationshipProperty], included)
: d.id)));
} else if (relationship.data === null) {
// null data
_set(deserializedData, relationshipKey, null);
} else {
// Object data
_set(deserializedData, relationshipKey, included
? this.deserializeIncluded(relationship.data.type, relationship.data.id, resourceOpts.relationships[relationshipProperty], included)
: relationship.data.id);
}
}
});
}
if (resourceOpts.unconvertCase) {
deserializedData = this._convertCase(deserializedData, resourceOpts.unconvertCase);
}
if (data.links) {
deserializedData.links = data.links;
}
if (data.meta) {
deserializedData.meta = data.meta;
}
return deserializedData;
}
deserializeIncluded(type, id, relationshipOpts, included) {
const includedResource = _find(included, {
type,
id,
});
if (!includedResource) {
return id;
}
return this.deserializeResource(type, includedResource, relationshipOpts.schema, included);
}
/**
* Serialize resource objects.
*
* @see {@link http://jsonapi.org/format/#document-resource-objects}
* @method JSONAPISerializer#serializeData
* @private
* @param {string} type resource's type.
* @param {Object|Object[]} data input data.
* @param {options} options resource's configuration options.
* @param {Object[]} included.
* @param {Object} extraData additional data.
* @return {Object|Object[]} serialized data.
*/
serializeData(type, data, options, included, extraData) {
// Empty data
if (_isEmpty(data)) {
// Return [] or null
return Array.isArray(data) ? data : null;
}
// Array data
if (Array.isArray(data)) {
return data.map(d => this.serializeData(type, d, options, included, extraData));
}
// Single data
return {
type,
id: data[options.id] ? data[options.id].toString() : undefined,
attributes: this.serializeAttributes(data, options),
relationships: this.serializeRelationships(data, options, included, extraData),
links: this.processOptionsValues(data, extraData, options.links),
};
}
/**
* Serialize mixed resource object with a dynamic type resolved from data
*
* @see {@link http://jsonapi.org/format/#document-resource-objects}
* @method JSONAPISerializer#serializeMixedData
* @private
* @param {Object} typeOption a dynamic type options.
* @param {Object|Object[]} data input data.
* @param {Object[]} included.
* @param {Object} extraData additional data.
* @return {Object|Object[]} serialized data.
*/
serializeMixedData(typeOption, data, included, extraData) {
// Empty data
if (_isEmpty(data)) {
// Return [] or null
return Array.isArray(data) ? data : null;
}
// Array data
if (Array.isArray(data)) {
return data.map(d => this.serializeMixedData(typeOption, d, included, extraData));
}
// Single data
// Resolve type from data (can be a string or a function deriving a type-string from each data-item)
const type = (typeof typeOption.type === 'function')
? typeOption.type(data)
: _get(data, typeOption.type);
if (!type) {
throw new Error(`No type can be resolved from data: ${JSON.stringify(data)}`);
}
if (!this.schemas[type]) {
throw new Error(`No type registered for ${type}`);
}
return this.serializeData(type, data, this.schemas[type].default, included, extraData);
}
/**
* Serialize top level 'included' key: an array of resource objects that are related to the resource data.
* Remove all duplicated resource.
*
* @method JSONAPISerializer#serializeIncluded
* @private
* @param {Object[]} included.
* @return {Object[]} included.
*/
serializeIncluded(included) {
const serializedIncluded = _uniqWith(included, _isEqual);
return Object.keys(serializedIncluded).length > 0 ? serializedIncluded : undefined;
}
/**
* Asynchronously serialize top level 'included' key: an array of resource
* objects that are related to the resource data.
* Remove all duplicated items.
*
* @method JSONAPISerializer#serializeIncludedAsync
* @private
* @param {Object[]} included.
* @return {Promise} resolves with serialized included data.
*/
serializeIncludedAsync(included) {
if (included.length === 0) {
return Promise.resolve(undefined);
}
// Convert array into stream and remove duplicates. Duplicates
// are identified by the comparison of a compound key of their
// `type` and `id` fields.
const uniqueStream = intoStream.obj(included)
.pipe(dedupeStream(item => `${item.type}-${item.id}`));
// Concat the stream to an array (which resolves with a promise).
return streamToArray(uniqueStream);
}
/**
* Serialize 'attributes' key of resource objects: an attributes object representing some of the resource's data.
*
* @see {@link http://jsonapi.org/format/#document-resource-object-attributes}
* @method JSONAPISerializer#serializeAttributes
* @private
* @param {Object|Object[]} data input data.
* @param {Object} options resource's configuration options.
* @return {Object} serialized attributes.
*/
serializeAttributes(data, options) {
if (options.whitelist.length > 0) {
data = _pick(data, options.whitelist);
}
// Support alternativeKey options for relationships
const alternativeKeys = [];
Object.keys(options.relationships).forEach((key) => {
const rOptions = options.relationships[key];
if (rOptions.alternativeKey) {
alternativeKeys.push(rOptions.alternativeKey);
}
});
// Remove unwanted keys (id, blacklist, relationships, alternativeKeys)
let serializedAttributes = _pick(data, _difference(Object.keys(data), [options.id].concat(Object.keys(options.relationships), alternativeKeys, options.blacklist)));
if (options.convertCase) {
serializedAttributes = this._convertCase(serializedAttributes, options.convertCase);
}
return Object.keys(serializedAttributes).length > 0 ? serializedAttributes : undefined;
}
/**
* Serialize 'relationships' key of resource objects: a relationships object describing relationships between the resource and other JSON API resources.
*
* @see {@link http://jsonapi.org/format/#document-resource-object-relationships}
* @method JSONAPISerializer#serializeRelationships
* @private
* @param {Object|Object[]} data input data.
* @param {Object} options resource's configuration options.
* @param {Object[]} included.
* @param {Object} extraData additional data.
* @return {Object} serialized relationships.
*/
serializeRelationships(data, options, included, extraData) {
const serializedRelationships = {};
Object.keys(options.relationships).forEach((relationship) => {
const rOptions = options.relationships[relationship];
// Support alternativeKey options for relationships
let relationshipKey = relationship;
if (!data[relationship] && rOptions.alternativeKey) {
relationshipKey = rOptions.alternativeKey;
}
let serializeRelationship = {
links: this.processOptionsValues(data, extraData, rOptions.links),
meta: this.processOptionsValues(data, extraData, rOptions.meta),
data: this.serializeRelationship(rOptions.type, rOptions.schema, _get(data, relationshipKey), included, data, extraData),
};
// Avoid empty relationship object
if (serializeRelationship.data === undefined && serializeRelationship.links === undefined && serializeRelationship.meta === undefined) {
serializeRelationship = {
data: null,
};
}
// Convert case
relationship = (options.convertCase) ? this._convertCase(relationship, options.convertCase) : relationship;
_set(serializedRelationships, relationship, serializeRelationship);
});
return Object.keys(serializedRelationships).length > 0 ? serializedRelationships : undefined;
}
/**
* Serialize 'data' key of relationship's resource objects.
*
* @see {@link http://jsonapi.org/format/#document-resource-object-linkage}
* @method JSONAPISerializer#serializeRelationship
* @private
* @param {string|Function} rType the relationship's type.
* @param {string} rSchema the relationship's schema
* @param {Object|Object[]} rData relationship's data.
* @param {Object[]} included.
* @param {Object} the entire resource's data.
* @param {Object} extraData additional data.
* @return {Object|Object[]} serialized relationship data.
*/
serializeRelationship(rType, rSchema, rData, included, data, extraData) {
const schema = rSchema || 'default';
// No relationship data
if (rData === undefined) {
return undefined;
}
// Empty relationship data
if (!(typeof rData === 'number') && _isEmpty(rData)) {
// Return [] or null
return Array.isArray(rData) ? rData : null;
}
// To-many relationships
if (Array.isArray(rData)) {
return rData.map(d => this.serializeRelationship(rType, schema, d, included, data, extraData));
}
// Resolve relationship type
const type = (typeof rType === 'function') ? rType(rData, data) : rType;
if (!type) {
throw new Error(`No type can be resolved from relationship's data: ${JSON.stringify(rData)}`);
}
if (!this.schemas[type]) {
throw new Error(`No type registered for "${type}"`);
}
if (!this.schemas[type][schema]) {
throw new Error(`No schema "${schema}" registered for type "${type}"`);
}
// To-one relationship
const rOptions = this.schemas[type][schema];
const serializedRelationship = { type };
// Support for unpopulated relationships (an id, or array of ids)
if (!_isObjectLike(rData)) {
serializedRelationship.id = rData.toString();
} else if (rData._bsontype && rData._bsontype === 'ObjectID') {
// Support for unpopulated relationships (with mongoDB BSON ObjectId)
serializedRelationship.id = rData.toString();
} else {
// Relationship has been populated
serializedRelationship.id = rData[rOptions.id].toString();
included.push(this.serializeData(type, rData, rOptions, included, extraData));
}
return serializedRelationship;
}
/**
* Process options values.
* Allows options to be an object or a function with 1 or 2 arguments
*
* @method JSONAPISerializer#processOptionsValues
* @private
* @param {Object} data data passed to functions options
* @param {Object} extraData additional data passed to functions options
* @param {Object} options configuration options.
* @param {string} fallbackModeIfOneArg fallback mode if only one argument is passed to function.
* Avoid breaking changes with issue https://github.com/danivek/json-api-serializer/issues/27.
* @return {Object}
*/
processOptionsValues(data, extraData, options, fallbackModeIfOneArg) {
let processedOptions = {};
if (options && typeof options === 'function') {
// Backward compatible with functions with one 'extraData' argument
processedOptions = (fallbackModeIfOneArg === 'extraData' && options.length === 1) ? options(extraData) : options(data, extraData);
} else {
Object.keys(options).forEach((key) => {
let processedValue = {};
if (options[key] && typeof options[key] === 'function') {
// Backward compatible with functions with one 'extraData' argument
processedValue = (fallbackModeIfOneArg === 'extraData' && options[key].length === 1) ? options[key](extraData) : options[key](data, extraData);
} else {
processedValue = options[key];
}
Object.assign(processedOptions, { [key]: processedValue });
});
}
// Clean all undefined values
processedOptions = _omitBy(processedOptions, _isUndefined);
return Object.keys(processedOptions).length > 0 ? processedOptions : undefined;
}
/**
* Recursively convert object keys case
*
* @method JSONAPISerializer#_convertCase
* @private
* @param {Object|Object[]|string} data to convert
* @param {string} convertCaseOptions can be snake_case', 'kebab-case' or 'camelCase' format.
* @return {Object}
*/
_convertCase(data, convertCaseOptions) {
let converted;
if (Array.isArray(data) || _isPlainObject(data)) {
converted = _transform(data, (result, value, key) => {
if (Array.isArray(value) || _isPlainObject(value)) {
result[this._convertCase(key, convertCaseOptions)] = this._convertCase(value, convertCaseOptions);
} else {
result[this._convertCase(key, convertCaseOptions)] = value;
}
});
} else {
switch (convertCaseOptions) {
case 'snake_case':
converted = _snakeCase(data);
break;
case 'kebab-case':
converted = _kebabCase(data);
break;
case 'camelCase':
converted = _camelCase(data);
break;
default: // Do nothing
}
}
return converted;
}
};