Skip to content

Commit cf49cdd

Browse files
committed
chore: fix JSDoc warning and improve JSDoc
1 parent e3bccac commit cf49cdd

File tree

2 files changed

+80
-28
lines changed

2 files changed

+80
-28
lines changed

lib/JSONAPISerializer.js

+71-19
Original file line numberDiff line numberDiff line change
@@ -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
*/
3232
module.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+
*/

lib/validator.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,8 @@ function validateError(err) {
173173
* @throws Will throw an error if the argument is not an object
174174
* @throws Will throw an error if the argument contains unpermitted members
175175
* @throws Will throw an error if `links.about` is present but not a string
176-
* @param {object} linksObj
177-
* @returns {object}
176+
* @param {object} linksObj links object
177+
* @returns {object} validated links
178178
*/
179179
const isValidLink = function isValidLink(linksObj) {
180180
const permittedMembers = ['about'];
@@ -211,12 +211,12 @@ function validateError(err) {
211211
* @function isValidSource
212212
* @private
213213
* @see https://jsonapi.org/format/#error-objects
214-
* @param {object} sourceObj
214+
* @param {object} sourceObj source object
215215
* @throws Will throw an error if the argument is not an object
216216
* @throws Will throw an error if the argument contains unpermitted members
217217
* @throws Will throw an error if `sourceObj.pointer` is present but not a string
218218
* @throws Will throw an error if `sourceObj.parameter` is present but not a string
219-
* @returns {object}
219+
* @returns {object} validated source
220220
*/
221221
const isValidSource = function isValidSource(sourceObj) {
222222
const permittedMembers = ['pointer', 'parameter'];
@@ -252,9 +252,9 @@ function validateError(err) {
252252
* @function isValidMeta
253253
* @private
254254
* @see https://jsonapi.org/format/#error-objects
255-
* @param {object} metaObj
255+
* @param {object} metaObj meta object
256256
* @throws Will throw an error if the argument is not an object
257-
* @returns {object}
257+
* @returns {object} validated meta
258258
*/
259259
const isValidMeta = function isValidMeta(metaObj) {
260260
if (typeof metaObj !== 'object') {
@@ -271,7 +271,7 @@ function validateError(err) {
271271
* @private
272272
* @see https://jsonapi.org/format/#error-objects
273273
* @param {number} theStatusCode The status code to validate
274-
* @returns {boolean}
274+
* @returns {boolean} is valid
275275
*/
276276
const isValidHttpStatusCode = function isValidHttpStatusCode(theStatusCode) {
277277
const validHttpStatusCodes = [
@@ -306,10 +306,10 @@ function validateError(err) {
306306
* @function isValidStatus
307307
* @private
308308
* @see https://jsonapi.org/format/#error-objects
309-
* @param {string|number} theStatusCode
309+
* @param {string|number} theStatusCode status code
310310
* @throws Will throw an error if the argument is not number-like
311311
* @throws Will throw an error if the argument is not a valid HTTP status code
312-
* @returns {string}
312+
* @returns {string} validated status
313313
*/
314314
const isValidStatus = function isValidStatus(theStatusCode) {
315315
const statusAsNumber = Number(theStatusCode);

0 commit comments

Comments
 (0)