@scalar/snippetz 0.7.4 → 0.7.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (245) hide show
  1. package/dist/clients/index.js +168 -169
  2. package/dist/httpsnippet-lite/helpers/code-builder.js +86 -74
  3. package/dist/httpsnippet-lite/helpers/escape.js +69 -44
  4. package/dist/httpsnippet-lite/helpers/headers.js +17 -14
  5. package/dist/httpsnippet-lite/helpers/reducer.js +1 -1
  6. package/dist/httpsnippet-lite/helpers/shell.js +13 -13
  7. package/dist/httpsnippet-lite/helpers/snippet-request.js +1 -1
  8. package/dist/httpsnippet-lite/helpers/url.js +4 -8
  9. package/dist/httpsnippet-lite/targets/c/libcurl/client.js +69 -67
  10. package/dist/httpsnippet-lite/targets/clojure/clj_http/client.js +165 -154
  11. package/dist/httpsnippet-lite/targets/csharp/restsharp/client.js +35 -38
  12. package/dist/httpsnippet-lite/targets/go/native/client.js +151 -147
  13. package/dist/httpsnippet-lite/targets/java/asynchttp/client.js +32 -35
  14. package/dist/httpsnippet-lite/targets/java/nethttp/client.js +40 -39
  15. package/dist/httpsnippet-lite/targets/java/okhttp/client.js +83 -73
  16. package/dist/httpsnippet-lite/targets/java/unirest/client.js +41 -34
  17. package/dist/httpsnippet-lite/targets/javascript/axios/client.js +81 -76
  18. package/dist/httpsnippet-lite/targets/javascript/jquery/client.js +77 -71
  19. package/dist/httpsnippet-lite/targets/javascript/xhr/client.js +73 -71
  20. package/dist/httpsnippet-lite/targets/kotlin/okhttp/client.js +83 -73
  21. package/dist/httpsnippet-lite/targets/node/axios/client.js +74 -69
  22. package/dist/httpsnippet-lite/targets/objc/helpers.js +52 -33
  23. package/dist/httpsnippet-lite/targets/objc/nsurlsession/client.js +123 -121
  24. package/dist/httpsnippet-lite/targets/ocaml/cohttp/client.js +60 -49
  25. package/dist/httpsnippet-lite/targets/powershell/common.js +46 -56
  26. package/dist/httpsnippet-lite/targets/powershell/restmethod/client.js +9 -13
  27. package/dist/httpsnippet-lite/targets/powershell/webrequest/client.js +9 -13
  28. package/dist/httpsnippet-lite/targets/python/python3/client.js +85 -68
  29. package/dist/httpsnippet-lite/targets/r/httr/client.js +114 -95
  30. package/dist/httpsnippet-lite/targets/ruby/native/client.js +66 -70
  31. package/dist/httpsnippet-lite/targets/shell/httpie/client.js +121 -107
  32. package/dist/httpsnippet-lite/targets/shell/wget/client.js +48 -43
  33. package/dist/httpsnippet-lite/targets/swift/helpers.js +62 -44
  34. package/dist/httpsnippet-lite/targets/swift/nsurlsession/client.js +126 -112
  35. package/dist/httpsnippet-lite/targets/target.js +1 -1
  36. package/dist/index.js +2 -9
  37. package/dist/libs/http.js +68 -56
  38. package/dist/libs/javascript.js +77 -60
  39. package/dist/libs/php.js +59 -49
  40. package/dist/libs/rust.js +60 -33
  41. package/dist/libs/shell.js +14 -5
  42. package/dist/plugins/c/libcurl/index.js +1 -5
  43. package/dist/plugins/c/libcurl/libcurl.js +13 -13
  44. package/dist/plugins/clojure/clj_http/clj_http.js +13 -13
  45. package/dist/plugins/clojure/clj_http/index.js +1 -5
  46. package/dist/plugins/csharp/httpclient/httpclient.js +186 -150
  47. package/dist/plugins/csharp/httpclient/index.js +1 -5
  48. package/dist/plugins/csharp/restsharp/index.js +1 -5
  49. package/dist/plugins/csharp/restsharp/restsharp.js +13 -13
  50. package/dist/plugins/dart/http/http.js +95 -93
  51. package/dist/plugins/dart/http/index.js +1 -5
  52. package/dist/plugins/fsharp/httpclient/httpclient.js +156 -122
  53. package/dist/plugins/fsharp/httpclient/index.js +1 -5
  54. package/dist/plugins/go/native/index.js +1 -5
  55. package/dist/plugins/go/native/native.js +13 -13
  56. package/dist/plugins/http/http11/http11.js +103 -85
  57. package/dist/plugins/http/http11/index.js +1 -5
  58. package/dist/plugins/java/asynchttp/asynchttp.js +13 -13
  59. package/dist/plugins/java/asynchttp/index.js +1 -5
  60. package/dist/plugins/java/nethttp/index.js +1 -5
  61. package/dist/plugins/java/nethttp/nethttp.js +13 -13
  62. package/dist/plugins/java/okhttp/index.js +1 -5
  63. package/dist/plugins/java/okhttp/okhttp.js +13 -13
  64. package/dist/plugins/java/unirest/index.js +1 -5
  65. package/dist/plugins/java/unirest/unirest.js +13 -13
  66. package/dist/plugins/js/axios/axios.js +13 -13
  67. package/dist/plugins/js/axios/index.js +1 -5
  68. package/dist/plugins/js/fetch/fetch.js +82 -68
  69. package/dist/plugins/js/fetch/index.js +1 -5
  70. package/dist/plugins/js/jquery/index.js +1 -5
  71. package/dist/plugins/js/jquery/jquery.js +13 -13
  72. package/dist/plugins/js/ofetch/index.js +1 -5
  73. package/dist/plugins/js/ofetch/ofetch.js +61 -48
  74. package/dist/plugins/js/xhr/index.js +1 -5
  75. package/dist/plugins/js/xhr/xhr.js +13 -13
  76. package/dist/plugins/kotlin/okhttp/index.js +1 -5
  77. package/dist/plugins/kotlin/okhttp/okhttp.js +13 -13
  78. package/dist/plugins/node/axios/axios.js +13 -13
  79. package/dist/plugins/node/axios/index.js +1 -5
  80. package/dist/plugins/node/fetch/fetch.js +86 -74
  81. package/dist/plugins/node/fetch/index.js +1 -5
  82. package/dist/plugins/node/ofetch/index.js +1 -5
  83. package/dist/plugins/node/ofetch/ofetch.js +61 -48
  84. package/dist/plugins/node/undici/index.js +1 -5
  85. package/dist/plugins/node/undici/undici.js +60 -47
  86. package/dist/plugins/objc/nsurlsession/index.js +1 -5
  87. package/dist/plugins/objc/nsurlsession/nsurlsession.js +13 -13
  88. package/dist/plugins/ocaml/cohttp/cohttp.js +13 -13
  89. package/dist/plugins/ocaml/cohttp/index.js +1 -5
  90. package/dist/plugins/php/curl/curl.js +143 -101
  91. package/dist/plugins/php/curl/index.js +1 -5
  92. package/dist/plugins/php/guzzle/guzzle.js +103 -83
  93. package/dist/plugins/php/guzzle/index.js +1 -5
  94. package/dist/plugins/powershell/restmethod/index.js +1 -5
  95. package/dist/plugins/powershell/restmethod/restmethod.js +13 -13
  96. package/dist/plugins/powershell/webrequest/index.js +1 -5
  97. package/dist/plugins/powershell/webrequest/webrequest.js +13 -13
  98. package/dist/plugins/python/httpx/async.js +17 -15
  99. package/dist/plugins/python/httpx/index.js +2 -7
  100. package/dist/plugins/python/httpx/sync.js +11 -12
  101. package/dist/plugins/python/python3/index.js +1 -5
  102. package/dist/plugins/python/python3/python3.js +13 -13
  103. package/dist/plugins/python/requests/index.js +1 -5
  104. package/dist/plugins/python/requests/requests.js +11 -12
  105. package/dist/plugins/python/requestsLike.js +128 -112
  106. package/dist/plugins/r/httr/httr.js +13 -13
  107. package/dist/plugins/r/httr/index.js +1 -5
  108. package/dist/plugins/ruby/native/index.js +1 -5
  109. package/dist/plugins/ruby/native/native.js +13 -13
  110. package/dist/plugins/rust/reqwest/index.js +1 -5
  111. package/dist/plugins/rust/reqwest/reqwest.js +110 -83
  112. package/dist/plugins/shell/curl/curl.js +128 -97
  113. package/dist/plugins/shell/curl/index.js +1 -5
  114. package/dist/plugins/shell/httpie/httpie.js +13 -13
  115. package/dist/plugins/shell/httpie/index.js +1 -5
  116. package/dist/plugins/shell/wget/index.js +1 -5
  117. package/dist/plugins/shell/wget/wget.js +13 -13
  118. package/dist/plugins/swift/nsurlsession/index.js +1 -5
  119. package/dist/plugins/swift/nsurlsession/nsurlsession.js +13 -13
  120. package/dist/snippetz.js +25 -28
  121. package/dist/utils/convertWithHttpSnippetLite.js +100 -96
  122. package/dist/utils/index.js +1 -5
  123. package/package.json +8 -12
  124. package/dist/clients/index.js.map +0 -7
  125. package/dist/httpsnippet-lite/helpers/code-builder.js.map +0 -7
  126. package/dist/httpsnippet-lite/helpers/escape.js.map +0 -7
  127. package/dist/httpsnippet-lite/helpers/headers.js.map +0 -7
  128. package/dist/httpsnippet-lite/helpers/reducer.js.map +0 -7
  129. package/dist/httpsnippet-lite/helpers/shell.js.map +0 -7
  130. package/dist/httpsnippet-lite/helpers/snippet-request.js.map +0 -7
  131. package/dist/httpsnippet-lite/helpers/url.js.map +0 -7
  132. package/dist/httpsnippet-lite/targets/c/libcurl/client.js.map +0 -7
  133. package/dist/httpsnippet-lite/targets/clojure/clj_http/client.js.map +0 -7
  134. package/dist/httpsnippet-lite/targets/csharp/restsharp/client.js.map +0 -7
  135. package/dist/httpsnippet-lite/targets/go/native/client.js.map +0 -7
  136. package/dist/httpsnippet-lite/targets/java/asynchttp/client.js.map +0 -7
  137. package/dist/httpsnippet-lite/targets/java/nethttp/client.js.map +0 -7
  138. package/dist/httpsnippet-lite/targets/java/okhttp/client.js.map +0 -7
  139. package/dist/httpsnippet-lite/targets/java/unirest/client.js.map +0 -7
  140. package/dist/httpsnippet-lite/targets/javascript/axios/client.js.map +0 -7
  141. package/dist/httpsnippet-lite/targets/javascript/jquery/client.js.map +0 -7
  142. package/dist/httpsnippet-lite/targets/javascript/xhr/client.js.map +0 -7
  143. package/dist/httpsnippet-lite/targets/kotlin/okhttp/client.js.map +0 -7
  144. package/dist/httpsnippet-lite/targets/node/axios/client.js.map +0 -7
  145. package/dist/httpsnippet-lite/targets/objc/helpers.js.map +0 -7
  146. package/dist/httpsnippet-lite/targets/objc/nsurlsession/client.js.map +0 -7
  147. package/dist/httpsnippet-lite/targets/ocaml/cohttp/client.js.map +0 -7
  148. package/dist/httpsnippet-lite/targets/powershell/common.js.map +0 -7
  149. package/dist/httpsnippet-lite/targets/powershell/restmethod/client.js.map +0 -7
  150. package/dist/httpsnippet-lite/targets/powershell/webrequest/client.js.map +0 -7
  151. package/dist/httpsnippet-lite/targets/python/python3/client.js.map +0 -7
  152. package/dist/httpsnippet-lite/targets/r/httr/client.js.map +0 -7
  153. package/dist/httpsnippet-lite/targets/ruby/native/client.js.map +0 -7
  154. package/dist/httpsnippet-lite/targets/shell/httpie/client.js.map +0 -7
  155. package/dist/httpsnippet-lite/targets/shell/wget/client.js.map +0 -7
  156. package/dist/httpsnippet-lite/targets/swift/helpers.js.map +0 -7
  157. package/dist/httpsnippet-lite/targets/swift/nsurlsession/client.js.map +0 -7
  158. package/dist/httpsnippet-lite/targets/target.js.map +0 -7
  159. package/dist/index.js.map +0 -7
  160. package/dist/libs/http.js.map +0 -7
  161. package/dist/libs/javascript.js.map +0 -7
  162. package/dist/libs/php.js.map +0 -7
  163. package/dist/libs/rust.js.map +0 -7
  164. package/dist/libs/shell.js.map +0 -7
  165. package/dist/plugins/c/libcurl/index.js.map +0 -7
  166. package/dist/plugins/c/libcurl/libcurl.js.map +0 -7
  167. package/dist/plugins/clojure/clj_http/clj_http.js.map +0 -7
  168. package/dist/plugins/clojure/clj_http/index.js.map +0 -7
  169. package/dist/plugins/csharp/httpclient/httpclient.js.map +0 -7
  170. package/dist/plugins/csharp/httpclient/index.js.map +0 -7
  171. package/dist/plugins/csharp/restsharp/index.js.map +0 -7
  172. package/dist/plugins/csharp/restsharp/restsharp.js.map +0 -7
  173. package/dist/plugins/dart/http/http.js.map +0 -7
  174. package/dist/plugins/dart/http/index.js.map +0 -7
  175. package/dist/plugins/fsharp/httpclient/httpclient.js.map +0 -7
  176. package/dist/plugins/fsharp/httpclient/index.js.map +0 -7
  177. package/dist/plugins/go/native/index.js.map +0 -7
  178. package/dist/plugins/go/native/native.js.map +0 -7
  179. package/dist/plugins/http/http11/http11.js.map +0 -7
  180. package/dist/plugins/http/http11/index.js.map +0 -7
  181. package/dist/plugins/java/asynchttp/asynchttp.js.map +0 -7
  182. package/dist/plugins/java/asynchttp/index.js.map +0 -7
  183. package/dist/plugins/java/nethttp/index.js.map +0 -7
  184. package/dist/plugins/java/nethttp/nethttp.js.map +0 -7
  185. package/dist/plugins/java/okhttp/index.js.map +0 -7
  186. package/dist/plugins/java/okhttp/okhttp.js.map +0 -7
  187. package/dist/plugins/java/unirest/index.js.map +0 -7
  188. package/dist/plugins/java/unirest/unirest.js.map +0 -7
  189. package/dist/plugins/js/axios/axios.js.map +0 -7
  190. package/dist/plugins/js/axios/index.js.map +0 -7
  191. package/dist/plugins/js/fetch/fetch.js.map +0 -7
  192. package/dist/plugins/js/fetch/index.js.map +0 -7
  193. package/dist/plugins/js/jquery/index.js.map +0 -7
  194. package/dist/plugins/js/jquery/jquery.js.map +0 -7
  195. package/dist/plugins/js/ofetch/index.js.map +0 -7
  196. package/dist/plugins/js/ofetch/ofetch.js.map +0 -7
  197. package/dist/plugins/js/xhr/index.js.map +0 -7
  198. package/dist/plugins/js/xhr/xhr.js.map +0 -7
  199. package/dist/plugins/kotlin/okhttp/index.js.map +0 -7
  200. package/dist/plugins/kotlin/okhttp/okhttp.js.map +0 -7
  201. package/dist/plugins/node/axios/axios.js.map +0 -7
  202. package/dist/plugins/node/axios/index.js.map +0 -7
  203. package/dist/plugins/node/fetch/fetch.js.map +0 -7
  204. package/dist/plugins/node/fetch/index.js.map +0 -7
  205. package/dist/plugins/node/ofetch/index.js.map +0 -7
  206. package/dist/plugins/node/ofetch/ofetch.js.map +0 -7
  207. package/dist/plugins/node/undici/index.js.map +0 -7
  208. package/dist/plugins/node/undici/undici.js.map +0 -7
  209. package/dist/plugins/objc/nsurlsession/index.js.map +0 -7
  210. package/dist/plugins/objc/nsurlsession/nsurlsession.js.map +0 -7
  211. package/dist/plugins/ocaml/cohttp/cohttp.js.map +0 -7
  212. package/dist/plugins/ocaml/cohttp/index.js.map +0 -7
  213. package/dist/plugins/php/curl/curl.js.map +0 -7
  214. package/dist/plugins/php/curl/index.js.map +0 -7
  215. package/dist/plugins/php/guzzle/guzzle.js.map +0 -7
  216. package/dist/plugins/php/guzzle/index.js.map +0 -7
  217. package/dist/plugins/powershell/restmethod/index.js.map +0 -7
  218. package/dist/plugins/powershell/restmethod/restmethod.js.map +0 -7
  219. package/dist/plugins/powershell/webrequest/index.js.map +0 -7
  220. package/dist/plugins/powershell/webrequest/webrequest.js.map +0 -7
  221. package/dist/plugins/python/httpx/async.js.map +0 -7
  222. package/dist/plugins/python/httpx/index.js.map +0 -7
  223. package/dist/plugins/python/httpx/sync.js.map +0 -7
  224. package/dist/plugins/python/python3/index.js.map +0 -7
  225. package/dist/plugins/python/python3/python3.js.map +0 -7
  226. package/dist/plugins/python/requests/index.js.map +0 -7
  227. package/dist/plugins/python/requests/requests.js.map +0 -7
  228. package/dist/plugins/python/requestsLike.js.map +0 -7
  229. package/dist/plugins/r/httr/httr.js.map +0 -7
  230. package/dist/plugins/r/httr/index.js.map +0 -7
  231. package/dist/plugins/ruby/native/index.js.map +0 -7
  232. package/dist/plugins/ruby/native/native.js.map +0 -7
  233. package/dist/plugins/rust/reqwest/index.js.map +0 -7
  234. package/dist/plugins/rust/reqwest/reqwest.js.map +0 -7
  235. package/dist/plugins/shell/curl/curl.js.map +0 -7
  236. package/dist/plugins/shell/curl/index.js.map +0 -7
  237. package/dist/plugins/shell/httpie/httpie.js.map +0 -7
  238. package/dist/plugins/shell/httpie/index.js.map +0 -7
  239. package/dist/plugins/shell/wget/index.js.map +0 -7
  240. package/dist/plugins/shell/wget/wget.js.map +0 -7
  241. package/dist/plugins/swift/nsurlsession/index.js.map +0 -7
  242. package/dist/plugins/swift/nsurlsession/nsurlsession.js.map +0 -7
  243. package/dist/snippetz.js.map +0 -7
  244. package/dist/utils/convertWithHttpSnippetLite.js.map +0 -7
  245. package/dist/utils/index.js.map +0 -7
