Skip to content

A modern and easy to use dashboard. 80+ integrations. 20K+ icons built in. Authentication out of the box. No YAML, drag and drop configuration.

1.3 KB
Raw Blame
import { z } from "zod/v4";

import type { StatsAuthenticationContext, StatsProvider } from "../types";

const mealieStatsResponseSchema = z
  .object({
    totalRecipes: z.number().finite().int().nonnegative(),
    totalUsers: z.number().finite().int().nonnegative(),
    totalCategories: z.number().finite().int().nonnegative(),
    totalTags: z.number().finite().int().nonnegative(),
  })
  .passthrough();

const getHttpAuthentication = (context: StatsAuthenticationContext) => ({
  headers: { Authorization: `Bearer ${context.secret("apiKey")}` },
});

export const mealieStatsProvider = {
  getHttpAuthentication,
  metrics: [
    { key: "recipes", label: "Recipes", unit: "count" },
    { key: "users", label: "Users", unit: "count" },
    { key: "categories", label: "Categories", unit: "count" },
    { key: "tags", label: "Tags", unit: "count" },
  ],

  async fetchAsync(context) {
    const response = await context.requestAsync("/api/households/statistics", {
      headers: getHttpAuthentication(context).headers,
      signal: context.signal,
    });
    const stats = mealieStatsResponseSchema.parse(response);

    return {
      recipes: stats.totalRecipes,
      users: stats.totalUsers,
      categories: stats.totalCategories,
      tags: stats.totalTags,
    };
  },
} satisfies StatsProvider;