From d37f37ff92473b88f72deac7ea61c4748baef78f Mon Sep 17 00:00:00 2001 From: Jithin Shah Date: Tue, 16 Nov 2021 12:37:46 +0530 Subject: [PATCH 1/4] Add cf object to express req object --- index.d.ts | 1 + index.js | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/index.d.ts b/index.d.ts index 0b2a94e..6c4b890 100644 --- a/index.d.ts +++ b/index.d.ts @@ -45,6 +45,7 @@ declare class Router { * @property {Object} query Object containing all query parameters * @property {Object} headers Object containing request headers * @property {Object|string} body Only available if method is `POST`, `PUT` or `PATCH`. Contains either the received body string or a parsed object if valid JSON was sent. + * @property {Object} cf object containing custom Cloudflare properties. (https://developers.cloudflare.com/workers/examples/accessing-the-cloudflare-object) */ /** * Response Object diff --git a/index.js b/index.js index 3c10453..532a29c 100644 --- a/index.js +++ b/index.js @@ -51,6 +51,7 @@ class Router { * @property {Object} query Object containing all query parameters * @property {Object} headers Object containing request headers * @property {Object|string} body Only available if method is `POST`, `PUT` or `PATCH`. Contains either the received body string or a parsed object if valid JSON was sent. + * @property {Object} cf object containing custom Cloudflare properties. (https://developers.cloudflare.com/workers/examples/accessing-the-cloudflare-object) */ /** @@ -300,7 +301,7 @@ class Router { 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.") } - const req = { headers: request.headers, method: request.method, url: request.url } + const req = { headers: request.headers, method: request.method, url: request.url, cf: request.cf || {} } req.params = [] if (req.method === 'OPTIONS' && Object.keys(this.corsConfig).length) { return new Response(null, { From ae0b311f717f2708683798730686d1d93bbd528b Mon Sep 17 00:00:00 2001 From: leko Date: Sun, 19 Dec 2021 05:41:07 +0800 Subject: [PATCH 2/4] Allow body in DELETE method --- index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 3c10453..684d4b9 100644 --- a/index.js +++ b/index.js @@ -50,7 +50,7 @@ class Router { * @property {Object} params Object containing all parameters defined in the url string * @property {Object} query Object containing all query parameters * @property {Object} headers Object containing request headers - * @property {Object|string} body Only available if method is `POST`, `PUT` or `PATCH`. Contains either the received body string or a parsed object if valid JSON was sent. + * @property {Object|string} body Only available if method is `POST`, `PUT`, `PATCH` or `DELETE`. Contains either the received body string or a parsed object if valid JSON was sent. */ /** @@ -313,7 +313,7 @@ class Router { status: this.corsConfig.optionsSuccessStatus }) } - if (['POST', 'PUT', 'PATCH'].includes(req.method)) { + if (['POST', 'PUT', 'PATCH', 'DELETE'].includes(req.method)) { if (req.headers.has('Content-Type') && req.headers.get('Content-Type').includes('json')) { try { req.body = await request.json() From 45c96a40481b6df1349632d304d107f78c6da42b Mon Sep 17 00:00:00 2001 From: Tobias Schneider Date: Sun, 16 Jan 2022 17:24:18 +0100 Subject: [PATCH 3/4] Update to v1.1.8 --- README.md | 2 +- index.d.ts | 2 +- package.json | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 7da8822..605b9fc 100644 --- a/README.md +++ b/README.md @@ -129,7 +129,7 @@ An unlimited number of functions getting [`req`](#req-object) and [`res`](#res-o Key | Type | Description --------- | ------------------- | ----------- -`body` | `object` / `string` | Only available if method is `POST`, `PUT` or `PATCH`. Contains either the received body string or a parsed object if valid JSON was sent. +`body` | `object` / `string` | Only available if method is `POST`, `PUT`, `PATCH` or `DELETE`. Contains either the received body string or a parsed object if valid JSON was sent. `headers` | `object` | Object containing request headers `method` | `string` | HTTP request method `params` | `object` | Object containing all parameters defined in the url string diff --git a/index.d.ts b/index.d.ts index 6c4b890..1b4ccdf 100644 --- a/index.d.ts +++ b/index.d.ts @@ -44,7 +44,7 @@ declare class Router { * @property {Object} params Object containing all parameters defined in the url string * @property {Object} query Object containing all query parameters * @property {Object} headers Object containing request headers - * @property {Object|string} body Only available if method is `POST`, `PUT` or `PATCH`. Contains either the received body string or a parsed object if valid JSON was sent. + * @property {Object|string} body Only available if method is `POST`, `PUT`, `PATCH` or `DELETE`. Contains either the received body string or a parsed object if valid JSON was sent. * @property {Object} cf object containing custom Cloudflare properties. (https://developers.cloudflare.com/workers/examples/accessing-the-cloudflare-object) */ /** diff --git a/package.json b/package.json index 1fc17d7..e3e1b35 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@tsndr/cloudflare-worker-router", - "version": "1.1.7", + "version": "1.1.8", "description": "", "main": "index.js", "scripts": {}, From fef1f00a371a5abc5ec52edbf2f8f0db86066cfb Mon Sep 17 00:00:00 2001 From: Tobias Schneider Date: Sun, 16 Jan 2022 17:37:14 +0100 Subject: [PATCH 4/4] Cleanup --- index.d.ts | 6 +++--- index.js | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/index.d.ts b/index.d.ts index 1b4ccdf..4f3432d 100644 --- a/index.d.ts +++ b/index.d.ts @@ -44,7 +44,7 @@ declare class Router { * @property {Object} params Object containing all parameters defined in the url string * @property {Object} query Object containing all query parameters * @property {Object} headers Object containing request headers - * @property {Object|string} body Only available if method is `POST`, `PUT`, `PATCH` or `DELETE`. Contains either the received body string or a parsed object if valid JSON was sent. + * @property {Object | string} body Only available if method is `POST`, `PUT`, `PATCH` or `DELETE`. Contains either the received body string or a parsed object if valid JSON was sent. * @property {Object} cf object containing custom Cloudflare properties. (https://developers.cloudflare.com/workers/examples/accessing-the-cloudflare-object) */ /** @@ -53,7 +53,7 @@ declare class Router { * @typedef RouterResponse * @property {Object} headers Object you can set response headers in * @property {number} status Return status code (default: `204`) - * @property {Object|string} body Either an `object` (will be converted to JSON) or a string + * @property {Object | string} body Either an `object` (will be converted to JSON) or a string */ /** * Next Function @@ -196,7 +196,7 @@ declare class Router { * * @private * @param {Request} request - * @returns {Route|undefined} + * @returns {Route | undefined} */ private getRoute(request: Request): Route | undefined /** diff --git a/index.js b/index.js index bcd388a..2280a98 100644 --- a/index.js +++ b/index.js @@ -50,7 +50,7 @@ class Router { * @property {Object} params Object containing all parameters defined in the url string * @property {Object} query Object containing all query parameters * @property {Object} headers Object containing request headers - * @property {Object|string} body Only available if method is `POST`, `PUT`, `PATCH` or `DELETE`. Contains either the received body string or a parsed object if valid JSON was sent. + * @property {Object | string} body Only available if method is `POST`, `PUT`, `PATCH` or `DELETE`. Contains either the received body string or a parsed object if valid JSON was sent. * @property {Object} cf object containing custom Cloudflare properties. (https://developers.cloudflare.com/workers/examples/accessing-the-cloudflare-object) */ @@ -60,7 +60,7 @@ class Router { * @typedef RouterResponse * @property {Object} headers Object you can set response headers in * @property {number} status Return status code (default: `204`) - * @property {Object|string} body Either an `object` (will be converted to JSON) or a string + * @property {Object | string} body Either an `object` (will be converted to JSON) or a string */ /** @@ -263,7 +263,7 @@ class Router { * * @private * @param {Request} request - * @returns {Route|undefined} + * @returns {Route | undefined} */ getRoute(request) { const url = new URL(request.url)