-
Notifications
You must be signed in to change notification settings - Fork 0
/
flash.js
51 lines (42 loc) · 1.37 KB
/
flash.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
const utils = require('./utils')
module.exports = function () {
return {
/**
* @name flash
* @description Set a flash message
* @param {string} scope
* @param {any} value
*/
setter (scope, value) {
if (!this.session) {
throw new Error('Session not found, make sure to register @fastify/secure-session')
}
if (typeof scope !== 'string') {
throw new Error('Expected scope to be a string')
}
// in this way we are a drop in replacement for @fastify/flash
let currentSession = this.session.get('flash')
if (!currentSession) {
currentSession = {}
}
utils.attachThingToObjectGivenPath(scope, currentSession, value)
this.session.set('flash', currentSession)
},
getter (scope) {
if (!this.request.session) {
throw new Error('Session not found, make sure to register @fastify/secure-session')
}
if (typeof scope !== 'string') {
throw new Error('Expected scope to be a string')
}
const currentSession = this.request.session.get('flash')
if (!currentSession) {
return undefined
}
const output = utils.retriveThingFromObjectGivenPath(scope, currentSession)
utils.deleteThingFromObjectGivenPath(scope, currentSession)
this.request.session.set('flash', currentSession)
return output
}
}
}