diff --git a/src/index.ts b/src/index.ts index a2634dd..4cb29d5 100644 --- a/src/index.ts +++ b/src/index.ts @@ -13,17 +13,17 @@ export interface Route { } /** - * Router Context - * - * @typedef RouterContext - * @property {RouterEnv} env Environment - * @property {RouterRequest} req Request Object - * @property {ExecutionContext} ctx Context Object - */ +* Router Context +* +* @typedef RouterContext +* @property {RouterEnv} env Environment +* @property {RouterRequest} req Request Object +* @property {ExecutionContext} ctx Context Object +*/ export interface RouterContext { - env: TEnv - req: RouterRequest - ctx: ExecutionContext + env: TEnv + req: RouterRequest + ctx: ExecutionContext } /** @@ -68,14 +68,14 @@ export interface RouterRequestQuery { } /** - * Handler Function - * - * @callback RouterHandler - * @param {RouterContext} ctx - * @returns {Promise Response | void} - */ +* Handler Function +* +* @callback RouterHandler +* @param {RouterContext} ctx +* @returns {Promise Response | void} +*/ export interface RouterHandler { - (ctx: RouterContext): Promise | Response | void + (ctx: RouterContext): Promise | Response | void } /** @@ -296,33 +296,33 @@ export class Router { return this } - private setCorsHeaders(headers: Headers = new Headers()): Headers { - if (this.corsConfig.allowOrigin && !headers.has('Access-Control-Allow-Origin')) - headers.set('Access-Control-Allow-Origin', this.corsConfig.allowOrigin) - if (this.corsConfig.allowMethods && !headers.has('Access-Control-Allow-Methods')) - headers.set('Access-Control-Allow-Methods', this.corsConfig.allowMethods) - if (this.corsConfig.allowHeaders && !headers.has('Access-Control-Allow-Headers')) - headers.set('Access-Control-Allow-Headers', this.corsConfig.allowHeaders) - if (this.corsConfig.maxAge && !headers.has('Access-Control-Max-Age')) - headers.set('Access-Control-Max-Age', this.corsConfig.maxAge.toString()) - return headers - } + private setCorsHeaders(headers: Headers = new Headers()): Headers { + if (this.corsConfig.allowOrigin && !headers.has('Access-Control-Allow-Origin')) + headers.set('Access-Control-Allow-Origin', this.corsConfig.allowOrigin) + if (this.corsConfig.allowMethods && !headers.has('Access-Control-Allow-Methods')) + headers.set('Access-Control-Allow-Methods', this.corsConfig.allowMethods) + if (this.corsConfig.allowHeaders && !headers.has('Access-Control-Allow-Headers')) + headers.set('Access-Control-Allow-Headers', this.corsConfig.allowHeaders) + if (this.corsConfig.maxAge && !headers.has('Access-Control-Max-Age')) + headers.set('Access-Control-Max-Age', this.corsConfig.maxAge.toString()) + return headers + } - /** - * 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 { - this.routes.push({ - method, - url, - handlers - }) + /** + * 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 { + this.routes.push({ + method, + url, + handlers + }) return this } @@ -369,56 +369,56 @@ export class Router { } /** - * Handle requests - * - * @param {TEnv} env - * @param {Request} request - * @param {any} [extend] - * @returns {Promise} - */ - public async handle(request: Request, env: TEnv, ctx: ExecutionContext, extend: any = {}): Promise { - const req: RouterRequest = { - ...extend, - method: request.method, - headers: request.headers, - url: request.url, - cf: request.cf, - params: {}, - query: {}, - body: '' - } + * Handle requests + * + * @param {TEnv} env + * @param {Request} request + * @param {any} [extend] + * @returns {Promise} + */ + public async handle(request: Request, env: TEnv, ctx: ExecutionContext, extend: any = {}): Promise { + const req: RouterRequest = { + ...extend, + method: request.method, + headers: request.headers, + url: request.url, + cf: request.cf, + params: {}, + query: {}, + body: '' + } - const route = this.getRoute(req) + const route = this.getRoute(req) - if (!route) - return new Response(this.debugMode ? 'Route not found!' : null, { status: 404 }) + if (!route) + return new Response(this.debugMode ? 'Route not found!' : null, { status: 404 }) - if (this.corsEnabled && req.method === 'OPTIONS') { - return new Response(null, { - headers: this.setCorsHeaders(), - status: this.corsConfig.optionsSuccessStatus - }) - } + if (this.corsEnabled && req.method === 'OPTIONS') { + return new Response(null, { + headers: this.setCorsHeaders(), + status: this.corsConfig.optionsSuccessStatus + }) + } - const handlers = [...this.globalHandlers, ...route.handlers] + const handlers = [...this.globalHandlers, ...route.handlers] - let response: Response | undefined + let response: Response | undefined - for (const handler of handlers) { - const res = await handler({ env, req, ctx }) + for (const handler of handlers) { + const res = await handler({ env, req, ctx }) - if (res) { - response = res - break - } - } + if (res) { + response = res + break + } + } - if (!response) - return new Response(this.debugMode ? 'Handler did not return a Response!' : null, { status: 404 }) + if (!response) + return new Response(this.debugMode ? 'Handler did not return a Response!' : null, { status: 404 }) - if (this.corsEnabled) - this.setCorsHeaders(response.headers) + if (this.corsEnabled) + this.setCorsHeaders(response.headers) - return response - } + return response + } }