Skip to content

🌐 A server-side framework to build amazing apps

License

Notifications You must be signed in to change notification settings

danydacosta/Rakkit

Repository files navigation

Nest Logo
A simple backend library written in TypeScript that provides REST API and Websocket tools to build amazing server-side applications

Getting started

npm i rakkit
yarn add rakkit

Code

Imports are skipped for code clarity. For each example we need to have a starting point:

import { Rakkit } from "rakkit"

Rakkit.start({
  routers: [
    // Your path the router files
    `${__dirname}/**/*Router.ts`
  ]
})

And below is the terminal output:

PUBLIC: http://localhost:4000
REST  : http://localhost:4000/rest
WS    : http://localhost:4000/ws

Routers:
βœ… http://localhost:4000/people

Simple app example

This is an example of a REST api app. The context is the same as a koa context, and you can use it like a it too.

PeopleRouter.ts

@Router("people")
class PeopleRouter {
  @Get("/")
  get(context: Context) {
    context.body = "hello world";
  }
  @Post("/")
  post(context: Context) {
    ...
  }
  @Put("/:id")
  put(context: Context) {
    ...
  }
  @Delete("/:id")
  delete(context: Context) {
    ...
  }
}

Middlewares

In this example we have two middlewares, the @BeforeMiddleware use methods that are executed before the @Get("/") and for the @AfterMiddleware they are executed after it.
You have three levels of middleware, on the endpoint (@Get, @Post, @Put, @Delete), on the router (@Router) and globaly (All router of your application). In this example we illustrate each level.
Middleware are executed in the order of the list that is passed in the @UseMiddleware decorator.
"Global middlewares" wrap "router middlewares" (global BeforeMiddleware are executed before the router BeforeMiddleware and global AfterMiddleware are executed after the router AfterMiddleware). "Router middleware" wraps "enpoint middlewares".

BeforeMiddleware.ts

@BeforeMiddleware()
class BeforeMiddleware implements IBaseMiddleware {
  @Get("/")
  use(context: Context, next: NextFunction) {
    ...
    next();
  }
}

AfterMiddleware.ts

@AfterMiddleware()
class AfterMiddleware implements IBaseMiddleware {
  @Get("/")
  use(context: Context, next: NextFunction) {
    ...
    next();
  }
}

App.ts

Rakkit.start({
  routers: [
    // Your path the router files
    `${__dirname}/**/*Router.ts`
  ],
  globalMiddlewares: [
    BeforeMiddleware,
    AfterMiddleware
  ]
})

PeopleRouter.ts

@Router("people")
@UseMiddleware(
  BeforeMiddleware,
  AfterMiddleware
)
class PeopleRouter {
  @Get("/")
  @UseMiddleware(
    BeforeMiddleware,
    AfterMiddleware
  )
  get(context: Context) {
    context.body = this._peopleService.MyServiceValue;
  }
}

With a service and DI

A service is a singleton that you can inject into your classes if they are also declared as a @Service or as a @Router, @Websocket, @AfterMiddleware, @BeforeMiddleware.
You can assign an id to the service (@Service(id?: string | number)) if you need to have multiple instances of the class running at the same time. You can also retrieve the specified instance with the @Inject decorator (@Service(id?: string | number)).

PeopleService.ts

@Service()
class PeopleService {
  MyServiceValue = "I'm a service";
}

PeopleRouter.ts

@Router("people")
class PeopleRouter {
  @Inject()
  private _peopleService: PeopleService;

  @Get("/")
  get(context: Context) {
    context.body = this._peopleService.MyServiceValue;
  }
}

Websockets

PeopleRouter.ts

@Websocket()
class PeopleRouter {
  @On("connection")
  connection(socket: Socket) {
    console.log("A new connection !");
  }

  @On("Hello")
  hello(socket: Socket, datas) {
    socket.emit("world", datas);
  }
}

The project history

Initially this tool was made in order to create a homemade Headless CMS. But as the project progressed, our needs grew and our backend application looked more and more like a Framework, so we choose to make it an independent tool to benefit the community and progress on a better basis.

Philosophy

We wanted to create a tool that would allow us to create backend applications much more simply with a small learning curve, so that anyone, with some TypeScript basics, could use it. We also didn't want to make a clone of NestJS.

Features (full typed, class based with decorated)

  • Dependency injection support
  • Rest API Creation (koa base)
  • Websocket app creation (socket.io)

Contributing

You can simply clone the project and submit a pull request, we will analyze it and decide whether or not it is valid.

TODO

  • Documentation

Chat with us

About

🌐 A server-side framework to build amazing apps

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published