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