Skip to content

Commit a3569a0

Browse files
committed
Add npm downloads
1 parent b65bf7b commit a3569a0

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

apps/components_guide/lib/components_guide/research/spec.ex

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,23 @@ defmodule ComponentsGuide.Research.Spec do
2424
end
2525
end
2626

27+
def search_for(:npm_downloads_last_month, query) when is_binary(query) do
28+
query = String.trim(query)
29+
url = "https://api.npmjs.org/downloads/point/last-month/#{query}"
30+
{:ok, data} = Source.json_at(url)
31+
32+
case data do
33+
%{"downloads" => downloads_count, "package" => name} ->
34+
%{downloads_count: downloads_count, name: name}
35+
36+
%{"error" => _error_message} ->
37+
nil
38+
39+
_ ->
40+
nil
41+
end
42+
end
43+
2744
def search_for(:whatwg_html_spec, query) when is_binary(query) do
2845
# url = "https://html.spec.whatwg.org/"
2946
url = "https://html.spec.whatwg.org/dev/"

apps/components_guide_web/lib/components_guide_web/controllers/research_controller.ex

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,28 @@ defmodule ComponentsGuideWeb.ResearchController do
8484
end
8585
end
8686

87+
defp npm_downloads(query) do
88+
case Spec.search_for(:npm_downloads_last_month, query) do
89+
%{downloads_count: downloads_count, name: name} ->
90+
content_tag(:article, [
91+
h2("NPM packages"),
92+
Section.card([
93+
content_tag(:h3, link(name, to: "https://www.npmjs.com/package/#{name}"), class: "text-2xl"),
94+
content_tag(
95+
:dl,
96+
[
97+
content_tag(:dt, "Monthly downloads", class: "text-base font-bold"),
98+
content_tag(:dd, "#{downloads_count}")
99+
]
100+
)
101+
])
102+
])
103+
104+
_ ->
105+
[]
106+
end
107+
end
108+
87109
defmodule CanIUse do
88110
alias ComponentsGuideWeb.ResearchController.Section
89111

@@ -109,7 +131,9 @@ defmodule ComponentsGuideWeb.ResearchController do
109131
content_tag(:dt, "Description", class: "font-bold"),
110132
content_tag(:dd, "#{description}"),
111133
case notes do
112-
"" -> []
134+
"" ->
135+
[]
136+
113137
notes ->
114138
[
115139
content_tag(:dt, "Notes", class: "font-bold"),
@@ -198,6 +222,7 @@ defmodule ComponentsGuideWeb.ResearchController do
198222
# ComponentsGuide.Research.Source.clear_cache()
199223
[
200224
bundlephobia(query),
225+
npm_downloads(query),
201226
caniuse(query),
202227
html_spec(query),
203228
aria_practices(query),

0 commit comments

Comments
 (0)