Advertisement
Guest User

Untitled

a guest
Apr 18th, 2019
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.66 KB | None | 0 0
  1. import { Controller, Get, Param, UsePipes, Query, Headers, Body, Post, Header } from '@nestjs/common';
  2. import { AppService } from './app.service';
  3. import { JoiValidationPipe } from './pipes/validation.pipe';
  4. import * as Joi from 'joi';
  5.  
  6. const schema = {
  7. query: {
  8. type: Joi.number(),
  9. },
  10. body: {
  11. example: Joi.number().required(),
  12. },
  13. param: {
  14. id: Joi.string().min(3).max(30).required(),
  15. },
  16. custom: {
  17. // shold have this format
  18. headers: {
  19. 'x-token': Joi.string().required(),
  20. },
  21. options: {
  22. allowUnknown: true,
  23. },
  24. },
  25. };
  26.  
  27. import { createParamDecorator } from '@nestjs/common';
  28.  
  29. export const HeadersCustom = createParamDecorator((data: object, req) => {
  30. return { ...req.headers, ...data };
  31. });
  32.  
  33. @Controller()
  34. export class AppController {
  35. constructor(private readonly appService: AppService) {}
  36.  
  37. @Get()
  38. getHello(): string {
  39. return this.appService.getHello();
  40. }
  41.  
  42. @Get(':id')
  43. findOne(
  44. @Param(new JoiValidationPipe(schema)) all,
  45. @Query(new JoiValidationPipe(schema)) query): string {
  46. return `params: #${JSON.stringify(all)}.. query: #${JSON.stringify(query)}`;
  47. }
  48.  
  49. @Post('/create/:id')
  50. create(
  51. @Param(new JoiValidationPipe(schema)) all,
  52. @Body(new JoiValidationPipe(schema)) body): string {
  53. return `params: #${JSON.stringify(all)}.. body: #${JSON.stringify(body)}`;
  54. }
  55.  
  56. @Post('/create2/:id')
  57. @UsePipes(new JoiValidationPipe(schema))
  58. create2(
  59. @Param() all,
  60. @Body() body,
  61. @Query() query,
  62. @HeadersCustom() headers): string {
  63. return `params: #${JSON.stringify(all)}.. body: #${JSON.stringify(body)} ... header: #${JSON.stringify(headers)}`;
  64. }
  65.  
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement