当前,我正在研究Visual Studio Code扩展支持。就我而言,我需要将最新的NuGet
软件包从Online安装到VSCode Blazor
项目。您能否建议我如何使用打字稿基于NuGet
软件包名称和最新NuGet
版本的软件包来获取所有已发布的NuGet
软件包?由于我需要Visual Studio Code的逻辑,因此无法在Visual Studio Code中使用C#
Visual Studio逻辑。
答案 0 :(得分:1)
根据document,您可以向此端点发送HTTP GET请求以枚举软件包版本:
GET {@id}?id={ID}&prerelease={PRERELEASE}&semVerLevel={SEMVERLEVEL}
例如。通过发送请求到:
https://azuresearch-ussc.nuget.org/autocomplete?id=Microsoft.AspNetCore.Blazor&prerelease=true&semVerLevel=2
您可以枚举以下软件包:
{"@context":{"@vocab":"http://schema.nuget.org/schema#"},"totalHits":1,"data":["0.1.0","0.2.0","0.2.1","0.3.0","0.4.0","0.5.0","0.5.1","0.6.0","0.7.0","0.8.0-preview-19075-0444","0.8.0-preview-19104-04","0.9.0-preview3-19153-02","0.9.0-preview3-19154-02","3.0.0-preview4-19216-03","3.0.0-preview5-19227-01","3.0.0-preview6.19307.2","3.0.0-preview7.19365.7","3.0.0-preview8.19405.7","3.0.0-preview9.19424.4","3.0.0-preview9.19457.4","3.0.0-preview9.19465.2","3.1.0-preview1.19508.20"]}
为了获得这样的基本URL,您可以向ServiceIndex
发送请求,并查询@id
等于@type
或{{1}的SearchAutocompleteService
}或SearchAutocompleteService/3.0.0-beta
。有关更多详细信息,请参阅https://docs.microsoft.com/en-us/nuget/api/search-autocomplete-service-resource#versioning
SearchAutocompleteService/3.0.0-rc
并调用以下函数:
interface ItemDescription {
"@id": String;
"@type": String;
"comment": String;
}
interface ServiceIndexResponse {
version: String;
resources: ItemDescription[];
} ;
interface PackageVersionsResponse{
"@context": any;
"totalHits": Number;
"data": String[];
}
function enumeratePackageVersion(packageId: String, prerelease: Boolean, semVerLevel? : string){
var serviceIndexUrl = "https://api.nuget.org/v3/index.json";
return fetch(serviceIndexUrl,{})
.then(r => r.json())
.then((r: ServiceIndexResponse) => {
let typeName ="SearchAutocompleteService" ;
let queries = r.resources.filter(item => item["@type"] == typeName);
if(queries.length == 0) throw new Error(`unable to get the ${typeName} from ServiceIndex`);
var serviceUrl = queries[0]["@id"];
return serviceUrl;
})
.then(serviceUrl =>{
var query = `id=${packageId}&prerelease=${prerelease}`;
if(semVerLevel){
query = query + `&semVerLevel=${semVerLevel}`;
}
return fetch(`${serviceUrl}?${query}`,{})
})
.then(r => r.json())
.then((r: PackageVersionsResponse) => r.data);
}
[编辑]
以上代码假设您使用的是提取API。您可以按照以下步骤使用enumeratePackageVersion("Microsoft.AspNetCore.Blazor", true, "2.0.0")
.then(r =>{
console.log(JSON.stringify(r));
});
:
node-fetch
安装node-fetch
npm i node-fetch --save
安装类型声明包npm i @types/node-fetch --save-dev