Headers Object to Headers Object
This commit is contained in:
@@ -29,10 +29,7 @@ router.cors()
|
|||||||
|
|
||||||
// Register global middleware
|
// Register global middleware
|
||||||
router.use((req, res, next) => {
|
router.use((req, res, next) => {
|
||||||
res.headers = {
|
res.headers.set('X-Global-Middlewares', 'true')
|
||||||
...res.headers,
|
|
||||||
'X-Global-Middlewares': 'true'
|
|
||||||
}
|
|
||||||
next()
|
next()
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -147,7 +144,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`, `PATCH` or `DELETE`. 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` | `Headers` | Request [Headers Object](https://developer.mozilla.org/en-US/docs/Web/API/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
|
||||||
`query` | `object` | Object containing all query parameters
|
`query` | `object` | Object containing all query parameters
|
||||||
@@ -157,7 +154,7 @@ Key | Type | Description
|
|||||||
Key | Type | Description
|
Key | Type | Description
|
||||||
--------- | ------------------- | -----------
|
--------- | ------------------- | -----------
|
||||||
`body` | `object` / `string` | Either set an `object` (will be converted to JSON) or a string
|
`body` | `object` / `string` | Either set an `object` (will be converted to JSON) or a string
|
||||||
`headers` | `object` | Object you can set response headers in
|
`headers` | `Headers` | Response [Headers Object](https://developer.mozilla.org/en-US/docs/Web/API/Headers)
|
||||||
`status` | `integer` | Return status code (default: `204`)
|
`status` | `integer` | Return status code (default: `204`)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
6
index.d.ts
vendored
6
index.d.ts
vendored
@@ -50,7 +50,7 @@ declare class Router {
|
|||||||
* @property {string} method HTTP request method
|
* @property {string} method HTTP request method
|
||||||
* @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 {Headers} headers Request headers object
|
||||||
* @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> | 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)
|
* @property {Object<string, string | number>} cf object containing custom Cloudflare properties. (https://developers.cloudflare.com/workers/examples/accessing-the-cloudflare-object)
|
||||||
*/
|
*/
|
||||||
@@ -58,7 +58,7 @@ declare class Router {
|
|||||||
* Response Object
|
* Response Object
|
||||||
*
|
*
|
||||||
* @typedef RouterResponse
|
* @typedef RouterResponse
|
||||||
* @property {Object<string, string>} headers Object you can set response headers in
|
* @property {Headers} headers Response headers object
|
||||||
* @property {number} status Return status code (default: `204`)
|
* @property {number} status Return status code (default: `204`)
|
||||||
* @property {Object<string, string> | string} body Either an `object` (will be converted to JSON) or a string
|
* @property {Object<string, string> | string} body Either an `object` (will be converted to JSON) or a string
|
||||||
* @property {Response} raw A response object that is to be returned, this will void all other res properties and return this as is.
|
* @property {Response} raw A response object that is to be returned, this will void all other res properties and return this as is.
|
||||||
@@ -306,7 +306,7 @@ type RouterResponse = {
|
|||||||
/**
|
/**
|
||||||
* Object you can set response headers in
|
* Object you can set response headers in
|
||||||
*/
|
*/
|
||||||
headers: Object<string, string>
|
headers: Headers
|
||||||
/**
|
/**
|
||||||
* Return status code (default: `204`)
|
* Return status code (default: `204`)
|
||||||
*/
|
*/
|
||||||
|
|||||||
25
index.js
25
index.js
@@ -54,7 +54,7 @@ class Router {
|
|||||||
* @property {string} method HTTP request method
|
* @property {string} method HTTP request method
|
||||||
* @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 {Headers} headers Request headers object
|
||||||
* @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> | 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)
|
* @property {Object<string, string | number>} cf object containing custom Cloudflare properties. (https://developers.cloudflare.com/workers/examples/accessing-the-cloudflare-object)
|
||||||
*/
|
*/
|
||||||
@@ -63,7 +63,7 @@ class Router {
|
|||||||
* Response Object
|
* Response Object
|
||||||
*
|
*
|
||||||
* @typedef RouterResponse
|
* @typedef RouterResponse
|
||||||
* @property {Object<string, string>} headers Object you can set response headers in
|
* @property {Headers} headers Response headers object
|
||||||
* @property {number} status Return status code (default: `204`)
|
* @property {number} status Return status code (default: `204`)
|
||||||
* @property {Object<string, string> | string} body Either an `object` (will be converted to JSON) or a string
|
* @property {Object<string, string> | string} body Either an `object` (will be converted to JSON) or a string
|
||||||
* @property {Response} raw A response object that is to be returned, this will void all other res properties and return this as is.
|
* @property {Response} raw A response object that is to be returned, this will void all other res properties and return this as is.
|
||||||
@@ -80,8 +80,8 @@ class Router {
|
|||||||
* Handler Function
|
* Handler Function
|
||||||
*
|
*
|
||||||
* @callback RouterHandler
|
* @callback RouterHandler
|
||||||
* @param {Request} request
|
* @param {RouterRequest} request
|
||||||
* @param {Response} response
|
* @param {RouterResponse} response
|
||||||
* @param {RouterNext} next
|
* @param {RouterNext} next
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@@ -359,15 +359,12 @@ class Router {
|
|||||||
status: 404
|
status: 404
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
const res = { headers: {} }
|
const res = { headers: new Headers() }
|
||||||
if (Object.keys(this.corsConfig).length) {
|
if (Object.keys(this.corsConfig).length) {
|
||||||
res.headers = {
|
res.headers.set('Access-Control-Allow-Origin', this.corsConfig.allowOrigin)
|
||||||
...res.headers,
|
res.headers.set('Access-Control-Allow-Methods', this.corsConfig.allowMethods)
|
||||||
'Access-Control-Allow-Origin': this.corsConfig.allowOrigin,
|
res.headers.set('Access-Control-Allow-Headers', this.corsConfig.allowHeaders)
|
||||||
'Access-Control-Allow-Methods': this.corsConfig.allowMethods,
|
res.headers.set('Access-Control-Max-Age', this.corsConfig.maxAge)
|
||||||
'Access-Control-Allow-Headers': this.corsConfig.allowHeaders,
|
|
||||||
'Access-Control-Max-Age': this.corsConfig.maxAge,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
const handlers = [...this.globalHandlers, ...route.handlers]
|
const handlers = [...this.globalHandlers, ...route.handlers]
|
||||||
let prevIndex = -1
|
let prevIndex = -1
|
||||||
@@ -380,8 +377,8 @@ class Router {
|
|||||||
}
|
}
|
||||||
await runner(0)
|
await runner(0)
|
||||||
if (typeof res.body === 'object') {
|
if (typeof res.body === 'object') {
|
||||||
if (!res.headers['Content-Type'])
|
if (!res.headers.has('Content-Type'))
|
||||||
res.headers['Content-Type'] = 'application/json'
|
res.headers.set('Content-Type', 'application/json')
|
||||||
res.body = JSON.stringify(res.body)
|
res.body = JSON.stringify(res.body)
|
||||||
}
|
}
|
||||||
if (res.raw) {
|
if (res.raw) {
|
||||||
|
|||||||
Reference in New Issue
Block a user