@fastify/flash
Version:
Flash message plugin for fastify.
50 lines (49 loc) • 1.94 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.flashFactory = flashFactory;
const util_1 = require("util");
function flashFactory() {
return {
request(type, ...message) {
if (!this.session) {
throw new Error('Session not found');
}
let currentSession = this.session.get('flash');
if (!currentSession) {
currentSession = {};
this.session.set('flash', currentSession);
}
if (message.length === 0) {
throw new Error('Provide a message to flash.');
}
if (Array.isArray(message[0])) {
for (let i = 0; i < message[0].length; i++) {
currentSession = Object.assign(Object.assign({}, currentSession), { [type]: (currentSession[type] || []).concat(message[0][i]) });
}
}
else {
currentSession = Object.assign(Object.assign({}, currentSession), { [type]: (currentSession[type] || []).concat(message.length > 1 ? util_1.format.apply(undefined, message) : message[0]) });
}
this.session.set('flash', currentSession);
return this.session.get('flash')[type].length;
},
reply(type) {
if (!this.request.session) {
throw new Error('Session not found');
}
if (!type) {
const allMessages = this.request.session.get('flash');
this.request.session.set('flash', {});
return allMessages;
}
let data = this.request.session.get('flash');
if (!data) {
data = {};
}
const messages = data[type];
delete data[type];
this.request.session.set('flash', data);
return messages || [];
},
};
}