1
0

3 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
fd7dce7256 3.2.5 2024-09-28 01:53:00 +02:00
3 changed files with 27 additions and 24 deletions

4
package-lock.json generated
View File

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

View File

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

View File

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