Ensure consistent patterns for API response handling, error management, and type safety across all API interactions. This includes standardizing error response structures based on status codes, properly defining response types to match actual API behavior, and implementing robust error handling with appropriate fallbacks.
Ensure consistent patterns for API response handling, error management, and type safety across all API interactions. This includes standardizing error response structures based on status codes, properly defining response types to match actual API behavior, and implementing robust error handling with appropriate fallbacks.
Key principles:
Example of consistent error handling:
if (res.statusCode === 400) {
return reject({
code: res.statusCode,
body: JSON.parse(body as any), // JSON response for user feedback
});
} else if (res.statusCode >= 401) {
return reject({
code: res.statusCode,
// No body parsing for non-JSON responses
});
}
Example of proper response type definition:
interface ApiResponse {
remediation: string | { resolve: string }; // Union type reflecting actual API behavior
target?: GitTarget | ContainerTarget | {}; // Optional with fallback
}
This ensures APIs are predictable, type-safe, and handle edge cases gracefully while maintaining consistency across different endpoints and response scenarios.
Enter the URL of a public GitHub repository