mirror of
https://github.com/kjanat/livedash-node.git
synced 2026-01-16 13:52:16 +01:00
- 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
118 lines
2.1 KiB
TypeScript
118 lines
2.1 KiB
TypeScript
/**
|
|
* Standardized API Response System
|
|
*
|
|
* Provides consistent response formatting across all API endpoints
|
|
* with proper typing, error handling, and metadata support.
|
|
*/
|
|
|
|
export interface PaginationMeta {
|
|
page: number;
|
|
limit: number;
|
|
total: number;
|
|
totalPages: number;
|
|
}
|
|
|
|
export interface APIResponseMeta {
|
|
timestamp: string;
|
|
requestId: string;
|
|
pagination?: PaginationMeta;
|
|
version?: string;
|
|
}
|
|
|
|
export interface APIResponse<T = unknown> {
|
|
success: boolean;
|
|
data?: T;
|
|
error?: string;
|
|
errors?: string[];
|
|
meta: APIResponseMeta;
|
|
}
|
|
|
|
/**
|
|
* Create a successful API response
|
|
*/
|
|
export function createSuccessResponse<T>(
|
|
data: T,
|
|
meta?: Partial<APIResponseMeta>
|
|
): APIResponse<T> {
|
|
return {
|
|
success: true,
|
|
data,
|
|
meta: {
|
|
timestamp: new Date().toISOString(),
|
|
requestId: crypto.randomUUID(),
|
|
version: "1.0",
|
|
...meta,
|
|
},
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Create an error API response
|
|
*/
|
|
export function createErrorResponse(
|
|
error: string,
|
|
errors?: string[],
|
|
meta?: Partial<APIResponseMeta>
|
|
): APIResponse {
|
|
return {
|
|
success: false,
|
|
error,
|
|
errors,
|
|
meta: {
|
|
timestamp: new Date().toISOString(),
|
|
requestId: crypto.randomUUID(),
|
|
version: "1.0",
|
|
...meta,
|
|
},
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Create a paginated success response
|
|
*/
|
|
export function createPaginatedResponse<T>(
|
|
data: T[],
|
|
pagination: PaginationMeta,
|
|
meta?: Partial<APIResponseMeta>
|
|
): APIResponse<T[]> {
|
|
return createSuccessResponse(data, {
|
|
...meta,
|
|
pagination,
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Extract pagination parameters from request
|
|
*/
|
|
export function extractPaginationParams(searchParams: URLSearchParams): {
|
|
page: number;
|
|
limit: number;
|
|
} {
|
|
const page = Math.max(
|
|
1,
|
|
Number.parseInt(searchParams.get("page") || "1", 10)
|
|
);
|
|
const limit = Math.min(
|
|
100,
|
|
Math.max(1, Number.parseInt(searchParams.get("limit") || "20", 10))
|
|
);
|
|
|
|
return { page, limit };
|
|
}
|
|
|
|
/**
|
|
* Calculate pagination metadata
|
|
*/
|
|
export function calculatePaginationMeta(
|
|
page: number,
|
|
limit: number,
|
|
total: number
|
|
): PaginationMeta {
|
|
return {
|
|
page,
|
|
limit,
|
|
total,
|
|
totalPages: Math.ceil(total / limit),
|
|
};
|
|
}
|