Skip to content

Commit

Permalink
Merge branch 'release/1.0.8'
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Aug 5, 2017
2 parents b442dbb + 27a5016 commit 0a59723
Show file tree
Hide file tree
Showing 11 changed files with 588 additions and 3 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
<a name="1.0.8"></a>
## [1.0.8](https://github.com/adonisjs/adonis-session/compare/v1.0.7...v1.0.8) (2017-08-05)


### Features

* **session:** add flash messages related view globals ([4347a1f](https://github.com/adonisjs/adonis-session/commit/4347a1f))
* **session:** add memory driver ([e90b1a7](https://github.com/adonisjs/adonis-session/commit/e90b1a7))
* **session:** add support for flash messages ([f0a536e](https://github.com/adonisjs/adonis-session/commit/f0a536e))



<a name="1.0.7"></a>
## [1.0.7](https://github.com/adonisjs/adonis-session/compare/v1.0.6...v1.0.7) (2017-08-02)

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": "@adonisjs/session",
"version": "1.0.7",
"version": "1.0.8",
"description": "This repo is the official session provider for Adonisjs apps. It supports multiple drivers to store session data.",
"main": "providers/SessionProvider",
"directories": {
Expand Down
6 changes: 6 additions & 0 deletions providers/SessionProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ class SessionProvider extends ServiceProvider {
this._registerProvider()
this._registerMiddleware()
}

boot () {
try {
require('../src/Session/FlashGlobals')(this.app.use('Adonis/Src/View'))
} catch (error) {}
}
}

module.exports = SessionProvider
55 changes: 55 additions & 0 deletions src/Session/Drivers/Memory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
'use strict'

/*
* adonis-session
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

const memoryStore = {}

/**
* Memory driver to save session values inside
* memory.
*
* NOTE: Should be used in testing only
*
* @class Memory
* @constructor
*/
class Memory {
/**
* Write session values to memory
*
* @method write
*
* @param {String} sessionId
* @param {Object} values
*
* @return {void}
*/
write (sessionId, values) {
memoryStore[sessionId] = values
}

/**
* Returns value for a given session id
*
* @method read
*
* @param {String} sessionId
*
* @return {String}
*/
read (sessionId) {
return memoryStore[sessionId]
}

touch () {}
}

module.exports = exports = Memory
exports.memoryStore = memoryStore
3 changes: 2 additions & 1 deletion src/Session/Drivers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@
module.exports = {
cookie: require('./Cookie'),
redis: require('./Redis'),
file: require('./File')
file: require('./File'),
memory: require('./Memory')
}
49 changes: 49 additions & 0 deletions src/Session/FlashGlobals.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
'use strict'

/*
* adonis-session
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

const _ = require('lodash')

module.exports = function (View) {
View.global('old', function (key, defaultValue) {
return _.get(this.flashMessages, key, defaultValue)
})

View.global('errors', function () {
return this.$globals.old('errors')
})

View.global('hasErrors', function (key) {
return !!_.size(this.$globals.errors())
})

View.global('getErrorFor', function (key) {
const errors = this.$globals.errors()

/**
* If errors is an object and not an array
* then return the value for the key
*/
if (_.isPlainObject(errors)) {
return _.get(errors, key)
}

/**
* Otherwise look inside array assuming validation
* error structure
*/
const errorMessage = _.find(errors, (error) => error.field === key)
return errorMessage ? errorMessage.message : null
})

View.global('hasErrorFor', function (key) {
return !!this.$globals.error(key)
})
}
10 changes: 10 additions & 0 deletions src/Session/Middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,16 @@ class SessionMiddleware {
*/
await ctx.session.instantiate()

/**
* Sharing flash messages with the view when view
* exists in the session and there is share
* method on it too.
*/
const flashMessages = ctx.session.pull('__flash__', {})
if (ctx.view && typeof (ctx.view.share) === 'function') {
ctx.view.share({ flashMessages })
}

/**
* Move the chain
*/
Expand Down
79 changes: 79 additions & 0 deletions src/Session/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
* file that was distributed with this source code.
*/

const _ = require('lodash')
const uuid = require('uuid')
const debug = require('debug')('adonis:session')
const GE = require('@adonisjs/generic-exceptions')
const Store = require('./Store')
const util = require('../../lib/util')

Expand Down Expand Up @@ -193,6 +195,83 @@ class Session {
clear () {
this._store.clear()
}

/* istanbul ignore next */
/**
* Flash entire request object to the session
*
* @method flashAll
*
* @chainable
*/
flashAll () {
return this.flash(this._request.all())
}

/* istanbul ignore next */
/**
* Flash only selected fields from request data to
* the session
*
* @method flashOnly
*
* @param {...Spread} fields
*
* @chainable
*/
flashOnly (...fields) {
return this.flash(this._request.only(...fields))
}

/* istanbul ignore next */
/**
* Flash request data to the session except
* certain fields
*
* @method flashExcept
*
* @param {...Spread} fields
*
* @chainable
*/
flashExcept (...fields) {
return this.flash(this._request.except(...fields))
}

/**
* Flash errors to the session
*
* @method withErrors
*
* @param {Object} errors
*
* @chainable
*/
withErrors (errors) {
return this.flash({ errors })
}

/**
* Flash data to the session
*
* @method flash
*
* @param {Object} data
*
* @chainable
*/
flash (data) {
if (!_.isPlainObject(data)) {
throw GE.InvalidArgumentException.invalidParameter('Flash data should be an object', data)
}
const flashMessage = this.get('__flash__', null)
if (!flashMessage) {
this.put('__flash__', data)
} else {
_.merge(flashMessage, data)
}
return this
}
}

module.exports = Session
Loading

0 comments on commit 0a59723

Please sign in to comment.