如何在VS Code中获取NuGet的版本列表和最新版本?

时间:2019-10-22 11:35:09

标签: asp.net-core visual-studio-code nuget vscode-extensions blazor

当前,我正在研究Visual Studio Code扩展支持。就我而言,我需要将最新的NuGet软件包从Online安装到VSCode Blazor项目。您能否建议我如何使用打字稿基于NuGet软件包名称和最新NuGet版本的软件包来获取所有已发布的NuGet软件包?由于我需要Visual Studio Code的逻辑,因此无法在Visual Studio Code中使用C# Visual Studio逻辑。

1 个答案:

答案 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)); });

  1. 通过node-fetch安装node-fetch
  2. 通过npm i node-fetch --save安装类型声明包
  3. 通过npm i @types/node-fetch --save-dev
  4. 导入提取

演示屏幕截图 enter image description here