@scalar/snippetz 0.2.6 → 0.2.8

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 (214) hide show
  1. package/dist/_virtual/client.js +133 -0
  2. package/dist/_virtual/client10.js +121 -0
  3. package/dist/_virtual/client11.js +128 -0
  4. package/dist/_virtual/client12.js +64 -0
  5. package/dist/_virtual/client13.js +128 -0
  6. package/dist/_virtual/client14.js +71 -0
  7. package/dist/_virtual/client15.js +81 -0
  8. package/dist/_virtual/client16.js +76 -0
  9. package/dist/_virtual/client17.js +81 -0
  10. package/dist/_virtual/client18.js +87 -0
  11. package/dist/_virtual/client19.js +45 -0
  12. package/dist/_virtual/client2.js +52 -0
  13. package/dist/_virtual/client20.js +44 -0
  14. package/dist/_virtual/client21.js +71 -0
  15. package/dist/_virtual/client22.js +45 -0
  16. package/dist/_virtual/client23.js +71 -0
  17. package/dist/_virtual/client24.js +122 -0
  18. package/dist/_virtual/client25.js +37 -0
  19. package/dist/_virtual/client26.js +154 -0
  20. package/dist/_virtual/client27.js +181 -0
  21. package/dist/_virtual/client28.js +43 -0
  22. package/dist/_virtual/client3.js +113 -0
  23. package/dist/_virtual/client4.js +70 -0
  24. package/dist/_virtual/client5.js +123 -0
  25. package/dist/_virtual/client6.js +166 -0
  26. package/dist/_virtual/client7.js +91 -0
  27. package/dist/_virtual/client8.js +13 -0
  28. package/dist/_virtual/client9.js +13 -0
  29. package/dist/core/types.d.ts +29 -1
  30. package/dist/core/types.d.ts.map +1 -1
  31. package/dist/core/utils/convertWithHttpSnippetLite.d.ts +7 -0
  32. package/dist/core/utils/convertWithHttpSnippetLite.d.ts.map +1 -0
  33. package/dist/core/utils/convertWithHttpSnippetLite.js +83 -0
  34. package/dist/httpsnippet-lite/dist/esm/helpers/code-builder.js +60 -0
  35. package/dist/httpsnippet-lite/dist/esm/helpers/escape.js +72 -0
  36. package/dist/httpsnippet-lite/dist/esm/helpers/headers.js +20 -0
  37. package/dist/httpsnippet-lite/dist/esm/helpers/shell.js +16 -0
  38. package/dist/httpsnippet-lite/dist/esm/targets/objc/helpers.js +56 -0
  39. package/dist/httpsnippet-lite/dist/esm/targets/php/helpers.js +35 -0
  40. package/dist/httpsnippet-lite/dist/esm/targets/powershell/common.js +51 -0
  41. package/dist/httpsnippet-lite/dist/esm/targets/python/helpers.js +61 -0
  42. package/dist/httpsnippet-lite/dist/esm/targets/swift/helpers.js +74 -0
  43. package/dist/plugins/c/libcurl/index.d.ts +2 -0
  44. package/dist/plugins/c/libcurl/index.d.ts.map +1 -0
  45. package/dist/plugins/c/libcurl/index.js +1 -0
  46. package/dist/plugins/c/libcurl/libcurl.d.ts +6 -0
  47. package/dist/plugins/c/libcurl/libcurl.d.ts.map +1 -0
  48. package/dist/plugins/c/libcurl/libcurl.js +16 -0
  49. package/dist/plugins/clojure/clj_http/clj_http.d.ts +6 -0
  50. package/dist/plugins/clojure/clj_http/clj_http.d.ts.map +1 -0
  51. package/dist/plugins/clojure/clj_http/clj_http.js +16 -0
  52. package/dist/plugins/clojure/clj_http/index.d.ts +2 -0
  53. package/dist/plugins/clojure/clj_http/index.d.ts.map +1 -0
  54. package/dist/plugins/clojure/clj_http/index.js +1 -0
  55. package/dist/plugins/csharp/httpclient/httpclient.d.ts +6 -0
  56. package/dist/plugins/csharp/httpclient/httpclient.d.ts.map +1 -0
  57. package/dist/plugins/csharp/httpclient/httpclient.js +16 -0
  58. package/dist/plugins/csharp/httpclient/index.d.ts +2 -0
  59. package/dist/plugins/csharp/httpclient/index.d.ts.map +1 -0
  60. package/dist/plugins/csharp/httpclient/index.js +1 -0
  61. package/dist/plugins/csharp/restsharp/index.d.ts +2 -0
  62. package/dist/plugins/csharp/restsharp/index.d.ts.map +1 -0
  63. package/dist/plugins/csharp/restsharp/index.js +1 -0
  64. package/dist/plugins/csharp/restsharp/restsharp.d.ts +6 -0
  65. package/dist/plugins/csharp/restsharp/restsharp.d.ts.map +1 -0
  66. package/dist/plugins/csharp/restsharp/restsharp.js +16 -0
  67. package/dist/plugins/go/native/index.d.ts +2 -0
  68. package/dist/plugins/go/native/index.d.ts.map +1 -0
  69. package/dist/plugins/go/native/index.js +1 -0
  70. package/dist/plugins/go/native/native.d.ts +6 -0
  71. package/dist/plugins/go/native/native.d.ts.map +1 -0
  72. package/dist/plugins/go/native/native.js +16 -0
  73. package/dist/plugins/http/http11/http11.d.ts +6 -0
  74. package/dist/plugins/http/http11/http11.d.ts.map +1 -0
  75. package/dist/plugins/http/http11/http11.js +16 -0
  76. package/dist/plugins/http/http11/index.d.ts +2 -0
  77. package/dist/plugins/http/http11/index.d.ts.map +1 -0
  78. package/dist/plugins/http/http11/index.js +1 -0
  79. package/dist/plugins/java/asynchttp/asynchttp.d.ts +6 -0
  80. package/dist/plugins/java/asynchttp/asynchttp.d.ts.map +1 -0
  81. package/dist/plugins/java/asynchttp/asynchttp.js +16 -0
  82. package/dist/plugins/java/asynchttp/index.d.ts +2 -0
  83. package/dist/plugins/java/asynchttp/index.d.ts.map +1 -0
  84. package/dist/plugins/java/asynchttp/index.js +1 -0
  85. package/dist/plugins/java/nethttp/index.d.ts +2 -0
  86. package/dist/plugins/java/nethttp/index.d.ts.map +1 -0
  87. package/dist/plugins/java/nethttp/index.js +1 -0
  88. package/dist/plugins/java/nethttp/nethttp.d.ts +6 -0
  89. package/dist/plugins/java/nethttp/nethttp.d.ts.map +1 -0
  90. package/dist/plugins/java/nethttp/nethttp.js +16 -0
  91. package/dist/plugins/java/okhttp/index.d.ts +2 -0
  92. package/dist/plugins/java/okhttp/index.d.ts.map +1 -0
  93. package/dist/plugins/java/okhttp/index.js +1 -0
  94. package/dist/plugins/java/okhttp/okhttp.d.ts +6 -0
  95. package/dist/plugins/java/okhttp/okhttp.d.ts.map +1 -0
  96. package/dist/plugins/java/okhttp/okhttp.js +16 -0
  97. package/dist/plugins/java/unirest/index.d.ts +2 -0
  98. package/dist/plugins/java/unirest/index.d.ts.map +1 -0
  99. package/dist/plugins/java/unirest/index.js +1 -0
  100. package/dist/plugins/java/unirest/unirest.d.ts +6 -0
  101. package/dist/plugins/java/unirest/unirest.d.ts.map +1 -0
  102. package/dist/plugins/java/unirest/unirest.js +16 -0
  103. package/dist/plugins/js/axios/axios.d.ts +6 -0
  104. package/dist/plugins/js/axios/axios.d.ts.map +1 -0
  105. package/dist/plugins/js/axios/axios.js +16 -0
  106. package/dist/plugins/js/axios/index.d.ts +2 -0
  107. package/dist/plugins/js/axios/index.d.ts.map +1 -0
  108. package/dist/plugins/js/axios/index.js +1 -0
  109. package/dist/plugins/js/jquery/index.d.ts +2 -0
  110. package/dist/plugins/js/jquery/index.d.ts.map +1 -0
  111. package/dist/plugins/js/jquery/index.js +1 -0
  112. package/dist/plugins/js/jquery/jquery.d.ts +6 -0
  113. package/dist/plugins/js/jquery/jquery.d.ts.map +1 -0
  114. package/dist/plugins/js/jquery/jquery.js +16 -0
  115. package/dist/plugins/js/xhr/index.d.ts +2 -0
  116. package/dist/plugins/js/xhr/index.d.ts.map +1 -0
  117. package/dist/plugins/js/xhr/index.js +1 -0
  118. package/dist/plugins/js/xhr/xhr.d.ts +6 -0
  119. package/dist/plugins/js/xhr/xhr.d.ts.map +1 -0
  120. package/dist/plugins/js/xhr/xhr.js +16 -0
  121. package/dist/plugins/kotlin/okhttp/index.d.ts +2 -0
  122. package/dist/plugins/kotlin/okhttp/index.d.ts.map +1 -0
  123. package/dist/plugins/kotlin/okhttp/index.js +1 -0
  124. package/dist/plugins/kotlin/okhttp/okhttp.d.ts +6 -0
  125. package/dist/plugins/kotlin/okhttp/okhttp.d.ts.map +1 -0
  126. package/dist/plugins/kotlin/okhttp/okhttp.js +16 -0
  127. package/dist/plugins/node/axios/axios.d.ts +6 -0
  128. package/dist/plugins/node/axios/axios.d.ts.map +1 -0
  129. package/dist/plugins/node/axios/axios.js +16 -0
  130. package/dist/plugins/node/axios/index.d.ts +2 -0
  131. package/dist/plugins/node/axios/index.d.ts.map +1 -0
  132. package/dist/plugins/node/axios/index.js +1 -0
  133. package/dist/plugins/objc/nsurlsession/index.d.ts +2 -0
  134. package/dist/plugins/objc/nsurlsession/index.d.ts.map +1 -0
  135. package/dist/plugins/objc/nsurlsession/index.js +1 -0
  136. package/dist/plugins/objc/nsurlsession/nsurlsession.d.ts +6 -0
  137. package/dist/plugins/objc/nsurlsession/nsurlsession.d.ts.map +1 -0
  138. package/dist/plugins/objc/nsurlsession/nsurlsession.js +16 -0
  139. package/dist/plugins/ocaml/cohttp/cohttp.d.ts +6 -0
  140. package/dist/plugins/ocaml/cohttp/cohttp.d.ts.map +1 -0
  141. package/dist/plugins/ocaml/cohttp/cohttp.js +16 -0
  142. package/dist/plugins/ocaml/cohttp/index.d.ts +2 -0
  143. package/dist/plugins/ocaml/cohttp/index.d.ts.map +1 -0
  144. package/dist/plugins/ocaml/cohttp/index.js +1 -0
  145. package/dist/plugins/php/curl/curl.d.ts +6 -0
  146. package/dist/plugins/php/curl/curl.d.ts.map +1 -0
  147. package/dist/plugins/php/curl/curl.js +16 -0
  148. package/dist/plugins/php/curl/index.d.ts +2 -0
  149. package/dist/plugins/php/curl/index.d.ts.map +1 -0
  150. package/dist/plugins/php/curl/index.js +1 -0
  151. package/dist/plugins/php/guzzle/guzzle.d.ts +6 -0
  152. package/dist/plugins/php/guzzle/guzzle.d.ts.map +1 -0
  153. package/dist/plugins/php/guzzle/guzzle.js +16 -0
  154. package/dist/plugins/php/guzzle/index.d.ts +2 -0
  155. package/dist/plugins/php/guzzle/index.d.ts.map +1 -0
  156. package/dist/plugins/php/guzzle/index.js +1 -0
  157. package/dist/plugins/powershell/restmethod/index.d.ts +2 -0
  158. package/dist/plugins/powershell/restmethod/index.d.ts.map +1 -0
  159. package/dist/plugins/powershell/restmethod/index.js +1 -0
  160. package/dist/plugins/powershell/restmethod/restmethod.d.ts +6 -0
  161. package/dist/plugins/powershell/restmethod/restmethod.d.ts.map +1 -0
  162. package/dist/plugins/powershell/restmethod/restmethod.js +16 -0
  163. package/dist/plugins/powershell/webrequest/index.d.ts +2 -0
  164. package/dist/plugins/powershell/webrequest/index.d.ts.map +1 -0
  165. package/dist/plugins/powershell/webrequest/index.js +1 -0
  166. package/dist/plugins/powershell/webrequest/webrequest.d.ts +6 -0
  167. package/dist/plugins/powershell/webrequest/webrequest.d.ts.map +1 -0
  168. package/dist/plugins/powershell/webrequest/webrequest.js +16 -0
  169. package/dist/plugins/python/python3/index.d.ts +2 -0
  170. package/dist/plugins/python/python3/index.d.ts.map +1 -0
  171. package/dist/plugins/python/python3/index.js +1 -0
  172. package/dist/plugins/python/python3/python3.d.ts +6 -0
  173. package/dist/plugins/python/python3/python3.d.ts.map +1 -0
  174. package/dist/plugins/python/python3/python3.js +16 -0
  175. package/dist/plugins/python/requests/index.d.ts +2 -0
  176. package/dist/plugins/python/requests/index.d.ts.map +1 -0
  177. package/dist/plugins/python/requests/index.js +1 -0
  178. package/dist/plugins/python/requests/requests.d.ts +6 -0
  179. package/dist/plugins/python/requests/requests.d.ts.map +1 -0
  180. package/dist/plugins/python/requests/requests.js +16 -0
  181. package/dist/plugins/r/httr/httr.d.ts +6 -0
  182. package/dist/plugins/r/httr/httr.d.ts.map +1 -0
  183. package/dist/plugins/r/httr/httr.js +16 -0
  184. package/dist/plugins/r/httr/index.d.ts +2 -0
  185. package/dist/plugins/r/httr/index.d.ts.map +1 -0
  186. package/dist/plugins/r/httr/index.js +1 -0
  187. package/dist/plugins/ruby/native/index.d.ts +2 -0
  188. package/dist/plugins/ruby/native/index.d.ts.map +1 -0
  189. package/dist/plugins/ruby/native/index.js +1 -0
  190. package/dist/plugins/ruby/native/native.d.ts +6 -0
  191. package/dist/plugins/ruby/native/native.d.ts.map +1 -0
  192. package/dist/plugins/ruby/native/native.js +16 -0
  193. package/dist/plugins/shell/httpie/httpie.d.ts +6 -0
  194. package/dist/plugins/shell/httpie/httpie.d.ts.map +1 -0
  195. package/dist/plugins/shell/httpie/httpie.js +16 -0
  196. package/dist/plugins/shell/httpie/index.d.ts +2 -0
  197. package/dist/plugins/shell/httpie/index.d.ts.map +1 -0
  198. package/dist/plugins/shell/httpie/index.js +1 -0
  199. package/dist/plugins/shell/wget/index.d.ts +2 -0
  200. package/dist/plugins/shell/wget/index.d.ts.map +1 -0
  201. package/dist/plugins/shell/wget/index.js +1 -0
  202. package/dist/plugins/shell/wget/wget.d.ts +6 -0
  203. package/dist/plugins/shell/wget/wget.d.ts.map +1 -0
  204. package/dist/plugins/shell/wget/wget.js +16 -0
  205. package/dist/plugins/swift/nsurlsession/index.d.ts +2 -0
  206. package/dist/plugins/swift/nsurlsession/index.d.ts.map +1 -0
  207. package/dist/plugins/swift/nsurlsession/index.js +1 -0
  208. package/dist/plugins/swift/nsurlsession/nsurlsession.d.ts +6 -0
  209. package/dist/plugins/swift/nsurlsession/nsurlsession.d.ts.map +1 -0
  210. package/dist/plugins/swift/nsurlsession/nsurlsession.js +16 -0
  211. package/dist/snippetz.d.ts +4 -4
  212. package/dist/snippetz.d.ts.map +1 -1
  213. package/dist/snippetz.js +60 -4
  214. package/package.json +145 -1
