# @magn/express-decorator

> Typescript Decorators to use with Express Routing

Latest version **1.0.4** (published 2020-10-18) · MIT license · 0 weekly downloads

## Install

```sh
npm install @magn/express-decorator
pnpm add @magn/express-decorator
yarn add @magn/express-decorator
bun add @magn/express-decorator
```

## Health

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

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.4 |
| Published | 2020-10-18 |
| First published | 2020-10-03 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 8.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 4 |
| Author | Matheus Catarino de Aguilar |
| Maintainers | matheusaguilar |
| Keywords | Typescript, typescript, Express, express, Decorator, decorator |

## Links

- npm: https://www.npmjs.com/package/@magn/express-decorator
- Repository: https://github.com/matheusaguilar/express-decorator
- Homepage: https://matheusaguilar.github.io/express-decorator/
- Issues: https://github.com/matheusaguilar/express-decorator/issues
- npm.io page: https://npm.io/package/@magn/express-decorator

## 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

- 1.0.4 (latest) — 2020-10-18
- 1.0.3 — 2020-10-18
- 1.0.2 — 2020-10-03
- 1.0.1 — 2020-10-03
- 1.0.0 — 2020-10-03

## README

## Express-Decorator

Typescript decorators for Node Express.

### Required to work

This package will need the packages:

```javascript
npm install reflect-metadata
```

```javascript
npm install express
```

Also don't forget to enable decorators inside tsconfig.json:

```javascript
{
  "experimentalDecorators": true,
  "emitDecoratorMetadata": true,
}
```

### Decorators Methods
GET, POST, PUT, DELETE, PATCH, AUTH 

### Example

You can download this example at: https://github.com/matheusaguilar/express-decorator-example

```javascript
import * as http from 'http';
import express from 'express';
import bodyParser from 'body-parser';
import { Request, Response, NextFunction } from 'express';
import { Get, Post, Put, Delete, Patch, Auth, loadRoute } from '@magn/express-decorator';

function createErrorMessage(message: string) {
  return JSON.stringify({ message });
}
 
function validateId(req: Request, res: Response, next: NextFunction) {
  if (req.body?.id) {
    next();  
  } else {
    res.status(400).send(createErrorMessage('You need to provide an id on the body params to access this endpoint.'));
  }
}

function validatePassword(req: Request, res: Response, next: NextFunction) {
  if (req.body?.password) {
    next();  
  } else {
    res.status(400).send(createErrorMessage('You need to provide a password on the body params to access this endpoint.'));
  }
}
 
export class ExampleRoute {
  @Get('/') 
  async getInfo(req: Request, res: Response) {
    res.status(200).send("Info");
  }
 
  @Post('/')
  async createInfo(req: Request, res: Response) {
    res.status(200).send("Created");
  }
 
  @Put('/')
  async updateInfo(req: Request, res: Response) {
    res.status(200).send("Updated");
  }
 
  @Auth(validateId)
  @Auth(validatePassword)
  @Delete('/')
  async deleteInfo(req: Request, res: Response) {
    res.status(200).send("Deleted");
  }

  @Patch('/')
  async patchInfo(req: Request, res: Response) {
    res.status(200).send("Patched");
  }
}
 
const app = express();
app.use(bodyParser.json());
app.use('/', loadRoute(new ExampleRoute()));
const httpServer = new http.Server(app);
const PORT = 8080;
httpServer.listen(PORT, () => {
  console.log(`Server listening on port ${PORT}!`);
});
```

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