|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const _ = require('lodash'); |
| 4 | +const joi = require('joi'); |
| 5 | + |
| 6 | +module.exports = class JSONAPISerializer { |
| 7 | + constructor(opts) { |
| 8 | + this.opts = opts || {}; |
| 9 | + this.schemas = {}; |
| 10 | + } |
| 11 | + |
| 12 | + validateOptions(options) { |
| 13 | + const optionsSchema = joi.object({ |
| 14 | + blackList: joi.array().items(joi.string()).single().default([]), |
| 15 | + id: joi.string().default('id'), |
| 16 | + relationships: joi.object().pattern(/.+/, joi.object({ |
| 17 | + type: joi.string().required(), |
| 18 | + })).default({}), |
| 19 | + }).required(); |
| 20 | + |
| 21 | + const validated = joi.validate(options, optionsSchema); |
| 22 | + |
| 23 | + if (validated.error) { |
| 24 | + throw new Error(validated.error); |
| 25 | + } |
| 26 | + |
| 27 | + return validated.value; |
| 28 | + } |
| 29 | + |
| 30 | + register(type, schemaName, options) { |
| 31 | + if (_.isObject(schemaName)) { |
| 32 | + options = schemaName; |
| 33 | + schemaName = 'default'; |
| 34 | + } |
| 35 | + |
| 36 | + const name = schemaName || 'default'; |
| 37 | + const opts = this.validateOptions(_.defaults({}, options)); |
| 38 | + |
| 39 | + _.set(this.schemas, [type, name].join('.'), opts); |
| 40 | + } |
| 41 | + |
| 42 | + serialize(type, data, schemaName) { |
| 43 | + const schema = schemaName || 'default'; |
| 44 | + |
| 45 | + if (!this.schemas[type]) { |
| 46 | + throw new Error('No type registered for ' + type); |
| 47 | + } |
| 48 | + |
| 49 | + if (schema && !this.schemas[type][schema]) { |
| 50 | + throw new Error('No schema ' + schema + ' registered for ' + type); |
| 51 | + } |
| 52 | + |
| 53 | + const included = []; |
| 54 | + return _.assign({}, { |
| 55 | + jsonapi: { |
| 56 | + version: '1.0', |
| 57 | + }, |
| 58 | + links: this.getLinks(this.opts, this.schemas[type][schema].topLevelLinks), |
| 59 | + data: this.serializeData(type, data, this.schemas[type][schema], included), |
| 60 | + included: this.serializeIncluded(included), |
| 61 | + }); |
| 62 | + } |
| 63 | + |
| 64 | + serializeData(type, data, options, included) { |
| 65 | + // Empty data |
| 66 | + if (_.isEmpty(data)) { |
| 67 | + // Respond [] or null |
| 68 | + return _.isArray(data) ? data : null; |
| 69 | + } |
| 70 | + |
| 71 | + // Array data |
| 72 | + if (_.isArray(data)) { |
| 73 | + return data.map(d => this.serializeData(type, d, options, included)); |
| 74 | + } |
| 75 | + |
| 76 | + // Single data |
| 77 | + const resource = { |
| 78 | + type: type, |
| 79 | + id: data[options.id], |
| 80 | + attributes: this.serializeAttributes(data, options), |
| 81 | + relationships: this.serializeRelationships(data, options, included), |
| 82 | + links: this.getLinks(data, options.links), |
| 83 | + }; |
| 84 | + |
| 85 | + return resource; |
| 86 | + } |
| 87 | + |
| 88 | + serializeIncluded(included) { |
| 89 | + const serializedIncluded = _.uniqWith(included, _.isEqual); |
| 90 | + return !_.isEmpty(serializedIncluded) ? serializedIncluded : undefined; |
| 91 | + } |
| 92 | + |
| 93 | + serializeAttributes(data, options) { |
| 94 | + return _.pick(data, _.difference(Object.keys(data), _.concat([options.id], Object.keys(options.relationships), options.blackList))); |
| 95 | + } |
| 96 | + |
| 97 | + serializeRelationships(data, options, included) { |
| 98 | + const serializedRelationships = {}; |
| 99 | + |
| 100 | + _.forOwn(options.relationships, (rOptions, relationship) => { |
| 101 | + const serializeRelationship = { |
| 102 | + data: this.serializeRelationship(rOptions.type, data[relationship], this.schemas[options.relationships[relationship].type].default, included), |
| 103 | + links: this.getLinks(data, rOptions.links), |
| 104 | + }; |
| 105 | + _.set(serializedRelationships, relationship, serializeRelationship); |
| 106 | + }); |
| 107 | + |
| 108 | + return !_.isEmpty(serializedRelationships) ? serializedRelationships : undefined; |
| 109 | + } |
| 110 | + |
| 111 | + serializeRelationship(rType, rData, rOptions, included) { |
| 112 | + // To-many relationships |
| 113 | + if (_.isArray(rData)) { |
| 114 | + return rData.map(d => this.serializeRelationship(rType, d, rOptions, included)); |
| 115 | + } |
| 116 | + |
| 117 | + // To-one relationship |
| 118 | + const serializedRelationship = { |
| 119 | + type: rType, |
| 120 | + }; |
| 121 | + |
| 122 | + // support for unpopulated relationships (an id, or array of ids) |
| 123 | + if (!_.isPlainObject(rData)) { |
| 124 | + // Relationship has not been populated |
| 125 | + serializedRelationship.id = rData; |
| 126 | + } else { |
| 127 | + // Relationship has been populated |
| 128 | + serializedRelationship.id = rData[rOptions.id]; |
| 129 | + included.push(this.serializeData(rType, rData, rOptions)); |
| 130 | + } |
| 131 | + |
| 132 | + return serializedRelationship; |
| 133 | + } |
| 134 | + |
| 135 | + getLinks(data, linksOptions) { |
| 136 | + const links = _.mapValues(linksOptions, linkOpts => { |
| 137 | + let link; |
| 138 | + if (_.isFunction(linkOpts)) { |
| 139 | + link = linkOpts(data); |
| 140 | + } else { |
| 141 | + link = linkOpts; |
| 142 | + } |
| 143 | + return link; |
| 144 | + }); |
| 145 | + |
| 146 | + return !_.isEmpty(links) ? links : undefined; |
| 147 | + } |
| 148 | +}; |
0 commit comments