-
-
Notifications
You must be signed in to change notification settings - Fork 7.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
examples(@nestjs) add swagger example
- Loading branch information
1 parent
243c83d
commit 7e9cca0
Showing
20 changed files
with
274 additions
and
12 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,21 @@ | ||
# dependencies | ||
/node_modules | ||
|
||
# IDE | ||
/.idea | ||
/.awcache | ||
/.vscode | ||
|
||
# misc | ||
npm-debug.log | ||
|
||
# example | ||
/quick-start | ||
|
||
# tests | ||
/test | ||
/coverage | ||
/.nyc_output | ||
|
||
# dist | ||
/dist |
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,14 @@ | ||
require('ts-node/register'); | ||
require('./src/server'); | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
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,14 @@ | ||
{ | ||
"moduleFileExtensions": [ | ||
"ts", | ||
"tsx", | ||
"js", | ||
"json" | ||
], | ||
"transform": { | ||
"^.+\\.tsx?$": "<rootDir>/node_modules/ts-jest/preprocessor.js" | ||
}, | ||
"testRegex": "/src/.*\\.(test|spec).(ts|tsx|js)$", | ||
"collectCoverageFrom" : ["src/**/*.{js,jsx,tsx,ts}", "!**/node_modules/**", "!**/vendor/**"], | ||
"coverageReporters": ["json", "lcov"] | ||
} |
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,38 @@ | ||
{ | ||
"name": "nest-typescript-starter", | ||
"version": "1.0.0", | ||
"description": "Nest TypeScript starter repository", | ||
"license": "MIT", | ||
"scripts": { | ||
"start": "node index.js", | ||
"prestart:prod": "tsc", | ||
"start:prod": "node dist/server.js", | ||
"test": "jest --config=jest.json", | ||
"test:watch": "jest --watch --config=jest.json", | ||
"test:coverage": "jest --config=jest.json --coverage --coverageDirectory=coverage", | ||
"e2e": "jest --config=e2e/jest-e2e.json --forceExit", | ||
"e2e:watch": "jest --watch --config=e2e/jest-e2e.json" | ||
}, | ||
"dependencies": { | ||
"@nestjs/common": "^4.4.2", | ||
"@nestjs/core": "^4.4.2", | ||
"@nestjs/microservices": "^4.3.0", | ||
"@nestjs/swagger": "^1.1.2", | ||
"@nestjs/testing": "^4.3.0", | ||
"@nestjs/websockets": "^4.3.0", | ||
"class-transformer": "^0.1.7", | ||
"class-validator": "^0.7.2", | ||
"redis": "^2.7.1", | ||
"reflect-metadata": "^0.1.10", | ||
"rxjs": "^5.4.3", | ||
"typescript": "^2.4.2" | ||
}, | ||
"devDependencies": { | ||
"@types/jest": "^20.0.8", | ||
"@types/node": "^7.0.41", | ||
"jest": "^20.0.4", | ||
"supertest": "^3.0.0", | ||
"ts-jest": "^20.0.14", | ||
"ts-node": "^3.3.0" | ||
} | ||
} |
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,8 @@ | ||
import { Module, NestModule, MiddlewaresConsumer } from '@nestjs/common'; | ||
import { CatsModule } from './cats/cats.module'; | ||
import { CatsController } from './cats/cats.controller'; | ||
|
||
@Module({ | ||
modules: [CatsModule], | ||
}) | ||
export class ApplicationModule {} |
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,25 @@ | ||
import { Controller, Get, Post, Body, Param } from '@nestjs/common'; | ||
import { CreateCatDto } from './dto/create-cat.dto'; | ||
import { CatsService } from './cats.service'; | ||
import { Cat } from './interfaces/cat.interface'; | ||
import { ApiUseTags, ApiBearerAuth, ApiResponse, ApiOperation } from '@nestjs/swagger'; | ||
|
||
@ApiBearerAuth() | ||
@ApiUseTags('cats') | ||
@Controller('cats') | ||
export class CatsController { | ||
constructor(private readonly catsService: CatsService) {} | ||
|
||
@Post() | ||
@ApiOperation({ title: 'Create cat' }) | ||
@ApiResponse({ status: 201, description: 'The record has been successfully created.'}) | ||
@ApiResponse({ status: 403, description: 'Forbidden.'}) | ||
async create(@Body() createCatDto: CreateCatDto) { | ||
this.catsService.create(createCatDto); | ||
} | ||
|
||
@Get(':id') | ||
findOne(@Param('id') id: string): Cat { | ||
return this.catsService.findOne(+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,9 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { CatsController } from './cats.controller'; | ||
import { CatsService } from './cats.service'; | ||
|
||
@Module({ | ||
controllers: [CatsController], | ||
components: [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,16 @@ | ||
import { Component } from '@nestjs/common'; | ||
import { Cat } from './interfaces/cat.interface'; | ||
import { CatsModule } from './cats.module'; | ||
|
||
@Component() | ||
export class CatsService { | ||
private readonly cats: Cat[] = []; | ||
|
||
create(cat: Cat) { | ||
this.cats.push(cat); | ||
} | ||
|
||
findOne(id: number): Cat { | ||
return this.cats[id]; | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
examples/11-swagger/src/modules/cats/dto/create-cat.dto.ts
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,16 @@ | ||
import { IsString, IsInt } from 'class-validator'; | ||
import { ApiModelProperty } from '@nestjs/swagger'; | ||
|
||
export class CreateCatDto { | ||
@ApiModelProperty({ type: String }) | ||
@IsString() | ||
readonly name; | ||
|
||
@ApiModelProperty({ type: Number }) | ||
@IsInt() | ||
readonly age; | ||
|
||
@ApiModelProperty({ type: String }) | ||
@IsString() | ||
readonly breed; | ||
} |
5 changes: 5 additions & 0 deletions
5
examples/11-swagger/src/modules/cats/interfaces/cat.interface.ts
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,5 @@ | ||
export interface Cat { | ||
readonly name: string; | ||
readonly age: number; | ||
readonly breed: 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,20 @@ | ||
import { NestFactory } from '@nestjs/core'; | ||
import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger'; | ||
import { ApplicationModule } from './modules/app.module'; | ||
|
||
async function bootstrap() { | ||
const app = await NestFactory.create(ApplicationModule); | ||
|
||
const options = new DocumentBuilder() | ||
.setTitle('Cats example') | ||
.setDescription('The cats API description') | ||
.setVersion('1.0') | ||
.addTag('cats') | ||
.addBearerAuth() | ||
.build(); | ||
const document = SwaggerModule.createDocument(app, options); | ||
SwaggerModule.setup('/api', app, document); | ||
|
||
await app.listen(3001); | ||
} | ||
bootstrap(); |
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,22 @@ | ||
{ | ||
"compilerOptions": { | ||
"module": "commonjs", | ||
"declaration": false, | ||
"noImplicitAny": false, | ||
"removeComments": true, | ||
"noLib": false, | ||
"emitDecoratorMetadata": true, | ||
"experimentalDecorators": true, | ||
"target": "es6", | ||
"sourceMap": true, | ||
"allowJs": true, | ||
"outDir": "./dist" | ||
}, | ||
"include": [ | ||
"src/**/*" | ||
], | ||
"exclude": [ | ||
"node_modules", | ||
"**/*.spec.ts" | ||
] | ||
} |
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,54 @@ | ||
{ | ||
"defaultSeverity": "error", | ||
"extends": [ | ||
"tslint:recommended" | ||
], | ||
"jsRules": { | ||
"no-unused-expression": true | ||
}, | ||
"rules": { | ||
"eofline": false, | ||
"indent": false, | ||
"quotemark": [ | ||
true, | ||
"single" | ||
], | ||
"ordered-imports": [ | ||
false | ||
], | ||
"max-line-length": [ | ||
150 | ||
], | ||
"member-ordering": [ | ||
false | ||
], | ||
"curly": false, | ||
"interface-name": [ | ||
false | ||
], | ||
"array-type": [ | ||
false | ||
], | ||
"member-access": [ | ||
false | ||
], | ||
"no-empty-interface": false, | ||
"no-empty": false, | ||
"arrow-parens": false, | ||
"object-literal-sort-keys": false, | ||
"no-unused-expression": false, | ||
"max-classes-per-file": [ | ||
false | ||
], | ||
"variable-name": [ | ||
false | ||
], | ||
"one-line": [ | ||
false | ||
], | ||
"one-variable-per-declaration": [ | ||
false | ||
] | ||
}, | ||
"rulesDirectory": [] | ||
} |
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
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
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
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
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
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
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