Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/bjufre/koa-setincontext
Middleware for Koa 2, that sets the state of the context i every request, with the values you pass.
https://github.com/bjufre/koa-setincontext
Last synced: 3 months ago
JSON representation
Middleware for Koa 2, that sets the state of the context i every request, with the values you pass.
- Host: GitHub
- URL: https://github.com/bjufre/koa-setincontext
- Owner: bjufre
- Created: 2016-12-18T13:03:00.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2017-09-23T13:55:46.000Z (almost 7 years ago)
- Last Synced: 2024-03-20T00:11:57.651Z (3 months ago)
- Language: JavaScript
- Size: 9.77 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Lists
- koa-awesome - koa-setincontext - Flexible middleware for Koa 2 that lets you set properties to the ctx.state for every request. (Packages / Middleware)
README
# koa-setincontext
Flexible middleware for Koa 2 that lets you set properties to the `ctx.state` for every request.This is particulary useful when you have dynamic properties which value may change on every request.
## Installation
`$ npm install koa-setincontext --save`
## Examples
#### Simple usage:
```javascript
import Koa from 'koa';
import setInContext from 'koa-setincontext';const app = new Koa();
const events = ['one', 'two', 'three'];app.use(setInContext('events', events));
app.use(async (ctx, next) => {
console.log(ctx.state.events) // => ['one', 'two', 'three']
return await next();
})
```#### With an object
```javascript
import Koa from 'koa';
import setInContext from 'koa-setincontext';const app = new Koa();
const events = ['one', 'two', 'three'];app.use(setInContext({
name: 'events',
value: events
}));app.use(async (ctx, next) => {
console.log(ctx.state.events) // => ['one', 'two', 'three']
return await next();
})
```#### With an array of objects:
```javascript
import Koa from 'koa';
import setInContext from 'koa-setincontext';const app = new Koa();
const events = ['one', 'two', 'three'];app.use(setInContext([
{
name: 'another',
value: 'awesome'
},
{
name: 'events',
value: events
}
]));app.use(async (ctx, next) => {
console.log(ctx.state.another) // => 'awesome'
console.log(ctx.state.events) // => ['one', 'two', 'three']
return await next();
})
```