Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

egg-decorators

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

egg-decorators

Easy-to-use decorators for writing controllers in egg.js.

  • 1.0.3
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

Easy-to-use decorators for writing controllers in egg.js.

Features

  • Define routes in together with the controller
  • Per-route middleware via decorators
  • Common middleware for a controller via decorators
  • Zero configuration

Quick glance

import { Controller } from 'egg'
import { Resource, Post, createDecorator } from '../egg-decorators'

const users = [
  { id: 1, username: 'alice', password: '123' },
  { id: 2, username: 'bob', password: '456' }
]

// Set ctx.body to what is returned from next middleware
const ReturnBody = createDecorator(async (ctx, next) => {
  ctx.body = await next()
})

// Make sure only authorized user can access
const Authorized = (self: boolean) => createDecorator((ctx, next) => {
  if (ctx.headers.authorization) {
    ctx.user = ...
    if (!self || ctx.user.id == ctx.params.id) {
      return next()
    }
  }

  ctx.status = 401
  return 'Unauthorized'
})

@Resource                   // Auto prefix all routes with '/users' because the name of the controller is 'UserController', and do `router.resource` as egg.js is
@ReturnBody                 // This middleware will apply to all the routes of the controller
export default class UserController extends Controller {
  // GET /users
  async index () {
    this.ctx.body = users
  }

  // GET /users/:id
  @Authorized(false)        // This middleware will only apply to this route
  async show () {
    this.ctx.body = users.find(user => user.id == this.ctx.params.id)
  }

  // POST /users
  async create () {
    users.push(this.ctx.body)
    this.ctx.status = 204
  }

  // PATCH /users/:id
  @Authorized(true)
  async update () {
    this.ctx.body = 'update resource'
  }

  // POST /users/login
  @Post('/login')           // Remember that we have a common prefix on the controller
  async login () {
    this.ctx.body = 'destroy resource'
  }
}

Installation

  1. Install egg-decorator

    yarn add egg-decorators         # Use yarn
    npm i egg-decorators --save     # Use npm
    
  2. Enable plugin in config/plugin.js

    exports.decorators = {
      enable: true,
      package: 'egg-decorators',
    }
    

Keywords

FAQs

Package last updated on 09 Feb 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc