Skip to main content

Decorators

Built-in decorators

Routing

  • @Get(path: string) registers a new GET endpoint
  • @Post(path: string) registers a new POST endpoint
  • @Put(path: string) registers a new PUT endpoint
  • @Delete(path: string) registers a new DELETE endpoint
  • @Options(path: string) registers a new OPTIONS endpoint
  • @Patch(path: string) registers a new PATCH endpoint

Validation

  • @Validation(schema: Object) validates request body or query parameters based on the provided schema

Plugins and Controllers

  • @KultController(path: string) registers a new controller with a prefix for all routes within it
  • @KultPlugin(name: string) registers a new plugin with the name of the plugin

Custom decorators

Defining a custom decorator

src/app/decorators/role.ts
import { Hook } from '@kult/core';

export const Role = (role: string) => {
return Hook((ctx, next) => {
// login to validate role
if (ctx.request.body.role !== role) {
ctx.throw('Unauthorized');
}
});
};

Using a custom decorator

src/controllers/user.controller.ts
import {
KultController,
Get,
Application,
ControllerBase,
Context,
} from '@kult/core';
import { Role } from '../decorators/role';

@KultController('/users')
class UserController extends ControllerBase {
constructor(app: Application) {
super(app);
}

@Get('/find')
@Role('Admin')
find(ctx: Context) {
return 'Hello World';
}
}

export default UserController;