Giter Site home page Giter Site logo

fastify-flash's Introduction

@fastify/flash

CI NPM version js-standard-style

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.

Install

npm i @fastify/flash

Usage

Flash messages are stored in the session. First, we need to register the session plugin: @fastify/secure-session.

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

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

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

fastify-flash's People

Contributors

fox1t avatar dependabot[bot] avatar fdawgs avatar mcollina avatar dependabot-preview[bot] avatar darkgl0w avatar jsumners avatar uzlopak avatar zekth avatar cristianossd avatar is2ei avatar eomm avatar frikille avatar rafaelgss avatar salmanm avatar azure-pipelines[bot] avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.