An open API service indexing awesome lists of open source software.

https://github.com/luizfbalves/nespress

Nespress is a wrapper around Express that allows you to use decorators to define routes. It also includes a simple way to register controllers and their routes.
https://github.com/luizfbalves/nespress

bun decorators express nestjs typescript

Last synced: 10 months ago
JSON representation

Nespress is a wrapper around Express that allows you to use decorators to define routes. It also includes a simple way to register controllers and their routes.

Awesome Lists containing this project

README

          

# Nespress πŸš€


πŸ‡§πŸ‡· PortuguΓͺs Brasileiro


NPM version
MIT License
NPM Downloads
GitHub commit activity

## πŸ“– About

**Nespress** is an elegant wrapper for Express that allows you to use decorators to define routes, similar to NestJS. The project makes it easy to develop RESTful APIs with TypeScript, providing a simple and intuitive way to register controllers and their routes.

> **Note:** This package is under active development and may undergo changes. Beta version available for community testing and feedback.

## ✨ Features

- 🏷️ **Decorators** for defining routes and HTTP methods
- πŸ”„ **Automatic registration** of controllers and routes
- πŸ’‰ **Dependency injection** similar to NestJS
- 🧩 **Modular and extensible** - easy to integrate with other packages
- πŸ“¦ **Zero configuration** - start using in seconds
- πŸ”’ **Strong typing** with TypeScript

## πŸš€ Installation

Choose your favorite package manager:

```bash
# NPM
npm install nespress

# Yarn
yarn add nespress

# Bun
bun i nespress
```

### TypeScript Configuration

Make sure to enable these options in your `tsconfig.json`:

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

## πŸ“ Basic Usage

### Starting the server

```typescript
import { Nespress } from 'nespress'

const app = new Nespress({ controllers: [] })
app.start(3000)
```

### Creating a simple controller

```typescript
import { Controller, Get, Post, BODY, QUERY, PARAM } from 'nespress/decorators'

@Controller({ path: '/users', version: 1 })
export class UsersController {
@Get()
getAll() {
return {
statusCode: 200,
data: ['John', 'Mary', 'Peter'],
}
}

@Post()
create(@BODY body: any) {
return {
statusCode: 201,
message: 'User created',
data: body,
}
}

@Get('/:id')
getById(@PARAM('id') id: string) {
return {
statusCode: 200,
data: { id, name: 'User ' + id },
}
}

@Get('/search')
search(@QUERY('name') name: string) {
return {
statusCode: 200,
data: { name },
}
}
}
```

### Registering the controller

```typescript
import { Nespress } from 'nespress'
import { UsersController } from './controllers/users.controller'

const app = new Nespress({
controllers: [UsersController],
})

app.start(3000)
console.log('Server running at http://localhost:3000')
```

### API documentation

After registering your controllers you can generate an OpenAPI specification.
Simply call `generateDocs()` on the `NespressCore` instance and access
`/api-docs`:

```typescript
import { NespressCore } from 'nespress/core'

const core = new NespressCore([UsersController])
core.generateDocs()
core.initialize(3000)
```

## 🧩 Dependency Injection

Nespress supports dependency injection in NestJS style:

```typescript
import { Controller, Get, Injectable, INJECT } from 'nespress/decorators'
import { Nespress } from 'nespress'

@Injectable()
class UserService {
private users = ['John', 'Mary', 'Peter']

getUsers() {
return this.users
}

getUserById(id: number) {
return this.users[id]
}
}

@Controller({ path: '/users', version: 1 })
class UserController {
@INJECT(UserService)
private userService!: UserService

@Get('/list')
getUsers() {
return {
statusCode: 200,
data: this.userService.getUsers(),
}
}
}

const app = new Nespress({
controllers: [UserController],
providers: [UserService],
})

app.start(3000)
```

## πŸ“Œ Available Decorators

### Controller Decorators

- `@Controller(options)` - Defines a class as a controller
- `@Injectable()` - Marks a class as Injectable
- `@INJECT(Provider)` - Injects a provider into a property

### HTTP Method Decorators

- `@Get(path?)` - Defines a GET route
- `@Post(path?)` - Defines a POST route
- `@Put(path?)` - Defines a PUT route
- `@Delete(path?)` - Defines a DELETE route
- `@Patch(path?)` - Defines a PATCH route

### Parameter Decorators

- `@BODY` - Accesses the request body
- `@PARAM(name?)` - Accesses a URL parameter
- `@QUERY(name?)` - Accesses a query parameter
- `@HEADERS(name?)` - Accesses request headers

## πŸ§ͺ Advanced Examples

### Middlewares

```typescript
import { Controller, Get, Middleware } from 'nespress/decorators'

function authMiddleware(req: Request, res: Response, next: NextFunction) {
if (req.headers.authorization) {
next()
} else {
res.status(401).json({ message: 'Unauthorized' })
}
}

@Controller({ path: '/admin' })
class AdminController {
@Get('/dashboard')
@Middleware(authMiddleware)
getDashboard() {
return {
statusCode: 200,
data: { message: 'Admin dashboard' },
}
}
}
```

### Nested Controllers

```typescript
@Controller({ path: '/api', version: 1 })
class ApiController {
@Controller({ path: '/users' })
class UsersController {
@Get()
getUsers() {
return { statusCode: 200, data: ['John', 'Mary'] };
}
}
}
```

## 🀝 Contributing

Contributions are welcome! Feel free to open issues or submit pull requests.

1. Fork the project
2. Create your feature branch (`git checkout -b feature/amazing-feature`)
3. Commit your changes (`git commit -m 'Add: awesome implementation'`)
4. Push to the branch (`git push origin feature/amazing-feature`)
5. Open a Pull Request

## πŸ“ƒ License

This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.

---

Made with β˜• and TypeScript.