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

modern-flash

v1.3.2

Published

Modern TypeScript rewrite of express-flash, allowing data such as error/success notifications to be temporarily stored in the session.

Downloads

17

Readme

Modern TypeScript rewrite of express-flash, allowing data such as error/success notifications to be temporarily stored in the session.

Installation

npm install --save modern-flash
# or
yarn add modern-flash
# or
pnpm add modern-flash

Usage

The default export is flash(). This is an Express middleware method, and it must be mounted after mounting session middleware, such as express-session or next-session. Specifically, when using next-session, make sure to install middleware that sets req.session.

Once mounted, request handlers will have a flash() function:

Setting values in the flash store

Simply call req.flash() with two arguments. The first is a key to be used when inserting the value into the flashes object of the session. The second is either a string, or an array of strings.

If a key already exists with this name, the value(s) will be appended to the existing array.

// Just one value:
req.flash("error", "The florpotron could not be configured. Please contact customer service.");

// Or multiple:
req.flash("error", [
	"I’m sorry, Dave. I’m afraid I can’t do that.",
	"Unshrink you? That would require some sort of a rebigulator!"
]);

// How about another key:
req.flash("success", "Your order has been placed!");

Getting all flashes from the store

Calling req.flash() with no arguments will return the entire flashes object from the session.

let flashes = req.flash();
res.json(flashes);

The above sample would produce the following (prettified) output:

{
	"error": [
		"The florpotron could not be configured. Please contact customer service.",
		"I’m sorry, Dave. I’m afraid I can’t do that.",
		"Unshrink you? That would require some sort of a rebigulator!"
	],

	"success": [
		"Your order has been placed!"
	]
}

In addition, this will empty the flashes object in the session, as you’re signalling that you have now sent these messages to the client, hence they are no longer needed.

If you don’t care about the keys and just want the values, you can flatten them into a single array using Array.prototype.flat():

let flashes = Object.values(req.flash()).flat();

You might also find it useful to use destructuring syntax if you know exactly which keys you’re interested in. However, keep in mind that all flashes will still be cleared from the session.

let { error, somethingElse } = req.flash();

Getting specific flash keys from the store

As req.flash() with no arguments will clear the entire store, you may find it useful to only retrieve specific keys you’re interested in.

let errors = req.flash("error");

Calling this method will remove the specified key from the store.

Getting the flashes within templating engines

A flashes property is added to the res.locals object, for convenience with templating engines that pass this through to your templates.

Accessing the res.locals.flashes property is equivalent to calling req.flash() – that is, you will clear the flash store by doing so. However, unlike req.flash(), once accessed once, subsequent accesses will return a cached value. This allows you to somewhat simplify your templating logic.

Here’s an example of how it could be used from a Pug template:

if Object.keys(flashes).length > 0
	.alert(role="alert")
		for items in Object.values(flashes)
			for message in items
				p= message

Note that res.locals.flashes is read-only. Changes made to this object will not have any effect on the flashes object in the session.

Complete example

import express from "express";
import session from "express-session";
import flash from "modern-flash";

const app = express();

// Set up a session. With express-session:
app.use(session({
	// Session config goes here
}));

// …or with next-session:
let getSession = nextSession({
	// Session config goes here
});

// Let modern-flash set itself up in the session:
app.use(flash());

// An example of how you could use it:
app.get("/", async (req, res, next) => {
	// If you use next-session, set the session object before accessing flash():
	req.session = await getSession(req, res);

	let error = req.flash("error");

	// Pretend you’re using a more elaborate templating system than this.
	// This is just for demonstration 🙂
	res.set("Content-Type", "text/html");
	res.end(`
		${error ? `<p>Error: ${error}</p>` : ""}
		<form action="" method="post">
			<button type="submit">Do something</button>
		</form>
	`);
});

app.post("/", (req, res, next) => {
	// Do some hardcore processing magic here…
	// …
	// Oops, something went wrong! Let’s tell the user:
	req.flash("error", "The florpotron could not be configured. Please contact customer service.");
	res.redirect("/");
});

app.listen(3000);

Credits

Developed by Chariz:

License

Licensed under the Apache License, version 2.0. Refer to LICENSE.md.