Compare commits
23 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
95ccb2263a
|
|||
|
dc88d900cb
|
|||
|
0dca90f4f9
|
|||
|
cb8724edc4
|
|||
|
08c329ff20
|
|||
|
e9a0a2436d
|
|||
|
76d7040a1b
|
|||
|
0037fb64ea
|
|||
|
|
a5ccf0fc01 | ||
|
|
e006d0a3b1 | ||
|
|
c7d7a642e6 | ||
|
|
49c7897bd0 | ||
|
e28976a4b6
|
|||
|
eea9e580f6
|
|||
|
68e614b3fa
|
|||
|
7a20454f79
|
|||
|
101de4f5f6
|
|||
|
9ee1182fa9
|
|||
|
0309b0131f
|
|||
|
afd7ea662f
|
|||
|
a742753cb7
|
|||
|
d631645b99
|
|||
|
cb1bf98053
|
@@ -7,3 +7,7 @@ insert_final_newline = false
|
|||||||
[src/**.ts]
|
[src/**.ts]
|
||||||
charset = utf-8
|
charset = utf-8
|
||||||
indent_style = tab
|
indent_style = tab
|
||||||
|
|
||||||
|
[README.md]
|
||||||
|
indent_style = space
|
||||||
|
indent_size = 4
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
name: Publish (pre)
|
name: Publish (main)
|
||||||
|
|
||||||
on:
|
on:
|
||||||
release:
|
release:
|
||||||
@@ -11,7 +11,7 @@ jobs:
|
|||||||
- uses: actions/checkout@v3
|
- uses: actions/checkout@v3
|
||||||
- uses: actions/setup-node@v3
|
- uses: actions/setup-node@v3
|
||||||
with:
|
with:
|
||||||
node-version: 16
|
node-version: latest
|
||||||
registry-url: https://registry.npmjs.org/
|
registry-url: https://registry.npmjs.org/
|
||||||
- name: Install dependencies
|
- name: Install dependencies
|
||||||
run: npm ci
|
run: npm ci
|
||||||
@@ -20,11 +20,12 @@ jobs:
|
|||||||
- name: Publish to npmjs
|
- name: Publish to npmjs
|
||||||
env:
|
env:
|
||||||
NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}
|
NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}
|
||||||
run: npm publish --tag pre --access public
|
run: npm publish --tag latest --access public
|
||||||
- uses: actions/setup-node@v1
|
- uses: actions/setup-node@v3
|
||||||
with:
|
with:
|
||||||
|
node-version: latest
|
||||||
registry-url: https://npm.pkg.github.com/
|
registry-url: https://npm.pkg.github.com/
|
||||||
- name: Publish to GPR
|
- name: Publish to GPR
|
||||||
env:
|
env:
|
||||||
NODE_AUTH_TOKEN: ${{secrets.GITHUB_TOKEN}}
|
NODE_AUTH_TOKEN: ${{secrets.GITHUB_TOKEN}}
|
||||||
run: npm publish --tag pre --access public
|
run: npm publish --tag latest --access public
|
||||||
@@ -7,7 +7,6 @@ From `v2.x.x` to `v3.x.x`.
|
|||||||
|
|
||||||
- [Update Router](#update-router)
|
- [Update Router](#update-router)
|
||||||
- [Handlers](#handlers)
|
- [Handlers](#handlers)
|
||||||
- [Fetch](#fetch--routerhandle)
|
|
||||||
|
|
||||||
|
|
||||||
## Update Router
|
## Update Router
|
||||||
@@ -15,7 +14,7 @@ From `v2.x.x` to `v3.x.x`.
|
|||||||
Update to the latest version version of the router.
|
Update to the latest version version of the router.
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
npm i -D @tsndr/cloudflare-worker-router
|
npm i -D @tsndr/cloudflare-worker-router@^3
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
91
README.md
91
README.md
@@ -33,6 +33,10 @@ Migrating from `v2.x.x`, check out the [Migration Guide](MIGRATION.md).
|
|||||||
```typescript
|
```typescript
|
||||||
import { Router } from '@tsndr/cloudflare-worker-router'
|
import { Router } from '@tsndr/cloudflare-worker-router'
|
||||||
|
|
||||||
|
// Env Types
|
||||||
|
export type Var<T = string> = T
|
||||||
|
export type Secret<T = string> = T
|
||||||
|
|
||||||
export type Env = {
|
export type Env = {
|
||||||
// Example binding to KV. Learn more at https://developers.cloudflare.com/workers/runtime-apis/kv/
|
// Example binding to KV. Learn more at https://developers.cloudflare.com/workers/runtime-apis/kv/
|
||||||
// MY_KV_NAMESPACE: KVNamespace
|
// MY_KV_NAMESPACE: KVNamespace
|
||||||
@@ -43,11 +47,23 @@ export type Env = {
|
|||||||
// Example binding to R2. Learn more at https://developers.cloudflare.com/workers/runtime-apis/r2/
|
// Example binding to R2. Learn more at https://developers.cloudflare.com/workers/runtime-apis/r2/
|
||||||
// MY_BUCKET: R2Bucket
|
// MY_BUCKET: R2Bucket
|
||||||
|
|
||||||
SECRET_TOKEN: string
|
ENVIRONMENT: Var<'dev' | 'prod'>
|
||||||
|
|
||||||
|
SECRET_TOKEN: Secret
|
||||||
}
|
}
|
||||||
|
|
||||||
// Initialize router
|
// Request Extension
|
||||||
const router = new Router<Env>()
|
export type ExtReq = {
|
||||||
|
userId?: number
|
||||||
|
}
|
||||||
|
|
||||||
|
// Context Extension
|
||||||
|
export type ExtCtx = {
|
||||||
|
//sentry?: Toucan
|
||||||
|
}
|
||||||
|
|
||||||
|
// Initialize Router
|
||||||
|
const router = new Router<Env, ExtCtx, ExtReq>()
|
||||||
|
|
||||||
// Enabling build in CORS support
|
// Enabling build in CORS support
|
||||||
router.cors()
|
router.cors()
|
||||||
@@ -210,13 +226,15 @@ If enabled will overwrite other `OPTIONS` requests.
|
|||||||
|
|
||||||
#### `config` (object, optional)
|
#### `config` (object, optional)
|
||||||
|
|
||||||
Key | Type | Default Value
|
Key | Type | Default Value
|
||||||
---------------------- | --------- | -------------
|
-------------------------- | ---------- | -------------
|
||||||
`allowOrigin` | `string` | `*`
|
`allowOrigin` | `string` | `*`
|
||||||
`allowMethods` | `string` | `*`
|
`allowMethods` | `string` | `*`
|
||||||
`allowHeaders` | `string` | `*`
|
`allowHeaders` | `string` | `*`
|
||||||
`maxAge` | `integer` | `86400`
|
`allowCredentials` | `boolean` | `undefined`
|
||||||
`optionsSuccessStatus` | `integer` | `204`
|
`vary` | `string` | `undefined`
|
||||||
|
`maxAge` | `integer` | `86400`
|
||||||
|
`optionsSuccessStatus` | `integer` | `204`
|
||||||
|
|
||||||
|
|
||||||
### Supported Methods
|
### Supported Methods
|
||||||
@@ -287,6 +305,10 @@ npm i -D @tsndr/cloudflare-worker-router
|
|||||||
```typescript
|
```typescript
|
||||||
import { Router } from '@tsndr/cloudflare-worker-router'
|
import { Router } from '@tsndr/cloudflare-worker-router'
|
||||||
|
|
||||||
|
// Env Types
|
||||||
|
export type Var<T = string> = T
|
||||||
|
export type Secret<T = string> = T
|
||||||
|
|
||||||
export type Env = {
|
export type Env = {
|
||||||
// Example binding to KV. Learn more at https://developers.cloudflare.com/workers/runtime-apis/kv/
|
// Example binding to KV. Learn more at https://developers.cloudflare.com/workers/runtime-apis/kv/
|
||||||
// MY_KV_NAMESPACE: KVNamespace
|
// MY_KV_NAMESPACE: KVNamespace
|
||||||
@@ -296,32 +318,57 @@ export type Env = {
|
|||||||
//
|
//
|
||||||
// Example binding to R2. Learn more at https://developers.cloudflare.com/workers/runtime-apis/r2/
|
// Example binding to R2. Learn more at https://developers.cloudflare.com/workers/runtime-apis/r2/
|
||||||
// MY_BUCKET: R2Bucket
|
// MY_BUCKET: R2Bucket
|
||||||
|
//
|
||||||
|
// Example Variable
|
||||||
|
// ENVIRONMENT: Var<'dev' | 'prod'>
|
||||||
|
//
|
||||||
|
// Example Secret
|
||||||
|
// JWT_SECRET: Secret
|
||||||
}
|
}
|
||||||
|
|
||||||
const router = new Router<Env>()
|
// Request Extension
|
||||||
|
export type ExtReq = {
|
||||||
|
userId?: number
|
||||||
|
}
|
||||||
|
|
||||||
|
// Context Extension
|
||||||
|
export type ExtCtx = {
|
||||||
|
//sentry?: Toucan
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handler Type
|
||||||
|
export type Handler = RouterHandler<Env, ExtCtx, ExtReq>
|
||||||
|
|
||||||
|
// Initialize Router
|
||||||
|
const router = new Router<Env, ExtCtx, ExtReq>()
|
||||||
|
|
||||||
|
// Enable Debug Mode
|
||||||
|
router.debug()
|
||||||
|
|
||||||
|
// Enabling build in CORS support
|
||||||
|
//router.cors()
|
||||||
|
|
||||||
/// Example Route
|
/// Example Route
|
||||||
//
|
//
|
||||||
// router.get('/hi', async () => {
|
// router.get('/hi', async () => {
|
||||||
// return new Response('Hello World')
|
// return new Response('Hello World')
|
||||||
//})
|
// })
|
||||||
|
|
||||||
|
|
||||||
/// Example Route for splitting into multiple files
|
/// Example Route for splitting into multiple files
|
||||||
//
|
//
|
||||||
// const hiHandler: RouteHandler<Env> = async () => {
|
// const helloHandler: Handler = async () => {
|
||||||
// return new Response('Hello World')
|
// return new Response('Hello World')
|
||||||
// }
|
// }
|
||||||
//
|
//
|
||||||
// router.get('/hi', hiHandler)
|
// router.get('/hellow', helloHandler)
|
||||||
|
|
||||||
|
|
||||||
// TODO: add your routes here
|
// TODO: add your routes here
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
async fetch(request: Request, env: Env, ctx: ExecutionContext) {
|
async fetch(request: Request, env: Env, ctx: ExecutionContext) {
|
||||||
return router.handle(request, env, ctx)
|
return router.handle(request, env, ctx)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -336,6 +383,12 @@ import { Router } from '@tsndr/cloudflare-worker-router'
|
|||||||
|
|
||||||
const router = new Router()
|
const router = new Router()
|
||||||
|
|
||||||
|
// Enable Debug Mode
|
||||||
|
//router.debug()
|
||||||
|
|
||||||
|
// Enabling build in CORS support
|
||||||
|
//router.cors()
|
||||||
|
|
||||||
/// Example Route
|
/// Example Route
|
||||||
//
|
//
|
||||||
// router.get('/hi', async () => {
|
// router.get('/hi', async () => {
|
||||||
|
|||||||
38
package-lock.json
generated
38
package-lock.json
generated
@@ -1,50 +1,36 @@
|
|||||||
{
|
{
|
||||||
"name": "@tsndr/cloudflare-worker-router",
|
"name": "@tsndr/cloudflare-worker-router",
|
||||||
"version": "3.0.0-13",
|
"version": "3.1.3",
|
||||||
"lockfileVersion": 2,
|
"lockfileVersion": 3,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"packages": {
|
"packages": {
|
||||||
"": {
|
"": {
|
||||||
"name": "@tsndr/cloudflare-worker-router",
|
"name": "@tsndr/cloudflare-worker-router",
|
||||||
"version": "3.0.0-13",
|
"version": "3.1.3",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@cloudflare/workers-types": "^4.20230115.0",
|
"@cloudflare/workers-types": "^4.20231025.0",
|
||||||
"typescript": "^4.9.5"
|
"typescript": "^5.2.2"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"node_modules/@cloudflare/workers-types": {
|
"node_modules/@cloudflare/workers-types": {
|
||||||
"version": "4.20230115.0",
|
"version": "4.20231025.0",
|
||||||
"resolved": "https://registry.npmjs.org/@cloudflare/workers-types/-/workers-types-4.20230115.0.tgz",
|
"resolved": "https://registry.npmjs.org/@cloudflare/workers-types/-/workers-types-4.20231025.0.tgz",
|
||||||
"integrity": "sha512-GPJEiO8AFN+jUpA+DHJ1qdVmk4s/hq8JYKjOV/+U7avGquQbVnj905+Kg6uAEfrq16muwmRKl+XJGqsvlBlDNg==",
|
"integrity": "sha512-TkcZkntUTOcvJ4vgmwpNfLTclpMbmbClZCe62B25/VTukmyv91joRa4eKzSjzCZUXTbFHNmVdOpmGaaJU2U3+A==",
|
||||||
"dev": true
|
"dev": true
|
||||||
},
|
},
|
||||||
"node_modules/typescript": {
|
"node_modules/typescript": {
|
||||||
"version": "4.9.5",
|
"version": "5.2.2",
|
||||||
"resolved": "https://registry.npmjs.org/typescript/-/typescript-4.9.5.tgz",
|
"resolved": "https://registry.npmjs.org/typescript/-/typescript-5.2.2.tgz",
|
||||||
"integrity": "sha512-1FXk9E2Hm+QzZQ7z+McJiHL4NW1F2EzMu9Nq9i3zAaGqibafqYwCVU6WyWAuyQRRzOlxou8xZSyXLEN8oKj24g==",
|
"integrity": "sha512-mI4WrpHsbCIcwT9cF4FZvr80QUeKvsUsUvKDoR+X/7XHQH98xYD8YHZg7ANtz2GtZt/CBq2QJ0thkGJMHfqc1w==",
|
||||||
"dev": true,
|
"dev": true,
|
||||||
"bin": {
|
"bin": {
|
||||||
"tsc": "bin/tsc",
|
"tsc": "bin/tsc",
|
||||||
"tsserver": "bin/tsserver"
|
"tsserver": "bin/tsserver"
|
||||||
},
|
},
|
||||||
"engines": {
|
"engines": {
|
||||||
"node": ">=4.2.0"
|
"node": ">=14.17"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
|
||||||
"dependencies": {
|
|
||||||
"@cloudflare/workers-types": {
|
|
||||||
"version": "4.20230115.0",
|
|
||||||
"resolved": "https://registry.npmjs.org/@cloudflare/workers-types/-/workers-types-4.20230115.0.tgz",
|
|
||||||
"integrity": "sha512-GPJEiO8AFN+jUpA+DHJ1qdVmk4s/hq8JYKjOV/+U7avGquQbVnj905+Kg6uAEfrq16muwmRKl+XJGqsvlBlDNg==",
|
|
||||||
"dev": true
|
|
||||||
},
|
|
||||||
"typescript": {
|
|
||||||
"version": "4.9.5",
|
|
||||||
"resolved": "https://registry.npmjs.org/typescript/-/typescript-4.9.5.tgz",
|
|
||||||
"integrity": "sha512-1FXk9E2Hm+QzZQ7z+McJiHL4NW1F2EzMu9Nq9i3zAaGqibafqYwCVU6WyWAuyQRRzOlxou8xZSyXLEN8oKj24g==",
|
|
||||||
"dev": true
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@tsndr/cloudflare-worker-router",
|
"name": "@tsndr/cloudflare-worker-router",
|
||||||
"version": "3.0.0-13",
|
"version": "3.1.3",
|
||||||
"description": "",
|
"description": "",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"types": "index.d.ts",
|
"types": "index.d.ts",
|
||||||
@@ -31,7 +31,7 @@
|
|||||||
},
|
},
|
||||||
"homepage": "https://github.com/tsndr/cloudflare-worker-router#readme",
|
"homepage": "https://github.com/tsndr/cloudflare-worker-router#readme",
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@cloudflare/workers-types": "^4.20230307.0",
|
"@cloudflare/workers-types": "^4.20231025.0",
|
||||||
"typescript": "^4.9.5"
|
"typescript": "^5.2.2"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
37
src/index.ts
37
src/index.ts
@@ -20,7 +20,7 @@ export type Route<TEnv, TCtx, TReq> = {
|
|||||||
* @property {RouterRequest} req Request Object
|
* @property {RouterRequest} req Request Object
|
||||||
* @property {ExecutionContext} ctx Context Object
|
* @property {ExecutionContext} ctx Context Object
|
||||||
*/
|
*/
|
||||||
export type RouterContext<TEnv = any, TCtx = any, TReq = any> = TCtx & {
|
export type RouterContext<TEnv = any, TCtx = {}, TReq = {}> = TCtx & {
|
||||||
env: TEnv
|
env: TEnv
|
||||||
req: RouterRequest<TReq>
|
req: RouterRequest<TReq>
|
||||||
dbg: boolean
|
dbg: boolean
|
||||||
@@ -80,7 +80,7 @@ export type RouterRequestQuery = {
|
|||||||
* @param {RouterContext} ctx
|
* @param {RouterContext} ctx
|
||||||
* @returns {Promise<Response | void> Response | void}
|
* @returns {Promise<Response | void> Response | void}
|
||||||
*/
|
*/
|
||||||
export type RouterHandler<TEnv = any, TCtx = any, TReq = any> = {
|
export type RouterHandler<TEnv = any, TCtx = {}, TReq = {}> = {
|
||||||
(ctx: RouterContext<TEnv, TCtx, TReq>): Promise<Response | void> | Response | void
|
(ctx: RouterContext<TEnv, TCtx, TReq>): Promise<Response | void> | Response | void
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -91,6 +91,8 @@ export type RouterHandler<TEnv = any, TCtx = any, TReq = any> = {
|
|||||||
* @property {string} [allowOrigin="*"] Access-Control-Allow-Origin (default: `*`)
|
* @property {string} [allowOrigin="*"] Access-Control-Allow-Origin (default: `*`)
|
||||||
* @property {string} [allowMethods="*"] Access-Control-Allow-Methods (default: `*`)
|
* @property {string} [allowMethods="*"] Access-Control-Allow-Methods (default: `*`)
|
||||||
* @property {string} [allowHeaders="*"] Access-Control-Allow-Headers (default: `*`)
|
* @property {string} [allowHeaders="*"] Access-Control-Allow-Headers (default: `*`)
|
||||||
|
* @property {boolean} [allowCredentials="true"] Access-Control-Allow-Credentials (default: undefined)
|
||||||
|
* @property {string} [vary="origin"] vary (default: undefined)
|
||||||
* @property {number} [maxAge=86400] Access-Control-Max-Age (default: `86400`)
|
* @property {number} [maxAge=86400] Access-Control-Max-Age (default: `86400`)
|
||||||
* @property {number} [optionsSuccessStatus=204] Return status code for OPTIONS request (default: `204`)
|
* @property {number} [optionsSuccessStatus=204] Return status code for OPTIONS request (default: `204`)
|
||||||
*/
|
*/
|
||||||
@@ -98,6 +100,8 @@ export type RouterCorsConfig = {
|
|||||||
allowOrigin?: string
|
allowOrigin?: string
|
||||||
allowMethods?: string
|
allowMethods?: string
|
||||||
allowHeaders?: string
|
allowHeaders?: string
|
||||||
|
allowCredentials?: boolean
|
||||||
|
vary?: string
|
||||||
maxAge?: number
|
maxAge?: number
|
||||||
optionsSuccessStatus?: number
|
optionsSuccessStatus?: number
|
||||||
}
|
}
|
||||||
@@ -116,7 +120,7 @@ export type RouterBuffer = {
|
|||||||
* @public
|
* @public
|
||||||
* @class
|
* @class
|
||||||
*/
|
*/
|
||||||
export class Router<TEnv = any, TCtx = any, TReq = any> {
|
export class Router<TEnv = any, TCtx = {}, TReq = {}> {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Router Array
|
* Router Array
|
||||||
@@ -150,14 +154,6 @@ export class Router<TEnv = any, TCtx = any, TReq = any> {
|
|||||||
*/
|
*/
|
||||||
protected corsConfig: RouterCorsConfig = {}
|
protected corsConfig: RouterCorsConfig = {}
|
||||||
|
|
||||||
/**
|
|
||||||
* Buffer
|
|
||||||
*
|
|
||||||
* @protected
|
|
||||||
* @type {RouterBuffer}
|
|
||||||
*/
|
|
||||||
protected buffer: RouterBuffer = {}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* CORS enabled
|
* CORS enabled
|
||||||
*
|
*
|
||||||
@@ -312,6 +308,8 @@ export class Router<TEnv = any, TCtx = any, TReq = any> {
|
|||||||
allowOrigin: config?.allowOrigin ?? '*',
|
allowOrigin: config?.allowOrigin ?? '*',
|
||||||
allowMethods: config?.allowMethods ?? '*',
|
allowMethods: config?.allowMethods ?? '*',
|
||||||
allowHeaders: config?.allowHeaders ?? '*',
|
allowHeaders: config?.allowHeaders ?? '*',
|
||||||
|
allowCredentials: config?.allowCredentials ?? undefined,
|
||||||
|
vary: config?.vary ?? undefined,
|
||||||
maxAge: config?.maxAge ?? 86400,
|
maxAge: config?.maxAge ?? 86400,
|
||||||
optionsSuccessStatus: config?.optionsSuccessStatus ?? 204
|
optionsSuccessStatus: config?.optionsSuccessStatus ?? 204
|
||||||
}
|
}
|
||||||
@@ -325,6 +323,10 @@ export class Router<TEnv = any, TCtx = any, TReq = any> {
|
|||||||
headers.set('Access-Control-Allow-Methods', this.corsConfig.allowMethods)
|
headers.set('Access-Control-Allow-Methods', this.corsConfig.allowMethods)
|
||||||
if (this.corsConfig.allowHeaders && !headers.has('Access-Control-Allow-Headers'))
|
if (this.corsConfig.allowHeaders && !headers.has('Access-Control-Allow-Headers'))
|
||||||
headers.set('Access-Control-Allow-Headers', this.corsConfig.allowHeaders)
|
headers.set('Access-Control-Allow-Headers', this.corsConfig.allowHeaders)
|
||||||
|
if (this.corsConfig.allowCredentials && !headers.has('Access-Control-Allow-Credentials'))
|
||||||
|
headers.set('Access-Control-Allow-Credentials', this.corsConfig.allowCredentials.toString())
|
||||||
|
if (this.corsConfig.vary && !headers.has('vary'))
|
||||||
|
headers.set('vary', this.corsConfig.vary.toString())
|
||||||
if (this.corsConfig.maxAge && !headers.has('Access-Control-Max-Age'))
|
if (this.corsConfig.maxAge && !headers.has('Access-Control-Max-Age'))
|
||||||
headers.set('Access-Control-Max-Age', this.corsConfig.maxAge.toString())
|
headers.set('Access-Control-Max-Age', this.corsConfig.maxAge.toString())
|
||||||
return headers
|
return headers
|
||||||
@@ -390,7 +392,7 @@ export class Router<TEnv = any, TCtx = any, TReq = any> {
|
|||||||
}) || this.routes.find(r => r.url === '*' && [request.method, '*'].includes(r.method))
|
}) || this.routes.find(r => r.url === '*' && [request.method, '*'].includes(r.method))
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handle requests
|
* Handle requests
|
||||||
*
|
*
|
||||||
* @param {Request} request
|
* @param {Request} request
|
||||||
@@ -400,6 +402,7 @@ export class Router<TEnv = any, TCtx = any, TReq = any> {
|
|||||||
* @returns {Promise<Response>}
|
* @returns {Promise<Response>}
|
||||||
*/
|
*/
|
||||||
public async handle(request: Request, env: TEnv, ctx?: ExecutionContext, extCtx?: TCtx, extReq?: TReq): Promise<Response> {
|
public async handle(request: Request, env: TEnv, ctx?: ExecutionContext, extCtx?: TCtx, extReq?: TReq): Promise<Response> {
|
||||||
|
const buffer: RouterBuffer = {};
|
||||||
const req = {
|
const req = {
|
||||||
...(extReq ?? {}),
|
...(extReq ?? {}),
|
||||||
method: request.method,
|
method: request.method,
|
||||||
@@ -409,11 +412,11 @@ export class Router<TEnv = any, TCtx = any, TReq = any> {
|
|||||||
raw: request,
|
raw: request,
|
||||||
params: {},
|
params: {},
|
||||||
query: {},
|
query: {},
|
||||||
arrayBuffer: async (): Promise<ArrayBuffer> => this.buffer.arrayBuffer ? this.buffer.arrayBuffer : this.buffer.arrayBuffer = await request.arrayBuffer(),
|
arrayBuffer: async (): Promise<ArrayBuffer> => buffer.arrayBuffer ? buffer.arrayBuffer : buffer.arrayBuffer = await request.clone().arrayBuffer(),
|
||||||
text: async (): Promise<string> => this.buffer.text ? this.buffer.text : this.buffer.text = await request.text(),
|
text: async (): Promise<string> => buffer.text ? buffer.text : buffer.text = await request.clone().text(),
|
||||||
json: async <T>(): Promise<T> => this.buffer.json ? this.buffer.json : this.buffer.json = await request.json<T>(),
|
json: async <T>(): Promise<T> => buffer.json ? buffer.json : buffer.json = await request.clone().json<T>(),
|
||||||
formData: async (): Promise<FormData> => this.buffer.formData ? this.buffer.formData : this.buffer.formData = await request.formData(),
|
formData: async (): Promise<FormData> => buffer.formData ? buffer.formData : buffer.formData = await request.clone().formData(),
|
||||||
blob: async (): Promise<Blob> => this.buffer.blob ? this.buffer.blob : this.buffer.blob = await request.blob(),
|
blob: async (): Promise<Blob> => buffer.blob ? buffer.blob : buffer.blob = await request.clone().blob(),
|
||||||
bearer: () => request.headers.get('Authorization')?.replace(/^(B|b)earer /, '').trim()
|
bearer: () => request.headers.get('Authorization')?.replace(/^(B|b)earer /, '').trim()
|
||||||
} as RouterRequest<TReq>
|
} as RouterRequest<TReq>
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user