rewrite in ts
This commit is contained in:
10
.github/workflows/publish-pre.yml
vendored
10
.github/workflows/publish-pre.yml
vendored
@@ -8,11 +8,12 @@ jobs:
|
|||||||
publish-npm:
|
publish-npm:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v2
|
- uses: actions/checkout@v3
|
||||||
- uses: actions/setup-node@v1
|
- uses: actions/setup-node@v3
|
||||||
with:
|
with:
|
||||||
node-version: 12
|
node-version: 16
|
||||||
registry-url: https://registry.npmjs.org/
|
registry-url: https://registry.npmjs.org/
|
||||||
|
- run: npm run build
|
||||||
- run: npm publish --tag pre --access public
|
- run: npm publish --tag pre --access public
|
||||||
env:
|
env:
|
||||||
NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}
|
NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}
|
||||||
@@ -23,8 +24,9 @@ jobs:
|
|||||||
- uses: actions/checkout@v2
|
- uses: actions/checkout@v2
|
||||||
- uses: actions/setup-node@v1
|
- uses: actions/setup-node@v1
|
||||||
with:
|
with:
|
||||||
node-version: 12
|
node-version: 16
|
||||||
registry-url: https://npm.pkg.github.com/
|
registry-url: https://npm.pkg.github.com/
|
||||||
|
- run: npm run build
|
||||||
- run: npm publish --tag pre --access public
|
- run: npm publish --tag pre --access public
|
||||||
env:
|
env:
|
||||||
NODE_AUTH_TOKEN: ${{secrets.GITHUB_TOKEN}}
|
NODE_AUTH_TOKEN: ${{secrets.GITHUB_TOKEN}}
|
||||||
5
.npmignore
Normal file
5
.npmignore
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
.github/
|
||||||
|
src/
|
||||||
|
test/
|
||||||
|
MIGRATION.md
|
||||||
|
tsconfig.json
|
||||||
@@ -12,7 +12,7 @@ See [Migration Guide](https://github.com/tsndr/cloudflare-worker-router/blob/v2-
|
|||||||
---
|
---
|
||||||
|
|
||||||
|
|
||||||
Cloudflare Workers Router is a super lightweight router (3.6 kB) with middleware support and ZERO dependencies for Cloudflare Workers.
|
Cloudflare Workers Router is a super lightweight router (2.30 KiB) with middleware support and **ZERO dependencies** for [Cloudflare Workers](https://workers.cloudflare.com/).
|
||||||
|
|
||||||
When I was trying out Cloudflare Workers I almost immediately noticed how fast it was compared to other serverless offerings. So I wanted to build a full-fledged API to see how it performs doing real work, but since I wasn't able to find a router that suited my needs I created my own.
|
When I was trying out Cloudflare Workers I almost immediately noticed how fast it was compared to other serverless offerings. So I wanted to build a full-fledged API to see how it performs doing real work, but since I wasn't able to find a router that suited my needs I created my own.
|
||||||
|
|
||||||
@@ -91,7 +91,7 @@ router.delete('/user/:id', ({ req, res, next }) => {
|
|||||||
|
|
||||||
// Listen Cloudflare Workers Fetch Event
|
// Listen Cloudflare Workers Fetch Event
|
||||||
export default {
|
export default {
|
||||||
async fetch(request, env, ctx) {
|
async fetch(request, env) {
|
||||||
return router.handle(env, request)
|
return router.handle(env, request)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
372
index.d.ts
vendored
372
index.d.ts
vendored
@@ -1,372 +0,0 @@
|
|||||||
export = Router
|
|
||||||
/**
|
|
||||||
* Router
|
|
||||||
*
|
|
||||||
* @class
|
|
||||||
* @constructor
|
|
||||||
* @public
|
|
||||||
*/
|
|
||||||
declare class Router {
|
|
||||||
/**
|
|
||||||
* Router Array
|
|
||||||
*
|
|
||||||
* @protected
|
|
||||||
* @type {Route[]}
|
|
||||||
*/
|
|
||||||
protected routes: Route[]
|
|
||||||
/**
|
|
||||||
* Global Handlers
|
|
||||||
*
|
|
||||||
* @protected
|
|
||||||
* @type {RouterHandler[]}
|
|
||||||
*/
|
|
||||||
protected globalHandlers: RouterHandler[]
|
|
||||||
/**
|
|
||||||
* Debug Mode
|
|
||||||
*
|
|
||||||
* @protected
|
|
||||||
* @type {boolean=false}
|
|
||||||
*/
|
|
||||||
protected debugMode: boolean = false
|
|
||||||
/**
|
|
||||||
* CORS Config
|
|
||||||
*
|
|
||||||
* @protected
|
|
||||||
* @type {RouterCorsConfig}
|
|
||||||
*/
|
|
||||||
protected corsConfig: RouterCorsConfig
|
|
||||||
/**
|
|
||||||
* Route Object
|
|
||||||
*
|
|
||||||
* @typedef Route
|
|
||||||
* @property {string} method HTTP request method
|
|
||||||
* @property {string} url URL String
|
|
||||||
* @property {RouterHandler[]} handlers Array of handler functions
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Router Context
|
|
||||||
*
|
|
||||||
* @typedef RouterContext
|
|
||||||
* @property {Object<string, string>} env Environment
|
|
||||||
* @property {RouterRequest} req Request Object
|
|
||||||
* @property {RouterResponse} res Response Object
|
|
||||||
* @property {RouterNext} next Next Handler
|
|
||||||
*/
|
|
||||||
/**
|
|
||||||
* Request Object
|
|
||||||
*
|
|
||||||
* @typedef RouterRequest
|
|
||||||
* @property {string} method HTTP request method
|
|
||||||
* @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 {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 | number>} cf object containing custom Cloudflare properties. (https://developers.cloudflare.com/workers/examples/accessing-the-cloudflare-object)
|
|
||||||
*/
|
|
||||||
/**
|
|
||||||
* Response Object
|
|
||||||
*
|
|
||||||
* @typedef RouterResponse
|
|
||||||
* @property {Headers} headers Response headers object
|
|
||||||
* @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 {Response} raw A response object that is to be returned, this will void all other res properties and return this as is.
|
|
||||||
*/
|
|
||||||
/**
|
|
||||||
* Next Function
|
|
||||||
*
|
|
||||||
* @callback RouterNext
|
|
||||||
* @returns {Promise}
|
|
||||||
*/
|
|
||||||
/**
|
|
||||||
* Handler Function
|
|
||||||
*
|
|
||||||
* @callback RouterHandler
|
|
||||||
* @param {RouterContext} ctx
|
|
||||||
*/
|
|
||||||
/**
|
|
||||||
* Register global handler
|
|
||||||
*
|
|
||||||
* @param {RouterHandler} handler
|
|
||||||
* @param handlers
|
|
||||||
*/
|
|
||||||
use(handler: RouterHandler): Router
|
|
||||||
/**
|
|
||||||
* Register CONNECT route
|
|
||||||
*
|
|
||||||
* @param {string} url
|
|
||||||
* @param {...RouterHandler} handlers
|
|
||||||
* @returns {Router}
|
|
||||||
*/
|
|
||||||
connect(url: string, ...handlers: RouterHandler[]): Router
|
|
||||||
/**
|
|
||||||
* Register DELETE route
|
|
||||||
*
|
|
||||||
* @param {string} url
|
|
||||||
* @param {...RouterHandler} handlers
|
|
||||||
* @returns {Router}
|
|
||||||
*/
|
|
||||||
delete(url: string, ...handlers: RouterHandler[]): Router
|
|
||||||
/**
|
|
||||||
* Register GET route
|
|
||||||
*
|
|
||||||
* @param {string} url
|
|
||||||
* @param {...RouterHandler} handlers
|
|
||||||
* @returns {Router}
|
|
||||||
*/
|
|
||||||
get(url: string, ...handlers: RouterHandler[]): Router
|
|
||||||
/**
|
|
||||||
* Register HEAD route
|
|
||||||
*
|
|
||||||
* @param {string} url
|
|
||||||
* @param {...RouterHandler} handlers
|
|
||||||
* @returns {Router}
|
|
||||||
*/
|
|
||||||
head(url: string, ...handlers: RouterHandler[]): Router
|
|
||||||
/**
|
|
||||||
* Register OPTIONS route
|
|
||||||
*
|
|
||||||
* @param {string} url
|
|
||||||
* @param {...RouterHandler} handlers
|
|
||||||
* @returns {Router}
|
|
||||||
*/
|
|
||||||
options(url: string, ...handlers: RouterHandler[]): Router
|
|
||||||
/**
|
|
||||||
* Register PATCH route
|
|
||||||
*
|
|
||||||
* @param {string} url
|
|
||||||
* @param {...RouterHandler} handlers
|
|
||||||
* @returns {Router}
|
|
||||||
*/
|
|
||||||
patch(url: string, ...handlers: RouterHandler[]): Router
|
|
||||||
/**
|
|
||||||
* Register POST route
|
|
||||||
*
|
|
||||||
* @param {string} url
|
|
||||||
* @param {...RouterHandler} handlers
|
|
||||||
* @returns {Router}
|
|
||||||
*/
|
|
||||||
post(url: string, ...handlers: RouterHandler[]): Router
|
|
||||||
/**
|
|
||||||
* Register PUT route
|
|
||||||
*
|
|
||||||
* @param {string} url
|
|
||||||
* @param {...RouterHandler} handlers
|
|
||||||
* @returns {Router}
|
|
||||||
*/
|
|
||||||
put(url: string, ...handlers: RouterHandler[]): Router
|
|
||||||
/**
|
|
||||||
* Register TRACE route
|
|
||||||
*
|
|
||||||
* @param {string} url
|
|
||||||
* @param {...RouterHandler} handlers
|
|
||||||
* @returns {Router}
|
|
||||||
*/
|
|
||||||
trace(url: string, ...handlers: RouterHandler[]): Router
|
|
||||||
/**
|
|
||||||
* Register route, ignoring method
|
|
||||||
*
|
|
||||||
* @param {string} url
|
|
||||||
* @param {...RouterHandler} handlers
|
|
||||||
* @returns {Router}
|
|
||||||
*/
|
|
||||||
any(url: string, ...handlers: RouterHandler[]): Router
|
|
||||||
/**
|
|
||||||
* Register route, ignoring method
|
|
||||||
*
|
|
||||||
* @deprecated since version 1.0.2, use .any(url, ...handlers) instead.
|
|
||||||
* @param {string} url
|
|
||||||
* @param {...RouterHandler} handlers
|
|
||||||
* @returns {Router}
|
|
||||||
*/
|
|
||||||
all(url: string, ...handlers: RouterHandler[]): Router
|
|
||||||
/**
|
|
||||||
* CORS Config
|
|
||||||
*
|
|
||||||
* @typedef RouterCorsConfig
|
|
||||||
* @property {string} allowOrigin Access-Control-Allow-Origin (default: `*`)
|
|
||||||
* @property {string} allowMethods Access-Control-Allow-Methods (default: `*`)
|
|
||||||
* @property {string} allowHeaders Access-Control-Allow-Headers (default: `*`)
|
|
||||||
* @property {number} maxAge Access-Control-Max-Age (default: `86400`)
|
|
||||||
* @property {number} optionsSuccessStatus Return status code for OPTIONS request (default: `204`)
|
|
||||||
*/
|
|
||||||
/**
|
|
||||||
* Debug Mode
|
|
||||||
*
|
|
||||||
* @param {boolean} [state=true] Whether to turn on or off debug mode (default: true)
|
|
||||||
*/
|
|
||||||
debug(state?: boolean): void
|
|
||||||
/**
|
|
||||||
* Enable CORS support
|
|
||||||
*
|
|
||||||
* @param {RouterCorsConfig} config
|
|
||||||
* @returns {Router}
|
|
||||||
*/
|
|
||||||
cors(config?: RouterCorsConfig): Router
|
|
||||||
/**
|
|
||||||
* Register route
|
|
||||||
*
|
|
||||||
* @private
|
|
||||||
* @param {string} method HTTP request method
|
|
||||||
* @param {string} url URL String
|
|
||||||
* @param {RouterHandler[]} handlers Arrar of handler functions
|
|
||||||
* @returns {Router}
|
|
||||||
*/
|
|
||||||
private register(method: string, url: string, handlers: RouterHandler[]): Router
|
|
||||||
/**
|
|
||||||
* Get Route by request
|
|
||||||
*
|
|
||||||
* @private
|
|
||||||
* @param {Request} request
|
|
||||||
* @returns {Route | undefined}
|
|
||||||
*/
|
|
||||||
private getRoute(request: Request): Route | undefined
|
|
||||||
/**
|
|
||||||
* Handle requests
|
|
||||||
*
|
|
||||||
* @param {any} env
|
|
||||||
* @param {Request} request
|
|
||||||
* @param {any=} extend
|
|
||||||
* @returns {Response}
|
|
||||||
*/
|
|
||||||
handle(env: any, request: Request, extend?: any): Response
|
|
||||||
}
|
|
||||||
declare namespace Router {
|
|
||||||
export { Route, RouterCorsConfig, RouterHandler, RouterContext, RouterRequest, RouterResponse, RouterNext }
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
* Route Object
|
|
||||||
*/
|
|
||||||
type Route = {
|
|
||||||
/**
|
|
||||||
* HTTP request method
|
|
||||||
*/
|
|
||||||
method: string
|
|
||||||
/**
|
|
||||||
* URL String
|
|
||||||
*/
|
|
||||||
url: string
|
|
||||||
/**
|
|
||||||
* Array of handler functions
|
|
||||||
*/
|
|
||||||
handlers: RouterHandler[]
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
* CORS Config
|
|
||||||
*/
|
|
||||||
type RouterCorsConfig = {
|
|
||||||
/**
|
|
||||||
* Access-Control-Allow-Origin (default: `*`)
|
|
||||||
*/
|
|
||||||
allowOrigin: string
|
|
||||||
/**
|
|
||||||
* Access-Control-Allow-Methods (default: `*`)
|
|
||||||
*/
|
|
||||||
allowMethods: string
|
|
||||||
/**
|
|
||||||
* Access-Control-Allow-Headers (default: `*`)
|
|
||||||
*/
|
|
||||||
allowHeaders: string
|
|
||||||
/**
|
|
||||||
* Access-Control-Max-Age (default: `86400`)
|
|
||||||
*/
|
|
||||||
maxAge: number
|
|
||||||
/**
|
|
||||||
* Return status code for OPTIONS request (default: `204`)
|
|
||||||
*/
|
|
||||||
optionsSuccessStatus: number
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
* Handler Function
|
|
||||||
*/
|
|
||||||
type RouterHandler = (ctx: RouterContext) => any
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Router Context
|
|
||||||
*/
|
|
||||||
type RouterContext = {
|
|
||||||
/**
|
|
||||||
* Environment
|
|
||||||
*/
|
|
||||||
env: Object<string, string>
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Request Object
|
|
||||||
*/
|
|
||||||
req: RouterRequest
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Response Object
|
|
||||||
*/
|
|
||||||
res: RouterResponse
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Next Handler
|
|
||||||
*/
|
|
||||||
next: RouterNext
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Request Object
|
|
||||||
*/
|
|
||||||
type RouterRequest = {
|
|
||||||
/**
|
|
||||||
* HTTP request method
|
|
||||||
*/
|
|
||||||
method: string
|
|
||||||
/**
|
|
||||||
* Object containing request headers
|
|
||||||
*/
|
|
||||||
headers: Headers
|
|
||||||
/**
|
|
||||||
* URL String
|
|
||||||
*/
|
|
||||||
url: string
|
|
||||||
/**
|
|
||||||
* Object containing all parameters defined in the url string
|
|
||||||
*/
|
|
||||||
params: {
|
|
||||||
[key: string]: 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: any
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Make it user extendable
|
|
||||||
*/
|
|
||||||
[key: string]: any
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
* Response Object
|
|
||||||
*/
|
|
||||||
type RouterResponse = {
|
|
||||||
/**
|
|
||||||
* Object you can set response headers in
|
|
||||||
*/
|
|
||||||
headers: Headers
|
|
||||||
/**
|
|
||||||
* Return status code (default: `204`)
|
|
||||||
*/
|
|
||||||
status: number
|
|
||||||
/**
|
|
||||||
* A response object to be directly returned to the client
|
|
||||||
*/
|
|
||||||
response: Response
|
|
||||||
/**
|
|
||||||
* Either an `object` (will be converted to JSON) or a string
|
|
||||||
*/
|
|
||||||
body: {
|
|
||||||
[key: string]: any
|
|
||||||
} | string
|
|
||||||
/**
|
|
||||||
* Upgraded websocket connection
|
|
||||||
*/
|
|
||||||
webSocket?: WebSocket
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
* Next Function
|
|
||||||
*/
|
|
||||||
type RouterNext = () => Promise<void>
|
|
||||||
50
package-lock.json
generated
Normal file
50
package-lock.json
generated
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
{
|
||||||
|
"name": "@tsndr/cloudflare-worker-router",
|
||||||
|
"version": "2.0.0-pre.7",
|
||||||
|
"lockfileVersion": 2,
|
||||||
|
"requires": true,
|
||||||
|
"packages": {
|
||||||
|
"": {
|
||||||
|
"name": "@tsndr/cloudflare-worker-router",
|
||||||
|
"version": "2.0.0-pre.7",
|
||||||
|
"license": "MIT",
|
||||||
|
"devDependencies": {
|
||||||
|
"@cloudflare/workers-types": "^3.13.0",
|
||||||
|
"typescript": "^4.7.4"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/@cloudflare/workers-types": {
|
||||||
|
"version": "3.13.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/@cloudflare/workers-types/-/workers-types-3.13.0.tgz",
|
||||||
|
"integrity": "sha512-oyhzfYlWBLgd9odJ/WHcsD/8B+IaAjSD+OcPEGLzX5kGRONjwcW3NY0WQfsVIhQzZ6AbPzjwkmj4D2VFwU1xRQ==",
|
||||||
|
"dev": true
|
||||||
|
},
|
||||||
|
"node_modules/typescript": {
|
||||||
|
"version": "4.7.4",
|
||||||
|
"resolved": "https://registry.npmjs.org/typescript/-/typescript-4.7.4.tgz",
|
||||||
|
"integrity": "sha512-C0WQT0gezHuw6AdY1M2jxUO83Rjf0HP7Sk1DtXj6j1EwkQNZrHAg2XPWlq62oqEhYvONq5pkC2Y9oPljWToLmQ==",
|
||||||
|
"dev": true,
|
||||||
|
"bin": {
|
||||||
|
"tsc": "bin/tsc",
|
||||||
|
"tsserver": "bin/tsserver"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": ">=4.2.0"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"dependencies": {
|
||||||
|
"@cloudflare/workers-types": {
|
||||||
|
"version": "3.13.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/@cloudflare/workers-types/-/workers-types-3.13.0.tgz",
|
||||||
|
"integrity": "sha512-oyhzfYlWBLgd9odJ/WHcsD/8B+IaAjSD+OcPEGLzX5kGRONjwcW3NY0WQfsVIhQzZ6AbPzjwkmj4D2VFwU1xRQ==",
|
||||||
|
"dev": true
|
||||||
|
},
|
||||||
|
"typescript": {
|
||||||
|
"version": "4.7.4",
|
||||||
|
"resolved": "https://registry.npmjs.org/typescript/-/typescript-4.7.4.tgz",
|
||||||
|
"integrity": "sha512-C0WQT0gezHuw6AdY1M2jxUO83Rjf0HP7Sk1DtXj6j1EwkQNZrHAg2XPWlq62oqEhYvONq5pkC2Y9oPljWToLmQ==",
|
||||||
|
"dev": true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
14
package.json
14
package.json
@@ -2,9 +2,11 @@
|
|||||||
"name": "@tsndr/cloudflare-worker-router",
|
"name": "@tsndr/cloudflare-worker-router",
|
||||||
"version": "2.0.0-pre.7",
|
"version": "2.0.0-pre.7",
|
||||||
"description": "",
|
"description": "",
|
||||||
"main": "index.js",
|
"main": "dist/index.js",
|
||||||
"types": "index.d.ts",
|
"types": "dist/index.d.ts",
|
||||||
"scripts": {},
|
"scripts": {
|
||||||
|
"build": "rm -rf dist/* && tsc"
|
||||||
|
},
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
"url": "git+https://github.com/tsndr/cloudflare-worker-router.git"
|
"url": "git+https://github.com/tsndr/cloudflare-worker-router.git"
|
||||||
@@ -27,5 +29,9 @@
|
|||||||
"bugs": {
|
"bugs": {
|
||||||
"url": "https://github.com/tsndr/cloudflare-worker-router/issues"
|
"url": "https://github.com/tsndr/cloudflare-worker-router/issues"
|
||||||
},
|
},
|
||||||
"homepage": "https://github.com/tsndr/cloudflare-worker-router#readme"
|
"homepage": "https://github.com/tsndr/cloudflare-worker-router#readme",
|
||||||
|
"devDependencies": {
|
||||||
|
"@cloudflare/workers-types": "^3.13.0",
|
||||||
|
"typescript": "^4.7.4"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,3 +1,120 @@
|
|||||||
|
/**
|
||||||
|
* Route Object
|
||||||
|
*
|
||||||
|
* @typedef Route
|
||||||
|
* @property {string} method HTTP request method
|
||||||
|
* @property {string} url URL String
|
||||||
|
* @property {RouterHandler[]} handlers Array of handler functions
|
||||||
|
*/
|
||||||
|
interface Route {
|
||||||
|
method: string
|
||||||
|
url: string
|
||||||
|
handlers: RouterHandler[]
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Router Context
|
||||||
|
*
|
||||||
|
* @typedef RouterContext
|
||||||
|
* @property {Object<string, string>} env Environment
|
||||||
|
* @property {RouterRequest} req Request Object
|
||||||
|
* @property {RouterResponse} res Response Object
|
||||||
|
* @property {RouterNext} next Next Handler
|
||||||
|
*/
|
||||||
|
interface RouterContext {
|
||||||
|
env: any
|
||||||
|
req: RouterRequest
|
||||||
|
res: RouterResponse
|
||||||
|
next: RouterNext
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Request Object
|
||||||
|
*
|
||||||
|
* @typedef RouterRequest
|
||||||
|
* @property {string} url URL
|
||||||
|
* @property {string} method HTTP request method
|
||||||
|
* @property {RouterRequestParams} params Object containing all parameters defined in the url string
|
||||||
|
* @property {RouterRequestQuery} query Object containing all query parameters
|
||||||
|
* @property {Headers} headers Request headers object
|
||||||
|
* @property {any} 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 {IncomingRequestCfProperties=} cf object containing custom Cloudflare properties. (https://developers.cloudflare.com/workers/examples/accessing-the-cloudflare-object)
|
||||||
|
*/
|
||||||
|
interface RouterRequest {
|
||||||
|
url: string
|
||||||
|
method: string
|
||||||
|
params: RouterRequestParams
|
||||||
|
query: RouterRequestQuery
|
||||||
|
headers: Headers
|
||||||
|
body: any
|
||||||
|
cf?: IncomingRequestCfProperties
|
||||||
|
}
|
||||||
|
|
||||||
|
interface RouterRequestParams {
|
||||||
|
[key: string]: string
|
||||||
|
}
|
||||||
|
|
||||||
|
interface RouterRequestQuery {
|
||||||
|
[key: string]: string
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Response Object
|
||||||
|
*
|
||||||
|
* @typedef RouterResponse
|
||||||
|
* @property {Headers} headers Response headers object
|
||||||
|
* @property {number=204} status Return status code (default: `204`)
|
||||||
|
* @property {any=} 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.
|
||||||
|
*/
|
||||||
|
interface RouterResponse {
|
||||||
|
headers: Headers
|
||||||
|
status?: number
|
||||||
|
body?: any
|
||||||
|
raw?: Response,
|
||||||
|
webSocket?: WebSocket
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Next Function
|
||||||
|
*
|
||||||
|
* @callback RouterNext
|
||||||
|
* @returns {Promise}
|
||||||
|
*/
|
||||||
|
interface RouterNext {
|
||||||
|
(): Promise<void>
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handler Function
|
||||||
|
*
|
||||||
|
* @callback RouterHandler
|
||||||
|
* @param {RouterContext} ctx
|
||||||
|
* @returns {Promise<void> | void}
|
||||||
|
*/
|
||||||
|
interface RouterHandler {
|
||||||
|
(ctx: RouterContext): Promise<void> | void
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* CORS Config
|
||||||
|
*
|
||||||
|
* @typedef RouterCorsConfig
|
||||||
|
* @property {string} allowOrigin Access-Control-Allow-Origin (default: `*`)
|
||||||
|
* @property {string} allowMethods Access-Control-Allow-Methods (default: `*`)
|
||||||
|
* @property {string} allowHeaders Access-Control-Allow-Headers (default: `*`)
|
||||||
|
* @property {number} maxAge Access-Control-Max-Age (default: `86400`)
|
||||||
|
* @property {number} optionsSuccessStatus Return status code for OPTIONS request (default: `204`)
|
||||||
|
*/
|
||||||
|
interface RouterCorsConfig {
|
||||||
|
allowOrigin: string
|
||||||
|
allowMethods: string
|
||||||
|
allowHeaders: string
|
||||||
|
maxAge: number
|
||||||
|
optionsSuccessStatus: number
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Router
|
* Router
|
||||||
*
|
*
|
||||||
@@ -7,99 +124,51 @@
|
|||||||
*/
|
*/
|
||||||
class Router {
|
class Router {
|
||||||
|
|
||||||
constructor() {
|
/**
|
||||||
/**
|
* Router Array
|
||||||
* Router Array
|
*
|
||||||
*
|
* @protected
|
||||||
* @protected
|
* @type {Route[]}
|
||||||
* @type {Route[]}
|
*/
|
||||||
*/
|
protected routes: Route[] = []
|
||||||
this.routes = []
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Global Handlers
|
* Global Handlers
|
||||||
*/
|
*/
|
||||||
this.globalHandlers = []
|
protected globalHandlers: RouterHandler[] = []
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Debug Mode
|
* Debug Mode
|
||||||
*
|
*
|
||||||
* @protected
|
* @protected
|
||||||
* @type {boolean}
|
* @type {boolean}
|
||||||
*/
|
*/
|
||||||
this.debugMode = false
|
protected debugMode: boolean = false
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* CORS Config
|
* CORS Config
|
||||||
*
|
*
|
||||||
* @protected
|
* @protected
|
||||||
* @type {RouterCorsConfig}
|
* @type {RouterCorsConfig}
|
||||||
*/
|
*/
|
||||||
this.corsConfig = {}
|
protected corsConfig: RouterCorsConfig = {
|
||||||
|
allowOrigin: '*',
|
||||||
|
allowMethods: '*',
|
||||||
|
allowHeaders: '*',
|
||||||
|
maxAge: 86400,
|
||||||
|
optionsSuccessStatus: 204
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Route Object
|
* Register global handlers
|
||||||
*
|
*
|
||||||
* @typedef Route
|
* @param {RouterHandler[]} handlers
|
||||||
* @property {string} method HTTP request method
|
* @returns {Router}
|
||||||
* @property {string} url URL String
|
|
||||||
* @property {RouterHandler[]} handlers Array of handler functions
|
|
||||||
*/
|
*/
|
||||||
|
public use(...handlers: RouterHandler[]): Router {
|
||||||
/**
|
for (let handler of handlers) {
|
||||||
* Router Context
|
this.globalHandlers.push(handler)
|
||||||
*
|
}
|
||||||
* @typedef RouterContext
|
|
||||||
* @property {Object<string, string>} env Environment
|
|
||||||
* @property {RouterRequest} req Request Object
|
|
||||||
* @property {RouterResponse} res Response Object
|
|
||||||
* @property {RouterNext} next Next Handler
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Request Object
|
|
||||||
*
|
|
||||||
* @typedef RouterRequest
|
|
||||||
* @property {string} method HTTP request method
|
|
||||||
* @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 {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 | number>} cf object containing custom Cloudflare properties. (https://developers.cloudflare.com/workers/examples/accessing-the-cloudflare-object)
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Response Object
|
|
||||||
*
|
|
||||||
* @typedef RouterResponse
|
|
||||||
* @property {Headers} headers Response headers object
|
|
||||||
* @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 {Response} raw A response object that is to be returned, this will void all other res properties and return this as is.
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Next Function
|
|
||||||
*
|
|
||||||
* @callback RouterNext
|
|
||||||
* @returns {Promise}
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Handler Function
|
|
||||||
*
|
|
||||||
* @callback RouterHandler
|
|
||||||
* @param {RouterContext} ctx
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Register global handler
|
|
||||||
*
|
|
||||||
* @param {RouterHandler} handler
|
|
||||||
*/
|
|
||||||
use(handlers) {
|
|
||||||
this.globalHandlers.push(handlers)
|
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -107,10 +176,10 @@ class Router {
|
|||||||
* Register CONNECT route
|
* Register CONNECT route
|
||||||
*
|
*
|
||||||
* @param {string} url
|
* @param {string} url
|
||||||
* @param {...RouterHandler} handlers
|
* @param {RouterHandler[]} handlers
|
||||||
* @returns {Router}
|
* @returns {Router}
|
||||||
*/
|
*/
|
||||||
connect(url, ...handlers) {
|
public connect(url: string, ...handlers: RouterHandler[]): Router {
|
||||||
return this.register('CONNECT', url, handlers)
|
return this.register('CONNECT', url, handlers)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -118,10 +187,10 @@ class Router {
|
|||||||
* Register DELETE route
|
* Register DELETE route
|
||||||
*
|
*
|
||||||
* @param {string} url
|
* @param {string} url
|
||||||
* @param {...RouterHandler} handlers
|
* @param {RouterHandler[]} handlers
|
||||||
* @returns {Router}
|
* @returns {Router}
|
||||||
*/
|
*/
|
||||||
delete(url, ...handlers) {
|
public delete(url: string, ...handlers: RouterHandler[]): Router {
|
||||||
return this.register('DELETE', url, handlers)
|
return this.register('DELETE', url, handlers)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -129,10 +198,10 @@ class Router {
|
|||||||
* Register GET route
|
* Register GET route
|
||||||
*
|
*
|
||||||
* @param {string} url
|
* @param {string} url
|
||||||
* @param {...RouterHandler} handlers
|
* @param {RouterHandler[]} handlers
|
||||||
* @returns {Router}
|
* @returns {Router}
|
||||||
*/
|
*/
|
||||||
get(url, ...handlers) {
|
public get(url: string, ...handlers: RouterHandler[]): Router {
|
||||||
return this.register('GET', url, handlers)
|
return this.register('GET', url, handlers)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -140,10 +209,10 @@ class Router {
|
|||||||
* Register HEAD route
|
* Register HEAD route
|
||||||
*
|
*
|
||||||
* @param {string} url
|
* @param {string} url
|
||||||
* @param {...RouterHandler} handlers
|
* @param {RouterHandler[]} handlers
|
||||||
* @returns {Router}
|
* @returns {Router}
|
||||||
*/
|
*/
|
||||||
head(url, ...handlers) {
|
public head(url: string, ...handlers: RouterHandler[]): Router {
|
||||||
return this.register('HEAD', url, handlers)
|
return this.register('HEAD', url, handlers)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -151,10 +220,10 @@ class Router {
|
|||||||
* Register OPTIONS route
|
* Register OPTIONS route
|
||||||
*
|
*
|
||||||
* @param {string} url
|
* @param {string} url
|
||||||
* @param {...RouterHandler} handlers
|
* @param {RouterHandler[]} handlers
|
||||||
* @returns {Router}
|
* @returns {Router}
|
||||||
*/
|
*/
|
||||||
options(url, ...handlers) {
|
public options(url: string, ...handlers: RouterHandler[]): Router {
|
||||||
return this.register('OPTIONS', url, handlers)
|
return this.register('OPTIONS', url, handlers)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -162,10 +231,10 @@ class Router {
|
|||||||
* Register PATCH route
|
* Register PATCH route
|
||||||
*
|
*
|
||||||
* @param {string} url
|
* @param {string} url
|
||||||
* @param {...RouterHandler} handlers
|
* @param {RouterHandler[]} handlers
|
||||||
* @returns {Router}
|
* @returns {Router}
|
||||||
*/
|
*/
|
||||||
patch(url, ...handlers) {
|
public patch(url: string, ...handlers: RouterHandler[]): Router {
|
||||||
return this.register('PATCH', url, handlers)
|
return this.register('PATCH', url, handlers)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -173,10 +242,10 @@ class Router {
|
|||||||
* Register POST route
|
* Register POST route
|
||||||
*
|
*
|
||||||
* @param {string} url
|
* @param {string} url
|
||||||
* @param {...RouterHandler} handlers
|
* @param {RouterHandler[]} handlers
|
||||||
* @returns {Router}
|
* @returns {Router}
|
||||||
*/
|
*/
|
||||||
post(url, ...handlers) {
|
public post(url: string, ...handlers: RouterHandler[]): Router {
|
||||||
return this.register('POST', url, handlers)
|
return this.register('POST', url, handlers)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -184,10 +253,10 @@ class Router {
|
|||||||
* Register PUT route
|
* Register PUT route
|
||||||
*
|
*
|
||||||
* @param {string} url
|
* @param {string} url
|
||||||
* @param {...RouterHandler} handlers
|
* @param {RouterHandler[]} handlers
|
||||||
* @returns {Router}
|
* @returns {Router}
|
||||||
*/
|
*/
|
||||||
put(url, ...handlers) {
|
public put(url: string, ...handlers: RouterHandler[]): Router {
|
||||||
return this.register('PUT', url, handlers)
|
return this.register('PUT', url, handlers)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -195,10 +264,10 @@ class Router {
|
|||||||
* Register TRACE route
|
* Register TRACE route
|
||||||
*
|
*
|
||||||
* @param {string} url
|
* @param {string} url
|
||||||
* @param {...RouterHandler} handlers
|
* @param {RouterHandler[]} handlers
|
||||||
* @returns {Router}
|
* @returns {Router}
|
||||||
*/
|
*/
|
||||||
trace(url, ...handlers) {
|
public trace(url: string, ...handlers: RouterHandler[]): Router {
|
||||||
return this.register('TRACE', url, handlers)
|
return this.register('TRACE', url, handlers)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -206,53 +275,29 @@ class Router {
|
|||||||
* Register route, ignoring method
|
* Register route, ignoring method
|
||||||
*
|
*
|
||||||
* @param {string} url
|
* @param {string} url
|
||||||
* @param {...RouterHandler} handlers
|
* @param {RouterHandler[]} handlers
|
||||||
* @returns {Router}
|
* @returns {Router}
|
||||||
*/
|
*/
|
||||||
any(url, ...handlers) {
|
public any(url: string, ...handlers: RouterHandler[]): Router {
|
||||||
return this.register('*', url, handlers)
|
return this.register('*', url, handlers)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Register route, ignoring method
|
|
||||||
*
|
|
||||||
* @deprecated since version 1.0.2, use .any(url, ...handlers) instead.
|
|
||||||
* @param {string} url
|
|
||||||
* @param {...RouterHandler} handlers
|
|
||||||
* @returns {Router}
|
|
||||||
*/
|
|
||||||
all(url, ...handlers) {
|
|
||||||
console.warn('WARNING: This function is deprecated and will be removed in a future release, please use .any(url, ...handlers) instead.')
|
|
||||||
return this.any(url, handlers)
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Debug Mode
|
* Debug Mode
|
||||||
*
|
*
|
||||||
* @param {boolean} [state=true] 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) {
|
public debug(state = true) {
|
||||||
this.debugMode = state
|
this.debugMode = state
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* CORS Config
|
|
||||||
*
|
|
||||||
* @typedef RouterCorsConfig
|
|
||||||
* @property {string} allowOrigin Access-Control-Allow-Origin (default: `*`)
|
|
||||||
* @property {string} allowMethods Access-Control-Allow-Methods (default: `*`)
|
|
||||||
* @property {string} allowHeaders Access-Control-Allow-Headers (default: `*`)
|
|
||||||
* @property {number} maxAge Access-Control-Max-Age (default: `86400`)
|
|
||||||
* @property {number} optionsSuccessStatus Return status code for OPTIONS request (default: `204`)
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Enable CORS support
|
* Enable CORS support
|
||||||
*
|
*
|
||||||
* @param {RouterCorsConfig} config
|
* @param {RouterCorsConfig} config
|
||||||
* @returns {Router}
|
* @returns {Router}
|
||||||
*/
|
*/
|
||||||
cors(config) {
|
public cors(config: RouterCorsConfig): Router {
|
||||||
config = config || {}
|
config = config || {}
|
||||||
this.corsConfig = {
|
this.corsConfig = {
|
||||||
allowOrigin: config.allowOrigin || '*',
|
allowOrigin: config.allowOrigin || '*',
|
||||||
@@ -273,7 +318,7 @@ class Router {
|
|||||||
* @param {RouterHandler[]} handlers Arrar of handler functions
|
* @param {RouterHandler[]} handlers Arrar of handler functions
|
||||||
* @returns {Router}
|
* @returns {Router}
|
||||||
*/
|
*/
|
||||||
register(method, url, handlers) {
|
private register(method: string, url: string, handlers: RouterHandler[]): Router {
|
||||||
this.routes.push({
|
this.routes.push({
|
||||||
method,
|
method,
|
||||||
url,
|
url,
|
||||||
@@ -287,16 +332,16 @@ class Router {
|
|||||||
*
|
*
|
||||||
* @private
|
* @private
|
||||||
* @param {Request} request
|
* @param {Request} request
|
||||||
* @returns {Route | undefined}
|
* @returns {RouterRequest | undefined}
|
||||||
*/
|
*/
|
||||||
getRoute(request) {
|
private getRoute(request: RouterRequest): Route | undefined {
|
||||||
const url = new URL(request.url)
|
const url = new URL(request.url)
|
||||||
const pathArr = url.pathname.split('/').filter(i => i)
|
const pathArr = url.pathname.split('/').filter(i => i)
|
||||||
return this.routes.find(r => {
|
return this.routes.find(r => {
|
||||||
const routeArr = r.url.split('/').filter(i => i)
|
const routeArr = r.url.split('/').filter(i => i)
|
||||||
if (![request.method, '*'].includes(r.method) || routeArr.length !== pathArr.length)
|
if (![request.method, '*'].includes(r.method) || routeArr.length !== pathArr.length)
|
||||||
return false
|
return false
|
||||||
const params = {}
|
const params: RouterRequestParams = {}
|
||||||
for (let i = 0; i < routeArr.length; i++) {
|
for (let i = 0; i < routeArr.length; i++) {
|
||||||
if (routeArr[i] !== pathArr[i] && routeArr[i][0] !== ':')
|
if (routeArr[i] !== pathArr[i] && routeArr[i][0] !== ':')
|
||||||
return false
|
return false
|
||||||
@@ -304,7 +349,7 @@ class Router {
|
|||||||
params[routeArr[i].substring(1)] = pathArr[i]
|
params[routeArr[i].substring(1)] = pathArr[i]
|
||||||
}
|
}
|
||||||
request.params = params
|
request.params = params
|
||||||
const query = {}
|
const query: any = {}
|
||||||
for (const [k, v] of url.searchParams.entries()) {
|
for (const [k, v] of url.searchParams.entries()) {
|
||||||
query[k] = v
|
query[k] = v
|
||||||
}
|
}
|
||||||
@@ -321,18 +366,15 @@ class Router {
|
|||||||
* @param {any=} extend
|
* @param {any=} extend
|
||||||
* @returns {Response}
|
* @returns {Response}
|
||||||
*/
|
*/
|
||||||
async handle(env, request, extend = {}) {
|
public async handle(env: any, request: Request, extend: any = {}) {
|
||||||
try {
|
try {
|
||||||
if (request instanceof Event) {
|
const req: RouterRequest = {
|
||||||
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 = {
|
|
||||||
...extend,
|
...extend,
|
||||||
method: request.method,
|
method: request.method,
|
||||||
headers: request.headers,
|
headers: request.headers,
|
||||||
url: request.url,
|
url: request.url,
|
||||||
params: [],
|
cf: request.cf,
|
||||||
|
params: {},
|
||||||
query: {},
|
query: {},
|
||||||
body: ''
|
body: ''
|
||||||
}
|
}
|
||||||
@@ -342,13 +384,13 @@ class Router {
|
|||||||
'Access-Control-Allow-Origin': this.corsConfig.allowOrigin,
|
'Access-Control-Allow-Origin': this.corsConfig.allowOrigin,
|
||||||
'Access-Control-Allow-Methods': this.corsConfig.allowMethods,
|
'Access-Control-Allow-Methods': this.corsConfig.allowMethods,
|
||||||
'Access-Control-Allow-Headers': this.corsConfig.allowHeaders,
|
'Access-Control-Allow-Headers': this.corsConfig.allowHeaders,
|
||||||
'Access-Control-Max-Age': this.corsConfig.maxAge
|
'Access-Control-Max-Age': this.corsConfig.maxAge.toString()
|
||||||
},
|
},
|
||||||
status: this.corsConfig.optionsSuccessStatus
|
status: this.corsConfig.optionsSuccessStatus
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
if (['POST', 'PUT', 'PATCH'].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()
|
||||||
} catch {
|
} catch {
|
||||||
@@ -363,21 +405,18 @@ class Router {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
const route = this.getRoute(req)
|
const route = this.getRoute(req)
|
||||||
if (!route) {
|
if (!route)
|
||||||
return new Response(this.debugMode ? 'Route not found!' : null, {
|
return new Response(this.debugMode ? 'Route not found!' : null, { status: 404 })
|
||||||
status: 404
|
const res: RouterResponse = { headers: new Headers() }
|
||||||
})
|
|
||||||
}
|
|
||||||
const res = { headers: new Headers() }
|
|
||||||
if (Object.keys(this.corsConfig).length) {
|
if (Object.keys(this.corsConfig).length) {
|
||||||
res.headers.set('Access-Control-Allow-Origin', this.corsConfig.allowOrigin)
|
res.headers.set('Access-Control-Allow-Origin', this.corsConfig.allowOrigin)
|
||||||
res.headers.set('Access-Control-Allow-Methods', this.corsConfig.allowMethods)
|
res.headers.set('Access-Control-Allow-Methods', this.corsConfig.allowMethods)
|
||||||
res.headers.set('Access-Control-Allow-Headers', this.corsConfig.allowHeaders)
|
res.headers.set('Access-Control-Allow-Headers', this.corsConfig.allowHeaders)
|
||||||
res.headers.set('Access-Control-Max-Age', this.corsConfig.maxAge)
|
res.headers.set('Access-Control-Max-Age', this.corsConfig.maxAge.toString())
|
||||||
}
|
}
|
||||||
const handlers = [...this.globalHandlers, ...route.handlers]
|
const handlers = [...this.globalHandlers, ...route.handlers]
|
||||||
let prevIndex = -1
|
let prevIndex = -1
|
||||||
const runner = async index => {
|
const runner = async (index: number) => {
|
||||||
if (index === prevIndex)
|
if (index === prevIndex)
|
||||||
throw new Error('next() called multiple times')
|
throw new Error('next() called multiple times')
|
||||||
prevIndex = index
|
prevIndex = index
|
||||||
@@ -390,25 +429,14 @@ class Router {
|
|||||||
res.headers.set('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)
|
||||||
return res.raw
|
return res.raw
|
||||||
}
|
return new Response([101, 204, 205, 304].includes(res.status || (res.body ? 200 : 204)) ? null : res.body, { status: res.status, headers: res.headers, webSocket: res.webSocket || null })
|
||||||
|
|
||||||
const resOpts = {
|
|
||||||
status: res.status || (res.body ? 200 : 204),
|
|
||||||
headers: res.headers
|
|
||||||
}
|
|
||||||
|
|
||||||
if (res.webSocket) {
|
|
||||||
resOpts.webSocket = res.webSocket
|
|
||||||
}
|
|
||||||
|
|
||||||
return new Response([101, 204, 205, 304].includes(resOpts.status) ? null : res.body, resOpts)
|
|
||||||
} catch(err) {
|
} catch(err) {
|
||||||
console.error(err)
|
console.error(err)
|
||||||
return new Response(this.debugMode ? err.stack : '', { status: 500 })
|
return new Response(this.debugMode && err instanceof Error ? err.stack : '', { status: 500 })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = Router
|
export default Router
|
||||||
27
tsconfig.json
Normal file
27
tsconfig.json
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
{
|
||||||
|
"compilerOptions": {
|
||||||
|
"outDir": "./dist",
|
||||||
|
"module": "commonjs",
|
||||||
|
"target": "esnext",
|
||||||
|
"lib": ["esnext"],
|
||||||
|
"declaration": true,
|
||||||
|
"strict": true,
|
||||||
|
"noImplicitAny": true,
|
||||||
|
"strictNullChecks": true,
|
||||||
|
"strictFunctionTypes": true,
|
||||||
|
"strictBindCallApply": true,
|
||||||
|
"strictPropertyInitialization": true,
|
||||||
|
"noImplicitThis": true,
|
||||||
|
"alwaysStrict": true,
|
||||||
|
"noUnusedLocals": true,
|
||||||
|
"noUnusedParameters": true,
|
||||||
|
"noImplicitReturns": true,
|
||||||
|
"noFallthroughCasesInSwitch": true,
|
||||||
|
"esModuleInterop": true,
|
||||||
|
"preserveConstEnums": true,
|
||||||
|
"moduleResolution": "node",
|
||||||
|
"types": ["@cloudflare/workers-types"]
|
||||||
|
},
|
||||||
|
"include": ["src"],
|
||||||
|
"exclude": ["node_modules", "dist", "test"]
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user