一个非常快速、强大、可扩展且异步的 Neovim HTTP 客户端,使用 Lua 编写。
rest.nvim 默认使用其自身用纯 Lua 编写的 curl 封装器,以及 tree-sitter
解析器来解析 http 文件。因此,你可以运行并获取与你在
编辑器中所见完全一致且详细的结果!
除此之外,你还可以编写与外部 HTTP 客户端(如 postman CLI)的集成。
[!IMPORTANT]
如果你遇到问题,请 报告它们,以便我们共同修复 :)
特性
- 易于使用
- 友好、有序且功能丰富的请求结果窗口
- 快速运行时,并附带关于你请求的统计信息
- 设置自定义的预请求和后置请求钩子,以动态地与数据交互
- 轻松基于响应设置环境变量,以便稍后重用数据
- 基于 Tree-sitter 的解析和语法高亮,以实现速度和完美准确性
- 使用原生
gq命令格式化响应体 - 在 HTTP 文件中使用动态/环境变量和 Lua 脚本的可能性
- 保存接收到的 cookies 并自动加载它们
安装
[!NOTE] 如果你在 安装此插件时遇到任何问题,请查看 此故障排除指南。
依赖项
- Neovim >= 0.10.1
curl- tree-sitter-http(如果你使用
lazy.nvim)
rocks.nvim(推荐)
:Rocks install rest.nvim
lazy.nvim
{
"rest-nvim/rest.nvim",
dependencies = {
"nvim-treesitter/nvim-treesitter",
opts = function (_, opts)
opts.ensure_installed = opts.ensure_installed or {}
table.insert(opts.ensure_installed, "http")
end,
}
}
[!IMPORTANT]
你需要高于 v11 的 lazy.nvim 来安装 rockspec 依赖
设置
无需调用 .setup()!
只需通过 vim.g.rest_nvim 设置你的选项。它在内部有完整的文档和类型定义,因此你在自动补全时能获得良好的体验 :)
---@type rest.Opts
vim.g.rest_nvim = {
-- ...
}
[!NOTE]
您还可以查阅
:h rest-nvim.config获取文档。
默认配置
---rest.nvim default configuration
---@class rest.Config
local default_config = {
---@type table<string, fun():string> Table of custom dynamic variables
custom_dynamic_variables = {},
---@class rest.Config.Request
request = {
---@type boolean Skip SSL verification, useful for unknown certificates
skip_ssl_verification = false,
---Default request hooks
---@class rest.Config.Request.Hooks
hooks = {
---@type boolean Encode URL before making request
encode_url = true,
---@type string Set `User-Agent` header when it is empty
user_agent = "rest.nvim v" .. require("rest-nvim.api").VERSION,
---@type boolean Set `Content-Type` header when it is empty and body is provided
set_content_type = true,
},
},
---@class rest.Config.Response
response = {
---Default response hooks
---@class rest.Config.Response.Hooks
hooks = {
---@type boolean Decode the request URL segments on response UI to improve readability
decode_url = true,
---@type boolean Format the response body using `gq` command
format = true,
},
},
---@class rest.Config.Clients
clients = {
---@class rest.Config.Clients.Curl
curl = {
---Statistics to be shown, takes cURL's `--write-out` flag variables
---See `man curl` for `--write-out` flag
---@type RestStatisticsStyle[]
statistics = {
{ id = "time_total", winbar = "take", title = "Time taken" },
{ id = "size_download", winbar = "size", title = "Download size" },
},
---Curl-secific request/response hooks
---@class rest.Config.Clients.Curl.Opts
opts = {
---@type boolean Add `--compressed` argument when `Accept-Encoding` header includes
---`gzip`
set_compressed = false,
---@type table<string, Certificate> Table containing certificates for each domains
certificates = {},
},
},
},
---@class rest.Config.Cookies
cookies = {
---@type boolean Whether enable cookies support or not
enable = true,
---@type string Cookies file path
path = vim.fs.joinpath(vim.fn.stdpath("data") --[[@as string]], "rest-nvim.cookies"),
},
---@class rest.Config.Env
env = {
---@type boolean
enable = true,
---@type string
pattern = ".*%.env.*",
---@type fun():string[]
find = function()
local config = require("rest-nvim.config")
return vim.fs.find(function(name, _)
return name:match(config.env.pattern)
end, {
path = vim.fn.getcwd(),
type = "file",
limit = math.huge,
})
end,
},
---@class rest.Config.UI
ui = {
---@type boolean Whether to set winbar to result panes
winbar = true,
---@class rest.Config.UI.Keybinds
keybinds = {
---@type string Mapping for cycle to previous result pane
prev = "H",
---@type string Mapping for cycle to next result pane
next = "L",
},
},
---@class rest.Config.Highlight
highlight = {
---@type boolean Whether current request highlighting is enabled or not
enable = true,
---@type number Duration time of the request highlighting in milliseconds
timeout = 750,
},
---@see vim.log.levels
---@type integer log level
_log_level = vim.log.levels.WARN,
}
用法
创建一个新的 http 文件或打开一个现有文件,然后运行 :Rest run {name} 命令,或者
只需将光标置于请求上,然后直接运行 :Rest run。
HTTP 文件语法
Method Request-URI HTTP-Version
Header-field: Header-value
Request-Body
[!NOTE] rest.nvim 遵循 intellij 的 http client 规范 用于
.http文件语法。你可以在 spec/examples 目录中找到使用示例。
键位绑定
默认情况下 rest.nvim 没有任何键位映射,除了结果缓冲区,因此不会与你现有的任何键位映射发生冲突。
命令
| 用户命令 | 行为 |
|---|---|
:Rest open | 打开结果面板 |
:Rest run | 运行光标下的请求 |
:Rest run {name} | 运行名为 {name} 的请求 |
:Rest last | 运行上一个请求 |
:Rest logs | 编辑日志文件 |
:Rest cookies | 编辑 cookies 文件 |
:Rest env show | 显示当前 .http 文件注册的 dotenv 文件 |
:Rest env select | 使用 vim.ui.select() 选择并注册 .env 文件 |
:Rest env set {path} | 将 .env 文件注册到当前 .http 文件 |
[!INFO] 所有打开新窗口的
:Rest子命令都支持command-modifiers(:h command-modifiers)。 例如,你可以运行:hor Rest open以水平分割方式打开结果面板。
有关更多信息,请参阅 :h rest-nvim.commands
Lua 脚本
http://localhost:8000
# @lang=lua
> {%
local json = vim.json.decode(response.body)
json.data = "overwritten"
response.body = vim.json.encode(json)
%}
在任意 script 元素上方添加 # @lang=lua 注释。
未包含 @lang 的脚本将被解析为 javascript 代码,以符合 http spec。
扩展
Telescope 扩展
rest.nvim 提供了一个 [telescope.nvim] 扩展,用于选择环境变量文件,
你可以使用以下代码片段加载并使用它:
-- first load extension
require("telescope").load_extension("rest")
-- then use it, you can also use the `:Telescope rest select_env` command
require("telescope").extensions.rest.select_env()
这是扩展功能运行的预览 :)
映射
- Enter: 选择 Env 文件
- Ctrl + O: 编辑 Env 文件
配置
config.env.pattern: 用于 env 文件模式 (lua-pattern)
Lualine 组件
我们还有一个 lualine 组件,用于获取你选择的 env 文件!
别担心,它只会显示在 HTTP 文件下。
-- Just add a component in your lualine config
{
sections = {
lualine_x = {
"rest"
}
}
}
-- To use a custom icon and color
{
sections = {
lualine_x = {
{
"rest",
icon = "",
fg = "#428890"
}
}
}
}
这是组件运行效果的预览 :)
贡献
- Fork 它 (https://github.com/rest-nvim/rest.nvim/fork)
- 创建你的功能分支 (
git checkout -b my-new-feature) - 提交你的更改 (
git commit -am 'feat: add some feature') - 推送到分支 (
git push -u origin my-new-feature) - 创建一个新的 Pull Request
[!IMPORTANT]
rest.nvim 使用 语义化提交,遵循 语义化版本控制,这些有助于自动发布,请在向项目提交更改时使用此约定。
可以通过 make test 运行测试。你必须安装 luarocks 以安装依赖项。
通过 make test 的测试运行器将自动安装所有必需的依赖项。
相关软件
许可证
rest.nvim 采用 GPLv3 许可证。