clean up
This commit is contained in:
170
src/index.ts
170
src/index.ts
@@ -13,17 +13,17 @@ export interface Route<TEnv> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Router Context
|
* Router Context
|
||||||
*
|
*
|
||||||
* @typedef RouterContext
|
* @typedef RouterContext
|
||||||
* @property {RouterEnv} env Environment
|
* @property {RouterEnv} env Environment
|
||||||
* @property {RouterRequest} req Request Object
|
* @property {RouterRequest} req Request Object
|
||||||
* @property {ExecutionContext} ctx Context Object
|
* @property {ExecutionContext} ctx Context Object
|
||||||
*/
|
*/
|
||||||
export interface RouterContext<TEnv = any> {
|
export interface RouterContext<TEnv = any> {
|
||||||
env: TEnv
|
env: TEnv
|
||||||
req: RouterRequest
|
req: RouterRequest
|
||||||
ctx: ExecutionContext
|
ctx: ExecutionContext
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -68,14 +68,14 @@ export interface RouterRequestQuery {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handler Function
|
* Handler Function
|
||||||
*
|
*
|
||||||
* @callback RouterHandler
|
* @callback RouterHandler
|
||||||
* @param {RouterContext} ctx
|
* @param {RouterContext} ctx
|
||||||
* @returns {Promise<Response | void> Response | void}
|
* @returns {Promise<Response | void> Response | void}
|
||||||
*/
|
*/
|
||||||
export interface RouterHandler<TEnv = any> {
|
export interface RouterHandler<TEnv = any> {
|
||||||
(ctx: RouterContext<TEnv>): Promise<Response | void> | Response | void
|
(ctx: RouterContext<TEnv>): Promise<Response | void> | Response | void
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -296,33 +296,33 @@ export class Router<TEnv = any> {
|
|||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
private setCorsHeaders(headers: Headers = new Headers()): Headers {
|
private setCorsHeaders(headers: Headers = new Headers()): Headers {
|
||||||
if (this.corsConfig.allowOrigin && !headers.has('Access-Control-Allow-Origin'))
|
if (this.corsConfig.allowOrigin && !headers.has('Access-Control-Allow-Origin'))
|
||||||
headers.set('Access-Control-Allow-Origin', this.corsConfig.allowOrigin)
|
headers.set('Access-Control-Allow-Origin', this.corsConfig.allowOrigin)
|
||||||
if (this.corsConfig.allowMethods && !headers.has('Access-Control-Allow-Methods'))
|
if (this.corsConfig.allowMethods && !headers.has('Access-Control-Allow-Methods'))
|
||||||
headers.set('Access-Control-Allow-Methods', this.corsConfig.allowMethods)
|
headers.set('Access-Control-Allow-Methods', this.corsConfig.allowMethods)
|
||||||
if (this.corsConfig.allowHeaders && !headers.has('Access-Control-Allow-Headers'))
|
if (this.corsConfig.allowHeaders && !headers.has('Access-Control-Allow-Headers'))
|
||||||
headers.set('Access-Control-Allow-Headers', this.corsConfig.allowHeaders)
|
headers.set('Access-Control-Allow-Headers', this.corsConfig.allowHeaders)
|
||||||
if (this.corsConfig.maxAge && !headers.has('Access-Control-Max-Age'))
|
if (this.corsConfig.maxAge && !headers.has('Access-Control-Max-Age'))
|
||||||
headers.set('Access-Control-Max-Age', this.corsConfig.maxAge.toString())
|
headers.set('Access-Control-Max-Age', this.corsConfig.maxAge.toString())
|
||||||
return headers
|
return headers
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Register route
|
* Register route
|
||||||
*
|
*
|
||||||
* @private
|
* @private
|
||||||
* @param {string} method HTTP request method
|
* @param {string} method HTTP request method
|
||||||
* @param {string} url URL String
|
* @param {string} url URL String
|
||||||
* @param {RouterHandler[]} handlers Arrar of handler functions
|
* @param {RouterHandler[]} handlers Arrar of handler functions
|
||||||
* @returns {Router}
|
* @returns {Router}
|
||||||
*/
|
*/
|
||||||
private register(method: string, url: string, handlers: RouterHandler<TEnv>[]): Router<TEnv> {
|
private register(method: string, url: string, handlers: RouterHandler<TEnv>[]): Router<TEnv> {
|
||||||
this.routes.push({
|
this.routes.push({
|
||||||
method,
|
method,
|
||||||
url,
|
url,
|
||||||
handlers
|
handlers
|
||||||
})
|
})
|
||||||
|
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
@@ -369,56 +369,56 @@ export class Router<TEnv = any> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handle requests
|
* Handle requests
|
||||||
*
|
*
|
||||||
* @param {TEnv} env
|
* @param {TEnv} env
|
||||||
* @param {Request} request
|
* @param {Request} request
|
||||||
* @param {any} [extend]
|
* @param {any} [extend]
|
||||||
* @returns {Promise<Response>}
|
* @returns {Promise<Response>}
|
||||||
*/
|
*/
|
||||||
public async handle(request: Request, env: TEnv, ctx: ExecutionContext, extend: any = {}): Promise<Response> {
|
public async handle(request: Request, env: TEnv, ctx: ExecutionContext, extend: any = {}): Promise<Response> {
|
||||||
const req: RouterRequest = {
|
const req: RouterRequest = {
|
||||||
...extend,
|
...extend,
|
||||||
method: request.method,
|
method: request.method,
|
||||||
headers: request.headers,
|
headers: request.headers,
|
||||||
url: request.url,
|
url: request.url,
|
||||||
cf: request.cf,
|
cf: request.cf,
|
||||||
params: {},
|
params: {},
|
||||||
query: {},
|
query: {},
|
||||||
body: ''
|
body: ''
|
||||||
}
|
}
|
||||||
|
|
||||||
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 })
|
||||||
|
|
||||||
if (this.corsEnabled && req.method === 'OPTIONS') {
|
if (this.corsEnabled && req.method === 'OPTIONS') {
|
||||||
return new Response(null, {
|
return new Response(null, {
|
||||||
headers: this.setCorsHeaders(),
|
headers: this.setCorsHeaders(),
|
||||||
status: this.corsConfig.optionsSuccessStatus
|
status: this.corsConfig.optionsSuccessStatus
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
const handlers = [...this.globalHandlers, ...route.handlers]
|
const handlers = [...this.globalHandlers, ...route.handlers]
|
||||||
|
|
||||||
let response: Response | undefined
|
let response: Response | undefined
|
||||||
|
|
||||||
for (const handler of handlers) {
|
for (const handler of handlers) {
|
||||||
const res = await handler({ env, req, ctx })
|
const res = await handler({ env, req, ctx })
|
||||||
|
|
||||||
if (res) {
|
if (res) {
|
||||||
response = res
|
response = res
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!response)
|
if (!response)
|
||||||
return new Response(this.debugMode ? 'Handler did not return a Response!' : null, { status: 404 })
|
return new Response(this.debugMode ? 'Handler did not return a Response!' : null, { status: 404 })
|
||||||
|
|
||||||
if (this.corsEnabled)
|
if (this.corsEnabled)
|
||||||
this.setCorsHeaders(response.headers)
|
this.setCorsHeaders(response.headers)
|
||||||
|
|
||||||
return response
|
return response
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user