Compare commits
10 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
67bba2a339
|
|||
|
c21e91bcb6
|
|||
|
fe2f8ec227
|
|||
|
0e78ec4787
|
|||
|
fd7dce7256
|
|||
|
1d72a15ca6
|
|||
|
a740f1b103
|
|||
|
21dc38b676
|
|||
|
9df19a50cd
|
|||
|
7dce99214d
|
@@ -11,3 +11,7 @@ indent_style = tab
|
||||
[README.md]
|
||||
indent_style = space
|
||||
indent_size = 4
|
||||
|
||||
[{package,package-lock}.json]
|
||||
indent_style = space
|
||||
indent_size = 2
|
||||
@@ -4,8 +4,6 @@ Cloudflare Workers Router is a super lightweight router (1.0K gzipped) with midd
|
||||
|
||||
When I was trying out Cloudflare Workers I almost immediately noticed how fast it was compared to other serverless offerings. So I wanted to build a full-fledged API to see how it performs doing real work, but since I wasn't able to find a router that suited my needs I created my own.
|
||||
|
||||
I worked a lot with [Express.js](https://expressjs.com/) in the past and really enjoyed their middleware approach, but since none of the available Cloudflare Worker routers offered middleware support at the time, I felt the need to create this router.
|
||||
|
||||
|
||||
## Contents
|
||||
|
||||
@@ -294,7 +292,7 @@ wrangler init <name>
|
||||
Use of TypeScript is strongly encouraged :)
|
||||
|
||||
```bash
|
||||
npm i -D @tsndr/cloudflare-worker-router
|
||||
npm i @tsndr/cloudflare-worker-router
|
||||
```
|
||||
|
||||
|
||||
|
||||
@@ -1,9 +0,0 @@
|
||||
import type { Config } from 'jest'
|
||||
|
||||
const config: Config = {
|
||||
preset: 'ts-jest',
|
||||
testEnvironment: 'node',
|
||||
verbose: true
|
||||
}
|
||||
|
||||
export default config
|
||||
4512
package-lock.json
generated
4512
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
21
package.json
21
package.json
@@ -1,23 +1,23 @@
|
||||
{
|
||||
"name": "@tsndr/cloudflare-worker-router",
|
||||
"version": "3.2.4",
|
||||
"version": "3.2.7",
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"type": "module",
|
||||
"exports": "./index.js",
|
||||
"types": "index.d.ts",
|
||||
"scripts": {
|
||||
"build": "tsc",
|
||||
"test": "jest"
|
||||
"test": "vitest"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/tsndr/cloudflare-worker-router.git"
|
||||
},
|
||||
"keywords": [
|
||||
"api",
|
||||
"cloudflare",
|
||||
"cloudflare-worker",
|
||||
"cloudflare-workers",
|
||||
"express",
|
||||
"expressjs",
|
||||
"framework",
|
||||
"middleware",
|
||||
"router",
|
||||
@@ -32,12 +32,9 @@
|
||||
},
|
||||
"homepage": "https://github.com/tsndr/cloudflare-worker-router#readme",
|
||||
"devDependencies": {
|
||||
"@cloudflare/workers-types": "^4.20231025.0",
|
||||
"@jest/globals": "^29.7.0",
|
||||
"@types/jest": "^29.5.11",
|
||||
"jest": "^29.7.0",
|
||||
"ts-jest": "^29.1.1",
|
||||
"ts-node": "^10.9.2",
|
||||
"typescript": "^5.2.2"
|
||||
"@cloudflare/workers-types": "^4.20240222.0",
|
||||
"@edge-runtime/vm": "^3.2.0",
|
||||
"typescript": "^5.3.3",
|
||||
"vitest": "^1.3.1"
|
||||
}
|
||||
}
|
||||
|
||||
43
src/index.ts
43
src/index.ts
@@ -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)
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { describe, expect, test } from '@jest/globals'
|
||||
import { describe, expect, test } from 'vitest'
|
||||
import { Router } from '../src/index'
|
||||
|
||||
describe('Router', () => {
|
||||
|
||||
9
vite.config.ts
Normal file
9
vite.config.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
import { defineConfig } from 'vitest/config'
|
||||
|
||||
export default defineConfig({
|
||||
test: {
|
||||
environment: 'edge-runtime',
|
||||
reporters: ['verbose'],
|
||||
watch: false
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user