@nestjs/swagger#ApiExtraModels TypeScript Examples

The following examples show how to use @nestjs/swagger#ApiExtraModels. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: home.controller.ts    From nest-js-boilerplate with MIT License 7 votes vote down vote up
@ApiExtraModels(User)
@Controller()
export default class HomeController {
  @ApiCookieAuth()
  @ApiOkResponse({
    schema: {
      type: 'object',
      properties: {
        data: {
          $ref: getSchemaPath(User),
        },
      },
    },
    description: 'Returns the logged user',
  })
  @ApiUnauthorizedResponse({
    schema: {
      type: 'object',
      example: {
        message: 'string',
      },
    },
    description: 'Returns the unauthorized error',
  })
  @UseGuards(IsLoggedGuard)
  @Get('/')
  @Render('home')
  public getIndex(@RequestUser() user: User): User {
    return user;
  }
}
Example #2
Source File: home.controller.ts    From nest-js-boilerplate with MIT License 6 votes vote down vote up
@ApiExtraModels(UserEntity)
@Controller()
export default class HomeController {
  @ApiCookieAuth()
  @ApiOkResponse({
    schema: {
      type: 'object',
      properties: {
        data: {
          $ref: getSchemaPath(UserEntity),
        },
      },
    },
    description: 'Returns the logged user',
  })
  @ApiUnauthorizedResponse({
    schema: {
      type: 'object',
      example: {
        message: 'string',
      },
    },
    description: 'Returns the unauthorized error',
  })
  @UseGuards(IsLoggedGuard)
  @Get('/')
  @Render('home')
  public getIndex(@RequestUser() user: UserEntity): UserEntity {
    return user;
  }
}
Example #3
Source File: api-pagination.response.ts    From nest-js-quiz-manager with MIT License 6 votes vote down vote up
ApiPaginatedResponse = <TModel extends Type<any>>(
  options: IPaginatedDecoratorApiResponse,
) => {
  return applyDecorators(
    ApiExtraModels(PaginatedDto),
    ApiOkResponse({
      description: options.description || 'Successfully received model list',
      schema: {
        allOf: [
          { $ref: getSchemaPath(PaginatedDto) },
          {
            properties: {
              items: {
                type: 'array',
                items: { $ref: getSchemaPath(options.model) },
              },
              meta: {
                type: 'any',
                default: {
                  totalItems: 2,
                  itemCount: 2,
                  itemsPerPage: 2,
                  totalPages: 1,
                  currentPage: 1,
                },
              },
            },
          },
        ],
      },
    }),
  );
}
Example #4
Source File: api-nested-query.decorator.ts    From amplication with Apache License 2.0 6 votes vote down vote up
// eslint-disable-next-line @typescript-eslint/ban-types,@typescript-eslint/explicit-module-boundary-types,@typescript-eslint/naming-convention
export function ApiNestedQuery(query: Function) {
  const constructor = query.prototype;
  const properties = Reflect.getMetadata(
    "swagger/apiModelPropertiesArray",
    constructor
  ).map((prop: any) => prop.slice(1));

  const decorators = properties
    .map((property: any) => {
      const { required, isArray } = Reflect.getMetadata(
        "swagger/apiModelProperties",
        constructor,
        property
      );
      const propertyType = Reflect.getMetadata(
        "design:type",
        constructor,
        property
      );
      const typedQuery = generateApiQueryObject(
        property,
        propertyType,
        required,
        isArray
      );
      return [ApiExtraModels(propertyType), ApiQuery(typedQuery)];
    })
    .flat();

  return applyDecorators(...decorators);
}