@@ -27,7 +27,7 @@ const { validateOptions, validateDynamicTypeOptions, validateError } = require('
2727 * const serializer = new JSONAPISerializer();
2828 *
2929 * @class JSONAPISerializer
30- * @param {object } [opts] Global options.
30+ * @param {Options } [opts] Global options.
3131 */
3232module . exports = class JSONAPISerializer {
3333 constructor ( opts ) {
@@ -49,8 +49,8 @@ module.exports = class JSONAPISerializer {
4949 *
5050 * @function JSONAPISerializer#register
5151 * @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.
5454 */
5555 register ( type , schema , options ) {
5656 if ( typeof schema === 'object' ) {
@@ -71,13 +71,13 @@ module.exports = class JSONAPISerializer {
7171 *
7272 * @see {@link http://jsonapi.org/format/#document-top-level }
7373 * @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.
7575 * @param {object|object[] } data input data.
7676 * @param {string|object } [schema='default'] resource's schema name.
7777 * @param {object } [extraData] additional data that can be used in topLevelMeta options.
7878 * @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.
8181 */
8282 serialize ( type , data , schema , extraData , excludeData , overrideSchemaOptions = { } ) {
8383 // Support optional arguments (schema)
@@ -155,12 +155,12 @@ module.exports = class JSONAPISerializer {
155155 *
156156 * @see {@link http://jsonapi.org/format/#document-top-level }
157157 * @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 .
159159 * @param {object|object[] } data input data.
160160 * @param {string } [schema='default'] resource's schema name.
161161 * @param {object } [extraData] additional data that can be used in topLevelMeta options.
162162 * @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.
164164 * @returns {Promise } resolves with serialized data.
165165 */
166166 serializeAsync ( type , data , schema , extraData , excludeData , overrideSchemaOptions = { } ) {
@@ -280,7 +280,7 @@ module.exports = class JSONAPISerializer {
280280 * Input data can be a simple object or an array of objects.
281281 *
282282 * @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 .
284284 * @param {object } data JSON API input data.
285285 * @param {string } [schema='default'] resource's schema name.
286286 * @returns {object } deserialized data.
@@ -318,7 +318,7 @@ module.exports = class JSONAPISerializer {
318318 * Input data can be a simple object or an array of objects.
319319 *
320320 * @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 .
322322 * @param {object } data JSON API input data.
323323 * @param {string } [schema='default'] resource's schema name.
324324 * @returns {Promise } resolves with serialized data.
@@ -404,7 +404,8 @@ module.exports = class JSONAPISerializer {
404404 * Input data must be a simple object.
405405 *
406406 * @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.
408409 * @param {object } data JSON API resource data.
409410 * @param {string } [schema='default'] resource's schema name.
410411 * @param {Map<string, object> } included Included resources.
@@ -529,6 +530,18 @@ module.exports = class JSONAPISerializer {
529530 return deserializedData ;
530531 }
531532
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+ */
532545 deserializeIncluded ( type , id , relationshipOpts , included , lineage ) {
533546 const includedResource = included . find (
534547 ( resource ) => resource . type === type && resource . id === id
@@ -555,10 +568,10 @@ module.exports = class JSONAPISerializer {
555568 * @private
556569 * @param {string } type resource's type.
557570 * @param {object|object[] } data input data.
558- * @param {object } options resource's configuration options.
571+ * @param {Options } options resource's configuration options.
559572 * @param {Map<string, object> } [included] Included resources.
560573 * @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.
562575 * @returns {object|object[] } serialized data.
563576 */
564577 serializeResource ( type , data , options , included , extraData , overrideSchemaOptions = { } ) {
@@ -599,11 +612,11 @@ module.exports = class JSONAPISerializer {
599612 * @see {@link http://jsonapi.org/format/#document-resource-objects }
600613 * @function JSONAPISerializer#serializeMixedResource
601614 * @private
602- * @param {object } typeOption a dynamic type options.
615+ * @param {DynamicTypeOptions } typeOption a dynamic type options.
603616 * @param {object|object[] } data input data.
604617 * @param {Map<string, object> } [included] Included resources.
605618 * @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.
607620 * @returns {object|object[] } serialized data.
608621 */
609622 serializeMixedResource ( typeOption , data , included , extraData , overrideSchemaOptions = { } ) {
@@ -646,7 +659,7 @@ module.exports = class JSONAPISerializer {
646659 * @function JSONAPISerializer#serializeAttributes
647660 * @private
648661 * @param {object|object[] } data input data.
649- * @param {object } options resource's configuration options.
662+ * @param {Options } options resource's configuration options.
650663 * @returns {object } serialized attributes.
651664 */
652665 serializeAttributes ( data , options ) {
@@ -685,10 +698,10 @@ module.exports = class JSONAPISerializer {
685698 * @function JSONAPISerializer#serializeRelationships
686699 * @private
687700 * @param {object|object[] } data input data.
688- * @param {object } options resource's configuration options.
701+ * @param {Options } options resource's configuration options.
689702 * @param {Map<string, object> } [included] Included resources.
690703 * @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.
692705 * @returns {object } serialized relationships.
693706 */
694707 serializeRelationships ( data , options , included , extraData , overrideSchemaOptions = { } ) {
@@ -746,7 +759,7 @@ module.exports = class JSONAPISerializer {
746759 * @param {Map<string, object> } [included] Included resources.
747760 * @param {object } [data] the entire resource's data.
748761 * @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.
750763 * @returns {object|object[] } serialized relationship data.
751764 */
752765 serializeRelationship (
@@ -957,3 +970,42 @@ module.exports = class JSONAPISerializer {
957970 return data ;
958971 }
959972} ;
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