Skyframe Documentation

Controller

Controller

Introduction

Nest Controllers are responsible for handling incoming HTTP requests and returning responses to the client (official NestJS documentation).

In Skyframe, we provide a generic SkfNestController base class. By extending the SkfNestController class, it gives you a list of predefined RESTful CRUD operation endpoints.

For example:

import { Controller, SkfNestController } from '@skyframe/nest';
@Controller('product', () => Product)
export class ProductController extends SkfNestController<Product> {}

Find a single product

GET /api/product/:id

List of products

GET /api/product -> Retrieve the first page of product list (first 20 by default)
GET /api/product?limit=50&offset=0 -> Retrieve the first 50 products
GET /api/product?limit=50&offset=50 -> Skip the first 50 products and retrieve the next 50

Create a product

POST /api/product
Request Body:
{
"name": "My product",
"description": "My product description",
"price": 5000
}

Update a product

PATCH /api/product/:id
Request Body:
{
"name": "New product name",
"description": "New product description",
"price": 4000
}

Partially update a product

PUT /api/product/:id
Request Body:
{
"price": 4500
}

Delete product

DELETE /api/product/:id

Advanced

Redefine CRUD endpoints

It is possible to redefine all CRUD operations with custom logic:

import { Controller, SkfNestController } from '@skyframe/nest';
@Controller('product', () => Product)
export class ProductController extends SkfNestController<Product> {
@AccessControl('View')
@Get()
public async customListOfProducts(
@Req() req: Request,
@Res() res: Response,
@Next() next: NextFunction
): Promise<void> {
// Return list of products
}
@AccessControl('View', 'id')
@Get(':id')
public async customFindProductById(
@Req() req: Request,
@Res() res: Response,
@Next() next: NextFunction
): Promise<void> {
// Return product by id
}
@AccessControl('Add')
@Post()
public async customCreateProduct(
@Req() req: Request,
@Res() res: Response,
@Next() next: NextFunction
): Promise<void> {
// Create new product
}
@AccessControl('Edit', 'id')
@Put(':id')
public async customUpdateProduct(
@Req() req: Request,
@Res() res: Response,
@Next() next: NextFunction
): Promise<void> {
// Update product
}
@AccessControl('Edit', 'id')
@Patch(':id')
public async customPartiallyUpdateProduct(
@Req() req: Request,
@Res() res: Response,
@Next() next: NextFunction
): Promise<void> {
// Partially update product
}
@AccessControl('Delete', 'id')
@Delete(':id')
public async customDeleteProduct(
@Req() req: Request,
@Res() res: Response,
@Next() next: NextFunction
): Promise<void> {
// Delete product
}
}

Working with relations

You may want to check the CRUD Endpoints Reference to know in-depth how are the foreign keys and related fields handled when you are creating, updating, or deleting entities.