0.1.0-alpha.2 • Published 4 years ago

snuji v0.1.0-alpha.2

Weekly downloads
1
License
MIT
Repository
-
Last release
4 years ago

snuji npm Build Status

snuji is a context-based middleware implementation.

Installation

yarn add snuji

or

npm install --save snuji

Usage

import compose, { Middlware, Handler } from "snuji";

// Define your shared middleware context
type Context = { value: number };

// Define your middleware; each can modify the context before,
// and after, the next middleware in the chain resolves.
const middleware: Middleware<Context>[] = [
	async (ctx, next) => {
		console.debug("Middleware 1 Start");
		ctx.value += 1;
		await next();
		console.debug("Middleware 1 End");
	},
	async (ctx, next) => {
		console.debug("Middleware 2 Start");
		ctx.value += 1;
		await next();
		console.debug("Middleware 2 End");
	}
];

// In an application create your context and run your
// middleware stack, and pass the context to your handler.

// In your application…
(async () => {
	// Instantiate your context
	const ctx: Context = { value: 0 };

	// Compose your middleware
	const stack = compose<Context>(...middleware);

	// Define your handler to be sandwiched by your middleware
	const handler: Handler<Context> = ctx => console.log("Value: " + ctx.value);

	// Pass your context through the middleware and handler stack.
	await stack(ctx, handler);

	// The mutated context is still available after the stack.
})();

// Output:
// Middleware 1 Start
// Middleware 2 Start
// Value: 2
// Middleware 2 End
// Middleware 1 End

Prior Art

This package is based on the middleware implementation included in koa.

What does "snuji" mean?

It's a Lojban gismu.

0.1.0-alpha.2

4 years ago

0.1.0-alpha.1

4 years ago