@@ -0,0 +1,70 @@
1
+ import { CodeBuilder } from '../httpsnippet-lite/dist/esm/helpers/code-builder.js';
2
+ import { escapeForSingleQuotes } from '../httpsnippet-lite/dist/esm/helpers/escape.js';
3
+
4
+ const native = {
5
+ info: {
6
+ key: 'native',
7
+ title: 'net::http',
8
+ link: 'http://ruby-doc.org/stdlib-2.2.1/libdoc/net/http/rdoc/Net/HTTP.html',
9
+ description: 'Ruby HTTP client',
10
+ },
11
+ convert: ({ uriObj, method: rawMethod, fullUrl, postData, allHeaders }, options = {}) => {
12
+ const { insecureSkipVerify = false } = options;
13
+ const { push, blank, join } = new CodeBuilder();
14
+ push("require 'uri'");
15
+ push("require 'net/http'");
16
+ blank();
17
+ // To support custom methods we check for the supported methods
18
+ // and if doesn't exist then we build a custom class for it
19
+ const method = rawMethod.toUpperCase();
20
+ const methods = [
21
+ 'GET',
22
+ 'POST',
23
+ 'HEAD',
24
+ 'DELETE',
25
+ 'PATCH',
26
+ 'PUT',
27
+ 'OPTIONS',
28
+ 'COPY',
29
+ 'LOCK',
30
+ 'UNLOCK',
31
+ 'MOVE',
32
+ 'TRACE',
33
+ ];
34
+ const capMethod = method.charAt(0) + method.substring(1).toLowerCase();
35
+ if (!methods.includes(method)) {
36
+ push(`class Net::HTTP::${capMethod} < Net::HTTPRequest`);
37
+ push(` METHOD = '${method.toUpperCase()}'`);
38
+ push(` REQUEST_HAS_BODY = '${(postData === null || postData === void 0 ? void 0 : postData.text) ? 'true' : 'false'}'`);
39
+ push(' RESPONSE_HAS_BODY = true');
40
+ push('end');
41
+ blank();
42
+ }
43
+ push(`url = URI("${fullUrl}")`);
44
+ blank();
45
+ push('http = Net::HTTP.new(url.host, url.port)');
46
+ if (uriObj.protocol === 'https:') {
47
+ push('http.use_ssl = true');
48
+ if (insecureSkipVerify) {
49
+ push('http.verify_mode = OpenSSL::SSL::VERIFY_NONE');
50
+ }
51
+ }
52
+ blank();
53
+ push(`request = Net::HTTP::${capMethod}.new(url)`);
54
+ const headers = Object.keys(allHeaders);
55
+ if (headers.length) {
56
+ headers.forEach(key => {
57
+ push(`request["${key}"] = '${escapeForSingleQuotes(allHeaders[key])}'`);
58
+ });
59
+ }
60
+ if (postData === null || postData === void 0 ? void 0 : postData.text) {
61
+ push(`request.body = ${JSON.stringify(postData.text)}`);
62
+ }
63
+ blank();
64
+ push('response = http.request(request)');
65
+ push('puts response.read_body');
66
+ return join();
67
+ },
68
+ };
69
+
70
+ export { native };
@@ -0,0 +1,123 @@
1
+ import { CodeBuilder } from '../httpsnippet-lite/dist/esm/helpers/code-builder.js';
2
+ import { escapeForDoubleQuotes, escapeForSingleQuotes } from '../httpsnippet-lite/dist/esm/helpers/escape.js';
3
+ import { getHeader } from '../httpsnippet-lite/dist/esm/helpers/headers.js';
4
+
5
+ /**
6
+ * @description
7
+ * HTTP code snippet generator for R using httr
8
+ *
9
+ * @author
10
+ * @gabrielakoreeda
11
+ *
12
+ * for any questions or issues regarding the generated code snippet, please open an issue mentioning the author.
13
+ */
14
+ const httr = {
15
+ info: {
16
+ key: 'httr',
17
+ title: 'httr',
18
+ link: 'https://cran.r-project.org/web/packages/httr/vignettes/quickstart.html',
19
+ description: 'httr: Tools for Working with URLs and HTTP',
20
+ },
21
+ convert: ({ url, queryObj, queryString, postData, allHeaders, method }, options = {}) => {
22
+ var _a, _b;
23
+ // Start snippet
24
+ const { push, blank, join } = new CodeBuilder({
25
+ indent: (_a = options.indent) !== null && _a !== void 0 ? _a : ' ',
26
+ });
27
+ // Import httr
28
+ push('library(httr)');
29
+ blank();
30
+ // Set URL
31
+ push(`url <- "${url}"`);
32
+ blank();
33
+ // Construct query string
34
+ const qs = queryObj;
35
+ delete queryObj.key;
36
+ const entries = Object.entries(qs);
37
+ const entriesCount = entries.length;
38
+ if (entriesCount === 1) {
39
+ const entry = entries[0];
40
+ push(`queryString <- list(${entry[0]} = "${entry[1]}")`);
41
+ blank();
42
+ }
43
+ else if (entriesCount > 1) {
44
+ push('queryString <- list(');
45
+ entries.forEach(([key, value], i) => {
46
+ const isLastItem = i !== entriesCount - 1;
47
+ const maybeComma = isLastItem ? ',' : '';
48
+ push(`${key} = "${value}"${maybeComma}`, 1);
49
+ });
50
+ push(')');
51
+ blank();
52
+ }
53
+ // Construct payload
54
+ const payload = JSON.stringify(postData === null || postData === void 0 ? void 0 : postData.text);
55
+ if (payload) {
56
+ push(`payload <- ${payload}`);
57
+ blank();
58
+ }
59
+ // Define encode
60
+ if (postData && (postData.text || postData.jsonObj || postData.params)) {
61
+ switch (postData.mimeType) {
62
+ case 'application/x-www-form-urlencoded':
63
+ push('encode <- "form"');
64
+ blank();
65
+ break;
66
+ case 'application/json':
67
+ push('encode <- "json"');
68
+ blank();
69
+ break;
70
+ case 'multipart/form-data':
71
+ push('encode <- "multipart"');
72
+ blank();
73
+ break;
74
+ default:
75
+ push('encode <- "raw"');
76
+ blank();
77
+ break;
78
+ }
79
+ }
80
+ // Construct headers
81
+ const cookieHeader = getHeader(allHeaders, 'cookie');
82
+ const acceptHeader = getHeader(allHeaders, 'accept');
83
+ const setCookies = cookieHeader
84
+ ? `set_cookies(\`${String(cookieHeader)
85
+ .replace(/;/g, '", `')
86
+ .replace(/` /g, '`')
87
+ .replace(/[=]/g, '` = "')}")`
88
+ : undefined;
89
+ const setAccept = acceptHeader ? `accept("${escapeForDoubleQuotes(acceptHeader)}")` : undefined;
90
+ const setContentType = `content_type("${escapeForDoubleQuotes((_b = postData === null || postData === void 0 ? void 0 : postData.mimeType) !== null && _b !== void 0 ? _b : 'application/octet-stream')}")`;
91
+ const otherHeaders = Object.entries(allHeaders)
92
+ // These headers are all handled separately:
93
+ .filter(([key]) => !['cookie', 'accept', 'content-type'].includes(key.toLowerCase()))
94
+ .map(([key, value]) => `'${key}' = '${escapeForSingleQuotes(value)}'`)
95
+ .join(', ');
96
+ const setHeaders = otherHeaders ? `add_headers(${otherHeaders})` : undefined;
97
+ // Construct request
98
+ let request = `response <- VERB("${method}", url`;
99
+ if (payload) {
100
+ request += ', body = payload';
101
+ }
102
+ if (queryString.length) {
103
+ request += ', query = queryString';
104
+ }
105
+ const headerAdditions = [setHeaders, setContentType, setAccept, setCookies]
106
+ .filter(x => !!x)
107
+ .join(', ');
108
+ if (headerAdditions) {
109
+ request += `, ${headerAdditions}`;
110
+ }
111
+ if (postData && (postData.text || postData.jsonObj || postData.params)) {
112
+ request += ', encode = encode';
113
+ }
114
+ request += ')';
115
+ push(request);
116
+ blank();
117
+ // Print response
118
+ push('content(response, "text")');
119
+ return join();
120
+ },
121
+ };
122
+
123
+ export { httr };
@@ -0,0 +1,166 @@
1
+ import { CodeBuilder } from '../httpsnippet-lite/dist/esm/helpers/code-builder.js';
2
+ import { escapeForDoubleQuotes } from '../httpsnippet-lite/dist/esm/helpers/escape.js';
3
+ import { getHeaderName } from '../httpsnippet-lite/dist/esm/helpers/headers.js';
4
+ import { literalRepresentation } from '../httpsnippet-lite/dist/esm/targets/python/helpers.js';
5
+
6
+ /**
7
+ * @description
8
+ * HTTP code snippet generator for Python using Requests
9
+ *
10
+ * @author
11
+ * @montanaflynn
12
+ *
13
+ * for any questions or issues regarding the generated code snippet, please open an issue mentioning the author.
14
+ */
15
+ const builtInMethods = ['HEAD', 'GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS'];
16
+ const requests = {
17
+ info: {
18
+ key: 'requests',
19
+ title: 'Requests',
20
+ link: 'http://docs.python-requests.org/en/latest/api/#requests.request',
21
+ description: 'Requests HTTP library',
22
+ },
23
+ convert: ({ queryObj, url, postData, allHeaders, method }, options) => {
24
+ const opts = {
25
+ indent: ' ',
26
+ pretty: true,
27
+ ...options,
28
+ };
29
+ // Start snippet
30
+ const { push, blank, join } = new CodeBuilder({ indent: opts.indent });
31
+ // Import requests
32
+ push('import requests');
33
+ blank();
34
+ // Set URL
35
+ push(`url = "${url}"`);
36
+ blank();
37
+ // Construct query string
38
+ let qs;
39
+ if (Object.keys(queryObj).length) {
40
+ qs = `querystring = ${JSON.stringify(queryObj)}`;
41
+ push(qs);
42
+ blank();
43
+ }
44
+ const headers = allHeaders;
45
+ // Construct payload
46
+ let payload = {};
47
+ const files = {};
48
+ let hasFiles = false;
49
+ let hasPayload = false;
50
+ let jsonPayload = false;
51
+ switch (postData === null || postData === void 0 ? void 0 : postData.mimeType) {
52
+ case 'application/json':
53
+ if (postData.jsonObj) {
54
+ push(`payload = ${literalRepresentation(postData.jsonObj, opts)}`);
55
+ jsonPayload = true;
56
+ hasPayload = true;
57
+ }
58
+ break;
59
+ case 'multipart/form-data':
60
+ if (!postData.params) {
61
+ break;
62
+ }
63
+ payload = {};
64
+ postData.params.forEach(p => {
65
+ if (p.fileName) {
66
+ files[p.name] = `open('${p.fileName}', 'rb')`;
67
+ hasFiles = true;
68
+ }
69
+ else {
70
+ payload[p.name] = p.value;
71
+ hasPayload = true;
72
+ }
73
+ });
74
+ if (hasFiles) {
75
+ push(`files = ${literalRepresentation(files, opts)}`);
76
+ if (hasPayload) {
77
+ push(`payload = ${literalRepresentation(payload, opts)}`);
78
+ }
79
+ // The requests library will only automatically add a `multipart/form-data` header if there are files being sent. If we're **only** sending form data we still need to send the boundary ourselves.
80
+ const headerName = getHeaderName(headers, 'content-type');
81
+ if (headerName) {
82
+ delete headers[headerName];
83
+ }
84
+ }
85
+ else {
86
+ const nonFilePayload = JSON.stringify(postData.text);
87
+ if (nonFilePayload) {
88
+ push(`payload = ${nonFilePayload}`);
89
+ hasPayload = true;
90
+ }
91
+ }
92
+ break;
93
+ default: {
94
+ if (!postData) {
95
+ break;
96
+ }
97
+ if (postData.mimeType === 'application/x-www-form-urlencoded' && postData.paramsObj) {
98
+ push(`payload = ${literalRepresentation(postData.paramsObj, opts)}`);
99
+ hasPayload = true;
100
+ break;
101
+ }
102
+ const payload = JSON.stringify(postData.text);
103
+ if (payload) {
104
+ push(`payload = ${payload}`);
105
+ hasPayload = true;
106
+ }
107
+ }
108
+ }
109
+ // Construct headers
110
+ const headerCount = Object.keys(headers).length;
111
+ if (headerCount === 0 && (hasPayload || hasFiles)) {
112
+ // If we don't have any heads but we do have a payload we should put a blank line here between that payload consturction and our execution of the requests library.
113
+ blank();
114
+ }
115
+ else if (headerCount === 1) {
116
+ for (const header in headers) {
117
+ push(`headers = {"${header}": "${escapeForDoubleQuotes(headers[header])}"}`);
118
+ blank();
119
+ }
120
+ }
121
+ else if (headerCount > 1) {
122
+ let count = 1;
123
+ push('headers = {');
124
+ for (const header in headers) {
125
+ if (count !== headerCount) {
126
+ push(`"${header}": "${escapeForDoubleQuotes(headers[header])}",`, 1);
127
+ }
128
+ else {
129
+ push(`"${header}": "${escapeForDoubleQuotes(headers[header])}"`, 1);
130
+ }
131
+ count += 1;
132
+ }
133
+ push('}');
134
+ blank();
135
+ }
136
+ // Construct request
137
+ let request = builtInMethods.includes(method)
138
+ ? `response = requests.${method.toLowerCase()}(url`
139
+ : `response = requests.request("${method}", url`;
140
+ if (hasPayload) {
141
+ if (jsonPayload) {
142
+ request += ', json=payload';
143
+ }
144
+ else {
145
+ request += ', data=payload';
146
+ }
147
+ }
148
+ if (hasFiles) {
149
+ request += ', files=files';
150
+ }
151
+ if (headerCount > 0) {
152
+ request += ', headers=headers';
153
+ }
154
+ if (qs) {
155
+ request += ', params=querystring';
156
+ }
157
+ request += ')';
158
+ push(request);
159
+ blank();
160
+ // Print response
161
+ push('print(response.json())');
162
+ return join();
163
+ },
164
+ };
165
+
166
+ export { requests };
@@ -0,0 +1,91 @@
1
+ import { CodeBuilder } from '../httpsnippet-lite/dist/esm/helpers/code-builder.js';
2
+ import { escapeForDoubleQuotes } from '../httpsnippet-lite/dist/esm/helpers/escape.js';
3
+
4
+ /**
5
+ * @description
6
+ * HTTP code snippet generator for native Python3.
7
+ *
8
+ * @author
9
+ * @montanaflynn
10
+ *
11
+ * for any questions or issues regarding the generated code snippet, please open an issue mentioning the author.
12
+ */
13
+ const python3 = {
14
+ info: {
15
+ key: 'python3',
16
+ title: 'http.client',
17
+ link: 'https://docs.python.org/3/library/http.client.html',
18
+ description: 'Python3 HTTP Client',
19
+ },
20
+ convert: ({ uriObj: { path, protocol, host }, postData, allHeaders, method }, options = {}) => {
21
+ const { insecureSkipVerify = false } = options;
22
+ const { push, blank, join } = new CodeBuilder();
23
+ // Start Request
24
+ push('import http.client');
25
+ if (insecureSkipVerify) {
26
+ push('import ssl');
27
+ }
28
+ blank();
29
+ // Check which protocol to be used for the client connection
30
+ if (protocol === 'https:') {
31
+ const sslContext = insecureSkipVerify ? ', context = ssl._create_unverified_context()' : '';
32
+ push(`conn = http.client.HTTPSConnection("${host}"${sslContext})`);
33
+ blank();
34
+ }
35
+ else {
36
+ push(`conn = http.client.HTTPConnection("${host}")`);
37
+ blank();
38
+ }
39
+ // Create payload string if it exists
40
+ const payload = JSON.stringify(postData === null || postData === void 0 ? void 0 : postData.text);
41
+ if (payload) {
42
+ push(`payload = ${payload}`);
43
+ blank();
44
+ }
45
+ // Create Headers
46
+ const headers = allHeaders;
47
+ const headerCount = Object.keys(headers).length;
48
+ if (headerCount === 1) {
49
+ for (const header in headers) {
50
+ push(`headers = { '${header}': "${escapeForDoubleQuotes(headers[header])}" }`);
51
+ blank();
52
+ }
53
+ }
54
+ else if (headerCount > 1) {
55
+ let count = 1;
56
+ push('headers = {');
57
+ for (const header in headers) {
58
+ if (count++ !== headerCount) {
59
+ push(` '${header}': "${escapeForDoubleQuotes(headers[header])}",`);
60
+ }
61
+ else {
62
+ push(` '${header}': "${escapeForDoubleQuotes(headers[header])}"`);
63
+ }
64
+ }
65
+ push('}');
66
+ blank();
67
+ }
68
+ // Make Request
69
+ if (payload && headerCount) {
70
+ push(`conn.request("${method}", "${path}", payload, headers)`);
71
+ }
72
+ else if (payload && !headerCount) {
73
+ push(`conn.request("${method}", "${path}", payload)`);
74
+ }
75
+ else if (!payload && headerCount) {
76
+ push(`conn.request("${method}", "${path}", headers=headers)`);
77
+ }
78
+ else {
79
+ push(`conn.request("${method}", "${path}")`);
80
+ }
81
+ // Get Response
82
+ blank();
83
+ push('res = conn.getresponse()');
84
+ push('data = res.read()');
85
+ blank();
86
+ push('print(data.decode("utf-8"))');
87
+ return join();
88
+ },
89
+ };
90
+
91
+ export { python3 };
@@ -0,0 +1,13 @@
1
+ import { generatePowershellConvert } from '../httpsnippet-lite/dist/esm/targets/powershell/common.js';
2
+
3
+ const webrequest = {
4
+ info: {
5
+ key: 'webrequest',
6
+ title: 'Invoke-WebRequest',
7
+ link: 'https://docs.microsoft.com/en-us/powershell/module/Microsoft.PowerShell.Utility/Invoke-WebRequest',
8
+ description: 'Powershell Invoke-WebRequest client',
9
+ },
10
+ convert: generatePowershellConvert('Invoke-WebRequest'),
11
+ };
12
+
13
+ export { webrequest };
@@ -0,0 +1,13 @@
1
+ import { generatePowershellConvert } from '../httpsnippet-lite/dist/esm/targets/powershell/common.js';
2
+
3
+ const restmethod = {
4
+ info: {
5
+ key: 'restmethod',
6
+ title: 'Invoke-RestMethod',
7
+ link: 'https://docs.microsoft.com/en-us/powershell/module/Microsoft.PowerShell.Utility/Invoke-RestMethod',
8
+ description: 'Powershell Invoke-RestMethod client',
9
+ },
10
+ convert: generatePowershellConvert('Invoke-RestMethod'),
11
+ };
12
+
13
+ export { restmethod };
@@ -4,12 +4,40 @@ export type { Request } from 'har-format';
4
4
  * List of available clients
