HM-45. Добавлена возможность использования тестового api с помощью заголовка
This commit is contained in:
@ -1,8 +1,9 @@
|
||||
import { Controller, Get, Post, Body, Param, Options, Header, Delete, HttpCode, Put } from '@nestjs/common';
|
||||
import {Controller, Get, Req, Post, Options, Header, Delete, HttpCode, Put} from '@nestjs/common';
|
||||
import {StoreService} from './store.service';
|
||||
import {Store, StoreRequest} from './store.schema';
|
||||
import {ApiResponse, ApiTags, ApiParam, ApiBody} from '@nestjs/swagger';
|
||||
import {ALLOW_ORIGIN_ALL, ALLOW_METHOD, ALLOW_CREDENTIALS, CONTENT_LENGTH, ALLOW_HEADERS} from 'src/consts';
|
||||
import {ALLOW_ORIGIN_ALL, ALLOW_METHOD, ALLOW_CREDENTIALS, CONTENT_LENGTH, ALLOW_HEADERS, COOLECTION_STORE} from 'src/consts';
|
||||
import {Request} from 'express';
|
||||
|
||||
const prepareStoreToStoreRequest = ({
|
||||
key, value, description, service_name, author
|
||||
@ -10,8 +11,13 @@ const prepareStoreToStoreRequest = ({
|
||||
key, value, description, service_name, author,
|
||||
});
|
||||
|
||||
@Controller('store')
|
||||
@ApiTags('store')
|
||||
const makeApiHeader = (request: Request): string => {
|
||||
const apiHeader = request.headers?.['api-name'];
|
||||
return typeof apiHeader === 'string' ? apiHeader : '';
|
||||
};
|
||||
|
||||
@Controller(COOLECTION_STORE)
|
||||
@ApiTags(COOLECTION_STORE)
|
||||
export class StoreController {
|
||||
constructor(
|
||||
private readonly storeService: StoreService
|
||||
@ -20,12 +26,13 @@ export class StoreController {
|
||||
@Get()
|
||||
@Header(...ALLOW_ORIGIN_ALL)
|
||||
@ApiResponse({
|
||||
status: 200,
|
||||
description: 'Список всех пар ключ-значение',
|
||||
status: 200,
|
||||
description: 'Список всех пар ключ-значение',
|
||||
type: [StoreRequest],
|
||||
})
|
||||
async findAll(): Promise<StoreRequest[]> {
|
||||
const storeList = await this.storeService.findAll();
|
||||
async findAll(@Req() request: Request): Promise<StoreRequest[]> {
|
||||
const api = makeApiHeader(request);
|
||||
const storeList = await this.storeService.findAll(api);
|
||||
return storeList.map(prepareStoreToStoreRequest);
|
||||
}
|
||||
|
||||
@ -40,8 +47,10 @@ export class StoreController {
|
||||
name: 'key',
|
||||
description: 'Уникальный ключ для получения api',
|
||||
})
|
||||
async findOne(@Param() {key}: {key: string}): Promise<StoreRequest> {
|
||||
const store = await this.storeService.findOne(key);
|
||||
async findOne(@Req() request: Request<{key: string}>): Promise<StoreRequest> {
|
||||
const {key} = request.params;
|
||||
const api = makeApiHeader(request);
|
||||
const store = await this.storeService.findOne(api, key);
|
||||
return prepareStoreToStoreRequest(store);
|
||||
}
|
||||
|
||||
@ -56,8 +65,9 @@ export class StoreController {
|
||||
type: StoreRequest,
|
||||
description: 'Принимает объект для создания api'
|
||||
})
|
||||
async create(@Body() createStoreClass: StoreRequest): Promise<StoreRequest> {
|
||||
const store = await this.storeService.create(createStoreClass);
|
||||
async create(@Req() request: Request<null, StoreRequest>): Promise<StoreRequest> {
|
||||
const api = makeApiHeader(request);
|
||||
const store = await this.storeService.create(api, request.body);
|
||||
return prepareStoreToStoreRequest(store);
|
||||
}
|
||||
|
||||
@ -72,8 +82,9 @@ export class StoreController {
|
||||
type: StoreRequest,
|
||||
description: 'Принимает объект для обновления api'
|
||||
})
|
||||
async update(@Body() updateStoreClass: StoreRequest): Promise<StoreRequest> {
|
||||
const store = await this.storeService.update(updateStoreClass);
|
||||
async update(@Req() request: Request<null, StoreRequest>): Promise<StoreRequest> {
|
||||
const api = makeApiHeader(request);
|
||||
const store = await this.storeService.update(api, request.body);
|
||||
return prepareStoreToStoreRequest(store);
|
||||
}
|
||||
|
||||
@ -88,8 +99,10 @@ export class StoreController {
|
||||
name: 'key',
|
||||
description: 'Уникальный ключ для удаления api',
|
||||
})
|
||||
async removeOne(@Param() {key}: {key: string}): Promise<StoreRequest> {
|
||||
const store = await this.storeService.removeOne(key);
|
||||
async removeOne(@Req() request: Request<{key: string}>): Promise<StoreRequest> {
|
||||
const {key} = request.params;
|
||||
const api = makeApiHeader(request);
|
||||
const store = await this.storeService.removeOne(api, key);
|
||||
return prepareStoreToStoreRequest(store);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user