access options

This commit is contained in:
2020-07-04 00:11:21 +03:00
parent 9238629bd1
commit 16d9296b54
2 changed files with 16 additions and 11 deletions

View File

@ -2,4 +2,6 @@ export const DB_NAME = '/store-service';
export const MONGO_URL = `mongodb://localhost:27017${DB_NAME}`; export const MONGO_URL = `mongodb://localhost:27017${DB_NAME}`;
export const ALLOW_ORIGIN_ALL: [string, string] = ['Access-Control-Allow-Origin', '*']; export const ALLOW_ORIGIN_ALL: [string, string] = ['Access-Control-Allow-Origin', '*'];
export const ALLOW_CREDENTIALS: [string, string] = ['Access-Control-Allow-Credentials', 'true']; export const ALLOW_CREDENTIALS: [string, string] = ['Access-Control-Allow-Credentials', 'true'];
export const ALLOW_METHOD: [string, string] = ['Access-Control-Request-Method', 'POST DELETE PUT GET']; export const CONTENT_LENGTH: [string, string] = ['Content-Length', '0'];
export const ALLOW_METHOD: [string, string] = ['Access-Control-Allow-Methods', 'GET,HEAD,PUT,PATCH,POST,DELETE'];
export const ALLOW_HEADERS: [string, string] = ['Access-Control-Allow-Headers', 'Version, Authorization, Content-Type'];

View File

@ -1,8 +1,8 @@
import { Controller, Get, Post, Body, Param, Header, Delete } from '@nestjs/common'; import { Controller, Get, Post, Body, Param, Options, Header, Delete, HttpCode } from '@nestjs/common';
import {StoreService} from './store.service'; import {StoreService} from './store.service';
import {Store, StoreResponse, StoreRequest} from './store.schema'; import {Store, StoreResponse, StoreRequest} from './store.schema';
import {ApiResponse} from '@nestjs/swagger'; import {ApiResponse} from '@nestjs/swagger';
import {ALLOW_ORIGIN_ALL, ALLOW_METHOD, ALLOW_CREDENTIALS} from 'src/consts'; import {ALLOW_ORIGIN_ALL, ALLOW_METHOD, ALLOW_CREDENTIALS, CONTENT_LENGTH, ALLOW_HEADERS} from 'src/consts';
@Controller('store') @Controller('store')
export class StoreController { export class StoreController {
@ -12,8 +12,6 @@ export class StoreController {
@Get() @Get()
@Header(...ALLOW_ORIGIN_ALL) @Header(...ALLOW_ORIGIN_ALL)
@Header(...ALLOW_METHOD)
@Header(...ALLOW_CREDENTIALS)
@ApiResponse({ @ApiResponse({
status: 200, status: 200,
description: 'Список всех пар ключ-значение', description: 'Список всех пар ключ-значение',
@ -25,8 +23,6 @@ export class StoreController {
@Get(':key') @Get(':key')
@Header(...ALLOW_ORIGIN_ALL) @Header(...ALLOW_ORIGIN_ALL)
@Header(...ALLOW_METHOD)
@Header(...ALLOW_CREDENTIALS)
@ApiResponse({ @ApiResponse({
status: 200, status: 200,
description: 'Возвращает пару ключ-значение по ключу', description: 'Возвращает пару ключ-значение по ключу',
@ -38,8 +34,6 @@ export class StoreController {
@Post() @Post()
@Header(...ALLOW_ORIGIN_ALL) @Header(...ALLOW_ORIGIN_ALL)
@Header(...ALLOW_METHOD)
@Header(...ALLOW_CREDENTIALS)
@ApiResponse({ @ApiResponse({
status: 200, status: 200,
description: 'Создает новую пару ключ-значение или заменяет существующую по ключу', description: 'Создает новую пару ключ-значение или заменяет существующую по ключу',
@ -51,8 +45,6 @@ export class StoreController {
@Delete(':key') @Delete(':key')
@Header(...ALLOW_ORIGIN_ALL) @Header(...ALLOW_ORIGIN_ALL)
@Header(...ALLOW_METHOD)
@Header(...ALLOW_CREDENTIALS)
@ApiResponse({ @ApiResponse({
status: 200, status: 200,
description: 'Удаляет пару ключ-значение по ключу', description: 'Удаляет пару ключ-значение по ключу',
@ -61,4 +53,15 @@ export class StoreController {
async removeOne(@Param() {key}: {key: string}): Promise<string> { async removeOne(@Param() {key}: {key: string}): Promise<string> {
return this.storeService.removeOne(key); return this.storeService.removeOne(key);
} }
@Options()
@Header(...ALLOW_ORIGIN_ALL)
@Header(...ALLOW_METHOD)
@Header(...ALLOW_CREDENTIALS)
@Header(...CONTENT_LENGTH)
@Header(...ALLOW_HEADERS)
@HttpCode(204)
async options() {
return '';
}
} }