1
0

Merge branch 'main' into patch-1

This commit is contained in:
Toby Schneider
2022-01-18 10:53:59 +01:00
committed by GitHub
4 changed files with 12 additions and 10 deletions

View File

@@ -129,7 +129,7 @@ An unlimited number of functions getting [`req`](#req-object) and [`res`](#res-o
Key | Type | Description 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 `headers` | `object` | Object containing request headers
`method` | `string` | HTTP request method `method` | `string` | HTTP request method
`params` | `object` | Object containing all parameters defined in the url string `params` | `object` | Object containing all parameters defined in the url string

3
index.d.ts vendored
View File

@@ -44,7 +44,8 @@ declare class Router {
* @property {Object<string, string>} params Object containing all parameters defined in the url string * @property {Object<string, string>} params Object containing all parameters defined in the url string
* @property {Object<string, string>} query Object containing all query parameters * @property {Object<string, string>} query Object containing all query parameters
* @property {Object<string, string>} headers Object containing request headers * @property {Object<string, string>} headers Object containing request headers
* @property {Object<string, string>|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, string> | 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, string | number>} cf object containing custom Cloudflare properties. (https://developers.cloudflare.com/workers/examples/accessing-the-cloudflare-object)
*/ */
/** /**
* Response Object * Response Object

View File

@@ -50,7 +50,8 @@ class Router {
* @property {Object<string, string>} params Object containing all parameters defined in the url string * @property {Object<string, string>} params Object containing all parameters defined in the url string
* @property {Object<string, string>} query Object containing all query parameters * @property {Object<string, string>} query Object containing all query parameters
* @property {Object<string, string>} headers Object containing request headers * @property {Object<string, string>} headers Object containing request headers
* @property {Object<string, string>|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, string> | 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, string | number>} 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 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 } const req = { headers: request.headers, method: request.method, url: request.url, cf: request.cf || {} }
req.params = [] req.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, {
@@ -313,7 +314,7 @@ class Router {
status: this.corsConfig.optionsSuccessStatus 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')) { if (req.headers.has('Content-Type') && req.headers.get('Content-Type').includes('json')) {
try { try {
req.body = await request.json() req.body = await request.json()

View File

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