1
0

8 Commits

Author SHA1 Message Date
49d5806d89 3.2.9 2024-10-26 21:42:49 +02:00
73a0d4a4f9 update dependencies, build for esm 2024-10-26 21:42:35 +02:00
24a85e9814 3.2.8 2024-09-28 02:29:17 +02:00
dfdc50ba8a update dev dependencies 2024-09-28 02:29:04 +02:00
67bba2a339 3.2.7 2024-09-28 02:25:54 +02:00
c21e91bcb6 npm audit fix 2024-09-28 02:25:46 +02:00
fe2f8ec227 3.2.6 2024-09-28 02:19:32 +02:00
0e78ec4787 return cors headers for 404 response as well 2024-09-28 02:19:07 +02:00
4 changed files with 344 additions and 664 deletions

953
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -1,6 +1,6 @@
{ {
"name": "@tsndr/cloudflare-worker-router", "name": "@tsndr/cloudflare-worker-router",
"version": "3.2.5", "version": "3.2.9",
"description": "", "description": "",
"type": "module", "type": "module",
"exports": "./index.js", "exports": "./index.js",
@@ -32,9 +32,9 @@
}, },
"homepage": "https://github.com/tsndr/cloudflare-worker-router#readme", "homepage": "https://github.com/tsndr/cloudflare-worker-router#readme",
"devDependencies": { "devDependencies": {
"@cloudflare/workers-types": "^4.20240222.0", "@cloudflare/workers-types": "^4.20241022.0",
"@edge-runtime/vm": "^3.2.0", "@edge-runtime/vm": "^4.0.3",
"typescript": "^5.3.3", "typescript": "^5.6.3",
"vitest": "^1.3.1" "vitest": "^2.1.3"
} }
} }

View File

@@ -406,21 +406,23 @@ export class Router<Env = any, CtxExt = {}, ReqExt = {}> {
} }
const route = this.getRoute(req) const route = this.getRoute(req)
let response: Response | undefined
if (!route) if (!route)
return new Response(this.debugMode ? 'Route not found!' : null, { status: 404 }) response = new Response(this.debugMode ? 'Route not found!' : null, { status: 404 })
const handlers = [...this.globalHandlers, ...route.handlers] if (!response) {
const dbg = this.debugMode const handlers = [
...this.globalHandlers,
let response: Response | undefined ...(route?.handlers ?? [])
]
for (const handler of handlers) { for (const handler of handlers) {
const context = { const context = {
...(ctxExt ?? {}), ...(ctxExt ?? {}),
env, env,
req, req,
dbg, dbg: this.debugMode,
ctx ctx
} as RouterContext<Env, CtxExt, ReqExt> } as RouterContext<Env, CtxExt, ReqExt>
@@ -431,9 +433,10 @@ export class Router<Env = any, CtxExt = {}, ReqExt = {}> {
break break
} }
} }
}
if (!response) if (!response)
return new Response(this.debugMode ? 'Handler did not return a Response!' : null, { status: 404 }) response = new Response(this.debugMode ? 'Handler did not return a Response!' : null, { status: 404 })
if (this.corsEnabled) { if (this.corsEnabled) {
response = new Response(response.body, response) response = new Response(response.body, response)

View File

@@ -1,7 +1,7 @@
{ {
"compilerOptions": { "compilerOptions": {
"outDir": ".", "outDir": ".",
"module": "commonjs", "module": "esnext",
"target": "esnext", "target": "esnext",
"lib": ["esnext"], "lib": ["esnext"],
"declaration": true, "declaration": true,