Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/gaurishhs/elysia-decorators
Decorators support plugin for Elysia
https://github.com/gaurishhs/elysia-decorators
Last synced: 14 days ago
JSON representation
Decorators support plugin for Elysia
- Host: GitHub
- URL: https://github.com/gaurishhs/elysia-decorators
- Owner: gaurishhs
- License: mit
- Created: 2023-03-07T17:44:03.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-07-07T05:59:42.000Z (4 months ago)
- Last Synced: 2024-10-26T13:08:28.839Z (18 days ago)
- Language: TypeScript
- Size: 71.3 KB
- Stars: 67
- Watchers: 1
- Forks: 6
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-elysia - Decorators - Use typescript decorators. (Plugins)
README
# elysia-decorators
![badge](https://github.com/gaurishhs/elysia-decorators/actions/workflows/npm-publish.yml/badge.svg)
- This plugin adds decorator and controller-based routing support to elysia.
> **Note:**
> You need to enable [experimentalDecorators](https://www.typescriptlang.org/tsconfig#experimentalDecorators) compiler option for this plugin to work.Please consider starring the project to show your ❤️ and support.
## Installation
Requires Bun v1.0.14 when using file-based controllers due to dependence on `Bun.Glob` introduced in v1.0.14.
```bash
bun add elysia-decorators
```## Example
- [Demo](https://github.com/gaurishhs/elysia-decorators/tree/main/demo)
```ts
import { Controller, Get, decorators } from "elysia-decorators";
import { Elysia } from "elysia";// /users prefix
@Controller("/users")
class UsersController {
@Get()
index() {
return "Hello World";
}
}const app = new Elysia();
app.use(
decorators({
controllers: [UsersController],
})
);app.listen(3000);
```## Documentation
Note: When using File-based controllers Bun v1.0.14 or more is required since it depends on `Bun.Glob`
### Controller Decorator
The `Controller` decorator is used to mark a class as a controller. It takes a string as an argument which is the prefix for all the routes in that controller. The prefix can be empty.
```ts
@Controller("/users")
class UsersController {
// ...
}
```---
### Method Decorators
The method decorators are used to mark a method as a route. They take a string as an argument which is the path for the route. The path can be empty.
```ts
@Controller("/users")
class UsersController {
@Get()
index() {
// ...
}
}
```The above code will create a route at `/users` which will respond to `GET` requests.
---
### Custom Decorator
You can use a custom method as well by using the `Custom` decorator.
```ts
@Controller("/users")
class UsersController {
@Custom("M-SEARCH", "/")
index() {
// ...
}
}
```The above code will create a route at `/users` which will respond to `M-SEARCH` requests.
## License
- This project is licensed under the MIT License - see the LICENSE file for details