PowerShellGallery.psm1

[CmdletBinding()]
param()

if ($PSVersionTable.PSVersion -lt '6.0') {
    [Diagnostics.CodeAnalysis.SuppressMessageAttribute(
        'PSAvoidAssignmentToAutomaticVariable', '', Justification = 'Compatibility with PowerShell 6.0 and newer.'
    )]
    $IsWindows = [System.Environment]::OSVersion.Platform -eq 'Win32NT'
}

$scriptName = 'PowerShellGallery'
Write-Verbose "[$scriptName] - Importing module"

#region - From [private]
Write-Verbose "[$scriptName] - [private] - Processing folder"

#region - From [private] - [Update-PSGalleryResourceListing]
Write-Verbose "[$scriptName] - [private] - [Update-PSGalleryResourceListing] - Importing"

function Update-PSGalleryResourceListing {
    <#
        .SYNOPSIS
        Updates the listing status of a module on the PowerShell Gallery.

        .EXAMPLE
        Update-PSGalleryResourceListing -Name 'MyModule' -Version '1.0.0' -Listed $true -APIKey 'myapikey'
    #>

    [CmdletBinding(SupportsShouldProcess)]
    param (
        # Name of the module.
        [Parameter(Mandatory)]
        [string] $Name,

        # Version of the module.
        [Parameter(Mandatory)]
        [string] $Version,

        # Whether the module is listed on the PowerShell Gallery.
        [Parameter(Mandatory)]
        [bool] $Listed,

        # API key for the PowerShell Gallery.
        [Parameter(Mandatory)]
        [string] $APIKey
    )

    $uri = "https://www.powershellgallery.com/packages/$Name/$Version/UpdateListed"

    $body = @{
        __RequestVerificationToken = $APIKey
        Version                    = $Version
        Listed                     = $Listed
    }

    if ($PSCmdlet.ShouldProcess("module [$Name]", 'Update listing status')) {
        Invoke-RestMethod -Uri $uri -Method Post -Body $body
    }
}

Write-Verbose "[$scriptName] - [private] - [Update-PSGalleryResourceListing] - Done"
#endregion - From [private] - [Update-PSGalleryResourceListing]

Write-Verbose "[$scriptName] - [private] - Done"
#endregion - From [private]

#region - From [public]
Write-Verbose "[$scriptName] - [public] - Processing folder"

#region - From [public] - [Get-PSGalleryAPI]
Write-Verbose "[$scriptName] - [public] - [Get-PSGalleryAPI] - Importing"

function Get-PSGalleryAPI {
    <#
        .SYNOPSIS
        Get the PowerShell Gallery API.
    #>

    [CmdletBinding()]
    param()

    Invoke-RestMethod -Method Get -Uri 'https://www.powershellgallery.com/api/v2/' -ContentType 'application/json'
}

Write-Verbose "[$scriptName] - [public] - [Get-PSGalleryAPI] - Done"
#endregion - From [public] - [Get-PSGalleryAPI]
#region - From [public] - [Get-PSGalleryResource]
Write-Verbose "[$scriptName] - [public] - [Get-PSGalleryResource] - Importing"

function Get-PSGalleryResource {
    <#
    .SYNOPSIS
    Get a resource from the PowerShell Gallery.

    .DESCRIPTION
    Long description

    .EXAMPLE
    Get-PSGalleryResource -Name 'MyModule' -Version '1.0.0'

    .NOTES
    General notes
    #>


    [CmdletBinding()]
    param (
        # Name of the module.
        [Parameter(Mandatory)]
        [string] $Name,

        # The API key for the PowerShell Gallery.
        [Parameter(Mandatory)]
        [string] $APIKey
    )

    $uri = "https://www.powershellgallery.com/packages/$Name"

    $body = @{
        __RequestVerificationToken = $APIKey
    }

    Invoke-RestMethod -Uri $uri -Method Get -Body $body
}

Write-Verbose "[$scriptName] - [public] - [Get-PSGalleryResource] - Done"
#endregion - From [public] - [Get-PSGalleryResource]
#region - From [public] - [Hide-PowerShellGalleryItem]
Write-Verbose "[$scriptName] - [public] - [Hide-PowerShellGalleryItem] - Importing"

function Hide-PowerShellGalleryItem {
    <#
        .SYNOPSIS
        Unlist a package on the PowerShell Gallery.
    #>

    [CmdletBinding()]
    [Alias('Unlist-PowerShellGalleryItem')]
    param ()

    Write-Warning 'This cmdlet is not yet implemented.'

}

Write-Verbose "[$scriptName] - [public] - [Hide-PowerShellGalleryItem] - Done"
#endregion - From [public] - [Hide-PowerShellGalleryItem]
#region - From [public] - [Show-PowerShellGalleryItem]
Write-Verbose "[$scriptName] - [public] - [Show-PowerShellGalleryItem] - Importing"

function Show-PowerShellGalleryItem {
    <#
        .SYNOPSIS
        List a package on the PowerShell Gallery.
    #>

    [CmdletBinding()]
    [Alias('List-PowerShellGalleryItem')]
    param ()

    Write-Warning 'This cmdlet is not yet implemented.'

}

Write-Verbose "[$scriptName] - [public] - [Show-PowerShellGalleryItem] - Done"
#endregion - From [public] - [Show-PowerShellGalleryItem]

Write-Verbose "[$scriptName] - [public] - Done"
#endregion - From [public]


$exports = @{
    Alias    = '*'
    Cmdlet   = ''
    Function = @(
        'Get-PSGalleryAPI'
        'Get-PSGalleryResource'
        'Hide-PowerShellGalleryItem'
        'Show-PowerShellGalleryItem'
    )
    Variable = ''
}
Export-ModuleMember @exports