@@ -1,5 +1 @@
1
- import { pythonRequests } from "./requests.js";
2
- export {
3
- pythonRequests
4
- };
5
- //# sourceMappingURL=index.js.map
1
+ export { pythonRequests } from './requests.js';
@@ -1,13 +1,12 @@
1
- import { requestsLikeGenerate } from "../../../plugins/python/requestsLike.js";
2
- const pythonRequests = {
3
- target: "python",
4
- client: "requests",
5
- title: "Requests",
6
- generate(request, configuration) {
7
- return requestsLikeGenerate("requests", request, configuration);
8
- }
1
+ import { requestsLikeGenerate } from '../../../plugins/python/requestsLike.js';
2
+ /**
3
+ * python/requests
4
+ */
5
+ export const pythonRequests = {
6
+ target: 'python',
7
+ client: 'requests',
8
+ title: 'Requests',
9
+ generate(request, configuration) {
10
+ return requestsLikeGenerate('requests', request, configuration);
11
+ },
9
12
  };
10
- export {
11
- pythonRequests
12
- };
13
- //# sourceMappingURL=requests.js.map
@@ -1,121 +1,137 @@
1
- import { reduceQueryParams } from "../../libs/http.js";
1
+ import { reduceQueryParams } from '../../libs/http.js';
2
2
  const LENGTH_CONSIDERED_AS_SHORT = 40;
3
+ // Function to convert JavaScript boolean and null values to Python equivalents
3
4
  function convertToPythonSyntax(str) {
4
- const replacements = [
5
- ["true", "True"],
6
- ["false", "False"],
7
- ["null", "None"]
8
- ];
9
- let result = str;
10
- for (const [jsonVal, pythonVal] of replacements) {
11
- const patterns = [`(: )${jsonVal}(?=,|\\n)`, `^( +)${jsonVal}(?=,|\\n)`];
12
- for (const pattern of patterns) {
13
- result = result.replace(new RegExp(pattern, "gm"), `$1${pythonVal}`);
5
+ const replacements = [
6
+ ['true', 'True'],
7
+ ['false', 'False'],
8
+ ['null', 'None'],
9
+ ];
10
+ let result = str;
11
+ for (const [jsonVal, pythonVal] of replacements) {
12
+ const patterns = [`(: )${jsonVal}(?=,|\\n)`, `^( +)${jsonVal}(?=,|\\n)`];
13
+ for (const pattern of patterns) {
14
+ result = result.replace(new RegExp(pattern, 'gm'), `$1${pythonVal}`);
15
+ }
14
16
  }
15
- }
16
- return result;
17
+ return result;
17
18
  }
18
- function requestsLikeGenerate(clientVar, request, configuration) {
19
- const normalizedRequest = {
20
- url: "https://example.com",
21
- method: "get",
22
- ...request
23
- };
24
- const method = normalizedRequest.method.toLowerCase();
25
- const options = {};
26
- if (normalizedRequest.headers?.length) {
27
- options.headers = normalizedRequest.headers.reduce(
28
- (acc, header) => {
29
- if (!(header.name in acc)) {
30
- acc[header.name] = header.value;
19
+ export function requestsLikeGenerate(clientVar, request, configuration) {
20
+ // Normalize request with defaults
21
+ const normalizedRequest = {
22
+ url: 'https://example.com',
23
+ method: 'get',
24
+ ...request,
25
+ };
26
+ // Normalize method to lowercase for requests library
27
+ const method = normalizedRequest.method.toLowerCase();
28
+ // Build options object
29
+ const options = {};
30
+ // Add headers if present
31
+ if (normalizedRequest.headers?.length) {
32
+ options.headers = normalizedRequest.headers.reduce((acc, header) => {
33
+ if (!(header.name in acc)) {
34
+ acc[header.name] = header.value;
35
+ }
36
+ return acc;
37
+ }, {});
38
+ }
39
+ // Add query parameters if present
40
+ if (normalizedRequest.queryString?.length) {
41
+ options.params = reduceQueryParams(normalizedRequest.queryString);
42
+ }
43
+ // Add cookies if present
44
+ if (normalizedRequest.cookies?.length) {
45
+ options.cookies = Object.fromEntries(normalizedRequest.cookies.map((c) => [c.name, c.value]));
46
+ }
47
+ // Add auth if present
48
+ if (configuration?.auth?.username && configuration?.auth?.password) {
49
+ options.auth = [configuration.auth.username, configuration.auth.password];
50
+ }
51
+ // Handle request body
52
+ if (normalizedRequest.postData) {
53
+ const { mimeType, text, params } = normalizedRequest.postData;
54
+ if (mimeType === 'application/json' && text) {
55
+ try {
56
+ options.json = JSON.parse(text);
57
+ }
58
+ catch {
59
+ options.data = text;
60
+ }
61
+ }
62
+ else if (mimeType === 'application/octet-stream' && text) {
63
+ options.data = text; // Store raw text, we'll handle the b"..." formatting later
64
+ }
65
+ else if (mimeType === 'multipart/form-data' && params) {
66
+ const files = [];
67
+ const formData = {};
68
+ params.forEach((param) => {
69
+ if (param.fileName !== undefined) {
70
+ files.push({ key: param.name, file: `open("${param.fileName}", "rb")` });
71
+ }
72
+ else if (param.value !== undefined) {
73
+ formData[param.name] = param.value;
74
+ }
75
+ });
76
+ if (Object.keys(files).length) {
77
+ options.files = files;
78
+ }
79
+ if (Object.keys(formData).length) {
80
+ options.data = formData;
81
+ }
82
+ }
83
+ else if (mimeType === 'application/x-www-form-urlencoded' && params) {
84
+ options.data = Object.fromEntries(params.map((p) => [p.name, p.value]));
31
85
  }
32
- return acc;
33
- },
34
- {}
35
- );
36
- }
37
- if (normalizedRequest.queryString?.length) {
38
- options.params = reduceQueryParams(normalizedRequest.queryString);
39
- }
40
- if (normalizedRequest.cookies?.length) {
41
- options.cookies = Object.fromEntries(normalizedRequest.cookies.map((c) => [c.name, c.value]));
42
- }
43
- if (configuration?.auth?.username && configuration?.auth?.password) {
44
- options.auth = [configuration.auth.username, configuration.auth.password];
45
- }
46
- if (normalizedRequest.postData) {
47
- const { mimeType, text, params } = normalizedRequest.postData;
48
- if (mimeType === "application/json" && text) {
49
- try {
50
- options.json = JSON.parse(text);
51
- } catch {
52
- options.data = text;
53
- }
54
- } else if (mimeType === "application/octet-stream" && text) {
55
- options.data = text;
56
- } else if (mimeType === "multipart/form-data" && params) {
57
- const files = [];
58
- const formData = {};
59
- params.forEach((param) => {
60
- if (param.fileName !== void 0) {
61
- files.push({ key: param.name, file: `open("${param.fileName}", "rb")` });
62
- } else if (param.value !== void 0) {
63
- formData[param.name] = param.value;
86
+ }
87
+ // Format all parameters
88
+ const formattedParams = [];
89
+ // Format URL based on length
90
+ const urlParam = `"${normalizedRequest.url}"`;
91
+ if (normalizedRequest.url.length > LENGTH_CONSIDERED_AS_SHORT) {
92
+ formattedParams.push(urlParam);
93
+ }
94
+ else {
95
+ // Will be handled in the return statement for short URLs
96
+ formattedParams.push('');
97
+ }
98
+ // Format options
99
+ for (const [key, value] of Object.entries(options)) {
100
+ if (key === 'auth') {
101
+ formattedParams.push(`${key}=(${convertToPythonSyntax(JSON.stringify(value[0]))}, ${convertToPythonSyntax(JSON.stringify(value[1]))})`);
102
+ }
103
+ else if (key === 'files') {
104
+ const filesTuples = value.map(({ key, file }) => ` ("${key}", ${file})`);
105
+ const filesStr = '[\n' + filesTuples.join(',\n') + '\n ]';
106
+ formattedParams.push(`${key}=${filesStr}`);
64
107
  }
65
- });
66
- if (Object.keys(files).length) {
67
- options.files = files;
68
- }
69
- if (Object.keys(formData).length) {
70
- options.data = formData;
71
- }
72
- } else if (mimeType === "application/x-www-form-urlencoded" && params) {
73
- options.data = Object.fromEntries(params.map((p) => [p.name, p.value]));
108
+ else if (key === 'json') {
109
+ const jsonString = convertToPythonSyntax(JSON.stringify(value, null, 2)
110
+ .split('\n')
111
+ .map((line, i) => (i === 0 ? line : ' ' + line))
112
+ .join('\n'));
113
+ formattedParams.push(`${key}=${jsonString}`);
114
+ }
115
+ else if (key === 'data' && normalizedRequest.postData?.mimeType === 'application/octet-stream') {
116
+ // Special handling for binary data
117
+ formattedParams.push(`${key}=b"${value}"`);
118
+ }
119
+ else {
120
+ const str = convertToPythonSyntax(JSON.stringify(value, null, 2)
121
+ .split('\n')
122
+ .map((line, i) => (i === 0 ? line : ' ' + line))
123
+ .join('\n'));
124
+ formattedParams.push(`${key}=${str}`);
125
+ }
126
+ }
127
+ // Build the final request string with conditional URL formatting
128
+ if (normalizedRequest.url.length > LENGTH_CONSIDERED_AS_SHORT) {
129
+ return `${clientVar}.${method}(\n ${formattedParams.join(',\n ')}\n)`;
74
130
  }
75
- }
76
- const formattedParams = [];
77
- const urlParam = `"${normalizedRequest.url}"`;
78
- if (normalizedRequest.url.length > LENGTH_CONSIDERED_AS_SHORT) {
79
- formattedParams.push(urlParam);
80
- } else {
81
- formattedParams.push("");
82
- }
83
- for (const [key, value] of Object.entries(options)) {
84
- if (key === "auth") {
85
- formattedParams.push(
86
- `${key}=(${convertToPythonSyntax(JSON.stringify(value[0]))}, ${convertToPythonSyntax(JSON.stringify(value[1]))})`
87
- );
88
- } else if (key === "files") {
89
- const filesTuples = value.map(({ key: key2, file }) => ` ("${key2}", ${file})`);
90
- const filesStr = "[\n" + filesTuples.join(",\n") + "\n ]";
91
- formattedParams.push(`${key}=${filesStr}`);
92
- } else if (key === "json") {
93
- const jsonString = convertToPythonSyntax(
94
- JSON.stringify(value, null, 2).split("\n").map((line, i) => i === 0 ? line : " " + line).join("\n")
95
- );
96
- formattedParams.push(`${key}=${jsonString}`);
97
- } else if (key === "data" && normalizedRequest.postData?.mimeType === "application/octet-stream") {
98
- formattedParams.push(`${key}=b"${value}"`);
99
- } else {
100
- const str = convertToPythonSyntax(
101
- JSON.stringify(value, null, 2).split("\n").map((line, i) => i === 0 ? line : " " + line).join("\n")
102
- );
103
- formattedParams.push(`${key}=${str}`);
131
+ // For short URLs with no additional parameters, return a single-line format
132
+ if (formattedParams.length <= 1) {
133
+ return `${clientVar}.${method}(${urlParam})`;
104
134
  }
105
- }
106
- if (normalizedRequest.url.length > LENGTH_CONSIDERED_AS_SHORT) {
107
- return `${clientVar}.${method}(
108
- ${formattedParams.join(",\n ")}
109
- )`;
110
- }
111
- if (formattedParams.length <= 1) {
112
- return `${clientVar}.${method}(${urlParam})`;
113
- }
114
- return `${clientVar}.${method}(${urlParam}${formattedParams.length > 1 ? "," : ""}
115
- ${formattedParams.slice(1).join(",\n ")}
116
- )`;
135
+ // For short URLs with parameters, maintain the multi-line format
136
+ return `${clientVar}.${method}(${urlParam}${formattedParams.length > 1 ? ',' : ''}\n ${formattedParams.slice(1).join(',\n ')}\n)`;
117
137
  }
118
- export {
119
- requestsLikeGenerate
120
- };
121
- //# sourceMappingURL=requestsLike.js.map
@@ -1,14 +1,14 @@
1
- import { httr } from "../../../httpsnippet-lite/targets/r/httr/client.js";
2
- import { convertWithHttpSnippetLite } from "../../../utils/convertWithHttpSnippetLite.js";
3
- const rHttr = {
4
- target: "r",
5
- client: "httr",
6
- title: "httr",
7
- generate(request) {
8
- return convertWithHttpSnippetLite(httr, request);
9
- }
1
+ import { httr } from '../../../httpsnippet-lite/targets/r/httr/client.js';
2
+ import { convertWithHttpSnippetLite } from '../../../utils/convertWithHttpSnippetLite.js';
3
+ /**
4
+ * r/httr
5
+ */
6
+ export const rHttr = {
7
+ target: 'r',
8
+ client: 'httr',
9
+ title: 'httr',
10
+ generate(request) {
11
+ // TODO: Write an own converter
12
+ return convertWithHttpSnippetLite(httr, request);
13
+ },
10
14
  };
11
- export {
12
- rHttr
13
- };
14
- //# sourceMappingURL=httr.js.map
@@ -1,5 +1 @@
1
- import { rHttr } from "./httr.js";
2
- export {
3
- rHttr
4
- };
5
- //# sourceMappingURL=index.js.map
1
+ export { rHttr } from './httr.js';
@@ -1,5 +1 @@
1
- import { rubyNative } from "./native.js";
2
- export {
3
- rubyNative
4
- };
5
- //# sourceMappingURL=index.js.map
1
+ export { rubyNative } from './native.js';
@@ -1,14 +1,14 @@
1
- import { native } from "../../../httpsnippet-lite/targets/ruby/native/client.js";
2
- import { convertWithHttpSnippetLite } from "../../../utils/convertWithHttpSnippetLite.js";
3
- const rubyNative = {
4
- target: "ruby",
5
- client: "native",
6
- title: "net::http",
7
- generate(request) {
8
- return convertWithHttpSnippetLite(native, request);
9
- }
1
+ import { native } from '../../../httpsnippet-lite/targets/ruby/native/client.js';
2
+ import { convertWithHttpSnippetLite } from '../../../utils/convertWithHttpSnippetLite.js';
3
+ /**
4
+ * ruby/native
5
+ */
6
+ export const rubyNative = {
7
+ target: 'ruby',
8
+ client: 'native',
9
+ title: 'net::http',
10
+ generate(request) {
11
+ // TODO: Write an own converter
12
+ return convertWithHttpSnippetLite(native, request);
13
+ },
10
14
  };
11
- export {
12
- rubyNative
13
- };
14
- //# sourceMappingURL=native.js.map
@@ -1,5 +1 @@
1
- import { rustReqwest } from "./reqwest.js";
2
- export {
3
- rustReqwest
4
- };
5
- //# sourceMappingURL=index.js.map
1
+ export { rustReqwest } from './reqwest.js';
@@ -1,96 +1,123 @@
1
- import { buildQueryString, buildUrl, normalizeRequest, processHeaders } from "../../../libs/http.js";
2
- import { createChain, formatJson, indent, wrapInDoubleQuotes } from "../../../libs/rust.js";
3
- const rustReqwest = {
4
- target: "rust",
5
- client: "reqwest",
6
- title: "reqwest",
7
- generate(request, options) {
8
- if (!request) {
9
- return "";
10
- }
11
- const normalizedRequest = normalizeRequest(request);
12
- const queryString = buildQueryString(normalizedRequest.queryString);
13
- const url = buildUrl(normalizedRequest.url || "", queryString);
14
- const headers = processHeaders(normalizedRequest);
15
- const chainedCalls = [];
16
- const authCall = createAuthCall(options?.auth);
17
- if (authCall) {
18
- chainedCalls.push(authCall);
19
- }
20
- chainedCalls.push(...createHeaderCalls(headers));
21
- const bodyCall = createBodyCall(normalizedRequest.postData);
22
- if (bodyCall) {
23
- chainedCalls.push(bodyCall);
24
- }
25
- return buildRustCode(url, normalizedRequest.method, chainedCalls);
26
- }
1
+ import { buildQueryString, buildUrl, normalizeRequest, processHeaders } from '../../../libs/http.js';
2
+ import { createChain, formatJson, indent, wrapInDoubleQuotes } from '../../../libs/rust.js';
3
+ /**
4
+ * rust/reqwest plugin for generating Rust reqwest HTTP client code
5
+ */
6
+ export const rustReqwest = {
7
+ target: 'rust',
8
+ client: 'reqwest',
9
+ title: 'reqwest',
10
+ generate(request, options) {
11
+ if (!request) {
12
+ return '';
13
+ }
14
+ // Normalization
15
+ const normalizedRequest = normalizeRequest(request);
16
+ // Query string
17
+ const queryString = buildQueryString(normalizedRequest.queryString);
18
+ const url = buildUrl(normalizedRequest.url || '', queryString);
19
+ // Headers and cookies
20
+ const headers = processHeaders(normalizedRequest);
21
+ // Chained calls
22
+ const chainedCalls = [];
23
+ // Auth
24
+ const authCall = createAuthCall(options?.auth);
25
+ if (authCall) {
26
+ chainedCalls.push(authCall);
27
+ }
28
+ // Headers
29
+ chainedCalls.push(...createHeaderCalls(headers));
30
+ // Body
31
+ const bodyCall = createBodyCall(normalizedRequest.postData);
32
+ if (bodyCall) {
33
+ chainedCalls.push(bodyCall);
34
+ }
35
+ // Code
36
+ return buildRustCode(url, normalizedRequest.method, chainedCalls);
37
+ },
27
38
  };
39
+ /**
40
+ * Helper function to create multipart form parts with proper indentation
41
+ */
28
42
  const createMultipartPart = (param) => {
29
- if (param.fileName) {
30
- return [
31
- indent(2, `let part = reqwest::multipart::Part::text(${wrapInDoubleQuotes(param.value || "")})`),
32
- indent(3, `.file_name(${wrapInDoubleQuotes(param.fileName)});`),
33
- indent(2, `form = form.part(${wrapInDoubleQuotes(param.name)}, part);`)
34
- ].join("\n");
35
- }
36
- return indent(2, `form = form.text(${wrapInDoubleQuotes(param.name)}, ${wrapInDoubleQuotes(param.value || "")});`);
43
+ if (param.fileName) {
44
+ return [
45
+ indent(2, `let part = reqwest::multipart::Part::text(${wrapInDoubleQuotes(param.value || '')})`),
46
+ indent(3, `.file_name(${wrapInDoubleQuotes(param.fileName)});`),
47
+ indent(2, `form = form.part(${wrapInDoubleQuotes(param.name)}, part);`),
48
+ ].join('\n');
49
+ }
50
+ return indent(2, `form = form.text(${wrapInDoubleQuotes(param.name)}, ${wrapInDoubleQuotes(param.value || '')});`);
37
51
  };
52
+ /**
53
+ * Creates authentication chained call if credentials are provided
54
+ */
38
55
  const createAuthCall = (auth) => {
39
- if (!auth?.username || !auth?.password) {
40
- return null;
41
- }
42
- return createChain("basic_auth", wrapInDoubleQuotes(auth.username), wrapInDoubleQuotes(auth.password));
56
+ if (!auth?.username || !auth?.password) {
57
+ return null;
58
+ }
59
+ return createChain('basic_auth', wrapInDoubleQuotes(auth.username), wrapInDoubleQuotes(auth.password));
43
60
  };
61
+ /**
62
+ * Creates header chained calls from headers object
63
+ */
44
64
  const createHeaderCalls = (headers) => {
45
- return Object.entries(headers).map(
46
- ([key, value]) => createChain("header", wrapInDoubleQuotes(key), wrapInDoubleQuotes(value))
47
- );
65
+ return Object.entries(headers).map(([key, value]) => createChain('header', wrapInDoubleQuotes(key), wrapInDoubleQuotes(value)));
48
66
  };
67
+ /**
68
+ * Creates body chained call based on content type
69
+ */
49
70
  const createBodyCall = (postData) => {
50
- if (!postData) {
51
- return null;
52
- }
53
- const { mimeType, text, params } = postData;
54
- switch (mimeType) {
55
- case "application/json": {
56
- const formattedJson = formatJson(text);
57
- return createChain("json", `&serde_json::json!(${formattedJson})`);
71
+ if (!postData) {
72
+ return null;
58
73
  }
59
- case "application/x-www-form-urlencoded": {
60
- const formData = params?.map((param) => `(${wrapInDoubleQuotes(param.name)}, ${wrapInDoubleQuotes(param.value || "")})`).join(", ") || "";
61
- return createChain("form", `&[${formData}]`);
74
+ const { mimeType, text, params } = postData;
75
+ switch (mimeType) {
76
+ case 'application/json': {
77
+ const formattedJson = formatJson(text);
78
+ return createChain('json', `&serde_json::json!(${formattedJson})`);
79
+ }
80
+ case 'application/x-www-form-urlencoded': {
81
+ const formData = params
82
+ ?.map((param) => `(${wrapInDoubleQuotes(param.name)}, ${wrapInDoubleQuotes(param.value || '')})`)
83
+ .join(', ') || '';
84
+ return createChain('form', `&[${formData}]`);
85
+ }
86
+ case 'multipart/form-data': {
87
+ const formParts = params?.map(createMultipartPart).join('\n') || '';
88
+ const multipartBlock = [
89
+ '.multipart({',
90
+ indent(2, 'let mut form = reqwest::multipart::Form::new();'),
91
+ formParts,
92
+ indent(3, 'form'),
93
+ indent(2, '})'),
94
+ ].join('\n');
95
+ return indent(1, multipartBlock);
96
+ }
97
+ default:
98
+ return createChain('body', wrapInDoubleQuotes(text || ''));
62
99
  }
63
- case "multipart/form-data": {
64
- const formParts = params?.map(createMultipartPart).join("\n") || "";
65
- const multipartBlock = [
66
- ".multipart({",
67
- indent(2, "let mut form = reqwest::multipart::Form::new();"),
68
- formParts,
69
- indent(3, "form"),
70
- indent(2, "})")
71
- ].join("\n");
72
- return indent(1, multipartBlock);
73
- }
74
- default:
75
- return createChain("body", wrapInDoubleQuotes(text || ""));
76
- }
77
100
  };
101
+ /**
102
+ * Builds the complete Rust code by assembling all code
103
+ */
78
104
  const buildRustCode = (url, method, chainedCalls) => {
79
- const code = ["let client = reqwest::Client::new();", ""];
80
- if (chainedCalls.length > 0) {
81
- code.push("let request = client");
82
- code.push(indent(1, `.${method.toLowerCase()}(${wrapInDoubleQuotes(url)})`));
83
- code.push(...chainedCalls);
84
- } else {
85
- code.push(`let request = client.${method.toLowerCase()}(${wrapInDoubleQuotes(url)})`);
86
- }
87
- const lastPart = code[code.length - 1];
88
- code[code.length - 1] = lastPart + ";";
89
- code.push("");
90
- code.push("let response = request.send().await?;");
91
- return code.join("\n");
92
- };
93
- export {
94
- rustReqwest
105
+ const code = ['let client = reqwest::Client::new();', ''];
106
+ // Add chained calls with proper formatting
107
+ if (chainedCalls.length > 0) {
108
+ code.push('let request = client');
109
+ code.push(indent(1, `.${method.toLowerCase()}(${wrapInDoubleQuotes(url)})`));
110
+ // Add a newline before the first chained call
111
+ code.push(...chainedCalls);
112
+ }
113
+ else {
114
+ code.push(`let request = client.${method.toLowerCase()}(${wrapInDoubleQuotes(url)})`);
115
+ }
116
+ // Add semicolon to the last chained call
117
+ const lastPart = code[code.length - 1];
118
+ code[code.length - 1] = lastPart + ';';
119
+ // Add response handling
120
+ code.push('');
121
+ code.push('let response = request.send().await?;');
122
+ return code.join('\n');
95
123
  };
96
- //# sourceMappingURL=reqwest.js.map