When working with TypeScript, it's important to note that since it is transpiled into JavaScript, directly reading from a configuration file may not be possible.
To dynamically update URLs for a service - switching from a development endpoint to a QA or production endpoint, there are a couple of approaches you can take. One option is to utilize Razor Syntax in your HTML/CSHTML files to retrieve values from the configuration file and inject them into the scripts. Alternatively, another method involves using Powershell during deployment to replace the URL values in your TypeScript files as part of the build process.
# PowerShell script example demonstrating how to replace URL values in *.js files.
$NewURL1 = $args[0];
$NewURL2 = "http://goodURL.com/api";
$files = Get-ChildItem "C:\MyProject\DropFolder" -Recurse -Include "exampleWithBadURL.js", "otherFile.js";
Foreach ($file in $files)
{
$fileContent = Get-Content($file) -Raw;
attrib $file -r;
# Perform the replacement.
$fileContent -replace "http://localhost:38774/", $NewURL1 | Out-File $file;
$fileContent -replace "http://badURL.com/api/", $NewURL2 | Out-File $file;
Write-Output($file.FullName);
}
If your build/release agent supports it, you can incorporate this Powershell script into your deployment process when moving code to a specific environment. For instance, I have used a similar approach in VSTS/TFS within my release definition occurring after file copying.