@scalar/snippetz 0.7.4 → 0.7.6

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 (249) 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 +206 -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 +169 -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.d.ts.map +1 -1
  57. package/dist/plugins/http/http11/http11.js +104 -85
  58. package/dist/plugins/http/http11/index.js +1 -5
  59. package/dist/plugins/java/asynchttp/asynchttp.js +13 -13
  60. package/dist/plugins/java/asynchttp/index.js +1 -5
  61. package/dist/plugins/java/nethttp/index.js +1 -5
  62. package/dist/plugins/java/nethttp/nethttp.js +13 -13
  63. package/dist/plugins/java/okhttp/index.js +1 -5
  64. package/dist/plugins/java/okhttp/okhttp.js +13 -13
  65. package/dist/plugins/java/unirest/index.js +1 -5
  66. package/dist/plugins/java/unirest/unirest.js +13 -13
  67. package/dist/plugins/js/axios/axios.js +13 -13
  68. package/dist/plugins/js/axios/index.js +1 -5
  69. package/dist/plugins/js/fetch/fetch.js +82 -68
  70. package/dist/plugins/js/fetch/index.js +1 -5
  71. package/dist/plugins/js/jquery/index.js +1 -5
  72. package/dist/plugins/js/jquery/jquery.js +13 -13
  73. package/dist/plugins/js/ofetch/index.js +1 -5
  74. package/dist/plugins/js/ofetch/ofetch.js +61 -48
  75. package/dist/plugins/js/xhr/index.js +1 -5
  76. package/dist/plugins/js/xhr/xhr.js +13 -13
  77. package/dist/plugins/kotlin/okhttp/index.js +1 -5
  78. package/dist/plugins/kotlin/okhttp/okhttp.js +13 -13
  79. package/dist/plugins/node/axios/axios.js +13 -13
  80. package/dist/plugins/node/axios/index.js +1 -5
  81. package/dist/plugins/node/fetch/fetch.js +86 -74
  82. package/dist/plugins/node/fetch/index.js +1 -5
  83. package/dist/plugins/node/ofetch/index.js +1 -5
  84. package/dist/plugins/node/ofetch/ofetch.js +61 -48
  85. package/dist/plugins/node/undici/index.js +1 -5
  86. package/dist/plugins/node/undici/undici.js +60 -47
  87. package/dist/plugins/objc/nsurlsession/index.js +1 -5
  88. package/dist/plugins/objc/nsurlsession/nsurlsession.js +13 -13
  89. package/dist/plugins/ocaml/cohttp/cohttp.js +13 -13
  90. package/dist/plugins/ocaml/cohttp/index.js +1 -5
  91. package/dist/plugins/php/curl/curl.js +143 -101
  92. package/dist/plugins/php/curl/index.js +1 -5
  93. package/dist/plugins/php/guzzle/guzzle.d.ts.map +1 -1
  94. package/dist/plugins/php/guzzle/guzzle.js +109 -83
  95. package/dist/plugins/php/guzzle/index.js +1 -5
  96. package/dist/plugins/powershell/restmethod/index.js +1 -5
  97. package/dist/plugins/powershell/restmethod/restmethod.js +13 -13
  98. package/dist/plugins/powershell/webrequest/index.js +1 -5
  99. package/dist/plugins/powershell/webrequest/webrequest.js +13 -13
  100. package/dist/plugins/python/httpx/async.js +17 -15
  101. package/dist/plugins/python/httpx/index.js +2 -7
  102. package/dist/plugins/python/httpx/sync.js +11 -12
  103. package/dist/plugins/python/python3/index.js +1 -5
  104. package/dist/plugins/python/python3/python3.js +13 -13
  105. package/dist/plugins/python/requests/index.js +1 -5
  106. package/dist/plugins/python/requests/requests.js +11 -12
  107. package/dist/plugins/python/requestsLike.d.ts.map +1 -1
  108. package/dist/plugins/python/requestsLike.js +145 -112
  109. package/dist/plugins/r/httr/httr.js +13 -13
  110. package/dist/plugins/r/httr/index.js +1 -5
  111. package/dist/plugins/ruby/native/index.js +1 -5
  112. package/dist/plugins/ruby/native/native.js +13 -13
  113. package/dist/plugins/rust/reqwest/index.js +1 -5
  114. package/dist/plugins/rust/reqwest/reqwest.js +126 -83
  115. package/dist/plugins/shell/curl/curl.d.ts.map +1 -1
  116. package/dist/plugins/shell/curl/curl.js +129 -97
  117. package/dist/plugins/shell/curl/index.js +1 -5
  118. package/dist/plugins/shell/httpie/httpie.js +13 -13
  119. package/dist/plugins/shell/httpie/index.js +1 -5
  120. package/dist/plugins/shell/wget/index.js +1 -5
  121. package/dist/plugins/shell/wget/wget.js +13 -13
  122. package/dist/plugins/swift/nsurlsession/index.js +1 -5
  123. package/dist/plugins/swift/nsurlsession/nsurlsession.js +13 -13
  124. package/dist/snippetz.js +25 -28
  125. package/dist/utils/convertWithHttpSnippetLite.js +100 -96
  126. package/dist/utils/index.js +1 -5
  127. package/package.json +8 -12
  128. package/dist/clients/index.js.map +0 -7
  129. package/dist/httpsnippet-lite/helpers/code-builder.js.map +0 -7
  130. package/dist/httpsnippet-lite/helpers/escape.js.map +0 -7
  131. package/dist/httpsnippet-lite/helpers/headers.js.map +0 -7
  132. package/dist/httpsnippet-lite/helpers/reducer.js.map +0 -7
  133. package/dist/httpsnippet-lite/helpers/shell.js.map +0 -7
  134. package/dist/httpsnippet-lite/helpers/snippet-request.js.map +0 -7
  135. package/dist/httpsnippet-lite/helpers/url.js.map +0 -7
  136. package/dist/httpsnippet-lite/targets/c/libcurl/client.js.map +0 -7
  137. package/dist/httpsnippet-lite/targets/clojure/clj_http/client.js.map +0 -7
  138. package/dist/httpsnippet-lite/targets/csharp/restsharp/client.js.map +0 -7
  139. package/dist/httpsnippet-lite/targets/go/native/client.js.map +0 -7
  140. package/dist/httpsnippet-lite/targets/java/asynchttp/client.js.map +0 -7
  141. package/dist/httpsnippet-lite/targets/java/nethttp/client.js.map +0 -7
  142. package/dist/httpsnippet-lite/targets/java/okhttp/client.js.map +0 -7
  143. package/dist/httpsnippet-lite/targets/java/unirest/client.js.map +0 -7
  144. package/dist/httpsnippet-lite/targets/javascript/axios/client.js.map +0 -7
  145. package/dist/httpsnippet-lite/targets/javascript/jquery/client.js.map +0 -7
  146. package/dist/httpsnippet-lite/targets/javascript/xhr/client.js.map +0 -7
  147. package/dist/httpsnippet-lite/targets/kotlin/okhttp/client.js.map +0 -7
  148. package/dist/httpsnippet-lite/targets/node/axios/client.js.map +0 -7
  149. package/dist/httpsnippet-lite/targets/objc/helpers.js.map +0 -7
  150. package/dist/httpsnippet-lite/targets/objc/nsurlsession/client.js.map +0 -7
  151. package/dist/httpsnippet-lite/targets/ocaml/cohttp/client.js.map +0 -7
  152. package/dist/httpsnippet-lite/targets/powershell/common.js.map +0 -7
  153. package/dist/httpsnippet-lite/targets/powershell/restmethod/client.js.map +0 -7
  154. package/dist/httpsnippet-lite/targets/powershell/webrequest/client.js.map +0 -7
  155. package/dist/httpsnippet-lite/targets/python/python3/client.js.map +0 -7
  156. package/dist/httpsnippet-lite/targets/r/httr/client.js.map +0 -7
  157. package/dist/httpsnippet-lite/targets/ruby/native/client.js.map +0 -7
  158. package/dist/httpsnippet-lite/targets/shell/httpie/client.js.map +0 -7
  159. package/dist/httpsnippet-lite/targets/shell/wget/client.js.map +0 -7
  160. package/dist/httpsnippet-lite/targets/swift/helpers.js.map +0 -7
  161. package/dist/httpsnippet-lite/targets/swift/nsurlsession/client.js.map +0 -7
  162. package/dist/httpsnippet-lite/targets/target.js.map +0 -7
  163. package/dist/index.js.map +0 -7
  164. package/dist/libs/http.js.map +0 -7
  165. package/dist/libs/javascript.js.map +0 -7
  166. package/dist/libs/php.js.map +0 -7
  167. package/dist/libs/rust.js.map +0 -7
  168. package/dist/libs/shell.js.map +0 -7
  169. package/dist/plugins/c/libcurl/index.js.map +0 -7
  170. package/dist/plugins/c/libcurl/libcurl.js.map +0 -7
  171. package/dist/plugins/clojure/clj_http/clj_http.js.map +0 -7
  172. package/dist/plugins/clojure/clj_http/index.js.map +0 -7
  173. package/dist/plugins/csharp/httpclient/httpclient.js.map +0 -7
  174. package/dist/plugins/csharp/httpclient/index.js.map +0 -7
  175. package/dist/plugins/csharp/restsharp/index.js.map +0 -7
  176. package/dist/plugins/csharp/restsharp/restsharp.js.map +0 -7
  177. package/dist/plugins/dart/http/http.js.map +0 -7
  178. package/dist/plugins/dart/http/index.js.map +0 -7
  179. package/dist/plugins/fsharp/httpclient/httpclient.js.map +0 -7
  180. package/dist/plugins/fsharp/httpclient/index.js.map +0 -7
  181. package/dist/plugins/go/native/index.js.map +0 -7
  182. package/dist/plugins/go/native/native.js.map +0 -7
  183. package/dist/plugins/http/http11/http11.js.map +0 -7
  184. package/dist/plugins/http/http11/index.js.map +0 -7
  185. package/dist/plugins/java/asynchttp/asynchttp.js.map +0 -7
  186. package/dist/plugins/java/asynchttp/index.js.map +0 -7
  187. package/dist/plugins/java/nethttp/index.js.map +0 -7
  188. package/dist/plugins/java/nethttp/nethttp.js.map +0 -7
  189. package/dist/plugins/java/okhttp/index.js.map +0 -7
  190. package/dist/plugins/java/okhttp/okhttp.js.map +0 -7
  191. package/dist/plugins/java/unirest/index.js.map +0 -7
  192. package/dist/plugins/java/unirest/unirest.js.map +0 -7
  193. package/dist/plugins/js/axios/axios.js.map +0 -7
  194. package/dist/plugins/js/axios/index.js.map +0 -7
  195. package/dist/plugins/js/fetch/fetch.js.map +0 -7
  196. package/dist/plugins/js/fetch/index.js.map +0 -7
  197. package/dist/plugins/js/jquery/index.js.map +0 -7
  198. package/dist/plugins/js/jquery/jquery.js.map +0 -7
  199. package/dist/plugins/js/ofetch/index.js.map +0 -7
  200. package/dist/plugins/js/ofetch/ofetch.js.map +0 -7
  201. package/dist/plugins/js/xhr/index.js.map +0 -7
  202. package/dist/plugins/js/xhr/xhr.js.map +0 -7
  203. package/dist/plugins/kotlin/okhttp/index.js.map +0 -7
  204. package/dist/plugins/kotlin/okhttp/okhttp.js.map +0 -7
  205. package/dist/plugins/node/axios/axios.js.map +0 -7
  206. package/dist/plugins/node/axios/index.js.map +0 -7
  207. package/dist/plugins/node/fetch/fetch.js.map +0 -7
  208. package/dist/plugins/node/fetch/index.js.map +0 -7
  209. package/dist/plugins/node/ofetch/index.js.map +0 -7
  210. package/dist/plugins/node/ofetch/ofetch.js.map +0 -7
  211. package/dist/plugins/node/undici/index.js.map +0 -7
  212. package/dist/plugins/node/undici/undici.js.map +0 -7
  213. package/dist/plugins/objc/nsurlsession/index.js.map +0 -7
  214. package/dist/plugins/objc/nsurlsession/nsurlsession.js.map +0 -7
  215. package/dist/plugins/ocaml/cohttp/cohttp.js.map +0 -7
  216. package/dist/plugins/ocaml/cohttp/index.js.map +0 -7
  217. package/dist/plugins/php/curl/curl.js.map +0 -7
  218. package/dist/plugins/php/curl/index.js.map +0 -7
  219. package/dist/plugins/php/guzzle/guzzle.js.map +0 -7
  220. package/dist/plugins/php/guzzle/index.js.map +0 -7
  221. package/dist/plugins/powershell/restmethod/index.js.map +0 -7
  222. package/dist/plugins/powershell/restmethod/restmethod.js.map +0 -7
  223. package/dist/plugins/powershell/webrequest/index.js.map +0 -7
  224. package/dist/plugins/powershell/webrequest/webrequest.js.map +0 -7
  225. package/dist/plugins/python/httpx/async.js.map +0 -7
  226. package/dist/plugins/python/httpx/index.js.map +0 -7
  227. package/dist/plugins/python/httpx/sync.js.map +0 -7
  228. package/dist/plugins/python/python3/index.js.map +0 -7
  229. package/dist/plugins/python/python3/python3.js.map +0 -7
  230. package/dist/plugins/python/requests/index.js.map +0 -7
  231. package/dist/plugins/python/requests/requests.js.map +0 -7
  232. package/dist/plugins/python/requestsLike.js.map +0 -7
  233. package/dist/plugins/r/httr/httr.js.map +0 -7
  234. package/dist/plugins/r/httr/index.js.map +0 -7
  235. package/dist/plugins/ruby/native/index.js.map +0 -7
  236. package/dist/plugins/ruby/native/native.js.map +0 -7
  237. package/dist/plugins/rust/reqwest/index.js.map +0 -7
  238. package/dist/plugins/rust/reqwest/reqwest.js.map +0 -7
  239. package/dist/plugins/shell/curl/curl.js.map +0 -7
  240. package/dist/plugins/shell/curl/index.js.map +0 -7
  241. package/dist/plugins/shell/httpie/httpie.js.map +0 -7
  242. package/dist/plugins/shell/httpie/index.js.map +0 -7
  243. package/dist/plugins/shell/wget/index.js.map +0 -7
  244. package/dist/plugins/shell/wget/wget.js.map +0 -7
  245. package/dist/plugins/swift/nsurlsession/index.js.map +0 -7
  246. package/dist/plugins/swift/nsurlsession/nsurlsession.js.map +0 -7
  247. package/dist/snippetz.js.map +0 -7
  248. package/dist/utils/convertWithHttpSnippetLite.js.map +0 -7
  249. package/dist/utils/index.js.map +0 -7
