forked from ojoanalogo/nestjs-redoc
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ddcab89
commit ee5c96c
Showing
10 changed files
with
333 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |