UNPKG

devour-client

Version:

A lightweight, framework agnostic, flexible JSON API client

126 lines (99 loc) 2.9 kB
"use strict"; var _isPlainObject = require('lodash/isPlainObject'); var _includes = require('lodash/includes'); var _map = require('lodash/map'); var _forOwn = require('lodash/forOwn'); function collection(modelName, items) { var _this = this; if (!modelName) { return items; } return items.map(function (item) { return resource.call(_this, modelName, item); }); } function resource(modelName, item) { if (!modelName) { return item; } var model = this.modelFor(modelName); var options = model.options || {}; var readOnly = options.readOnly || []; var typeName = options.type || this.pluralize(modelName); var serializedAttributes = {}; var serializedRelationships = {}; var serializedResource = {}; if (options.serializer) { return options.serializer.call(this, item); } _forOwn(model.attributes, function (value, key) { if (isReadOnly(key, readOnly)) { return; } if (isRelationship(value)) { serializeRelationship(key, item[key], value, serializedRelationships); } else if (item[key] !== undefined) { serializedAttributes[key] = item[key]; } }); serializedResource.type = typeName; if (Object.keys(serializedAttributes).length > 0) { serializedResource.attributes = serializedAttributes; } if (Object.keys(serializedRelationships).length > 0) { serializedResource.relationships = serializedRelationships; } if (item.id) { serializedResource.id = item.id; } if (item.meta) { serializedResource.meta = item.meta; } if (item.links) { serializedResource.links = item.links; } return serializedResource; } function isReadOnly(attribute, readOnly) { return readOnly.indexOf(attribute) !== -1; } function isRelationship(attribute) { return _isPlainObject(attribute) && _includes(['hasOne', 'hasMany'], attribute.jsonApi); } function serializeRelationship(relationshipName, relationship, relationshipType, serializeRelationships) { if (relationshipType.jsonApi === 'hasMany' && relationship !== undefined) { serializeRelationships[relationshipName] = serializeHasMany(relationship, relationshipType.type); } if (relationshipType.jsonApi === 'hasOne' && relationship !== undefined) { serializeRelationships[relationshipName] = serializeHasOne(relationship, relationshipType.type); } } function serializeHasMany(relationships, type) { return { data: _map(relationships, function (item) { return { id: item.id, type: type || item.type, meta: item.meta }; }) }; } function serializeHasOne(relationship, type) { if (relationship === null) { return { data: null }; } return { data: { id: relationship.id, type: type || relationship.type, meta: relationship.meta } }; } module.exports = { resource: resource, collection: collection };