@@ -27,7 +27,7 @@ const { validateOptions, validateDynamicTypeOptions, validateError } = require('
27
27
* const serializer = new JSONAPISerializer();
28
28
*
29
29
* @class JSONAPISerializer
30
- * @param {object } [opts] Global options.
30
+ * @param {Options } [opts] Global options.
31
31
*/
32
32
module . exports = class JSONAPISerializer {
33
33
constructor ( opts ) {
@@ -49,8 +49,8 @@ module.exports = class JSONAPISerializer {
49
49
*
50
50
* @function JSONAPISerializer#register
51
51
* @param {string } type resource's type.
52
- * @param {string|object } [schema='default'] schema name.
53
- * @param {object } [options] options.
52
+ * @param {string|Options } [schema='default'] schema name.
53
+ * @param {Options } [options] options.
54
54
*/
55
55
register ( type , schema , options ) {
56
56
if ( typeof schema === 'object' ) {
@@ -71,13 +71,13 @@ module.exports = class JSONAPISerializer {
71
71
*
72
72
* @see {@link http://jsonapi.org/format/#document-top-level }
73
73
* @function JSONAPISerializer#serialize
74
- * @param {string|object } type resource's type as string or a dynamic type options as object.
74
+ * @param {string|DynamicTypeOptions } type resource's type as string or a dynamic type options as object.
75
75
* @param {object|object[] } data input data.
76
76
* @param {string|object } [schema='default'] resource's schema name.
77
77
* @param {object } [extraData] additional data that can be used in topLevelMeta options.
78
78
* @param {boolean } [excludeData] boolean that can be set to exclude the `data` property in serialized data.
79
- * @param {object } [overrideSchemaOptions=] additional schema options, a map of types with options to override
80
- * @returns {object } serialized data.
79
+ * @param {object } [overrideSchemaOptions={} ] additional schema options, a map of types with options to override.
80
+ * @returns {object|object[] } serialized data.
81
81
*/
82
82
serialize ( type , data , schema , extraData , excludeData , overrideSchemaOptions = { } ) {
83
83
// Support optional arguments (schema)
@@ -155,12 +155,12 @@ module.exports = class JSONAPISerializer {
155
155
*
156
156
* @see {@link http://jsonapi.org/format/#document-top-level }
157
157
* @function JSONAPISerializer#serializeAsync
158
- * @param {string|object } type resource's type or an object with a dynamic type resolved from data. .
158
+ * @param {string|DynamicTypeOptions } type resource's type as string or a dynamic type options as object .
159
159
* @param {object|object[] } data input data.
160
160
* @param {string } [schema='default'] resource's schema name.
161
161
* @param {object } [extraData] additional data that can be used in topLevelMeta options.
162
162
* @param {boolean } [excludeData] boolean that can be set to exclude the `data` property in serialized data.
163
- * @param {object } [overrideSchemaOptions=] additional schema options, a map of types with options to override
163
+ * @param {object } [overrideSchemaOptions={} ] additional schema options, a map of types with options to override.
164
164
* @returns {Promise } resolves with serialized data.
165
165
*/
166
166
serializeAsync ( type , data , schema , extraData , excludeData , overrideSchemaOptions = { } ) {
@@ -280,7 +280,7 @@ module.exports = class JSONAPISerializer {
280
280
* Input data can be a simple object or an array of objects.
281
281
*
282
282
* @function JSONAPISerializer#deserialize
283
- * @param {string|object } type resource's type as string or an object with a dynamic type resolved from data .
283
+ * @param {string|DynamicTypeOptions } type resource's type as string or a dynamic type options as object .
284
284
* @param {object } data JSON API input data.
285
285
* @param {string } [schema='default'] resource's schema name.
286
286
* @returns {object } deserialized data.
@@ -318,7 +318,7 @@ module.exports = class JSONAPISerializer {
318
318
* Input data can be a simple object or an array of objects.
319
319
*
320
320
* @function JSONAPISerializer#deserializeAsync
321
- * @param {string|object } type resource's type as string or an object with a dynamic type resolved from data .
321
+ * @param {string|DynamicTypeOptions } type resource's type as string or a dynamic type options as object .
322
322
* @param {object } data JSON API input data.
323
323
* @param {string } [schema='default'] resource's schema name.
324
324
* @returns {Promise } resolves with serialized data.
@@ -404,7 +404,8 @@ module.exports = class JSONAPISerializer {
404
404
* Input data must be a simple object.
405
405
*
406
406
* @function JSONAPISerializer#deserializeResource
407
- * @param {string|object } type resource's type as string or an object with a dynamic type resolved from data.
407
+ * @private
408
+ * @param {string|DynamicTypeOptions } type resource's type as string or an object with a dynamic type resolved from data.
408
409
* @param {object } data JSON API resource data.
409
410
* @param {string } [schema='default'] resource's schema name.
410
411
* @param {Map<string, object> } included Included resources.
@@ -529,6 +530,18 @@ module.exports = class JSONAPISerializer {
529
530
return deserializedData ;
530
531
}
531
532
533
+ /**
534
+ * Deserialize included
535
+ *
536
+ * @function JSONAPISerializer#deserializeIncluded
537
+ * @private
538
+ * @param {string } type resource's type as string or an object with a dynamic type resolved from data.
539
+ * @param {string } id identifier of the resource.
540
+ * @param {RelationshipOptions } relationshipOpts relationship option.
541
+ * @param {Map<string, object> } included Included resources.
542
+ * @param {string[] } lineage resource identifiers already deserialized to prevent circular references.
543
+ * @returns {object } deserialized data.
544
+ */
532
545
deserializeIncluded ( type , id , relationshipOpts , included , lineage ) {
533
546
const includedResource = included . find (
534
547
( resource ) => resource . type === type && resource . id === id
@@ -555,10 +568,10 @@ module.exports = class JSONAPISerializer {
555
568
* @private
556
569
* @param {string } type resource's type.
557
570
* @param {object|object[] } data input data.
558
- * @param {object } options resource's configuration options.
571
+ * @param {Options } options resource's configuration options.
559
572
* @param {Map<string, object> } [included] Included resources.
560
573
* @param {object } [extraData] additional data.
561
- * @param {object } [overrideSchemaOptions=] additional schema options, a map of types with options to override
574
+ * @param {object } [overrideSchemaOptions={} ] additional schema options, a map of types with options to override.
562
575
* @returns {object|object[] } serialized data.
563
576
*/
564
577
serializeResource ( type , data , options , included , extraData , overrideSchemaOptions = { } ) {
@@ -599,11 +612,11 @@ module.exports = class JSONAPISerializer {
599
612
* @see {@link http://jsonapi.org/format/#document-resource-objects }
600
613
* @function JSONAPISerializer#serializeMixedResource
601
614
* @private
602
- * @param {object } typeOption a dynamic type options.
615
+ * @param {DynamicTypeOptions } typeOption a dynamic type options.
603
616
* @param {object|object[] } data input data.
604
617
* @param {Map<string, object> } [included] Included resources.
605
618
* @param {object } [extraData] additional data.
606
- * @param {object } [overrideSchemaOptions=] additional schema options, a map of types with options to override
619
+ * @param {object } [overrideSchemaOptions={} ] additional schema options, a map of types with options to override.
607
620
* @returns {object|object[] } serialized data.
608
621
*/
609
622
serializeMixedResource ( typeOption , data , included , extraData , overrideSchemaOptions = { } ) {
@@ -646,7 +659,7 @@ module.exports = class JSONAPISerializer {
646
659
* @function JSONAPISerializer#serializeAttributes
647
660
* @private
648
661
* @param {object|object[] } data input data.
649
- * @param {object } options resource's configuration options.
662
+ * @param {Options } options resource's configuration options.
650
663
* @returns {object } serialized attributes.
651
664
*/
652
665
serializeAttributes ( data , options ) {
@@ -685,10 +698,10 @@ module.exports = class JSONAPISerializer {
685
698
* @function JSONAPISerializer#serializeRelationships
686
699
* @private
687
700
* @param {object|object[] } data input data.
688
- * @param {object } options resource's configuration options.
701
+ * @param {Options } options resource's configuration options.
689
702
* @param {Map<string, object> } [included] Included resources.
690
703
* @param {object } [extraData] additional data.
691
- * @param {object } [overrideSchemaOptions=] additional schema options, a map of types with options to override
704
+ * @param {object } [overrideSchemaOptions={} ] additional schema options, a map of types with options to override.
692
705
* @returns {object } serialized relationships.
693
706
*/
694
707
serializeRelationships ( data , options , included , extraData , overrideSchemaOptions = { } ) {
@@ -746,7 +759,7 @@ module.exports = class JSONAPISerializer {
746
759
* @param {Map<string, object> } [included] Included resources.
747
760
* @param {object } [data] the entire resource's data.
748
761
* @param {object } [extraData] additional data.
749
- * @param {object } [overrideSchemaOptions=] additional schema options, a map of types with options to override
762
+ * @param {object } [overrideSchemaOptions={} ] additional schema options, a map of types with options to override.
750
763
* @returns {object|object[] } serialized relationship data.
751
764
*/
752
765
serializeRelationship (
@@ -957,3 +970,42 @@ module.exports = class JSONAPISerializer {
957
970
return data ;
958
971
}
959
972
} ;
973
+
974
+ /**
975
+ * @typedef {object } Options
976
+ * @property {string } [id='id'] the key to use as the reference. Default = 'id'
977
+ * @property {string[] } [blacklist=[]] an array of blacklisted attributes. Default = []
978
+ * @property {string[] } [whitelist=[]] an array of whitelisted attributes. Default = []
979
+ * @property {boolean } [jsonapiObject=true] enable/Disable JSON API Object. Default = true
980
+ * @property {Function|object } [links] describes the links inside data
981
+ * @property {Function|object } [topLevelLinks] describes the top-level links
982
+ * @property {Function|object } [topLevelMeta] describes the top-level meta
983
+ * @property {Function|object } [meta] describes resource-level meta
984
+ * @property {object.<string, RelationshipOptions> } [relationships] an object defining some relationships
985
+ * @property {string[] } [blacklistOnDeserialize=[]] an array of blacklisted attributes. Default = []
986
+ * @property {string[] } [whitelistOnDeserialize=[]] an array of whitelisted attributes. Default = []
987
+ * @property {('kebab-case'|'snake_case'|'camelCase') } [convertCase] case conversion for serializing data
988
+ * @property {('kebab-case'|'snake_case'|'camelCase') } [unconvertCase] case conversion for deserializing data
989
+ * @property {number } [convertCaseCacheSize=5000] When using convertCase, a LRU cache is utilized for optimization. The default size of the cache is 5000 per conversion type.
990
+ * @property {Function } [beforeSerialize] a function to transform data before serialization.
991
+ * @property {Function } [afterDeserialize] a function to transform data after deserialization.
992
+ */
993
+
994
+ /**
995
+ * @typedef {object } RelationshipOptions
996
+ * @property {string|Function } type a string or a function for the type to use for serializing the relationship (type need to be register)
997
+ * @property {string } [alternativeKey] an alternative key (string or path) to use if relationship key not exist (example: 'author_id' as an alternative key for 'author' relationship)
998
+ * @property {string } [schema] a custom schema for serializing the relationship. If no schema define, it use the default one.
999
+ * @property {Function|object } [links] describes the links for the relationship
1000
+ * @property {Function|object } [meta] describes meta that contains non-standard meta-information about the relationship
1001
+ * @property {Function } [deserialize] describes the function which should be used to deserialize a related property which is not included in the JSON:API document
1002
+ */
1003
+
1004
+ /**
1005
+ *
1006
+ * @typedef {object } DynamicTypeOptions
1007
+ * @property {string } id a string for the path to the key to use to determine type or a function deriving a type-string from each data-item.
1008
+ * @property {boolean } [jsonapiObject=true] enable/Disable JSON API Object.
1009
+ * @property {Function|object } [topLevelLinks] describes the top-level links
1010
+ * @property {Function|object } [topLevelMeta] describes the top-level meta.
1011
+ */
0 commit comments