UNPKG

@athenna/http

Version:

The Athenna Http server. Built on top of fastify.

279 lines (278 loc) 6.95 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 type { RouteJson, RouteHandler, RequestHandler, MiddlewareRecord, MiddlewareRouteType, TerminatorRouteType, InterceptorRouteType } from '#src/types'; import type { HTTPMethods, RouteOptions } from 'fastify'; import { Macroable } from '@athenna/common'; import { type RouteSchemaOptions } from '#src/router/RouteSchema'; export declare class Route extends Macroable { /** * Holds all the route implementations to be registered in the Server. */ route: { url?: string; data?: Record<string, any>; methods: HTTPMethods[]; handler?: RequestHandler; name?: string; deleted?: boolean; prefixes?: string[]; middlewares?: MiddlewareRecord; fastify?: Partial<RouteOptions>; }; constructor(url: string, methods: HTTPMethods[], handler: RouteHandler); /** * Set the route name. * * @example * ```ts * Route.name('profile') * ``` */ name(name: string): Route; /** * Set fastify vanilla route options. * * @example * ```ts * Route.vanillaOptions({ schema: {} }) * ``` */ vanillaOptions(options: Partial<RouteOptions>): Route; /** * Define data values for the route. Data values will be available in the * request context. * * @example * ```ts * Route.data({ permission: 'post:create' }).post('/posts', ({ data }) => { * console.log(data.permission) * }) * ``` * */ data(data: Record<string, any> | string, value?: any): Route; /** * Set a prefix for the route. * * @example * ```ts * Route.prefix('/api/v1') * ``` */ prefix(prefix: string): Route; /** * Set a named validator, validator closure or a MiddlewareContract implementation * in the route. */ validator(validator: MiddlewareRouteType, prepend?: boolean): Route; /** * Set a named middleware, middleware closure or a MiddlewareContract implementation * in the route. */ middleware(middleware: MiddlewareRouteType, prepend?: boolean): Route; /** * Set a named interceptor, interceptor closure or a InterceptorContract implementation * in the route. */ interceptor(interceptor: InterceptorRouteType, prepend?: boolean): Route; /** * Set a named terminator, terminator closure or a TerminatorContract implementation * in the route. */ terminator(terminator: TerminatorRouteType, prepend?: boolean): Route; /** * Set up all helmet options for route. * * @example * ```ts * Route.helmet({ * dnsPrefetchControl: { allow: true } * }) * ``` */ helmet(options: Omit<import('@fastify/helmet').FastifyHelmetOptions, 'global'>): Route; /** * Set up all schema options for route. * * @example * ```ts * Route.schema({ * body: { * type: 'array', * items: { * oneOf: [{ type: 'string', description: 'User name' }] * } * } * }) * ``` */ schema(options: RouteSchemaOptions): Route; /** * Set up all rate limit options for route. * * @example * ```ts * Route.rateLimit({ * max: 3, * timeWindow: '1 minute' * }) * ``` */ rateLimit(options: import('@fastify/rate-limit').RateLimitOptions): Route; /** * Hide the route in swagger docs. * * @example * ```ts * Route.hide() * ``` */ hide(): Route; /** * Set the route as deprecated in swagger docs. * * @example * ```ts * Route.deprecated() * ``` */ deprecated(): Route; /** * Set a summary for the route swagger docs. * * @example * ```ts * Route.summary('List all users') * ``` */ summary(summary: string): Route; /** * Set a description for the route swagger docs. * * @example * ```ts * Route.description('List all users') * ``` */ description(description: string): Route; /** * Set tags for the route swagger docs. * * @example * ```ts * Route.tags(['v1']) * ``` */ tags(tags: string[]): Route; /** * Set body param for the route swagger docs. * * @example * ```ts * Route.body('name', { * type: 'string', * description: 'User name' * }) * ``` */ body(name: string, body?: any): Route; /** * Set param for the route swagger docs. * * @example * ```ts * Route.param('id', { description: 'Set the user id' }) * ``` */ param(name: string, param?: any): Route; /** * Set query string for the route swagger docs. * * @example * ```ts * Route.queryString('page', { description: 'Set the pagination page' }) * ``` */ queryString(name: string, queryString?: any): Route; /** * Set response for the route swagger docs. * * @example * For objects * ```ts * Route.response(200, { * description: 'Return one user', * properties: { * name: { type: 'string', description: 'User name' } * } * }) * ``` * * For arrays * ```ts * Route.response(200, { * description: 'Return users paginated', * schema: { * type: 'array' * oneOf: [ * { type: 'string', description: 'User name' } * ] * } * }) * ``` */ response(statusCode: number, response?: any): Route; /** * Set the route security in swagger docs. * * @example * ```ts * Route.security('apiToken', []) * ``` */ security(key: string, values: string[]): Route; /** * Set the external documentation url in swagger docs. * * @example * ```ts * Route.externalDocs('https://github.com/athennaio/docs', 'Athenna Framework Documentation') * ``` */ externalDocs(url: string, description?: string): Route; /** * Set the type of the content that the API consumes in swagger docs. * * @example * ```ts * Route.consumes(['json', 'yaml', ...]) * ``` */ consumes(consumes: string[]): Route; /** * Set the type of the content that the API produces in swagger docs. * * @example * ```ts * Route.produces(['json', 'yaml', ...]) * ``` */ produces(produces: string[]): Route; /** * Get the route as JSON. * * @example * ```ts * Route.toJSON() * ``` */ toJSON(): RouteJson; private getUrl; private removeSlashes; }