npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

fastify-better-flash

v0.0.2

Published

Flash message plugin for Fastify

Downloads

4

Readme

fastify-better-flash

js-standard-style CI workflow

This plugin is inspired by the @fastify/flash plugin. The main difference is the way it access the messages, using a "dot syntax" and it has a better typescript support. See below for more details.

Supports Fastify versions 4.x

Install

npm i fastify-better-flash @fastify/secure-session
pnpm i fastify-better-flash @fastify/secure-session
yarn add fastify-better-flash @fastify/secure-session

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')

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(require('fastify-better-flash'))

fastify.listen({ port: 3000 })

Usage

In your route handler you can use the flash method to set a flash message. Let's say that we want to have the following messages:

  success: boolean
  validations: {
    errors: Array<{ field: string, message: string }>
  }
  genericError: string

we can set them in the route handler like this:


fastify.get('/', async (request, reply) => {
  request.flash('success', true)
  request.flash('validations.errors', [{ field: 'email', message: 'Email is required' }])
  request.flash('genericError', 'Something went wrong')
})

Then we can consume them like this:


fastify.get('/flash', async (request, reply) => {
  const success = reply.flash('success')
  // Note that we can access the nested object using the dot notation
  const validation = reply.flash('validations')
  const validationErrors = validation ? validation.errors : []
  // it works with arrays too, this will return the first error field property
  const validationErrors = reply.flash('validations.errors.0.field')
  const genericError = reply.flash('genericError')
  return { success, validationErrors, genericError }
})

Typescript

You can define the schema of your flash messages using typescript. By using the fastify-better-flash module you can customize the interface FlashSessionType that be extended to define the schema of your flash messages.


declare module 'fastify-better-flash' {
  export interface FlashSessionType {
    // Define your flash message schema here
    success: boolean
    validations: {
      errors: Array<{ field: string, message: string }>
    }
  }
}

And then in your route handler you can use the flash method to set a flash message.


fastify.get('/', async (request, reply) => {
  // Also the keys will be typed
  request.flash('success', 'This is a success message') // This will not compile

  const validationErrors = reply.flash('validations.errors') // This will have the correct types
  return reply.redirect('/flash')
})

By design the getter method flash will alaways return the type that you have defined or undefined. This is meant to signal the developer that the presence of the object in the flash session storage is not guaranteed on every request.

Acknowledgements

License

Licensed under MIT.