Giter VIP home page Giter VIP logo

koa-better-flash's Introduction

koa-better-flash

build status code coverage code style styled with prettier made with lass license

Flash message middleware for Koa and Passport

Table of Contents

Install

npm:

npm install koa-better-flash

yarn:

yarn add koa-better-flash

Usage

const Koa = require('koa');
const Router = require('koa-router');
const session = require('koa-generic-session');
const flash = require('koa-better-flash');

const app = new Koa();
const router = new Router();

router.post('/', (ctx, next) => {
  // you can also pass an array of messages:
  // ctx.flash('info', [ 'hi', 'hello', 'good morning' ]);
  ctx.flash('info', 'hello world');
  ctx.status = 200;
});

router.get('/', ctx => {
  // to get all messages by type simply call `ctx.flash()`
  ctx.body = ctx.flash('info');
  // outputs: [ 'hello world ']
});

app.keys = [ 'keys' ];
app.use(session());
app.use(flash());
app.use(router.routes());
app.listen();

Contributors

Name Website
Nick Baugh http://niftylettuce.com/

License

MIT © Nick Baugh

koa-better-flash's People

Contributors

niftylettuce avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

koa-better-flash's Issues

Add flash to ctx.state?

Usually you want to show the flash message in a template. With ctx.state being the primary way to share messages with templates, would it be a good idea to ctx.state.flash = ctx.flash()?

I currently do this with a middleware:

exports.addFlashToState = () => {
  return async (ctx, next) => {
    ctx.state.flash = ctx.flash();
    await next();
  };
};

But that only works when you have multiple middlewares to pass through, and not in the case of e.g.:

router.post("profile", authenticated(), async ctx => {
  ctx.flash("success", "Profile successfully updated");
  await ctx.render("profile", {});
});

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.