Pagination @Put(), @Delete()

  1. Status code problem

    Nest will give a default status code of 200 when the request is successful. But we can also customize the status code and return.

    Nest does not need to memorize all status codes, you can use @HttpCode(HttpStatus. Prompt) to select the prompt status, which will automatically return the corresponding status code.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    import { Controller, Get, Param, Post, Body } from '@nestjs/common';

    @Controller('coffees')
    export class CoffeesController {
    @Post()
    @HttpCode(HttpStatus.GONE) //Decorator position, return 410GONE.
    create(@Body('name') body){
    return body;
    }
    }

    @HttpCode(HttpStatus.hint) is placed before the callback function, which means that the result returned by the callback function after it must be the only definite status.


  2. @Res() native express response processing method

    The @Res() decorator can be used in endpoint method parameters, allowing us to use native express response handling methods.

    1
    2
    3
    4
    5
    6
    7
    @Controller('coffees')
    export class CoffeesController {
    @Get()
    findAll(@Res() response){
    response.status(200)send('This action returns all coffees');
    }
    }

    This approach is flexible and provides header manipulation, library specific functionality, etc., but should be used with care. It has the disadvantage of losing compatibility with Nest features that rely on Nest’s standard response handling, such as interceptors and the @HttpCode() decorator. It also makes the code harder to test and has to mock the response object.


  3. Put and Patch

    Put replaces the entire resource, so we need to include the entire object in the request payload.

    Patch is different in that it only modifies part of the resource. We could even just update a single property of the resource if we wanted to.

    The corresponding methods for Put and Patch are update .

    1
    2
    3
    4
    5
    6
    7
    @Controller('coffees')
    export class CoffeesController {
    @Patch(':id')
    update(@Param('id') id: string, @Body() body){ //Pay attention to the spelling
    return `This action updates #${id} coffee`;
    }
    }

    Pay attention to the spelling here. When using Params, Query or req.body parameters, you must first declare the corresponding @ decorator. In addition, if it is all res.body, it should be written as @Body() body , () cannot be lost.


  4. Delete

    The corresponding method of Delete is remove() .

    1
    2
    3
    4
    5
    6
    7
    @Controller('coffees')
    export class CoffeesController {
    @Delete(':id')
    remove(@Param('id') id: string){ //remove, note that id is also passed in
    return `This action removes #${id} coffee`;
    }
    }


  1. Pagination

    We want to use path parameters: identify a specific resource, and when using query parameters: filter or sort that resource.

    The @Query decorator in Nest is used to get all or a specific part of query parameters.

    1
    2
    3
    4
    5
    6
    7
    8
    @Controller('coffees')
    export class CoffeesController {
    @Get()
    findAll(@Query() paginationQuery){
    const { limit, offset} = paginationQuery;
    return `This action returns all coffees. Limit: ${limit}, offset: ${offset}`;
    }
    }

Share