UNPKG

@athenna/http

Version:

The Athenna Http server. Built on top of fastify.

113 lines (112 loc) 3.34 kB
/** * @athenna/http * * (c) João Lenon <lenon@athenna.io> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ import { Route } from '#src/router/Route'; import { Macroable } from '@athenna/common'; import { RouteGroup } from '#src/router/RouteGroup'; import { RouteResource } from '#src/router/RouteResource'; import type { RouteJson, RequestHandler, RouteHandler } from '#src/types'; import type { HTTPMethods, FastifyInstance, RouteOptions } from 'fastify'; export declare class Router extends Macroable { /** * All routes registered. */ routes: (Route | RouteGroup | RouteResource)[]; /** * Route groups opened. */ private openedGroups; /** * The controller instance. */ private controllerInstance; /** * List the routes registered. */ list(): RouteJson[]; /** * Set the controller instance. */ controller(controller: any): this; /** * Add route for a given pattern and methods */ route(pattern: string, methods: HTTPMethods[], handler: RouteHandler): Route; /** * Creates a vanilla fastify route without using Athenna router. */ vanillaRoute(options?: RouteOptions): FastifyInstance; /** * Creates a new route resource. */ resource(resource: string, controller?: any): RouteResource; /** * This method is a convenient shortcut to render a view without * defining an explicit handler. */ view(pattern: string, view: string, data?: any): Route; /** * This method is a convenient shortcut to redirect a route without * defining an explicit handler. */ redirect(pattern: string, url: string, status?: number): Route; /** * Define a route that handles all common HTTP methods. */ any(pattern: string, handler: RequestHandler): Route; /** * Define GET route. */ get(pattern: string, handler: RouteHandler): Route; /** * Define HEAD route. */ head(pattern: string, handler: RouteHandler): Route; /** * Define POST route. */ post(pattern: string, handler: RouteHandler): Route; /** * Define PUT route. */ put(pattern: string, handler: RouteHandler): Route; /** * Define PATCH route. */ patch(pattern: string, handler: RouteHandler): Route; /** * Define DELETE route. */ delete(pattern: string, handler: RouteHandler): Route; /** * Define OPTIONS route. */ options(pattern: string, handler: RouteHandler): Route; /** * Creates a group of routes. Anything applied in route groups will be applied * in the routes that are inside that group. */ group(callback: () => void): RouteGroup; /** * Register all the routes inside the http server. After routes are registered, * anyone could be registered anymore. */ register(): void; /** * Transform some route array to a route json array. */ toJSON(routes: (Route | RouteGroup | RouteResource)[]): RouteJson[]; /** * Get the most recent route group created. */ private getRecentGroup; /** * Indicates if if a valid controller handler method. */ private isValidControllerHandler; }