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 c7f1299..049180b 100644 --- a/index.d.ts +++ b/index.d.ts @@ -44,7 +44,8 @@ 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) */ /** * Response Object @@ -52,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 * @property {Response} response A response object that is to be returned, this will void all other res properties and return this as is. */ /** @@ -196,7 +197,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 225022d..7f9d761 100644 --- a/index.js +++ b/index.js @@ -50,7 +50,8 @@ 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) */ /** @@ -59,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 */ /** @@ -262,7 +263,7 @@ class Router { * * @private * @param {Request} request - * @returns {Route|undefined} + * @returns {Route | undefined} */ getRoute(request) { const url = new URL(request.url) @@ -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, { @@ -313,7 +314,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() 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": {},