import { Elysia, type Context } from "elysia"; import type { Config } from "../types/config.types"; import type { DocumentedRouteInput } from "../types/route.types"; /** * Main ApiNote class for creating documented, type-safe REST APIs with Elysia * * ApiNote provides a high-level abstraction over Elysia to create well-documented REST APIs * with built-in JWT authentication, CORS support, automatic OpenAPI documentation generation, * and type-safe request/response validation. It simplifies the process of building production-ready * APIs by handling common middleware setup, schema validation, and documentation automatically. * * @class * @example * ```typescript * const api = new ApiNote({ * title: 'Todo API', * description: 'A simple todo management API', * version: '1.0.0', * host: 'localhost:8080' * }, 'jwt-secret-key'); * * api.documentedRoute({ * method: 'GET', * path: '/todos', * summary: 'List all todos', * handler: async (ctx) => ({ todos: [] }), * responses: { * '200': 'Success' * } * }); * * await api.listen(8080); * ``` */ export declare class ApiNote { private app; private config; private jwtSecret; private routes; /** * Creates a new ApiNote instance with the specified configuration * * Initializes the Elysia application, sets up middleware (JWT, CORS, Security Headers, * Error Handling, etc.), and configures OpenAPI documentation. The constructor automatically * applies default values for host and basePath if not provided. * * @param config - Configuration object for the API * @param config.title - API title for documentation * @param config.description - API description for documentation * @param config.version - Semantic version of the API * @param config.host - Host address (default: "localhost") * @param config.basePath - Base path for all routes (default: "/") * @param config.customMiddleware - Optional custom middleware array * @param config.securityHeaders - Security headers configuration (default: enabled) * @param config.rateLimit - Rate limiting configuration (default: disabled) * @param config.errorHandler - Error handler configuration (default: enabled) * @param config.healthCheck - Health check configuration (default: enabled) * @param config.cors - CORS configuration (default: permissive) * @param jwtSecret - Secret key for JWT operations (REQUIRED: provide your own secret) * * @example * ```typescript * const api = new ApiNote({ * title: 'User Service', * description: 'Microservice for user management', * version: '2.1.0', * basePath: '/api/v2', * securityHeaders: { hsts: true, csp: true }, * rateLimit: { max: 100, windowMs: 60000 }, * healthCheck: { path: '/health', includeDetails: true } * }, process.env.JWT_SECRET!); * ``` */ constructor(config: Config, jwtSecret?: string); /** * Sets up all middleware for the Elysia application * * This private method is called during construction to configure middleware * in the following order (numbered list): * (1) Error handler middleware (must be first to catch all errors) * (2) Security headers middleware (HSTS, CSP, etc.) * (3) Rate limiting middleware (DoS protection) * (4) CORS middleware for cross-origin requests * (5) JWT middleware for authentication token handling * (6) Health check endpoints * (7) Custom middleware provided in the configuration * * Each middleware can be disabled or customized via the config object * * @private * @returns {void} */ private setupMiddleware; /** * Sets up default routes to handle common browser requests * * This private method adds handlers for: * - /favicon.ico - Returns 204 No Content to prevent NOT_FOUND errors * * @private * @returns {void} */ private setupDefaultRoutes; /** * Registers a documented route with automatic validation, authentication, and OpenAPI generation * * This is the primary method for adding routes to your API. It handles: * - Request validation (query params, path params, headers, body) * - Response documentation and schema validation * - JWT authentication when requiresAuth is true * - Automatic OpenAPI/Swagger documentation generation * - Type-safe request/response handling with TypeBox schemas * * @param input - Complete route definition with documentation * @param input.method - HTTP method (GET, POST, PUT, DELETE, PATCH, OPTIONS, HEAD) * @param input.path - Route path (e.g., "/users/:id") * @param input.handler - Async function to handle the request * @param input.summary - Short summary for API documentation * @param input.description - Detailed description of the endpoint * @param input.tags - Array of tags for grouping in documentation * @param input.params - Array of parameter definitions (query, path, header) * @param input.schemasRequest - Request body schema (TypeBox or plain object) * @param input.schemasResponse - Response body schema for documentation * @param input.responses - Response status codes and descriptions * @param input.requiresAuth - Whether JWT authentication is required * * @returns The ApiNote instance for method chaining * * @example * ```typescript * api.documentedRoute({ * method: 'POST', * path: '/users', * summary: 'Create a new user', * description: 'Creates a new user account with the provided information', * tags: ['Users'], * schemasRequest: { * name: 'string', * email: 'string', * age: 'number' * }, * handler: async (ctx) => { * const body = ctx.body; * return { id: 1, ...body }; * }, * responses: { * '201': 'User created successfully', * '400': 'Invalid input' * }, * requiresAuth: true * }).documentedRoute({ * method: 'GET', * path: '/users/:id', * params: [{ * name: 'id', * in: 'path', * type: 'string', * required: true * }], * handler: async (ctx) => ({ user: { id: ctx.params.id } }) * }); * ``` */ documentedRoute(input: DocumentedRouteInput): ApiNote; /** * Registers a simple route without automatic documentation or validation * * Use this method when you need to add a route that doesn't require OpenAPI documentation * or automatic validation. This is useful for: * - Quick prototyping * - Internal endpoints that don't need documentation * - Custom endpoints with manual validation * * Note: Routes added with this method won't appear in the OpenAPI documentation * and won't have automatic request/response validation. * * @param method - HTTP method (GET, POST, PUT, DELETE, PATCH, OPTIONS, HEAD) * @param path - Route path relative to basePath (e.g., "/health") * @param handler - Function to handle the request, receives Elysia Context * * @returns The ApiNote instance for method chaining * * @example * ```typescript * api.route('GET', '/health', (ctx) => { * return { status: 'ok', timestamp: Date.now() }; * }).route('POST', '/webhook', async (ctx) => { * await processWebhook(ctx.body); * return { received: true }; * }); * ``` */ route(method: DocumentedRouteInput["method"], path: string, handler: (context: Context) => any): this; /** * Retrieves all registered documented routes * * Returns an array of all routes that were added using the documentedRoute() method. * This is useful for: * - Debugging route configurations * - Generating custom documentation * - Testing route registration * - Analyzing API structure * * Note: Routes added with route() method are not included in this list. * * @returns Array of documented route definitions * * @example * ```typescript * const routes = api.getRoutes(); * console.log(`Total documented routes: ${routes.length}`); * routes.forEach(route => { * console.log(`${route.method} ${route.path} - ${route.summary}`); * }); * ``` */ getRoutes(): DocumentedRouteInput[]; /** * Gets direct access to the underlying Elysia application instance * * This method provides access to the raw Elysia app for advanced use cases where * you need to: * - Add custom Elysia plugins * - Access low-level Elysia features * - Integrate with other Elysia middleware * - Customize error handling at the Elysia level * * Warning: Direct manipulation of the Elysia app may bypass ApiNote's built-in * features and documentation generation. Use with caution. * * @returns The Elysia application instance * * @example * ```typescript * const elysiaApp = api.getApp(); * * // Add custom Elysia plugin * elysiaApp.use(customPlugin()); * * // Add custom error handler * elysiaApp.onError((error) => { * console.error('Custom error handler:', error); * return { error: error.message }; * }); * ``` */ getApp(): Elysia; /** * Starts the HTTP server and begins listening for requests * * This method initializes the server and binds it to the specified port. It performs * the following actions: * - Determines the listening port from config.host, parameter, or defaults to 8080 * - Sets up global error handling * - Starts the Elysia server * - Displays server information including network addresses * - Shows available network interfaces (Wi-Fi and Ethernet) * - Prints the API documentation URL * * Port resolution priority: * 1. Port specified in config.host (e.g., "localhost:3000") * 2. Port parameter passed to listen() * 3. Default port 8080 * * @param port - Optional port number to listen on (overridden by config.host port) * * @returns Promise that resolves when the server starts successfully * @throws Error if the server fails to start (e.g., port already in use) * * @example * ```typescript * // Listen on specific port * await api.listen(3000); * * // Listen on default port (8080) * await api.listen(); * * // With error handling * try { * await api.listen(8080); * console.log('Server started successfully'); * } catch (error) { * console.error('Failed to start server:', error); * } * ``` */ listen(port?: number): Promise; /** * Gracefully stops the HTTP server and closes all connections * * This method shuts down the Elysia server, stopping it from accepting new connections * and closing existing ones. Use this for: * - Graceful shutdown procedures * - Testing cleanup * - Application termination * - Hot reloading * * @returns {void} * * @example * ```typescript * // Graceful shutdown on SIGTERM * process.on('SIGTERM', () => { * console.log('Shutting down gracefully...'); * api.stop(); * process.exit(0); * }); * * // Testing cleanup * afterAll(() => { * api.stop(); * }); * ``` */ stop(): void; } //# sourceMappingURL=api-note.d.ts.map