1
0

Merge branch 'main' into main

This commit is contained in:
Toby Schneider
2022-06-01 20:39:09 +02:00
committed by GitHub
4 changed files with 89 additions and 36 deletions

View File

@@ -27,9 +27,14 @@ const router = new Router()
// Enabling buildin CORS support // Enabling buildin CORS support
router.cors() router.cors()
// Register global middleware
router.use((req, res, next) => {
res.headers.set('X-Global-Middlewares', 'true')
next()
})
// Simple get // Simple get
router.get('/user', (req, res) => { router.get('/user', (req, res) => {
res.body = { res.body = {
data: { data: {
id: 1, id: 1,
@@ -89,6 +94,15 @@ Enable or disable debug mode. Which will return the `error.stack` in case of an
State is a `boolean` which determines if debug mode should be enabled or not (default: `true`) State is a `boolean` which determines if debug mode should be enabled or not (default: `true`)
### `router.use(handler)`
Register a global middleware handler.
#### `handler` (function)
Handler is a `function` which will be called for every request.
### `router.cors([config])` ### `router.cors([config])`
If enabled will overwrite other `OPTIONS` requests. If enabled will overwrite other `OPTIONS` requests.
@@ -130,18 +144,17 @@ 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
### `res`-Object ### `res`-Object
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`)
`webSocket` | `WebSocket` | Upgraded websocket connection `webSocket` | `WebSocket` | Upgraded websocket connection

39
index.d.ts vendored
View File

@@ -14,13 +14,20 @@ declare class Router {
* @type {Route[]} * @type {Route[]}
*/ */
protected routes: Route[] protected routes: Route[]
/**
* Global Handlers
*
* @protected
* @type {Handler[]}
*/
protected globalHandlers: Handler[]
/** /**
* Debug Mode * Debug Mode
* *
* @protected * @protected
* @type {boolean} * @type {boolean}
*/ */
protected debugMode: boolean protected debugMode: boolean = false
/** /**
* CORS Config * CORS Config
* *
@@ -43,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)
*/ */
@@ -51,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.
@@ -70,6 +77,13 @@ declare class Router {
* @param {Response} response * @param {Response} response
* @param {next} next * @param {next} next
*/ */
/**
* Register global handler
*
* @param {RouterHandler} handler
* @param handlers
*/
use(handler: RouterHandler): Router
/** /**
* Register CONNECT route * Register CONNECT route
* *
@@ -172,9 +186,9 @@ declare class Router {
/** /**
* Debug Mode * Debug Mode
* *
* @param {boolean} state Whether to turn on or off debug mode (default: true) * @param {boolean} [state=true] Whether to turn on or off debug mode (default: true)
*/ */
debug(state: boolean): void debug(state?: boolean): void
/** /**
* Enable CORS support * Enable CORS support
* *
@@ -204,9 +218,10 @@ declare class Router {
* Handle requests * Handle requests
* *
* @param {Request} request * @param {Request} request
* @param {any=} extend
* @returns {Response} * @returns {Response}
*/ */
handle(request: Request): Response handle(request: Request, extend?: any): Response
} }
declare namespace Router { declare namespace Router {
export { Route, RouterRequest, RouterResponse, RouterNext, RouterHandler, RouterCorsConfig } export { Route, RouterRequest, RouterResponse, RouterNext, RouterHandler, RouterCorsConfig }
@@ -265,16 +280,20 @@ type RouterRequest = {
* HTTP request method * HTTP request method
*/ */
method: string method: string
/**
* Object containing request headers
*/
headers: Headers
/**
* URL String
*/
url: string
/** /**
* 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

@@ -16,6 +16,11 @@ class Router {
*/ */
this.routes = [] this.routes = []
/**
* Global Handlers
*/
this.globalHandlers = []
/** /**
* Debug Mode * Debug Mode
* *
@@ -49,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)
*/ */
@@ -58,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.
@@ -75,11 +80,21 @@ 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
*/ */
/**
* Register global handler
*
* @param {RouterHandler} handler
*/
use(handlers) {
this.globalHandlers.push(handlers)
return this
}
/** /**
* Register CONNECT route * Register CONNECT route
* *
@@ -206,7 +221,7 @@ class Router {
/** /**
* Debug Mode * Debug Mode
* *
* @param {boolean} state Whether to turn on or off debug mode (default: true) * @param {boolean} [state=true] Whether to turn on or off debug mode (default: true)
*/ */
debug(state = true) { debug(state = true) {
this.debugMode = state this.debugMode = state
@@ -294,16 +309,24 @@ class Router {
* Handle requests * Handle requests
* *
* @param {Request} request * @param {Request} request
* @param {any=} extend
* @returns {Response} * @returns {Response}
*/ */
async handle(request) { async handle(request, extend = {}) {
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 = [] ...extend,
method: request.method,
headers: request.headers,
url: request.url,
params: [],
query: {},
body: ''
}
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: {
@@ -315,7 +338,7 @@ class Router {
status: this.corsConfig.optionsSuccessStatus status: this.corsConfig.optionsSuccessStatus
}) })
} }
if (['POST', 'PUT', 'PATCH', 'DELETE'].includes(req.method)) { if (['POST', 'PUT', 'PATCH'].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()
@@ -336,28 +359,26 @@ 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]
let prevIndex = -1 let prevIndex = -1
const runner = async index => { const runner = async index => {
if (index === prevIndex) if (index === prevIndex)
throw new Error('next() called multiple times') throw new Error('next() called multiple times')
prevIndex = index prevIndex = index
if (typeof route.handlers[index] === 'function') if (typeof handlers[index] === 'function')
await route.handlers[index](req, res, async () => await runner(index + 1)) await handlers[index](req, res, async () => await runner(index + 1))
} }
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) {

View File

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