Code Monkey home page Code Monkey logo

egg-plugin-context's Introduction

egg-plugin-context

An egg plugin for handle context

NPM version Codacy Badge Known Vulnerabilities npm download License

Sonar

Install

# use pnpm
$ pnpm install egg-plugin-context

# use npm
$ npm install egg-plugin-context --save

# use yarn
$ yarn add egg-plugin-context

Usage

Enable context plugin

// {app_root}/config/plugin.js
exports.withContext = {
  enable: true,
  package: 'egg-plugin-context'
}

1. Simple use

// {app_root}/router.js

module.exports = app => {
  const { router, controller, middleware } = app

  router.post('/api/test', middleware.withContext({ token: 100 }), controller.test)
}

// ctx.context.token === 100

2. Arguments passed into function

// {app_root}/router.js

module.exports = app => {
  const { router, controller, middleware } = app

  router.post(
    '/api/test',
    middleware.withContext(ctx => ({ test: 100 })),
    controller.test
  )
}

// ctx.context.test === 100

3. Arguments passed into async function

// {app_root}/router.js

module.exports = app => {
  const { router, controller, middleware } = app

  router.post(
    '/api/test',
    middleware.withContext(async ctx => Promise.resolve({ test: 100 })),
    controller.test
  )
}

// ctx.context.test === 100

Options

contextName

The contextName determines where to get the Context, like: ctx.context

// {app_root}/router.js

module.exports = app => {
  const { router, controller, middleware } = app

  router.post(
    '/api/test',
    middleware.withContext({ token: 100 }, { contextName: 'contextData' }),
    controller.test
  )
}

// ctx.contextData.token === 100

Change logs

Change logs

Questions & Suggestions

Please open an issue here.

License

MIT

egg-plugin-context's People

Contributors

saqqdy avatar dependabot[bot] avatar

Stargazers

Roman avatar  avatar  avatar

Watchers

 avatar  avatar

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.