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.
- Host: GitHub
- URL: https://github.com/luizfbalves/nespress
- Owner: luizfbalves
- Created: 2025-01-10T23:51:57.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-07-23T15:58:28.000Z (11 months ago)
- Last Synced: 2025-09-02T18:55:39.643Z (10 months ago)
- Topics: bun, decorators, express, nestjs, typescript
- Language: TypeScript
- Homepage:
- Size: 171 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Nespress π
π§π· PortuguΓͺs Brasileiro
## π 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.