@scalar/snippetz 0.7.4 → 0.7.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (245) hide show
  1. package/dist/clients/index.js +168 -169
  2. package/dist/httpsnippet-lite/helpers/code-builder.js +86 -74
  3. package/dist/httpsnippet-lite/helpers/escape.js +69 -44
  4. package/dist/httpsnippet-lite/helpers/headers.js +17 -14
  5. package/dist/httpsnippet-lite/helpers/reducer.js +1 -1
  6. package/dist/httpsnippet-lite/helpers/shell.js +13 -13
  7. package/dist/httpsnippet-lite/helpers/snippet-request.js +1 -1
  8. package/dist/httpsnippet-lite/helpers/url.js +4 -8
  9. package/dist/httpsnippet-lite/targets/c/libcurl/client.js +69 -67
  10. package/dist/httpsnippet-lite/targets/clojure/clj_http/client.js +165 -154
  11. package/dist/httpsnippet-lite/targets/csharp/restsharp/client.js +35 -38
  12. package/dist/httpsnippet-lite/targets/go/native/client.js +151 -147
  13. package/dist/httpsnippet-lite/targets/java/asynchttp/client.js +32 -35
  14. package/dist/httpsnippet-lite/targets/java/nethttp/client.js +40 -39
  15. package/dist/httpsnippet-lite/targets/java/okhttp/client.js +83 -73
  16. package/dist/httpsnippet-lite/targets/java/unirest/client.js +41 -34
  17. package/dist/httpsnippet-lite/targets/javascript/axios/client.js +81 -76
  18. package/dist/httpsnippet-lite/targets/javascript/jquery/client.js +77 -71
  19. package/dist/httpsnippet-lite/targets/javascript/xhr/client.js +73 -71
  20. package/dist/httpsnippet-lite/targets/kotlin/okhttp/client.js +83 -73
  21. package/dist/httpsnippet-lite/targets/node/axios/client.js +74 -69
  22. package/dist/httpsnippet-lite/targets/objc/helpers.js +52 -33
  23. package/dist/httpsnippet-lite/targets/objc/nsurlsession/client.js +123 -121
  24. package/dist/httpsnippet-lite/targets/ocaml/cohttp/client.js +60 -49
  25. package/dist/httpsnippet-lite/targets/powershell/common.js +46 -56
  26. package/dist/httpsnippet-lite/targets/powershell/restmethod/client.js +9 -13
  27. package/dist/httpsnippet-lite/targets/powershell/webrequest/client.js +9 -13
  28. package/dist/httpsnippet-lite/targets/python/python3/client.js +85 -68
  29. package/dist/httpsnippet-lite/targets/r/httr/client.js +114 -95
  30. package/dist/httpsnippet-lite/targets/ruby/native/client.js +66 -70
  31. package/dist/httpsnippet-lite/targets/shell/httpie/client.js +121 -107
  32. package/dist/httpsnippet-lite/targets/shell/wget/client.js +48 -43
  33. package/dist/httpsnippet-lite/targets/swift/helpers.js +62 -44
  34. package/dist/httpsnippet-lite/targets/swift/nsurlsession/client.js +126 -112
  35. package/dist/httpsnippet-lite/targets/target.js +1 -1
  36. package/dist/index.js +2 -9
  37. package/dist/libs/http.js +68 -56
  38. package/dist/libs/javascript.js +77 -60
  39. package/dist/libs/php.js +59 -49
  40. package/dist/libs/rust.js +60 -33
  41. package/dist/libs/shell.js +14 -5
  42. package/dist/plugins/c/libcurl/index.js +1 -5
  43. package/dist/plugins/c/libcurl/libcurl.js +13 -13
  44. package/dist/plugins/clojure/clj_http/clj_http.js +13 -13
  45. package/dist/plugins/clojure/clj_http/index.js +1 -5
  46. package/dist/plugins/csharp/httpclient/httpclient.js +186 -150
  47. package/dist/plugins/csharp/httpclient/index.js +1 -5
  48. package/dist/plugins/csharp/restsharp/index.js +1 -5
  49. package/dist/plugins/csharp/restsharp/restsharp.js +13 -13
  50. package/dist/plugins/dart/http/http.js +95 -93
  51. package/dist/plugins/dart/http/index.js +1 -5
  52. package/dist/plugins/fsharp/httpclient/httpclient.js +156 -122
  53. package/dist/plugins/fsharp/httpclient/index.js +1 -5
  54. package/dist/plugins/go/native/index.js +1 -5
  55. package/dist/plugins/go/native/native.js +13 -13
  56. package/dist/plugins/http/http11/http11.js +103 -85
  57. package/dist/plugins/http/http11/index.js +1 -5
  58. package/dist/plugins/java/asynchttp/asynchttp.js +13 -13
  59. package/dist/plugins/java/asynchttp/index.js +1 -5
  60. package/dist/plugins/java/nethttp/index.js +1 -5
  61. package/dist/plugins/java/nethttp/nethttp.js +13 -13
  62. package/dist/plugins/java/okhttp/index.js +1 -5
  63. package/dist/plugins/java/okhttp/okhttp.js +13 -13
  64. package/dist/plugins/java/unirest/index.js +1 -5
  65. package/dist/plugins/java/unirest/unirest.js +13 -13
  66. package/dist/plugins/js/axios/axios.js +13 -13
  67. package/dist/plugins/js/axios/index.js +1 -5
  68. package/dist/plugins/js/fetch/fetch.js +82 -68
  69. package/dist/plugins/js/fetch/index.js +1 -5
  70. package/dist/plugins/js/jquery/index.js +1 -5
  71. package/dist/plugins/js/jquery/jquery.js +13 -13
  72. package/dist/plugins/js/ofetch/index.js +1 -5
  73. package/dist/plugins/js/ofetch/ofetch.js +61 -48
  74. package/dist/plugins/js/xhr/index.js +1 -5
  75. package/dist/plugins/js/xhr/xhr.js +13 -13
  76. package/dist/plugins/kotlin/okhttp/index.js +1 -5
  77. package/dist/plugins/kotlin/okhttp/okhttp.js +13 -13
  78. package/dist/plugins/node/axios/axios.js +13 -13
  79. package/dist/plugins/node/axios/index.js +1 -5
  80. package/dist/plugins/node/fetch/fetch.js +86 -74
  81. package/dist/plugins/node/fetch/index.js +1 -5
  82. package/dist/plugins/node/ofetch/index.js +1 -5
  83. package/dist/plugins/node/ofetch/ofetch.js +61 -48
  84. package/dist/plugins/node/undici/index.js +1 -5
  85. package/dist/plugins/node/undici/undici.js +60 -47
  86. package/dist/plugins/objc/nsurlsession/index.js +1 -5
  87. package/dist/plugins/objc/nsurlsession/nsurlsession.js +13 -13
  88. package/dist/plugins/ocaml/cohttp/cohttp.js +13 -13
  89. package/dist/plugins/ocaml/cohttp/index.js +1 -5
  90. package/dist/plugins/php/curl/curl.js +143 -101
  91. package/dist/plugins/php/curl/index.js +1 -5
  92. package/dist/plugins/php/guzzle/guzzle.js +103 -83
  93. package/dist/plugins/php/guzzle/index.js +1 -5
  94. package/dist/plugins/powershell/restmethod/index.js +1 -5
  95. package/dist/plugins/powershell/restmethod/restmethod.js +13 -13
  96. package/dist/plugins/powershell/webrequest/index.js +1 -5
  97. package/dist/plugins/powershell/webrequest/webrequest.js +13 -13
  98. package/dist/plugins/python/httpx/async.js +17 -15
  99. package/dist/plugins/python/httpx/index.js +2 -7
  100. package/dist/plugins/python/httpx/sync.js +11 -12
  101. package/dist/plugins/python/python3/index.js +1 -5
  102. package/dist/plugins/python/python3/python3.js +13 -13
  103. package/dist/plugins/python/requests/index.js +1 -5
  104. package/dist/plugins/python/requests/requests.js +11 -12
  105. package/dist/plugins/python/requestsLike.js +128 -112
  106. package/dist/plugins/r/httr/httr.js +13 -13
  107. package/dist/plugins/r/httr/index.js +1 -5
  108. package/dist/plugins/ruby/native/index.js +1 -5
  109. package/dist/plugins/ruby/native/native.js +13 -13
  110. package/dist/plugins/rust/reqwest/index.js +1 -5
  111. package/dist/plugins/rust/reqwest/reqwest.js +110 -83
  112. package/dist/plugins/shell/curl/curl.js +128 -97
  113. package/dist/plugins/shell/curl/index.js +1 -5
  114. package/dist/plugins/shell/httpie/httpie.js +13 -13
  115. package/dist/plugins/shell/httpie/index.js +1 -5
  116. package/dist/plugins/shell/wget/index.js +1 -5
  117. package/dist/plugins/shell/wget/wget.js +13 -13
  118. package/dist/plugins/swift/nsurlsession/index.js +1 -5
  119. package/dist/plugins/swift/nsurlsession/nsurlsession.js +13 -13
  120. package/dist/snippetz.js +25 -28
  121. package/dist/utils/convertWithHttpSnippetLite.js +100 -96
  122. package/dist/utils/index.js +1 -5
  123. package/package.json +8 -12
  124. package/dist/clients/index.js.map +0 -7
  125. package/dist/httpsnippet-lite/helpers/code-builder.js.map +0 -7
  126. package/dist/httpsnippet-lite/helpers/escape.js.map +0 -7
  127. package/dist/httpsnippet-lite/helpers/headers.js.map +0 -7
  128. package/dist/httpsnippet-lite/helpers/reducer.js.map +0 -7
  129. package/dist/httpsnippet-lite/helpers/shell.js.map +0 -7
  130. package/dist/httpsnippet-lite/helpers/snippet-request.js.map +0 -7
  131. package/dist/httpsnippet-lite/helpers/url.js.map +0 -7
  132. package/dist/httpsnippet-lite/targets/c/libcurl/client.js.map +0 -7
  133. package/dist/httpsnippet-lite/targets/clojure/clj_http/client.js.map +0 -7
  134. package/dist/httpsnippet-lite/targets/csharp/restsharp/client.js.map +0 -7
  135. package/dist/httpsnippet-lite/targets/go/native/client.js.map +0 -7
  136. package/dist/httpsnippet-lite/targets/java/asynchttp/client.js.map +0 -7
  137. package/dist/httpsnippet-lite/targets/java/nethttp/client.js.map +0 -7
  138. package/dist/httpsnippet-lite/targets/java/okhttp/client.js.map +0 -7
  139. package/dist/httpsnippet-lite/targets/java/unirest/client.js.map +0 -7
  140. package/dist/httpsnippet-lite/targets/javascript/axios/client.js.map +0 -7
  141. package/dist/httpsnippet-lite/targets/javascript/jquery/client.js.map +0 -7
  142. package/dist/httpsnippet-lite/targets/javascript/xhr/client.js.map +0 -7
  143. package/dist/httpsnippet-lite/targets/kotlin/okhttp/client.js.map +0 -7
  144. package/dist/httpsnippet-lite/targets/node/axios/client.js.map +0 -7
  145. package/dist/httpsnippet-lite/targets/objc/helpers.js.map +0 -7
  146. package/dist/httpsnippet-lite/targets/objc/nsurlsession/client.js.map +0 -7
  147. package/dist/httpsnippet-lite/targets/ocaml/cohttp/client.js.map +0 -7
  148. package/dist/httpsnippet-lite/targets/powershell/common.js.map +0 -7
  149. package/dist/httpsnippet-lite/targets/powershell/restmethod/client.js.map +0 -7
  150. package/dist/httpsnippet-lite/targets/powershell/webrequest/client.js.map +0 -7
  151. package/dist/httpsnippet-lite/targets/python/python3/client.js.map +0 -7
  152. package/dist/httpsnippet-lite/targets/r/httr/client.js.map +0 -7
  153. package/dist/httpsnippet-lite/targets/ruby/native/client.js.map +0 -7
  154. package/dist/httpsnippet-lite/targets/shell/httpie/client.js.map +0 -7
  155. package/dist/httpsnippet-lite/targets/shell/wget/client.js.map +0 -7
  156. package/dist/httpsnippet-lite/targets/swift/helpers.js.map +0 -7
  157. package/dist/httpsnippet-lite/targets/swift/nsurlsession/client.js.map +0 -7
  158. package/dist/httpsnippet-lite/targets/target.js.map +0 -7
  159. package/dist/index.js.map +0 -7
  160. package/dist/libs/http.js.map +0 -7
  161. package/dist/libs/javascript.js.map +0 -7
  162. package/dist/libs/php.js.map +0 -7
  163. package/dist/libs/rust.js.map +0 -7
  164. package/dist/libs/shell.js.map +0 -7
  165. package/dist/plugins/c/libcurl/index.js.map +0 -7
  166. package/dist/plugins/c/libcurl/libcurl.js.map +0 -7
  167. package/dist/plugins/clojure/clj_http/clj_http.js.map +0 -7
  168. package/dist/plugins/clojure/clj_http/index.js.map +0 -7
  169. package/dist/plugins/csharp/httpclient/httpclient.js.map +0 -7
  170. package/dist/plugins/csharp/httpclient/index.js.map +0 -7
  171. package/dist/plugins/csharp/restsharp/index.js.map +0 -7
  172. package/dist/plugins/csharp/restsharp/restsharp.js.map +0 -7
  173. package/dist/plugins/dart/http/http.js.map +0 -7
  174. package/dist/plugins/dart/http/index.js.map +0 -7
  175. package/dist/plugins/fsharp/httpclient/httpclient.js.map +0 -7
  176. package/dist/plugins/fsharp/httpclient/index.js.map +0 -7
  177. package/dist/plugins/go/native/index.js.map +0 -7
  178. package/dist/plugins/go/native/native.js.map +0 -7
  179. package/dist/plugins/http/http11/http11.js.map +0 -7
  180. package/dist/plugins/http/http11/index.js.map +0 -7
  181. package/dist/plugins/java/asynchttp/asynchttp.js.map +0 -7
  182. package/dist/plugins/java/asynchttp/index.js.map +0 -7
  183. package/dist/plugins/java/nethttp/index.js.map +0 -7
  184. package/dist/plugins/java/nethttp/nethttp.js.map +0 -7
  185. package/dist/plugins/java/okhttp/index.js.map +0 -7
  186. package/dist/plugins/java/okhttp/okhttp.js.map +0 -7
  187. package/dist/plugins/java/unirest/index.js.map +0 -7
  188. package/dist/plugins/java/unirest/unirest.js.map +0 -7
  189. package/dist/plugins/js/axios/axios.js.map +0 -7
  190. package/dist/plugins/js/axios/index.js.map +0 -7
  191. package/dist/plugins/js/fetch/fetch.js.map +0 -7
  192. package/dist/plugins/js/fetch/index.js.map +0 -7
  193. package/dist/plugins/js/jquery/index.js.map +0 -7
  194. package/dist/plugins/js/jquery/jquery.js.map +0 -7
  195. package/dist/plugins/js/ofetch/index.js.map +0 -7
  196. package/dist/plugins/js/ofetch/ofetch.js.map +0 -7
  197. package/dist/plugins/js/xhr/index.js.map +0 -7
  198. package/dist/plugins/js/xhr/xhr.js.map +0 -7
  199. package/dist/plugins/kotlin/okhttp/index.js.map +0 -7
  200. package/dist/plugins/kotlin/okhttp/okhttp.js.map +0 -7
  201. package/dist/plugins/node/axios/axios.js.map +0 -7
  202. package/dist/plugins/node/axios/index.js.map +0 -7
  203. package/dist/plugins/node/fetch/fetch.js.map +0 -7
  204. package/dist/plugins/node/fetch/index.js.map +0 -7
  205. package/dist/plugins/node/ofetch/index.js.map +0 -7
  206. package/dist/plugins/node/ofetch/ofetch.js.map +0 -7
  207. package/dist/plugins/node/undici/index.js.map +0 -7
  208. package/dist/plugins/node/undici/undici.js.map +0 -7
  209. package/dist/plugins/objc/nsurlsession/index.js.map +0 -7
  210. package/dist/plugins/objc/nsurlsession/nsurlsession.js.map +0 -7
  211. package/dist/plugins/ocaml/cohttp/cohttp.js.map +0 -7
  212. package/dist/plugins/ocaml/cohttp/index.js.map +0 -7
  213. package/dist/plugins/php/curl/curl.js.map +0 -7
  214. package/dist/plugins/php/curl/index.js.map +0 -7
  215. package/dist/plugins/php/guzzle/guzzle.js.map +0 -7
  216. package/dist/plugins/php/guzzle/index.js.map +0 -7
  217. package/dist/plugins/powershell/restmethod/index.js.map +0 -7
  218. package/dist/plugins/powershell/restmethod/restmethod.js.map +0 -7
  219. package/dist/plugins/powershell/webrequest/index.js.map +0 -7
  220. package/dist/plugins/powershell/webrequest/webrequest.js.map +0 -7
  221. package/dist/plugins/python/httpx/async.js.map +0 -7
  222. package/dist/plugins/python/httpx/index.js.map +0 -7
  223. package/dist/plugins/python/httpx/sync.js.map +0 -7
  224. package/dist/plugins/python/python3/index.js.map +0 -7
  225. package/dist/plugins/python/python3/python3.js.map +0 -7
  226. package/dist/plugins/python/requests/index.js.map +0 -7
  227. package/dist/plugins/python/requests/requests.js.map +0 -7
  228. package/dist/plugins/python/requestsLike.js.map +0 -7
  229. package/dist/plugins/r/httr/httr.js.map +0 -7
  230. package/dist/plugins/r/httr/index.js.map +0 -7
  231. package/dist/plugins/ruby/native/index.js.map +0 -7
  232. package/dist/plugins/ruby/native/native.js.map +0 -7
  233. package/dist/plugins/rust/reqwest/index.js.map +0 -7
  234. package/dist/plugins/rust/reqwest/reqwest.js.map +0 -7
  235. package/dist/plugins/shell/curl/curl.js.map +0 -7
  236. package/dist/plugins/shell/curl/index.js.map +0 -7
  237. package/dist/plugins/shell/httpie/httpie.js.map +0 -7
  238. package/dist/plugins/shell/httpie/index.js.map +0 -7
  239. package/dist/plugins/shell/wget/index.js.map +0 -7
  240. package/dist/plugins/shell/wget/wget.js.map +0 -7
  241. package/dist/plugins/swift/nsurlsession/index.js.map +0 -7
  242. package/dist/plugins/swift/nsurlsession/nsurlsession.js.map +0 -7
  243. package/dist/snippetz.js.map +0 -7
  244. package/dist/utils/convertWithHttpSnippetLite.js.map +0 -7
  245. package/dist/utils/index.js.map +0 -7
