Skip to content

Commit

Permalink
Merge pull request #1 from twg/master
Browse files Browse the repository at this point in the history
Update master
  • Loading branch information
GregPeden committed Sep 21, 2017
2 parents 4c2eefb + f430833 commit d1d0a12
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 11 deletions.
14 changes: 9 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const _ = require('lodash')
const Promise = require('es6-promise').Promise
const deserialize = require('./middleware/json-api/_deserialize')
const serialize = require('./middleware/json-api/_serialize')
const Minilog = require('minilog')
const Logger = require('./logger')

/*
* == JsonApiMiddleware
Expand Down Expand Up @@ -79,7 +79,6 @@ class JsonApi {
this.serialize = serialize
this.builderStack = []
this.resetBuilderOnCall = !!options.resetBuilderOnCall
this.logger = Minilog('devour')
if (options.pluralize === false) {
this.pluralize = s => s
this.pluralize.singular = s => s
Expand All @@ -89,15 +88,15 @@ class JsonApi {
this.pluralize = pluralize
}
this.trailingSlash = options.trailingSlash === true ? _.forOwn(_.clone(defaults.trailingSlash), (v, k, o) => { _.set(o, k, true) }) : options.trailingSlash
options.logger ? Minilog.enable() : Minilog.disable()
options.logger ? Logger.enable() : Logger.disable()

if (deprecatedConstructors(arguments)) {
this.logger.warn('Constructor (apiUrl, middleware) has been deprecated, initialize Devour with an object.')
Logger.warn('Constructor (apiUrl, middleware) has been deprecated, initialize Devour with an object.')
}
}

enableLogging (enabled = true) {
enabled ? Minilog.enable() : Minilog.disable()
enabled ? Logger.enable() : Logger.disable()
}

one (model, id) {
Expand All @@ -110,6 +109,11 @@ class JsonApi {
return this
}

relationships () {
this.builderStack.push({path: 'relationships'})
return this
}

resetBuilder () {
this.builderStack = []
}
Expand Down
37 changes: 37 additions & 0 deletions src/logger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const Minilog = require('minilog')

class Logger {
static debug (message) {
this.instantiate().debug(message)
}

static disable () {
Minilog.disable()
}

static enable () {
Minilog.enable()
}

static error (message) {
this.instantiate().error(message)
}

static info (message) {
this.instantiate().info(message)
}

static instantiate () {
if (!this.minilog) {
this.minilog = Minilog('devour')
}

return this.minilog
}

static warn (message) {
this.instantiate().warn(message)
}
}

module.exports = Logger
7 changes: 4 additions & 3 deletions src/middleware/json-api/_deserialize.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const _ = require('lodash')
const Logger = require('../../logger')

const cache = new class {
constructor () { this._cache = [] }
Expand Down Expand Up @@ -50,7 +51,7 @@ function resource (item, included, useCache = false) {
}

if (_.isUndefined(attrConfig) && attr !== 'id') {
console.warn(`Resource response contains attribute "${attr}", but it is not present on model config and therefore not deserialized.`)
Logger.warn(`Resource response contains attribute "${attr}", but it is not present on model config and therefore not deserialized.`)
} else {
deserializedModel[attr] = value
}
Expand All @@ -68,9 +69,9 @@ function resource (item, included, useCache = false) {
}

if (_.isUndefined(relConfig)) {
console.warn(`Resource response contains relationship "${rel}", but it is not present on model config and therefore not deserialized.`)
Logger.warn(`Resource response contains relationship "${rel}", but it is not present on model config and therefore not deserialized.`)
} else if (!isRelationship(relConfig)) {
console.warn(`Resource response contains relationship "${rel}", but it is present on model config as a plain attribute.`)
Logger.warn(`Resource response contains relationship "${rel}", but it is present on model config as a plain attribute.`)
} else {
deserializedModel[rel] =
attachRelationsFor.call(this, model, relConfig, item, included, rel)
Expand Down
9 changes: 8 additions & 1 deletion src/middleware/json-api/_serialize.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,14 @@ function resource (modelName, item) {
})

serializedResource.type = typeName
serializedResource.attributes = serializedAttributes

var attrValues = Object.keys(serializedAttributes).map(key => {
return serializedAttributes[key]
})

if (Boolean(attrValues) && attrValues.filter(val => val === undefined).length !== attrValues.length) {
serializedResource.attributes = serializedAttributes
}

if (Object.keys(serializedRelationships).length > 0) {
serializedResource.relationships = serializedRelationships
Expand Down
4 changes: 3 additions & 1 deletion src/middleware/json-api/res-errors.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const Logger = require('../../logger')

function buildErrors (serverErrors) {
if (!serverErrors) {
console.log('Unidentified error')
Logger.error('Unidentified error')
return
} else {
let errors = {}
Expand Down
6 changes: 5 additions & 1 deletion test/api/api-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,10 @@ describe('JsonApi', () => {
expect(jsonApi.one('bar', '1').all('foo').urlFor()).to.eql('http://myapi.com/bars/1/foos/')
})

it('should construct the relationships URL', () => {
expect(jsonApi.one('bar', '1').relationships().all('foo').urlFor()).to.eql('http://myapi.com/bars/1/relationships/foos/')
})

it('should construct resource urls with urlFor', () => {
expect(jsonApi.urlFor({model: 'foo', id: '1'})).to.eql('http://myapi.com/foos/1/')
expect(jsonApi.one('foo', '1').urlFor()).to.eql('http://myapi.com/foos/1/')
Expand Down Expand Up @@ -739,7 +743,7 @@ describe('JsonApi', () => {
}).catch(err => console.log(err))
})

it('should not cache the second requeset', (done) => {
it('should not cache the second request', (done) => {
mockResponse(jsonApi, {
data: {
data: [{
Expand Down
9 changes: 9 additions & 0 deletions test/api/serialize-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@ describe('serialize', () => {
jsonApi = new JsonApi({apiUrl: 'http://myapi.com'})
})

it('should not serialize undefined attributes', () => {
jsonApi.define('product', {
title: '',
about: ''
})
let serializedItem = serialize.resource.call(jsonApi, 'product', {title: undefined, about: undefined})
expect(serializedItem.attributes).to.eql(undefined)
})

it('should serialize resource items', () => {
jsonApi.define('product', {
title: '',
Expand Down

0 comments on commit d1d0a12

Please sign in to comment.