arvox-backend
Version:
Un framework backend moderne et modulaire basé sur Hono, TypeScript et l'architecture hexagonale avec authentification Better Auth + Drizzle intégrée
208 lines • 7.04 kB
TypeScript
import { OpenAPIHono } from '@hono/zod-openapi';
import { z } from 'zod';
import { IController } from '../interfaces/controller.interface';
import { ResponseUtil } from '../utils/response.util';
import { PaginationUtil } from '../utils/pagination.util';
/**
* Base controller class providing common HTTP functionality
* All controllers should extend this class for consistent behavior
*/
export declare abstract class BaseController implements IController {
controller: OpenAPIHono;
protected responseUtil: ResponseUtil;
protected paginationUtil: PaginationUtil;
constructor();
/**
* Initialize all routes for this controller
* Must be implemented by child classes
*/
abstract initRoutes(): void;
/**
* Handle standard success response
* @param data - Data to return
* @param status - HTTP status code (default: 200)
* @returns Formatted success response
*/
protected success<T>(data: T, status?: number): {
json: {
success: boolean;
data: T;
};
status: number;
};
/**
* Handle standard error response
* @param error - Error message or Error object
* @param status - HTTP status code (default: 400)
* @returns Formatted error response
*/
protected error(error: string | Error, status?: number): {
json: {
success: boolean;
error: string;
};
status: number;
};
/**
* Handle paginated response
* @param items - Array of items
* @param total - Total count of items
* @param page - Current page number
* @param limit - Items per page
* @param status - HTTP status code (default: 200)
* @returns Formatted paginated response
*/
protected paginated<T>(items: T[], total: number, page: number, limit: number, status?: number): {
json: {
success: boolean;
data: {
items: T[];
pagination: {
total: number;
page: number;
limit: number;
totalPages: number;
hasNext: boolean;
hasPrev: boolean;
};
};
};
status: number;
};
/**
* Extract pagination parameters from request context
* @param c - Hono context
* @returns Pagination parameters with defaults
*/
protected getPaginationParams(c: any): {
page: number;
limit: number;
skip: number;
};
/**
* Handle file upload validation
* @param file - Uploaded file
* @param allowedTypes - Array of allowed MIME types
* @param maxSize - Maximum file size in bytes
* @throws Error if validation fails
*/
protected validateFile(file: File, allowedTypes: string[], maxSize: number): void;
/**
* Handle multipart form data extraction
* @param c - Hono context
* @returns Promise with form data object
*/
protected extractFormData(c: any): Promise<{
[key: string]: any;
}>;
/**
* Extract user information from authenticated context
* @param c - Hono context
* @returns User information or null if not authenticated
*/
protected getAuthenticatedUser(c: any): any | null;
/**
* Check if user has required role
* @param c - Hono context
* @param requiredRoles - Array of required roles
* @returns Boolean indicating if user has required role
*/
protected hasRole(c: any, requiredRoles: string[]): boolean;
/**
* Create a simplified POST route with automatic OpenAPI configuration
* @param path - Route path
* @param schema - Request and response schemas
* @param handler - Route handler function
* @param options - Additional options
*/
protected createPostRoute<TRequest, TResponse>(path: string, schema: {
request: z.ZodSchema<TRequest>;
response: z.ZodSchema<TResponse>;
tag?: string;
summary?: string;
description?: string;
}, handler: (c: any, body: TRequest) => Promise<any>, options?: {
security?: boolean;
multipart?: boolean;
statusCode?: number;
}): void;
/**
* Create a simplified GET route for listing resources with pagination
* @param path - Route path
* @param schema - Response schema
* @param handler - Route handler function
* @param options - Additional options
*/
protected createListRoute<TResponse>(path: string, schema: {
response: z.ZodSchema<TResponse>;
tag?: string;
summary?: string;
description?: string;
}, handler: (c: any, query: {
page: number;
limit: number;
search?: string;
sort?: string;
}) => Promise<any>, options?: {
security?: boolean;
}): void;
/**
* Create a simplified GET route for single resource
* @param path - Route path (should include {id} parameter)
* @param schema - Response schema
* @param handler - Route handler function
* @param options - Additional options
*/
protected createGetByIdRoute<TResponse>(path: string, schema: {
response: z.ZodSchema<TResponse>;
tag?: string;
summary?: string;
description?: string;
}, handler: (c: any, id: string) => Promise<any>, options?: {
security?: boolean;
}): void;
/**
* Create a simplified PUT route
* @param path - Route path (should include {id} parameter)
* @param schema - Request and response schemas
* @param handler - Route handler function
* @param options - Additional options
*/
protected createPutRoute<TRequest, TResponse>(path: string, schema: {
request: z.ZodSchema<TRequest>;
response: z.ZodSchema<TResponse>;
tag?: string;
summary?: string;
description?: string;
}, handler: (c: any, id: string, body: TRequest) => Promise<any>, options?: {
security?: boolean;
multipart?: boolean;
}): void;
/**
* Create a simplified DELETE route
* @param path - Route path (should include {id} parameter)
* @param schema - Optional configuration
* @param handler - Route handler function
* @param options - Additional options
*/
protected createDeleteRoute(path: string, schema: {
tag?: string;
summary?: string;
description?: string;
}, handler: (c: any, id: string) => Promise<any>, options?: {
security?: boolean;
}): void;
/**
* Get default tag for routes (can be overridden in child classes)
*/
protected getDefaultTag(): string;
/**
* Get resource name for documentation (can be overridden in child classes)
*/
protected getResourceName(): string;
/**
* Get standardized error response schema
*/
private getErrorResponse;
}
//# sourceMappingURL=base-controller.d.ts.map