diff --git a/src/index.ts b/src/index.ts index 2594b08..e7fe9ce 100644 --- a/src/index.ts +++ b/src/index.ts @@ -6,10 +6,10 @@ * @property {string} url URL String * @property {RouterHandler[]} handlers Array of handler functions */ -export type Route = { +export type Route = { method: string url: string - handlers: RouterHandler[] + handlers: RouterHandler[] } /** @@ -20,9 +20,9 @@ export type Route = { * @property {RouterRequest} req Request Object * @property {ExecutionContext} ctx Context Object */ -export type RouterContext = { +export type RouterContext = TCtx & { env: TEnv - req: RouterRequest + req: RouterRequest dbg: boolean ctx?: ExecutionContext } @@ -39,7 +39,7 @@ export type RouterContext = { * @property {Request} raw Raw Request Object * @property {IncomingRequestCfProperties} [cf] object containing custom Cloudflare properties. (https://developers.cloudflare.com/workers/examples/accessing-the-cloudflare-object) */ -export type RouterRequest = { +export type RouterRequest = TReq & { url: string method: string params: RouterRequestParams @@ -53,7 +53,7 @@ export type RouterRequest = { blob(): Promise bearer: () => string | undefined cf?: IncomingRequestCfProperties -} & TExt +} /** * Request Parameters @@ -80,8 +80,8 @@ export type RouterRequestQuery = { * @param {RouterContext} ctx * @returns {Promise Response | void} */ -export type RouterHandler = { - (ctx: RouterContext): Promise | Response | void +export type RouterHandler = { + (ctx: RouterContext): Promise | Response | void } /** @@ -116,7 +116,7 @@ export type RouterBuffer = { * @public * @class */ -export class Router { +export class Router { /** * Router Array @@ -124,7 +124,7 @@ export class Router { * @protected * @type {Route[]} */ - protected routes: Route[] = [] + protected routes: Route[] = [] /** * Global Handlers @@ -132,7 +132,7 @@ export class Router { * @protected * @type {RouterHandler[]} */ - protected globalHandlers: RouterHandler[] = [] + protected globalHandlers: RouterHandler[] = [] /** * Debug Mode @@ -172,7 +172,7 @@ export class Router { * @param {RouterHandler[]} handlers * @returns {Router} */ - public use(...handlers: RouterHandler[]): Router { + public use(...handlers: RouterHandler[]): Router { for (let handler of handlers) { this.globalHandlers.push(handler) } @@ -186,7 +186,7 @@ export class Router { * @param {RouterHandler[]} handlers * @returns {Router} */ - public connect(url: string, ...handlers: RouterHandler[]): Router { + public connect(url: string, ...handlers: RouterHandler[]): Router { return this.register('CONNECT', url, handlers) } @@ -197,7 +197,7 @@ export class Router { * @param {RouterHandler[]} handlers * @returns {Router} */ - public delete(url: string, ...handlers: RouterHandler[]): Router { + public delete(url: string, ...handlers: RouterHandler[]): Router { return this.register('DELETE', url, handlers) } @@ -208,7 +208,7 @@ export class Router { * @param {RouterHandler[]} handlers * @returns {Router} */ - public get(url: string, ...handlers: RouterHandler[]): Router { + public get(url: string, ...handlers: RouterHandler[]): Router { return this.register('GET', url, handlers) } @@ -219,7 +219,7 @@ export class Router { * @param {RouterHandler[]} handlers * @returns {Router} */ - public head(url: string, ...handlers: RouterHandler[]): Router { + public head(url: string, ...handlers: RouterHandler[]): Router { return this.register('HEAD', url, handlers) } @@ -230,7 +230,7 @@ export class Router { * @param {RouterHandler[]} handlers * @returns {Router} */ - public options(url: string, ...handlers: RouterHandler[]): Router { + public options(url: string, ...handlers: RouterHandler[]): Router { return this.register('OPTIONS', url, handlers) } @@ -241,7 +241,7 @@ export class Router { * @param {RouterHandler[]} handlers * @returns {Router} */ - public patch(url: string, ...handlers: RouterHandler[]): Router { + public patch(url: string, ...handlers: RouterHandler[]): Router { return this.register('PATCH', url, handlers) } @@ -252,7 +252,7 @@ export class Router { * @param {RouterHandler[]} handlers * @returns {Router} */ - public post(url: string, ...handlers: RouterHandler[]): Router { + public post(url: string, ...handlers: RouterHandler[]): Router { return this.register('POST', url, handlers) } @@ -263,7 +263,7 @@ export class Router { * @param {RouterHandler[]} handlers * @returns {Router} */ - public put(url: string, ...handlers: RouterHandler[]): Router { + public put(url: string, ...handlers: RouterHandler[]): Router { return this.register('PUT', url, handlers) } @@ -274,7 +274,7 @@ export class Router { * @param {RouterHandler[]} handlers * @returns {Router} */ - public trace(url: string, ...handlers: RouterHandler[]): Router { + public trace(url: string, ...handlers: RouterHandler[]): Router { return this.register('TRACE', url, handlers) } @@ -285,7 +285,7 @@ export class Router { * @param {RouterHandler[]} handlers * @returns {Router} */ - public any(url: string, ...handlers: RouterHandler[]): Router { + public any(url: string, ...handlers: RouterHandler[]): Router { return this.register('*', url, handlers) } @@ -295,7 +295,7 @@ export class Router { * @param {boolean} [state=true] Whether to turn on or off debug mode (default: true) * @returns {Router} */ - public debug(state: boolean = true): Router { + public debug(state: boolean = true): Router { this.debugMode = state return this } @@ -306,7 +306,7 @@ export class Router { * @param {RouterCorsConfig} [config] * @returns {Router} */ - public cors(config?: RouterCorsConfig): Router { + public cors(config?: RouterCorsConfig): Router { this.corsEnabled = true this.corsConfig = { allowOrigin: config?.allowOrigin ?? '*', @@ -339,7 +339,7 @@ export class Router { * @param {RouterHandler[]} handlers Arrar of handler functions * @returns {Router} */ - private register(method: string, url: string, handlers: RouterHandler[]): Router { + private register(method: string, url: string, handlers: RouterHandler[]): Router { this.routes.push({ method, url, @@ -356,7 +356,7 @@ export class Router { * @param {RouterRequest} request * @returns {Route | undefined} */ - private getRoute(request: RouterRequest): Route | undefined { + private getRoute(request: RouterRequest): Route | undefined { const url = new URL(request.url) const pathArr = url.pathname.split('/').filter(i => i) @@ -395,12 +395,13 @@ export class Router { * * @param {Request} request * @param {TEnv} env - * @param {TExt} [ext] + * @param {TCtx} [extCtx] + * @param {TReq} [extReq] * @returns {Promise} */ - public async handle(request: Request, env: TEnv, ctx?: ExecutionContext, ext?: TExt): Promise { + public async handle(request: Request, env: TEnv, ctx?: ExecutionContext, extCtx?: TCtx, extReq?: TReq): Promise { const req = { - ...(ext ?? {}), + ...(extReq ?? {}), method: request.method, headers: request.headers, url: request.url, @@ -414,7 +415,7 @@ export class Router { formData: async (): Promise => this.buffer.formData ? this.buffer.formData : this.buffer.formData = await request.formData(), blob: async (): Promise => this.buffer.blob ? this.buffer.blob : this.buffer.blob = await request.blob(), bearer: () => request.headers.get('Authorization')?.replace(/^(B|b)earer /, '').trim() - } as RouterRequest + } as RouterRequest if (this.corsEnabled && req.method === 'OPTIONS') { return new Response(null, { @@ -434,7 +435,15 @@ export class Router { let response: Response | undefined for (const handler of handlers) { - const res = await handler({ env, req, dbg, ctx }) + const context = { + ...(extCtx ?? {}), + env, + req, + dbg, + ctx + } as RouterContext + + const res = await handler(context) if (res) { response = res