1
0

minor type fixes

This commit is contained in:
2022-07-19 18:51:31 +02:00
parent 6f84645f89
commit b16a56bbca

View File

@@ -16,7 +16,7 @@
* Router Context * Router Context
* *
* @typedef RouterContext * @typedef RouterContext
* @property {Object<string, string>} env Environment * @property {RouterEnv} env Environment
* @property {RouterRequest} req Request Object * @property {RouterRequest} req Request Object
* @property {RouterResponse} res Response Object * @property {RouterResponse} res Response Object
* @property {RouterNext} next Next Handler * @property {RouterNext} next Next Handler
@@ -37,7 +37,7 @@ export interface RouterContext {
* @property {RouterRequestParams} params Object containing all parameters defined in the url string * @property {RouterRequestParams} params Object containing all parameters defined in the url string
* @property {RouterRequestQuery} query Object containing all query parameters * @property {RouterRequestQuery} query Object containing all query parameters
* @property {Headers} headers Request headers object * @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 {string | 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) * @property {IncomingRequestCfProperties} [cf] object containing custom Cloudflare properties. (https://developers.cloudflare.com/workers/examples/accessing-the-cloudflare-object)
*/ */
export interface RouterRequest { export interface RouterRequest {
@@ -46,7 +46,7 @@ export interface RouterRequest {
params: RouterRequestParams params: RouterRequestParams
query: RouterRequestQuery query: RouterRequestQuery
headers: Headers headers: Headers
body: any body: string | any
cf?: IncomingRequestCfProperties cf?: IncomingRequestCfProperties
[key: string]: any [key: string]: any
} }
@@ -75,13 +75,13 @@ export interface RouterRequestQuery {
* @typedef RouterResponse * @typedef RouterResponse
* @property {Headers} headers Response headers object * @property {Headers} headers Response headers object
* @property {number} [status=204] Return status code (default: `204`) * @property {number} [status=204] Return status code (default: `204`)
* @property {any} [body] Either an `object` (will be converted to JSON) or a string * @property {string | 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. * @property {Response} [raw] A response object that is to be returned, this will void all other res properties and return this as is.
*/ */
export interface RouterResponse { export interface RouterResponse {
headers: Headers headers: Headers
status?: number status?: number
body?: any body?: string | any
raw?: Response, raw?: Response,
webSocket?: WebSocket webSocket?: WebSocket
} }
@@ -90,7 +90,7 @@ export interface RouterResponse {
* Next Function * Next Function
* *
* @callback RouterNext * @callback RouterNext
* @returns {Promise} * @returns {Promise<void>}
*/ */
export interface RouterNext { export interface RouterNext {
(): Promise<void> (): Promise<void>
@@ -143,6 +143,9 @@ export default class Router {
/** /**
* Global Handlers * Global Handlers
*
* @protected
* @type {RouterHandler[]}
*/ */
protected globalHandlers: RouterHandler[] = [] protected globalHandlers: RouterHandler[] = []
@@ -297,7 +300,7 @@ export default class Router {
* @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)
* @returns {Router} * @returns {Router}
*/ */
public debug(state = true): Router { public debug(state: boolean = true): Router {
this.debugMode = state this.debugMode = state
return this return this
} }
@@ -341,8 +344,8 @@ export default class Router {
* Get Route by request * Get Route by request
* *
* @private * @private
* @param {Request} request * @param {RouterRequest} request
* @returns {RouterRequest | undefined} * @returns {Route | undefined}
*/ */
private getRoute(request: RouterRequest): Route | undefined { private getRoute(request: RouterRequest): Route | undefined {
const url = new URL(request.url) const url = new URL(request.url)