1
0

Add env and ctx to router.handle()

This commit is contained in:
2022-03-18 17:57:35 +01:00
parent 2437f82e94
commit 6e6a771c1a
3 changed files with 33 additions and 8 deletions

24
index.d.ts vendored
View File

@@ -265,16 +265,32 @@ type RouterRequest = {
* HTTP request method * HTTP request method
*/ */
method: string method: string
/**
* Object containing request headers
*/
headers: Headers
/**
* URL String
*/
url: string
/**
* Environment object
*/
env: any
/**
* Context object
*/
ctx: any
/**
* Cloudflare object
*/
cf: any
/** /**
* Object containing all parameters defined in the url string * Object containing all parameters defined in the url string
*/ */
params: { params: {
[key: string]: string [key: string]: string
} }
/**
* Object containing request headers
*/
headers: Headers
/** /**
* Only available if method is `POST`, `PUT` or `PATCH`. Contains either the received body string or a parsed object if valid JSON was sent. * Only available if method is `POST`, `PUT` or `PATCH`. Contains either the received body string or a parsed object if valid JSON was sent.
*/ */

View File

@@ -294,16 +294,25 @@ class Router {
* Handle requests * Handle requests
* *
* @param {Request} request * @param {Request} request
* @param {any} env
* @param {any} ctx
* @returns {Response} * @returns {Response}
*/ */
async handle(request) { async handle(request, env, ctx) {
try { try {
if (request instanceof Event) { if (request instanceof Event) {
request = request.request request = request.request
console.warn("Warning: Using `event` on `router.handle()` is deprecated and might go away in future versions, please use `event.request` instead.") console.warn("Warning: Using `event` on `router.handle()` is deprecated and might go away in future versions, please use `event.request` instead.")
} }
const req = { headers: request.headers, method: request.method, url: request.url, cf: request.cf || {} } const req = {
req.params = [] method: request.method,
headers: request.headers,
url: request.url,
env: env || {},
ctx: ctx || {},
cf: request.cf || {},
params: []
}
if (req.method === 'OPTIONS' && Object.keys(this.corsConfig).length) { if (req.method === 'OPTIONS' && Object.keys(this.corsConfig).length) {
return new Response(null, { return new Response(null, {
headers: { headers: {

View File

@@ -1,6 +1,6 @@
{ {
"name": "@tsndr/cloudflare-worker-router", "name": "@tsndr/cloudflare-worker-router",
"version": "1.1.11", "version": "1.2.0",
"description": "", "description": "",
"main": "index.js", "main": "index.js",
"scripts": {}, "scripts": {},