1
0

added the ability to specify the type of RouterContext.env

This commit is contained in:
Anbcodes
2022-10-06 15:57:18 -04:00
parent d3b86f1dbe
commit 972dc916f7

View File

@@ -6,10 +6,10 @@
* @property {string} url URL String * @property {string} url URL String
* @property {RouterHandler[]} handlers Array of handler functions * @property {RouterHandler[]} handlers Array of handler functions
*/ */
export interface Route { export interface Route<T> {
method: string method: string
url: string url: string
handlers: RouterHandler[] handlers: RouterHandler<T>[]
} }
/** /**
@@ -21,8 +21,8 @@
* @property {RouterResponse} res Response Object * @property {RouterResponse} res Response Object
* @property {RouterNext} next Next Handler * @property {RouterNext} next Next Handler
*/ */
export interface RouterContext { export interface RouterContext<T> {
env: any env: T
req: RouterRequest req: RouterRequest
res: RouterResponse res: RouterResponse
next: RouterNext next: RouterNext
@@ -103,8 +103,8 @@ export interface RouterNext {
* @param {RouterContext} ctx * @param {RouterContext} ctx
* @returns {Promise<void> | void} * @returns {Promise<void> | void}
*/ */
export interface RouterHandler { export interface RouterHandler<T> {
(ctx: RouterContext): Promise<void> | void (ctx: RouterContext<T>): Promise<void> | void
} }
/** /**
@@ -131,7 +131,7 @@ export interface RouterCorsConfig {
* @public * @public
* @class * @class
*/ */
export class Router { export class Router<T = any> {
/** /**
* Router Array * Router Array
@@ -139,7 +139,7 @@ export class Router {
* @protected * @protected
* @type {Route[]} * @type {Route[]}
*/ */
protected routes: Route[] = [] protected routes: Route<T>[] = []
/** /**
* Global Handlers * Global Handlers
@@ -147,7 +147,7 @@ export class Router {
* @protected * @protected
* @type {RouterHandler[]} * @type {RouterHandler[]}
*/ */
protected globalHandlers: RouterHandler[] = [] protected globalHandlers: RouterHandler<T>[] = []
/** /**
* Debug Mode * Debug Mode
@@ -179,7 +179,7 @@ export class Router {
* @param {RouterHandler[]} handlers * @param {RouterHandler[]} handlers
* @returns {Router} * @returns {Router}
*/ */
public use(...handlers: RouterHandler[]): Router { public use(...handlers: RouterHandler<T>[]): Router<T> {
for (let handler of handlers) { for (let handler of handlers) {
this.globalHandlers.push(handler) this.globalHandlers.push(handler)
} }
@@ -193,7 +193,7 @@ export class Router {
* @param {RouterHandler[]} handlers * @param {RouterHandler[]} handlers
* @returns {Router} * @returns {Router}
*/ */
public connect(url: string, ...handlers: RouterHandler[]): Router { public connect(url: string, ...handlers: RouterHandler<T>[]): Router<T> {
return this.register('CONNECT', url, handlers) return this.register('CONNECT', url, handlers)
} }
@@ -204,7 +204,7 @@ export class Router {
* @param {RouterHandler[]} handlers * @param {RouterHandler[]} handlers
* @returns {Router} * @returns {Router}
*/ */
public delete(url: string, ...handlers: RouterHandler[]): Router { public delete(url: string, ...handlers: RouterHandler<T>[]): Router<T> {
return this.register('DELETE', url, handlers) return this.register('DELETE', url, handlers)
} }
@@ -215,7 +215,7 @@ export class Router {
* @param {RouterHandler[]} handlers * @param {RouterHandler[]} handlers
* @returns {Router} * @returns {Router}
*/ */
public get(url: string, ...handlers: RouterHandler[]): Router { public get(url: string, ...handlers: RouterHandler<T>[]): Router<T> {
return this.register('GET', url, handlers) return this.register('GET', url, handlers)
} }
@@ -226,7 +226,7 @@ export class Router {
* @param {RouterHandler[]} handlers * @param {RouterHandler[]} handlers
* @returns {Router} * @returns {Router}
*/ */
public head(url: string, ...handlers: RouterHandler[]): Router { public head(url: string, ...handlers: RouterHandler<T>[]): Router<T> {
return this.register('HEAD', url, handlers) return this.register('HEAD', url, handlers)
} }
@@ -237,7 +237,7 @@ export class Router {
* @param {RouterHandler[]} handlers * @param {RouterHandler[]} handlers
* @returns {Router} * @returns {Router}
*/ */
public options(url: string, ...handlers: RouterHandler[]): Router { public options(url: string, ...handlers: RouterHandler<T>[]): Router<T> {
return this.register('OPTIONS', url, handlers) return this.register('OPTIONS', url, handlers)
} }
@@ -248,7 +248,7 @@ export class Router {
* @param {RouterHandler[]} handlers * @param {RouterHandler[]} handlers
* @returns {Router} * @returns {Router}
*/ */
public patch(url: string, ...handlers: RouterHandler[]): Router { public patch(url: string, ...handlers: RouterHandler<T>[]): Router<T> {
return this.register('PATCH', url, handlers) return this.register('PATCH', url, handlers)
} }
@@ -259,7 +259,7 @@ export class Router {
* @param {RouterHandler[]} handlers * @param {RouterHandler[]} handlers
* @returns {Router} * @returns {Router}
*/ */
public post(url: string, ...handlers: RouterHandler[]): Router { public post(url: string, ...handlers: RouterHandler<T>[]): Router<T> {
return this.register('POST', url, handlers) return this.register('POST', url, handlers)
} }
@@ -270,7 +270,7 @@ export class Router {
* @param {RouterHandler[]} handlers * @param {RouterHandler[]} handlers
* @returns {Router} * @returns {Router}
*/ */
public put(url: string, ...handlers: RouterHandler[]): Router { public put(url: string, ...handlers: RouterHandler<T>[]): Router<T> {
return this.register('PUT', url, handlers) return this.register('PUT', url, handlers)
} }
@@ -281,7 +281,7 @@ export class Router {
* @param {RouterHandler[]} handlers * @param {RouterHandler[]} handlers
* @returns {Router} * @returns {Router}
*/ */
public trace(url: string, ...handlers: RouterHandler[]): Router { public trace(url: string, ...handlers: RouterHandler<T>[]): Router<T> {
return this.register('TRACE', url, handlers) return this.register('TRACE', url, handlers)
} }
@@ -292,7 +292,7 @@ export class Router {
* @param {RouterHandler[]} handlers * @param {RouterHandler[]} handlers
* @returns {Router} * @returns {Router}
*/ */
public any(url: string, ...handlers: RouterHandler[]): Router { public any(url: string, ...handlers: RouterHandler<T>[]): Router<T> {
return this.register('*', url, handlers) return this.register('*', url, handlers)
} }
@@ -302,7 +302,7 @@ export class Router {
* @param {boolean} [state=true] Whether to turn on or off debug mode (default: true) * @param {boolean} [state=true] Whether to turn on or off debug mode (default: true)
* @returns {Router} * @returns {Router}
*/ */
public debug(state: boolean = true): Router { public debug(state: boolean = true): Router<T> {
this.debugMode = state this.debugMode = state
return this return this
} }
@@ -313,7 +313,7 @@ export class Router {
* @param {RouterCorsConfig} [config] * @param {RouterCorsConfig} [config]
* @returns {Router} * @returns {Router}
*/ */
public cors(config?: RouterCorsConfig): Router { public cors(config?: RouterCorsConfig): Router<T> {
this.corsEnabled = true this.corsEnabled = true
this.corsConfig = { this.corsConfig = {
allowOrigin: config?.allowOrigin || '*', allowOrigin: config?.allowOrigin || '*',
@@ -334,7 +334,7 @@ export class Router {
* @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[]): Router { private register(method: string, url: string, handlers: RouterHandler<T>[]): Router<T> {
this.routes.push({ this.routes.push({
method, method,
url, url,
@@ -351,7 +351,7 @@ export class Router {
* @param {RouterRequest} request * @param {RouterRequest} request
* @returns {Route | undefined} * @returns {Route | undefined}
*/ */
private getRoute(request: RouterRequest): Route | undefined { private getRoute(request: RouterRequest): Route<T> | undefined {
const url = new URL(request.url) const url = new URL(request.url)
const pathArr = url.pathname.split('/').filter(i => i) const pathArr = url.pathname.split('/').filter(i => i)
@@ -388,12 +388,12 @@ export class Router {
/** /**
* Handle requests * Handle requests
* *
* @param {any} env * @param {T} env
* @param {Request} request * @param {Request} request
* @param {any} [extend] * @param {any} [extend]
* @returns {Promise<Response>} * @returns {Promise<Response>}
*/ */
public async handle(env: any, request: Request, extend: any = {}): Promise<Response> { public async handle(env: T, request: Request, extend: any = {}): Promise<Response> {
try { try {
const req: RouterRequest = { const req: RouterRequest = {
...extend, ...extend,