# decorator-egg

> Egg.js 装饰器

Latest version **1.0.2** (published 2019-07-25) · MIT license · 0 weekly downloads

## Install

```sh
npm install decorator-egg
pnpm add decorator-egg
yarn add decorator-egg
bun add decorator-egg
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.2 |
| Published | 2019-07-25 |
| First published | 2019-07-19 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=8.9.0 |
| Dependencies | 0 |
| Unpacked size | 274.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | lblblong |
| Maintainers | lblblong |

## Links

- npm: https://www.npmjs.com/package/decorator-egg
- npm.io page: https://npm.io/package/decorator-egg

## Recent versions

- 1.0.2 (latest) — 2019-07-25
- 1.0.1 — 2019-07-19
- 1.0.0 — 2019-07-19

## README

# decorator-egg

> 使用装饰器给 egg 注册路由

# Usage

### For Install
```bash
npm install --save decorator-egg
```

### Setup

In `router.ts` or `route.js`

```ts
// router.ts or route.js
import { Application } from 'egg';
import { initRouter } from 'decorator-egg';

export default (app: Application) => {
    initRouter(app);
}
```

# Prefix Url Globally

```js
// router.ts or `router.js`
initRouter(app, { prefix: '/api' })

// controller.ts or `controller.js`
import { Get } from 'decorator-egg'

export default class index extends Controller {
    @Get('/user') //===>>/api/user
    async get() {
        this.ctx.body = 'hello, decorator-egg.'
    }
}

```

# Prefix Url For Controller
```js
// controller.ts or `controller.js`
import { Router, Get } from 'decorator-egg'

@Router('/home')
export default class home extends Controller {
    @Get('/test') //===>>/home/test
    async get() {
        this.ctx.body = 'hello, decorator-egg.'
    }
}

```

# Router Middleware

Router middleware will run before the target function.

***Example***

```ts
import { Controller } from 'egg';
import { Router, Get, Post, Del, Put } from 'decorator-egg';

// @routerDecorator.prefix('/example')
@Router('/example', (ctx, next) => {
    console.log(ctx.request.URL);
    console.log('ExampleController的prefix中的中间件1');
    next();
}, async (ctx, next) => {
    await next();
    console.log(ctx.request.URL);
    console.log('ExampleController的prefix中的中间件2');
})
export default class ExampleController extends Controller {

    @Get('/index') // ===>>/example/index get
    @Post('/index2') // ===>>/example/index2 post
    public async test1 () {
        const { ctx } = this;
        ctx.body = 'hello, decorator-egg.'
    }

    @Put('/parms/:id') // ===>>/parms/:id put
    public async test2 () {
        const { ctx } = this;
        ctx.body = 'hello, decorator-egg.'
    }

    @Del('/parms/:id/:pwd', isLogin, hasDelAuth) // ===>>/parms/:id/:pwd del
    public async test3 () {
        const { ctx } = this;
        ctx.body = 'hello, decorator-egg.'
    }
}

// isLogin middleware
const isLogin = async (ctx, next) => {
    if (ctx.params['id']) {
        await next();
    } else {
        ctx.status = 401;
    }
}

// hasDelAuth middleware
const hasDelAuth = async (ctx, next) => {
    if (ctx.params['pwd'] === 'admin') {
        await next();
        // ctx.body = 'xxx'
    } else {
        ctx.status = 403;
    }
}

```

The MIT License (MIT)

---
_Source: https://npm.io/package/decorator-egg · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
