feat: complete development environment setup and code quality improvements

- Set up pre-commit hooks with husky and lint-staged for automated code quality
- Improved TypeScript type safety by replacing 'any' types with proper generics
- Fixed markdown linting violations (MD030 spacing) across all documentation
- Fixed compound adjective hyphenation in technical documentation
- Fixed invalid JSON union syntax in API documentation examples
- Automated code formatting and linting on commit
- Enhanced error handling with better type constraints
- Configured biome and markdownlint for consistent code style
- All changes verified with successful production build
This commit is contained in:
2025-07-13 14:44:05 +02:00
parent 1d4e695e41
commit e2301725a3
54 changed files with 2335 additions and 1863 deletions

View File

@@ -17,7 +17,11 @@ export class APIError extends Error {
message: string,
public readonly statusCode: number = 500,
public readonly code: string = "INTERNAL_ERROR",
public readonly details?: any,
public readonly details?:
| Record<string, unknown>
| string[]
| string
| number,
public readonly logLevel: "info" | "warn" | "error" = "error"
) {
super(message);
@@ -100,7 +104,10 @@ export class ConflictError extends APIError {
* Database Error - for database operation failures
*/
export class DatabaseError extends APIError {
constructor(message = "Database operation failed", details?: any) {
constructor(
message = "Database operation failed",
details?: Record<string, unknown> | string
) {
super(message, 500, "DATABASE_ERROR", details, "error");
}
}
@@ -112,7 +119,7 @@ export class ExternalServiceError extends APIError {
constructor(
service: string,
message = "External service error",
details?: any
details?: Record<string, unknown>
) {
super(
`${service} service error: ${message}`,
@@ -138,7 +145,11 @@ function shouldExposeError(error: unknown): boolean {
/**
* Log error with appropriate level
*/
function logError(error: unknown, requestId: string, context?: any): void {
function logError(
error: unknown,
requestId: string,
context?: Record<string, unknown>
): void {
const logData = {
requestId,
error: error instanceof Error ? error.message : String(error),
@@ -170,7 +181,7 @@ function logError(error: unknown, requestId: string, context?: any): void {
export function handleAPIError(
error: unknown,
requestId?: string,
context?: any
context?: Record<string, unknown>
): NextResponse {
const id = requestId || crypto.randomUUID();
@@ -220,7 +231,7 @@ export function handleAPIError(
/**
* Async error handler for promise chains
*/
export function asyncErrorHandler<T extends any[], R>(
export function asyncErrorHandler<T extends readonly unknown[], R>(
fn: (...args: T) => Promise<R>
) {
return async (...args: T): Promise<R> => {
@@ -237,7 +248,7 @@ export function asyncErrorHandler<T extends any[], R>(
/**
* Error boundary for API route handlers
*/
export function withErrorHandling<T extends any[], R>(
export function withErrorHandling<T extends readonly unknown[], R>(
handler: (...args: T) => Promise<NextResponse> | NextResponse
) {
return async (...args: T): Promise<NextResponse> => {

View File

@@ -91,10 +91,10 @@ export interface APIHandlerOptions {
/**
* API handler function type
*/
export type APIHandler<T = any> = (
export type APIHandler<T = unknown> = (
context: APIContext,
validatedData?: any,
validatedQuery?: any
validatedData?: unknown,
validatedQuery?: unknown
) => Promise<T>;
/**
@@ -226,7 +226,7 @@ async function validateInput<T>(
if (error instanceof SyntaxError) {
throw new ValidationError(["Invalid JSON in request body"]);
}
throw new ValidationError(error as any);
throw new ValidationError(error as z.ZodError);
}
}
@@ -239,7 +239,7 @@ function validateQuery<T>(request: NextRequest, schema: z.ZodSchema<T>): T {
const query = Object.fromEntries(searchParams.entries());
return schema.parse(query);
} catch (error) {
throw new ValidationError(error as any);
throw new ValidationError(error as z.ZodError);
}
}
@@ -285,7 +285,7 @@ function addCORSHeaders(
/**
* Main API handler factory
*/
export function createAPIHandler<T = any>(
export function createAPIHandler<T = unknown>(
handler: APIHandler<T>,
options: APIHandlerOptions = {}
) {
@@ -368,7 +368,7 @@ export function createAPIHandler<T = any>(
/**
* Utility function for GET endpoints
*/
export function createGETHandler<T = any>(
export function createGETHandler<T = unknown>(
handler: APIHandler<T>,
options: Omit<APIHandlerOptions, "validateInput"> = {}
) {
@@ -381,7 +381,7 @@ export function createGETHandler<T = any>(
/**
* Utility function for POST endpoints
*/
export function createPOSTHandler<T = any>(
export function createPOSTHandler<T = unknown>(
handler: APIHandler<T>,
options: APIHandlerOptions = {}
) {
@@ -394,7 +394,7 @@ export function createPOSTHandler<T = any>(
/**
* Utility function for authenticated endpoints
*/
export function createAuthenticatedHandler<T = any>(
export function createAuthenticatedHandler<T = unknown>(
handler: APIHandler<T>,
options: APIHandlerOptions = {}
) {
@@ -408,7 +408,7 @@ export function createAuthenticatedHandler<T = any>(
/**
* Utility function for admin endpoints
*/
export function createAdminHandler<T = any>(
export function createAdminHandler<T = unknown>(
handler: APIHandler<T>,
options: APIHandlerOptions = {}
) {

View File

@@ -58,10 +58,11 @@ export {
UserRole,
} from "./handler";
import { createPermissionChecker, type Permission } from "./authorization";
// Re-import types for use in functions below
import type { APIContext, APIHandler, APIHandlerOptions } from "./handler";
import { createAPIHandler } from "./handler";
import { Permission, createPermissionChecker } from "./authorization";
// Response utilities
export {
type APIResponse,

View File

@@ -19,7 +19,7 @@ export interface APIResponseMeta {
version?: string;
}
export interface APIResponse<T = any> {
export interface APIResponse<T = unknown> {
success: boolean;
data?: T;
error?: string;