Skip to content

Commit

Permalink
examples(@nestjs) add swagger example
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilmysliwiec committed Nov 25, 2017
1 parent 243c83d commit 7e9cca0
Show file tree
Hide file tree
Showing 20 changed files with 274 additions and 12 deletions.
21 changes: 21 additions & 0 deletions examples/11-swagger/.gitignore
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
14 changes: 14 additions & 0 deletions examples/11-swagger/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
require('ts-node/register');
require('./src/server');












14 changes: 14 additions & 0 deletions examples/11-swagger/jest.json
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"]
}
38 changes: 38 additions & 0 deletions examples/11-swagger/package.json
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"
}
}
8 changes: 8 additions & 0 deletions examples/11-swagger/src/modules/app.module.ts
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 {}
25 changes: 25 additions & 0 deletions examples/11-swagger/src/modules/cats/cats.controller.ts
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);
}
}
9 changes: 9 additions & 0 deletions examples/11-swagger/src/modules/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],
components: [CatsService],
})
export class CatsModule {}
16 changes: 16 additions & 0 deletions examples/11-swagger/src/modules/cats/cats.service.ts
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 examples/11-swagger/src/modules/cats/dto/create-cat.dto.ts
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;
}
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;
}
20 changes: 20 additions & 0 deletions examples/11-swagger/src/server.ts
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();
22 changes: 22 additions & 0 deletions examples/11-swagger/tsconfig.json
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"
]
}
54 changes: 54 additions & 0 deletions examples/11-swagger/tslint.json
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": []
}
2 changes: 2 additions & 0 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,7 @@ gulp.task('move', function() {
gulp.dest('examples/08-passport/node_modules/@nestjs')
).pipe(
gulp.dest('examples/09-babel-example/node_modules/@nestjs')
).pipe(
gulp.dest('examples/11-swagger/node_modules/@nestjs')
);
});
5 changes: 2 additions & 3 deletions lib/common/interfaces/nest-application.interface.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,11 @@ export interface INestApplication {
init(): Promise<void>;
/**
* The wrapper function around native `express.use()` method.
* Example `app.use(bodyParser.json())`
* Example `app.use(cors())`
*
* @param {} requestHandler Express Request Handler
* @returns void
*/
use(requestHandler: any): void;
use(...args: any[]): void;
/**
* Starts the application.
*
Expand Down
2 changes: 1 addition & 1 deletion lib/core/nest-application.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export declare class NestApplication implements INestApplication {
getMicroservices(): INestMicroservice[];
startAllMicroservices(callback?: () => void): void;
startAllMicroservicesAsync(): Promise<void>;
use(requestHandler: any): void;
use(...args: any[]): void;
listen(port: number, callback?: () => void): any;
listen(port: number, hostname: string, callback?: () => void): any;
listenAsync(port: number, hostname?: string): Promise<any>;
Expand Down
4 changes: 2 additions & 2 deletions lib/core/nest-application.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ class NestApplication {
startAllMicroservicesAsync() {
return new Promise((resolve) => this.startAllMicroservices(resolve));
}
use(requestHandler) {
this.express.use(requestHandler);
use(...args) {
this.express.use(...args);
}
listen(port, ...args) {
return __awaiter(this, void 0, void 0, function* () {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nestjs",
"version": "4.4.1",
"version": "4.4.2",
"description": "Modern, fast, powerful node.js web framework",
"main": "index.js",
"scripts": {
Expand Down
5 changes: 2 additions & 3 deletions src/common/interfaces/nest-application.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,11 @@ export interface INestApplication {

/**
* The wrapper function around native `express.use()` method.
* Example `app.use(bodyParser.json())`
* Example `app.use(cors())`
*
* @param {} requestHandler Express Request Handler
* @returns void
*/
use(requestHandler): void;
use(...args): void;

/**
* Starts the application.
Expand Down
4 changes: 2 additions & 2 deletions src/core/nest-application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ export class NestApplication implements INestApplication {
return new Promise((resolve) => this.startAllMicroservices(resolve));
}

public use(requestHandler) {
this.express.use(requestHandler);
public use(...args) {
this.express.use(...args);
}

public async listen(port: number, callback?: () => void);
Expand Down

0 comments on commit 7e9cca0

Please sign in to comment.