File tree Expand file tree Collapse file tree 5 files changed +37
-1
lines changed
Expand file tree Collapse file tree 5 files changed +37
-1
lines changed Original file line number Diff line number Diff 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" , {
Original file line number Diff line number Diff 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" },
Original file line number Diff line number Diff line change 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
57local 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
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change 1313vim .opt .runtimepath :append (rest_nvim_dir )
1414vim .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 },
You can’t perform that action at this time.
0 commit comments