feat: allow root controller method allow all cors
This commit is contained in:
3
.vscode/settings.json
vendored
3
.vscode/settings.json
vendored
@@ -12,9 +12,6 @@
|
|||||||
"[typescriptreact]": {
|
"[typescriptreact]": {
|
||||||
"editor.defaultFormatter": "esbenp.prettier-vscode"
|
"editor.defaultFormatter": "esbenp.prettier-vscode"
|
||||||
},
|
},
|
||||||
"editor.codeActionsOnSave": {
|
|
||||||
"source.organizeImports": true,
|
|
||||||
},
|
|
||||||
"material-icon-theme.activeIconPack": "nest",
|
"material-icon-theme.activeIconPack": "nest",
|
||||||
"cSpell.words": [
|
"cSpell.words": [
|
||||||
"qaqdmin"
|
"qaqdmin"
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import {
|
|||||||
Get,
|
Get,
|
||||||
HttpCode,
|
HttpCode,
|
||||||
Post,
|
Post,
|
||||||
|
UseInterceptors,
|
||||||
} from '@nestjs/common'
|
} from '@nestjs/common'
|
||||||
import { ApiTags } from '@nestjs/swagger'
|
import { ApiTags } from '@nestjs/swagger'
|
||||||
import { InjectModel } from 'nestjs-typegoose'
|
import { InjectModel } from 'nestjs-typegoose'
|
||||||
@@ -11,6 +12,7 @@ import PKG from '../package.json'
|
|||||||
import { Auth } from './common/decorator/auth.decorator'
|
import { Auth } from './common/decorator/auth.decorator'
|
||||||
import { HttpCache } from './common/decorator/cache.decorator'
|
import { HttpCache } from './common/decorator/cache.decorator'
|
||||||
import { IpLocation, IpRecord } from './common/decorator/ip.decorator'
|
import { IpLocation, IpRecord } from './common/decorator/ip.decorator'
|
||||||
|
import { AllowAllCorsInterceptor } from './common/interceptors/allow-all-cors.interceptor'
|
||||||
import { RedisKeys } from './constants/cache.constant'
|
import { RedisKeys } from './constants/cache.constant'
|
||||||
import { OptionModel } from './modules/configs/configs.model'
|
import { OptionModel } from './modules/configs/configs.model'
|
||||||
import { CacheService } from './processors/cache/cache.service'
|
import { CacheService } from './processors/cache/cache.service'
|
||||||
@@ -23,6 +25,8 @@ export class AppController {
|
|||||||
@InjectModel(OptionModel)
|
@InjectModel(OptionModel)
|
||||||
private readonly optionModel: MongooseModel<OptionModel>,
|
private readonly optionModel: MongooseModel<OptionModel>,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
|
@UseInterceptors(AllowAllCorsInterceptor)
|
||||||
@Get(['/', '/info'])
|
@Get(['/', '/info'])
|
||||||
async appInfo() {
|
async appInfo() {
|
||||||
return {
|
return {
|
||||||
@@ -35,6 +39,7 @@ export class AppController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Get('/ping')
|
@Get('/ping')
|
||||||
|
@UseInterceptors(AllowAllCorsInterceptor)
|
||||||
ping(): 'pong' {
|
ping(): 'pong' {
|
||||||
return 'pong'
|
return 'pong'
|
||||||
}
|
}
|
||||||
|
|||||||
39
src/common/interceptors/allow-all-cors.interceptor.ts
Normal file
39
src/common/interceptors/allow-all-cors.interceptor.ts
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
import {
|
||||||
|
CallHandler,
|
||||||
|
ExecutionContext,
|
||||||
|
NestInterceptor,
|
||||||
|
RequestMethod,
|
||||||
|
} from '@nestjs/common'
|
||||||
|
import { FastifyReply } from 'fastify'
|
||||||
|
export class AllowAllCorsInterceptor implements NestInterceptor {
|
||||||
|
intercept(context: ExecutionContext, next: CallHandler<any>) {
|
||||||
|
const handle = next.handle()
|
||||||
|
const response: FastifyReply<any> = context.switchToHttp().getResponse()
|
||||||
|
const allowedMethods = [
|
||||||
|
RequestMethod.GET,
|
||||||
|
RequestMethod.HEAD,
|
||||||
|
RequestMethod.PUT,
|
||||||
|
RequestMethod.PATCH,
|
||||||
|
RequestMethod.POST,
|
||||||
|
RequestMethod.DELETE,
|
||||||
|
]
|
||||||
|
const allowedHeaders = [
|
||||||
|
'Authorization',
|
||||||
|
'Origin',
|
||||||
|
'No-Cache',
|
||||||
|
'X-Requested-With',
|
||||||
|
'If-Modified-Since',
|
||||||
|
'Last-Modified',
|
||||||
|
'Cache-Control',
|
||||||
|
'Expires',
|
||||||
|
'Content-Type',
|
||||||
|
]
|
||||||
|
response.headers({
|
||||||
|
'Access-Control-Allow-Origin': '*',
|
||||||
|
'Access-Control-Allow-Headers': allowedHeaders.join(','),
|
||||||
|
'Access-Control-Allow-Methods': allowedMethods.join(','),
|
||||||
|
'Access-Control-Max-Age': '86400',
|
||||||
|
})
|
||||||
|
return handle
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,3 +1,4 @@
|
|||||||
import './utils/global.util'
|
import './utils/global.util'
|
||||||
import './zx.global'
|
import './zx.global'
|
||||||
import './_bootstrap'
|
// organize-imports-ignore
|
||||||
|
import './bootstrap'
|
||||||
|
|||||||
@@ -43,7 +43,7 @@ export class NoteController {
|
|||||||
private readonly countingService: CountingService,
|
private readonly countingService: CountingService,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
@Get('latest')
|
@Get('/latest')
|
||||||
@ApiOperation({ summary: '获取最新发布一篇记录' })
|
@ApiOperation({ summary: '获取最新发布一篇记录' })
|
||||||
@VisitDocument('Note')
|
@VisitDocument('Note')
|
||||||
async getLatestOne(@IsMaster() isMaster: boolean) {
|
async getLatestOne(@IsMaster() isMaster: boolean) {
|
||||||
|
|||||||
Reference in New Issue
Block a user