cat-middleware

0.2.8 • Public • Published

Cat-middleware NPM version Build Status

Middleware of koa server. See more information in code and here.

Middleware

koa-react-render

Render html with react and nunjucks. If you need to use radium. You will get radiumConfig as props to your component.

Install
  • nunjucks
  • react
  • react-dom
Arguments
  • component: This component is used to render to the template.
  • options(default: {})
    • root(default: './views'): This is the folder of the templates which is for nunjucks.
    • renderKey(default: 'content'): This is the variable in template which will be replace with component.
    • template(default: 'template.html'): This is the template of the html.
    • You can add other variables for your template.
Example
import reactRender from 'cat-middleware/lib/koa-react-render';
 
...
app.use(reactRender(
  <div>render react</div>
));
...

koa-i18n

Read json file and use it as string. It will read the cookies from client. You can set i18n in cookies to change the language.

Arguments
  • options(default: {})
    • root(default: './i18n'): This is the folder of the files.
    • i18n(default: 'en-us'): This is the default language for this middleware.
Example
import i18n from 'cat-middleware/lib/koa-i18n';
 
...
app.use(i18n());
...
// Then you can use `i18n` in your `ctx`.

koa-authentication

Use to check the authentication. You must have user in ctx.state and authentication in user. You can use koa-passport to do this.

koa-authentication.configure
  • Arguments
    • authentication_levels(default: {}): This is used to check the authentication. For example, it will be like {none: 0, user: 1, superuser: 999}.
    • env(default: true): If this is false, this middleware will not check the authentication. Remeber to use this with process.env.NODE_ENV.
koa-authentication.set
  • Arguments
    • authentication(default: 'none'): Use to set authentication in url.
    • redirect(default: '/'): Redirect to the url when user dose not pass the authentication.
Example
import authentication from 'cat-middleware/lib/koa-authentication';
 
...
app.use(authentication.configure({
  none: 0,
  user: 1
}, process.env.NODE_ENV === 'production'));
 
...
router.get(
  '/authentication/',
  authentication.set('user', '/authentication/fail/'), ctx => {
    // do something here
  }
);
...

koa-relay-data

Use to get the data from fetch with react-relay.

Install
  • babel-polyfill
  • fetch-everywhere
  • react-relay
Arguments
  • link
  • query
  • variables
Example
import relayData from 'cat-middleware/lib/koa-relay-data';
 
...
app.use(relayData(
  link, graphql`
    query relayData {
      data {
        key
      }
    }
  `
));
...
// Then you can get the data `graphql_data` in your `ctx`.

License

MIT © hsuting

Readme

Keywords

Package Sidebar

Install

npm i cat-middleware

Weekly Downloads

26

Version

0.2.8

License

MIT

Last publish

Collaborators

  • hsuting