.ts
TypeScript
(application/typescript)
// 3rd-party
import fastify from "fastify";
// generated via script[generate:prisma]
import { PrismaClient } from "@prisma/client";
// app
import type {
  AppSessionData,
  AppThemeScheme,
  SectionsWithPages,
} from "../../app/types";
import type { CryptoServiceAPI } from "../../app/services/crypto/types";

declare module "@ethicdevs/fastify-custom-session" {
  declare interface CustomSession extends AppSessionData {}
}

declare module "fastify" {
  interface FastifyInstance {
    // from crypto plugin
    cryptoService: CryptoServiceAPI;
    // from prisma plugin
    prisma: PrismaClient;
  }
  interface FastifyRequest {
    // from crypto plugin
    cryptoService: CryptoServiceAPI;
    // from prisma plugin
    prisma: PrismaClient;
    // from cookies
    cookies: {
      theme_scheme: AppThemeScheme;
    };
    // from app
    sectionsWithPages: SectionsWithPages;
    // from react-monolith
    // A request utility that maps a viewName to its routerPath
    namedViewsPathMap: Record<string, string>;
    // A request utility that maps a routerPath to its viewName
    pathNamedViewsMap: Record<string, string>;
  }

  interface FastifyReply {
    makeRequestHandler: (
      request: FastifyRequest,
      reply: FastifyReply
    ) => <T extends Record<string, any>>(
      viewName: string,
      props: T & CommonViewProps,
      viewCtx?: ViewContext
    ) => Promise<
      FastifyReply<
        Server,
        IncomingMessage,
        ServerResponse,
        RouteGenericInterface,
        unknown
      >
    >;
  }
}