- Fixed server

This commit is contained in:
Ferdi Koomen 2020-09-24 17:30:25 +02:00
parent 4834c4f8af
commit d76cac7348
17 changed files with 262 additions and 68 deletions

View File

@ -53,10 +53,10 @@
"test:update": "jest --updateSnapshot",
"test:watch": "jest --watch",
"test:coverage": "jest --coverage",
"eslint": "eslint \"./src/**/*.{js,ts}\" \"./test/**/*.{js,ts}\" \"./bin/index.js\"",
"eslint:fix": "eslint \"./src/**/*.{js,ts}\" \"./test/**/*.{js,ts}\" \"./bin/index.js\" --fix",
"prettier": "prettier \"./src/**/*.{js,ts}\" \"./test/**/*.{js,ts}\" \"./bin/index.js\" --check",
"prettier:fix": "prettier \"./src/**/*.{js,ts}\" \"./test/**/*.{js,ts}\" \"./bin/index.js\" --write",
"eslint": "eslint \"./src/**/*.ts\" \"./test/**/*.ts\" \"./bin/index.js\"",
"eslint:fix": "eslint \"./src/**/*.ts\" \"./test/**/*.ts\" \"./bin/index.js\" --fix",
"prettier": "prettier \"./src/**/*.ts\" \"./test/**/*.ts\" \"./bin/index.js\" --check",
"prettier:fix": "prettier \"./src/**/*.ts\" \"./test/**/*.ts\" \"./bin/index.js\" --write",
"prepublish": "yarn run clean && yarn run release",
"codecov": "codecov --token=66c30c23-8954-4892-bef9-fbaed0a2e42b"
},

View File

