JSPM

typescript-rest-body-guard

2.0.1
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • 0
  • Score
    100M100P100Q12997F
  • License MIT

Body-checking decorator for typescript-rest

Package Exports

  • typescript-rest-body-guard

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (typescript-rest-body-guard) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

BodyGuard for typescript-rest

BodyGuard is a library to use in conjunction with typescript-rest. The library provides a decorator to check if request body data contains all arguments you need.

Installation

Using npm:

$ npm i typescript-rest-body-guard

Or using yarn:

$ yarn add typescript-rest-body-guard

Preconditions

You must provide a DTO class representing your POST data for the request.

// DTOs.ts

export class LoginDTO {
  public constructor(
    public username: string = '',
    public password: string = ''
  ) {}
}

Then you can use it with BodyGuard in your controller:

import { Path, POST } from 'typescript-rest';
import { BodyGuard } from 'typescript-rest-body-guard';

import DTOs from './DTOs';

@Path('/auth')
export default class AuthController {
  @BodyGuard
  @Path('/login')
  @POST
  public login(data: DTOs.LoginDTO) {
    // ...
  }
}

Now request body arguments are guaranteed not to be empty or BodyGuard automatically will return an error to user.