import { app, HttpRequest, HttpResponseInit, InvocationContext } from "@azure/functions";
import { products, orders } from "../shared/data.js";
interface HealthStatus {
status: "healthy" | "degraded" | "unhealthy";
timestamp: string;
version: string;
checks: {
name: string;
status: "pass" | "fail";
message?: string;
}[];
stats: {
productsCount: number;
ordersCount: number;
categories: string[];
};
}
/**
* GET /api/health - Health check endpoint
* Returns the health status of the API
*/
export async function health(request: HttpRequest, context: InvocationContext): Promise<HttpResponseInit> {
context.log(`health function processed a request`);
const categories = [...new Set(products.map(p => p.category))];
const healthStatus: HealthStatus = {
status: "healthy",
timestamp: new Date().toISOString(),
version: "1.0.0",
checks: [
{
name: "products-data",
status: products.length > 0 ? "pass" : "fail",
message: products.length > 0 ? `${products.length} products available` : "No products loaded"
},
{
name: "orders-store",
status: "pass",
message: `${orders.length} orders in memory`
}
],
stats: {
productsCount: products.length,
ordersCount: orders.length,
categories
}
};
// Determine overall status
const hasFailures = healthStatus.checks.some(c => c.status === "fail");
if (hasFailures) {
healthStatus.status = "degraded";
}
return {
status: healthStatus.status === "healthy" ? 200 : 503,
jsonBody: healthStatus
};
}
app.http("health", {
methods: ["GET"],
authLevel: "anonymous",
route: "health",
handler: health
});