fix cors always on issue
This commit is contained in:
47
src/index.ts
47
src/index.ts
@@ -118,11 +118,11 @@ export interface RouterHandler {
|
|||||||
* @property {number} [optionsSuccessStatus=204] Return status code for OPTIONS request (default: `204`)
|
* @property {number} [optionsSuccessStatus=204] Return status code for OPTIONS request (default: `204`)
|
||||||
*/
|
*/
|
||||||
export interface RouterCorsConfig {
|
export interface RouterCorsConfig {
|
||||||
allowOrigin: string
|
allowOrigin?: string
|
||||||
allowMethods: string
|
allowMethods?: string
|
||||||
allowHeaders: string
|
allowHeaders?: string
|
||||||
maxAge: number
|
maxAge?: number
|
||||||
optionsSuccessStatus: number
|
optionsSuccessStatus?: number
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -163,13 +163,7 @@ export default class Router {
|
|||||||
* @protected
|
* @protected
|
||||||
* @type {RouterCorsConfig}
|
* @type {RouterCorsConfig}
|
||||||
*/
|
*/
|
||||||
protected corsConfig: RouterCorsConfig = {
|
protected corsConfig: RouterCorsConfig = {}
|
||||||
allowOrigin: '*',
|
|
||||||
allowMethods: '*',
|
|
||||||
allowHeaders: '*',
|
|
||||||
maxAge: 86400,
|
|
||||||
optionsSuccessStatus: 204
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Register global handlers
|
* Register global handlers
|
||||||
@@ -391,14 +385,21 @@ export default class Router {
|
|||||||
query: {},
|
query: {},
|
||||||
body: ''
|
body: ''
|
||||||
}
|
}
|
||||||
if (req.method === 'OPTIONS' && Object.keys(this.corsConfig).length) {
|
|
||||||
|
const headers = new Headers()
|
||||||
|
|
||||||
|
if (this.corsConfig.allowOrigin)
|
||||||
|
headers.set('Access-Control-Allow-Origin', this.corsConfig.allowOrigin)
|
||||||
|
if (this.corsConfig.allowMethods)
|
||||||
|
headers.set('Access-Control-Allow-Methods', this.corsConfig.allowMethods)
|
||||||
|
if (this.corsConfig.allowHeaders)
|
||||||
|
headers.set('Access-Control-Allow-Headers', this.corsConfig.allowHeaders)
|
||||||
|
if (this.corsConfig.maxAge)
|
||||||
|
headers.set('Access-Control-Max-Age', this.corsConfig.maxAge.toString())
|
||||||
|
|
||||||
|
if (req.method === 'OPTIONS') {
|
||||||
return new Response(null, {
|
return new Response(null, {
|
||||||
headers: {
|
headers,
|
||||||
'Access-Control-Allow-Origin': this.corsConfig.allowOrigin,
|
|
||||||
'Access-Control-Allow-Methods': this.corsConfig.allowMethods,
|
|
||||||
'Access-Control-Allow-Headers': this.corsConfig.allowHeaders,
|
|
||||||
'Access-Control-Max-Age': this.corsConfig.maxAge!.toString()
|
|
||||||
},
|
|
||||||
status: this.corsConfig.optionsSuccessStatus
|
status: this.corsConfig.optionsSuccessStatus
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -420,13 +421,7 @@ export default class Router {
|
|||||||
const route = this.getRoute(req)
|
const route = this.getRoute(req)
|
||||||
if (!route)
|
if (!route)
|
||||||
return new Response(this.debugMode ? 'Route not found!' : null, { status: 404 })
|
return new Response(this.debugMode ? 'Route not found!' : null, { status: 404 })
|
||||||
const res: RouterResponse = { headers: new Headers() }
|
const res: RouterResponse = { headers }
|
||||||
if (Object.keys(this.corsConfig).length) {
|
|
||||||
res.headers.set('Access-Control-Allow-Origin', this.corsConfig.allowOrigin)
|
|
||||||
res.headers.set('Access-Control-Allow-Methods', this.corsConfig.allowMethods)
|
|
||||||
res.headers.set('Access-Control-Allow-Headers', this.corsConfig.allowHeaders)
|
|
||||||
res.headers.set('Access-Control-Max-Age', this.corsConfig.maxAge.toString())
|
|
||||||
}
|
|
||||||
const handlers = [...this.globalHandlers, ...route.handlers]
|
const handlers = [...this.globalHandlers, ...route.handlers]
|
||||||
let prevIndex = -1
|
let prevIndex = -1
|
||||||
const runner = async (index: number) => {
|
const runner = async (index: number) => {
|
||||||
|
|||||||
Reference in New Issue
Block a user