@ -1,12 +1,11 @@
import { Module } from '@nestjs/common';
import { DefaultsController } from './controllers/DefaultsController';
import { ParametersController } from './controllers/ParametersController';
import { ResponseController } from './controllers/ResponseController';
import { SimpleController } from './controllers/SimpleController';
@Module({
controllers: [SimpleController, ParametersController, DefaultsController, ResponseController],
controllers: [SimpleController, ParametersController, ResponseController],
})
export class AppModule {
//

View File

@ -1,20 +0,0 @@
import { Controller, Get, Param } from '@nestjs/common';
import { ApiResponse, ApiTags } from '@nestjs/swagger';
import { ModelWithString } from '../models/ModelWithString';
/* eslint-disable @typescript-eslint/no-unused-vars */
@ApiTags('defaults')
@Controller('defaults')
export class DefaultsController {
@Get('monkey')
@ApiResponse({
status: 200,
type: ModelWithString,
})
monkey(@Param('id') id: string): ModelWithString {
return {
prop: 'Hello World!',
};
}
}

View File

@ -1,4 +1,4 @@
import { Controller, Get, Param } from '@nestjs/common';
import { Body, Controller, Get, Param, Post } from '@nestjs/common';
import { ApiResponse, ApiTags } from '@nestjs/swagger';
import { ModelWithString } from '../models/ModelWithString';
@ -7,14 +7,72 @@ import { ModelWithString } from '../models/ModelWithString';
@ApiTags('parameters')
@Controller('parameters')
export class ParametersController {
@Get('monkey')
@Get('callWithParameters')
public callWithParameters(
@Param('parameterString') parameterString: string,
@Param('parameterNumber') parameterNumber: number,
@Param('parameterBoolean') parameterBoolean: boolean,
@Param('parameterDictionary') parameterDictionary: Record<string, string>
): any {
return {
parameterString,
parameterNumber,
parameterBoolean,
parameterDictionary,
};
}
@Get('callWithWeirdParameterNames')
public callWithWeirdParameterNames(
@Param('parameter.1') parameter1: string,
@Param('parameter-2') parameter2: string,
@Param('parameter_3') parameter3: string,
@Param('PARAMETER.4') parameter4: string,
@Param('PARAMETER+5') parameter5: string,
@Param('PARAMETER&6') parameter6: string
): any {
return {
parameter1,
parameter2,
parameter3,
parameter4,
parameter5,
parameter6,
};
}
@Get('callWithDefaultParameters')
@ApiResponse({
status: 200,
type: ModelWithString,
})
monkey(@Param('id') id: string): ModelWithString {
public callWithDefaultParameters(
@Param('parameterNumberWithDefault') parameterNumberWithDefault: number = 123,
@Param('parameterBooleanWithDefault') parameterBooleanWithDefault: boolean = true,
@Param('parameterStringWithDefault') parameterStringWithDefault: string = 'Hello World!'
): any {
return {
prop: 'Hello World!',
parameterNumberWithDefault,
parameterBooleanWithDefault,
parameterStringWithDefault,
};
}
@Get('getCallWithBody')
@ApiResponse({
status: 200,
type: ModelWithString,
})
public getCallWithBody(@Body() body: ModelWithString): ModelWithString {
return body;
}
@Post('postCallWithBody')
@ApiResponse({
status: 200,
type: ModelWithString,
})
public postCallWithBody(@Body() body: ModelWithString): ModelWithString {
return body;
}
}

View File

@ -1,20 +1,129 @@
import { Controller, Get, Param } from '@nestjs/common';
import { Controller, Get } from '@nestjs/common';
import { ApiResponse, ApiTags } from '@nestjs/swagger';
import { ModelThatExtends } from '../models/ModelThatExtends';
import { ModelThatExtendsExtends } from '../models/ModelThatExtendsExtends';
import { ModelWithArray } from '../models/ModelWithArray';
import { ModelWithBoolean } from '../models/ModelWithBoolean';
import { ModelWithDictionary } from '../models/ModelWithDictionary';
import { ModelWithInteger } from '../models/ModelWithInteger';
import { ModelWithProperties } from '../models/ModelWithProperties';
import { ModelWithReference } from '../models/ModelWithReference';
import { ModelWithString } from '../models/ModelWithString';
/* eslint-disable @typescript-eslint/no-unused-vars */
@ApiTags('response')
@Controller('response')
export class ResponseController {
@Get('monkey')
@ApiResponse({
status: 200,
type: ModelWithString,
})
monkey(@Param('id') id: string): ModelWithString {
@Get('getString')
@ApiResponse({ status: 200, type: String })
public getString(): string {
return 'Hello World!';
}
@Get('getNumber')
@ApiResponse({ status: 200, type: Number })
public getNumber(): number {
return 123;
}
@Get('getBoolean')
@ApiResponse({ status: 200, type: Boolean })
public getBoolean(): boolean {
return true;
}
@Get('getModelWithString')
@ApiResponse({ status: 200, type: ModelWithString })
public getModelWithString(): ModelWithString {
return {
prop: 'Hello World!',
};
}
@Get('getModelWithInteger')
@ApiResponse({ status: 200, type: ModelWithInteger })
public getModelWithInteger(): ModelWithInteger {
return {
prop: 123,
};
}
@Get('getModelWithBoolean')
@ApiResponse({ status: 200, type: ModelWithBoolean })
public getModelWithBoolean(): ModelWithBoolean {
return {
prop: true,
};
}
@Get('getModelWithArray')
@ApiResponse({ status: 200, type: ModelWithArray })
public getModelWithArray(): ModelWithArray {
return {
prop: ['foo', 'bar'],
};
}
@Get('getModelWithDictionary')
@ApiResponse({ status: 200, type: ModelWithDictionary })
public getModelWithDictionary(): ModelWithDictionary {
return {
prop: {
foo: 'bar',
},
};
}
@Get('getModelWithReference')
@ApiResponse({ status: 200, type: ModelWithReference })
public getModelWithReference(): ModelWithReference {
return {
prop: {
prop: 'Hello World!',
},
};
}
@Get('getModelWithProperties')
@ApiResponse({ status: 200, type: ModelWithProperties })
public getModelWithProperties(): ModelWithProperties {
return {
string: 'Hello World!',
number: 123,
boolean: true,
array: ['foo', 'bar'],
dictionary: {
foo: 'bar',
},
};
}
@Get('getModelThatExtends')
@ApiResponse({ status: 200, type: ModelThatExtends })
public getModelThatExtends(): ModelThatExtends {
return {
prop: 'prop',
propertyA: 'propertyA',
propertyB: {
prop: 'propertyB',
},
};
}
@Get('getModelThatExtendsExtends')
@ApiResponse({ status: 200, type: ModelThatExtendsExtends })
public getModelThatExtendsExtends(): ModelThatExtendsExtends {
return {
prop: 'prop',
propertyA: 'propertyA',
propertyB: {
prop: 'propertyB',
},
propertyC: 'propertyC',
propertyD: {
prop: 'propertyD',
},
};
}
}

View File

@ -1,20 +1,42 @@
import { Controller, Get, Param } from '@nestjs/common';
import { ApiResponse, ApiTags } from '@nestjs/swagger';
import { ModelWithString } from '../models/ModelWithString';
import { Controller, Delete, Get, Head, Options, Patch, Post, Put } from '@nestjs/common';
import { ApiTags } from '@nestjs/swagger';
/* eslint-disable @typescript-eslint/no-unused-vars */
@ApiTags('simple')
@Controller('simple')
export class SimpleController {
@Get('monkey')
@ApiResponse({
status: 200,
type: ModelWithString,
})
monkey(@Param('id') id: string): ModelWithString {
return {
prop: 'Hello World!',
};
@Get('getCallWithoutParametersAndResponse')
public getCallWithoutParametersAndResponse(): void {
//
}
@Put('putCallWithoutParametersAndResponse')
public putCallWithoutParametersAndResponse(): void {
//
}
@Post('postCallWithoutParametersAndResponse')
public postCallWithoutParametersAndResponse(): void {
//
}
@Delete('deleteCallWithoutParametersAndResponse')
public deleteCallWithoutParametersAndResponse(): void {
//
}
@Options('optionsCallWithoutParametersAndResponse')
public optionsCallWithoutParametersAndResponse(): void {
//
}
@Head('headCallWithoutParametersAndResponse')
public headCallWithoutParametersAndResponse(): void {
//
}
@Patch('patchCallWithoutParametersAndResponse')
public patchCallWithoutParametersAndResponse(): void {
//
}
}

View File

@ -3,10 +3,17 @@ import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
import { AppModule } from './AppModule';
/**
* This is a simple OpenAPI test server that we use to run e2e tests. You can find
* more information inside the controllers and models of this server.
* When you run this server the following urls are available:
* - Swagger UI: http://localhost:3000/api
* - Swagger Specification: http://localhost:3000/api-json
*/
async function bootstrap(): Promise<void> {
const app = await NestFactory.create(AppModule);
const options = new DocumentBuilder().setTitle('OpenAPI').setDescription('The OpenAPI description').setVersion('1.0').build();
const options = new DocumentBuilder().setTitle('OpenAPI').setVersion('1.0').build();
const document = SwaggerModule.createDocument(app, options);

View File

@ -1,6 +1,11 @@
import { ApiProperty } from '@nestjs/swagger';
export class ModelThatExtends {
import { ModelWithString } from './ModelWithString';
export class ModelThatExtends extends ModelWithString {
@ApiProperty()
prop?: number;
public readonly propertyA?: string;
@ApiProperty()
public readonly propertyB?: ModelWithString;
}

View File

@ -1,6 +1,12 @@
import { ApiProperty } from '@nestjs/swagger';
export class ModelThatExtendsExtends {
import { ModelThatExtends } from './ModelThatExtends';
import { ModelWithString } from './ModelWithString';
export class ModelThatExtendsExtends extends ModelThatExtends {
@ApiProperty()
prop?: number;
public readonly propertyC?: string;
@ApiProperty()
public readonly propertyD?: ModelWithString;
}

View File

@ -2,5 +2,5 @@ import { ApiProperty } from '@nestjs/swagger';
export class ModelWithArray {
@ApiProperty()
prop?: number;
public readonly prop?: string[];
}

View File

@ -2,5 +2,5 @@ import { ApiProperty } from '@nestjs/swagger';
export class ModelWithBoolean {
@ApiProperty()
prop?: number;
public readonly prop?: boolean;
}

View File

@ -2,5 +2,5 @@ import { ApiProperty } from '@nestjs/swagger';
export class ModelWithDictionary {
@ApiProperty()
prop?: number;
public readonly prop?: Record<string, string>;
}

View File

@ -2,5 +2,5 @@ import { ApiProperty } from '@nestjs/swagger';
export class ModelWithInteger {
@ApiProperty()
prop?: number;
public readonly prop?: number;
}

View File

@ -1,6 +0,0 @@
import { ApiProperty } from '@nestjs/swagger';
export class ModelWithPattern {
@ApiProperty()
prop?: number;
}

View File

@ -2,5 +2,17 @@ import { ApiProperty } from '@nestjs/swagger';
export class ModelWithProperties {
@ApiProperty()
prop?: number;
public readonly number?: number;
@ApiProperty()
public readonly string?: string;
@ApiProperty()
public readonly boolean?: boolean;
@ApiProperty()
public readonly array?: string[];
@ApiProperty()
public readonly dictionary?: Record<string, string>;
}

View File

@ -1,6 +1,8 @@
import { ApiProperty } from '@nestjs/swagger';
import { ModelWithString } from './ModelWithString';
export class ModelWithReference {
@ApiProperty()
prop?: number;
public readonly prop?: ModelWithString;
}

View File

@ -2,5 +2,5 @@ import { ApiProperty } from '@nestjs/swagger';
export class ModelWithString {
@ApiProperty()
prop?: string;
public readonly prop?: string;
}