@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,167 +1,223 @@
1
- import { encode } from "js-base64";
2
- import { createSearchParams } from "../../../libs/http.js";
3
- const csharpHttpclient = {
4
- target: "csharp",
5
- client: "httpclient",
6
- title: "HttpClient",
7
- generate(request, configuration) {
8
- const normalizedRequest = {
9
- method: "GET",
10
- url: "",
11
- ...request
12
- };
13
- normalizedRequest.method = normalizedRequest.method.toUpperCase();
14
- const searchParams = createSearchParams(normalizedRequest.queryString);
15
- const queryString = searchParams.size ? `?${searchParams.toString()}` : "";
16
- const url = `${normalizedRequest.url}${queryString}`;
17
- const lines = [];
18
- lines.push("using var client = new HttpClient();");
19
- lines.push("");
20
- const httpMethod = getHttpMethod(normalizedRequest.method);
21
- lines.push(`var request = new HttpRequestMessage(${httpMethod}, "${url}");`);
22
- addHeadersAndAuth(lines, normalizedRequest, configuration);
23
- addBodyContent(lines, normalizedRequest);
24
- lines.push("");
25
- lines.push("using var response = await client.SendAsync(request);");
26
- return lines.join("\n");
27
- }
1
+ import { encode } from 'js-base64';
2
+ import { createSearchParams } from '../../../libs/http.js';
3
+ /**
4
+ * csharp/httpclient
5
+ */
6
+ export const csharpHttpclient = {
7
+ target: 'csharp',
8
+ client: 'httpclient',
9
+ title: 'HttpClient',
10
+ generate(request, configuration) {
11
+ // Defaults
12
+ const normalizedRequest = {
13
+ method: 'GET',
14
+ url: '',
15
+ ...request,
16
+ };
17
+ // Normalization
18
+ normalizedRequest.method = normalizedRequest.method.toUpperCase();
19
+ // Build URL with query string
20
+ const searchParams = createSearchParams(normalizedRequest.queryString);
21
+ const queryString = searchParams.size ? `?${searchParams.toString()}` : '';
22
+ const url = `${normalizedRequest.url}${queryString}`;
23
+ // Start building the snippet
24
+ const lines = [];
25
+ // HttpClient declaration
26
+ lines.push('using var client = new HttpClient();');
27
+ lines.push('');
28
+ // HttpRequestMessage
29
+ const httpMethod = getHttpMethod(normalizedRequest.method);
30
+ lines.push(`var request = new HttpRequestMessage(${httpMethod}, "${url}");`);
31
+ // Headers and auth
32
+ addHeadersAndAuth(lines, normalizedRequest, configuration);
33
+ // Body content
34
+ addBodyContent(lines, normalizedRequest);
35
+ // Send request and read response
36
+ lines.push('');
37
+ lines.push('using var response = await client.SendAsync(request);');
38
+ return lines.join('\n');
39
+ },
28
40
  };
41
+ /**
42
+ * Convert HTTP method to HttpMethod constant
43
+ */
29
44
  function getHttpMethod(method) {
30
- switch (method) {
31
- case "GET":
32
- return "HttpMethod.Get";
33
- case "POST":
34
- return "HttpMethod.Post";
35
- case "PUT":
36
- return "HttpMethod.Put";
37
- case "DELETE":
38
- return "HttpMethod.Delete";
39
- case "PATCH":
40
- return "HttpMethod.Patch";
41
- case "HEAD":
42
- return "HttpMethod.Head";
43
- case "OPTIONS":
44
- return "HttpMethod.Options";
45
- default:
46
- return `new HttpMethod("${method}")`;
47
- }
45
+ switch (method) {
46
+ case 'GET':
47
+ return 'HttpMethod.Get';
48
+ case 'POST':
49
+ return 'HttpMethod.Post';
50
+ case 'PUT':
51
+ return 'HttpMethod.Put';
52
+ case 'DELETE':
53
+ return 'HttpMethod.Delete';
54
+ case 'PATCH':
55
+ return 'HttpMethod.Patch';
56
+ case 'HEAD':
57
+ return 'HttpMethod.Head';
58
+ case 'OPTIONS':
59
+ return 'HttpMethod.Options';
60
+ default:
61
+ return `new HttpMethod("${method}")`;
62
+ }
48
63
  }
64
+ /**
65
+ * Add headers and authentication to the request
66
+ */
49
67
  function addHeadersAndAuth(lines, request, configuration) {
50
- const headers = request.headers || [];
51
- const cookies = request.cookies || [];
52
- const authHeader = headers.find((h) => h.name.toLowerCase() === "authorization");
53
- if (authHeader) {
54
- const [scheme, parameter] = authHeader.value.split(" ", 2);
55
- if (scheme && parameter) {
56
- lines.push(`request.Headers.Authorization = new AuthenticationHeaderValue("${scheme}", "${parameter}");`);
68
+ const headers = request.headers || [];
69
+ const cookies = request.cookies || [];
70
+ // Check for explicit Authorization header first
71
+ const authHeader = headers.find((h) => h.name.toLowerCase() === 'authorization');
72
+ if (authHeader) {
73
+ const [scheme, parameter] = authHeader.value.split(' ', 2);
74
+ if (scheme && parameter) {
75
+ lines.push(`request.Headers.Authorization = new AuthenticationHeaderValue("${scheme}", "${parameter}");`);
76
+ }
77
+ }
78
+ else if (configuration?.auth?.username && configuration?.auth?.password) {
79
+ // Use configuration auth if no explicit header
80
+ const credentials = encode(`${configuration.auth.username}:${configuration.auth.password}`);
81
+ lines.push(`request.Headers.Authorization = new AuthenticationHeaderValue("Basic", "${credentials}");`);
82
+ }
83
+ // Process other headers (keep only the last value for duplicates)
84
+ const processedHeaders = new Map();
85
+ for (const header of headers) {
86
+ const name = header.name;
87
+ const value = header.value;
88
+ if (name.toLowerCase() === 'authorization') {
89
+ // Already handled above
90
+ continue;
91
+ }
92
+ processedHeaders.set(name, value);
57
93
  }
58
- } else if (configuration?.auth?.username && configuration?.auth?.password) {
59
- const credentials = encode(`${configuration.auth.username}:${configuration.auth.password}`);
60
- lines.push(`request.Headers.Authorization = new AuthenticationHeaderValue("Basic", "${credentials}");`);
61
- }
62
- const processedHeaders = /* @__PURE__ */ new Map();
63
- for (const header of headers) {
64
- const name = header.name;
65
- const value = header.value;
66
- if (name.toLowerCase() === "authorization") {
67
- continue;
94
+ for (const [name, value] of processedHeaders) {
95
+ if (name.toLowerCase() === 'accept' && isMediaType(value)) {
96
+ lines.push(`request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("${value}"));`);
97
+ }
98
+ else if (name.toLowerCase() === 'content-type' && request.postData) {
99
+ // Content-Type will be set on content object
100
+ continue;
101
+ }
102
+ else {
103
+ lines.push(`request.Headers.TryAddWithoutValidation("${name}", "${value}");`);
104
+ }
68
105
  }
69
- processedHeaders.set(name, value);
70
- }
71
- for (const [name, value] of processedHeaders) {
72
- if (name.toLowerCase() === "accept" && isMediaType(value)) {
73
- lines.push(`request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("${value}"));`);
74
- } else if (name.toLowerCase() === "content-type" && request.postData) {
75
- continue;
76
- } else {
77
- lines.push(`request.Headers.TryAddWithoutValidation("${name}", "${value}");`);
106
+ // Add cookies
107
+ if (cookies.length > 0) {
108
+ const cookieString = cookies.map((cookie) => `${cookie.name}=${cookie.value}`).join('; ');
109
+ lines.push(`request.Headers.TryAddWithoutValidation("Cookie", "${cookieString}");`);
78
110
  }
79
- }
80
- if (cookies.length > 0) {
81
- const cookieString = cookies.map((cookie) => `${cookie.name}=${cookie.value}`).join("; ");
82
- lines.push(`request.Headers.TryAddWithoutValidation("Cookie", "${cookieString}");`);
83
- }
84
111
  }
112
+ /**
113
+ * Add body content to the request
114
+ */
85
115
  function addBodyContent(lines, request) {
86
- if (!request.postData) {
87
- return;
88
- }
89
- const { mimeType, text, params } = request.postData;
90
- if (mimeType === "application/json" && text) {
91
- try {
92
- const jsonData = JSON.parse(text);
93
- const prettyJson = JSON.stringify(jsonData, null, 2);
94
- const rawStringLiteral = createRawStringLiteral(prettyJson);
95
- lines.push("request.Content = new StringContent(");
96
- lines.push(`${rawStringLiteral},`);
97
- lines.push('System.Text.Encoding.UTF8, "application/json");');
98
- } catch {
99
- const rawStringLiteral = createRawStringLiteral(text);
100
- lines.push("request.Content = new StringContent(");
101
- lines.push(`${rawStringLiteral},`);
102
- lines.push('System.Text.Encoding.UTF8, "application/json");');
116
+ if (!request.postData) {
117
+ return;
118
+ }
119
+ const { mimeType, text, params } = request.postData;
120
+ if (mimeType === 'application/json' && text) {
121
+ try {
122
+ const jsonData = JSON.parse(text);
123
+ const prettyJson = JSON.stringify(jsonData, null, 2);
124
+ const rawStringLiteral = createRawStringLiteral(prettyJson);
125
+ lines.push('request.Content = new StringContent(');
126
+ lines.push(`${rawStringLiteral},`);
127
+ lines.push('System.Text.Encoding.UTF8, "application/json");');
128
+ }
129
+ catch {
130
+ const rawStringLiteral = createRawStringLiteral(text);
131
+ lines.push('request.Content = new StringContent(');
132
+ lines.push(`${rawStringLiteral},`);
133
+ lines.push('System.Text.Encoding.UTF8, "application/json");');
134
+ }
135
+ }
136
+ else if (mimeType === 'application/x-www-form-urlencoded' && params) {
137
+ // Check for duplicate field names
138
+ const fieldNames = params.map((p) => p.name);
139
+ const hasDuplicates = fieldNames.length !== new Set(fieldNames).size;
140
+ if (hasDuplicates) {
141
+ // Use List<KeyValuePair> for duplicates
142
+ lines.push('var formParams = new List<KeyValuePair<string, string>>');
143
+ lines.push('{');
144
+ for (const param of params) {
145
+ lines.push(` new("${param.name}", "${param.value}"),`);
146
+ }
147
+ lines.push('};');
148
+ lines.push('request.Content = new FormUrlEncodedContent(formParams);');
149
+ }
150
+ else {
151
+ // Use Dictionary for clean syntax
152
+ lines.push('var formParams = new Dictionary<string, string>');
153
+ lines.push('{');
154
+ for (const param of params) {
155
+ lines.push(` ["${param.name}"] = "${param.value}",`);
156
+ }
157
+ lines.push('};');
158
+ lines.push('request.Content = new FormUrlEncodedContent(formParams);');
159
+ }
103
160
  }
104
- } else if (mimeType === "application/x-www-form-urlencoded" && params) {
105
- const fieldNames = params.map((p) => p.name);
106
- const hasDuplicates = fieldNames.length !== new Set(fieldNames).size;
107
- if (hasDuplicates) {
108
- lines.push("var formParams = new List<KeyValuePair<string, string>>");
109
- lines.push("{");
110
- for (const param of params) {
111
- lines.push(` new("${param.name}", "${param.value}"),`);
112
- }
113
- lines.push("};");
114
- lines.push("request.Content = new FormUrlEncodedContent(formParams);");
115
- } else {
116
- lines.push("var formParams = new Dictionary<string, string>");
117
- lines.push("{");
118
- for (const param of params) {
119
- lines.push(` ["${param.name}"] = "${param.value}",`);
120
- }
121
- lines.push("};");
122
- lines.push("request.Content = new FormUrlEncodedContent(formParams);");
161
+ else if (mimeType === 'multipart/form-data' && params) {
162
+ lines.push('var content = new MultipartFormDataContent();');
163
+ let multipartContentIndex = 0;
164
+ for (const param of params) {
165
+ if (param.fileName !== undefined) {
166
+ if (param.contentType) {
167
+ const contentName = `fileContent${multipartContentIndex++}`;
168
+ lines.push(`var ${contentName} = new StreamContent(File.OpenRead("${escapeCSharpString(param.fileName)}"));`);
169
+ lines.push(`${contentName}.Headers.ContentType = new MediaTypeHeaderValue("${param.contentType}");`);
170
+ lines.push(`content.Add(${contentName}, "${escapeCSharpString(param.name)}", "${escapeCSharpString(param.fileName)}");`);
171
+ }
172
+ else {
173
+ lines.push(`content.Add(new StreamContent(File.OpenRead("${escapeCSharpString(param.fileName)}")), "${escapeCSharpString(param.name)}", "${escapeCSharpString(param.fileName)}");`);
174
+ }
175
+ }
176
+ else {
177
+ if (param.contentType) {
178
+ const contentName = `stringContent${multipartContentIndex++}`;
179
+ lines.push(`var ${contentName} = new StringContent("${escapeCSharpString(param.value ?? '')}");`);
180
+ lines.push(`${contentName}.Headers.ContentType = new MediaTypeHeaderValue("${param.contentType}");`);
181
+ lines.push(`content.Add(${contentName}, "${escapeCSharpString(param.name)}");`);
182
+ }
183
+ else {
184
+ lines.push(`content.Add(new StringContent("${escapeCSharpString(param.value ?? '')}"), "${escapeCSharpString(param.name)}");`);
185
+ }
186
+ }
187
+ }
188
+ lines.push('request.Content = content;');
123
189
  }
124
- } else if (mimeType === "multipart/form-data" && params) {
125
- lines.push("var content = new MultipartFormDataContent();");
126
- for (const param of params) {
127
- if (param.fileName !== void 0) {
128
- lines.push(
129
- `content.Add(new StreamContent(File.OpenRead("${param.fileName}")), "${param.name}", "${param.fileName}");`
130
- );
131
- } else {
132
- lines.push(`content.Add(new StringContent("${param.value}"), "${param.name}");`);
133
- }
190
+ else if (mimeType === 'application/octet-stream' && text) {
191
+ lines.push('var content = new ByteArrayContent(System.Text.Encoding.UTF8.GetBytes("' + text.replace(/"/g, '\\"') + '"));');
192
+ lines.push('content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");');
193
+ lines.push('request.Content = content;');
194
+ }
195
+ else if (text) {
196
+ // Fallback for other content types
197
+ const rawStringLiteral = createRawStringLiteral(text);
198
+ lines.push('request.Content = new StringContent(');
199
+ lines.push(`${rawStringLiteral},`);
200
+ lines.push(`System.Text.Encoding.UTF8, "${mimeType}");`);
134
201
  }
135
- lines.push("request.Content = content;");
136
- } else if (mimeType === "application/octet-stream" && text) {
137
- lines.push(
138
- 'var content = new ByteArrayContent(System.Text.Encoding.UTF8.GetBytes("' + text.replace(/"/g, '\\"') + '"));'
139
- );
140
- lines.push('content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");');
141
- lines.push("request.Content = content;");
142
- } else if (text) {
143
- const rawStringLiteral = createRawStringLiteral(text);
144
- lines.push("request.Content = new StringContent(");
145
- lines.push(`${rawStringLiteral},`);
146
- lines.push(`System.Text.Encoding.UTF8, "${mimeType}");`);
147
- }
148
202
  }
203
+ /**
204
+ * Create a C# raw string literal with minimal quote count
205
+ */
149
206
  function createRawStringLiteral(text) {
150
- let quoteCount = 3;
151
- while (text.includes('"'.repeat(quoteCount))) {
152
- quoteCount++;
153
- }
154
- const quotes = '"'.repeat(quoteCount);
155
- return `${quotes}
156
- ${text}
157
- ${quotes}`;
207
+ // Find the minimum number of quotes needed
208
+ let quoteCount = 3;
209
+ while (text.includes('"'.repeat(quoteCount))) {
210
+ quoteCount++;
211
+ }
212
+ const quotes = '"'.repeat(quoteCount);
213
+ return `${quotes}\n${text}\n${quotes}`;
158
214
  }
215
+ function escapeCSharpString(text) {
216
+ return text.replace(/\\/g, '\\\\').replace(/"/g, '\\"');
217
+ }
218
+ /**
219
+ * Check if a value looks like a media type
220
+ */
159
221
  function isMediaType(value) {
160
- return /^[a-zA-Z0-9][a-zA-Z0-9!#$&\-\^_]*\/[a-zA-Z0-9][a-zA-Z0-9!#$&\-\^_]*(\s*;\s*[a-zA-Z0-9][a-zA-Z0-9!#$&\-\^_]*=.*)?$/.test(
161
- value
162
- );
222
+ return /^[a-zA-Z0-9][a-zA-Z0-9!#$&\-\^_]*\/[a-zA-Z0-9][a-zA-Z0-9!#$&\-\^_]*(\s*;\s*[a-zA-Z0-9][a-zA-Z0-9!#$&\-\^_]*=.*)?$/.test(value);
163
223
  }
164
- export {
165
- csharpHttpclient
166
- };
167
- //# sourceMappingURL=httpclient.js.map
@@ -1,5 +1 @@
1
- import { csharpHttpclient } from "./httpclient.js";
2
- export {
3
- csharpHttpclient
4
- };
5
- //# sourceMappingURL=index.js.map
1
+ export { csharpHttpclient } from './httpclient.js';
@@ -1,5 +1 @@
1
- import { csharpRestsharp } from "./restsharp.js";
2
- export {
3
- csharpRestsharp
4
- };
5
- //# sourceMappingURL=index.js.map
1
+ export { csharpRestsharp } from './restsharp.js';
@@ -1,14 +1,14 @@
1
- import { restsharp } from "../../../httpsnippet-lite/targets/csharp/restsharp/client.js";
2
- import { convertWithHttpSnippetLite } from "../../../utils/convertWithHttpSnippetLite.js";
3
- const csharpRestsharp = {
4
- target: "csharp",
5
- client: "restsharp",
6
- title: "RestSharp",
7
- generate(request) {
8
- return convertWithHttpSnippetLite(restsharp, request);
9
- }
1
+ import { restsharp } from '../../../httpsnippet-lite/targets/csharp/restsharp/client.js';
2
+ import { convertWithHttpSnippetLite } from '../../../utils/convertWithHttpSnippetLite.js';
3
+ /**
4
+ * csharp/restsharp
5
+ */
6
+ export const csharpRestsharp = {
7
+ target: 'csharp',
8
+ client: 'restsharp',
9
+ title: 'RestSharp',
10
+ generate(request) {
11
+ // TODO: Write an own converter
12
+ return convertWithHttpSnippetLite(restsharp, request);
13
+ },
10
14
  };
11
- export {
12
- csharpRestsharp
13
- };
14
- //# sourceMappingURL=restsharp.js.map
@@ -1,97 +1,99 @@
1
- const dartHttp = {
2
- target: "dart",
3
- client: "http",
4
- title: "Http",
5
- generate(request, options) {
6
- const normalizedRequest = {
7
- method: "GET",
8
- ...request
9
- };
10
- normalizedRequest.method = normalizedRequest.method.toUpperCase();
11
- let code = `import 'package:http/http.dart' as http;
12
-
13
- void main() async {
14
- `;
15
- let cookieHeader = "";
16
- let cookieString = "";
17
- if (normalizedRequest.cookies && normalizedRequest.cookies.length > 0) {
18
- cookieString = normalizedRequest.cookies.map((cookie) => `${encodeURIComponent(cookie.name)}=${encodeURIComponent(cookie.value)}`).join("; ");
19
- cookieHeader = ` "Cookie": "${cookieString}",
20
- `;
21
- }
22
- const headers = normalizedRequest.headers?.reduce(
23
- (acc, header) => {
24
- if (header.value && !/[; ]/.test(header.name)) {
25
- acc[header.name] = header.value;
1
+ /**
2
+ * dart/http
3
+ */
4
+ export const dartHttp = {
5
+ target: 'dart',
6
+ client: 'http',
7
+ title: 'Http',
8
+ generate(request, options) {
9
+ // Defaults
10
+ const normalizedRequest = {
11
+ method: 'GET',
12
+ ...request,
13
+ };
14
+ // Normalize method to uppercase
15
+ normalizedRequest.method = normalizedRequest.method.toUpperCase();
16
+ // Start building the Dart code
17
+ let code = `import 'package:http/http.dart' as http;\n\nvoid main() async {\n`;
18
+ // Handle cookies
19
+ let cookieHeader = '';
20
+ let cookieString = '';
21
+ if (normalizedRequest.cookies && normalizedRequest.cookies.length > 0) {
22
+ cookieString = normalizedRequest.cookies
23
+ .map((cookie) => `${encodeURIComponent(cookie.name)}=${encodeURIComponent(cookie.value)}`)
24
+ .join('; ');
25
+ cookieHeader = ` "Cookie": "${cookieString}",\n`;
26
26
  }
27
- return acc;
28
- },
29
- {}
30
- ) || {};
31
- if (options?.auth) {
32
- const { username, password } = options.auth;
33
- if (username && password) {
34
- const credentials = `${username}:${password}`;
35
- headers["Authorization"] = `'Basic ' + base64Encode(utf8.encode('${credentials}'))`;
36
- }
37
- }
38
- if (cookieHeader) {
39
- headers["Cookie"] = cookieString;
40
- }
41
- if (Object.keys(headers).length > 0) {
42
- code += " final headers = <String,String>{\n";
43
- for (const [key, value] of Object.entries(headers)) {
44
- if (value.includes("utf8.encode")) {
45
- code += ` '${key}': ${value},
46
- `;
47
- } else {
48
- code += ` '${key}': '${value}',
49
- `;
27
+ // Handle headers
28
+ const headers = normalizedRequest.headers?.reduce((acc, header) => {
29
+ if (header.value && !/[; ]/.test(header.name)) {
30
+ acc[header.name] = header.value;
31
+ }
32
+ return acc;
33
+ }, {}) || {};
34
+ // Add Authorization header if credentials are provided
35
+ if (options?.auth) {
36
+ const { username, password } = options.auth;
37
+ if (username && password) {
38
+ const credentials = `${username}:${password}`;
39
+ headers['Authorization'] = `'Basic ' + base64Encode(utf8.encode('${credentials}'))`;
40
+ }
50
41
  }
51
- }
52
- code += " };\n\n";
53
- }
54
- const queryString = normalizedRequest.queryString?.length ? "?" + normalizedRequest.queryString.map((param) => `${encodeURIComponent(param.name)}=${encodeURIComponent(param.value)}`).join("&") : "";
55
- const url = `${normalizedRequest.url}${queryString}`;
56
- let body = "";
57
- if (normalizedRequest.postData) {
58
- if (normalizedRequest.postData.mimeType === "application/json") {
59
- body = ` final body = r'${normalizedRequest.postData.text}';
60
-
61
- `;
62
- } else if (normalizedRequest.postData.mimeType === "application/x-www-form-urlencoded") {
63
- body = ` final body = '${normalizedRequest.postData.params?.map((param) => `${encodeURIComponent(param.name)}=${encodeURIComponent(param.value ?? "")}`).join("&") || ""}';
64
-
65
- `;
66
- } else if (normalizedRequest.postData.mimeType === "multipart/form-data") {
67
- body = " final body = <String,String>{\n";
68
- for (const param of normalizedRequest.postData.params || []) {
69
- const value = param.value || "";
70
- const fileName = param.fileName || "";
71
- body += ` '${param.name}': '${fileName || value}',
72
- `;
42
+ if (cookieHeader) {
43
+ headers['Cookie'] = cookieString;
73
44
  }
74
- body += " };\n\n";
75
- } else if (normalizedRequest.postData.mimeType === "application/octet-stream") {
76
- body = ` final body = '${normalizedRequest.postData.text}';
77
-
78
- `;
79
- }
80
- }
81
- if (body) {
82
- code += body;
83
- }
84
- const method = normalizedRequest.method.toLowerCase();
85
- const headersPart = Object.keys(headers).length > 0 ? ", headers: headers" : "";
86
- const bodyPart = body ? ", body: body" : "";
87
- code += ` final response = await http.${method}(Uri.parse('${url}')${headersPart}${bodyPart});
88
- `;
89
- code += " print(response.body);\n";
90
- code += "}";
91
- return code;
92
- }
93
- };
94
- export {
95
- dartHttp
45
+ if (Object.keys(headers).length > 0) {
46
+ code += ' final headers = <String,String>{\n';
47
+ for (const [key, value] of Object.entries(headers)) {
48
+ if (value.includes('utf8.encode')) {
49
+ code += ` '${key}': ${value},\n`;
50
+ }
51
+ else {
52
+ code += ` '${key}': '${value}',\n`;
53
+ }
54
+ }
55
+ code += ' };\n\n';
56
+ }
57
+ // Handle query string
58
+ const queryString = normalizedRequest.queryString?.length
59
+ ? '?' +
60
+ normalizedRequest.queryString
61
+ .map((param) => `${encodeURIComponent(param.name)}=${encodeURIComponent(param.value)}`)
62
+ .join('&')
63
+ : '';
64
+ const url = `${normalizedRequest.url}${queryString}`;
65
+ // Handle body
66
+ let body = '';
67
+ if (normalizedRequest.postData) {
68
+ if (normalizedRequest.postData.mimeType === 'application/json') {
69
+ body = ` final body = r'${normalizedRequest.postData.text}';\n\n`;
70
+ }
71
+ else if (normalizedRequest.postData.mimeType === 'application/x-www-form-urlencoded') {
72
+ body = ` final body = '${normalizedRequest.postData.params?.map((param) => `${encodeURIComponent(param.name)}=${encodeURIComponent(param.value ?? '')}`).join('&') || ''}';\n\n`;
73
+ }
74
+ else if (normalizedRequest.postData.mimeType === 'multipart/form-data') {
75
+ body = ' final body = <String,String>{\n';
76
+ for (const param of normalizedRequest.postData.params || []) {
77
+ const value = param.value || '';
78
+ const fileName = param.fileName || '';
79
+ body += ` '${param.name}': '${fileName || value}',\n`;
80
+ }
81
+ body += ' };\n\n';
82
+ }
83
+ else if (normalizedRequest.postData.mimeType === 'application/octet-stream') {
84
+ body = ` final body = '${normalizedRequest.postData.text}';\n\n`;
85
+ }
86
+ }
87
+ if (body) {
88
+ code += body;
89
+ }
90
+ // Handle method and request
91
+ const method = normalizedRequest.method.toLowerCase();
92
+ const headersPart = Object.keys(headers).length > 0 ? ', headers: headers' : '';
93
+ const bodyPart = body ? ', body: body' : '';
94
+ code += ` final response = await http.${method}(Uri.parse('${url}')${headersPart}${bodyPart});\n`;
95
+ code += ' print(response.body);\n';
96
+ code += '}';
97
+ return code;
98
+ },
96
99
  };
97
- //# sourceMappingURL=http.js.map
@@ -1,5 +1 @@
1
- import { dartHttp } from "./http.js";
2
- export {
3
- dartHttp
4
- };
5
- //# sourceMappingURL=index.js.map
1
+ export { dartHttp } from './http.js';