@@ -1,77 +1,87 @@
1
- import { CodeBuilder } from "../../../../httpsnippet-lite/helpers/code-builder.js";
2
- import { escapeForDoubleQuotes } from "../../../../httpsnippet-lite/helpers/escape.js";
3
- const okhttp = {
4
- info: {
5
- key: "okhttp",
6
- title: "OkHttp",
7
- link: "http://square.github.io/okhttp/",
8
- description: "An HTTP Request Client Library"
9
- },
10
- convert: ({ postData, fullUrl, method, allHeaders }, options) => {
11
- const opts = {
12
- indent: " ",
13
- ...options
14
- };
15
- const { blank, join, push } = new CodeBuilder({ indent: opts.indent });
16
- const methods = ["GET", "POST", "PUT", "DELETE", "PATCH", "HEAD"];
17
- const methodsWithBody = ["POST", "PUT", "DELETE", "PATCH"];
18
- push("val client = OkHttpClient()");
19
- blank();
20
- if (postData !== null && postData !== void 0) {
21
- if (postData.mimeType === "application/x-www-form-urlencoded" && postData.params) {
22
- push("val formBody = FormBody.Builder()");
23
- postData.params.forEach((param) => {
24
- push(`.addEncoded("${param.name}", "${param.value}")`, 1);
25
- });
26
- push(".build()", 1);
1
+ /**
2
+ * @description
3
+ * HTTP code snippet generator for Kotlin using OkHttp.
4
+ *
5
+ * @author
6
+ * @seanghay
7
+ *
8
+ * for any questions or issues regarding the generated code snippet, please open an issue mentioning the author.
9
+ */
10
+ import { CodeBuilder } from '../../../../httpsnippet-lite/helpers/code-builder.js';
11
+ import { escapeForDoubleQuotes } from '../../../../httpsnippet-lite/helpers/escape.js';
12
+ export const okhttp = {
13
+ info: {
14
+ key: 'okhttp',
15
+ title: 'OkHttp',
16
+ link: 'http://square.github.io/okhttp/',
17
+ description: 'An HTTP Request Client Library',
18
+ },
19
+ convert: ({ postData, fullUrl, method, allHeaders }, options) => {
20
+ const opts = {
21
+ indent: ' ',
22
+ ...options,
23
+ };
24
+ const { blank, join, push } = new CodeBuilder({ indent: opts.indent });
25
+ const methods = ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'HEAD'];
26
+ const methodsWithBody = ['POST', 'PUT', 'DELETE', 'PATCH'];
27
+ push('val client = OkHttpClient()');
27
28
  blank();
28
- } else if (postData.mimeType === "multipart/form-data" && postData.params) {
29
- push("val body = MultipartBody.Builder()");
30
- push(".setType(MultipartBody.FORM)", 1);
31
- postData.params.forEach((param) => {
32
- if (param.fileName !== void 0) {
33
- push(
34
- `.addFormDataPart("${param.name}", "${param.fileName}", RequestBody.create(MediaType.parse("application/octet-stream"), File("${param.fileName}")))`,
35
- 1
36
- );
37
- } else if (param.value !== void 0) {
38
- push(`.addFormDataPart("${param.name}", "${param.value}")`, 1);
39
- }
29
+ if (postData !== null && postData !== void 0) {
30
+ if (postData.mimeType === 'application/x-www-form-urlencoded' && postData.params) {
31
+ push('val formBody = FormBody.Builder()');
32
+ postData.params.forEach((param) => {
33
+ push(`.addEncoded("${param.name}", "${param.value}")`, 1);
34
+ });
35
+ push('.build()', 1);
36
+ blank();
37
+ }
38
+ else if (postData.mimeType === 'multipart/form-data' && postData.params) {
39
+ push('val body = MultipartBody.Builder()');
40
+ push('.setType(MultipartBody.FORM)', 1);
41
+ postData.params.forEach((param) => {
42
+ if (param.fileName !== undefined) {
43
+ push(`.addFormDataPart("${param.name}", "${param.fileName}", RequestBody.create(MediaType.parse("application/octet-stream"), File("${param.fileName}")))`, 1);
44
+ }
45
+ else if (param.value !== undefined) {
46
+ push(`.addFormDataPart("${param.name}", "${param.value}")`, 1);
47
+ }
48
+ });
49
+ push('.build()', 1);
50
+ blank();
51
+ }
52
+ else {
53
+ push(`val mediaType = MediaType.parse("${postData.mimeType}")`);
54
+ push(`val body = RequestBody.create(mediaType, ${JSON.stringify(postData.text)})`);
55
+ }
56
+ }
57
+ push('val request = Request.Builder()');
58
+ push(`.url("${fullUrl}")`, 1);
59
+ if (!methods.includes(method.toUpperCase())) {
60
+ if (postData === null || postData === void 0 ? void 0 : postData.text) {
61
+ push(`.method("${method.toUpperCase()}", body)`, 1);
62
+ }
63
+ else {
64
+ push(`.method("${method.toUpperCase()}", null)`, 1);
65
+ }
66
+ }
67
+ else if (methodsWithBody.includes(method.toUpperCase())) {
68
+ if (postData === null || postData === void 0 ? void 0 : postData.text || postData.params) {
69
+ push(`.${method.toLowerCase()}(body)`, 1);
70
+ }
71
+ else {
72
+ push(`.${method.toLowerCase()}(null)`, 1);
73
+ }
74
+ }
75
+ else {
76
+ push(`.${method.toLowerCase()}()`, 1);
77
+ }
78
+ // Add headers, including the cookies
79
+ Object.keys(allHeaders).forEach((key) => {
80
+ push(`.addHeader("${key}", "${escapeForDoubleQuotes(allHeaders[key])}")`, 1);
40
81
  });
41
- push(".build()", 1);
82
+ push('.build()', 1);
42
83
  blank();
43
- } else {
44
- push(`val mediaType = MediaType.parse("${postData.mimeType}")`);
45
- push(`val body = RequestBody.create(mediaType, ${JSON.stringify(postData.text)})`);
46
- }
47
- }
48
- push("val request = Request.Builder()");
49
- push(`.url("${fullUrl}")`, 1);
50
- if (!methods.includes(method.toUpperCase())) {
51
- if (postData === null || postData === void 0 ? void 0 : postData.text) {
52
- push(`.method("${method.toUpperCase()}", body)`, 1);
53
- } else {
54
- push(`.method("${method.toUpperCase()}", null)`, 1);
55
- }
56
- } else if (methodsWithBody.includes(method.toUpperCase())) {
57
- if (postData === null || postData === void 0 ? void 0 : postData.text || postData.params) {
58
- push(`.${method.toLowerCase()}(body)`, 1);
59
- } else {
60
- push(`.${method.toLowerCase()}(null)`, 1);
61
- }
62
- } else {
63
- push(`.${method.toLowerCase()}()`, 1);
64
- }
65
- Object.keys(allHeaders).forEach((key) => {
66
- push(`.addHeader("${key}", "${escapeForDoubleQuotes(allHeaders[key])}")`, 1);
67
- });
68
- push(".build()", 1);
69
- blank();
70
- push("val response = client.newCall(request).execute()");
71
- return join();
72
- }
73
- };
74
- export {
75
- okhttp
84
+ push('val response = client.newCall(request).execute()');
85
+ return join();
86
+ },
76
87
  };
77
- //# sourceMappingURL=client.js.map
@@ -1,73 +1,78 @@
1
- import stringifyObject from "stringify-object";
2
- import { CodeBuilder } from "../../../../httpsnippet-lite/helpers/code-builder.js";
3
- const axios = {
4
- info: {
5
- key: "axios",
6
- title: "Axios",
7
- link: "https://github.com/axios/axios",
8
- description: "Promise based HTTP client for the browser and node.js"
9
- },
10
- convert: ({ method, url, queryObj, allHeaders, postData }, options) => {
11
- const opts = {
12
- indent: " ",
13
- ...options
14
- };
15
- const { blank, join, push, addPostProcessor } = new CodeBuilder({
16
- indent: opts.indent
17
- });
18
- push("const axios = require('axios').default;");
19
- const reqOpts = {
20
- method,
21
- url
22
- };
23
- if (Object.keys(queryObj).length) {
24
- reqOpts.params = queryObj;
25
- }
26
- if (Object.keys(allHeaders).length) {
27
- reqOpts.headers = allHeaders;
28
- }
29
- switch (postData === null || postData === void 0 ? void 0 : postData.mimeType) {
30
- case "application/x-www-form-urlencoded":
31
- if (postData.params) {
32
- push("const { URLSearchParams } = require('url');");
33
- blank();
34
- push("const encodedParams = new URLSearchParams();");
35
- postData.params.forEach((param) => {
36
- push(`encodedParams.set('${param.name}', '${param.value}');`);
37
- });
38
- blank();
39
- reqOpts.data = "encodedParams,";
40
- addPostProcessor((code) => code.replace(/'encodedParams,'/, "encodedParams,"));
1
+ /**
2
+ * @description
3
+ * HTTP code snippet generator for Javascript & Node.js using Axios.
4
+ *
5
+ * @author
6
+ * @rohit-gohri
7
+ *
8
+ * for any questions or issues regarding the generated code snippet, please open an issue mentioning the author.
9
+ */
10
+ import stringifyObject from 'stringify-object';
11
+ import { CodeBuilder } from '../../../../httpsnippet-lite/helpers/code-builder.js';
12
+ export const axios = {
13
+ info: {
14
+ key: 'axios',
15
+ title: 'Axios',
16
+ link: 'https://github.com/axios/axios',
17
+ description: 'Promise based HTTP client for the browser and node.js',
18
+ },
19
+ convert: ({ method, url, queryObj, allHeaders, postData }, options) => {
20
+ const opts = {
21
+ indent: ' ',
22
+ ...options,
23
+ };
24
+ const { blank, join, push, addPostProcessor } = new CodeBuilder({
25
+ indent: opts.indent,
26
+ });
27
+ push("const axios = require('axios').default;");
28
+ const reqOpts = {
29
+ method,
30
+ url,
31
+ };
32
+ if (Object.keys(queryObj).length) {
33
+ reqOpts.params = queryObj;
41
34
  }
42
- break;
43
- case "application/json":
44
- blank();
45
- if (postData?.jsonObj) {
46
- reqOpts.data = postData.jsonObj;
35
+ if (Object.keys(allHeaders).length) {
36
+ reqOpts.headers = allHeaders;
47
37
  }
48
- break;
49
- default:
50
- blank();
51
- if (postData === null || postData === void 0 ? void 0 : postData.text) {
52
- reqOpts.data = postData.text;
38
+ switch (postData === null || postData === void 0 ? void 0 : postData.mimeType) {
39
+ case 'application/x-www-form-urlencoded':
40
+ if (postData.params) {
41
+ push("const { URLSearchParams } = require('url');");
42
+ blank();
43
+ push('const encodedParams = new URLSearchParams();');
44
+ postData.params.forEach((param) => {
45
+ push(`encodedParams.set('${param.name}', '${param.value}');`);
46
+ });
47
+ blank();
48
+ reqOpts.data = 'encodedParams,';
49
+ addPostProcessor((code) => code.replace(/'encodedParams,'/, 'encodedParams,'));
50
+ }
51
+ break;
52
+ case 'application/json':
53
+ blank();
54
+ if (postData?.jsonObj) {
55
+ reqOpts.data = postData.jsonObj;
56
+ }
57
+ break;
58
+ default:
59
+ blank();
60
+ if (postData === null || postData === void 0 ? void 0 : postData.text) {
61
+ reqOpts.data = postData.text;
62
+ }
53
63
  }
54
- }
55
- const stringifiedOptions = stringifyObject(reqOpts, {
56
- indent: " ",
57
- inlineCharacterLimit: 80
58
- });
59
- push(`const options = ${stringifiedOptions};`);
60
- blank();
61
- push("try {");
62
- push("const { data } = await axios.request(options);", 1);
63
- push("console.log(data);", 1);
64
- push("} catch (error) {");
65
- push("console.error(error);", 1);
66
- push("}");
67
- return join();
68
- }
69
- };
70
- export {
71
- axios
64
+ const stringifiedOptions = stringifyObject(reqOpts, {
65
+ indent: ' ',
66
+ inlineCharacterLimit: 80,
67
+ });
68
+ push(`const options = ${stringifiedOptions};`);
69
+ blank();
70
+ push('try {');
71
+ push('const { data } = await axios.request(options);', 1);
72
+ push('console.log(data);', 1);
73
+ push('} catch (error) {');
74
+ push('console.error(error);', 1);
75
+ push('}');
76
+ return join();
77
+ },
72
78
  };
73
- //# sourceMappingURL=client.js.map
@@ -1,38 +1,57 @@
1
- const nsDeclaration = (nsClass, name, parameters, indent) => {
2
- const opening = `${nsClass} *${name} = `;
3
- const literal = literalRepresentation(parameters, indent ? opening.length : void 0);
4
- return `${opening}${literal};`;
1
+ /**
2
+ * Create a string corresponding to a valid declaration and initialization of an Objective-C object literal.
3
+ *
4
+ * @param nsClass Class of the literal
5
+ * @param name Desired name of the instance
6
+ * @param parameters Key-value object of parameters to translate to an Objective-C object literal
7
+ * @param indent If true, will declare the literal by indenting each new key/value pair.
8
+ * @return A valid Objective-C declaration and initialization of an Objective-C object literal.
9
+ *
10
+ * @example
11
+ * nsDeclaration('NSDictionary', 'params', {a: 'b', c: 'd'}, true)
12
+ * // returns:
13
+ * NSDictionary *params = @{ @"a": @"b",
14
+ * @"c": @"d" };
15
+ *
16
+ * nsDeclaration('NSDictionary', 'params', {a: 'b', c: 'd'})
17
+ * // returns:
18
+ * NSDictionary *params = @{ @"a": @"b", @"c": @"d" };
19
+ */
20
+ export const nsDeclaration = (nsClass, name, parameters, indent) => {
21
+ const opening = `${nsClass} *${name} = `;
22
+ const literal = literalRepresentation(parameters, indent ? opening.length : undefined);
23
+ return `${opening}${literal};`;
5
24
  };
25
+ /**
26
+ * Create a valid Objective-C string of a literal value according to its type.
27
+ *
28
+ * @param value Any JavaScript literal
29
+ */
6
30
  const literalRepresentation = (value, indentation) => {
7
- const join = indentation === void 0 ? ", " : `,
8
- ${" ".repeat(indentation)}`;
9
- switch (Object.prototype.toString.call(value)) {
10
- case "[object Number]":
11
- return `@${value}`;
12
- case "[object Array]": {
13
- const valuesRepresentation = value.map((value2) => literalRepresentation(value2));
14
- return `@[ ${valuesRepresentation.join(join)} ]`;
15
- }
16
- case "[object Object]": {
17
- const keyValuePairs = [];
18
- const _value = value;
19
- for (const key in _value) {
20
- if (Object.hasOwn(_value, key)) {
21
- keyValuePairs.push(`@"${key}": ${literalRepresentation(_value[key])}`);
31
+ const join = indentation === undefined ? ', ' : `,\n ${' '.repeat(indentation)}`;
32
+ switch (Object.prototype.toString.call(value)) {
33
+ case '[object Number]':
34
+ return `@${value}`;
35
+ case '[object Array]': {
36
+ const valuesRepresentation = value.map((value) => literalRepresentation(value));
37
+ return `@[ ${valuesRepresentation.join(join)} ]`;
38
+ }
39
+ case '[object Object]': {
40
+ const keyValuePairs = [];
41
+ const _value = value;
42
+ for (const key in _value) {
43
+ if (Object.hasOwn(_value, key)) {
44
+ keyValuePairs.push(`@"${key}": ${literalRepresentation(_value[key])}`);
45
+ }
46
+ }
47
+ return `@{ ${keyValuePairs.join(join)} }`;
22
48
  }
23
- }
24
- return `@{ ${keyValuePairs.join(join)} }`;
49
+ case '[object Boolean]':
50
+ return value ? '@YES' : '@NO';
51
+ default:
52
+ if (value === null || value === undefined) {
53
+ return '';
54
+ }
55
+ return `@"${value.toString().replace(/"/g, '\\"')}"`;
25
56
  }
26
- case "[object Boolean]":
27
- return value ? "@YES" : "@NO";
28
- default:
29
- if (value === null || value === void 0) {
30
- return "";
31
- }
32
- return `@"${value.toString().replace(/"/g, '\\"')}"`;
33
- }
34
- };
35
- export {
36
- nsDeclaration
37
57
  };
38
- //# sourceMappingURL=helpers.js.map