2.0.6 • Published 9 months ago

@prairielearn/flash v2.0.6

Weekly downloads
-
License
-
Repository
github
Last release
9 months ago

@prairielearn/flash

Adds support for flash messages to Express applications.

Usage

req.session must exist. In PrairieLearn, this is provided by the @prairielearn/session package. In other applications, express-session is a common way to implement sessions in Express applications.

import express from 'express';
import session from 'express-session';
import { flashMiddleware } from '@prairielearn/flash';

const app = express();

app.use(
  session({
    // See https://www.npmjs.com/package/express-session for more information
    // about configuring the session middleware.
    secret: 'secret',
    resave: false,
    saveUninitialized: true,
  }),
);
app.use(flashMiddleware());

Now, you can use the flash() function exported by @prairielearn/flash to read/write flash messages.

import { flash } from '@prairielearn/flash';

app.get('/set-flash', (req, res) => {
  flash('notice', 'Your preferences have been updated.');
  flash('success', 'Course created successfully.');
  flash('warning', 'Syncing completed with 5 warnings.');
  flash('error', html`Group must have <em>fewer than 10 members</em>.`);

  res.redirect('/display-flash');
});

app.get('/display-flash', (req, res) => {
  const messages = flash();
  res.json(messages);
});

The flash() function has three behaviors:

  • flash(type: string, message: string | HtmlSafeString): Set a message with the given type.
  • flash(type: string): FlashMessage[]: Get all messages with the given type.
  • flash(types: string[]): FlashMessage[]: Get all messages with any of the given types.
  • flash(): FlashMessage[]: Get all messages.

Once a message is read, it is immediately removed from the persisted list of messages. A message is only removed once it is read; it will be persisted indefinitely if it iw not.

2.0.6

9 months ago

2.0.3

12 months ago

2.0.2

1 year ago

2.0.5

11 months ago

2.0.4

11 months ago

2.0.1

1 year ago

2.0.0

1 year ago

1.1.9

1 year ago

1.1.8

1 year ago

1.1.7

1 year ago

1.1.6

1 year ago

1.1.5

2 years ago

1.1.4

2 years ago

1.1.1

2 years ago

1.0.2

2 years ago

1.1.0

2 years ago

1.0.1

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.1.3

2 years ago

1.0.4

2 years ago

1.1.2

2 years ago

1.0.3

2 years ago

1.0.0

2 years ago