Skip to content

Commit 89623ab

Browse files
fix: update Npm suggestions provider to use public registry API for improved reliability
1 parent 00fa61a commit 89623ab

1 file changed

Lines changed: 6 additions & 4 deletions

File tree

  • CmdPalWebSearchShortcut/WebSearchShortcut/SuggestionsProviders

CmdPalWebSearchShortcut/WebSearchShortcut/SuggestionsProviders/Npm.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ public async Task<IReadOnlyList<Suggestion>> GetSuggestionsAsync(string query, C
2020
{
2121
try
2222
{
23-
const string api = "https://www.npmjs.com/search/suggestions?q=";
23+
// Use the public registry API instead of the website internal API to avoid bot protection issues
24+
const string api = "https://registry.npmjs.org/-/v1/search?text=";
2425

2526
await using var resultStream = await Http
2627
.GetStreamAsync(api + Uri.EscapeDataString(query), cancellationToken)
@@ -30,14 +31,15 @@ public async Task<IReadOnlyList<Suggestion>> GetSuggestionsAsync(string query, C
3031
.ParseAsync(resultStream, cancellationToken: cancellationToken)
3132
.ConfigureAwait(false);
3233

33-
var results = json.RootElement.EnumerateArray();
34+
var results = json.RootElement.GetProperty("objects").EnumerateArray();
3435

3536
Suggestion[] items = [
3637
.. results
3738
.Select(o =>
3839
{
39-
var title = o.GetProperty("name").GetString();
40-
var description = o.GetProperty("description").GetString();
40+
var pkg = o.GetProperty("package");
41+
var title = pkg.GetProperty("name").GetString();
42+
var description = pkg.TryGetProperty("description", out var desc) ? desc.GetString() : "";
4143
return title is null ? null : new Suggestion(title, description ?? "");
4244
})
4345
.Where(s => s is not null)

0 commit comments

Comments
 (0)