Private/Test-GhostConnection.ps1

function Test-GhostConnection {
    <#
    .SYNOPSIS
        Stops the calling function when there is no connection to a Ghost site.

    .DESCRIPTION
        Every public function that calls the API runs this first, so a missing connection is reported with the
        same clear message everywhere instead of a failing web request.

    .EXAMPLE
        Test-GhostConnection

        Throws when Connect-Ghost has not been run in this session.
    #>

    [CmdletBinding()]
    [OutputType([void])]
    param()

    if (-not $script:GhostConnection) {
        $exception = [System.InvalidOperationException]::new('Not connected to a Ghost site. Run Connect-Ghost first.')
        $record = [System.Management.Automation.ErrorRecord]::new(
            $exception,
            'GhostNotConnected',
            [System.Management.Automation.ErrorCategory]::ConnectionError,
            $null
        )
        $PSCmdlet.ThrowTerminatingError($record)
    }
}