File tree 5 files changed +37
-1
lines changed
5 files changed +37
-1
lines changed Original file line number Diff line number Diff line change @@ -18,9 +18,27 @@ function autocmds.setup()
18
18
local config = require (" rest-nvim.config" )
19
19
local utils = require (" rest-nvim.utils" )
20
20
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
22
23
req .url = utils .escape (req .url , true )
23
24
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
24
42
end
25
43
})
26
44
vim .api .nvim_create_autocmd (" User" , {
Original file line number Diff line number Diff line change @@ -28,6 +28,8 @@ function check.validate(cfg)
28
28
[" request.skip_ssl_verification" ] = { cfg .request .skip_ssl_verification , " boolean" },
29
29
[" request.hooks" ] = { cfg .request .hooks , " table" },
30
30
[" 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" },
31
33
response = { cfg .response , " table" },
32
34
[" response.hooks" ] = { cfg .response .hooks , " table" },
33
35
clients = { cfg .clients , " table" },
Original file line number Diff line number Diff line change 1
1
--- @mod rest-nvim.config.default rest.nvim default configuration
2
2
3
+ local api = require (" rest-nvim.api" )
4
+
3
5
--- rest.nvim default configuration
4
6
--- @class rest.Config
5
7
local default_config = {
@@ -14,6 +16,10 @@ local default_config = {
14
16
hooks = {
15
17
--- @type boolean Encode URL before making request
16
18
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 ,
17
23
},
18
24
},
19
25
--- @class rest.Config.Response
Original file line number Diff line number Diff line change @@ -38,6 +38,11 @@ local config
38
38
--- @class rest.Opts.Request.Hooks
39
39
--- Encode URL before making request (Default: `true`)
40
40
--- @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
41
46
42
47
--- @class rest.Opts.Response
43
48
--- Default response hooks (aka. request handlers) configuration
Original file line number Diff line number Diff line change 13
13
vim .opt .runtimepath :append (rest_nvim_dir )
14
14
vim .g .rest_nvim = {
15
15
_log_level = vim .log .levels .INFO ,
16
+ request = {
17
+ hooks = {
18
+ set_user_agent = false ,
19
+ }
20
+ },
16
21
cookies = {
17
22
path = " /tmp/rest-nvim.cookies"
18
23
},
You can’t perform that action at this time.
0 commit comments