Skip to content

Commit a53434e

Browse files
chore: minor type fixes
add `rest.` prefix to all classes
1 parent f8b6151 commit a53434e

File tree

13 files changed

+84
-80
lines changed

13 files changed

+84
-80
lines changed

lua/rest-nvim/client/curl/cli.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ function builder.http_version(version)
219219
end
220220

221221
---build curl request arguments based on Request object
222-
---@param request Request
222+
---@param request rest.Request
223223
---@return string[] args
224224
function builder.build(request)
225225
local args = {}
@@ -241,7 +241,7 @@ function builder.build(request)
241241
end
242242

243243
---returns future containing Result
244-
---@param request Request Request data to be passed to cURL
244+
---@param request rest.Request Request data to be passed to cURL
245245
---@return nio.control.Future future future containing rest.Response
246246
function curl.request(request)
247247
local progress_handle = progress.handle.create({

lua/rest-nvim/client/curl/libcurl.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ end
5555

5656
---Execute an HTTP request using cURL
5757
---return return nil if execution failed
58-
---@param request Request Request data to be passed to cURL
58+
---@param request rest.Request Request data to be passed to cURL
5959
---@return table? info The request information (url, method, headers, body, etc)
6060
function client.request_(request)
6161
logger.info("sending request to: " .. request.url)

lua/rest-nvim/commands.lua

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
-- HACK: what is the type of opts here?
3232

3333
---@class RestCmd
34-
---@field impl fun(args:string[], opts: any) The command implementation
34+
---@field impl fun(args:string[], opts: table?) The command implementation
3535
---@field complete? fun(subcmd_arg_lead: string): string[] Command completions callback, taking the lead of the subcommand's argument
3636

3737
local commands = {}
@@ -45,13 +45,11 @@ local parser = require("rest-nvim.parser")
4545
---@type table<string, RestCmd>
4646
local rest_command_tbl = {
4747
run = {
48-
-- TODO: run request by name
49-
impl = function(args, opts)
48+
impl = function(args, _opts)
5049
if #args > 1 then
5150
vim.notify("Running multiple request isn't supported yet", vim.log.levels.WARN)
5251
return
5352
elseif #args == 1 then
54-
-- TODO: get request by name
5553
request.run_by_name(args[1])
5654
return
5755
end

lua/rest-nvim/config/check.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ local function validate(tbl)
1818
end
1919

2020
---Validates the configuration
21-
---@param cfg RestConfig
21+
---@param cfg rest.Config
2222
---@return boolean is_valid
2323
---@return string|nil error_message
2424
function check.validate(cfg)

lua/rest-nvim/config/init.lua

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,50 +6,50 @@
66
---
77
---@brief ]]
88

9-
---@type RestConfig
10-
local config = {}
9+
---@type rest.Config
10+
local config
1111

1212
---@alias RestResultFormatters string|fun(body:string):string,table
1313

14-
---@class RestOptsHighlight
14+
---@class rest.Opts.Highlight
1515
--- Whether current request highlighting is enabled or not (Default: `true`)
1616
---@field enable? boolean
1717
--- Duration time of the request highlighting in milliseconds (Default: `250`)
1818
---@field timeout? number
1919

20-
---@class RestOptsResult
21-
---@field window? RestOptsResultWindow
22-
---@field behavior? RestOptsResultBehavior
23-
---@field keybinds? RestOptsResultKeybinds
20+
---@class rest.Opts.Result
21+
---@field window? rest.Opts.Result.Window
22+
---@field behavior? rest.Opts.Result.Behavior
23+
---@field keybinds? rest.Opts.Result.Keybinds
2424

25-
---@class RestOptsResultWindow
25+
---@class rest.Opts.Result.Window
2626
--- Open request results in a horizontal split (Default: `false`)
2727
---@field horizontal? boolean
2828
---@field enter? boolean
2929

30-
---@class RestOptsResultBehavior
30+
---@class rest.Opts.Result.Behavior
3131
---@field decode_url boolean
32-
---@field statistics RestOptsStatistics
32+
---@field statistics rest.Opts.Statistics
3333
---@field formatters table<string,RestResultFormatters>
3434

35-
---@class RestOptsStatistics
35+
---@class rest.Opts.Statistics
3636
---@field enable boolean
37-
---@field stats table<string,RestOptsResultStatStyle>
37+
---@field stats table<string,rest.Opts.Result.Stat.Style>
3838

39-
---@class RestOptsResultKeybinds
39+
---@class rest.Opts.Result.Keybinds
4040
--- Mapping for cycle to previous result pane (Default: `"H"`)
4141
---@field prev? string
4242
--- Mapping for cycle to next result pane (Default: `"L"`)
4343
---@field next? string
4444

45-
---@class RestOptsResultStatStyle
45+
---@class rest.Opts.Result.Stat.Style
4646
--- Title used on result pane
4747
---@field title? string
4848
--- Winbar title. Set to `false` or `nil` to not show for winbar, set to empty string
4949
--- to hide title If true, rest.nvim will use lowered `title` field
5050
---@field winbar? string|boolean
5151

52-
---@class RestOpts
52+
---@class rest.Opts
5353
--- Environment variables file pattern for telescope.nvim
5454
--- (Default: `".*env.*$"`)
5555
---@field env_pattern? string
@@ -60,15 +60,15 @@ local config = {}
6060
--- Table of custom dynamic variables
6161
---@field custom_dynamic_variables? table<string, fun():string>
6262
--- Request highlighting config
63-
---@field highlight? RestOptsHighlight
63+
---@field highlight? rest.Opts.Highlight
6464
--- Result view config
65-
---@field result? RestOptsResult
65+
---@field result? rest.Opts.Result
6666

67-
---@type RestOpts
67+
---@type rest.Opts
6868
vim.g.rest_nvim = vim.g.rest_nvim
6969

7070
---rest.nvim default configuration
71-
---@class RestConfig
71+
---@class rest.Config
7272
local default_config = {
7373
---@type string Environment variables file pattern for telescope.nvim
7474
env_pattern = ".*env.*$",
@@ -80,9 +80,9 @@ local default_config = {
8080
---@type table<string, fun():string> Table of custom dynamic variables
8181
custom_dynamic_variables = {},
8282

83-
---@class RestConfigResult
83+
---@class rest.Config.Result
8484
result = {
85-
---@class RestConfigResultWindow
85+
---@class rest.Cnofig.Result.Window
8686
window = {
8787
-- TODO: use `:horizontal` instead. see `:h command-modifiers` and opts.smods
8888
---@type boolean Open request results in a horizontal split
@@ -94,17 +94,17 @@ local default_config = {
9494
---@type boolean
9595
cookies = true,
9696
},
97-
---@class RestConfigResultBehavior
97+
---@class rest.Config.Result.Behavior
9898
behavior = {
9999
---@type boolean Whether to decode the request URL query parameters to improve readability
100100
decode_url = true,
101-
---@class RestConfigResultStats
101+
---@class rest.Config.Result.Stats
102102
statistics = {
103103
---@type boolean Whether enable statistics or not
104104
enable = true,
105105
---Statistics to be shown, takes cURL's easy getinfo constants name
106106
---@see https://curl.se/libcurl/c/curl_easy_getinfo.html
107-
---@type table<string,RestOptsResultStatStyle>
107+
---@type table<string,rest.Opts.Result.Stat.Style>
108108
stats = {
109109
total_time = { winbar = "take", title = "Time taken" },
110110
size_download_t = { winbar = "size", title = "Download size" },
@@ -133,15 +133,15 @@ local default_config = {
133133
end,
134134
},
135135
},
136-
---@class RestConfigResultKeybinds
136+
---@class rest.Config.Result.Keybinds
137137
keybinds = {
138138
---@type string Mapping for cycle to previous result pane
139139
prev = "H",
140140
---@type string Mapping for cycle to next result pane
141141
next = "L",
142142
},
143143
},
144-
---@class RestConfigHighlight
144+
---@class rest.Config.Highlight
145145
highlight = {
146146
---@type boolean Whether current request highlighting is enabled or not
147147
enable = true,
@@ -151,7 +151,7 @@ local default_config = {
151151
---@see vim.log.levels
152152
---@type integer log level
153153
_log_level = vim.log.levels.WARN,
154-
---@class RestConfigDebugInfo
154+
---@class rest.Config.DebugInfo
155155
_debug_info = {
156156
-- NOTE: default option is `nil` to prevent overwriting as empty array
157157
---@type string[]
@@ -166,7 +166,7 @@ config = vim.tbl_deep_extend("force", {
166166
unrecognized_configs = check.get_unrecognized_keys(opts, default_config),
167167
},
168168
}, default_config, opts)
169-
---@cast config RestConfig
169+
---@cast config rest.Config
170170
local ok, err = check.validate(config)
171171

172172
if not ok then

lua/rest-nvim/context.lua

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ local dotenv = require("rest-nvim.dotenv")
44
local config = require("rest-nvim.config")
55
local M = {}
66

7-
---@class Context
7+
---@class rest.Context
88
---@field vars table<string,string>
99
---@field files string[]
10-
---@field request? Request
10+
---@field request? rest.Request
1111
---@field response? rest.Response
1212
local Context = {}
1313
Context.__index = Context
@@ -40,9 +40,9 @@ local rest_variables = {
4040
end,
4141
}
4242

43-
---@return Context
43+
---@return rest.Context
4444
function Context:new()
45-
---@type Context
45+
---@type rest.Context
4646
local obj = {
4747
__index = self,
4848
vars = {},

lua/rest-nvim/init.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
local rest = {}
1010

1111
---Set up rest.nvim
12-
---@param user_configs RestOpts User configurations
12+
---@param user_configs rest.Opts User configurations
1313
function rest.setup(user_configs)
1414
-- Set up rest.nvim configurations
1515
vim.g.rest_nvim = user_configs

lua/rest-nvim/parser/dotenv.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ function M.parse(path, setter)
5252
local file_contents = utils.read_file(path)
5353
if env_ext == "json" then
5454
local ok, json_tbl = pcall(vim.json.decode, file_contents)
55-
if not ok or not type(json_tbl) == "table" or vim.islist(json_tbl) then
55+
if not ok or type(json_tbl) ~= "table" or vim.islist(json_tbl) then
5656
logger.error("failed parsing json data")
5757
return false
5858
end

lua/rest-nvim/parser/init.lua

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,6 @@ local config = require("rest-nvim.config")
1212

1313
---@alias BodyType "json"|"xml"|"external"|"form"|"graphql"
1414

15-
---@class ReqBody
16-
---@field __TYPE BodyType
17-
---@field data any
18-
1915
local NAMED_REQUEST_QUERY = vim.treesitter.query.parse("http", [[
2016
(section
2117
(request_separator
@@ -39,7 +35,7 @@ local function get_node_field_text(node, field, source)
3935
end
4036

4137
---@param src string
42-
---@param context Context
38+
---@param context rest.Context
4339
---@param encoder? fun(s:string):string
4440
---@return string
4541
---@return integer
@@ -56,7 +52,7 @@ end
5652

5753
---@param req_node TSNode Tree-sitter request node
5854
---@param source Source
59-
---@param context Context
55+
---@param context rest.Context
6056
---@return table<string,string> headers
6157
local function parse_headers(req_node, source, context)
6258
local headers = {}
@@ -74,12 +70,12 @@ end
7470

7571
---@param body_node TSNode
7672
---@param source Source
77-
---@param context Context
78-
---@return ReqBody|nil
73+
---@param context rest.Context
74+
---@return rest.Request.Body|nil
7975
function M.parse_body(body_node, source, context)
8076
local body = {}
8177
body.__TYPE = body_node:type():gsub("_%w+", "")
82-
---@cast body ReqBody
78+
---@cast body rest.Request.Body
8379
if body.__TYPE == "json" then
8480
body.data = vim.trim(vim.treesitter.get_node_text(body_node, source))
8581
body.data = expand_variables(body.data, context)
@@ -128,7 +124,7 @@ local IN_PLACE_VARIABLE_QUERY = "(variable_declaration) @inplace_variable"
128124

129125
---parse all in-place variables from source
130126
---@param source Source
131-
---@return Context ctx
127+
---@return rest.Context ctx
132128
function M.create_context(source)
133129
local query = vim.treesitter.query.parse("http", IN_PLACE_VARIABLE_QUERY)
134130
local ctx = Context:new()
@@ -184,7 +180,7 @@ end
184180

185181
---@param vd_node TSNode
186182
---@param source Source
187-
---@param ctx Context
183+
---@param ctx rest.Context
188184
function M.parse_variable_declaration(vd_node, source, ctx)
189185
vim.validate({ node = utils.ts_node_spec(vd_node, "variable_declaration") })
190186
local name = assert(get_node_field_text(vd_node, "name", source))
@@ -204,7 +200,7 @@ end
204200

205201
---@param script_node TSNode
206202
---@param source Source
207-
---@param context Context
203+
---@param context rest.Context
208204
function M.parse_pre_request_script(script_node, source, context)
209205
local node = assert(script_node:named_child(0))
210206
local str = parse_script(node, source)
@@ -213,7 +209,7 @@ end
213209

214210
---@param handler_node TSNode
215211
---@param source Source
216-
---@param context Context
212+
---@param context rest.Context
217213
function M.parse_request_handler(handler_node, source, context)
218214
local node = assert(handler_node:named_child(0))
219215
local str = parse_script(node, source)
@@ -239,8 +235,8 @@ end
239235
---failed.
240236
---@param node TSNode Tree-sitter request node
241237
---@param source Source
242-
---@param context? Context
243-
---@return Request|nil
238+
---@param context? rest.Context
239+
---@return rest.Request|nil
244240
function M.parse(node, source, context)
245241
assert(node:type() == "section")
246242
context = context or Context:new()
@@ -293,7 +289,7 @@ function M.parse(node, source, context)
293289
url = headers["host"]..url
294290
headers["host"] = nil
295291
end
296-
---@type Request
292+
---@type rest.Request
297293
return {
298294
name = name,
299295
context = context,

lua/rest-nvim/request.lua

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,24 @@ local ui = require("rest-nvim.ui.result")
1010
local nio = require("nio")
1111
local response = require("rest-nvim.response")
1212

13-
---@class Request
14-
---@field context Context
13+
---@class rest.Request.Body
14+
---@field __TYPE BodyType
15+
---@field data any
16+
17+
---@class rest.Request
18+
---@field context rest.Context
1519
---@field name? string The request identifier
1620
---@field method string The request method
1721
---@field url string The request URL
1822
---@field http_version? string The request HTTP protocol
1923
---@field headers table<string,string>
20-
---@field body? ReqBody
24+
---@field body? rest.Request.Body
2125
---@field handlers fun()[]
2226

23-
---@type Request|nil
27+
---@type rest.Request|nil
2428
local rest_nvim_last_request = nil
2529

26-
---@param req Request
30+
---@param req rest.Request
2731
local function run_request(req)
2832
logger.debug("run_request")
2933
local client = require("rest-nvim.client.curl.cli")

0 commit comments

Comments
 (0)