Skip to content

Commit

Permalink
allow the transform function to be a promise
Browse files Browse the repository at this point in the history
  • Loading branch information
Ralph Huwiler committed Feb 8, 2018
1 parent d781a16 commit b10c0ba
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 39 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ All notable changes to this project will be documented in this file.

## [Unreleased]

## [0.0.4] - 2018-02-08

### Changed
- Allow the transform function to be a promise


## [0.0.3] - 2018-02-08
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "adonis-bumblebee",
"version": "0.0.3",
"version": "0.0.4",
"description": "Api Transformer Provider for AdonisJs Framework",
"main": "index.js",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion src/Bumblebee/Scope.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class Scope {
async _item (data, transformer) {
let transformerInstance = this._getTransformerInstance(transformer)

let transformed = transformerInstance.transform(await data, this._ctx)
let transformed = await transformerInstance.transform(await data, this._ctx)

let includeData = await transformerInstance.processIncludedResources(this, await data)

Expand Down
4 changes: 2 additions & 2 deletions src/Bumblebee/TransformerAbstract.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class TransformerAbstract {
const Scope = require('./Scope')

for (let include of this.defaultInclude()) {
let resource = this.callIncludeFunction(include, data)
let resource = await this.callIncludeFunction(include, data)

let childScope = new Scope(parentScope._manager, resource, parentScope._ctx)

Expand All @@ -51,7 +51,7 @@ class TransformerAbstract {
return includeData
}

callIncludeFunction (include, data) {
async callIncludeFunction (include, data) {
let includeName = `include${include.charAt(0).toUpperCase()}${include.slice(1)}`

return this[includeName](data)
Expand Down
35 changes: 0 additions & 35 deletions test/promise-data.spec.js

This file was deleted.

82 changes: 82 additions & 0 deletions test/promise.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
'use strict'

/**
* adonis-bumblebee
*
* (c) Ralph Huwiler <ralph@huwiler.rocks>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

const test = require('japa')
const { ioc } = require('@adonisjs/fold')

const setup = require('./setup')
const TransformerAbstract = require('../src/Bumblebee/TransformerAbstract')

class BookTransformer extends TransformerAbstract {
defaultInclude () {
return [
'author'
]
}

async transform (book) {
return {
id: book.id,
title: book.title,
year: book.yr
}
}

async includeAuthor (book) {
return this.item(book.author, author => ({name: author.name}))
}
}

test.group('Promise', (group) => {
group.before(async () => {
await setup()
})

test('data can be a promise and will resolve before transforming', async (assert) => {
const Context = ioc.use('Adonis/Src/HttpContext')
const {transform} = new Context()

let data = new Promise((resolve, reject) => {
setTimeout(resolve, 1, {item_id: 3})
})

let transformed = await transform
.item(data, model => ({id: model.item_id}))

assert.equal(transformed.id, 3)
})

test('the transform function can be a promise', async (assert) => {
const Context = ioc.use('Adonis/Src/HttpContext')
const {transform} = new Context()

let data = {
id: 1,
title: 'A Game of Thrones',
yr: 1996,
author: {
name: 'George R. R. Martin'
}
}

let transformed = await transform
.item(data, BookTransformer)

assert.deepEqual(transformed, {
id: 1,
title: 'A Game of Thrones',
year: 1996,
author: {
name: 'George R. R. Martin'
}
})
})
})

0 comments on commit b10c0ba

Please sign in to comment.