Compare commits
4 Commits
v3.1.2
...
a4fd646d6b
| Author | SHA1 | Date | |
|---|---|---|---|
|
a4fd646d6b
|
|||
|
606d5f1cb2
|
|||
|
95ccb2263a
|
|||
|
dc88d900cb
|
4
package-lock.json
generated
4
package-lock.json
generated
@@ -1,12 +1,12 @@
|
|||||||
{
|
{
|
||||||
"name": "@tsndr/cloudflare-worker-router",
|
"name": "@tsndr/cloudflare-worker-router",
|
||||||
"version": "3.1.2",
|
"version": "3.1.3",
|
||||||
"lockfileVersion": 3,
|
"lockfileVersion": 3,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"packages": {
|
"packages": {
|
||||||
"": {
|
"": {
|
||||||
"name": "@tsndr/cloudflare-worker-router",
|
"name": "@tsndr/cloudflare-worker-router",
|
||||||
"version": "3.1.2",
|
"version": "3.1.3",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@cloudflare/workers-types": "^4.20231025.0",
|
"@cloudflare/workers-types": "^4.20231025.0",
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@tsndr/cloudflare-worker-router",
|
"name": "@tsndr/cloudflare-worker-router",
|
||||||
"version": "3.1.2",
|
"version": "3.1.3",
|
||||||
"description": "",
|
"description": "",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"types": "index.d.ts",
|
"types": "index.d.ts",
|
||||||
|
|||||||
90
src/index.ts
90
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, TCtx, TReq> = {
|
export type Route<Env, CtxExt, ReqExt> = {
|
||||||
method: string
|
method: string
|
||||||
url: string
|
url: string
|
||||||
handlers: RouterHandler<TEnv, TCtx, TReq>[]
|
handlers: RouterHandler<Env, CtxExt, ReqExt>[]
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -20,9 +20,9 @@ export type Route<TEnv, TCtx, TReq> = {
|
|||||||
* @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, TCtx = any, TReq = any> = TCtx & {
|
export type RouterContext<Env = any, CtxExt = {}, ReqExt = {}> = CtxExt & {
|
||||||
env: TEnv
|
env: Env
|
||||||
req: RouterRequest<TReq>
|
req: RouterRequest<ReqExt>
|
||||||
dbg: boolean
|
dbg: boolean
|
||||||
ctx?: ExecutionContext
|
ctx?: ExecutionContext
|
||||||
}
|
}
|
||||||
@@ -39,7 +39,7 @@ export type RouterContext<TEnv = any, TCtx = any, TReq = any> = TCtx & {
|
|||||||
* @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<TReq> = TReq & {
|
export type RouterRequest<ReqExt> = ReqExt & {
|
||||||
url: string
|
url: string
|
||||||
method: string
|
method: string
|
||||||
params: RouterRequestParams
|
params: RouterRequestParams
|
||||||
@@ -51,7 +51,7 @@ export type RouterRequest<TReq> = TReq & {
|
|||||||
json<T>(): Promise<T>
|
json<T>(): Promise<T>
|
||||||
formData(): Promise<FormData>
|
formData(): Promise<FormData>
|
||||||
blob(): Promise<Blob>
|
blob(): Promise<Blob>
|
||||||
bearer: () => string | undefined
|
bearer(): string | undefined
|
||||||
cf?: IncomingRequestCfProperties
|
cf?: IncomingRequestCfProperties
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -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, TCtx = any, TReq = any> = {
|
export type RouterHandler<Env = any, CtxExt = {}, ReqExt = {}> = {
|
||||||
(ctx: RouterContext<TEnv, TCtx, TReq>): Promise<Response | void> | Response | void
|
(ctx: RouterContext<Env, CtxExt, ReqExt>): Promise<Response | void> | Response | void
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -120,7 +120,7 @@ export type RouterBuffer = {
|
|||||||
* @public
|
* @public
|
||||||
* @class
|
* @class
|
||||||
*/
|
*/
|
||||||
export class Router<TEnv = any, TCtx = any, TReq = any> {
|
export class Router<Env = any, CtxExt = {}, ReqExt = {}> {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Router Array
|
* Router Array
|
||||||
@@ -128,7 +128,7 @@ export class Router<TEnv = any, TCtx = any, TReq = any> {
|
|||||||
* @protected
|
* @protected
|
||||||
* @type {Route[]}
|
* @type {Route[]}
|
||||||
*/
|
*/
|
||||||
protected routes: Route<TEnv, TCtx, TReq>[] = []
|
protected routes: Route<Env, CtxExt, ReqExt>[] = []
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Global Handlers
|
* Global Handlers
|
||||||
@@ -136,7 +136,7 @@ export class Router<TEnv = any, TCtx = any, TReq = any> {
|
|||||||
* @protected
|
* @protected
|
||||||
* @type {RouterHandler[]}
|
* @type {RouterHandler[]}
|
||||||
*/
|
*/
|
||||||
protected globalHandlers: RouterHandler<TEnv, TCtx, TReq>[] = []
|
protected globalHandlers: RouterHandler<Env, CtxExt, ReqExt>[] = []
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Debug Mode
|
* Debug Mode
|
||||||
@@ -168,24 +168,13 @@ export class Router<TEnv = any, TCtx = any, TReq = any> {
|
|||||||
* @param {RouterHandler[]} handlers
|
* @param {RouterHandler[]} handlers
|
||||||
* @returns {Router}
|
* @returns {Router}
|
||||||
*/
|
*/
|
||||||
public use(...handlers: RouterHandler<TEnv, TCtx, TReq>[]): Router<TEnv, TCtx, TReq> {
|
public use(...handlers: RouterHandler<Env, CtxExt, ReqExt>[]): Router<Env, CtxExt, ReqExt> {
|
||||||
for (let handler of handlers) {
|
for (let handler of handlers) {
|
||||||
this.globalHandlers.push(handler)
|
this.globalHandlers.push(handler)
|
||||||
}
|
}
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Register CONNECT route
|
|
||||||
*
|
|
||||||
* @param {string} url
|
|
||||||
* @param {RouterHandler[]} handlers
|
|
||||||
* @returns {Router}
|
|
||||||
*/
|
|
||||||
public connect(url: string, ...handlers: RouterHandler<TEnv, TCtx, TReq>[]): Router<TEnv, TCtx, TReq> {
|
|
||||||
return this.register('CONNECT', url, handlers)
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Register DELETE route
|
* Register DELETE route
|
||||||
*
|
*
|
||||||
@@ -193,7 +182,7 @@ export class Router<TEnv = any, TCtx = any, TReq = any> {
|
|||||||
* @param {RouterHandler[]} handlers
|
* @param {RouterHandler[]} handlers
|
||||||
* @returns {Router}
|
* @returns {Router}
|
||||||
*/
|
*/
|
||||||
public delete(url: string, ...handlers: RouterHandler<TEnv, TCtx, TReq>[]): Router<TEnv, TCtx, TReq> {
|
public delete(url: string, ...handlers: RouterHandler<Env, CtxExt, ReqExt>[]): Router<Env, CtxExt, ReqExt> {
|
||||||
return this.register('DELETE', url, handlers)
|
return this.register('DELETE', url, handlers)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -204,7 +193,7 @@ export class Router<TEnv = any, TCtx = any, TReq = any> {
|
|||||||
* @param {RouterHandler[]} handlers
|
* @param {RouterHandler[]} handlers
|
||||||
* @returns {Router}
|
* @returns {Router}
|
||||||
*/
|
*/
|
||||||
public get(url: string, ...handlers: RouterHandler<TEnv, TCtx, TReq>[]): Router<TEnv, TCtx, TReq> {
|
public get(url: string, ...handlers: RouterHandler<Env, CtxExt, ReqExt>[]): Router<Env, CtxExt, ReqExt> {
|
||||||
return this.register('GET', url, handlers)
|
return this.register('GET', url, handlers)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -215,7 +204,7 @@ export class Router<TEnv = any, TCtx = any, TReq = any> {
|
|||||||
* @param {RouterHandler[]} handlers
|
* @param {RouterHandler[]} handlers
|
||||||
* @returns {Router}
|
* @returns {Router}
|
||||||
*/
|
*/
|
||||||
public head(url: string, ...handlers: RouterHandler<TEnv, TCtx, TReq>[]): Router<TEnv, TCtx, TReq> {
|
public head(url: string, ...handlers: RouterHandler<Env, CtxExt, ReqExt>[]): Router<Env, CtxExt, ReqExt> {
|
||||||
return this.register('HEAD', url, handlers)
|
return this.register('HEAD', url, handlers)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -226,7 +215,7 @@ export class Router<TEnv = any, TCtx = any, TReq = any> {
|
|||||||
* @param {RouterHandler[]} handlers
|
* @param {RouterHandler[]} handlers
|
||||||
* @returns {Router}
|
* @returns {Router}
|
||||||
*/
|
*/
|
||||||
public options(url: string, ...handlers: RouterHandler<TEnv, TCtx, TReq>[]): Router<TEnv, TCtx, TReq> {
|
public options(url: string, ...handlers: RouterHandler<Env, CtxExt, ReqExt>[]): Router<Env, CtxExt, ReqExt> {
|
||||||
return this.register('OPTIONS', url, handlers)
|
return this.register('OPTIONS', url, handlers)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -237,7 +226,7 @@ export class Router<TEnv = any, TCtx = any, TReq = any> {
|
|||||||
* @param {RouterHandler[]} handlers
|
* @param {RouterHandler[]} handlers
|
||||||
* @returns {Router}
|
* @returns {Router}
|
||||||
*/
|
*/
|
||||||
public patch(url: string, ...handlers: RouterHandler<TEnv, TCtx, TReq>[]): Router<TEnv, TCtx, TReq> {
|
public patch(url: string, ...handlers: RouterHandler<Env, CtxExt, ReqExt>[]): Router<Env, CtxExt, ReqExt> {
|
||||||
return this.register('PATCH', url, handlers)
|
return this.register('PATCH', url, handlers)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -248,7 +237,7 @@ export class Router<TEnv = any, TCtx = any, TReq = any> {
|
|||||||
* @param {RouterHandler[]} handlers
|
* @param {RouterHandler[]} handlers
|
||||||
* @returns {Router}
|
* @returns {Router}
|
||||||
*/
|
*/
|
||||||
public post(url: string, ...handlers: RouterHandler<TEnv, TCtx, TReq>[]): Router<TEnv, TCtx, TReq> {
|
public post(url: string, ...handlers: RouterHandler<Env, CtxExt, ReqExt>[]): Router<Env, CtxExt, ReqExt> {
|
||||||
return this.register('POST', url, handlers)
|
return this.register('POST', url, handlers)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -259,21 +248,10 @@ export class Router<TEnv = any, TCtx = any, TReq = any> {
|
|||||||
* @param {RouterHandler[]} handlers
|
* @param {RouterHandler[]} handlers
|
||||||
* @returns {Router}
|
* @returns {Router}
|
||||||
*/
|
*/
|
||||||
public put(url: string, ...handlers: RouterHandler<TEnv, TCtx, TReq>[]): Router<TEnv, TCtx, TReq> {
|
public put(url: string, ...handlers: RouterHandler<Env, CtxExt, ReqExt>[]): Router<Env, CtxExt, ReqExt> {
|
||||||
return this.register('PUT', url, handlers)
|
return this.register('PUT', url, handlers)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Register TRACE route
|
|
||||||
*
|
|
||||||
* @param {string} url
|
|
||||||
* @param {RouterHandler[]} handlers
|
|
||||||
* @returns {Router}
|
|
||||||
*/
|
|
||||||
public trace(url: string, ...handlers: RouterHandler<TEnv, TCtx, TReq>[]): Router<TEnv, TCtx, TReq> {
|
|
||||||
return this.register('TRACE', url, handlers)
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Register route, ignoring method
|
* Register route, ignoring method
|
||||||
*
|
*
|
||||||
@@ -281,7 +259,7 @@ export class Router<TEnv = any, TCtx = any, TReq = any> {
|
|||||||
* @param {RouterHandler[]} handlers
|
* @param {RouterHandler[]} handlers
|
||||||
* @returns {Router}
|
* @returns {Router}
|
||||||
*/
|
*/
|
||||||
public any(url: string, ...handlers: RouterHandler<TEnv, TCtx, TReq>[]): Router<TEnv, TCtx, TReq> {
|
public any(url: string, ...handlers: RouterHandler<Env, CtxExt, ReqExt>[]): Router<Env, CtxExt, ReqExt> {
|
||||||
return this.register('*', url, handlers)
|
return this.register('*', url, handlers)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -291,7 +269,7 @@ export class Router<TEnv = any, TCtx = any, TReq = 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, TCtx, TReq> {
|
public debug(state: boolean = true): Router<Env, CtxExt, ReqExt> {
|
||||||
this.debugMode = state
|
this.debugMode = state
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
@@ -302,7 +280,7 @@ export class Router<TEnv = any, TCtx = any, TReq = any> {
|
|||||||
* @param {RouterCorsConfig} [config]
|
* @param {RouterCorsConfig} [config]
|
||||||
* @returns {Router}
|
* @returns {Router}
|
||||||
*/
|
*/
|
||||||
public cors(config?: RouterCorsConfig): Router<TEnv, TCtx, TReq> {
|
public cors(config?: RouterCorsConfig): Router<Env, CtxExt, ReqExt> {
|
||||||
this.corsEnabled = true
|
this.corsEnabled = true
|
||||||
this.corsConfig = {
|
this.corsConfig = {
|
||||||
allowOrigin: config?.allowOrigin ?? '*',
|
allowOrigin: config?.allowOrigin ?? '*',
|
||||||
@@ -341,7 +319,7 @@ export class Router<TEnv = any, TCtx = any, TReq = 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, TCtx, TReq>[]): Router<TEnv, TCtx, TReq> {
|
private register(method: string, url: string, handlers: RouterHandler<Env, CtxExt, ReqExt>[]): Router<Env, CtxExt, ReqExt> {
|
||||||
this.routes.push({
|
this.routes.push({
|
||||||
method,
|
method,
|
||||||
url,
|
url,
|
||||||
@@ -358,7 +336,7 @@ export class Router<TEnv = any, TCtx = any, TReq = any> {
|
|||||||
* @param {RouterRequest} request
|
* @param {RouterRequest} request
|
||||||
* @returns {Route | undefined}
|
* @returns {Route | undefined}
|
||||||
*/
|
*/
|
||||||
private getRoute(request: RouterRequest<TReq>): Route<TEnv, TCtx, TReq> | undefined {
|
private getRoute(request: RouterRequest<ReqExt>): Route<Env, CtxExt, ReqExt> | 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)
|
||||||
|
|
||||||
@@ -396,15 +374,15 @@ export class Router<TEnv = any, TCtx = any, TReq = any> {
|
|||||||
* Handle requests
|
* Handle requests
|
||||||
*
|
*
|
||||||
* @param {Request} request
|
* @param {Request} request
|
||||||
* @param {TEnv} env
|
* @param {Env} env
|
||||||
* @param {TCtx} [extCtx]
|
* @param {CtxExt} [ctxExt]
|
||||||
* @param {TReq} [extReq]
|
* @param {ReqExt} [reqExt]
|
||||||
* @returns {Promise<Response>}
|
* @returns {Promise<Response>}
|
||||||
*/
|
*/
|
||||||
public async handle(request: Request, env: TEnv, ctx?: ExecutionContext, extCtx?: TCtx, extReq?: TReq): Promise<Response> {
|
public async handle(request: Request, env: Env, ctx?: ExecutionContext, ctxExt?: CtxExt, reqExt?: ReqExt): Promise<Response> {
|
||||||
const buffer: RouterBuffer = {};
|
const buffer: RouterBuffer = {}
|
||||||
const req = {
|
const req = {
|
||||||
...(extReq ?? {}),
|
...(reqExt ?? {}),
|
||||||
method: request.method,
|
method: request.method,
|
||||||
headers: request.headers,
|
headers: request.headers,
|
||||||
url: request.url,
|
url: request.url,
|
||||||
@@ -418,7 +396,7 @@ export class Router<TEnv = any, TCtx = any, TReq = any> {
|
|||||||
formData: async (): Promise<FormData> => buffer.formData ? buffer.formData : buffer.formData = await request.clone().formData(),
|
formData: async (): Promise<FormData> => buffer.formData ? buffer.formData : buffer.formData = await request.clone().formData(),
|
||||||
blob: async (): Promise<Blob> => buffer.blob ? buffer.blob : buffer.blob = await request.clone().blob(),
|
blob: async (): Promise<Blob> => buffer.blob ? buffer.blob : buffer.blob = await request.clone().blob(),
|
||||||
bearer: () => request.headers.get('Authorization')?.replace(/^(B|b)earer /, '').trim()
|
bearer: () => request.headers.get('Authorization')?.replace(/^(B|b)earer /, '').trim()
|
||||||
} as RouterRequest<TReq>
|
} as RouterRequest<ReqExt>
|
||||||
|
|
||||||
if (this.corsEnabled && req.method === 'OPTIONS') {
|
if (this.corsEnabled && req.method === 'OPTIONS') {
|
||||||
return new Response(null, {
|
return new Response(null, {
|
||||||
@@ -439,12 +417,12 @@ export class Router<TEnv = any, TCtx = any, TReq = any> {
|
|||||||
|
|
||||||
for (const handler of handlers) {
|
for (const handler of handlers) {
|
||||||
const context = {
|
const context = {
|
||||||
...(extCtx ?? {}),
|
...(ctxExt ?? {}),
|
||||||
env,
|
env,
|
||||||
req,
|
req,
|
||||||
dbg,
|
dbg,
|
||||||
ctx
|
ctx
|
||||||
} as RouterContext<TEnv, TCtx, TReq>
|
} as RouterContext<Env, CtxExt, ReqExt>
|
||||||
|
|
||||||
const res = await handler(context)
|
const res = await handler(context)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user