Skip to content

Commit 5880134

Browse files
feat(config): more builtin request hooks
1 parent e795f7c commit 5880134

File tree

5 files changed

+37
-1
lines changed

5 files changed

+37
-1
lines changed

lua/rest-nvim/autocmds.lua

+19-1
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,27 @@ function autocmds.setup()
1818
local config = require("rest-nvim.config")
1919
local utils = require("rest-nvim.utils")
2020
local req = _G.rest_request
21-
if config.request.hooks.encode_url then
21+
local hooks = config.request.hooks
22+
if hooks.encode_url then
2223
req.url = utils.escape(req.url, true)
2324
end
25+
if hooks.user_agent ~= "" then
26+
local header_empty = not req.headers["user-agent"] or #req.headers["user-agent"] < 1
27+
if header_empty then
28+
req.headers["user-agent"] = { hooks.user_agent }
29+
end
30+
end
31+
if hooks.set_content_type then
32+
local header_empty = not req.headers["content-type"] or #req.headers["content-type"] < 1
33+
if header_empty and req.body then
34+
if req.body.__TYPE == "json" then
35+
req.headers["content-type"] = { "application/json" }
36+
elseif req.body.__TYPE == "xml" then
37+
req.headers["content-type"] = { "application/xml" }
38+
-- TODO: auto-set content-type header for external body
39+
end
40+
end
41+
end
2442
end
2543
})
2644
vim.api.nvim_create_autocmd("User", {

lua/rest-nvim/config/check.lua

+2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ 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" },
32+
["request.hooks.set_content_type"] = { cfg.request.hooks.set_content_type, "boolean" },
3133
response = { cfg.response, "table" },
3234
["response.hooks"] = { cfg.response.hooks, "table" },
3335
clients = { cfg.clients, "table" },

lua/rest-nvim/config/default.lua

+6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
---@mod rest-nvim.config.default rest.nvim default configuration
22

3+
local api = require("rest-nvim.api")
4+
35
---rest.nvim default configuration
46
---@class rest.Config
57
local default_config = {
@@ -14,6 +16,10 @@ local default_config = {
1416
hooks = {
1517
---@type boolean Encode URL before making request
1618
encode_url = true,
19+
---@type string Set `User-Agent` header when it is empty
20+
user_agent = "rest.nvim v" .. api.VERSION,
21+
---@type boolean Set `Content-Type` header when it is empty and body is provided
22+
set_content_type = true,
1723
},
1824
},
1925
---@class rest.Config.Response

lua/rest-nvim/config/init.lua

+5
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ local config
3838
---@class rest.Opts.Request.Hooks
3939
--- Encode URL before making request (Default: `true`)
4040
---@field encode_url? boolean
41+
--- Set `User-Agent` header when it is empty. Set as empty string to disable.
42+
--- (Default: `rest.nvim {version}`)
43+
---@field user_agent? string
44+
--- Set `Content-Type` header when it is empty but request body is provided
45+
---@field set_content_type? boolean
4146

4247
---@class rest.Opts.Response
4348
--- Default response hooks (aka. request handlers) configuration

spec/minimum_init.lua

+5
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ end
1313
vim.opt.runtimepath:append(rest_nvim_dir)
1414
vim.g.rest_nvim = {
1515
_log_level = vim.log.levels.INFO,
16+
request = {
17+
hooks = {
18+
set_user_agent = false,
19+
}
20+
},
1621
cookies = {
1722
path = "/tmp/rest-nvim.cookies"
1823
},

0 commit comments

Comments
 (0)