# simple-ts-express-decorators

> Simple express decorators for typescript

Latest version **1.3.0** (published 2022-12-12) · MIT license · 0 weekly downloads

## Install

```sh
npm install simple-ts-express-decorators
pnpm add simple-ts-express-decorators
yarn add simple-ts-express-decorators
bun add simple-ts-express-decorators
```

## 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.3.0 |
| Published | 2022-12-12 |
| First published | 2019-11-11 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 23.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 3 |
| Author | Andrew |
| Maintainers | arnidan |
| Keywords | express, expressjs, typescript, ts, decorators, controller |

## Links

- npm: https://www.npmjs.com/package/simple-ts-express-decorators
- Repository: https://github.com/arnidan/simple-ts-express-decorators
- npm.io page: https://npm.io/package/simple-ts-express-decorators

## Dependencies (2)

- [glob](https://npm.io/package/glob.md) ^8.0.3
- [reflect-metadata](https://npm.io/package/reflect-metadata.md) ^0.1.13

## Alternatives

- [localforage](https://npm.io/package/localforage.md) — 6.2M weekly downloads
- [localforage-observable](https://npm.io/package/localforage-observable.md) — 30.8K weekly downloads
- [@y/y](https://npm.io/package/@y/y.md) — 30.1K weekly downloads
- [@metaobjectsdev/render](https://npm.io/package/@metaobjectsdev/render.md) — 3.5K weekly downloads
- [@ledgerhq/coin-algorand](https://npm.io/package/@ledgerhq/coin-algorand.md) — 1.1K weekly downloads

## Recent versions

- 1.3.0 (latest) — 2022-12-12
- 1.2.0-0 (next) — 2021-09-22
- 1.2.0 — 2021-09-22
- 1.1.0 — 2020-11-03
- 1.1.0-5 — 2020-11-03
- 1.1.0-4 — 2020-11-03
- 1.1.0-3 — 2020-11-03
- 1.1.0-2 — 2020-11-03
- 1.1.0-1 — 2020-11-03
- 1.1.0-0 — 2020-11-03
- 1.0.4 — 2020-05-01
- 1.0.3 — 2020-02-21
- 1.0.2 — 2019-11-12
- 1.0.1 — 2019-11-11

## README

# Simple typescript decorators for express

![Build](https://github.com/arnidan/simple-ts-express-decorators/workflows/CI/badge.svg)
[![Coverage Status](https://coveralls.io/repos/github/arnidan/simple-ts-express-decorators/badge.svg?branch=master)](https://coveralls.io/github/arnidan/simple-ts-express-decorators?branch=master)

Simple controller and methods typescript decorators for express. 
Inspired by [ts-decorator-routing](https://github.com/nehalist/ts-decorator-routing) 

Provides pure request and response from express without complicated handlers or transformers. 

## Installation

1. Install module

    ```bash
    npm install simple-ts-express-decorators
    ```
    
    ```
    yarn add simple-ts-express-decorators
    ```

1. Install `reflect-metadata`

    ```bash
    npm install reflect-metadata
    ```
    
    ```
    yarn add reflect-metadata
    ```

1. Import `reflect-metadata` before using provided decorators

1. Enable typescript decorators in your `tsconfig.json`

    ```json
    {
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true
    }
    ````

## Example of usage

`UsersController.ts`

```typescript
import {Controller, Get} from 'simple-ts-express-decorators'; 
import {Request, Response, RequestHandler} from 'express';
import multer, {memoryStorage} from 'multer';

const upload = multer({storage: memoryStorage()});

const logMiddleware: RequestHandler = (req, res, next) => {
  console.log('log');

  next();
};

@Controller('/', logMiddleware)
export class UsersController {

  @Get('/users')
  index(request: Request, response: Response) {
    response.json([
      {id: 1, username: 'example'}
    ]);
  }

  @Post('/users', upload.sindle('avatar')) // example of usage with middleware
  create(request: Request, response: Response) {
    const avatar = request.file;

    // ...save

    response.status(201);
  }
}
```

`index.ts`

```typescript
import "reflect-metadata";
import * as express from 'express';
import {ControllersLoader} from 'simple-ts-express-decorators';
import {UserController} from './UserController'; 

const app = express();

new ControllersLoader({
  controllers: [UserController]
}).load(app);

app.listen(3000);
```

## Configuration options

| Option      | Description                                                                                                           |
|-------------|-----------------------------------------------------------------------------------------------------------------------|
| controllers | Required. Array of controllers to load or array of glob patterns to load controllers from                             |
| container   | Any container implementation with `get()` method. For example [InversifyJS](https://github.com/inversify/InversifyJS) |

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