A simple backend library written in TypeScript that provides REST API and Websocket tools to build amazing server-side applications
npm i rakkit
yarn add rakkit
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
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) {
...
}
}
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;
}
}
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;
}
}
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);
}
}
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.
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.
You can simply clone the project and submit a pull request, we will analyze it and decide whether or not it is valid.
- Documentation