5
5
  */
6
6
  export type AvailableClients = [
7
+ 'c/libcurl',
8
+ 'clojure/clj_http',
9
+ 'csharp/httpclient',
10
+ 'csharp/restsharp',
11
+ 'go/native',
12
+ 'http/http1.1',
13
+ 'java/asynchttp',
14
+ 'java/nethttp',
15
+ 'java/okhttp',
16
+ 'java/unirest',
17
+ 'js/axios',
7
18
  'js/fetch',
19
+ 'js/jquery',
8
20
  'js/ofetch',
21
+ 'js/xhr',
22
+ 'kotlin/okhttp',
23
+ 'node/axios',
9
24
  'node/fetch',
10
25
  'node/ofetch',
11
26
  'node/undici',
12
- 'shell/curl'
27
+ 'objc/nsurlsession',
28
+ 'ocaml/cohttp',
29
+ 'php/curl',
30
+ 'php/guzzle',
31
+ 'powershell/restmethod',
32
+ 'powershell/webrequest',
33
+ 'python/python3',
34
+ 'python/requests',
35
+ 'r/httr',
36
+ 'ruby/native',
37
+ 'shell/curl',
38
+ 'shell/httpie',
39
+ 'shell/wget',
40
+ 'swift/nsurlsession'
13
41
  ];
