66---
77--- @brief ]]
88
9+ --- @type RestConfig
910local 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
7172local 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+ )
161176end
162177
163178return config
0 commit comments