Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { Controller, Get, Param, UsePipes, Query, Headers, Body, Post, Header } from '@nestjs/common';
- import { AppService } from './app.service';
- import { JoiValidationPipe } from './pipes/validation.pipe';
- import * as Joi from 'joi';
- const schema = {
- query: {
- type: Joi.number(),
- },
- body: {
- example: Joi.number().required(),
- },
- param: {
- id: Joi.string().min(3).max(30).required(),
- },
- custom: {
- // shold have this format
- headers: {
- 'x-token': Joi.string().required(),
- },
- options: {
- allowUnknown: true,
- },
- },
- };
- import { createParamDecorator } from '@nestjs/common';
- export const HeadersCustom = createParamDecorator((data: object, req) => {
- return { ...req.headers, ...data };
- });
- @Controller()
- export class AppController {
- constructor(private readonly appService: AppService) {}
- @Get()
- getHello(): string {
- return this.appService.getHello();
- }
- @Get(':id')
- findOne(
- @Param(new JoiValidationPipe(schema)) all,
- @Query(new JoiValidationPipe(schema)) query): string {
- return `params: #${JSON.stringify(all)}.. query: #${JSON.stringify(query)}`;
- }
- @Post('/create/:id')
- create(
- @Param(new JoiValidationPipe(schema)) all,
- @Body(new JoiValidationPipe(schema)) body): string {
- return `params: #${JSON.stringify(all)}.. body: #${JSON.stringify(body)}`;
- }
- @Post('/create2/:id')
- @UsePipes(new JoiValidationPipe(schema))
- create2(
- @Param() all,
- @Body() body,
- @Query() query,
- @HeadersCustom() headers): string {
- return `params: #${JSON.stringify(all)}.. body: #${JSON.stringify(body)} ... header: #${JSON.stringify(headers)}`;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement