@accelerated-software-development/caddy-api-client - v0.4.3
    Preparing search index...

    Class CaddyClient

    Client for interacting with Caddy Admin API

    Index

    Constructors

    Methods

    • Adapt a configuration from one format to another Commonly used to convert Caddyfile to JSON configuration.

      Parameters

      • config: string

        The configuration content to adapt

      • adapter: string = "caddyfile"

        The adapter to use (e.g., "caddyfile", "json", "yaml", "nginx", "apache")

      Returns Promise<Config & Record<string, unknown>>

      The adapted configuration as validated JSON

      If config or adapter parameters are invalid

      If Caddy cannot parse the configuration

      If unable to connect to Caddy Admin API

      If the request times out

    • Add a route to a server

      Parameters

      • server: string

        Server name

      • route: CaddyRoute

        Route configuration

      Returns Promise<boolean>

      True if route was added, false if already exists

      If route configuration is invalid

      If Caddy API returns an error response

      If unable to connect to Caddy Admin API

      If the request times out

    • Add multiple routes to a server (convenience method to avoid loops) This method adds routes one at a time and handles idempotency for each.

      Performance: Fetches existing routes once and caches them to avoid N+1 API calls.

      Parameters

      • server: string

        Server name

      • routes: CaddyRoute[]

        Array of routes to add

      Returns Promise<{ added: number; skipped: number }>

      Object with counts of added and skipped routes

      If any route configuration is invalid

      If Caddy API returns an error response

      If unable to connect to Caddy Admin API

      If the request times out

      const routes = buildServiceRoutes({ host: "api.localhost", dial: "localhost:3000" });
      const result = await client.addRoutes("https_server", routes);
      console.log(`Added ${result.added}, skipped ${result.skipped}`);
    • Apply a full configuration to Caddy

      Replaces the entire running Caddy configuration with the provided config object. This is the primary method for applying loaded or modified configurations.

      Parameters

      • config: Config | Config & Record<string, unknown>

        Full Caddy configuration object

      Returns Promise<void>

      If config is invalid

      If Caddy rejects the configuration

      If unable to connect to Caddy Admin API

      If the request times out

      // Load, modify, and apply configuration
      const config = await loadCaddyfile("./Caddyfile");

      // Modify the config
      config.apps.http.servers.srv0.routes.push(
      buildHostRoute({ host: "api.example.com", dial: "localhost:3000" })
      );

      // Apply to running Caddy
      await client.applyConfig(config);
    • Get current Caddy configuration

      Returns Promise<Config & Record<string, unknown>>

      Full Caddy configuration as JSON, validated against Caddy schema

      If Caddy API returns an error response

      If unable to connect to Caddy Admin API

      If the request times out

    • Get routes for a specific server

      Parameters

      • server: string

        Server name (e.g., "https_server")

      Returns Promise<CaddyRoute[]>

      Array of routes, validated against Caddy route schema

      If Caddy API returns an error response

      If unable to connect to Caddy Admin API

      If the request times out

    • Get configuration for a specific server

      Parameters

      • server: string

        Server name

      Returns Promise<Server & Record<string, unknown>>

      Server configuration object, validated against Caddy schema

      If Caddy API returns an error response

      If unable to connect to Caddy Admin API

      If the request times out

    • Get information about all servers

      Returns Promise<Record<string, Server & Record<string, unknown>>>

      Server configurations, validated against Caddy schema

      If Caddy API returns an error response

      If unable to connect to Caddy Admin API

      If the request times out

    • Get reverse proxy upstream status Returns the current state of all configured upstream servers.

      Returns Promise<UpstreamStatus[]>

      Array of upstream server status objects, validated

      If Caddy API returns an error response

      If unable to connect to Caddy Admin API

      If the request times out

    • Get Caddy version information

      Returns Promise<{ version?: string } & Record<string, unknown>>

      Version information object with version string and additional metadata

    • Insert a route at a specific position in the server's route list

      Parameters

      • server: string

        Server name

      • route: CaddyRoute

        Route to insert

      • position: "beginning" | "end" | "after-health-checks" = "after-health-checks"

        Where to insert the route

      Returns Promise<void>

      void

    • Replace all routes for a server (PATCH)

      Parameters

      • server: string

        Server name

      • routes: CaddyRoute[]

        Array of routes

      Returns Promise<void>

      If any route configuration is invalid

      If Caddy API returns an error response

      If unable to connect to Caddy Admin API

      If the request times out

    • Patch server configuration

      Parameters

      • serverConfig: Record<string, Partial<Server> | Record<string, unknown>>

        Server configuration object (server name -> server config)

      Returns Promise<void>

    • Remove a route by its

      Parameters

      • server: string

        Server name

      • id: string

        Route

      Returns Promise<boolean>

      true if route was found and removed, false otherwise

      to remove

    • Remove routes matching a hostname

      Parameters

      • hostname: string

        Hostname to match

      • server: string = "https_server"

        Server name

      Returns Promise<number>

      Number of routes removed

    • Replace a route by its

      Parameters

      • server: string

        Server name

      • id: string

        Route

      • newRoute: CaddyRoute

        New route configuration

      Returns Promise<boolean>

      true if route was found and replaced, false otherwise

      to replace

    • Make an HTTP request to Caddy Admin API with timeout

      Parameters

      • path: string

        API endpoint path

      • options: RequestInit = {}

        Fetch options

      Returns Promise<Response>

      Response object

      NetworkError, TimeoutError, CaddyApiError

    • Gracefully stop the Caddy server This triggers a graceful shutdown, allowing active connections to complete.

      Returns Promise<void>

      If Caddy API returns an error response

      If unable to connect to Caddy Admin API

      If the request times out