@cheetah.js/core
v0.1.35
Published
Cheetah.js is a framework for building web applications object oriented with TypeScript and Bun.sh.
Downloads
276
Maintainers
Readme
Cheetah.js
Cheetah.js is a simple object-oriented framework for Bun still in development. Check the ORM documentation here.
Menu
Installation
For install Cheetah.js, run the command below:
bun install @cheetah.js/core
Your tsconfig.json should have the following settings:
{
"compilerOptions": {
"experimentalDecorators": true,
"emitDecoratorMetadata": true
}
}
Start the server
import { Cheetah } from '@cheetah.js/core';
new Cheetah().listen();
Controller and Routes
In Cheetah.js, all classes in dependency injection are considered providers, including controllers.
Example:
import { Controller, Get, Cheetah } from '@cheetah.js/core';
@Controller()
export class HomeController {
@Get('/')
index() {
return 'Hello World!';
}
}
new Cheetah({
providers: [ HomeController ]
}).listen();
To receive parameters in the route, simply add the ":" before the parameter name. And to receive it in the method, just use the @Param decorator.
import { Controller, Get } from '@cheetah.js/core';
@Controller()
export class HomeController {
@Get(':name')
index(@Param('name') name: string) {
return `Hello ${name}!`;
}
}
Validation
Cheetah.js validates route parameters using class-validator. Simply add the DTO as a method parameter and Cheetah.js will validate the route parameters.
Exemplo:
import { Controller, Get, Query } from '@cheetah.js/core';
export class UserDto {
@IsString()
name: string;
}
@Controller()
export class HomeController {
@Get()
index(@Query() user: UserDto) {
return `Hello ${user.name}!`;
}
}
To configure the validator, simply pass the options in the Cheetah.js constructor:
import { Cheetah } from '@cheetah.js/core';
new Cheetah({
validator: {
whitelist: true
}
}).listen();
Dependency injection
Cheetah.js provides support for dependency injection using the @Service decorator. The available scopes are Singleton (default), request and instance. You can define services to handle business logic and inject them into controllers or other services as needed.
Example:
import { Service } from '@cheetah.js/core';
@Service() // Default scope is Singleton
export class UserService {
create() {
return 'User created!';
}
}
@Service()
export class AnotherService {
constructor(userService: UserService) {
console.log(userService.create()); // User created!
}
}
Middleware
Cheetah.js supports the use of middleware to process requests before they reach their defined routes. This allows you to perform additional logic and manipulate context. Middlewares can be added to the class or method and all middleware must be in the scope of dependency injection.
Example:
import { Context, Middleware, Service, CheetahMiddleware, CheetahClosure } from '@cheetah.js/core';
@Service
export class LoggerMiddleware implements CheetahMiddleware {
handle(context: Context, next: CheetahClosure) {
next();
}
}
@Middleware(LoggerMiddleware)
@Controller()
export class HomeController {
@Get('/')
index() {
return 'Hello World!';
}
}
Logging
We provide the LoggerService service, it uses pinojs to log.
import { Cheetah, LoggerService, Controller } from '@cheetah.js/core';
@Controller()
export class HomeController {
constructor(logger: LoggerService) {
this.logger.info("Hello World!")
}
}
new Cheetah({logger: {level: 'info'}}).listen();
If you need to customize the logger, it can be configured:
import { Cheetah } from '@cheetah.js/core';
new Cheetah().useLogger(CustomServiceLogger)
Contributing
Contributions are welcome! Feel free to open issues and submit pull requests.