14
42
  /** Programming language */
15
43
  export type TargetId = AvailableClients[number] extends `${infer T}/${string}` ? T : never;
@@ -1 +1 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/core/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,YAAY,CAAA;AAEzC,YAAY,EAAE,OAAO,EAAE,MAAM,YAAY,CAAA;AAEzC;;GAEG;AACH,MAAM,MAAM,gBAAgB,GAAG;IAC7B,UAAU;IACV,WAAW;IACX,YAAY;IACZ,aAAa;IACb,aAAa;IACb,YAAY;CACb,CAAA;AAED,2BAA2B;AAC3B,MAAM,MAAM,QAAQ,GAAG,gBAAgB,CAAC,MAAM,CAAC,SAAS,GAAG,MAAM,CAAC,IAAI,MAAM,EAAE,GAC1E,CAAC,GACD,KAAK,CAAA;AAET,kBAAkB;AAClB,MAAM,MAAM,QAAQ,CAAC,CAAC,SAAS,MAAM,IAAI,CAAC,SAAS,QAAQ,GACvD,OAAO,CACL,gBAAgB,CAAC,MAAM,CAAC,EACxB,GAAG,CAAC,IAAI,MAAM,EAAE,CACjB,SAAS,GAAG,CAAC,IAAI,MAAM,CAAC,EAAE,GACzB,CAAC,GACD,KAAK,GACP,KAAK,CAAA;AAET,uCAAuC;AACvC,MAAM,MAAM,MAAM,GAAG;IACnB,mCAAmC;IACnC,MAAM,EAAE,QAAQ,CAAA;IAChB,oCAAoC;IACpC,MAAM,EAAE,QAAQ,CAAC,QAAQ,CAAC,CAAA;IAC1B,8BAA8B;IAC9B,QAAQ,EAAE,CACR,OAAO,CAAC,EAAE,OAAO,CAAC,OAAO,CAAC,EAC1B,aAAa,CAAC,EAAE,mBAAmB,KAChC,MAAM,CAAA;CACZ,CAAA;AAED;;GAEG;AACH,MAAM,MAAM,mBAAmB,GAAG;IAChC,mDAAmD;IACnD,IAAI,CAAC,EAAE;QAAE,QAAQ,EAAE,MAAM,CAAC;QAAC,QAAQ,EAAE,MAAM,CAAA;KAAE,CAAA;CAC9C,CAAA"}
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/core/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,YAAY,CAAA;AAEzC,YAAY,EAAE,OAAO,EAAE,MAAM,YAAY,CAAA;AAEzC;;GAEG;AACH,MAAM,MAAM,gBAAgB,GAAG;IAC7B,WAAW;IACX,kBAAkB;IAClB,mBAAmB;IACnB,kBAAkB;IAClB,WAAW;IACX,cAAc;IACd,gBAAgB;IAChB,cAAc;IACd,aAAa;IACb,cAAc;IACd,UAAU;IACV,UAAU;IACV,WAAW;IACX,WAAW;IACX,QAAQ;IACR,eAAe;IACf,YAAY;IACZ,YAAY;IACZ,aAAa;IACb,aAAa;IACb,mBAAmB;IACnB,cAAc;IACd,UAAU;IACV,YAAY;IACZ,uBAAuB;IACvB,uBAAuB;IACvB,gBAAgB;IAChB,iBAAiB;IACjB,QAAQ;IACR,aAAa;IACb,YAAY;IACZ,cAAc;IACd,YAAY;IACZ,oBAAoB;CACrB,CAAA;AAED,2BAA2B;AAC3B,MAAM,MAAM,QAAQ,GAAG,gBAAgB,CAAC,MAAM,CAAC,SAAS,GAAG,MAAM,CAAC,IAAI,MAAM,EAAE,GAC1E,CAAC,GACD,KAAK,CAAA;AAET,kBAAkB;AAClB,MAAM,MAAM,QAAQ,CAAC,CAAC,SAAS,MAAM,IAAI,CAAC,SAAS,QAAQ,GACvD,OAAO,CACL,gBAAgB,CAAC,MAAM,CAAC,EACxB,GAAG,CAAC,IAAI,MAAM,EAAE,CACjB,SAAS,GAAG,CAAC,IAAI,MAAM,CAAC,EAAE,GACzB,CAAC,GACD,KAAK,GACP,KAAK,CAAA;AAET,uCAAuC;AACvC,MAAM,MAAM,MAAM,GAAG;IACnB,mCAAmC;IACnC,MAAM,EAAE,QAAQ,CAAA;IAChB,oCAAoC;IACpC,MAAM,EAAE,QAAQ,CAAC,QAAQ,CAAC,CAAA;IAC1B,8BAA8B;IAC9B,QAAQ,EAAE,CACR,OAAO,CAAC,EAAE,OAAO,CAAC,OAAO,CAAC,EAC1B,aAAa,CAAC,EAAE,mBAAmB,KAChC,MAAM,CAAA;CACZ,CAAA;AAED;;GAEG;AACH,MAAM,MAAM,mBAAmB,GAAG;IAChC,mDAAmD;IACnD,IAAI,CAAC,EAAE;QAAE,QAAQ,EAAE,MAAM,CAAC;QAAC,QAAQ,EAAE,MAAM,CAAA;KAAE,CAAA;CAC9C,CAAA"}
@@ -0,0 +1,7 @@
1
+ import type { Client } from '@/httpsnippet-lite/dist/types/targets/targets';
2
+ import type { Request } from 'har-format';
3
+ /**
4
+ * Takes a httpsnippet-lite client and converts the given request to a code example with it.
5
+ */
6
+ export declare function convertWithHttpSnippetLite(client: Client<object>, partialRequest?: Partial<Request>): string;
7
+ //# sourceMappingURL=convertWithHttpSnippetLite.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"convertWithHttpSnippetLite.d.ts","sourceRoot":"","sources":["../../../src/core/utils/convertWithHttpSnippetLite.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,+CAA+C,CAAA;AAC3E,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,YAAY,CAAA;AAEzC;;GAEG;AACH,wBAAgB,0BAA0B,CACxC,MAAM,EAAE,MAAM,CAAC,MAAM,CAAC,EACtB,cAAc,CAAC,EAAE,OAAO,CAAC,OAAO,CAAC,GAChC,MAAM,CAiGR"}
@@ -0,0 +1,83 @@
1
+ /**
2
+ * Takes a httpsnippet-lite client and converts the given request to a code example with it.
3
+ */
4
+ function convertWithHttpSnippetLite(client, partialRequest) {
5
+ const request = {
6
+ url: partialRequest?.url ?? '',
7
+ httpVersion: partialRequest?.httpVersion ?? 'HTTP/1.1',
8
+ cookies: partialRequest?.cookies ?? [],
9
+ headers: partialRequest?.headers ?? [],
10
+ headersSize: partialRequest?.headersSize ?? 0,
11
+ bodySize: partialRequest?.bodySize ?? 0,
12
+ queryString: partialRequest?.queryString ?? [],
13
+ ...partialRequest,
14
+ };
15
+ const allHeaders = (request?.headers ?? []).reduce((acc, header) => ({
16
+ ...acc,
17
+ [header.name]: header.value,
18
+ }), {});
19
+ const queryObj = (request.queryString ?? []).reduce((acc, param) => ({
20
+ ...acc,
21
+ [param.name]: param.value,
22
+ }), {});
23
+ const cookiesObj = (request.cookies ?? []).reduce((acc, cookie) => ({
24
+ ...acc,
25
+ [cookie.name]: cookie.value,
26
+ }), {});
27
+ const parsedUrl = new URL(request.url);
28
+ const uriObj = {
29
+ protocol: parsedUrl.protocol,
30
+ hostname: parsedUrl.hostname,
31
+ host: parsedUrl.hostname,
32
+ port: parsedUrl.port,
33
+ pathname: parsedUrl.pathname
34
+ .split('/')
35
+ .map((segment) => encodeURIComponent(decodeURIComponent(segment)))
36
+ .join('/') + parsedUrl.search,
37
+ path: parsedUrl.pathname
38
+ .split('/')
39
+ .map((segment) => encodeURIComponent(decodeURIComponent(segment)))
40
+ .join('/') + parsedUrl.search,
41
+ search: parsedUrl.search,
42
+ hash: parsedUrl.hash,
43
+ href: parsedUrl.href,
44
+ origin: parsedUrl.origin,
45
+ password: parsedUrl.password,
46
+ searchParams: parsedUrl.searchParams,
47
+ username: parsedUrl.username,
48
+ toString: parsedUrl.toString,
49
+ toJSON: () => parsedUrl.toJSON(),
50
+ };
51
+ return client?.convert({
52
+ url: request.url,
53
+ uriObj,
54
+ method: request.method?.toLocaleUpperCase() ?? 'GET',
55
+ httpVersion: request.httpVersion,
56
+ cookies: request.cookies ?? [],
57
+ headers: request.headers ?? [],
58
+ headersSize: request.headersSize ?? 0,
59
+ headersObj: allHeaders ?? {},
60
+ bodySize: request.bodySize ?? 0,
61
+ queryString: request.queryString ?? [],
62
+ postData: request.postData
63
+ ? {
64
+ mimeType: request.postData.mimeType ?? 'application/json',
65
+ text: request.postData.text,
66
+ params: request.postData.params ?? [],
67
+ jsonObj: request.postData.mimeType?.includes('json')
68
+ ? JSON.parse(request.postData.text ?? '{}')
69
+ : undefined,
70
+ paramsObj: request.postData.params?.reduce((acc, param) => ({
71
+ ...acc,
72
+ [param.name]: param.value ?? '',
73
+ }), {}) ?? {},
74
+ }
75
+ : undefined,
76
+ allHeaders: allHeaders ?? {},
77
+ fullUrl: request.url,
78
+ queryObj: queryObj ?? {},
79
+ cookiesObj: cookiesObj ?? {},
80
+ });
81
+ }
82
+
83
+ export { convertWithHttpSnippetLite };