-
-
Notifications
You must be signed in to change notification settings - Fork 257
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(registry): add api module (#524)
- Loading branch information
1 parent
d726743
commit 72efedb
Showing
3 changed files
with
74 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
local _ = require "mason-core.functional" | ||
local fetch = require "mason-core.fetch" | ||
|
||
local api = {} | ||
|
||
local stringify_params = _.compose(_.join "&", _.map(_.join "="), _.sort_by(_.head), _.to_pairs) | ||
|
||
---@async | ||
---@param path string | ||
---@param opts { params: table<string, any>? }? | ||
---@return Result # JSON decoded response. | ||
function api.get(path, opts) | ||
if opts and opts.params then | ||
local params = stringify_params(opts.params) | ||
path = ("%s?%s"):format(path, params) | ||
end | ||
-- https://github.com/williamboman/mason-registry-api | ||
return fetch(("https://api.mason-registry.dev%s"):format(path)):map_catching(vim.json.decode) | ||
end | ||
|
||
return api |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
local stub = require "luassert.stub" | ||
local Result = require "mason-core.result" | ||
|
||
describe("mason-registry API", function() | ||
---@module "mason-registry.api" | ||
local api | ||
local fetch | ||
before_each(function() | ||
fetch = stub.new() | ||
package.loaded["mason-core.fetch"] = fetch | ||
package.loaded["mason-registry.api"] = nil | ||
api = require "mason-registry.api" | ||
end) | ||
|
||
it("should stringify query parameters", function() | ||
fetch.returns(Result.success [[{}]]) | ||
|
||
api.get("/api/data", { | ||
params = { | ||
page = 2, | ||
page_limit = 10, | ||
sort = "ASC", | ||
}, | ||
}) | ||
|
||
assert.spy(fetch).was_called(1) | ||
assert.spy(fetch).was_called_with "https://api.mason-registry.dev/api/data?page=2&page_limit=10&sort=ASC" | ||
end) | ||
|
||
it("should deserialize JSON", function() | ||
fetch.returns(Result.success [[{"field": ["value"]}]]) | ||
|
||
local result = api.get("/"):get_or_throw() | ||
|
||
assert.same({ field = { "value" } }, result) | ||
end) | ||
end) |