1
+ -- luacheck: push ignore 631
2
+
1
3
--[[
2
4
Curl Wrapper
3
5
@@ -28,6 +30,9 @@ see test/plenary/curl_spec.lua for examples.
28
30
author = github.com/tami5
29
31
]]
30
32
--
33
+ --- @alias PlenaryCurlMethod fun ( url : string | PlenaryCurlOptions , opts ?: PlenaryCurlOptions ): PlenaryCurlResponse | PlenaryJob | string[]
34
+
35
+ -- luacheck: pop
31
36
32
37
local util , parse = {}, {}
33
38
@@ -41,6 +46,8 @@ local compat = require "plenary.compat"
41
46
-- Utils ----------------------------------------------------
42
47
---- ---------------------------------------------------------
43
48
49
+ --- @param str string | integer
50
+ --- @return string | integer
44
51
util .url_encode = function (str )
45
52
if type (str ) ~= " number" then
46
53
str = str :gsub (" \r ?\n " , " \r\n " )
@@ -54,12 +61,20 @@ util.url_encode = function(str)
54
61
end
55
62
end
56
63
64
+ --- @param kv table<string , string>
65
+ --- @param prefix string
66
+ --- @param sep string
67
+ --- @return string[]
57
68
util .kv_to_list = function (kv , prefix , sep )
58
69
return compat .flatten (F .kv_map (function (kvp )
59
70
return { prefix , kvp [1 ] .. sep .. kvp [2 ] }
60
71
end , kv ))
61
72
end
62
73
74
+ --- @param kv table<string , string>
75
+ --- @param sep ? string
76
+ --- @param kvsep string
77
+ --- @return string
63
78
util .kv_to_str = function (kv , sep , kvsep )
64
79
return F .join (
65
80
F .kv_map (function (kvp )
@@ -69,6 +84,7 @@ util.kv_to_str = function(kv, sep, kvsep)
69
84
)
70
85
end
71
86
87
+ --- @return { [1] : string , [2] : string }
72
88
util .gen_dump_path = function ()
73
89
local path
74
90
local id = string.gsub (" xxxx4xxx" , " [xy]" , function (l )
87
103
-- Parsers ----------------------------------------------------
88
104
---- -----------------------------------------------------------
89
105
106
+ --- comment
107
+ --- @param t ? table<string , string>
108
+ --- @return string[] ?
90
109
parse .headers = function (t )
91
110
if not t then
92
111
return
93
112
end
113
+ --- @param str string
114
+ --- @return string
94
115
local upper = function (str )
95
116
return string.gsub (" " .. str , " %W%l" , string.upper ):sub (2 )
96
117
end
97
118
return util .kv_to_list (
98
119
(function ()
120
+ --- @type table<string , string>
99
121
local normilzed = {}
100
122
for k , v in pairs (t ) do
101
123
normilzed [upper (k :gsub (" _" , " %-" ))] = v
@@ -107,13 +129,17 @@ parse.headers = function(t)
107
129
)
108
130
end
109
131
132
+ --- @param t ? table<string , string>
133
+ --- @return string[] ?
110
134
parse .data_body = function (t )
111
135
if not t then
112
136
return
113
137
end
114
138
return util .kv_to_list (t , " -d" , " =" )
115
139
end
116
140
141
+ --- @param xs ? string | table<string , string>
142
+ --- @return string[] ?
117
143
parse .raw_body = function (xs )
118
144
if not xs then
119
145
return
@@ -125,20 +151,26 @@ parse.raw_body = function(xs)
125
151
end
126
152
end
127
153
154
+ --- @param t ? table<string , string>
155
+ --- @return string[] ?
128
156
parse .form = function (t )
129
157
if not t then
130
158
return
131
159
end
132
160
return util .kv_to_list (t , " -F" , " =" )
133
161
end
134
162
163
+ --- @param t ? table<string , string>
164
+ --- @return string ?
135
165
parse .curl_query = function (t )
136
166
if not t then
137
167
return
138
168
end
139
169
return util .kv_to_str (t , " &" , " =" )
140
170
end
141
171
172
+ --- @param s ? string
173
+ --- @return { [1] : " -I" | " -X" , [2] : string ? }?
142
174
parse .method = function (s )
143
175
if not s then
144
176
return
@@ -150,39 +182,50 @@ parse.method = function(s)
150
182
end
151
183
end
152
184
185
+ --- @param p ? string
186
+ --- @return { [1] : " -d" , [2] : string }?
153
187
parse .file = function (p )
154
188
if not p then
155
189
return
156
190
end
157
191
return { " -d" , " @" .. P .expand (P .new (p )) }
158
192
end
159
193
194
+ --- @param xs string | table<string , string>
195
+ --- @return { [1] : " -u" , [2] : string }?
160
196
parse .auth = function (xs )
161
197
if not xs then
162
198
return
163
199
end
164
200
return { " -u" , type (xs ) == " table" and util .kv_to_str (xs , nil , " :" ) or xs }
165
201
end
166
202
203
+ --- @param xs string
204
+ --- @param q table<string , string>
205
+ --- @return string ?
167
206
parse .url = function (xs , q )
168
207
if not xs then
169
208
return
170
209
end
171
- q = parse .curl_query (q )
210
+ local query = parse .curl_query (q )
172
211
if type (xs ) == " string" then
173
- return q and xs .. " ?" .. q or xs
212
+ return query and xs .. " ?" .. query or xs
174
213
elseif type (xs ) == " table" then
175
214
error " Low level URL definition is not supported."
176
215
end
177
216
end
178
217
218
+ --- @param s string ?
219
+ --- @return { [1] : " -H" , [2] : string }?
179
220
parse .accept_header = function (s )
180
221
if not s then
181
222
return
182
223
end
183
224
return { " -H" , " Accept: " .. s }
184
225
end
185
226
227
+ --- @param s string ?
228
+ --- @return { [1] : string }?
186
229
parse .http_version = function (s )
187
230
if not s then
188
231
return
198
241
199
242
-- Parse Request -------------------------------------------
200
243
---- --------------------------------------------------------
244
+ --- @class PlenaryCurlOptions
245
+ --- @field auth ? string | table<string , string> Basic request auth , ' user:pass' , or {" user" , " pass" }
246
+ --- @field body ? string | string[] The request body
247
+ --- @field dry_run ? boolean whether to return the args to be ran through curl.
248
+ --- @field form ? table<string , string> request form
249
+ --- @field http_version ? string HTTP version to use : ' HTTP/0.9' , ' HTTP/1.0' , ' HTTP/1.1' , ' HTTP/2' , or ' HTTP/3'
250
+ --- @field insecure ? boolean Allow insecure server connections
251
+ --- @field output ? string where to download something.
252
+ --- @field proxy ? string [protocol : //] host[ : port] Use this proxy
253
+ --- @field query ? table<string , string> url query , append after the url
254
+ --- @field raw ? string[] any additonal curl args , it must be an array /list.
255
+ --- @field timeout ? integer request timeout in mseconds
256
+ --- @field url ? string The url to make the request to.
257
+ --- @field accept ? string
258
+ --- @field callback ? fun ( response : PlenaryCurlResponse )
259
+ --- @field compressed ? boolean
260
+ --- @field data ? string[]
261
+ --- @field dump ? string
262
+ --- @field headers ? string[]
263
+ --- @field in_file ? string
264
+ --- @field method ? string
265
+ --- @field on_error ? fun ( err : { message : string , stderr : string , exit : integer })
266
+ --- @field raw_body ? string
267
+ --- @field stream ? PlenaryJobCallback
268
+
269
+ --- @param opts PlenaryCurlOptions
270
+ --- @return string[] result , PlenaryCurlOptions opts
201
271
parse .request = function (opts )
202
272
if opts .body then
203
- local b = opts .body
273
+ local b = opts .body --[[ @as string|string[] ]]
204
274
local silent_is_file = function ()
205
275
local status , result = pcall (P .is_file , P .new (b ))
206
276
return status and result
249
319
250
320
-- Parse response ------------------------------------------
251
321
---- --------------------------------------------------------
322
+ --- @class PlenaryCurlResponse
323
+ --- @field status integer
324
+ --- @field headers string[]
325
+ --- @field body string
326
+ --- @field exit integer
327
+
328
+ --- @param lines string[]
329
+ --- @param dump_path string
330
+ --- @param code integer
331
+ --- @return PlenaryCurlResponse
252
332
parse .response = function (lines , dump_path , code )
253
333
local headers = P .readlines (dump_path )
254
334
local status = tonumber (string.match (headers [1 ], " ([%w+]%d+)" ))
@@ -265,6 +345,8 @@ parse.response = function(lines, dump_path, code)
265
345
}
266
346
end
267
347
348
+ --- @param specs PlenaryCurlOptions
349
+ --- @return PlenaryCurlResponse | PlenaryJob | string[]
268
350
local request = function (specs )
269
351
local response = {}
270
352
local args , opts = parse .request (vim .tbl_extend (" force" , {
@@ -277,6 +359,7 @@ local request = function(specs)
277
359
return args
278
360
end
279
361
362
+ --- @type PlenaryJobOptions
280
363
local job_opts = {
281
364
command = vim .g .plenary_curl_bin_path or " curl" ,
282
365
args = args ,
@@ -300,7 +383,7 @@ local request = function(specs)
300
383
error (message )
301
384
end
302
385
end
303
- local output = parse .response (j :result (), opts .dump [2 ], code )
386
+ local output = parse .response (( j :result () --[[ @as string[] ]] ), opts .dump [2 ], code )
304
387
if opts .callback then
305
388
return opts .callback (output )
306
389
else
321
404
322
405
-- Main ----------------------------------------------------
323
406
---- --------------------------------------------------------
407
+
408
+ --- @class PlenaryCurl
409
+ --- @field get PlenaryCurlMethod
410
+ --- @field post PlenaryCurlMethod
411
+ --- @field put PlenaryCurlMethod
412
+ --- @field head PlenaryCurlMethod
413
+ --- @field patch PlenaryCurlMethod
414
+ --- @field delete PlenaryCurlMethod
415
+ --- @field request PlenaryCurlMethod
416
+
417
+ --- @return PlenaryCurl
324
418
return (function ()
325
419
local spec = {}
420
+ --- @param method " get" | " post" | " put" | " head" | " patch" | " delete" | " request"
421
+ --- @return PlenaryCurlMethod
326
422
local partial = function (method )
327
423
return function (url , opts )
328
424
opts = opts or {}
0 commit comments