Private/Get-GhostAPIData.ps1

function Get-GhostAPIData {
    <#
    .SYNOPSIS
        Returns the API definition for a public function.

    .DESCRIPTION
        Every public function that calls the Ghost Admin API has an entry here, keyed by the function name. The
        entry describes the endpoint as data, so the function itself only has to pass its parameters through the
        shared helpers (New-URIString, New-QueryString, New-BodyString, Submit-Request, Test-ReturnFormat).

        An entry holds one definition per Ghost version in which the endpoint changed. The definition with the
        highest version that is not newer than the connected site is returned.

        Keys of a definition:

        Description What the call does. Shown by -WhatIf and -Confirm.
        URI Path below /ghost/api/admin. Tokens such as {id} are replaced by the parameter with that
                     name. Use a hashtable keyed by parameter set name when the path depends on the parameter set.
        Method Get, Post, Put or Delete.
        Body Parameter name = name of the field in the request body.
        Query Parameter name = name of the query string parameter.
        Result Name of the property that wraps the resource in requests and responses, such as 'posts'.
        TypeName Type name given to the returned objects; ObjectDefinitions holds the matching format file.
        Success The HTTP status code of a successful call.

    .EXAMPLE
        Get-GhostAPIData -Endpoint 'Get-GhostPost'

        Returns the definition of the posts endpoint for the connected site.
    #>

    [CmdletBinding()]
    [OutputType([hashtable])]
    param(
        # Name of the public function to return the definition for.
        [Parameter(Mandatory)]
        [string] $Endpoint,

        # Ghost version to resolve the definition for. Defaults to the version of the connected site.
        [version] $Version
    )

    # Fields of a post that can be written, shared by the endpoints that create and update posts.
    $postFields = @{
        Title               = 'title'
        Lexical             = 'lexical'
        Html                = 'html'
        Slug                = 'slug'
        Status              = 'status'
        Visibility          = 'visibility'
        Featured            = 'featured'
        Tags                = 'tags'
        Authors             = 'authors'
        FeatureImage        = 'feature_image'
        FeatureImageAlt     = 'feature_image_alt'
        FeatureImageCaption = 'feature_image_caption'
        CustomExcerpt       = 'custom_excerpt'
        PublishedAt         = 'published_at'
        CanonicalUrl        = 'canonical_url'
        MetaTitle           = 'meta_title'
        MetaDescription     = 'meta_description'
        OgImage             = 'og_image'
        OgTitle             = 'og_title'
        OgDescription       = 'og_description'
        TwitterImage        = 'twitter_image'
        TwitterTitle        = 'twitter_title'
        TwitterDescription  = 'twitter_description'
        CodeInjectionHead   = 'codeinjection_head'
        CodeInjectionFoot   = 'codeinjection_foot'
        CustomTemplate      = 'custom_template'
    }

    # Fields of a tag that can be written, shared by the endpoints that create and update tags.
    $tagFields = @{
        Name               = 'name'
        Slug               = 'slug'
        Description        = 'description'
        FeatureImage       = 'feature_image'
        Visibility         = 'visibility'
        AccentColor        = 'accent_color'
        CanonicalUrl       = 'canonical_url'
        MetaTitle          = 'meta_title'
        MetaDescription    = 'meta_description'
        OgImage            = 'og_image'
        OgTitle            = 'og_title'
        OgDescription      = 'og_description'
        TwitterImage       = 'twitter_image'
        TwitterTitle       = 'twitter_title'
        TwitterDescription = 'twitter_description'
        CodeInjectionHead  = 'codeinjection_head'
        CodeInjectionFoot  = 'codeinjection_foot'
    }

    $api = @{
        'Connect-Ghost'       = @{
            '5.0' = @{
                Description = 'Read the public information of the site'
                URI         = '/site/'
                Method      = 'Get'
                Body        = @{}
                Query       = @{}
                Result      = 'site'
                TypeName    = ''
                Success     = 200
            }
        }
        'Copy-GhostPost'      = @{
            '5.0' = @{
                Description = 'Copy a post to a new draft'
                URI         = '/posts/{id}/copy/'
                Method      = 'Post'
                Body        = @{}
                Query       = @{}
                Result      = 'posts'
                TypeName    = 'Ghost.Post'
                Success     = 201
            }
        }
        'Enable-GhostTheme'   = @{
            '5.0' = @{
                Description = 'Activate a theme'
                URI         = '/themes/{name}/activate/'
                Method      = 'Put'
                Body        = @{}
                Query       = @{}
                Result      = 'themes'
                TypeName    = 'Ghost.Theme'
                Success     = 200
            }
        }
        'Export-GhostContent' = @{
            '5.0' = @{
                Description = 'Export the content of the site'
                URI         = '/db/'
                Method      = 'Get'
                Body        = @{}
                Query       = @{}
                Result      = ''
                TypeName    = ''
                Success     = 200
            }
        }
        'Get-GhostPost'       = @{
            '5.0' = @{
                Description = 'Browse posts or read a single post'
                URI         = @{
                    Browse = '/posts/'
                    Id     = '/posts/{id}/'
                    Slug   = '/posts/slug/{slug}/'
                }
                Method      = 'Get'
                Body        = @{}
                Query       = @{
                    Include = 'include'
                    Formats = 'formats'
                    Fields  = 'fields'
                    Filter  = 'filter'
                    Order   = 'order'
                    Limit   = 'limit'
                    Page    = 'page'
                }
                Result      = 'posts'
                TypeName    = 'Ghost.Post'
                Success     = 200
            }
        }
        'Get-GhostTag'        = @{
            '5.0' = @{
                Description = 'Browse tags or read a single tag'
                URI         = @{
                    Browse = '/tags/'
                    Id     = '/tags/{id}/'
                    Slug   = '/tags/slug/{slug}/'
                }
                Method      = 'Get'
                Body        = @{}
                Query       = @{
                    Include = 'include'
                    Fields  = 'fields'
                    Filter  = 'filter'
                    Order   = 'order'
                    Limit   = 'limit'
                    Page    = 'page'
                }
                Result      = 'tags'
                TypeName    = 'Ghost.Tag'
                Success     = 200
            }
        }
        'Import-GhostContent' = @{
            '5.0' = @{
                Description = 'Import the content of a file'
                URI         = '/db/'
                Method      = 'Post'
                Body        = @{}
                Query       = @{}
                Result      = ''
                TypeName    = ''
                Success     = 200
            }
        }
        'New-GhostImage'      = @{
            '5.0' = @{
                Description = 'Upload an image'
                URI         = '/images/upload/'
                Method      = 'Post'
                Body        = @{
                    Purpose   = 'purpose'
                    Reference = 'ref'
                }
                Query       = @{}
                Result      = 'images'
                TypeName    = 'Ghost.Image'
                Success     = 201
            }
        }
        'New-GhostPost'       = @{
            '5.0' = @{
                Description = 'Create a post'
                URI         = '/posts/'
                Method      = 'Post'
                Body        = $postFields
                Query       = @{
                    Source = 'source'
                }
                Result      = 'posts'
                TypeName    = 'Ghost.Post'
                Success     = 201
            }
        }
        'New-GhostTag'        = @{
            '5.0' = @{
                Description = 'Create a tag'
                URI         = '/tags/'
                Method      = 'Post'
                Body        = $tagFields
                Query       = @{}
                Result      = 'tags'
                TypeName    = 'Ghost.Tag'
                Success     = 201
            }
        }
        'New-GhostTheme'      = @{
            '5.0' = @{
                Description = 'Upload a theme'
                URI         = '/themes/upload/'
                Method      = 'Post'
                Body        = @{}
                Query       = @{}
                Result      = 'themes'
                TypeName    = 'Ghost.Theme'
                Success     = 200
            }
        }
        'Publish-GhostPost'   = @{
            '5.0' = @{
                Description = 'Publish or schedule a post'
                URI         = '/posts/{id}/'
                Method      = 'Put'
                Body        = @{
                    Status      = 'status'
                    PublishedAt = 'published_at'
                    EmailOnly   = 'email_only'
                    UpdatedAt   = 'updated_at'
                }
                Query       = @{
                    Newsletter   = 'newsletter'
                    EmailSegment = 'email_segment'
                }
                Result      = 'posts'
                TypeName    = 'Ghost.Post'
                Success     = 200
            }
        }
        'Remove-GhostPost'    = @{
            '5.0' = @{
                Description = 'Delete a post'
                URI         = '/posts/{id}/'
                Method      = 'Delete'
                Body        = @{}
                Query       = @{}
                Result      = ''
                TypeName    = ''
                Success     = 204
            }
        }
        'Remove-GhostTag'     = @{
            '5.0' = @{
                Description = 'Delete a tag'
                URI         = '/tags/{id}/'
                Method      = 'Delete'
                Body        = @{}
                Query       = @{}
                Result      = ''
                TypeName    = ''
                Success     = 204
            }
        }
        'Set-GhostPost'       = @{
            '5.0' = @{
                Description = 'Update a post'
                URI         = '/posts/{id}/'
                Method      = 'Put'
                Body        = $postFields + @{
                    UpdatedAt = 'updated_at'
                }
                Query       = @{
                    Source       = 'source'
                    SaveRevision = 'save_revision'
                }
                Result      = 'posts'
                TypeName    = 'Ghost.Post'
                Success     = 200
            }
        }
        'Set-GhostTag'        = @{
            '5.0' = @{
                Description = 'Update a tag'
                URI         = '/tags/{id}/'
                Method      = 'Put'
                Body        = $tagFields
                Query       = @{}
                Result      = 'tags'
                TypeName    = 'Ghost.Tag'
                Success     = 200
            }
        }
        'Unpublish-GhostPost' = @{
            '5.0' = @{
                Description = 'Revert a post to draft'
                URI         = '/posts/{id}/'
                Method      = 'Put'
                Body        = @{
                    Status    = 'status'
                    UpdatedAt = 'updated_at'
                }
                Query       = @{}
                Result      = 'posts'
                TypeName    = 'Ghost.Post'
                Success     = 200
            }
        }
    }

    if (-not $api.ContainsKey($Endpoint)) {
        throw "No API data is defined for '$Endpoint'. Add an entry to Get-GhostAPIData."
    }

    if (-not $PSBoundParameters.ContainsKey('Version') -and $script:GhostConnection) {
        $Version = $script:GhostConnection['Version']
    }

    $definition = $api[$Endpoint]
    $available = @($definition.Keys | ForEach-Object { [version] $_ } | Sort-Object -Descending)
    $selected = $available[0]
    if ($Version) {
        $selected = $available | Where-Object { $_ -le $Version } | Select-Object -First 1
        if (-not $selected) {
            throw "'$Endpoint' needs Ghost $($available[-1]) or later, but the connected site runs Ghost $Version."
        }
    }

    $resources = $definition[$selected.ToString()]
    $resources['Function'] = $Endpoint
    $resources
}