Skip to content

Latest commit

 

History

History
106 lines (81 loc) · 2.75 KB

File metadata and controls

106 lines (81 loc) · 2.75 KB

egg-tiaozhan-controller-extension

NPM version build status Test coverage David deps Known Vulnerabilities npm download

Tiaozhan Controller Extension

  1. Get current route from ctx.currentRoute. ctx.currentRoute.Controller for the target Controller class and ctx.currentRoute.methodName for the target method name.

  2. Return response by return.

Install

$ npm i egg-tiaozhan-controller-extension --save

Usage

// {app_root}/config/plugin.js
exports.tiaozhanControllerExtension = {
  enable: true,
  package: 'egg-tiaozhan-controller-extension',
};

Configuration

// {app_root}/config/config.default.js
exports.tiaozhanControllerExtension = {
};

No Configuration.

Example

Get Current Route

class HomeController extends Controller {
  helloEgg() {
    // 也可以在中间件等环境使用
    const route = this.ctx.currentRoute;
    // route.Controller === HomeController
    // route.Controller.prototype === HomeController.prototype
    // route.methodName === 'helloEgg'
  }
}

Use Reflect

class HomeController extends Controller {
  helloEgg() {
    // some code
  }
}

// Need reflect-metadata
Reflect.defineMetadata('mykey', { meta: 123 }, HomeController.prototype);

Use return to response

class HomeController extends Controller {
  simpleText() {
    return 'hi, egg';
  }

  complexBody() {
    return {
      message: 'hi, egg',
    };
  }
}

Questions & Suggestions

Please open a PR.

License

MIT