@@ -1,100 +1,132 @@
1
- import { escapeSingleQuotes } from "../../../libs/shell.js";
2
- const shellCurl = {
3
- target: "shell",
4
- client: "curl",
5
- title: "Curl",
6
- generate(request, configuration) {
7
- const normalizedRequest = {
8
- method: "GET",
9
- ...request
10
- };
11
- normalizedRequest.method = normalizedRequest.method.toUpperCase();
12
- const parts = ["curl"];
13
- const queryString = normalizedRequest.queryString?.length ? "?" + normalizedRequest.queryString.map((param) => {
14
- const encodedName = encodeURIComponent(param.name);
15
- const encodedValue = encodeURIComponent(param.value);
16
- return `${encodedName}=${encodedValue}`;
17
- }).join("&") : "";
18
- const url = `${normalizedRequest.url}${queryString}`;
19
- const hasSpecialChars = /[\s<>[\]{}|\\^%$]/.test(url);
20
- const urlPart = queryString || hasSpecialChars ? `'${url}'` : url;
21
- parts[0] = `curl ${urlPart}`;
22
- if (normalizedRequest.method !== "GET") {
23
- parts.push(`--request ${normalizedRequest.method}`);
24
- }
25
- if (configuration?.auth?.username && configuration?.auth?.password) {
26
- const authValue = escapeSingleQuotes(`${configuration.auth.username}:${configuration.auth.password}`);
27
- parts.push(`--user '${authValue}'`);
28
- }
29
- if (normalizedRequest.headers?.length) {
30
- normalizedRequest.headers.forEach((header) => {
31
- const headerValue = escapeSingleQuotes(`${header.name}: ${header.value}`);
32
- parts.push(`--header '${headerValue}'`);
33
- });
34
- const acceptEncoding = normalizedRequest.headers.find((header) => header.name.toLowerCase() === "accept-encoding");
35
- if (acceptEncoding && /gzip|deflate/.test(acceptEncoding.value)) {
36
- parts.push("--compressed");
37
- }
38
- }
39
- if (normalizedRequest.cookies?.length) {
40
- const cookieString = normalizedRequest.cookies.map((cookie) => {
41
- const encodedName = encodeURIComponent(cookie.name);
42
- const encodedValue = encodeURIComponent(cookie.value);
43
- return `${encodedName}=${encodedValue}`;
44
- }).join("; ");
45
- const escapedCookieString = escapeSingleQuotes(cookieString);
46
- parts.push(`--cookie '${escapedCookieString}'`);
47
- }
48
- if (normalizedRequest.postData) {
49
- if (normalizedRequest.postData.mimeType === "application/json") {
50
- if (normalizedRequest.postData.text) {
51
- try {
52
- const jsonData = JSON.parse(normalizedRequest.postData.text);
53
- const prettyJson = JSON.stringify(jsonData, null, 2);
54
- const escapedJson = escapeSingleQuotes(prettyJson);
55
- parts.push(`--data '${escapedJson}'`);
56
- } catch {
57
- const escapedText = escapeSingleQuotes(normalizedRequest.postData.text ?? "");
58
- parts.push(`--data '${escapedText}'`);
59
- }
1
+ import { escapeSingleQuotes } from '../../../libs/shell.js';
2
+ /**
3
+ * shell/curl
4
+ */
5
+ export const shellCurl = {
6
+ target: 'shell',
7
+ client: 'curl',
8
+ title: 'Curl',
9
+ generate(request, configuration) {
10
+ // Defaults
11
+ const normalizedRequest = {
12
+ method: 'GET',
13
+ ...request,
14
+ };
15
+ // Normalization
16
+ normalizedRequest.method = normalizedRequest.method.toUpperCase();
17
+ // Build curl command parts
18
+ const parts = ['curl'];
19
+ // URL (quote if has query parameters or special characters)
20
+ const queryString = normalizedRequest.queryString?.length
21
+ ? '?' +
22
+ normalizedRequest.queryString
23
+ .map((param) => {
24
+ // Ensure both name and value are fully URI encoded
25
+ const encodedName = encodeURIComponent(param.name);
26
+ const encodedValue = encodeURIComponent(param.value);
27
+ return `${encodedName}=${encodedValue}`;
28
+ })
29
+ .join('&')
30
+ : '';
31
+ const url = `${normalizedRequest.url}${queryString}`;
32
+ const hasSpecialChars = /[\s<>[\]{}|\\^%$]/.test(url);
33
+ const urlPart = queryString || hasSpecialChars ? `'${url}'` : url;
34
+ parts[0] = `curl ${urlPart}`;
35
+ // Method
36
+ if (normalizedRequest.method !== 'GET') {
37
+ parts.push(`--request ${normalizedRequest.method}`);
60
38
  }
61
- } else if (normalizedRequest.postData.mimeType === "application/octet-stream") {
62
- const escapedText = escapeSingleQuotes(normalizedRequest.postData.text ?? "");
63
- parts.push(`--data-binary '${escapedText}'`);
64
- } else if (normalizedRequest.postData.mimeType === "application/x-www-form-urlencoded" && normalizedRequest.postData.params) {
65
- normalizedRequest.postData.params.forEach((param) => {
66
- const escapedValue = escapeSingleQuotes(param.value ?? "");
67
- const encodedName = encodeURIComponent(param.name);
68
- const escapedName = escapeSingleQuotes(encodedName);
69
- parts.push(`--data-urlencode '${escapedName}=${escapedValue}'`);
70
- });
71
- } else if (normalizedRequest.postData.mimeType === "multipart/form-data" && normalizedRequest.postData.params) {
72
- normalizedRequest.postData.params.forEach((param) => {
73
- const escapedName = escapeSingleQuotes(param.name);
74
- if (param.fileName !== void 0) {
75
- const escapedFileName = escapeSingleQuotes(param.fileName);
76
- parts.push(`--form '${escapedName}=@${escapedFileName}'`);
77
- } else {
78
- const escapedValue = escapeSingleQuotes(param.value ?? "");
79
- parts.push(`--form '${escapedName}=${escapedValue}'`);
80
- }
81
- });
82
- } else {
83
- try {
84
- const jsonData = JSON.parse(normalizedRequest.postData.text ?? "");
85
- const prettyJson = JSON.stringify(jsonData, null, 2);
86
- const escapedJson = escapeSingleQuotes(prettyJson);
87
- parts.push(`--data '${escapedJson}'`);
88
- } catch {
89
- const escapedText = escapeSingleQuotes(normalizedRequest.postData.text ?? "");
90
- parts.push(`--data '${escapedText}'`);
39
+ // Basic Auth
40
+ if (configuration?.auth?.username && configuration?.auth?.password) {
41
+ const authValue = escapeSingleQuotes(`${configuration.auth.username}:${configuration.auth.password}`);
42
+ parts.push(`--user '${authValue}'`);
91
43
  }
92
- }
93
- }
94
- return parts.join(" \\\n ");
95
- }
96
- };
97
- export {
98
- shellCurl
44
+ // Headers
45
+ if (normalizedRequest.headers?.length) {
46
+ normalizedRequest.headers.forEach((header) => {
47
+ const headerValue = escapeSingleQuotes(`${header.name}: ${header.value}`);
48
+ parts.push(`--header '${headerValue}'`);
49
+ });
50
+ // Add compressed flag if Accept-Encoding header includes compression
51
+ const acceptEncoding = normalizedRequest.headers.find((header) => header.name.toLowerCase() === 'accept-encoding');
52
+ if (acceptEncoding && /gzip|deflate/.test(acceptEncoding.value)) {
53
+ parts.push('--compressed');
54
+ }
55
+ }
56
+ // Cookies
57
+ if (normalizedRequest.cookies?.length) {
58
+ const cookieString = normalizedRequest.cookies
59
+ .map((cookie) => {
60
+ // Encode both cookie name and value to handle special characters
61
+ const encodedName = encodeURIComponent(cookie.name);
62
+ const encodedValue = encodeURIComponent(cookie.value);
63
+ return `${encodedName}=${encodedValue}`;
64
+ })
65
+ .join('; ');
66
+ const escapedCookieString = escapeSingleQuotes(cookieString);
67
+ parts.push(`--cookie '${escapedCookieString}'`);
68
+ }
69
+ // Body
70
+ if (normalizedRequest.postData) {
71
+ if (normalizedRequest.postData.mimeType === 'application/json') {
72
+ // Pretty print JSON data
73
+ if (normalizedRequest.postData.text) {
74
+ try {
75
+ const jsonData = JSON.parse(normalizedRequest.postData.text);
76
+ const prettyJson = JSON.stringify(jsonData, null, 2);
77
+ const escapedJson = escapeSingleQuotes(prettyJson);
78
+ parts.push(`--data '${escapedJson}'`);
79
+ }
80
+ catch {
81
+ // If JSON parsing fails, use the original text
82
+ const escapedText = escapeSingleQuotes(normalizedRequest.postData.text ?? '');
83
+ parts.push(`--data '${escapedText}'`);
84
+ }
85
+ }
86
+ }
87
+ else if (normalizedRequest.postData.mimeType === 'application/octet-stream') {
88
+ const escapedText = escapeSingleQuotes(normalizedRequest.postData.text ?? '');
89
+ parts.push(`--data-binary '${escapedText}'`);
90
+ }
91
+ else if (normalizedRequest.postData.mimeType === 'application/x-www-form-urlencoded' &&
92
+ normalizedRequest.postData.params) {
93
+ // Handle URL-encoded form data
94
+ normalizedRequest.postData.params.forEach((param) => {
95
+ const escapedValue = escapeSingleQuotes(param.value ?? '');
96
+ const encodedName = encodeURIComponent(param.name);
97
+ const escapedName = escapeSingleQuotes(encodedName);
98
+ parts.push(`--data-urlencode '${escapedName}=${escapedValue}'`);
99
+ });
100
+ }
101
+ else if (normalizedRequest.postData.mimeType === 'multipart/form-data' && normalizedRequest.postData.params) {
102
+ // Handle multipart form data
103
+ normalizedRequest.postData.params.forEach((param) => {
104
+ const escapedName = escapeSingleQuotes(param.name);
105
+ const multipartValueSuffix = param.contentType ? `;type=${param.contentType}` : '';
106
+ if (param.fileName !== undefined) {
107
+ const escapedFileName = escapeSingleQuotes(`${param.fileName}${multipartValueSuffix}`);
108
+ parts.push(`--form '${escapedName}=@${escapedFileName}'`);
109
+ }
110
+ else {
111
+ const escapedValue = escapeSingleQuotes(`${param.value ?? ''}${multipartValueSuffix}`);
112
+ parts.push(`--form '${escapedName}=${escapedValue}'`);
113
+ }
114
+ });
115
+ }
116
+ else {
117
+ // Try to parse and pretty print if it's JSON, otherwise use raw text
118
+ try {
119
+ const jsonData = JSON.parse(normalizedRequest.postData.text ?? '');
120
+ const prettyJson = JSON.stringify(jsonData, null, 2);
121
+ const escapedJson = escapeSingleQuotes(prettyJson);
122
+ parts.push(`--data '${escapedJson}'`);
123
+ }
124
+ catch {
125
+ const escapedText = escapeSingleQuotes(normalizedRequest.postData.text ?? '');
126
+ parts.push(`--data '${escapedText}'`);
127
+ }
128
+ }
129
+ }
130
+ return parts.join(' \\\n ');
131
+ },
99
132
  };
100
- //# sourceMappingURL=curl.js.map
@@ -1,5 +1 @@
1
- import { shellCurl } from "./curl.js";
2
- export {
3
- shellCurl
4
- };
5
- //# sourceMappingURL=index.js.map
1
+ export { shellCurl } from './curl.js';
@@ -1,14 +1,14 @@
1
- import { httpie } from "../../../httpsnippet-lite/targets/shell/httpie/client.js";
2
- import { convertWithHttpSnippetLite } from "../../../utils/convertWithHttpSnippetLite.js";
3
- const shellHttpie = {
4
- target: "shell",
5
- client: "httpie",
6
- title: "HTTPie",
7
- generate(request) {
8
- return convertWithHttpSnippetLite(httpie, request);
9
- }
1
+ import { httpie } from '../../../httpsnippet-lite/targets/shell/httpie/client.js';
2
+ import { convertWithHttpSnippetLite } from '../../../utils/convertWithHttpSnippetLite.js';
3
+ /**
4
+ * shell/httpie
5
+ */
6
+ export const shellHttpie = {
7
+ target: 'shell',
8
+ client: 'httpie',
9
+ title: 'HTTPie',
10
+ generate(request) {
11
+ // TODO: Write an own converter
12
+ return convertWithHttpSnippetLite(httpie, request);
13
+ },
10
14
  };
11
- export {
12
- shellHttpie
13
- };
14
- //# sourceMappingURL=httpie.js.map
@@ -1,5 +1 @@
1
- import { shellHttpie } from "./httpie.js";
2
- export {
3
- shellHttpie
4
- };
5
- //# sourceMappingURL=index.js.map
1
+ export { shellHttpie } from './httpie.js';
@@ -1,5 +1 @@
1
- import { shellWget } from "./wget.js";
2
- export {
3
- shellWget
4
- };
5
- //# sourceMappingURL=index.js.map
1
+ export { shellWget } from './wget.js';
@@ -1,14 +1,14 @@
1
- import { wget } from "../../../httpsnippet-lite/targets/shell/wget/client.js";
2
- import { convertWithHttpSnippetLite } from "../../../utils/convertWithHttpSnippetLite.js";
3
- const shellWget = {
4
- target: "shell",
5
- client: "wget",
6
- title: "Wget",
7
- generate(request) {
8
- return convertWithHttpSnippetLite(wget, request);
9
- }
1
+ import { wget } from '../../../httpsnippet-lite/targets/shell/wget/client.js';
2
+ import { convertWithHttpSnippetLite } from '../../../utils/convertWithHttpSnippetLite.js';
3
+ /**
4
+ * shell/wget
5
+ */
6
+ export const shellWget = {
7
+ target: 'shell',
8
+ client: 'wget',
9
+ title: 'Wget',
10
+ generate(request) {
11
+ // TODO: Write an own converter
12
+ return convertWithHttpSnippetLite(wget, request);
13
+ },
10
14
  };
11
- export {
12
- shellWget
13
- };
14
- //# sourceMappingURL=wget.js.map
@@ -1,5 +1 @@
1
- import { swiftNsurlsession } from "./nsurlsession.js";
2
- export {
3
- swiftNsurlsession
4
- };
5
- //# sourceMappingURL=index.js.map
1
+ export { swiftNsurlsession } from './nsurlsession.js';
@@ -1,14 +1,14 @@
1
- import { nsurlsession } from "../../../httpsnippet-lite/targets/swift/nsurlsession/client.js";
2
- import { convertWithHttpSnippetLite } from "../../../utils/convertWithHttpSnippetLite.js";
3
- const swiftNsurlsession = {
4
- target: "swift",
5
- client: "nsurlsession",
6
- title: "NSURLSession",
7
- generate(request) {
8
- return convertWithHttpSnippetLite(nsurlsession, request);
9
- }
1
+ import { nsurlsession } from '../../../httpsnippet-lite/targets/swift/nsurlsession/client.js';
2
+ import { convertWithHttpSnippetLite } from '../../../utils/convertWithHttpSnippetLite.js';
3
+ /**
4
+ * swift/nsurlsession
5
+ */
6
+ export const swiftNsurlsession = {
7
+ target: 'swift',
8
+ client: 'nsurlsession',
9
+ title: 'NSURLSession',
10
+ generate(request) {
11
+ // TODO: Write an own converter
12
+ return convertWithHttpSnippetLite(nsurlsession, request);
13
+ },
10
14
  };
11
- export {
12
- swiftNsurlsession
13
- };
14
- //# sourceMappingURL=nsurlsession.js.map
package/dist/snippetz.js CHANGED
@@ -1,30 +1,27 @@
1
- import { clients } from "./clients/index.js";
2
- function snippetz() {
3
- function findPlugin(target, client) {
4
- return clients.find(({ key }) => key === target)?.clients.find((plugin) => plugin.client === client);
5
- }
6
- return {
7
- print(target, client, request) {
8
- return findPlugin(target, client)?.generate(request);
9
- },
10
- clients() {
11
- return clients;
12
- },
13
- plugins() {
14
- return clients.flatMap(
15
- ({ key, clients: clients2 }) => clients2.map((plugin) => ({
16
- target: key,
17
- client: plugin.client
18
- }))
19
- );
20
- },
21
- findPlugin,
22
- hasPlugin(target, client) {
23
- return Boolean(findPlugin(target, client));
1
+ import { clients } from './clients/index.js';
2
+ /**
3
+ * Generate code examples for HAR requests
4
+ */
5
+ export function snippetz() {
6
+ function findPlugin(target, client) {
7
+ return clients.find(({ key }) => key === target)?.clients.find((plugin) => plugin.client === client);
24
8
  }
25
- };
9
+ return {
10
+ print(target, client, request) {
11
+ return findPlugin(target, client)?.generate(request);
12
+ },
13
+ clients() {
14
+ return clients;
15
+ },
16
+ plugins() {
17
+ return clients.flatMap(({ key, clients }) => clients.map((plugin) => ({
18
+ target: key,
19
+ client: plugin.client,
20
+ })));
21
+ },
22
+ findPlugin,
23
+ hasPlugin(target, client) {
24
+ return Boolean(findPlugin(target, client));
25
+ },
26
+ };
26
27
  }
27
- export {
28
- snippetz
29
- };
30
- //# sourceMappingURL=snippetz.js.map
@@ -1,98 +1,102 @@
1
- import { reduceQueryParams } from "../libs/http.js";
2
- function convertWithHttpSnippetLite(client, request) {
3
- const urlObject = new URL(request?.url ?? "");
4
- const url = urlObject.pathname === "/" ? urlObject.origin : urlObject.toString();
5
- const harRequest = {
6
- method: request?.method ?? "GET",
7
- url,
8
- httpVersion: "HTTP/1.1",
9
- cookies: [],
10
- // Cookies are handled through headers
11
- headers: request?.headers ?? [],
12
- headersSize: -1,
13
- bodySize: -1,
14
- queryString: Array.from(urlObject.searchParams.entries()).map(([name, value]) => ({
15
- name,
16
- value
17
- })),
18
- postData: request?.postData
19
- };
20
- const allHeaders = (harRequest?.headers ?? []).reduce(
21
- (acc, header) => ({
22
- ...acc,
23
- [header.name]: header.value
24
- }),
25
- {}
26
- );
27
- const queryObj = reduceQueryParams(harRequest.queryString);
28
- const cookiesObj = (harRequest.cookies ?? []).reduce(
29
- (acc, cookie) => ({
30
- ...acc,
31
- [cookie.name]: cookie.value
32
- }),
33
- {}
34
- );
35
- const parsedUrl = new URL(harRequest.url);
36
- const uriObj = {
37
- protocol: parsedUrl.protocol,
38
- hostname: parsedUrl.hostname,
39
- host: parsedUrl.hostname,
40
- port: parsedUrl.port,
41
- pathname: parsedUrl.pathname.split("/").map((segment) => encodeURIComponent(decodeURIComponent(segment))).join("/") + parsedUrl.search,
42
- path: parsedUrl.pathname.split("/").map((segment) => encodeURIComponent(decodeURIComponent(segment))).join("/") + parsedUrl.search,
43
- search: parsedUrl.search,
44
- hash: parsedUrl.hash,
45
- href: parsedUrl.href,
46
- origin: parsedUrl.origin,
47
- password: parsedUrl.password,
48
- searchParams: parsedUrl.searchParams,
49
- username: parsedUrl.username,
50
- toString: parsedUrl.toString,
51
- toJSON: () => parsedUrl.toJSON()
52
- };
53
- const convertRequest = {
54
- url: harRequest.url,
55
- uriObj,
56
- method: harRequest.method?.toLocaleUpperCase() ?? "GET",
57
- httpVersion: harRequest.httpVersion,
58
- cookies: harRequest.cookies ?? [],
59
- headers: harRequest.headers ?? [],
60
- headersSize: harRequest.headersSize ?? 0,
61
- headersObj: allHeaders ?? {},
62
- bodySize: harRequest.bodySize ?? 0,
63
- queryString: harRequest.queryString ?? [],
64
- postData: harRequest.postData ? {
65
- mimeType: harRequest.postData.mimeType ?? "application/json",
66
- text: harRequest.postData.text ?? "",
67
- params: harRequest.postData.params ?? [],
68
- paramsObj: harRequest.postData.params?.reduce(
69
- (acc, param) => {
70
- if (param.name && param.value !== void 0) {
71
- acc[param.name] = param.value;
72
- }
73
- return acc;
74
- },
75
- {}
76
- ) ?? {}
77
- } : void 0,
78
- allHeaders: allHeaders ?? {},
79
- fullUrl: harRequest.url,
80
- queryObj: queryObj ?? {},
81
- cookiesObj: cookiesObj ?? {}
82
- };
83
- if (convertRequest.postData?.mimeType === "application/json" && convertRequest.postData.text) {
84
- try {
85
- convertRequest.postData.jsonObj = JSON.parse(convertRequest.postData.text);
86
- } catch (error) {
87
- console.error("Error parsing JSON:", error);
1
+ import { reduceQueryParams } from '../libs/http.js';
2
+ /**
3
+ * Takes a httpsnippet-lite client and converts the given request to a code example with it.
4
+ *
5
+ * @deprecated This a temporary wrapper around httpsnippet-lite. Let's write all the generators ourselves instead.
6
+ */
7
+ export function convertWithHttpSnippetLite(
8
+ // Couldn't find the proper type, there was always a mismatch.
9
+ client, request) {
10
+ const urlObject = new URL(request?.url ?? '');
11
+ // If it's just the domain, omit the trailing slash
12
+ const url = urlObject.pathname === '/' ? urlObject.origin : urlObject.toString();
13
+ const harRequest = {
14
+ method: request?.method ?? 'GET',
15
+ url,
16
+ httpVersion: 'HTTP/1.1',
17
+ cookies: [], // Cookies are handled through headers
18
+ headers: request?.headers ?? [],
19
+ headersSize: -1,
20
+ bodySize: -1,
21
+ queryString: Array.from(urlObject.searchParams.entries()).map(([name, value]) => ({
22
+ name,
23
+ value,
24
+ })),
25
+ postData: request?.postData,
26
+ };
27
+ const allHeaders = (harRequest?.headers ?? []).reduce((acc, header) => ({
28
+ ...acc,
29
+ [header.name]: header.value,
30
+ }), {});
31
+ const queryObj = reduceQueryParams(harRequest.queryString);
32
+ const cookiesObj = (harRequest.cookies ?? []).reduce((acc, cookie) => ({
33
+ ...acc,
34
+ [cookie.name]: cookie.value,
35
+ }), {});
36
+ const parsedUrl = new URL(harRequest.url);
37
+ const uriObj = {
38
+ protocol: parsedUrl.protocol,
39
+ hostname: parsedUrl.hostname,
40
+ host: parsedUrl.hostname,
41
+ port: parsedUrl.port,
42
+ pathname: parsedUrl.pathname
43
+ .split('/')
44
+ .map((segment) => encodeURIComponent(decodeURIComponent(segment)))
45
+ .join('/') + parsedUrl.search,
46
+ path: parsedUrl.pathname
47
+ .split('/')
48
+ .map((segment) => encodeURIComponent(decodeURIComponent(segment)))
49
+ .join('/') + parsedUrl.search,
50
+ search: parsedUrl.search,
51
+ hash: parsedUrl.hash,
52
+ href: parsedUrl.href,
53
+ origin: parsedUrl.origin,
54
+ password: parsedUrl.password,
55
+ searchParams: parsedUrl.searchParams,
56
+ username: parsedUrl.username,
57
+ toString: parsedUrl.toString,
58
+ toJSON: () => parsedUrl.toJSON(),
59
+ };
60
+ const convertRequest = {
61
+ url: harRequest.url,
62
+ uriObj,
63
+ method: harRequest.method?.toLocaleUpperCase() ?? 'GET',
64
+ httpVersion: harRequest.httpVersion,
65
+ cookies: harRequest.cookies ?? [],
66
+ headers: harRequest.headers ?? [],
67
+ headersSize: harRequest.headersSize ?? 0,
68
+ headersObj: allHeaders ?? {},
69
+ bodySize: harRequest.bodySize ?? 0,
70
+ queryString: harRequest.queryString ?? [],
71
+ postData: harRequest.postData
72
+ ? {
73
+ mimeType: harRequest.postData.mimeType ?? 'application/json',
74
+ text: harRequest.postData.text ?? '',
75
+ params: harRequest.postData.params ?? [],
76
+ paramsObj: harRequest.postData.params?.reduce((acc, param) => {
77
+ if (param.name && param.value !== undefined) {
78
+ acc[param.name] = param.value;
79
+ }
80
+ return acc;
81
+ }, {}) ?? {},
82
+ }
83
+ : undefined,
84
+ allHeaders: allHeaders ?? {},
85
+ fullUrl: harRequest.url,
86
+ queryObj: queryObj ?? {},
87
+ cookiesObj: cookiesObj ?? {},
88
+ };
89
+ // If the request is a JSON request, parse the text as JSON
90
+ if (convertRequest.postData?.mimeType === 'application/json' && convertRequest.postData.text) {
91
+ try {
92
+ convertRequest.postData.jsonObj = JSON.parse(convertRequest.postData.text);
93
+ }
94
+ catch (error) {
95
+ console.error('Error parsing JSON:', error);
96
+ }
88
97
  }
89
- }
90
- if (typeof client.convert === "function") {
91
- return client.convert(convertRequest);
92
- }
93
- return "";
98
+ if (typeof client.convert === 'function') {
99
+ return client.convert(convertRequest);
100
+ }
101
+ return '';
94
102
  }
95
- export {
96
- convertWithHttpSnippetLite
97
- };
98
- //# sourceMappingURL=convertWithHttpSnippetLite.js.map
@@ -1,5 +1 @@
1
- import { convertWithHttpSnippetLite } from "./convertWithHttpSnippetLite.js";
2
- export {
3
- convertWithHttpSnippetLite
4
- };
5
- //# sourceMappingURL=index.js.map
1
+ export { convertWithHttpSnippetLite } from './convertWithHttpSnippetLite.js';