0.1.35 • Published 5 months ago

@cheetah.js/core v0.1.35

Weekly downloads
-
License
MIT
Repository
github
Last release
5 months ago

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.

0.1.35

5 months ago

0.1.30

5 months ago

0.1.31

5 months ago

0.1.32

5 months ago

0.1.33

5 months ago

0.1.34

5 months ago

0.1.27

5 months ago

0.1.28

5 months ago

0.1.29

5 months ago

0.1.20

6 months ago

0.1.21

6 months ago

0.1.22

6 months ago

0.1.23

6 months ago

0.1.24

6 months ago

0.1.25

5 months ago

0.1.26

5 months ago

0.1.18

6 months ago

0.1.19

6 months ago

0.1.9

6 months ago

0.1.8

6 months ago

0.1.7

6 months ago

0.1.6

6 months ago

0.1.5

6 months ago

0.1.4

6 months ago

0.1.3

6 months ago

0.1.2

6 months ago

0.1.1

6 months ago

0.1.0

6 months ago

0.0.54

6 months ago

0.0.53

6 months ago

0.0.52

6 months ago

0.0.51

6 months ago

0.0.50

6 months ago

0.0.49

6 months ago

0.0.48

6 months ago

0.0.47

6 months ago

0.0.46

6 months ago

0.0.45

6 months ago

0.0.44

6 months ago

0.0.43

6 months ago

0.0.42

6 months ago

0.0.40

6 months ago

0.0.39

6 months ago

0.0.37

6 months ago

0.0.36

6 months ago

0.0.35

6 months ago

0.0.1

6 months ago

0.0.33

6 months ago

0.0.32

6 months ago

0.0.31

6 months ago

0.0.30

6 months ago

0.0.19

6 months ago

0.0.18

6 months ago

0.0.17

6 months ago

0.0.16

6 months ago

0.0.15

6 months ago

0.0.14

6 months ago

0.0.13

6 months ago

0.0.12

6 months ago

0.0.11

6 months ago

0.0.10

6 months ago

0.0.9

6 months ago