Skip to content

Commit

Permalink
Move over to passing packages as array in POST body
Browse files Browse the repository at this point in the history
  • Loading branch information
Caleb-T-Owens committed Jan 28, 2024
1 parent 62f7f93 commit ebddf48
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 23 deletions.
26 changes: 14 additions & 12 deletions lib/importmap/jspm_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ def generate(install:, flatten_scope:, env:, provider:)
response_json(response)
end

def download(versioned_package_name:, provider:, exclude:)
response = post_json("#{self.class.download_endpoint}/#{versioned_package_name}", {
def download(versioned_package_names:, provider:, exclude:)
response = post_json("#{self.class.download_endpoint}", {
packages: versioned_package_names,
provider: normalize_provider(provider),
exclude:
})
Expand All @@ -28,18 +29,19 @@ def download(versioned_package_name:, provider:, exclude:)

return {} if json.blank?

files = json.dig(versioned_package_name, "files")
package_uri = URI(json.dig(versioned_package_name, "pkgUrl"))

output_files = {}

Net::HTTP.start(package_uri.hostname, { use_ssl: true }) do |http|
files.each do |file|
output_files[file] = fetch_file(http, "#{package_uri.path}/#{file}")
json.transform_values do |package_download_details|
files = package_download_details["files"]
package_uri = URI(package_download_details["pkgUrl"])

Net::HTTP.start(package_uri.hostname, { use_ssl: true }) do |http|
files.map do |file|
[
file,
fetch_file(http, "#{package_uri.path}/#{file}")
]
end.to_h
end
end

output_files
end

private
Expand Down
6 changes: 4 additions & 2 deletions lib/importmap/package.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@ def download

jspm_api = Importmap::JspmApi.new

versioned_package_name = "#{@package_name}#{@version}"

files = jspm_api.download(
versioned_package_name: "#{@package_name}#{@version}",
versioned_package_names: [versioned_package_name],
provider: @provider,
exclude: ["unused", "readme", "types"]
)
)[versioned_package_name]

files.each do |file, downloaded_file|
save_vendored_file(file, downloaded_file)
Expand Down
18 changes: 9 additions & 9 deletions test/jspm_api_integration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,25 @@ class Importmap::JspmApiIntegrationTest < ActiveSupport::TestCase
end

test "#download when given a valid input" do
result = @jspm_api.download(versioned_package_name: "@popperjs/[email protected]", provider: "jspm.io", exclude: [])
result = @jspm_api.download(versioned_package_names: ["@popperjs/[email protected]"], provider: "jspm.io", exclude: [])

assert result.keys.include?("lib/dom-utils/getWindow.js")
assert result.keys.include?("lib/index.js")
assert result["@popperjs/[email protected]"].keys.include?("lib/dom-utils/getWindow.js")
assert result["@popperjs/[email protected]"].keys.include?("lib/index.js")

result = @jspm_api.download(versioned_package_name: "@popperjs/[email protected]", provider: "jspm", exclude: [])
result = @jspm_api.download(versioned_package_names: ["@popperjs/[email protected]"], provider: "jspm", exclude: [])

assert result.keys.include?("lib/dom-utils/getWindow.js")
assert result.keys.include?("lib/index.js")
assert result["@popperjs/[email protected]"].keys.include?("lib/dom-utils/getWindow.js")
assert result["@popperjs/[email protected]"].keys.include?("lib/index.js")
end

test "#download when given a bad package" do
result = @jspm_api.download(versioned_package_name: "@popperjs/corenoversion", provider: "jspm.io", exclude: [])
result = @jspm_api.download(versioned_package_names: ["@popperjs/corenoversion"], provider: "jspm.io", exclude: [])

assert_equal result, {}
end

test "#download when given a bad provider" do
result = @jspm_api.download(versioned_package_name: "@popperjs/corenoversion", provider: "jspmfoobarbaz", exclude: [])
result = @jspm_api.download(versioned_package_names: ["@popperjs/corenoversion"], provider: "jspmfoobarbaz", exclude: [])

assert_equal result, {}
end
Expand All @@ -35,7 +35,7 @@ class Importmap::JspmApiIntegrationTest < ActiveSupport::TestCase
Importmap::JspmApi.download_endpoint = URI("https://invalid./error")

assert_raises(Importmap::JspmApi::HTTPError) do
@jspm_api.download(versioned_package_name: "@popperjs/[email protected]", provider: "jspm.io", exclude: [])
@jspm_api.download(versioned_package_names: ["@popperjs/[email protected]"], provider: "jspm.io", exclude: [])
end
ensure
Importmap::JspmApi.download_endpoint = original_endpoint
Expand Down

0 comments on commit ebddf48

Please sign in to comment.