1.1.2 • Published 1 year ago

@typed-web-api/nestjs v1.1.2

Weekly downloads
-
License
ISC
Repository
github
Last release
1 year ago

@typed-web-api/nestjs

Server library to validate the return type of a NestJS web API's endpoints, based on a type declaration generated via @typed-web-api/common.

Example

Given the following sample NestJS controller:

import { Body, Controller, Get, Param, Post, Query } from '@nestjs/common';

@Controller('/users')
export class UsersController {
  @Post('/login')
  async login(@Body() body: { email: string; password: string }) {
    /* ... */
    return loginResponse;
  }

  @Get('/')
  async getUsers(@Query() query: { limit?: string; skip?: string }) {
    /* ... */
    return users;
  }

  @Get('/:userId')
  async getUserById(@Param('userId') userId: string) {
    /* ... */
    return user;
  }
}

This is how to validate the endpoints' return type by using HttpMethod and ServerEndpoints (given the sample UserEndpoints type described in @typed-web-api/common):

import { Body, Controller, Param, Query } from '@nestjs/common';
import { HttpMethod, ServerEndpoints } from '@typed-web-api/nestjs';
import { UserEndpoints } from '...';

@Controller()
export class UsersController implements ServerEndpoints<UserEndpoints> {
  @HttpMethod()
  async '/users/login_post'(@Body() body: { email: string; password: string }) {
    /* ... */
    return loginResponse; // Expected return type => LoginResponse;
  }

  @HttpMethod()
  async '/users_get'(@Query() query: { limit?: string; skip?: string }) {
    /* ... */
    return users; // Expected return type => User[];
  }

  @HttpMethod()
  async '/users/:userId_get'(@Param('userId') userId: string) {
    /* ... */
    return user; // Expected return type => User;
  }
}
1.1.2

1 year ago

1.1.1

2 years ago

1.1.0

2 years ago