allow for context extension
This commit is contained in:
71
src/index.ts
71
src/index.ts
@@ -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 type Route<TEnv, TExt> = {
|
export type Route<TEnv, TCtx, TReq> = {
|
||||||
method: string
|
method: string
|
||||||
url: string
|
url: string
|
||||||
handlers: RouterHandler<TEnv, TExt>[]
|
handlers: RouterHandler<TEnv, TCtx, TReq>[]
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -20,9 +20,9 @@ export type Route<TEnv, TExt> = {
|
|||||||
* @property {RouterRequest} req Request Object
|
* @property {RouterRequest} req Request Object
|
||||||
* @property {ExecutionContext} ctx Context Object
|
* @property {ExecutionContext} ctx Context Object
|
||||||
*/
|
*/
|
||||||
export type RouterContext<TEnv = any, TExt = any> = {
|
export type RouterContext<TEnv = any, TCtx = any, TReq = any> = TCtx & {
|
||||||
env: TEnv
|
env: TEnv
|
||||||
req: RouterRequest<TExt>
|
req: RouterRequest<TReq>
|
||||||
dbg: boolean
|
dbg: boolean
|
||||||
ctx?: ExecutionContext
|
ctx?: ExecutionContext
|
||||||
}
|
}
|
||||||
@@ -39,7 +39,7 @@ export type RouterContext<TEnv = any, TExt = any> = {
|
|||||||
* @property {Request} raw Raw Request Object
|
* @property {Request} raw Raw Request Object
|
||||||
* @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)
|
||||||
*/
|
*/
|
||||||
export type RouterRequest<TExt> = {
|
export type RouterRequest<TReq> = TReq & {
|
||||||
url: string
|
url: string
|
||||||
method: string
|
method: string
|
||||||
params: RouterRequestParams
|
params: RouterRequestParams
|
||||||
@@ -53,7 +53,7 @@ export type RouterRequest<TExt> = {
|
|||||||
blob(): Promise<Blob>
|
blob(): Promise<Blob>
|
||||||
bearer: () => string | undefined
|
bearer: () => string | undefined
|
||||||
cf?: IncomingRequestCfProperties
|
cf?: IncomingRequestCfProperties
|
||||||
} & TExt
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Request Parameters
|
* Request Parameters
|
||||||
@@ -80,8 +80,8 @@ export type RouterRequestQuery = {
|
|||||||
* @param {RouterContext} ctx
|
* @param {RouterContext} ctx
|
||||||
* @returns {Promise<Response | void> Response | void}
|
* @returns {Promise<Response | void> Response | void}
|
||||||
*/
|
*/
|
||||||
export type RouterHandler<TEnv = any, TExt = any> = {
|
export type RouterHandler<TEnv = any, TCtx = any, TReq = any> = {
|
||||||
(ctx: RouterContext<TEnv, TExt>): Promise<Response | void> | Response | void
|
(ctx: RouterContext<TEnv, TCtx, TReq>): Promise<Response | void> | Response | void
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -116,7 +116,7 @@ export type RouterBuffer = {
|
|||||||
* @public
|
* @public
|
||||||
* @class
|
* @class
|
||||||
*/
|
*/
|
||||||
export class Router<TEnv = any, TExt = any> {
|
export class Router<TEnv = any, TCtx = any, TReq = any> {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Router Array
|
* Router Array
|
||||||
@@ -124,7 +124,7 @@ export class Router<TEnv = any, TExt = any> {
|
|||||||
* @protected
|
* @protected
|
||||||
* @type {Route[]}
|
* @type {Route[]}
|
||||||
*/
|
*/
|
||||||
protected routes: Route<TEnv, TExt>[] = []
|
protected routes: Route<TEnv, TCtx, TReq>[] = []
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Global Handlers
|
* Global Handlers
|
||||||
@@ -132,7 +132,7 @@ export class Router<TEnv = any, TExt = any> {
|
|||||||
* @protected
|
* @protected
|
||||||
* @type {RouterHandler[]}
|
* @type {RouterHandler[]}
|
||||||
*/
|
*/
|
||||||
protected globalHandlers: RouterHandler<TEnv, TExt>[] = []
|
protected globalHandlers: RouterHandler<TEnv, TCtx, TReq>[] = []
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Debug Mode
|
* Debug Mode
|
||||||
@@ -172,7 +172,7 @@ export class Router<TEnv = any, TExt = any> {
|
|||||||
* @param {RouterHandler[]} handlers
|
* @param {RouterHandler[]} handlers
|
||||||
* @returns {Router}
|
* @returns {Router}
|
||||||
*/
|
*/
|
||||||
public use(...handlers: RouterHandler<TEnv, TExt>[]): Router<TEnv, TExt> {
|
public use(...handlers: RouterHandler<TEnv, TCtx, TReq>[]): Router<TEnv, TCtx, TReq> {
|
||||||
for (let handler of handlers) {
|
for (let handler of handlers) {
|
||||||
this.globalHandlers.push(handler)
|
this.globalHandlers.push(handler)
|
||||||
}
|
}
|
||||||
@@ -186,7 +186,7 @@ export class Router<TEnv = any, TExt = any> {
|
|||||||
* @param {RouterHandler[]} handlers
|
* @param {RouterHandler[]} handlers
|
||||||
* @returns {Router}
|
* @returns {Router}
|
||||||
*/
|
*/
|
||||||
public connect(url: string, ...handlers: RouterHandler<TEnv, TExt>[]): Router<TEnv, TExt> {
|
public connect(url: string, ...handlers: RouterHandler<TEnv, TCtx, TReq>[]): Router<TEnv, TCtx, TReq> {
|
||||||
return this.register('CONNECT', url, handlers)
|
return this.register('CONNECT', url, handlers)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -197,7 +197,7 @@ export class Router<TEnv = any, TExt = any> {
|
|||||||
* @param {RouterHandler[]} handlers
|
* @param {RouterHandler[]} handlers
|
||||||
* @returns {Router}
|
* @returns {Router}
|
||||||
*/
|
*/
|
||||||
public delete(url: string, ...handlers: RouterHandler<TEnv, TExt>[]): Router<TEnv, TExt> {
|
public delete(url: string, ...handlers: RouterHandler<TEnv, TCtx, TReq>[]): Router<TEnv, TCtx, TReq> {
|
||||||
return this.register('DELETE', url, handlers)
|
return this.register('DELETE', url, handlers)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -208,7 +208,7 @@ export class Router<TEnv = any, TExt = any> {
|
|||||||
* @param {RouterHandler[]} handlers
|
* @param {RouterHandler[]} handlers
|
||||||
* @returns {Router}
|
* @returns {Router}
|
||||||
*/
|
*/
|
||||||
public get(url: string, ...handlers: RouterHandler<TEnv, TExt>[]): Router<TEnv, TExt> {
|
public get(url: string, ...handlers: RouterHandler<TEnv, TCtx, TReq>[]): Router<TEnv, TCtx, TReq> {
|
||||||
return this.register('GET', url, handlers)
|
return this.register('GET', url, handlers)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -219,7 +219,7 @@ export class Router<TEnv = any, TExt = any> {
|
|||||||
* @param {RouterHandler[]} handlers
|
* @param {RouterHandler[]} handlers
|
||||||
* @returns {Router}
|
* @returns {Router}
|
||||||
*/
|
*/
|
||||||
public head(url: string, ...handlers: RouterHandler<TEnv, TExt>[]): Router<TEnv, TExt> {
|
public head(url: string, ...handlers: RouterHandler<TEnv, TCtx, TReq>[]): Router<TEnv, TCtx, TReq> {
|
||||||
return this.register('HEAD', url, handlers)
|
return this.register('HEAD', url, handlers)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -230,7 +230,7 @@ export class Router<TEnv = any, TExt = any> {
|
|||||||
* @param {RouterHandler[]} handlers
|
* @param {RouterHandler[]} handlers
|
||||||
* @returns {Router}
|
* @returns {Router}
|
||||||
*/
|
*/
|
||||||
public options(url: string, ...handlers: RouterHandler<TEnv, TExt>[]): Router<TEnv, TExt> {
|
public options(url: string, ...handlers: RouterHandler<TEnv, TCtx, TReq>[]): Router<TEnv, TCtx, TReq> {
|
||||||
return this.register('OPTIONS', url, handlers)
|
return this.register('OPTIONS', url, handlers)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -241,7 +241,7 @@ export class Router<TEnv = any, TExt = any> {
|
|||||||
* @param {RouterHandler[]} handlers
|
* @param {RouterHandler[]} handlers
|
||||||
* @returns {Router}
|
* @returns {Router}
|
||||||
*/
|
*/
|
||||||
public patch(url: string, ...handlers: RouterHandler<TEnv, TExt>[]): Router<TEnv, TExt> {
|
public patch(url: string, ...handlers: RouterHandler<TEnv, TCtx, TReq>[]): Router<TEnv, TCtx, TReq> {
|
||||||
return this.register('PATCH', url, handlers)
|
return this.register('PATCH', url, handlers)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -252,7 +252,7 @@ export class Router<TEnv = any, TExt = any> {
|
|||||||
* @param {RouterHandler[]} handlers
|
* @param {RouterHandler[]} handlers
|
||||||
* @returns {Router}
|
* @returns {Router}
|
||||||
*/
|
*/
|
||||||
public post(url: string, ...handlers: RouterHandler<TEnv, TExt>[]): Router<TEnv, TExt> {
|
public post(url: string, ...handlers: RouterHandler<TEnv, TCtx, TReq>[]): Router<TEnv, TCtx, TReq> {
|
||||||
return this.register('POST', url, handlers)
|
return this.register('POST', url, handlers)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -263,7 +263,7 @@ export class Router<TEnv = any, TExt = any> {
|
|||||||
* @param {RouterHandler[]} handlers
|
* @param {RouterHandler[]} handlers
|
||||||
* @returns {Router}
|
* @returns {Router}
|
||||||
*/
|
*/
|
||||||
public put(url: string, ...handlers: RouterHandler<TEnv, TExt>[]): Router<TEnv, TExt> {
|
public put(url: string, ...handlers: RouterHandler<TEnv, TCtx, TReq>[]): Router<TEnv, TCtx, TReq> {
|
||||||
return this.register('PUT', url, handlers)
|
return this.register('PUT', url, handlers)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -274,7 +274,7 @@ export class Router<TEnv = any, TExt = any> {
|
|||||||
* @param {RouterHandler[]} handlers
|
* @param {RouterHandler[]} handlers
|
||||||
* @returns {Router}
|
* @returns {Router}
|
||||||
*/
|
*/
|
||||||
public trace(url: string, ...handlers: RouterHandler<TEnv, TExt>[]): Router<TEnv, TExt> {
|
public trace(url: string, ...handlers: RouterHandler<TEnv, TCtx, TReq>[]): Router<TEnv, TCtx, TReq> {
|
||||||
return this.register('TRACE', url, handlers)
|
return this.register('TRACE', url, handlers)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -285,7 +285,7 @@ export class Router<TEnv = any, TExt = any> {
|
|||||||
* @param {RouterHandler[]} handlers
|
* @param {RouterHandler[]} handlers
|
||||||
* @returns {Router}
|
* @returns {Router}
|
||||||
*/
|
*/
|
||||||
public any(url: string, ...handlers: RouterHandler<TEnv, TExt>[]): Router<TEnv, TExt> {
|
public any(url: string, ...handlers: RouterHandler<TEnv, TCtx, TReq>[]): Router<TEnv, TCtx, TReq> {
|
||||||
return this.register('*', url, handlers)
|
return this.register('*', url, handlers)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -295,7 +295,7 @@ export class Router<TEnv = any, TExt = any> {
|
|||||||
* @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<TEnv, TExt> {
|
public debug(state: boolean = true): Router<TEnv, TCtx, TReq> {
|
||||||
this.debugMode = state
|
this.debugMode = state
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
@@ -306,7 +306,7 @@ export class Router<TEnv = any, TExt = any> {
|
|||||||
* @param {RouterCorsConfig} [config]
|
* @param {RouterCorsConfig} [config]
|
||||||
* @returns {Router}
|
* @returns {Router}
|
||||||
*/
|
*/
|
||||||
public cors(config?: RouterCorsConfig): Router<TEnv, TExt> {
|
public cors(config?: RouterCorsConfig): Router<TEnv, TCtx, TReq> {
|
||||||
this.corsEnabled = true
|
this.corsEnabled = true
|
||||||
this.corsConfig = {
|
this.corsConfig = {
|
||||||
allowOrigin: config?.allowOrigin ?? '*',
|
allowOrigin: config?.allowOrigin ?? '*',
|
||||||
@@ -339,7 +339,7 @@ export class Router<TEnv = any, TExt = any> {
|
|||||||
* @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, TExt>[]): Router<TEnv, TExt> {
|
private register(method: string, url: string, handlers: RouterHandler<TEnv, TCtx, TReq>[]): Router<TEnv, TCtx, TReq> {
|
||||||
this.routes.push({
|
this.routes.push({
|
||||||
method,
|
method,
|
||||||
url,
|
url,
|
||||||
@@ -356,7 +356,7 @@ export class Router<TEnv = any, TExt = any> {
|
|||||||
* @param {RouterRequest} request
|
* @param {RouterRequest} request
|
||||||
* @returns {Route | undefined}
|
* @returns {Route | undefined}
|
||||||
*/
|
*/
|
||||||
private getRoute(request: RouterRequest<TExt>): Route<TEnv, TExt> | undefined {
|
private getRoute(request: RouterRequest<TReq>): Route<TEnv, TCtx, TReq> | 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)
|
||||||
|
|
||||||
@@ -395,12 +395,13 @@ export class Router<TEnv = any, TExt = any> {
|
|||||||
*
|
*
|
||||||
* @param {Request} request
|
* @param {Request} request
|
||||||
* @param {TEnv} env
|
* @param {TEnv} env
|
||||||
* @param {TExt} [ext]
|
* @param {TCtx} [extCtx]
|
||||||
|
* @param {TReq} [extReq]
|
||||||
* @returns {Promise<Response>}
|
* @returns {Promise<Response>}
|
||||||
*/
|
*/
|
||||||
public async handle(request: Request, env: TEnv, ctx?: ExecutionContext, ext?: TExt): Promise<Response> {
|
public async handle(request: Request, env: TEnv, ctx?: ExecutionContext, extCtx?: TCtx, extReq?: TReq): Promise<Response> {
|
||||||
const req = {
|
const req = {
|
||||||
...(ext ?? {}),
|
...(extReq ?? {}),
|
||||||
method: request.method,
|
method: request.method,
|
||||||
headers: request.headers,
|
headers: request.headers,
|
||||||
url: request.url,
|
url: request.url,
|
||||||
@@ -414,7 +415,7 @@ export class Router<TEnv = any, TExt = any> {
|
|||||||
formData: async (): Promise<FormData> => this.buffer.formData ? this.buffer.formData : this.buffer.formData = await request.formData(),
|
formData: async (): Promise<FormData> => this.buffer.formData ? this.buffer.formData : this.buffer.formData = await request.formData(),
|
||||||
blob: async (): Promise<Blob> => this.buffer.blob ? this.buffer.blob : this.buffer.blob = await request.blob(),
|
blob: async (): Promise<Blob> => this.buffer.blob ? this.buffer.blob : this.buffer.blob = await request.blob(),
|
||||||
bearer: () => request.headers.get('Authorization')?.replace(/^(B|b)earer /, '').trim()
|
bearer: () => request.headers.get('Authorization')?.replace(/^(B|b)earer /, '').trim()
|
||||||
} as RouterRequest<TExt>
|
} as RouterRequest<TReq>
|
||||||
|
|
||||||
if (this.corsEnabled && req.method === 'OPTIONS') {
|
if (this.corsEnabled && req.method === 'OPTIONS') {
|
||||||
return new Response(null, {
|
return new Response(null, {
|
||||||
@@ -434,7 +435,15 @@ export class Router<TEnv = any, TExt = any> {
|
|||||||
let response: Response | undefined
|
let response: Response | undefined
|
||||||
|
|
||||||
for (const handler of handlers) {
|
for (const handler of handlers) {
|
||||||
const res = await handler({ env, req, dbg, ctx })
|
const context = {
|
||||||
|
...(extCtx ?? {}),
|
||||||
|
env,
|
||||||
|
req,
|
||||||
|
dbg,
|
||||||
|
ctx
|
||||||
|
} as RouterContext<TEnv, TCtx, TReq>
|
||||||
|
|
||||||
|
const res = await handler(context)
|
||||||
|
|
||||||
if (res) {
|
if (res) {
|
||||||
response = res
|
response = res
|
||||||
|
|||||||
Reference in New Issue
Block a user