Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

1.1 KB
Raw Blame
/* eslint-disable no-param-reassign */
import fp from "fastify-plugin";

import { getConfig } from "@app/lib/config/env";

import { DefaultResponseErrorsSchema } from "../routes/sanitizedSchemas";

const isScimRoutes = (pathname: string) =>
  pathname.startsWith("/api/v1/scim/Users") || pathname.startsWith("/api/v1/scim/Groups");

const isAcmeRoutes = (pathname: string) => pathname.startsWith("/api/v1/cert-manager/acme/");

const isOauthTokenRoute = (pathname: string) => pathname === "/api/v1/oauth/token";

export const registerResponseSchemaHooks = fp(async (server) => {
  const appCfg = getConfig();

  server.addHook("onRoute", (routeOptions) => {
    if (appCfg.isDevelopmentMode && routeOptions.schema) {
      routeOptions.schema.hide = false;
    }

    if (
      routeOptions.schema &&
      routeOptions.schema.response &&
      !isScimRoutes(routeOptions.path) &&
      !isAcmeRoutes(routeOptions.path) &&
      !isOauthTokenRoute(routeOptions.path)
    ) {
      routeOptions.schema.response = {
        ...DefaultResponseErrorsSchema,
        ...routeOptions.schema.response
      };
    }
  });
});