Skip to content

Commit

Permalink
feat: add sample folder
Browse files Browse the repository at this point in the history
  • Loading branch information
ojoanalogo committed Nov 6, 2020
1 parent ddcab89 commit ee5c96c
Show file tree
Hide file tree
Showing 10 changed files with 333 additions and 0 deletions.
7 changes: 7 additions & 0 deletions sample/app.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Module } from '@nestjs/common';
import { CatsModule } from './cats/cats.module';

@Module({
imports: [CatsModule],
})
export class AppModule {}
81 changes: 81 additions & 0 deletions sample/cats/cats.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { Body, Controller, Get, Param, Post, Query } from '@nestjs/common';
import {
ApiBearerAuth,
ApiConsumes,
ApiExtension,
ApiHeader,
ApiOperation,
ApiResponse,
ApiSecurity,
ApiTags,
} from '@nestjs/swagger';
import { CatsService } from './cats.service';
import { Cat } from './classes/cat.class';
import { CreateCatDto } from './dto/create-cat.dto';
import { PaginationQuery } from './dto/pagination-query.dto';

@ApiSecurity('basic')
@ApiBearerAuth()
@ApiTags('cats')
@ApiHeader({
name: 'header',
required: false,
description: 'Test',
schema: { default: 'test' },
})
@Controller('cats')
export class CatsController {
constructor(private readonly catsService: CatsService) {}

@ApiTags('create cats')
@Post()
@ApiOperation({ summary: 'Create cat' })
@ApiResponse({
status: 201,
description: 'The record has been successfully created.',
type: () => Cat,
})
@ApiResponse({ status: 403, description: 'Forbidden.' })
@ApiExtension('x-foo', { test: 'bar ' })
async create(@Body() createCatDto: CreateCatDto): Promise<Cat> {
return this.catsService.create(createCatDto);
}

@Get(':id')
@ApiResponse({
status: 200,
description: 'The found record',
type: Cat,
})
@ApiExtension('x-auth-type', 'NONE')
findOne(@Param('id') id: string): Cat {
return this.catsService.findOne(+id);
}

@Get()
findAll(@Query() paginationQuery: PaginationQuery) {}

@Get('bulk')
findAllBulk(@Query() paginationQuery: PaginationQuery[]) {}

@Post('bulk')
async createBulk(@Body() createCatDto: CreateCatDto[]): Promise<Cat> {
return null;
}

@ApiConsumes('application/x-www-form-urlencoded')
@Post('as-form-data')
@ApiOperation({ summary: 'Create cat' })
@ApiResponse({
status: 201,
description: 'The record has been successfully created.',
type: Cat,
})
@ApiResponse({ status: 403, description: 'Forbidden.' })
async createAsFormData(@Body() createCatDto: CreateCatDto): Promise<Cat> {
return this.catsService.create(createCatDto);
}

@Get('site*')
getSite() {}
}
9 changes: 9 additions & 0 deletions sample/cats/cats.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Module } from '@nestjs/common';
import { CatsController } from './cats.controller';
import { CatsService } from './cats.service';

@Module({
controllers: [CatsController],
providers: [CatsService],
})
export class CatsModule {}
17 changes: 17 additions & 0 deletions sample/cats/cats.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Injectable } from '@nestjs/common';
import { Cat } from './classes/cat.class';
import { CreateCatDto } from './dto/create-cat.dto';

@Injectable()
export class CatsService {
private readonly cats: Cat[] = [];

create(cat: CreateCatDto): Cat {
this.cats.push(cat);
return cat;
}

findOne(id: number): Cat {
return this.cats[id];
}
}
56 changes: 56 additions & 0 deletions sample/cats/classes/cat.class.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { ApiProperty } from '@nestjs/swagger';
import { LettersEnum } from '../dto/pagination-query.dto';

