From a65e3cd53ca2821eee5b45d8428837c5b9f6a799 Mon Sep 17 00:00:00 2001 From: vigdorov Date: Thu, 2 Jul 2020 21:56:52 +0300 Subject: [PATCH] swager documentation --- src/main.ts | 10 +++++----- src/store/store.controller.ts | 20 ++++++++++++++++++-- src/store/store.schema.ts | 19 ++++++++++++++++++- src/store/store.service.ts | 4 ++-- 4 files changed, 43 insertions(+), 10 deletions(-) diff --git a/src/main.ts b/src/main.ts index 2444a65..7975d47 100644 --- a/src/main.ts +++ b/src/main.ts @@ -6,11 +6,11 @@ async function bootstrap() { const app = await NestFactory.create(AppModule); const options = new DocumentBuilder() - .setTitle('Store API') - .setDescription('API работает по принципу LocalStorage в браузере.') - .setVersion('1.0.0') - .addTag('store') - .build(); + .setTitle('Store API') + .setDescription('API работает по принципу LocalStorage в браузере.') + .setVersion('1.0.0') + .addTag('store') + .build(); const document = SwaggerModule.createDocument(app, options); SwaggerModule.setup('api', app, document); diff --git a/src/store/store.controller.ts b/src/store/store.controller.ts index 913ad85..3f8dee5 100644 --- a/src/store/store.controller.ts +++ b/src/store/store.controller.ts @@ -1,6 +1,7 @@ import { Controller, Get, Post, Body, Param } from '@nestjs/common'; import {StoreService} from './store.service'; -import {Store, StoreParams} from './store.schema'; +import {Store, StoreResponse, StoreRequest} from './store.schema'; +import {ApiResponse} from '@nestjs/swagger'; @Controller('store') export class StoreController { @@ -9,17 +10,32 @@ export class StoreController { ) {} @Get() + @ApiResponse({ + status: 200, + description: 'Список всех пар ключ-значение', + type: [StoreResponse], + }) async findAll(): Promise { return this.storeService.findAll(); } @Get(':key?') + @ApiResponse({ + status: 200, + description: 'Возвращает пару ключ-значение по ключу', + type: StoreResponse, + }) async findOne(@Param() {key}: {key: string}): Promise { return this.storeService.findOne(key); } @Post() - async create(@Body() createStoreClass: StoreParams): Promise { + @ApiResponse({ + status: 200, + description: 'Создает новую пару ключ-значение или заменяет существующую по ключу', + type: StoreResponse, + }) + async create(@Body() createStoreClass: StoreRequest): Promise { return this.storeService.create(createStoreClass); } } diff --git a/src/store/store.schema.ts b/src/store/store.schema.ts index 96d8d69..3a72d6b 100644 --- a/src/store/store.schema.ts +++ b/src/store/store.schema.ts @@ -1,10 +1,27 @@ import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose'; import { Document } from 'mongoose'; +import {ApiProperty} from '@nestjs/swagger'; -export class StoreParams { +export class StoreRequest { + @ApiProperty() key: string; + + @ApiProperty() value: any; } +export class StoreResponse { + @ApiProperty() + key: string; + + @ApiProperty() + value: any; + + @ApiProperty() + _id: string; + + @ApiProperty() + __v: number; +} @Schema() export class Store extends Document { diff --git a/src/store/store.service.ts b/src/store/store.service.ts index cda7d36..e2b5977 100644 --- a/src/store/store.service.ts +++ b/src/store/store.service.ts @@ -1,7 +1,7 @@ import {Model} from 'mongoose'; import {Injectable} from '@nestjs/common'; import {InjectModel} from '@nestjs/mongoose'; -import {Store, StoreParams} from './store.schema'; +import {Store, StoreRequest} from './store.schema'; @Injectable() @@ -12,7 +12,7 @@ export class StoreService { return this.storeModel.find().exec(); } - async create(store: StoreParams): Promise { + async create(store: StoreRequest): Promise { const searchStore = await this.findOne(store.key); if (searchStore) {