Added the code for the express-flasher; added ESLint; added Woodpecker CI config
This commit is contained in:
parent
038a0c1a32
commit
db9ff7ef05
15
.eslintrc.json
Normal file
15
.eslintrc.json
Normal file
@ -0,0 +1,15 @@
|
||||
{
|
||||
"env": {
|
||||
"es2021": true,
|
||||
"node": true
|
||||
},
|
||||
"extends": "google",
|
||||
"overrides": [
|
||||
],
|
||||
"parserOptions": {
|
||||
"ecmaVersion": "latest",
|
||||
"sourceType": "module"
|
||||
},
|
||||
"rules": {
|
||||
}
|
||||
}
|
20
.woodpecker.yml
Normal file
20
.woodpecker.yml
Normal file
@ -0,0 +1,20 @@
|
||||
pipeline:
|
||||
setup:
|
||||
image: node:18
|
||||
commands:
|
||||
- npm install
|
||||
|
||||
lint:
|
||||
image: node:18
|
||||
commands:
|
||||
- npm run lint
|
||||
|
||||
gitea_release:
|
||||
image: plugins/gitea-release
|
||||
settings:
|
||||
api_key:
|
||||
from_secret: gitea_api_key
|
||||
base_url: https://git.metaunix.net
|
||||
title: "${CI_COMMIT_TAG}"
|
||||
when:
|
||||
event: tag
|
2
LICENSE
2
LICENSE
@ -1,4 +1,4 @@
|
||||
Copyright (c) <year> <owner>
|
||||
Copyright (c) 2022 Bit Goblin
|
||||
|
||||
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
||||
|
||||
|
46
README.md
46
README.md
@ -1,3 +1,49 @@
|
||||
# express-flasher
|
||||
|
||||
Express.js middleware to set and retrieve session flash messages
|
||||
|
||||
## Requirements
|
||||
|
||||
For the `express-flasher` module to work, you must first have the `express-session` module enabled in your express app. Please refer to [the express-session documentation](https://www.npmjs.com/package/express-session) on how to set it up.
|
||||
|
||||
## Installation
|
||||
|
||||
Installating this module is super easy with NPM:
|
||||
|
||||
`npm install --save express-flasher`
|
||||
|
||||
## Usage
|
||||
|
||||
After the module is installed, you can set up the express-flasher by registering the middleware (both middlewares are important!):
|
||||
|
||||
```javascript
|
||||
const flasher = require('express-flasher');
|
||||
...
|
||||
app.use(flasher.flash()); // enables the req.flash() method for setting flash messages
|
||||
app.use(flasher.flashRead()); // reads flash messages and adds it to the view
|
||||
```
|
||||
|
||||
Now you can set messages in your routes:
|
||||
|
||||
```javascript
|
||||
exports.index = function(req, res) {
|
||||
flash('info', 'This is a flash!');
|
||||
res.render('index.twig');
|
||||
};
|
||||
```
|
||||
|
||||
And finally, you can read the flash messages in your views:
|
||||
|
||||
```twig
|
||||
...
|
||||
{% if flash %}
|
||||
<div class="flash-message {{ flash.type }}">
|
||||
<p>{{ flash.msg }}</p>
|
||||
</div>
|
||||
{% endif %}
|
||||
...
|
||||
```
|
||||
|
||||
## License
|
||||
|
||||
This project is licensed under the BSD 2-Clause license.
|
||||
|
40
index.js
Normal file
40
index.js
Normal file
@ -0,0 +1,40 @@
|
||||
// used to set a flash message for next request
|
||||
exports.flash = function() {
|
||||
return function(req, res, next) {
|
||||
if (req.flash) {
|
||||
return next();
|
||||
}
|
||||
|
||||
req.flash = _flash;
|
||||
next();
|
||||
};
|
||||
};
|
||||
|
||||
// used on new requests to check for flash messages stored in session
|
||||
exports.flashRead = function() {
|
||||
return function(req, res, next) {
|
||||
if (typeof req.session.flash !== 'undefined') {
|
||||
res.locals.flash = req.session.flash;
|
||||
}
|
||||
|
||||
next();
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
* Sets a session-based flash message.
|
||||
*
|
||||
* @this req
|
||||
* @param {string} type - user-defined type of flash message
|
||||
* @param {string} msg - message to be displayed to the user
|
||||
*/
|
||||
function _flash(type, msg) {
|
||||
// verify that express-session has been enabled
|
||||
if (this.session === undefined) throw Error('req.flash() requires sessions');
|
||||
|
||||
// set session properties
|
||||
this.session.flash = {
|
||||
type: type,
|
||||
msg: msg,
|
||||
};
|
||||
}
|
1937
package-lock.json
generated
Normal file
1937
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
26
package.json
Normal file
26
package.json
Normal file
@ -0,0 +1,26 @@
|
||||
{
|
||||
"name": "express-flasher",
|
||||
"version": "1.0.0",
|
||||
"description": "Express.js middleware to easily set and retrieve session flash messages.",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"lint": "eslint index.js",
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://git.metaunix.net/BitGoblin/express-flasher"
|
||||
},
|
||||
"keywords": [
|
||||
"express",
|
||||
"expressjs",
|
||||
"flash",
|
||||
"session"
|
||||
],
|
||||
"author": "Gregory Ballantine <gballantine@bitgoblin.tech>",
|
||||
"license": "BSD-2-Clause",
|
||||
"devDependencies": {
|
||||
"eslint": "^8.26.0",
|
||||
"eslint-config-google": "^0.14.0"
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user