Skip to content

Commit ec41cec

Browse files
fix: minor issues
1 parent 5fa9afc commit ec41cec

File tree

8 files changed

+92
-520
lines changed

8 files changed

+92
-520
lines changed

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ local parser = {}
4747

4848
---@package
4949
---@param str string
50-
---@return {version:string,code:number,status:string}
50+
---@return rest.Response.status
5151
function parser.parse_verbose_status(str)
5252
local version, code = str:match("^(%S+) (%d+)")
5353
return {
@@ -91,6 +91,7 @@ local VERBOSE_PREFIX_RES_HEADER = "<"
9191
local VERBOSE_PREFIX_RES_BODY = "{"
9292

9393
---@param lines string[]
94+
---@return rest.Response
9495
function parser.parse_verbose(lines)
9596
local response = {
9697
headers = {},
@@ -241,7 +242,7 @@ end
241242

242243
---returns future containing Result
243244
---@param request Request Request data to be passed to cURL
244-
---@return nio.control.Future
245+
---@return nio.control.Future future future containing rest.Response
245246
function curl.request(request)
246247
local progress_handle = progress.handle.create({
247248
title = "Fetching",

lua/rest-nvim/context.lua

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ local M = {}
88
---@field vars table<string,string>
99
---@field files string[]
1010
---@field request? Request
11-
---@field response? any
11+
---@field response? rest.Response
1212
local Context = {}
1313
Context.__index = Context
1414

@@ -47,6 +47,7 @@ function Context:new()
4747
__index = self,
4848
vars = {},
4949
files = {},
50+
---@diagnostic disable-next-line: missing-fields
5051
response = {}, -- create response table here to pass the reference first
5152
}
5253
setmetatable(obj, self)

lua/rest-nvim/response.lua

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
local response = {}
2+
3+
local config = require("rest-nvim.config")
4+
local logger = require("rest-nvim.logger")
5+
6+
---@class rest.Response
7+
---@field status rest.Response.status
8+
---@field body string?
9+
---@field headers table<string,string>
10+
11+
---@class rest.Response.status
12+
---@field code number
13+
---@field version string
14+
15+
-- TODO: format asynchronously
16+
17+
---Try format the result body
18+
---@param content_type string?
19+
---@param body string
20+
---@return string[]
21+
function response.try_format_body(content_type, body)
22+
---@type string
23+
local res_type
24+
if content_type then
25+
res_type = vim.split(content_type, "/")[2]:gsub(";.*", "")
26+
end
27+
if res_type == "octet-stream" then
28+
return { "Binary answer" }
29+
else
30+
local formatters = config.result.behavior.formatters
31+
local fmt = formatters[res_type]
32+
if fmt then
33+
if type(fmt) == "function" then
34+
elseif vim.fn.executable(fmt[1] or fmt) then
35+
local cmd = type(fmt) == "string" and { fmt } or fmt
36+
---@cast cmd string[]
37+
local sc = vim.system(cmd, { stdin = body }):wait()
38+
if sc.code == 0 and sc.stdout then
39+
body = sc.stdout --[[@as string]]
40+
else
41+
logger.error("Error running formatter '" .. fmt .. "' on response body:\n" .. sc.stderr)
42+
vim.notify("Formatting response body failed. See `:Rest logs` for more info", vim.log.levels.ERROR)
43+
end
44+
end
45+
elseif res_type ~= nil then
46+
local msg = (
47+
"Could not find a formatter for the body type "
48+
.. res_type
49+
.. " returned in the request, the results will not be formatted"
50+
)
51+
logger.info(msg)
52+
vim.notify(msg, vim.log.levels.INFO)
53+
end
54+
return vim.split(body, "\n")
55+
end
56+
end
57+
58+
return response

0 commit comments

Comments
 (0)