Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/laola2013/koa-state-helper
State middleware for koa
https://github.com/laola2013/koa-state-helper
helper koa2 middleware state
Last synced: 2 days ago
JSON representation
State middleware for koa
- Host: GitHub
- URL: https://github.com/laola2013/koa-state-helper
- Owner: laola2013
- License: mit
- Created: 2020-05-16T01:17:07.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2020-05-22T11:12:37.000Z (over 4 years ago)
- Last Synced: 2024-04-25T16:02:57.774Z (9 months ago)
- Topics: helper, koa2, middleware, state
- Language: JavaScript
- Homepage:
- Size: 8.79 KB
- Stars: 9
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: History.md
- License: LICENSE
Awesome Lists containing this project
README
# koa-state-helper
State middleware for koa
## Installation
```
npm install koa-state-helper
```## Example
```
// app.jsconst Koa = require('koa');
const helper = require('koa-state-helper');const app = new Koa();
app.use(helper());
app.listen(3000);
// controller.js
export async function Home(ctx) {
const isMobile = ctx.state.isMobile(ctx);
await ctx.render('home/home.njk', {
isMobile
});
}```
## Extension
```
// app.jsconst Koa = require('koa');
const helper = require('koa-state-helper');const app = new Koa();
app.use(helper({
isMobile: (ctx) => { // ...do something },
isOtherVertify: (ctx) => { // ...do something }
}));app.listen(3000);
```
## Options```
/**
* @param [object] options
* [Function or Boolean(false)] options[key]:
* - [Function] isMobile: default is valid
* - [Function] isWechat: default is valid
* - [Function] isDevEnv: default is valid
* - [Function] header: default is valid
* all of the above are built-in methods, you can pass in false to invalid or pass in new method to rewrite
*/
```* DefaultOptions‘s value is `Function`,developer can pass in new method to rewrite,
If you pass in a value(eg. `""`、`null` or `undefined`) parsed by `Boolean` method as false, it will be considered that you don’t want to use it and
it will not appear in the state
* Allows you to extend the state, value must be `Funciton`,
whether return value depends on your application scenario