diff options
Diffstat (limited to 'lua/mason-registry/api.lua')
| -rw-r--r-- | lua/mason-registry/api.lua | 47 |
1 files changed, 46 insertions, 1 deletions
diff --git a/lua/mason-registry/api.lua b/lua/mason-registry/api.lua index 037d7fd6..b700ba47 100644 --- a/lua/mason-registry/api.lua +++ b/lua/mason-registry/api.lua @@ -8,9 +8,11 @@ local BASE_URL = "https://api.mason-registry.dev" local stringify_params = _.compose(_.join "&", _.map(_.join "="), _.sort_by(_.head), _.to_pairs) +---@alias ApiFetchOpts { params: table<string, any>? } + ---@async ---@param path string ----@param opts { params: table<string, any>? }? +---@param opts ApiFetchOpts? ---@return Result # JSON decoded response. function api.get(path, opts) if opts and opts.params then @@ -24,4 +26,47 @@ function api.get(path, opts) }):map_catching(vim.json.decode) end +---@alias ApiSignature<T> fun(path_params: T, opts?: ApiFetchOpts): Result + +---@param path_template string +local function get(path_template) + ---@param path_params table + ---@param opts ApiFetchOpts? + return function(path_params, opts) + local path = path_template:gsub("{([%w_%.0-9]+)}", function(prop) + return path_params[prop] + end) + -- This is done so that test stubs trigger as expected (you have to explicitly match against nil arguments) + if opts then + return api.get(path, opts) + else + return api.get(path) + end + end +end + +api.repo = { + releases = { + ---@type ApiSignature<{ repo: string }> + latest = get "/api/repo/{repo}/releases/latest", + ---@type ApiSignature<{ repo: string }> + all = get "/api/repo/{repo}/releases/all", + }, + tags = { + ---@type ApiSignature<{ repo: string }> + latest = get "/api/repo/{repo}/tags/latest", + ---@type ApiSignature<{ repo: string }> + all = get "/api/repo/{repo}/tags/all", + }, +} + +api.npm = { + versions = { + ---@type ApiSignature<{ package: string }> + latest = get "/api/npm/{package}/versions/latest", + ---@type ApiSignature<{ package: string }> + all = get "/api/npm/{package}/versions/all", + }, +} + return api |
