# @decorators/express

> node decorators - decorators for express library

Latest version **3.0.0** (published 2023-07-28) · MIT license · 0 weekly downloads

## Install

```sh
npm install @decorators/express
pnpm add @decorators/express
yarn add @decorators/express
bun add @decorators/express
```

## Health

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

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 3.0.0 |
| Published | 2023-07-28 |
| First published | 2016-09-27 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 73.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Serhii Sol |
| Maintainers | decorators |
| Keywords | nodejs, express, node decorators, typescript |

## Links

- npm: https://www.npmjs.com/package/@decorators/express
- Repository: https://github.com/serhiisol/node-decorators
- Homepage: https://github.com/serhiisol/node-decorators#readme
- Issues: https://github.com/serhiisol/node-decorators/issues
- npm.io page: https://npm.io/package/@decorators/express

## Alternatives

- [@openai/codex-sdk](https://npm.io/package/@openai/codex-sdk.md) — 731.4K weekly downloads
- [babel-plugin-transform-react-jsx](https://npm.io/package/babel-plugin-transform-react-jsx.md) — 565.0K weekly downloads
- [babel-helper-remove-or-void](https://npm.io/package/babel-helper-remove-or-void.md) — 508.5K weekly downloads
- [@pnpm/store-controller-types](https://npm.io/package/@pnpm/store-controller-types.md) — 186.9K weekly downloads
- [react-native-signature-canvas](https://npm.io/package/react-native-signature-canvas.md) — 155.6K weekly downloads

## Recent versions

- 3.0.0 (latest) — 2023-07-28
- 2.9.4 — 2023-06-28
- 2.9.3 — 2023-04-03
- 2.9.1 — 2023-02-03
- 2.9.0 — 2023-01-30
- 2.8.1 — 2023-01-30
- 2.8.0 — 2023-01-18
- 2.7.0 — 2022-12-19
- 2.6.0 — 2021-06-20
- 2.5.0 — 2021-04-08
- 2.4.0 — 2020-07-08
- 2.3.0 — 2018-07-05
- 2.2.2 — 2018-03-01
- 2.2.1 — 2018-01-14
- 2.2.0 — 2017-11-30
- … 9 more at https://npm.io/package/@decorators/express/versions

## README

![Node Decorators](https://github.com/serhiisol/node-decorators/blob/master/decorators.png?raw=true)

Project implements decorators for modern tools for NodeJS like:
- [ExpressJS]

### Installation
```
npm install @decorators/express --save
```

### API
#### Functions
* **attachControllers(app: Express.Application | Express.Router, controllers: Controller[])** - attach controllers to express application
* **attachControllerInstances(app: Express.Application | Express.Router, controllers: Controller[])** - attach already existing instances of controllers

#### Decorators
##### Class
* **@Controller(baseUrl: string, middleware?: Middleware[])**, **@Controller(baseUrl: string, routerOptions: RouterOptions, middleware?: Middleware[])** - Registers controller for base url

##### Method
* **@All(url: string, middleware?: Middleware[])** - Registers all routes
* **@Get(url: string, middleware?: Middleware[])** - Registers get route
* **@Post(url: string, middleware?: Middleware[])** - Registers post route
* **@Put(url: string, middleware?: Middleware[])** - Registers put route
* **@Delete(url: string, middleware?: Middleware[])** - Registers delete route
* **@Patch(url: string, middleware?: Middleware[])** - Registers patch route
* **@Options(url: string, middleware?: Middleware[])** - Registers options route
* **@Head(url: string, middleware?: Middleware[])** - Registers head route
* **@Status(code: number)** - Specifies status code for the route

where middleware is the class that implements `Middleware` interface.

To use class, import `Middleware` interface and implement it, like so:
```typescript
import { Middleware } from '@decorators/express';

class UserMiddleware implements Middleware {
  public use(request: Request, response: Response, next: NextFunction): void {
    next();
  }
}
```

or a simple function

```typescript
function userMiddleware(request: Request, response: Response, next: NextFunction): void {
  next();
}
```

##### Parameter
* **@Request(property?: string)** (alias **@Req(property?: string)**) - Returns express req object or any other object, if name was specified
* **@Response()** (alias **@Res**) - Returns express res object
* **@Next()** - Returns express next function
* **@Params(param?: string)** - Express req.params object or single param, if param name was specified
* **@Query(param?: string)** - Express req.query object or single query param, if query param name was specified
* **@Body(param?: string)** - Express req.body object or single body param, if body param name was specified
* **@Headers(property?: string)** - Express req.headers object or single headers param, if headers param name was specified
* **@Cookies(param?: string)** - Express req.body object or single cookies param, if cookies param name was specified

#### Error middleware
To add error middleware, that handles unhandled errors simply implement `ErrorMiddleware` interface and provide it using `ERROR_MIDDLEWARE` token, like so:

```typescript
import { Container, ErrorMiddleware, ERROR_MIDDLEWARE } from '@decorators/express';

@Injectable()
class ServerErrorMiddleware implements ErrorMiddleware {
  public use(error: Error, request: Request, response: Response, next: NextFunction) {
    next();
  }
}

Container.provide([
  { provide: ERROR_MIDDLEWARE, useClass: ServerErrorMiddleware }
]);
```

or as a function

```typescript
import { Container, ERROR_MIDDLEWARE } from '@decorators/express';

function serverErrorMiddleware(error: Error, request: Request, response: Response, next: NextFunction) {
  next();
}

Container.provide([
  { provide: ERROR_MIDDLEWARE, useValue: serverErrorMiddleware }
]);
```

#### Dependency injection
This module supports dependency injection provided by `@decorators/di` module. For example, see the full example below.

### Example Express Application and Controller:
```typescript
import {
  Response, Params, Controller, Get,
  attachControllers, Middleware
} from '@decorators/express';

@Controller('/')
class UsersController {

  constructor(private userService: UserService) {}

  @Get('/users/:id')
  getData(@Response() res, @Params('id') id: string) {
    res.send(this.userService.findById(id));
  }
}

let app = express();
attachControllers(app, [UsersController]);

app.listen(3000);
```

You can also attach controllers to express.Router instance. This is useful when you want to namespace all of the routes with a prefix:

```
import { attachControllers } from '@decorators/express';

const apiRouter = express.Router();
attachControllers(apiRouter, [UsersController]);

app.use('/v1/api', apiRouter);
```


You can also use custom decorators as middleware :

Custom Decorator
```typescript
import { attachMiddleware } from "@decorators/express";
import {Request,Response,NextFunction} from '@decorators/express/node_modules/express';

export function Access(key: string) {
    return function (target: any,propertyKey: string,descriptor: PropertyDescriptor) {
      attachMiddleware(target,propertyKey,(req : Request,res : Response,next : NextFunction)=>{
            if(["CAN_ACCESS_TEST","CAN_ACCESS_HOME"].includes(key)){
              next();
            }else{
              res.send("ACCESS DENIED");
            }
        })
    };
  }

```


Controller Code 

```typescript
@Controller("/")
export class MainController {

    @Access("CAN_ACCESS_TEST")
    @Get("/test")
    getB() {
        return "You can access the test";
    }

    @Access("CAN_ACCESS_HOME")
    @Get("/home")
    getB() {
        return "You can access the home";
    }
}

```

*Note:-*``` Please use custom decorators before express decorators otherwise system will not detect any controller metadata and your decorator will not invoked.```

Will work:
```typescript
    @Access("CAN_ACCESS_TEST")
    @Get("/test")
    getB() {
        return "You can access the test";
    }
```
Will not work:
```typescript
    @Get("/test")
    @Access("CAN_ACCESS_TEST")
    getB() {
        return "You can access the test";
    }
```


[ExpressJS]:http://expressjs.com

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