diff --git a/CHANGELOG.md b/CHANGELOG.md index 3ec6abd..f9e07e2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file. ## [Unreleased] +## [0.0.7] - 2018-02-10 + +### Fixed +- If an include function does not return an instance of `ResourceAbstrace`, the returned value is used without transformation. + + ## [0.0.6] - 2018-02-10 This release adds optional includes that can be requested on a per-transform basis. diff --git a/package.json b/package.json index e727250..b55fe94 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "adonis-bumblebee", - "version": "0.0.6", + "version": "0.0.7", "description": "Api Transformer Provider for AdonisJs Framework", "main": "index.js", "scripts": { diff --git a/src/Bumblebee/Resources/index.js b/src/Bumblebee/Resources/index.js index a6c0ee9..8ee1a7b 100644 --- a/src/Bumblebee/Resources/index.js +++ b/src/Bumblebee/Resources/index.js @@ -1,9 +1,11 @@ const Collection = require('./Collection') const Item = require('./Item') const Null = require('./Null') +const ResourceAbstract = require('./ResourceAbstract') module.exports = { Collection, Item, - Null + Null, + ResourceAbstract } diff --git a/src/Bumblebee/TransformerAbstract.js b/src/Bumblebee/TransformerAbstract.js index 926556a..8795dce 100644 --- a/src/Bumblebee/TransformerAbstract.js +++ b/src/Bumblebee/TransformerAbstract.js @@ -48,7 +48,11 @@ class TransformerAbstract { for (let include of this.figureOutWhichIncludes(parentScope)) { let resource = await this.callIncludeFunction(include, data) - includeData[include] = await this.createChildScopeFor(parentScope, resource, include).toArray() + if (resource instanceof Resources.ResourceAbstract) { + includeData[include] = await this.createChildScopeFor(parentScope, resource, include).toArray() + } else { + includeData[include] = resource + } } return includeData diff --git a/test/includes.spec.js b/test/includes.spec.js index 22c858c..ab77a21 100644 --- a/test/includes.spec.js +++ b/test/includes.spec.js @@ -19,7 +19,8 @@ class Book2Transformer extends TransformerAbstract { availableInclude () { return [ 'author', - 'characters' + 'characters', + 'school' ] } @@ -35,6 +36,9 @@ class Book2Transformer extends TransformerAbstract { includeCharacters (book) { return this.collection(book.characters, Book2CharacterTransformer) } + includeSchool (book) { + return 'Hogwarts' + } } class Book2CharacterTransformer extends TransformerAbstract { @@ -142,4 +146,17 @@ test.group('Includes can be an array or a string', () => { assert.deepEqual(transformed, {title: 'Harry Potter and the Deathly Hallows'}) }) + + test('an include function can return a object to be merged', async (assert) => { + let transformed = await Bumblebee.create() + .include(['school']) + .item(data) + .transformWith(Book2Transformer) + .toArray() + + assert.deepEqual(transformed, { + title: 'Harry Potter and the Deathly Hallows', + school: 'Hogwarts' + }) + }) })