1
0

cors fix, clean up

This commit is contained in:
2022-06-27 09:34:12 +02:00
parent df785496e6
commit feec3390ab

View File

@@ -38,7 +38,7 @@ interface RouterContext {
* @property {RouterRequestQuery} query Object containing all query parameters * @property {RouterRequestQuery} query Object containing all query parameters
* @property {Headers} headers Request headers object * @property {Headers} headers Request headers object
* @property {any} body Only available if method is `POST`, `PUT`, `PATCH` or `DELETE`. Contains either the received body string or a parsed object if valid JSON was sent. * @property {any} body Only available if method is `POST`, `PUT`, `PATCH` or `DELETE`. Contains either the received body string or a parsed object if valid JSON was sent.
* @property {IncomingRequestCfProperties=} cf object containing custom Cloudflare properties. (https://developers.cloudflare.com/workers/examples/accessing-the-cloudflare-object) * @property {IncomingRequestCfProperties} [cf] object containing custom Cloudflare properties. (https://developers.cloudflare.com/workers/examples/accessing-the-cloudflare-object)
*/ */
interface RouterRequest { interface RouterRequest {
url: string url: string
@@ -50,10 +50,20 @@ interface RouterRequest {
cf?: IncomingRequestCfProperties cf?: IncomingRequestCfProperties
} }
/**
* Request Parameters
*
* @typedef RouterRequestParams
*/
interface RouterRequestParams { interface RouterRequestParams {
[key: string]: string [key: string]: string
} }
/**
* Request Query
*
* @typedef RouterRequestQuery
*/
interface RouterRequestQuery { interface RouterRequestQuery {
[key: string]: string [key: string]: string
} }
@@ -63,9 +73,9 @@ interface RouterRequestQuery {
* *
* @typedef RouterResponse * @typedef RouterResponse
* @property {Headers} headers Response headers object * @property {Headers} headers Response headers object
* @property {number=204} status Return status code (default: `204`) * @property {number} [status=204] Return status code (default: `204`)
* @property {any=} body Either an `object` (will be converted to JSON) or a string * @property {any} [body] Either an `object` (will be converted to JSON) or a string
* @property {Response=} raw A response object that is to be returned, this will void all other res properties and return this as is. * @property {Response} [raw] A response object that is to be returned, this will void all other res properties and return this as is.
*/ */
interface RouterResponse { interface RouterResponse {
headers: Headers headers: Headers
@@ -100,11 +110,11 @@ interface RouterHandler {
* CORS Config * CORS Config
* *
* @typedef RouterCorsConfig * @typedef RouterCorsConfig
* @property {string} allowOrigin Access-Control-Allow-Origin (default: `*`) * @property {string} [allowOrigin="*"] Access-Control-Allow-Origin (default: `*`)
* @property {string} allowMethods Access-Control-Allow-Methods (default: `*`) * @property {string} [allowMethods="*"] Access-Control-Allow-Methods (default: `*`)
* @property {string} allowHeaders Access-Control-Allow-Headers (default: `*`) * @property {string} [allowHeaders="*"] Access-Control-Allow-Headers (default: `*`)
* @property {number} maxAge Access-Control-Max-Age (default: `86400`) * @property {number} [maxAge=86400] Access-Control-Max-Age (default: `86400`)
* @property {number} optionsSuccessStatus Return status code for OPTIONS request (default: `204`) * @property {number} [optionsSuccessStatus=204] Return status code for OPTIONS request (default: `204`)
*/ */
interface RouterCorsConfig { interface RouterCorsConfig {
allowOrigin: string allowOrigin: string
@@ -118,9 +128,8 @@ interface RouterCorsConfig {
/** /**
* Router * Router
* *
* @class
* @constructor
* @public * @public
* @class
*/ */
class Router { class Router {
@@ -296,17 +305,16 @@ class Router {
/** /**
* Enable CORS support * Enable CORS support
* *
* @param {RouterCorsConfig} config * @param {RouterCorsConfig} [config]
* @returns {Router} * @returns {Router}
*/ */
public cors(config: RouterCorsConfig): Router { public cors(config?: RouterCorsConfig): Router {
config = config || {}
this.corsConfig = { this.corsConfig = {
allowOrigin: config.allowOrigin || '*', allowOrigin: config?.allowOrigin || '*',
allowMethods: config.allowMethods || '*', allowMethods: config?.allowMethods || '*',
allowHeaders: config.allowHeaders || '*, Authorization', allowHeaders: config?.allowHeaders || '*, Authorization',
maxAge: config.maxAge || 86400, maxAge: config?.maxAge || 86400,
optionsSuccessStatus: config.optionsSuccessStatus || 204 optionsSuccessStatus: config?.optionsSuccessStatus || 204
} }
return this return this
} }
@@ -365,7 +373,7 @@ class Router {
* *
* @param {any} env * @param {any} env
* @param {Request} request * @param {Request} request
* @param {any=} extend * @param {any} [extend]
* @returns {Response} * @returns {Response}
*/ */
public async handle(env: any, request: Request, extend: any = {}) { public async handle(env: any, request: Request, extend: any = {}) {
@@ -386,7 +394,7 @@ class Router {
'Access-Control-Allow-Origin': this.corsConfig.allowOrigin, 'Access-Control-Allow-Origin': this.corsConfig.allowOrigin,
'Access-Control-Allow-Methods': this.corsConfig.allowMethods, 'Access-Control-Allow-Methods': this.corsConfig.allowMethods,
'Access-Control-Allow-Headers': this.corsConfig.allowHeaders, 'Access-Control-Allow-Headers': this.corsConfig.allowHeaders,
'Access-Control-Max-Age': this.corsConfig.maxAge.toString() 'Access-Control-Max-Age': this.corsConfig.maxAge!.toString()
}, },
status: this.corsConfig.optionsSuccessStatus status: this.corsConfig.optionsSuccessStatus
}) })