How to use the @nestjs/swagger.ApiOkResponse function in @nestjs/swagger

To help you get started, we’ve selected a few @nestjs/swagger examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github nartc / nest-mean / server / src / todo / todo.controller.ts View on Github external
exist.content = content;
        exist.isCompleted = isCompleted;
        exist.level = level;

        try {
            const updated = await this._todoService.update(id, exist);
            return this._todoService.map(updated.toJSON(), Todo, TodoVm);
        } catch (e) {
            throw new HttpException(e, HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    @Delete(':id')
    // @Roles(UserRole.Admin)
    // @UseGuards(AuthGuard('jwt'), RolesGuard)
    @ApiOkResponse({ type: TodoVm })
    @ApiBadRequestResponse({ type: ApiException })
    @ApiOperation(GetOperationId(Todo.modelName, 'Delete'))
    async delete(@Param('id') id: string): Promise {
        try {
            const deleted = await this._todoService.delete(id);
            return this._todoService.map(deleted.toJSON(), Todo, TodoVm);
        } catch (e) {
            throw new InternalServerErrorException(e);
        }
    }
}

export function EnumToArray(enumVariable: any): string[] {
    return Object.keys(enumVariable).map(k => enumVariable[k]);
}
github nartc / nest-mean / server / src / todo / todo.controller.ts View on Github external
filter['isCompleted'] = isCompleted;
            }
        }

        try {
            const todos = await this._todoService.findAll(filter);
            return this._todoService.mapArray(map(todos, todo => todo.toJSON()), Todo, TodoVm);
        } catch (e) {
            throw new HttpException(e, HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    @Put()
    // @Roles(UserRole.Admin, UserRole.User)
    // @UseGuards(AuthGuard('jwt'), RolesGuard)
    @ApiOkResponse({ type: TodoVm })
    @ApiBadRequestResponse({ type: ApiException })
    @ApiOperation(GetOperationId(Todo.modelName, 'Update'))
    async update(@Body() vm: TodoVm): Promise {
        const { id, content, level, isCompleted } = vm;

        if (!vm || !id) {
            throw new HttpException('Missing parameters', HttpStatus.BAD_REQUEST);
        }

        const exist = await this._todoService.findById(id);

        if (!exist) {
            throw new HttpException(`${id} Not found`, HttpStatus.NOT_FOUND);
        }

        if (exist.isCompleted) {
github jiayisheji / jianshu / libs / database / src / lib / crud.controller.ts View on Github external
import { ApiOperation, ApiResponse, ApiOkResponse, ApiForbiddenResponse, ApiCreatedResponse, ApiNotFoundResponse, ApiBadRequestResponse, ApiUnauthorizedResponse } from '@nestjs/swagger';
import { HttpStatus, Get, Param, HttpCode, Post, Body, Put, Delete, Query } from '@nestjs/common';
import { CrudRepositoryService, Paginator, PaginationParams } from './crud-repository.service';
import { FindAndModifyWriteOpResultObject } from 'mongodb';
import { Typegoose, InstanceType } from 'typegoose';

@ApiUnauthorizedResponse({ description: 'Unauthorized' })
@ApiForbiddenResponse({ description: 'Forbidden.' })
export abstract class CrudController  {

  constructor(private readonly crudService: CrudRepositoryService) { }

  @ApiOperation({ title: 'find all' })
  @ApiOkResponse({ description: 'Found records' })
  @Get()
  public async findAll(@Query() filter: PaginationParams>): Promise>> {
    return this.crudService.paginator(filter);
  }

  @ApiOperation({ title: 'Find by id' })
  @ApiOkResponse({ description: 'Found one record' })
  @ApiNotFoundResponse({ description: 'Record not found' })
  @Get(':id')
  public async findById(@Param('id') id: string): Promise> {
    return this.crudService.findOne(id);
  }

  @ApiOperation({ title: 'Create new record' })
  @ApiCreatedResponse({ description: 'The record has been successfully created.' })
  @ApiBadRequestResponse({ description: 'Invalid input, The response body may contain clues as to what went wrong' })
github jmcdo29 / zeldaPlay / src / server / app / zeldaplay / character / character.controller.ts View on Github external
import { DbCharacter, DbCharacterShort } from '@DbModel/index';
import { CharacterIdParam, UserIdParam } from '@Parameter/index';

@ApiUseTags('Character')
@Controller('character')
export class CharacterController {
  constructor(private readonly characterService: CharacterService) {}

  @Get()
  @ApiOperation({
    title: 'Get All Unassigned Characters',
    description:
      'Get all of the characters who do not belong to a user. ' +
      'These are returned and shown as an example for the user to get an idea of how the app works.'
  })
  @ApiOkResponse({ type: DbCharacterShort, isArray: true })
  getAll(): Observable {
    return this.characterService.getAll();
  }

  @Post('new/:userId')
  @ApiOperation({
    title: 'New Character',
    description:
      'Using the User id, create and assign a new character based on the incoming body'
  })
  @ApiImplicitBody({ name: 'character', type: CharacterDTO })
  @ApiImplicitParam({ name: 'userId', type: 'string', required: true })
  @UseGuards(AuthGuard)
  @ApiBearerAuth()
  @ApiOkResponse({ type: DbCharacter })
  newChar(
github jmcdo29 / zeldaPlay / src / server / app / zeldaplay / spell / spell.controller.ts View on Github external
@ApiOkResponse({ type: DbSpell })
  newSpell(
    @Body('spell', SpellPipe) inSpell: DbSpell,
    @Param() params: CharacterIdParam
  ): Observable {
    return this.spellService.newSpell(inSpell, params.charId);
  }

  @Patch('update/:spellId')
  @ApiOperation({
    title: 'Update Spell',
    description: 'Update an existing spell based on its id.'
  })
  @UseGuards(AuthGuard)
  @ApiBearerAuth()
  @ApiOkResponse({ type: DbSpell })
  @ApiImplicitParam({ name: 'spellId', required: true, type: 'string' })
  @ApiImplicitBody({ name: 'spell', type: SpellDTO })
  updateSpell(
    @Body('spell', SpellPipe) inSpell: DbSpell,
    @Param() params: SpellIdParam
  ): Observable {
    return this.spellService.updateSpell(inSpell);
  }
}
github NarHakobyan / awesome-nest-boilerplate / src / modules / auth / auth.controller.ts View on Github external
import { AuthService } from './auth.service';
import { LoginPayloadDto } from './dto/LoginPayloadDto';
import { UserLoginDto } from './dto/UserLoginDto';
import { UserRegisterDto } from './dto/UserRegisterDto';

@Controller('auth')
@ApiUseTags('auth')
export class AuthController {
    constructor(
        public readonly userService: UserService,
        public readonly authService: AuthService,
    ) {}

    @Post('login')
    @HttpCode(HttpStatus.OK)
    @ApiOkResponse({
        type: LoginPayloadDto,
        description: 'User info with access token',
    })
    async userLogin(
        @Body() userLoginDto: UserLoginDto,
    ): Promise {
        const userEntity = await this.authService.validateUser(userLoginDto);

        const token = await this.authService.createToken(userEntity);
        return new LoginPayloadDto(userEntity.toDto(), token);
    }

    @Post('register')
    @HttpCode(HttpStatus.OK)
    @ApiOkResponse({ type: UserDto, description: 'Successfully Registered' })
    @ApiImplicitFile({ name: 'avatar', required: true })
github jiayisheji / jianshu / apps / client / src / app / auth / auth.controller.ts View on Github external
@Ip() ip: string
  ) {
    this.logger.log(ip);
    return this.authService.login(user);
  }

  /**
   * 登出账号
   * @returns
   * @memberof AuthController
   */
  @Get('/logout')
  @UseGuards(AuthGuard('jwt'))
  @ApiOperation({ title: '登出' })
  @ApiBearerAuth()
  @ApiOkResponse({ description: '登出成功' })
  @ApiForbiddenResponse({ description: '未登录' })
  @HttpCode(HttpStatus.OK)
  public async logout(@CurrentUser() user: User) {
    this.logger.log(user);
    return {};
  }
}
github jmcdo29 / zeldaPlay / src / server / app / zeldaplay / skill / skill.controller.ts View on Github external
import { DbSkill } from '@DbModel/index';
import { CharacterIdParam } from '@Parameter/index';
import { SkillService } from '@Skill/skill.service';
import { Observable } from 'rxjs';

@ApiUseTags('Skill')
@Controller('character/skill')
export class SkillController {
  constructor(private readonly skillService: SkillService) {}

  @Get(':charId')
  @ApiOperation({
    title: 'get character skills',
    description: 'Get all the skills of the specified character.'
  })
  @ApiOkResponse({ type: DbSkill, isArray: true })
  getSkills(@Param() params: CharacterIdParam): Observable {
    return this.skillService.getCharacterSkills(params.charId);
  }
}
github jmcdo29 / zeldaPlay / src / server / app / auth / auth.controller.ts View on Github external
@Controller()
export class AuthController {
  constructor(private readonly authService: AuthService) {}

  @Post('login')
  @ApiOperation({ title: 'Login', description: 'Log the user in' })
  @ApiImplicitBody({ name: 'user', type: UserDTO })
  @ApiOkResponse({ type: JwtReturnDTO })
  login(@Body('user') user: UserDTO): Observable {
    return this.authService.login(user);
  }

  @Post('signup')
  @ApiOperation({ title: 'Signup', description: 'Sign the new user up' })
  @ApiImplicitBody({ name: 'user', type: NewUserDTO })
  @ApiOkResponse({ type: JwtReturnDTO })
  signup(@Body('user') user: NewUserDTO): Observable {
    return this.authService.signup(user);
  }

  @Post('logout')
  @ApiOperation({ title: 'Logout', description: 'Allow the user to log out.' })
  logout(): void {
    return;
  }
}
github jmcdo29 / zeldaPlay / src / server / app / zeldaplay / note / note.controller.ts View on Github external
description: 'Get all the notes of one character.'
  })
  @ApiImplicitParam({ name: 'charId', type: 'string', required: true })
  @ApiOkResponse({ type: DbNote, isArray: true })
  getNotes(@Param() params: CharacterIdParam): Observable {
    return this.noteService.getNotes(params.charId);
  }

  @Post(':charId')
  @ApiOperation({
    title: 'New Note',
    description: 'Make a new note tied to this character.'
  })
  @ApiBearerAuth()
  @UseGuards(AuthGuard)
  @ApiOkResponse({ type: DbNote })
  @ApiImplicitParam({ name: 'charId', type: 'string', required: true })
  @ApiImplicitBody({ name: 'note', type: NoteDTO })
  newNote(
    @Body('note', NotePipe) inNote: DbNote,
    @Param() params: CharacterIdParam
  ): Observable {
    return this.noteService.saveNote(inNote, params.charId);
  }
}