plugins/rest: v3 migration

This commit is contained in:
Austin Horstman 2024-09-10 22:25:11 -05:00
parent 78816c0c9c
commit a75c2235d9
No known key found for this signature in database
2 changed files with 185 additions and 216 deletions

View file

@ -191,30 +191,6 @@ lib.nixvim.neovim-plugin.mkNeovimPlugin {
]; ];
settingsOptions = { settingsOptions = {
client = defaultNullOpts.mkStr "curl" ''
The HTTP client to be used when running requests.
'';
env_file = defaultNullOpts.mkStr ".env" ''
Environment variables file to be used for the request variables in the document.
'';
env_pattern = defaultNullOpts.mkStr "\\.env$" ''
Environment variables file pattern for `telescope.nvim`.
'';
env_edit_command = defaultNullOpts.mkStr "tabedit" ''
Neovim command to edit an environment file.
'';
encode_url = defaultNullOpts.mkBool true ''
Encode URL before making request.
'';
skip_ssl_verification = defaultNullOpts.mkBool false ''
Skip SSL verification, useful for unknown certificates.
'';
custom_dynamic_variables = lib.mkOption { custom_dynamic_variables = lib.mkOption {
type = with types; nullOr (maybeRaw (attrsOf strLuaFn)); type = with types; nullOr (maybeRaw (attrsOf strLuaFn));
default = null; default = null;
@ -234,113 +210,77 @@ lib.nixvim.neovim-plugin.mkNeovimPlugin {
apply = v: if lib.isAttrs v then lib.mapAttrs (_: lib.nixvim.mkRaw) v else v; apply = v: if lib.isAttrs v then lib.mapAttrs (_: lib.nixvim.mkRaw) v else v;
}; };
logs = { request = {
level = defaultNullOpts.mkNullable types.logLevel "info" '' skip_ssl_verification = defaultNullOpts.mkBool false ''
The logging level name, see `:h vim.log.levels`. Skip SSL verification, useful for unknown certificates.
''; '';
save = defaultNullOpts.mkBool true '' hooks = {
Whether to save log messages into a `.log` file. encode_url = defaultNullOpts.mkBool true ''
Encode URL before making request.
'';
user_agent = defaultNullOpts.mkStr (lib.nixvim.mkRaw ''"rest.nvim v" .. require("rest-nvim.api").VERSION'') ''
Set `User-Agent` header when it is empty.
'';
set_content_type = defaultNullOpts.mkBool true ''
Set `Content-Type` header when it is empty and body is provided.
'';
};
};
response = {
hooks = {
decode_url = defaultNullOpts.mkBool true ''
Encode URL before making request.
'';
format = defaultNullOpts.mkBool true ''
Format the response body using `gq` command.
'';
};
};
clients = defaultNullOpts.mkAttrsOf types.anything {
curl = {
statistics = [
{
id = "time_total";
winbar = "take";
title = "Time taken";
}
{
id = "size_download";
winbar = "size";
title = "Download size";
}
];
opts = {
set_compressed = false;
};
};
} ''Table of client configurations.'';
cookies = {
enable = defaultNullOpts.mkBool true ''
Whether to enable cookies support or not.
'';
path = defaultNullOpts.mkStr (lib.nixvim.mkRaw ''vim.fs.joinpath(vim.fn.stdpath("data") --[[@as string]], "rest-nvim.cookies")'') ''
Environment variables file pattern for `telescope.nvim`.
''; '';
}; };
result = { env = {
split = { enable = defaultNullOpts.mkBool true ''
horizontal = defaultNullOpts.mkBool false '' Whether to enable env file support or not.
Open request results in a horizontal split. '';
'';
in_place = defaultNullOpts.mkBool false '' pattern = defaultNullOpts.mkStr "\\.env$" ''
Keep the HTTP file buffer above|left when split horizontal|vertical. Environment variables file pattern for `telescope.nvim`.
''; '';
stay_in_current_window_after_split = defaultNullOpts.mkBool true ''
Stay in the current window (HTTP file) or change the focus to the results window.
'';
};
behavior = {
show_info = {
url = defaultNullOpts.mkBool true ''
Display the request URL.
'';
headers = defaultNullOpts.mkBool true ''
Display the request headers.
'';
http_info = defaultNullOpts.mkBool true ''
Display the request HTTP information.
'';
curl_command = defaultNullOpts.mkBool true ''
Display the cURL command that was used for the request.
'';
};
decode_url = defaultNullOpts.mkBool true ''
Whether to decode the request URL query parameters to improve readability.
'';
statistics = {
enable = defaultNullOpts.mkBool true ''
Whether to enable statistics or not.
'';
stats = defaultNullOpts.mkListOf (with types; attrsOf str) [
{
__unkeyed = "total_time";
title = "Time taken:";
}
{
__unkeyed = "size_download_t";
title = "Download size:";
}
] "See https://curl.se/libcurl/c/curl_easy_getinfo.html.";
};
formatters = {
json = defaultNullOpts.mkStr "jq" ''
JSON formatter.
'';
html = defaultNullOpts.mkStr {
__raw = ''
function(body)
if vim.fn.executable("tidy") == 0 then
return body, { found = false, name = "tidy" }
end
local fmt_body = vim.fn.system({
"tidy",
"-i",
"-q",
"--tidy-mark", "no",
"--show-body-only", "auto",
"--show-errors", "0",
"--show-warnings", "0",
"-",
}, body):gsub("\n$", "")
return fmt_body, { found = true, name = "tidy" }
end
'';
} "HTML formatter.";
};
};
keybinds = {
buffer_local = defaultNullOpts.mkBool false ''
Enable keybinds only in request result buffer.
'';
prev = defaultNullOpts.mkStr "H" ''
Mapping for cycle to previous result pane.
'';
next = defaultNullOpts.mkStr "L" ''
Mapping for cycle to next result pane.
'';
};
}; };
highlight = { highlight = {
@ -353,6 +293,10 @@ lib.nixvim.neovim-plugin.mkNeovimPlugin {
''; '';
}; };
_log_level = defaultNullOpts.mkNullableWithRaw types.logLevel "warn" ''
The logging level name, see `:h vim.log.levels`.
'';
keybinds = keybinds =
defaultNullOpts.mkListOf (with types; listOf str) defaultNullOpts.mkListOf (with types; listOf str)
[ [
@ -374,31 +318,31 @@ lib.nixvim.neovim-plugin.mkNeovimPlugin {
}; };
settingsExample = { settingsExample = {
client = "curl"; request = {
env_file = ".env"; skip_ssl_verification = true;
logs = {
level = "info";
save = true;
}; };
result = { response = {
split = { hooks = {
horizontal = false; format = false;
in_place = false;
stay_in_current_window_after_split = true;
}; };
}; };
keybinds = [ clients = {
[ curl = {
"<localleader>rr" opts = {
"<cmd>Rest run<cr>" set_compressed = true;
"Run request under the cursor" };
] };
[ };
"<localleader>rl" cookies = {
"<cmd>Rest run last<cr>" enable = false;
"Re-run latest request" };
] env = {
]; enable = false;
};
ui = {
winbar = false;
};
_log_level = "info";
}; };
extraOptions = { extraOptions = {
@ -417,6 +361,9 @@ lib.nixvim.neovim-plugin.mkNeovimPlugin {
enableTelescope = lib.mkEnableOption "telescope integration"; enableTelescope = lib.mkEnableOption "telescope integration";
}; };
# NOTE: plugin uses globals table for configuration
callSetup = false;
extraConfig = cfg: { extraConfig = cfg: {
assertions = [ assertions = [
{ {
@ -434,9 +381,60 @@ lib.nixvim.neovim-plugin.mkNeovimPlugin {
} }
]; ];
# TODO: introduced 2024-09-23: remove after 24.11
warnings =
let
definedOpts = lib.filter (opt: lib.hasAttrByPath (lib.toList opt) cfg.settings) [
"client"
"env_file"
"env_pattern"
"env_edit_command"
"encode_url"
"skip_ssl_verification"
[
"logs"
"level"
]
[
"logs"
"save"
]
[
"result"
"split"
]
[
"result"
"behavior"
]
[
"result"
"statistics"
]
[
"result"
"decode_url"
]
[
"result"
"formatters"
]
[
"result"
"keybinds"
]
];
in
lib.optional (definedOpts != [ ]) ''
Nixvim(plugins.rest): The following v2 settings options are no longer supported in v3:
${lib.concatMapStringsSep "\n" (opt: " - ${lib.showOption (lib.toList opt)}") definedOpts}
'';
# TODO: There may be some interactions between this & telescope, maybe requiring #2292 # TODO: There may be some interactions between this & telescope, maybe requiring #2292
plugins.rest.luaConfig.post = lib.mkIf cfg.enableTelescope ''require("telescope").load_extension("rest")''; plugins.rest.luaConfig.post = lib.mkIf cfg.enableTelescope ''require("telescope").load_extension("rest")'';
globals.rest_nvim = cfg.settings;
extraPackages = [ cfg.curlPackage ]; extraPackages = [ cfg.curlPackage ];
filetype = lib.mkIf cfg.enableHttpFiletypeAssociation { filetype = lib.mkIf cfg.enableHttpFiletypeAssociation {

View file

@ -13,69 +13,51 @@
enable = true; enable = true;
settings = { settings = {
client = "curl";
env_file = ".env";
env_pattern = "\\.env$";
env_edit_command = "tabedit";
encode_url = true;
skip_ssl_verification = false;
custom_dynamic_variables = { }; custom_dynamic_variables = { };
logs = { request = {
level = "info"; skip_ssl_verification = false;
save = true; hooks = {
encode_url = true;
user_agent.__raw = ''"rest.nvim v" .. require("rest-nvim.api").VERSION'';
set_content_type = true;
};
}; };
result = { response = {
split = { hooks = {
horizontal = false;
in_place = false;
stay_in_current_window_after_split = true;
};
behavior = {
show_info = {
url = true;
headers = true;
http_info = true;
curl_command = true;
};
decode_url = true; decode_url = true;
statistics = { format = true;
enable = true; };
stats = [ };
{ clients = {
__unkeyed = "total_time"; curl = {
title = "Time taken:"; statistics = [
} {
{ id = "time_total";
__unkeyed = "size_download_t"; winbar = "take";
title = "Download size:"; title = "Time taken";
} }
]; {
}; id = "size_download";
formatters = { winbar = "size";
json = "jq"; title = "Download size";
html.__raw = '' }
function(body) ];
if vim.fn.executable("tidy") == 0 then opts = {
return body, { found = false, name = "tidy" } set_compressed = false;
end
local fmt_body = vim.fn.system({
"tidy",
"-i",
"-q",
"--tidy-mark", "no",
"--show-body-only", "auto",
"--show-errors", "0",
"--show-warnings", "0",
"-",
}, body):gsub("\n$", "")
return fmt_body, { found = true, name = "tidy" }
end
'';
}; };
}; };
};
cookies = {
enable = true;
path.__raw = ''vim.fs.joinpath(vim.fn.stdpath("data") --[[@as string]], "rest-nvim.cookies")'';
};
env = {
enable = true;
pattern = ".*%.env.*";
};
ui = {
winbar = true;
keybinds = { keybinds = {
buffer_local = false;
prev = "H"; prev = "H";
next = "L"; next = "L";
}; };
@ -84,18 +66,7 @@
enable = true; enable = true;
timeout = 750; timeout = 750;
}; };
keybinds = [ _log_level.__raw = ''vim.log.levels.WARN'';
[
"<localleader>rr"
"<cmd>Rest run<cr>"
"Run request under the cursor"
]
[
"<localleader>rl"
"<cmd>Rest run last<cr>"
"Re-run latest request"
]
];
}; };
}; };
}; };