All files / src/routes stats.ts

72.72% Statements 16/22
100% Branches 1/1
50% Functions 1/2
72.72% Lines 16/22

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 351x                 1x 1x 1x 1x 1x 1x     1x 39x   39x         39x 39x 39x 39x       39x 39x  
import type { FastifyPluginAsync, FastifyRequest } from "fastify";
import type { AppConfig } from "../config.js";
import {
  catalogStatsProperties,
  catalogStatsRequired,
  computeCatalogStats,
} from "../lib/catalog-stats.js";
import { assertServiceToken } from "../lib/service-token.js";
 
const statsResponseSchema = {
  type: "object",
  required: [...catalogStatsRequired],
  additionalProperties: false,
  properties: { ...catalogStatsProperties },
} as const;
 
/** Публичная статистика каталога (открытые данные) — для дашборда всем пользователям. */
export const statsRoutes: FastifyPluginAsync<{ config: AppConfig }> = async (app, opts) => {
  const { config } = opts;
 
  function requireServiceToken(request: FastifyRequest): void {
    const token = request.headers["x-service-token"];
    assertServiceToken(Array.isArray(token) ? token[0] : token, config.serviceToken);
  }
 
  app.get(
    "/stats",
    { schema: { response: { 200: statsResponseSchema } } },
    async (request, reply) => {
      requireServiceToken(request);
      return reply.send(await computeCatalogStats());
    },
  );
};