Public/Get-DuneActionDefinition.ps1

<#
.SYNOPSIS
Retrieve an action's definition body as YAML or JSON.

.DESCRIPTION
Calls `GET /actions/{Id}/definition` and returns the `Definition` field from the response. Defaults to YAML output; use `-Format Json` to get the JSON representation.

.PARAMETER Id
The GUID of the action. Uses the `Id` parameter set.

.PARAMETER Action
A `DuneAction` object; returns the definition of the supplied action (pipeline input supported).

.PARAMETER Format
Requested definition format. `Yaml` (default) or `Json`.

.PARAMETER IncludeReferencedObjects
If set, forwards `IncludeReferencedObjects=1` to the API.

.PARAMETER IncludeDeleted
If set, forwards `IncludeDeleted=1` to the API (retrieves the definition even if the action is soft-deleted).

.EXAMPLE
PS> Get-DuneActionDefinition -Id 3d8f6b5a-...
Returns the action's definition as a YAML string.

.EXAMPLE
PS> Get-DuneAction -Name "backup-db" | Get-DuneActionDefinition -Format Json
Pipeline example; returns the definition as a JSON string.
#>

function Get-DuneActionDefinition {
    [CmdletBinding(DefaultParameterSetName = 'Id')]
    param (
        [Parameter(ParameterSetName = 'Id', Mandatory, Position = 0)]
        [guid]$Id,

        [Parameter(ParameterSetName = 'Action', Mandatory, ValueFromPipeline)]
        [DuneAction]$Action,

        [Parameter()]
        [ValidateSet('Yaml', 'Json')]
        [string]$Format = 'Yaml',

        [Parameter()]
        [switch]$IncludeReferencedObjects,

        [Parameter()]
        [switch]$IncludeDeleted
    )

    begin {
        Write-Debug "$($MyInvocation.MyCommand)|begin"
        $BaseUri = 'actions'
        $Method  = 'GET'
    }

    process {
        Write-Debug "$($MyInvocation.MyCommand)|process|$($PSCmdlet.ParameterSetName)"

        $Uri = switch ($PSCmdlet.ParameterSetName) {
            'Id'     { '{0}/{1}/definition' -f $BaseUri, $Id }
            'Action' { '{0}/{1}/definition' -f $BaseUri, $Action.Id }
        }

        $Uri = $Uri | Add-UriQueryParam "DefinitionFormat=$Format"
        if ($IncludeReferencedObjects) { $Uri = $Uri | Add-UriQueryParam 'IncludeReferencedObjects=1' }
        if ($IncludeDeleted)           { $Uri = $Uri | Add-UriQueryParam 'IncludeDeleted=1' }

        $Response = Invoke-DuneApiRequest -Uri $Uri -Method $Method
        $Dto      = $Response.Content | ConvertFrom-Json

        $Dto.Definition
    }

    end {
        Write-Debug "$($MyInvocation.MyCommand)|end"
    }
}