Skip to content

Commit 9346bcc

Browse files
fix(config): require loop
1 parent 95f810a commit 9346bcc

File tree

4 files changed

+9
-7
lines changed

4 files changed

+9
-7
lines changed

lua/rest-nvim/autocmds.lua

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ function autocmds.setup()
2525
if hooks.user_agent ~= "" then
2626
local header_empty = not req.headers["user-agent"] or #req.headers["user-agent"] < 1
2727
if header_empty then
28-
req.headers["user-agent"] = { hooks.user_agent }
28+
local user_agent = type(hooks.user_agent) == "function" and hooks.user_agent() or hooks.user_agent
29+
---@cast user_agent string
30+
req.headers["user-agent"] = { user_agent }
2931
end
3032
end
3133
if hooks.set_content_type then

lua/rest-nvim/config/check.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ function check.validate(cfg)
2828
["request.skip_ssl_verification"] = { cfg.request.skip_ssl_verification, "boolean" },
2929
["request.hooks"] = { cfg.request.hooks, "table" },
3030
["request.hooks.encode_url"] = { cfg.request.hooks.encode_url, "boolean" },
31-
["request.hooks.user_agent"] = { cfg.request.hooks.user_agent, "string" },
31+
["request.hooks.user_agent"] = { cfg.request.hooks.user_agent, { "function", "string" } },
3232
["request.hooks.set_content_type"] = { cfg.request.hooks.set_content_type, "boolean" },
3333
response = { cfg.response, "table" },
3434
["response.hooks"] = { cfg.response.hooks, "table" },

lua/rest-nvim/config/default.lua

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
---@mod rest-nvim.config.default rest.nvim default configuration
22

3-
local api = require("rest-nvim.api")
4-
53
---rest.nvim default configuration
64
---@class rest.Config
75
local default_config = {
@@ -16,8 +14,10 @@ local default_config = {
1614
hooks = {
1715
---@type boolean Encode URL before making request
1816
encode_url = true,
19-
---@type string Set `User-Agent` header when it is empty
20-
user_agent = "rest.nvim v" .. api.VERSION,
17+
---@type string|fun():string Set `User-Agent` header when it is empty
18+
user_agent = function ()
19+
return "rest.nvim v" .. require("rest-nvim.api").VERSION
20+
end,
2121
---@type boolean Set `Content-Type` header when it is empty and body is provided
2222
set_content_type = true,
2323
},

spec/minimum_init.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ vim.g.rest_nvim = {
1515
_log_level = vim.log.levels.INFO,
1616
request = {
1717
hooks = {
18-
set_user_agent = false,
18+
user_agent = "",
1919
}
2020
},
2121
cookies = {

0 commit comments

Comments
 (0)