export class Cat {
@ApiProperty({ example: 'Kitty', description: 'The name of the Cat' })
name: string;

@ApiProperty({ example: 1, minimum: 0, description: 'The age of the Cat' })
age: number;

@ApiProperty({
example: 'Maine Coon',
description: 'The breed of the Cat',
})
breed: string;

@ApiProperty({
name: '_tags',
type: [String],
})
tags?: string[];

@ApiProperty()
createdAt: Date;

@ApiProperty({
type: String,
isArray: true,
})
urls?: string[];

@ApiProperty({
name: '_options',
type: 'array',
items: {
type: 'object',
properties: {
isReadonly: {
type: 'string',
},
},
},
})
options?: Record<string, any>[];

@ApiProperty({
enum: LettersEnum,
})
enum: LettersEnum;

@ApiProperty({
enum: LettersEnum,
isArray: true,
})
enumArr: LettersEnum;
}
64 changes: 64 additions & 0 deletions sample/cats/dto/create-cat.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { ApiExtraModels, ApiProperty } from '@nestjs/swagger';
import { ExtraModel } from './extra-model.dto';
import { LettersEnum } from './pagination-query.dto';
import { TagDto } from './tag.dto';

@ApiExtraModels(ExtraModel)
export class CreateCatDto {
@ApiProperty()
readonly name: string;

@ApiProperty({ minimum: 1, maximum: 200 })
readonly age: number;

@ApiProperty({ name: '_breed', type: String })
readonly breed: string;

@ApiProperty({
type: [String],
})
readonly tags?: string[];

@ApiProperty()
createdAt: Date;

@ApiProperty({
type: 'string',
isArray: true,
})
readonly urls?: string[];

@ApiProperty({
type: 'array',
items: {
type: 'object',
properties: {
isReadonly: {
type: 'string',
},
},
},
})
readonly options?: Record<string, any>[];

@ApiProperty({
enum: LettersEnum,
enumName: 'LettersEnum',
})
readonly enum: LettersEnum;

@ApiProperty({
enum: LettersEnum,
enumName: 'LettersEnum',
isArray: true,
})
readonly enumArr: LettersEnum;

@ApiProperty({ description: 'tag', required: false })
readonly tag: TagDto;

nested: {
first: string;
second: number;
};
}
9 changes: 9 additions & 0 deletions sample/cats/dto/extra-model.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { ApiProperty } from '@nestjs/swagger';

export class ExtraModel {
@ApiProperty()
readonly one: string;

@ApiProperty()
readonly two: number;
}
42 changes: 42 additions & 0 deletions sample/cats/dto/pagination-query.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { ApiProperty } from '@nestjs/swagger';

export enum LettersEnum {
A = 'A',
B = 'B',
C = 'C',
}

export class PaginationQuery {
@ApiProperty({
minimum: 0,
maximum: 10000,
title: 'Page',
exclusiveMaximum: true,
exclusiveMinimum: true,
format: 'int32',
default: 0,
})
page: number;

@ApiProperty({
name: '_sortBy',
type: [String],
})
sortBy: string[];

@ApiProperty()
limit: number;

@ApiProperty({
enum: LettersEnum,
enumName: 'LettersEnum',
})
enum: LettersEnum;

@ApiProperty({
enum: LettersEnum,
enumName: 'LettersEnum',
isArray: true,
})
enumArr: LettersEnum;
}
6 changes: 6 additions & 0 deletions sample/cats/dto/tag.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { ApiProperty } from '@nestjs/swagger';

export class TagDto {
@ApiProperty({ description: 'name' })
name: string;
}
42 changes: 42 additions & 0 deletions sample/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { NestFactory } from '@nestjs/core';
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
import { RedocModule, RedocOptions } from '../src';
import { AppModule } from './app.module';

async function bootstrap() {
const app = await NestFactory.create(AppModule);
const options = new DocumentBuilder()
.setTitle('Cats example')
.setDescription('The cats API description')
.setVersion('1.0')
.addTag('cats')
.addBasicAuth()
.addBearerAuth()
.addOAuth2()
.addApiKey()
.addCookieAuth()
.addSecurityRequirements('bearer')
.build();
const document = SwaggerModule.createDocument(app, options);
const redocOptions: RedocOptions = {
title: 'Redoc Module',
logo: {
url: 'https://redocly.github.io/redoc/petstore-logo.png',
backgroundColor: '#F0F0F0',
altText: 'Bonusami Logo',
},
sortPropsAlphabetically: true,
hideDownloadButton: false,
hideHostname: false,
noAutoAuth: true,
pathInMiddlePanel: true,
auth: {
enabled: true,
user: 'admin',
password: '123',
},
};
await RedocModule.setup('docs', app, document, redocOptions);
await app.listen(8000);
}
bootstrap();

0 comments on commit ee5c96c

Please sign in to comment.