Skip to content

Commit a6cf82b

Browse files
refactor(config)!: rewrite configs
- `require("rest-nvim").setup()` is not a requirement now - `vim.g.rest_nvim` instead of `_G._rest_nvim` - Create new `RestOpts` class in parallel with `RestConfig` class. First one is optional. - cleanup some configs like `config.result.behavior.statistics.stats`
1 parent 287a34a commit a6cf82b

File tree

19 files changed

+180
-176
lines changed

19 files changed

+180
-176
lines changed

lua/rest-nvim/autocmds.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@
99
local autocmds = {}
1010

1111
local result = require("rest-nvim.result")
12+
local config = require("rest-nvim.config")
1213
local result_help = require("rest-nvim.result.help")
1314

1415
---Set up Rest autocommands group
1516
function autocmds.setup()
1617
local rest_nvim_augroup = vim.api.nvim_create_augroup("Rest", {})
17-
local keybinds = _G._rest_nvim.result.keybinds
18+
local keybinds = config.result.keybinds
1819

1920
vim.api.nvim_create_autocmd({ "BufEnter", "BufWinEnter" }, {
2021
group = rest_nvim_augroup,

lua/rest-nvim/client/curl.lua

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ local found_curl, curl = pcall(require, "cURL.safe")
1212

1313
local utils = require("rest-nvim.utils")
1414
local logger = require("rest-nvim.logger")
15+
local config = require("rest-nvim.config")
1516

1617
-- TODO: add support for submitting forms in the `client.request` function
1718

@@ -112,13 +113,13 @@ local function curl_error(code)
112113
return "cURL error " .. tostring(code) .. ": " .. curl_error_dictionary[code]
113114
end
114115

116+
-- TODO: don't render statistics here. render from rest-nvim.result
117+
115118
---Get request statistics
116119
---@param req table cURL request class
117-
---@param statistics_tbl RestConfigResultStats Statistics table
118-
---@return table Request statistics
120+
---@param statistics_tbl table Statistics table
121+
---@return table<string,string> stats Request statistics
119122
local function get_stats(req, statistics_tbl)
120-
local stats = {}
121-
122123
local function get_stat(req_, stat_)
123124
local curl_info = curl["INFO_" .. stat_:upper()]
124125
if not curl_info then
@@ -138,18 +139,11 @@ local function get_stats(req, statistics_tbl)
138139
return stat_info
139140
end
140141

141-
local stat_key, stat_title, stat_info
142-
for _, stat in pairs(statistics_tbl) do
143-
for k, v in pairs(stat) do
144-
if type(k) == "string" and k == "title" then
145-
stat_title = v
146-
end
147-
if type(k) == "number" then
148-
stat_key = v
149-
stat_info = get_stat(req, v)
150-
end
151-
end
152-
stats[stat_key] = stat_title .. " " .. stat_info
142+
local stats = {}
143+
144+
for name, _ in pairs(statistics_tbl) do
145+
-- stats[name] = style.title .. " " .. get_stat(req, name)
146+
stats[name] = get_stat(req, name)
153147
end
154148

155149
return stats
@@ -178,7 +172,7 @@ function client.request_(request)
178172
end
179173

180174
-- Whether to skip SSL host and peer verification
181-
local skip_ssl_verification = _G._rest_nvim.skip_ssl_verification
175+
local skip_ssl_verification = config.skip_ssl_verification
182176
local req = curl.easy_init()
183177
req:setopt({
184178
url = request.url,
@@ -189,7 +183,7 @@ function client.request_(request)
189183
})
190184

191185
-- Encode URL query parameters and set the request URL again with the encoded values
192-
local should_encode_url = _G._rest_nvim.encode_url
186+
local should_encode_url = config.encode_url
193187
if should_encode_url then
194188
-- Create a new URL as we cannot extract the URL from the req object
195189
local _url = curl.url()
@@ -259,7 +253,7 @@ function client.request_(request)
259253
local ok, err = req:perform()
260254
if ok then
261255
-- Get request statistics if they are enabled
262-
local stats_config = _G._rest_nvim.result.behavior.statistics
256+
local stats_config = config.result.behavior.statistics
263257
if stats_config.enable then
264258
info.statistics = get_stats(req, stats_config.stats)
265259
end

lua/rest-nvim/commands.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ local rest_command_tbl = {
5858
},
5959
logs = {
6060
impl = function(_)
61-
vim.cmd.e(require("rest-nvim.logger").get_logfile())
61+
vim.cmd.e(logger.get_logfile())
6262
end,
6363
},
6464
env = {

lua/rest-nvim/config/check.lua

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,29 +24,18 @@ end
2424
function check.validate(cfg)
2525
local ok, err = validate({
2626
env_pattern = { cfg.env_pattern, "string" },
27-
env_edit_command = { cfg.env_edit_command, "string" },
2827
encode_url = { cfg.encode_url, "boolean" },
2928
skip_ssl_verification = { cfg.skip_ssl_verification, "boolean" },
3029
custom_dynamic_variables = { cfg.custom_dynamic_variables, "table" },
31-
-- RestConfigLogs
32-
level = { cfg.logs.level, "string" },
33-
save = { cfg.logs.save, "boolean" },
3430
-- RestConfigResult
3531
result = { cfg.result, "table" },
36-
-- RestConfigResultSplit
37-
split = { cfg.result.split, "table" },
38-
horizontal = { cfg.result.split.horizontal, "boolean" },
39-
in_place = { cfg.result.split.in_place, "boolean" },
40-
stay_in_current_window_after_split = { cfg.result.split.stay_in_current_window_after_split, "boolean" },
32+
-- RestConfigResultWindow
33+
window = { cfg.result.window, "table" },
34+
horizontal = { cfg.result.window.horizontal, "boolean" },
35+
enter = { cfg.result.window.enter, "boolean" },
4136
-- RestConfigResultBehavior
4237
behavior = { cfg.result.behavior, "table" },
4338
decode_url = { cfg.result.behavior.decode_url, "boolean" },
44-
-- RestConfigResultInfo
45-
show_info = { cfg.result.behavior.show_info, "table" },
46-
url = { cfg.result.behavior.show_info.url, "boolean" },
47-
headers = { cfg.result.behavior.show_info.headers, "boolean" },
48-
http_info = { cfg.result.behavior.show_info.http_info, "boolean" },
49-
curl_command = { cfg.result.behavior.show_info.curl_command, "boolean" },
5039
-- RestConfigResultStats
5140
statistics = { cfg.result.behavior.statistics, "table" },
5241
statistics_enable = { cfg.result.behavior.statistics.enable, "boolean" },

lua/rest-nvim/config/init.lua

Lines changed: 111 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -6,100 +6,106 @@
66
---
77
---@brief ]]
88

9+
---@type RestConfig
910
local config = {}
1011

11-
local logger = require("rest-nvim.logger")
12-
13-
---@class RestConfigDebug
14-
---@field unrecognized_configs table<string,string> Unrecognized configuration options
15-
16-
---@class RestConfigLogs
17-
---@field level string The logging level name, see `:h vim.log.levels`. Default is `"info"`
18-
---@field save boolean Whether to save log messages into a `.log` file. Default is `true`
19-
20-
---@class RestConfigResult
21-
---@field split RestConfigResultSplit Result split window behavior
22-
---@field behavior RestConfigResultBehavior Result buffer behavior
23-
---@field keybinds RestConfigResultKeybinds Keybinds settings to navigate throught request results
24-
25-
---@class RestConfigResultSplit
26-
---@field horizontal boolean Open request results in a horizontal split
27-
---@field in_place boolean Keep the HTTP file buffer above|left when split horizontal|vertical
28-
---@field stay_in_current_window_after_split boolean Stay in the current window (HTTP file) or change the focus to the results window
29-
30-
---@class RestConfigResultBehavior
31-
---@field show_info RestConfigResultInfo Request results information
32-
---@field decode_url boolean Whether to decode the request URL query parameters to improve readability
33-
---@field statistics RestConfigResultStats Request results statistics
34-
---@field formatters RestConfigResultFormatters Formatters for the request results body. If the formatter is a function it should return two values, the formatted body and a table containing two values `found` (whether the formatter has been found or not) and `name` (the formatter name)
35-
36-
---@class RestConfigResultInfo
37-
---@field url boolean Display the request URL
38-
---@field headers boolean Display the request headers
39-
---@field http_info boolean Display the request HTTP information
40-
---@field curl_command boolean Display the cURL command that was used for the request
41-
42-
---@class RestConfigResultStats
43-
---@field enable boolean Whether enable statistics or not
44-
---@field stats string[]|{ [1]: string, title: string }[] Statistics to be shown, takes cURL's easy getinfo constants name
45-
46-
---@class RestConfigResultFormatters
47-
---@field json string|fun(body: string): string,table JSON formatter
48-
---@field html string|fun(body: string): string,table HTML formatter
49-
50-
---@class RestConfigResultKeybinds
51-
---@field prev string Mapping for cycle to previous result pane
52-
---@field next string Mapping for cycle to next result pane
53-
54-
---@class RestConfigHighlight
55-
---@field enable boolean Whether current request highlighting is enabled or not
56-
---@field timeout number Duration time of the request highlighting in milliseconds
57-
58-
---@class RestConfig
59-
---@field env_pattern string Environment variables file pattern for telescope.nvim
60-
---@field env_edit_command string Neovim command to edit an environment file, default is `"tabedit"`
61-
---@field encode_url boolean Encode URL before making request
62-
---@field skip_ssl_verification boolean Skip SSL verification, useful for unknown certificates
63-
---@field custom_dynamic_variables table<string, fun():string> Table of custom dynamic variables
64-
---@field logs RestConfigLogs Logging system configuration
65-
---@field result RestConfigResult Request results buffer behavior
66-
---@field highlight RestConfigHighlight Request highlighting
67-
---@field _debug_info? RestConfigDebug Configurations debug information, set automatically
12+
---@alias RestResultFormatters string|fun(body:string):string,table
13+
14+
---@class RestOptsHighlight
15+
--- Whether current request highlighting is enabled or not (Default: `true`)
16+
---@field enable? boolean
17+
--- Duration time of the request highlighting in milliseconds (Default: `250`)
18+
---@field timeout? number
19+
20+
---@class RestOptsResult
21+
---@field window? RestOptsResultWindow
22+
---@field behavior? RestOptsResultBehavior
23+
---@field keybinds? RestOptsResultKeybinds
24+
25+
---@class RestOptsResultWindow
26+
--- Open request results in a horizontal split (Default: `false`)
27+
---@field horizontal? boolean
28+
---@field enter? boolean
29+
30+
---@class RestOptsResultBehavior
31+
---@field decode_url boolean
32+
---@field statistics RestOptsStatistics
33+
---@field formatters table<string,RestResultFormatters>
34+
35+
---@class RestOptsStatistics
36+
---@field enable boolean
37+
---@field stats table<string,RestOptsResultStatStyle>
38+
39+
---@class RestOptsResultKeybinds
40+
--- Mapping for cycle to previous result pane (Default: `"H"`)
41+
---@field prev? string
42+
--- Mapping for cycle to next result pane (Default: `"L"`)
43+
---@field next? string
44+
45+
---@class RestOptsResultStatStyle
46+
--- Title used on result pane
47+
---@field title? string
48+
--- Winbar title. Set to `false` or `nil` to not show for winbar, set to empty string
49+
--- to hide title If true, rest.nvim will use lowered `title` field
50+
---@field winbar? string|boolean
51+
52+
---@class RestOpts
53+
--- Environment variables file pattern for telescope.nvim
54+
--- (Default: `".*env.*$"`)
55+
---@field env_pattern? string
56+
--- Encode URL before making request (Default: `true`)
57+
---@field encode_url? boolean
58+
--- Skip SSL verification, useful for unknown certificates (Default: `false`)
59+
---@field skip_ssl_verification? boolean
60+
--- Table of custom dynamic variables
61+
---@field custom_dynamic_variables? table<string, fun():string>
62+
--- Request highlighting config
63+
---@field highlight? RestOptsHighlight
64+
--- Result view config
65+
---@field result? RestOptsResult
66+
67+
---@type RestOpts
68+
vim.g.rest_nvim = vim.g.rest_nvim
6869

6970
---rest.nvim default configuration
70-
---@type RestConfig
71+
---@class RestConfig
7172
local default_config = {
73+
---@type string Environment variables file pattern for telescope.nvim
7274
env_pattern = ".*env.*$",
73-
env_edit_command = "tabedit",
75+
76+
---@type boolean Encode URL before making request
7477
encode_url = true,
78+
---@type boolean Skip SSL verification, useful for unknown certificates
7579
skip_ssl_verification = false,
80+
---@type table<string, fun():string> Table of custom dynamic variables
7681
custom_dynamic_variables = {},
77-
logs = {
78-
level = "info",
79-
save = true,
80-
},
82+
83+
---@class RestConfigResult
8184
result = {
82-
split = {
85+
---@class RestConfigResultWindow
86+
window = {
87+
---@type boolean Open request results in a horizontal split
8388
horizontal = false,
84-
in_place = false,
85-
stay_in_current_window_after_split = true,
89+
---@type boolean Change the focus to the results window or stay in the current window (HTTP file)
90+
enter = false,
8691
},
92+
---@class RestConfigResultBehavior
8793
behavior = {
94+
---@type boolean Whether to decode the request URL query parameters to improve readability
8895
decode_url = true,
89-
show_info = {
90-
url = true,
91-
headers = true,
92-
http_info = true,
93-
curl_command = true,
94-
},
96+
---@class RestConfigResultStats
9597
statistics = {
98+
---@type boolean Whether enable statistics or not
9699
enable = true,
100+
---Statistics to be shown, takes cURL's easy getinfo constants name
97101
---@see https://curl.se/libcurl/c/curl_easy_getinfo.html
102+
---@type table<string,RestOptsResultStatStyle>
98103
stats = {
99-
{ "total_time", title = "Time taken:" },
100-
{ "size_download_t", title = "Download size:" },
104+
total_time = { winbar = true, title = "Time taken" },
105+
size_download_t = { winbar = true, title = "Download size" },
101106
},
102107
},
108+
---@type table<string,RestResultFormatters>
103109
formatters = {
104110
json = "jq",
105111
html = function(body)
@@ -122,42 +128,51 @@ local default_config = {
122128
end,
123129
},
124130
},
131+
---@class RestConfigResultKeybinds
125132
keybinds = {
133+
---@type string Mapping for cycle to previous result pane
126134
prev = "H",
135+
---@type string Mapping for cycle to next result pane
127136
next = "L",
128137
},
129138
},
139+
---@class RestConfigHighlight
130140
highlight = {
141+
---@type boolean Whether current request highlighting is enabled or not
131142
enable = true,
143+
---@type number Duration time of the request highlighting in milliseconds
132144
timeout = 750,
133145
},
146+
---@see vim.log.levels
147+
---@type integer log level
134148
_log_level = vim.log.levels.WARN,
149+
---@class RestConfigDebugInfo
150+
_debug_info = {
151+
-- NOTE: default option is `nil` to prevent overwriting as empty array
152+
---@type string[]
153+
unrecognized_configs = nil,
154+
},
135155
}
136156

137-
---Set user-defined configurations for rest.nvim
138-
---@param user_configs RestConfig User configurations
139-
---@return RestConfig rest.nvim configuration table
140-
function config.set(user_configs)
141-
local check = require("rest-nvim.config.check")
142-
143-
local conf = vim.tbl_deep_extend("force", {
144-
_debug_info = {
145-
unrecognized_configs = check.get_unrecognized_keys(user_configs, default_config),
146-
},
147-
}, default_config, user_configs)
148-
149-
local ok, err = check.validate(conf)
150-
151-
if not ok then
152-
---@cast err string
153-
conf.logger.error(err)
154-
end
157+
local check = require("rest-nvim.config.check")
158+
local opts = vim.g.rest_nvim or {}
159+
config = vim.tbl_deep_extend("force", {
160+
_debug_info = {
161+
unrecognized_configs = check.get_unrecognized_keys(opts, default_config),
162+
},
163+
}, default_config, opts)
164+
---@cast config RestConfig
165+
local ok, err = check.validate(config)
155166

156-
if #conf._debug_info.unrecognized_configs > 0 then
157-
conf.logger.warn("Unrecognized configs found in setup: " .. vim.inspect(conf._debug_info.unrecognized_configs))
158-
end
167+
if not ok then
168+
vim.notify("Rest.nvim: " .. err, vim.log.levels.ERROR)
169+
end
159170

160-
return conf
171+
if #config._debug_info.unrecognized_configs > 0 then
172+
vim.notify(
173+
"Unrecognized configs found in setup: " .. vim.inspect(config._debug_info.unrecognized_configs),
174+
vim.log.levels.WARN
175+
)
161176
end
162177

163178
return config

0 commit comments

Comments
 (0)