npm package: @impactor/nest
Description: utils for NestJs. Automatically generate APIs and crud operators for NestJs apps
Author: Sherif Eldeeb
Latest version: 5.0.0

utils for NestJs

## Decorators

## @Controller()

Quickly generate all API endpoints and Crud operations of a controller without a single line of code.
It also generate Swagger decorators for all routes.

[Read more...](./README.controller.md)

## @Prop()

a param decorator that extracts a field from the request
and validates it using pipes
and add the suitable Swagger's docs.

### Examples

```ts

// extract `userId` from params
@Get(':userId')
 findUserById(@Prop('userId') userId: string){ ... }

// extract the id from the query string
 @Get()
 filterByTenant(@Prop('tenantId', 'query') tenantId: string){ ... }

//  extract the product id from the request's body
  @Post()
  createOrder(@Prop('productId','body') productId: string){ ... }

// manipulate the swagger's docs: make the id optional
 @Get('id')
 find(@Prop('id', 'params', { required: false }) id?: string){ ... }

```

## Exceptions

RPC exceptions equivalent to the corresponding HTTP exceptions, such as:

- `RpcBadRequestException` -> `HttpBadRequestException`
- `RpcConflictException` -> `HttpConflictException`
- `RpcInternalServerErrorException` -> `HttpInternalServerErrorException`
- `RpcMethodNotAllowedException` -> `HttpMethodNotAllowedException`
- `RpcNotFoundException` -> `HttpNotFoundException`
- `RpcNotImplementedException` -> `HttpNotImplementedException`
- `RpcUnauthorizedException` -> `HttpUnauthorizedException`

Use these exceptions in microservices instead of the HTTP exceptions Nest provides, so you can handle RPC exception correctly.

usage:

inside microservices:

```ts
export function doAction() {
  // if a bad request is received
  throw new RpcBadRequestException(); // correct
  throw new HttpBadRequestException(); // wrong
}
```

if you returned an instance of HttpException, Nest will handle it just like any other http, causes the exception not propagated to the microservice that requested the action and your response will be lost.

Using RPC exceptions instead, with the RPC exception filter we provide, you can handle this type of exceptions correctly.

## Exception Filters

- RPC: convert Error to RpcException
- WS: convert Error to WsException
- TypeORM: detect TypeORM exceptions and handle them.

## Guards

### Auth guard

- `JwtAuthGuard`: inspects the JWT token and guard your routes based on this token.

it also checks the specified roles to determine weather to pass the guard or not.

Thi guard also provides the following decorators:

- `@Public()` and `@Private()`: to allow or disallow un-authenticated users to use the route. You can use it in the class-level or route-level.
- `@Roles()`: if provided, the guard checks the user roles before allowing him to use the route. you need to provide the user role when creating `req.user` object.

### example

```ts
// app.module.ts
import { JwtAuthGuard } from "@impactor/nest"

@Module({
  imports: [...],
   providers: [
    AppService,
    {
      provide: APP_GUARD,
      useClass: JwtAuthGuard, //<---
    },
  ],
});
```

```ts
// users.controller.ts
import { Public, Private } from "@impactor/nest";

@Controller()
@Private() //<--- lock all routes
export class UsersController {
  @Pot()
  addUser() {}

  @Get()
  @Public() //<--- allow this route
  getUserById() {}

  @Get()
  @Roles("admin", "moderators") //<--- limit access to admins and moderators only
  getAllUsers() {}
}
```

## Features

- `BasicModule`
- `createApp()`: create a Nest app [learn more](./README.createApp.md)
- `createMicroservice()`: create a microservice
- `generate-metadata`: generate Swagger metadata