Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/fastify/fastify-flash
Flash message plugin for Fastify
https://github.com/fastify/fastify-flash
fastify fastify-plugin
Last synced: about 24 hours ago
JSON representation
Flash message plugin for Fastify
- Host: GitHub
- URL: https://github.com/fastify/fastify-flash
- Owner: fastify
- License: mit
- Created: 2019-03-02T18:25:33.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2024-12-05T14:15:35.000Z (17 days ago)
- Last Synced: 2024-12-14T20:04:35.396Z (8 days ago)
- Topics: fastify, fastify-plugin
- Language: TypeScript
- Homepage:
- Size: 143 KB
- Stars: 29
- Watchers: 17
- Forks: 14
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# @fastify/flash
[![CI](https://github.com/fastify/fastify-flash/actions/workflows/ci.yml/badge.svg?branch=master)](https://github.com/fastify/fastify-flash/actions/workflows/ci.yml)
[![NPM version](https://img.shields.io/npm/v/@fastify/flash.svg?style=flat)](https://www.npmjs.com/package/@fastify/flash)
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](https://standardjs.com/)The flash is a special area of the session used for storing messages. Messages are written to the flash and cleared after being displayed to the user. The flash is typically used in combination with redirects, ensuring that the message is available to the next page that is to be rendered.
This plugin is inspired by [connect-flash](https://github.com/jaredhanson/connect-flash).
## Install
`npm i @fastify/flash`## Usage
Flash messages are stored in the session. First, we need to register the session plugin: [@fastify/secure-session](https://www.npmjs.com/package/@fastify/secure-session).``` javascript
const fastify = require('fastify')()
const fastifySession = require('@fastify/secure-session')
const fastifyFlash = require('@fastify/flash')fastify.register(fastifySession, {
// adapt this to point to the directory where secret-key is located
key: fs.readFileSync(path.join(__dirname, 'secret-key')),
cookie: {
// options from setCookie, see https://github.com/fastify/fastify-cookie
}
})
fastify.register(fastifyFlash)fastify.get('/test', (req, reply) => {
req.flash('warning', ['username required', 'password required'])const warning = reply.flash('warning')
reply.send({ warning }) // {"warning":["username required","password required"]}
})
```### Note on session plugin
`@fastify/secure-session` can be replaced by any session plugin as long as it:1. registers via the onRequest hook
2. supports req.session as the session store## API
### Set a flash massage(s)
Signature
``` typescript
req.flash(type: string, ...message: string[] | [string[]]): number
```
It can be called in three different ways:
- `req.flash('info', 'Welcome back')`
- `req.flash('warning', ['username required', 'password required'])`
- `req.flash('info', 'Hello %s', 'Jared') // will use util.format to format the string``req.flash` returns the number of messages store with provided type.
### Get a flash message(s)
signature
``` typescript
reply.flash(type?: string): { [k: string]: undefined | string[] } | string[]
```
It can be called in two different ways:
- `reply.flash() // returns all messages as object { [k: string]: undefined | string[] }`
- `reply.flash('info') // returns array of messages that are stored with provided type`## License
MIT