1
0

2 Commits

Author SHA1 Message Date
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
3 changed files with 26 additions and 23 deletions

4
package-lock.json generated
View File

@@ -1,12 +1,12 @@
{
"name": "@tsndr/cloudflare-worker-router",
"version": "3.2.5",
"version": "3.2.6",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "@tsndr/cloudflare-worker-router",
"version": "3.2.5",
"version": "3.2.6",
"license": "MIT",
"devDependencies": {
"@cloudflare/workers-types": "^4.20240222.0",

View File

@@ -1,6 +1,6 @@
{
"name": "@tsndr/cloudflare-worker-router",
"version": "3.2.5",
"version": "3.2.6",
"description": "",
"type": "module",
"exports": "./index.js",

View File

@@ -406,34 +406,37 @@ export class Router<Env = any, CtxExt = {}, ReqExt = {}> {
}
const route = this.getRoute(req)
if (!route)
return new Response(this.debugMode ? 'Route not found!' : null, { status: 404 })
const handlers = [...this.globalHandlers, ...route.handlers]
const dbg = this.debugMode
let response: Response | undefined
for (const handler of handlers) {
const context = {
...(ctxExt ?? {}),
env,
req,
dbg,
ctx
} as RouterContext<Env, CtxExt, ReqExt>
if (!route)
response = new Response(this.debugMode ? 'Route not found!' : null, { status: 404 })
const res = await handler(context)
if (!response) {
const handlers = [
...this.globalHandlers,
...(route?.handlers ?? [])
]
if (res) {
response = res
break
for (const handler of handlers) {
const context = {
...(ctxExt ?? {}),
env,
req,
dbg: this.debugMode,
ctx
} as RouterContext<Env, CtxExt, ReqExt>
const res = await handler(context)
if (res) {
response = res
break
}
}
}
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) {
response = new Response(response.body, response)