Compare commits
3 Commits
v3.2.6
...
1b2956b437
| Author | SHA1 | Date | |
|---|---|---|---|
|
1b2956b437
|
|||
|
64458ca755
|
|||
|
eeea1102af
|
@@ -11,7 +11,3 @@ indent_style = tab
|
||||
[README.md]
|
||||
indent_style = space
|
||||
indent_size = 4
|
||||
|
||||
[{package,package-lock}.json]
|
||||
indent_style = space
|
||||
indent_size = 2
|
||||
4
.github/workflows/publish-main.yml
vendored
4
.github/workflows/publish-main.yml
vendored
@@ -1,8 +1,8 @@
|
||||
name: Publish
|
||||
name: Publish (main)
|
||||
|
||||
on:
|
||||
release:
|
||||
types: [ published ]
|
||||
types: [published]
|
||||
|
||||
jobs:
|
||||
publish:
|
||||
|
||||
21
.github/workflows/test.yml
vendored
21
.github/workflows/test.yml
vendored
@@ -1,21 +0,0 @@
|
||||
name: Test
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [ main ]
|
||||
pull_request:
|
||||
branches: [ main ]
|
||||
|
||||
jobs:
|
||||
run:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- uses: actions/setup-node@v3
|
||||
with:
|
||||
node-version: latest
|
||||
registry-url: https://registry.npmjs.org/
|
||||
- name: Install dependencies
|
||||
run: npm ci
|
||||
- name: Run tests
|
||||
run: npm test
|
||||
11
.npmignore
11
.npmignore
@@ -1,10 +1,7 @@
|
||||
.editorconfig
|
||||
.github/
|
||||
.gitignore
|
||||
.nvmrc
|
||||
coverage/
|
||||
jest.config.ts
|
||||
MIGRATION.md
|
||||
src/
|
||||
tests/
|
||||
test/
|
||||
.nvmrc
|
||||
MIGRATION.md
|
||||
tsconfig.json
|
||||
.editorconfig
|
||||
@@ -4,6 +4,8 @@ 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
|
||||
|
||||
@@ -238,6 +240,7 @@ Key | Type | Default Value
|
||||
### Supported Methods
|
||||
|
||||
- `router.any(url, [...handlers])`
|
||||
- `router.connect(url, [...handlers])`
|
||||
- `router.delete(url, [...handlers])`
|
||||
- `router.get(url, [...handlers])`
|
||||
- `router.head(url, [...handlers])`
|
||||
@@ -245,6 +248,7 @@ Key | Type | Default Value
|
||||
- `router.patch(url, [...handlers])`
|
||||
- `router.post(url, [...handlers])`
|
||||
- `router.put(url, [...handlers])`
|
||||
- `router.trace(url, [...handlers])`
|
||||
|
||||
|
||||
#### `url` (string)
|
||||
@@ -292,7 +296,7 @@ wrangler init <name>
|
||||
Use of TypeScript is strongly encouraged :)
|
||||
|
||||
```bash
|
||||
npm i @tsndr/cloudflare-worker-router
|
||||
npm i -D @tsndr/cloudflare-worker-router
|
||||
```
|
||||
|
||||
|
||||
|
||||
9
jest.config.ts
Normal file
9
jest.config.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
import type { Config } from 'jest'
|
||||
|
||||
const config: Config = {
|
||||
preset: 'ts-jest',
|
||||
testEnvironment: 'node',
|
||||
verbose: true
|
||||
}
|
||||
|
||||
export default config
|
||||
4513
package-lock.json
generated
4513
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
22
package.json
22
package.json
@@ -1,23 +1,22 @@
|
||||
{
|
||||
"name": "@tsndr/cloudflare-worker-router",
|
||||
"version": "3.2.6",
|
||||
"version": "3.1.3",
|
||||
"description": "",
|
||||
"type": "module",
|
||||
"exports": "./index.js",
|
||||
"main": "index.js",
|
||||
"types": "index.d.ts",
|
||||
"scripts": {
|
||||
"build": "tsc",
|
||||
"test": "vitest"
|
||||
"build": "tsc"
|
||||
},
|
||||
"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,9 +31,12 @@
|
||||
},
|
||||
"homepage": "https://github.com/tsndr/cloudflare-worker-router#readme",
|
||||
"devDependencies": {
|
||||
"@cloudflare/workers-types": "^4.20240222.0",
|
||||
"@edge-runtime/vm": "^3.2.0",
|
||||
"typescript": "^5.3.3",
|
||||
"vitest": "^1.3.1"
|
||||
"@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"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { describe, expect, test } from 'vitest'
|
||||
import { Router } from '../src/index'
|
||||
import { describe, expect, test } from '@jest/globals'
|
||||
import { Router } from '.'
|
||||
|
||||
describe('Router', () => {
|
||||
test('use', async () => {
|
||||
61
src/index.ts
61
src/index.ts
@@ -175,6 +175,17 @@ export class Router<Env = any, CtxExt = {}, ReqExt = {}> {
|
||||
return this
|
||||
}
|
||||
|
||||
/**
|
||||
* Register CONNECT route
|
||||
*
|
||||
* @param {string} url
|
||||
* @param {RouterHandler[]} handlers
|
||||
* @returns {Router}
|
||||
*/
|
||||
public connect(url: string, ...handlers: RouterHandler<Env, CtxExt, ReqExt>[]): Router<Env, CtxExt, ReqExt> {
|
||||
return this.register('CONNECT', url, handlers)
|
||||
}
|
||||
|
||||
/**
|
||||
* Register DELETE route
|
||||
*
|
||||
@@ -252,6 +263,17 @@ export class Router<Env = any, CtxExt = {}, ReqExt = {}> {
|
||||
return this.register('PUT', url, handlers)
|
||||
}
|
||||
|
||||
/**
|
||||
* Register TRACE route
|
||||
*
|
||||
* @param {string} url
|
||||
* @param {RouterHandler[]} handlers
|
||||
* @returns {Router}
|
||||
*/
|
||||
public trace(url: string, ...handlers: RouterHandler<Env, CtxExt, ReqExt>[]): Router<Env, CtxExt, ReqExt> {
|
||||
return this.register('TRACE', url, handlers)
|
||||
}
|
||||
|
||||
/**
|
||||
* Register route, ignoring method
|
||||
*
|
||||
@@ -406,37 +428,34 @@ export class Router<Env = any, CtxExt = {}, ReqExt = {}> {
|
||||
}
|
||||
|
||||
const route = this.getRoute(req)
|
||||
let response: Response | undefined
|
||||
|
||||
if (!route)
|
||||
response = new Response(this.debugMode ? 'Route not found!' : null, { status: 404 })
|
||||
return new Response(this.debugMode ? 'Route not found!' : null, { status: 404 })
|
||||
|
||||
if (!response) {
|
||||
const handlers = [
|
||||
...this.globalHandlers,
|
||||
...(route?.handlers ?? [])
|
||||
]
|
||||
const handlers = [...this.globalHandlers, ...route.handlers]
|
||||
const dbg = this.debugMode
|
||||
|
||||
for (const handler of handlers) {
|
||||
const context = {
|
||||
...(ctxExt ?? {}),
|
||||
env,
|
||||
req,
|
||||
dbg: this.debugMode,
|
||||
ctx
|
||||
} as RouterContext<Env, CtxExt, ReqExt>
|
||||
let response: Response | undefined
|
||||
|
||||
const res = await handler(context)
|
||||
for (const handler of handlers) {
|
||||
const context = {
|
||||
...(ctxExt ?? {}),
|
||||
env,
|
||||
req,
|
||||
dbg,
|
||||
ctx
|
||||
} as RouterContext<Env, CtxExt, ReqExt>
|
||||
|
||||
if (res) {
|
||||
response = res
|
||||
break
|
||||
}
|
||||
const res = await handler(context)
|
||||
|
||||
if (res) {
|
||||
response = res
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if (!response)
|
||||
response = new Response(this.debugMode ? 'Handler did not return a Response!' : null, { status: 404 })
|
||||
return new Response(this.debugMode ? 'Handler did not return a Response!' : null, { status: 404 })
|
||||
|
||||
if (this.corsEnabled) {
|
||||
response = new Response(response.body, response)
|
||||
|
||||
@@ -1,9 +0,0 @@
|
||||
import { defineConfig } from 'vitest/config'
|
||||
|
||||
export default defineConfig({
|
||||
test: {
|
||||
environment: 'edge-runtime',
|
||||
reporters: ['verbose'],
|
||||
watch: false
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user