From d658dccceb42b0b432aca9e02ffb2bae7cca9ce0 Mon Sep 17 00:00:00 2001 From: Marc Collins Date: Tue, 19 Nov 2024 14:04:28 +1100 Subject: [PATCH] Add BuildNPURI function for dynamic URL construction and update DynamicLoading script --- src/Private/BuildNPURI.ps1 | 31 +++++++++++++++++++ .../BuildFiles/Scripts/DynamicLoading.ps1 | 5 ++- 2 files changed, 33 insertions(+), 3 deletions(-) create mode 100644 src/Private/BuildNPURI.ps1 diff --git a/src/Private/BuildNPURI.ps1 b/src/Private/BuildNPURI.ps1 new file mode 100644 index 0000000..7fd501a --- /dev/null +++ b/src/Private/BuildNPURI.ps1 @@ -0,0 +1,31 @@ +function BuildNPURI { + param ( + [string]$Path, + [switch]$NPE, + [string]$URLServerAPI, + [string]$URLServerNPE, + [hashtable]$QueryParameters + ) + + if ([uri]::IsWellFormedUriString($Path, [System.UriKind]::Absolute)) { + return $Path + } + + # Base URL depending on the NPE flag + $BaseURL = if ($NPE) { $URLServerNPE } else { $URLServerAPI } + $FullPath = [System.IO.Path]::Combine($BaseURL.TrimEnd('/'), $Path.TrimStart('/')) + + # Add query parameters + if ($QueryParameters) { + $UriBuilder = [System.UriBuilder]$FullPath + $QueryString = [System.Web.HttpUtility]::ParseQueryString($UriBuilder.Query) + foreach ($key in $QueryParameters.Keys) { + if (-not $QueryString[$key]) { + $QueryString[$key] = $QueryParameters[$key] + } + } + $UriBuilder.Query = $QueryString.ToString() + return $UriBuilder.Uri.AbsoluteUri + } + return $FullPath +} \ No newline at end of file diff --git a/src/Resources/BuildFiles/Scripts/DynamicLoading.ps1 b/src/Resources/BuildFiles/Scripts/DynamicLoading.ps1 index ce656aa..12efa9a 100644 --- a/src/Resources/BuildFiles/Scripts/DynamicLoading.ps1 +++ b/src/Resources/BuildFiles/Scripts/DynamicLoading.ps1 @@ -3,7 +3,6 @@ $DynamicLoading = @' [System.IO.DirectoryInfo]$publicFunctionsPath = Join-Path $modulePath -ChildPath 'Public' [System.IO.DirectoryInfo]$privateFunctionsPath = Join-Path $modulePath -ChildPath 'Private' [System.IO.DirectoryInfo]$classesPath = Join-Path $modulePath -ChildPath 'Classes' -Write-Warning "$PSScriptRoot" $aliases = @() [regex]$FunctionName = [regex]::new('(?<=function )([\w]+-[\w]+)(?>[\s]+\{)', [System.Text.RegularExpressions.RegexOptions]::IgnoreCase) @@ -18,9 +17,9 @@ if ($publicFunctionsPath.Exists) { $alias = Get-Alias -Definition $function -ErrorAction SilentlyContinue if ($alias) { $aliases += $alias - Export-ModuleMember -Function $function -Alias $alias -Verbose + Export-ModuleMember -Function $function -Alias $alias } else { - Export-ModuleMember -Function "$($function)" -Verbose + Export-ModuleMember -Function "$($function)" } } }