@ohos-ports/curlconverter 4.12.0-beta.0

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 (270) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +153 -0
  3. package/dist/src/Headers.d.ts +23 -0
  4. package/dist/src/Headers.js +287 -0
  5. package/dist/src/Headers.js.map +1 -0
  6. package/dist/src/Query.d.ts +8 -0
  7. package/dist/src/Query.js +136 -0
  8. package/dist/src/Query.js.map +1 -0
  9. package/dist/src/Request.d.ts +200 -0
  10. package/dist/src/Request.js +1191 -0
  11. package/dist/src/Request.js.map +1 -0
  12. package/dist/src/Warnings.d.ts +15 -0
  13. package/dist/src/Warnings.js +111 -0
  14. package/dist/src/Warnings.js.map +1 -0
  15. package/dist/src/cli.d.ts +2 -0
  16. package/dist/src/cli.js +333 -0
  17. package/dist/src/cli.js.map +1 -0
  18. package/dist/src/curl/auth.d.ts +12 -0
  19. package/dist/src/curl/auth.js +40 -0
  20. package/dist/src/curl/auth.js.map +1 -0
  21. package/dist/src/curl/form.d.ts +28 -0
  22. package/dist/src/curl/form.js +218 -0
  23. package/dist/src/curl/form.js.map +1 -0
  24. package/dist/src/curl/opts.d.ts +2052 -0
  25. package/dist/src/curl/opts.js +1000 -0
  26. package/dist/src/curl/opts.js.map +1 -0
  27. package/dist/src/curl/url.d.ts +14 -0
  28. package/dist/src/curl/url.js +218 -0
  29. package/dist/src/curl/url.js.map +1 -0
  30. package/dist/src/generators/ansible.d.ts +5 -0
  31. package/dist/src/generators/ansible.js +282 -0
  32. package/dist/src/generators/ansible.js.map +1 -0
  33. package/dist/src/generators/c.d.ts +12 -0
  34. package/dist/src/generators/c.js +1230 -0
  35. package/dist/src/generators/c.js.map +1 -0
  36. package/dist/src/generators/cfml.d.ts +5 -0
  37. package/dist/src/generators/cfml.js +134 -0
  38. package/dist/src/generators/cfml.js.map +1 -0
  39. package/dist/src/generators/clojure.d.ts +7 -0
  40. package/dist/src/generators/clojure.js +436 -0
  41. package/dist/src/generators/clojure.js.map +1 -0
  42. package/dist/src/generators/csharp.d.ts +8 -0
  43. package/dist/src/generators/csharp.js +356 -0
  44. package/dist/src/generators/csharp.js.map +1 -0
  45. package/dist/src/generators/dart.d.ts +5 -0
  46. package/dist/src/generators/dart.js +273 -0
  47. package/dist/src/generators/dart.js.map +1 -0
  48. package/dist/src/generators/elixir.d.ts +8 -0
  49. package/dist/src/generators/elixir.js +296 -0
  50. package/dist/src/generators/elixir.js.map +1 -0
  51. package/dist/src/generators/go.d.ts +5 -0
  52. package/dist/src/generators/go.js +228 -0
  53. package/dist/src/generators/go.js.map +1 -0
  54. package/dist/src/generators/har.d.ts +7 -0
  55. package/dist/src/generators/har.js +115 -0
  56. package/dist/src/generators/har.js.map +1 -0
  57. package/dist/src/generators/http.d.ts +7 -0
  58. package/dist/src/generators/http.js +165 -0
  59. package/dist/src/generators/http.js.map +1 -0
  60. package/dist/src/generators/httpie.d.ts +5 -0
  61. package/dist/src/generators/httpie.js +446 -0
  62. package/dist/src/generators/httpie.js.map +1 -0
  63. package/dist/src/generators/java/httpurlconnection.d.ts +5 -0
  64. package/dist/src/generators/java/httpurlconnection.js +123 -0
  65. package/dist/src/generators/java/httpurlconnection.js.map +1 -0
  66. package/dist/src/generators/java/java.d.ts +8 -0
  67. package/dist/src/generators/java/java.js +247 -0
  68. package/dist/src/generators/java/java.js.map +1 -0
  69. package/dist/src/generators/java/jsoup.d.ts +5 -0
  70. package/dist/src/generators/java/jsoup.js +152 -0
  71. package/dist/src/generators/java/jsoup.js.map +1 -0
  72. package/dist/src/generators/java/okhttp.d.ts +5 -0
  73. package/dist/src/generators/java/okhttp.js +232 -0
  74. package/dist/src/generators/java/okhttp.js.map +1 -0
  75. package/dist/src/generators/javascript/axios.d.ts +5 -0
  76. package/dist/src/generators/javascript/axios.js +332 -0
  77. package/dist/src/generators/javascript/axios.js.map +1 -0
  78. package/dist/src/generators/javascript/got.d.ts +5 -0
  79. package/dist/src/generators/javascript/got.js +277 -0
  80. package/dist/src/generators/javascript/got.js.map +1 -0
  81. package/dist/src/generators/javascript/http.d.ts +8 -0
  82. package/dist/src/generators/javascript/http.js +183 -0
  83. package/dist/src/generators/javascript/http.js.map +1 -0
  84. package/dist/src/generators/javascript/javascript.d.ts +37 -0
  85. package/dist/src/generators/javascript/javascript.js +676 -0
  86. package/dist/src/generators/javascript/javascript.js.map +1 -0
  87. package/dist/src/generators/javascript/jquery.d.ts +15 -0
  88. package/dist/src/generators/javascript/jquery.js +297 -0
  89. package/dist/src/generators/javascript/jquery.js.map +1 -0
  90. package/dist/src/generators/javascript/ky.d.ts +7 -0
  91. package/dist/src/generators/javascript/ky.js +245 -0
  92. package/dist/src/generators/javascript/ky.js.map +1 -0
  93. package/dist/src/generators/javascript/request.d.ts +5 -0
  94. package/dist/src/generators/javascript/request.js +113 -0
  95. package/dist/src/generators/javascript/request.js.map +1 -0
  96. package/dist/src/generators/javascript/superagent.d.ts +10 -0
  97. package/dist/src/generators/javascript/superagent.js +301 -0
  98. package/dist/src/generators/javascript/superagent.js.map +1 -0
  99. package/dist/src/generators/javascript/xhr.d.ts +8 -0
  100. package/dist/src/generators/javascript/xhr.js +147 -0
  101. package/dist/src/generators/javascript/xhr.js.map +1 -0
  102. package/dist/src/generators/json.d.ts +45 -0
  103. package/dist/src/generators/json.js +197 -0
  104. package/dist/src/generators/json.js.map +1 -0
  105. package/dist/src/generators/julia.d.ts +8 -0
  106. package/dist/src/generators/julia.js +365 -0
  107. package/dist/src/generators/julia.js.map +1 -0
  108. package/dist/src/generators/kotlin.d.ts +8 -0
  109. package/dist/src/generators/kotlin.js +325 -0
  110. package/dist/src/generators/kotlin.js.map +1 -0
  111. package/dist/src/generators/lua.d.ts +5 -0
  112. package/dist/src/generators/lua.js +192 -0
  113. package/dist/src/generators/lua.js.map +1 -0
  114. package/dist/src/generators/matlab/common.d.ts +16 -0
  115. package/dist/src/generators/matlab/common.js +238 -0
  116. package/dist/src/generators/matlab/common.js.map +1 -0
  117. package/dist/src/generators/matlab/httpinterface.d.ts +2 -0
  118. package/dist/src/generators/matlab/httpinterface.js +211 -0
  119. package/dist/src/generators/matlab/httpinterface.js.map +1 -0
  120. package/dist/src/generators/matlab/matlab.d.ts +5 -0
  121. package/dist/src/generators/matlab/matlab.js +29 -0
  122. package/dist/src/generators/matlab/matlab.js.map +1 -0
  123. package/dist/src/generators/matlab/webservices.d.ts +2 -0
  124. package/dist/src/generators/matlab/webservices.js +191 -0
  125. package/dist/src/generators/matlab/webservices.js.map +1 -0
  126. package/dist/src/generators/objectivec.d.ts +8 -0
  127. package/dist/src/generators/objectivec.js +269 -0
  128. package/dist/src/generators/objectivec.js.map +1 -0
  129. package/dist/src/generators/ocaml.d.ts +8 -0
  130. package/dist/src/generators/ocaml.js +150 -0
  131. package/dist/src/generators/ocaml.js.map +1 -0
  132. package/dist/src/generators/perl.d.ts +9 -0
  133. package/dist/src/generators/perl.js +209 -0
  134. package/dist/src/generators/perl.js.map +1 -0
  135. package/dist/src/generators/php/guzzle.d.ts +5 -0
  136. package/dist/src/generators/php/guzzle.js +328 -0
  137. package/dist/src/generators/php/guzzle.js.map +1 -0
  138. package/dist/src/generators/php/php.d.ts +8 -0
  139. package/dist/src/generators/php/php.js +217 -0
  140. package/dist/src/generators/php/php.js.map +1 -0
  141. package/dist/src/generators/php/requests.d.ts +5 -0
  142. package/dist/src/generators/php/requests.js +89 -0
  143. package/dist/src/generators/php/requests.js.map +1 -0
  144. package/dist/src/generators/powershell.d.ts +11 -0
  145. package/dist/src/generators/powershell.js +377 -0
  146. package/dist/src/generators/powershell.js.map +1 -0
  147. package/dist/src/generators/python/http.d.ts +5 -0
  148. package/dist/src/generators/python/http.js +128 -0
  149. package/dist/src/generators/python/http.js.map +1 -0
  150. package/dist/src/generators/python/python.d.ts +22 -0
  151. package/dist/src/generators/python/python.js +1693 -0
  152. package/dist/src/generators/python/python.js.map +1 -0
  153. package/dist/src/generators/r/httr.d.ts +10 -0
  154. package/dist/src/generators/r/httr.js +298 -0
  155. package/dist/src/generators/r/httr.js.map +1 -0
  156. package/dist/src/generators/r/httr2.d.ts +5 -0
  157. package/dist/src/generators/r/httr2.js +214 -0
  158. package/dist/src/generators/r/httr2.js.map +1 -0
  159. package/dist/src/generators/ruby/httparty.d.ts +6 -0
  160. package/dist/src/generators/ruby/httparty.js +308 -0
  161. package/dist/src/generators/ruby/httparty.js.map +1 -0
  162. package/dist/src/generators/ruby/ruby.d.ts +14 -0
  163. package/dist/src/generators/ruby/ruby.js +515 -0
  164. package/dist/src/generators/ruby/ruby.js.map +1 -0
  165. package/dist/src/generators/rust.d.ts +8 -0
  166. package/dist/src/generators/rust.js +177 -0
  167. package/dist/src/generators/rust.js.map +1 -0
  168. package/dist/src/generators/swift.d.ts +8 -0
  169. package/dist/src/generators/swift.js +219 -0
  170. package/dist/src/generators/swift.js.map +1 -0
  171. package/dist/src/generators/wget.d.ts +8 -0
  172. package/dist/src/generators/wget.js +472 -0
  173. package/dist/src/generators/wget.js.map +1 -0
  174. package/dist/src/index.d.ts +53 -0
  175. package/dist/src/index.js +54 -0
  176. package/dist/src/index.js.map +1 -0
  177. package/dist/src/parse.d.ts +13 -0
  178. package/dist/src/parse.js +43 -0
  179. package/dist/src/parse.js.map +1 -0
  180. package/dist/src/shell/Parser.d.ts +4 -0
  181. package/dist/src/shell/Parser.js +6 -0
  182. package/dist/src/shell/Parser.js.map +1 -0
  183. package/dist/src/shell/Word.d.ts +48 -0
  184. package/dist/src/shell/Word.js +486 -0
  185. package/dist/src/shell/Word.js.map +1 -0
  186. package/dist/src/shell/tokenizer.d.ts +3 -0
  187. package/dist/src/shell/tokenizer.js +610 -0
  188. package/dist/src/shell/tokenizer.js.map +1 -0
  189. package/dist/src/shell/webParser.d.ts +4 -0
  190. package/dist/src/shell/webParser.js +15 -0
  191. package/dist/src/shell/webParser.js.map +1 -0
  192. package/dist/src/utils.d.ts +6 -0
  193. package/dist/src/utils.js +21 -0
  194. package/dist/src/utils.js.map +1 -0
  195. package/dist/tools/compare-requests.d.ts +2 -0
  196. package/dist/tools/compare-requests.js +448 -0
  197. package/dist/tools/compare-requests.js.map +1 -0
  198. package/dist/tools/gen-test.d.ts +2 -0
  199. package/dist/tools/gen-test.js +114 -0
  200. package/dist/tools/gen-test.js.map +1 -0
  201. package/dist/tree-sitter-bash.wasm +0 -0
  202. package/index.d.ts +3 -0
  203. package/package.json +97 -0
  204. package/src/Headers.ts +334 -0
  205. package/src/Query.ts +147 -0
  206. package/src/Request.ts +1649 -0
  207. package/src/Warnings.ts +154 -0
  208. package/src/cli.ts +561 -0
  209. package/src/curl/auth.ts +45 -0
  210. package/src/curl/form.ts +297 -0
  211. package/src/curl/opts.ts +1417 -0
  212. package/src/curl/url.ts +241 -0
  213. package/src/generators/ansible.ts +364 -0
  214. package/src/generators/c.ts +1371 -0
  215. package/src/generators/cfml.ts +155 -0
  216. package/src/generators/clojure.ts +521 -0
  217. package/src/generators/csharp.ts +383 -0
  218. package/src/generators/dart.ts +293 -0
  219. package/src/generators/elixir.ts +349 -0
  220. package/src/generators/go.ts +269 -0
  221. package/src/generators/har.ts +147 -0
  222. package/src/generators/http.ts +194 -0
  223. package/src/generators/httpie.ts +506 -0
  224. package/src/generators/java/httpurlconnection.ts +151 -0
  225. package/src/generators/java/java.ts +280 -0
  226. package/src/generators/java/jsoup.ts +172 -0
  227. package/src/generators/java/okhttp.ts +257 -0
  228. package/src/generators/javascript/axios.ts +395 -0
  229. package/src/generators/javascript/got.ts +362 -0
  230. package/src/generators/javascript/http.ts +232 -0
  231. package/src/generators/javascript/javascript.ts +825 -0
  232. package/src/generators/javascript/jquery.ts +360 -0
  233. package/src/generators/javascript/ky.ts +315 -0
  234. package/src/generators/javascript/request.ts +152 -0
  235. package/src/generators/javascript/superagent.ts +373 -0
  236. package/src/generators/javascript/xhr.ts +192 -0
  237. package/src/generators/json.ts +293 -0
  238. package/src/generators/julia.ts +398 -0
  239. package/src/generators/kotlin.ts +355 -0
  240. package/src/generators/lua.ts +209 -0
  241. package/src/generators/matlab/common.ts +281 -0
  242. package/src/generators/matlab/httpinterface.ts +284 -0
  243. package/src/generators/matlab/matlab.ts +39 -0
  244. package/src/generators/matlab/webservices.ts +263 -0
  245. package/src/generators/objectivec.ts +297 -0
  246. package/src/generators/ocaml.ts +168 -0
  247. package/src/generators/perl.ts +237 -0
  248. package/src/generators/php/guzzle.ts +368 -0
  249. package/src/generators/php/php.ts +239 -0
  250. package/src/generators/php/requests.ts +103 -0
  251. package/src/generators/powershell.ts +430 -0
  252. package/src/generators/python/http.ts +167 -0
  253. package/src/generators/python/python.ts +1934 -0
  254. package/src/generators/r/httr.ts +328 -0
  255. package/src/generators/r/httr2.ts +268 -0
  256. package/src/generators/ruby/httparty.ts +351 -0
  257. package/src/generators/ruby/ruby.ts +564 -0
  258. package/src/generators/rust.ts +243 -0
  259. package/src/generators/swift.ts +235 -0
  260. package/src/generators/wget.ts +533 -0
  261. package/src/index.ts +89 -0
  262. package/src/parse.ts +72 -0
  263. package/src/shell/Parser.ts +8 -0
  264. package/src/shell/Word.ts +531 -0
  265. package/src/shell/tokenizer.ts +736 -0
  266. package/src/shell/webParser.ts +18 -0
  267. package/src/utils.ts +25 -0
  268. package/tools/compare-requests.ts +571 -0
  269. package/tools/extract_curl_args.py +511 -0
  270. package/tools/gen-test.ts +129 -0
@@ -0,0 +1,281 @@
1
+ import { CCError } from "../../utils.js";
2
+ import { Word } from "../../shell/Word.js";
3
+ import type { Request } from "../../parse.js";
4
+
5
+ // Wrap in brakets so that splitting keeps the characters to escape
6
+ const regexEscape = /(\p{C}|[^ \P{Z}])/gu;
7
+ // TODO: do we need to consider that some strings could be used
8
+ // with sprintf() and have to have more stuff escaped?
9
+ function strToParts(s: string): string[] {
10
+ if (!s) {
11
+ return ["''"];
12
+ }
13
+ const parts = s
14
+ .replace(/'/g, "''")
15
+ .split(regexEscape)
16
+ .filter((x) => x) // empty strings between consecutive matches
17
+ .flatMap((x) => {
18
+ if (x.match(regexEscape)) {
19
+ if (x.length === 1) {
20
+ switch (x) {
21
+ case "\x07":
22
+ return "sprintf('\\a')";
23
+ case "\b":
24
+ return "sprintf('\\b')";
25
+ case "\f":
26
+ return "sprintf('\\f')";
27
+ case "\n":
28
+ return "newline";
29
+ case "\r":
30
+ return "sprintf('\\r')";
31
+ case "\t":
32
+ return "sprintf('\\t')";
33
+ case "\v":
34
+ return "sprintf('\\v')";
35
+ default:
36
+ return `char(${x.charCodeAt(0)})`;
37
+ }
38
+ } else {
39
+ return [`char(${x.charCodeAt(0)})`, `char(${x.charCodeAt(1)})`];
40
+ }
41
+ }
42
+ return "'" + x + "'";
43
+ });
44
+ return parts;
45
+ }
46
+ function joinParts(parts: string[]): string {
47
+ if (parts.length > 1) {
48
+ return "[" + parts.join(" ") + "]";
49
+ }
50
+ if (parts.length === 0) {
51
+ return "''"; // shouldn't happen
52
+ }
53
+ return parts[0];
54
+ }
55
+ function reprStr(s: string): string {
56
+ return joinParts(strToParts(s));
57
+ }
58
+ function repr(w: Word | null) {
59
+ // In context of url parameters, don't accept nulls and such.
60
+ if (w === null || w.length === 0) {
61
+ return "''";
62
+ }
63
+
64
+ let parts: string[] = [];
65
+ for (const t of w.tokens) {
66
+ if (typeof t === "string") {
67
+ parts = parts.concat(strToParts(t));
68
+ } else if (t.type === "variable") {
69
+ // https://www.mathworks.com/help/matlab/ref/getenv.html
70
+ parts.push("getenv(" + reprStr(t.value) + ")");
71
+ } else {
72
+ // TODO: is this array access correct?
73
+ // https://www.mathworks.com/help/matlab/ref/system.html
74
+ parts.push("system(" + reprStr(t.value) + "){2}");
75
+ }
76
+ }
77
+ return joinParts(parts);
78
+ }
79
+
80
+ function setVariableValue(
81
+ outputVariable: string | null,
82
+ value: string,
83
+ termination?: string,
84
+ ): string {
85
+ let result = "";
86
+
87
+ if (outputVariable) {
88
+ result += outputVariable + " = ";
89
+ }
90
+
91
+ result += value;
92
+ result +=
93
+ typeof termination === "undefined" || termination === null
94
+ ? ";"
95
+ : termination;
96
+ return result;
97
+ }
98
+
99
+ function callFunction(
100
+ outputVariable: string | null,
101
+ functionName: string,
102
+ params: string | string[] | string[][],
103
+ termination?: string,
104
+ ) {
105
+ let functionCall = functionName + "(";
106
+ if (Array.isArray(params)) {
107
+ const singleLine = params
108
+ .map((x) => (Array.isArray(x) ? x.join(", ") : x))
109
+ .join(", ");
110
+ const indentLevel = 1;
111
+ const indent = " ".repeat(4 * indentLevel);
112
+ const skipToNextLine = "...\n" + indent;
113
+ let multiLine = skipToNextLine;
114
+ multiLine += params
115
+ .map((x) => (Array.isArray(x) ? x.join(", ") : x))
116
+ .join("," + skipToNextLine);
117
+ multiLine += "...\n";
118
+
119
+ // Split the params in multiple lines - if one line is not enough
120
+ const combinedSingleLineLength =
121
+ [outputVariable, functionName, singleLine]
122
+ .map((x) => (x ? x.length : 0))
123
+ .reduce((x, y) => x + y) +
124
+ (outputVariable ? 3 : 0) +
125
+ 2 +
126
+ (termination ? termination.length : 1);
127
+ functionCall += combinedSingleLineLength < 120 ? singleLine : multiLine;
128
+ } else {
129
+ functionCall += params;
130
+ }
131
+ functionCall += ")";
132
+ return setVariableValue(outputVariable, functionCall, termination);
133
+ }
134
+
135
+ function addCellArray(
136
+ mapping: ([Word, Word] | [string, Word | string])[],
137
+ keysNotToQuote?: string[],
138
+ indentLevel = 1,
139
+ pairs?: boolean,
140
+ ) {
141
+ if (mapping.length === 0) return ""; // shouldn't happen
142
+
143
+ const indentUnit = " ".repeat(4);
144
+ const indent = indentUnit.repeat(indentLevel);
145
+ const indentPrevLevel = indentUnit.repeat(indentLevel - 1);
146
+ const separator = pairs ? ", " : " ";
147
+
148
+ let response = "";
149
+ if (!pairs) {
150
+ response += "{";
151
+ }
152
+ if (pairs && mapping.length > 1) {
153
+ response += "...";
154
+ }
155
+ for (const [counter, [key, value]] of mapping.entries()) {
156
+ const k = typeof key === "string" ? reprStr(key) : repr(key);
157
+ let val: string;
158
+ if (
159
+ keysNotToQuote &&
160
+ keysNotToQuote.includes(key.toLowerCase().toString())
161
+ ) {
162
+ val = value.toString();
163
+ } else {
164
+ val = typeof value === "string" ? reprStr(value) : repr(value);
165
+ }
166
+ if (mapping.length > 1) {
167
+ response += "\n" + indent;
168
+ }
169
+ response += k + separator + val;
170
+
171
+ if (pairs && mapping.length > 1) {
172
+ // Don't add trailing comma
173
+ if (counter !== mapping.length - 1) {
174
+ response += ",";
175
+ }
176
+ response += "...";
177
+ }
178
+ }
179
+ if (mapping.length > 1) {
180
+ response += "\n" + indentPrevLevel;
181
+ }
182
+ if (!pairs) {
183
+ response += "}";
184
+ }
185
+ return response;
186
+ }
187
+
188
+ function structify(
189
+ obj: number[] | string[] | { [key: string]: string } | string | number | null,
190
+ indentLevel?: number,
191
+ ) {
192
+ let response = "";
193
+ indentLevel = !indentLevel ? 1 : ++indentLevel;
194
+ const indent = " ".repeat(4 * indentLevel);
195
+ const prevIndent = " ".repeat(4 * (indentLevel - 1));
196
+
197
+ if (obj instanceof Array) {
198
+ const list: string[] = [];
199
+ let listContainsNumbers = true;
200
+ for (const k in obj) {
201
+ if (listContainsNumbers && typeof obj[k] !== "number") {
202
+ listContainsNumbers = false;
203
+ }
204
+ const value = structify(obj[k], indentLevel);
205
+ list.push(`${value}`);
206
+ }
207
+ if (listContainsNumbers) {
208
+ const listString = list.join(" ");
209
+ response += `[${listString}]`;
210
+ } else {
211
+ list.unshift("{{");
212
+ const listString = list.join(`\n${indent}`);
213
+ response += `${listString}\n${prevIndent}}}`;
214
+ }
215
+ } else if (obj instanceof Object) {
216
+ response += "struct(...";
217
+ let first = true;
218
+ for (const k in obj) {
219
+ if (Object.prototype.hasOwnProperty.call(obj, k)) {
220
+ if (!k[0].match(/[a-z]/i)) {
221
+ throw new CCError(
222
+ "MATLAB structs do not support keys starting with non-alphabet symbols",
223
+ );
224
+ }
225
+ // recursive call to scan property
226
+ if (first) {
227
+ first = false;
228
+ } else {
229
+ response += ",...";
230
+ }
231
+ response += `\n${indent}`;
232
+ response += `'${k}', `;
233
+ response += structify(obj[k], indentLevel);
234
+ }
235
+ }
236
+ response += "...";
237
+ response += `\n${prevIndent})`;
238
+ } else if (typeof obj === "number") {
239
+ // not an Object so obj[k] here is a value
240
+ response += obj.toString();
241
+ } else {
242
+ response += obj === null ? "string(nan)" : reprStr(obj);
243
+ }
244
+
245
+ return response;
246
+ }
247
+
248
+ function containsBody(request: Request): boolean {
249
+ return Boolean(request.data || request.multipartUploads);
250
+ }
251
+
252
+ function prepareQueryString(request: Request): string | null {
253
+ if (!request.urls[0].queryList) {
254
+ return null;
255
+ }
256
+ return setVariableValue("params", addCellArray(request.urls[0].queryList));
257
+ }
258
+
259
+ function prepareCookies(request: Request): string | null {
260
+ if (!request.cookies) {
261
+ return null;
262
+ }
263
+ return setVariableValue("cookies", addCellArray(request.cookies));
264
+ }
265
+
266
+ const cookieString = "char(join(join(cookies, '='), '; '))";
267
+ const paramsString = "char(join(join(params, '='), '&'))";
268
+
269
+ export {
270
+ reprStr,
271
+ repr,
272
+ setVariableValue,
273
+ callFunction,
274
+ addCellArray,
275
+ structify,
276
+ containsBody,
277
+ prepareQueryString,
278
+ prepareCookies,
279
+ cookieString,
280
+ paramsString,
281
+ };
@@ -0,0 +1,284 @@
1
+ import { Word } from "../../shell/Word.js";
2
+ import type { Request, Warnings } from "../../parse.js";
3
+
4
+ import {
5
+ reprStr,
6
+ repr,
7
+ setVariableValue,
8
+ callFunction,
9
+ structify,
10
+ containsBody,
11
+ prepareQueryString,
12
+ prepareCookies,
13
+ } from "./common.js";
14
+
15
+ function prepareHeaders(request: Request): string | null {
16
+ if (!request.headers.length) {
17
+ return null;
18
+ }
19
+
20
+ const headerStrs: string[] = [];
21
+ for (const [key, value] of request.headers) {
22
+ if (value === null) {
23
+ continue;
24
+ }
25
+ const keyStr = key.toLowerCase().toString();
26
+ if (keyStr === "cookie" && request.cookies) {
27
+ // TODO: curl -H 'Cookie: foo=bar' example.com
28
+ // adds an extra newline before the cookie
29
+ const cookieFieldParams = callFunction(
30
+ null,
31
+ "cellfun",
32
+ [
33
+ "@(x) Cookie(x{:})",
34
+ callFunction(null, "num2cell", ["cookies", "2"], ""),
35
+ ],
36
+ "",
37
+ );
38
+ headerStrs.push(
39
+ callFunction(null, "field.CookieField", cookieFieldParams, ""),
40
+ );
41
+ } else if (keyStr === "accept") {
42
+ const accepts = value.split(",");
43
+ if (accepts.length === 1) {
44
+ headerStrs.push(`field.AcceptField(MediaType(${repr(value)}))`);
45
+ } else {
46
+ let acceptheader = "field.AcceptField([";
47
+ for (const accept of accepts) {
48
+ acceptheader += `\n MediaType(${repr(accept.trim())})`;
49
+ }
50
+ acceptheader += "\n ])";
51
+ headerStrs.push(acceptheader);
52
+ }
53
+ } else {
54
+ headerStrs.push(`HeaderField(${repr(key)}, ${repr(value)})`);
55
+ }
56
+ }
57
+
58
+ if (headerStrs.length === 1) {
59
+ return setVariableValue("header", headerStrs[0]);
60
+ }
61
+
62
+ let header = "[\n";
63
+ header += " " + headerStrs.join("\n ") + "\n";
64
+ header += "]'";
65
+ return setVariableValue("header", header);
66
+ }
67
+
68
+ function prepareURI(request: Request) {
69
+ const uriParams: string[] = [];
70
+ if (request.urls[0].queryList) {
71
+ uriParams.push(repr(request.urls[0].urlWithoutQueryList));
72
+ uriParams.push("QueryParameter(params')");
73
+ } else {
74
+ uriParams.push(repr(request.urls[0].url));
75
+ }
76
+ return callFunction("uri", "URI", uriParams);
77
+ }
78
+
79
+ function prepareAuth(request: Request): string[] {
80
+ const options: string[] = [];
81
+ const optionsParams: string[] = [];
82
+ if (request.urls[0].auth) {
83
+ const [usr, pass] = request.urls[0].auth;
84
+ const userfield = `'Username', ${repr(usr)}`;
85
+ const passfield = `'Password', ${repr(pass)}`;
86
+ const authparams = (usr.length ? `${userfield}, ` : "") + passfield;
87
+ optionsParams.push(reprStr("Credentials"), "cred");
88
+ options.push(callFunction("cred", "Credentials", authparams));
89
+ }
90
+
91
+ if (request.insecure) {
92
+ optionsParams.push(reprStr("VerifyServerName"), "false");
93
+ }
94
+
95
+ if (optionsParams.length > 0) {
96
+ options.push(callFunction("options", "HTTPOptions", optionsParams));
97
+ }
98
+
99
+ return options;
100
+ }
101
+
102
+ function prepareMultipartUploads(request: Request): string | null {
103
+ if (!request.multipartUploads) {
104
+ return null;
105
+ }
106
+ const params: [string, string][] = [];
107
+ for (const m of request.multipartUploads) {
108
+ const readsFile = "contentFile" in m;
109
+ const value = readsFile ? m.contentFile.prepend("@") : m.content; // TODO: something nicer
110
+ const fileProvider = prepareDataProvider(
111
+ value,
112
+ null,
113
+ "",
114
+ 1,
115
+ true,
116
+ !readsFile,
117
+ );
118
+ params.push([repr(m.name), fileProvider as string]); // TODO: can be a string[]
119
+ }
120
+ return callFunction("body", "MultipartFormProvider", params);
121
+ }
122
+
123
+ function isJsonString(str: string): boolean {
124
+ // Source: https://stackoverflow.com/a/3710226/5625738
125
+ try {
126
+ JSON.parse(str);
127
+ } catch (e) {
128
+ return false;
129
+ }
130
+ return true;
131
+ }
132
+
133
+ // prepareDataProvider(value, null, "", 1, true, !readsFile);
134
+ // prepareDataProvider(request.data, "body", ";", 0, request.isDataBinary, request.isDataRaw);
135
+ function prepareDataProvider(
136
+ value: Word,
137
+ output: string | null,
138
+ termination: string,
139
+ indentLevel: number,
140
+ isDataBinary = true,
141
+ isDataRaw = false,
142
+ ): string | string[] {
143
+ if (!isDataRaw && value.charAt(0) === "@") {
144
+ const filename = value.slice(1);
145
+ if (!isDataBinary) {
146
+ return [
147
+ callFunction(output, "fileread", repr(filename)),
148
+ setVariableValue(`${output}(${output}==13 | ${output}==10)`, "[]"),
149
+ ];
150
+ }
151
+ // >> imformats % for seeing MATLAB supported image formats
152
+ const extension = (filename.split(".")[1] ?? "").toLowerCase().toString();
153
+ const isImage = ["jpeg", "jpg", "png", "tif", "gif"].includes(extension);
154
+ const provider = isImage ? "ImageProvider" : "FileProvider";
155
+ return callFunction(output, provider, repr(filename), termination);
156
+ }
157
+
158
+ if (!value.length) {
159
+ return callFunction(output, "FileProvider", "", termination);
160
+ }
161
+
162
+ if (value.isString() && isJsonString(value.toString())) {
163
+ const obj = JSON.parse(value.toString());
164
+ // If failed to create a struct for the JSON, then return a string
165
+ try {
166
+ const structure = structify(obj, indentLevel);
167
+ return callFunction(output, "JSONProvider", structure, termination);
168
+ } catch (e) {
169
+ return callFunction(output, "StringProvider", repr(value), termination);
170
+ }
171
+ }
172
+
173
+ // TODO: StringProvider should be the default
174
+ const formValue = value
175
+ .split("&")
176
+ .map((x) => x.split("=").map((x) => repr(x)));
177
+ return callFunction(output, "FormProvider", formValue, termination);
178
+ }
179
+
180
+ function prepareData(request: Request) {
181
+ if (!request.data || request.multipartUploads) {
182
+ return null;
183
+ }
184
+
185
+ if (request.data && request.data.split("&", 2).length > 1) {
186
+ const data = request.data.split("&").map((x: Word) =>
187
+ x.split("=").map((x) => {
188
+ let ans = repr(x);
189
+ if (x.isString()) {
190
+ try {
191
+ const jsonData = JSON.parse(x.toString());
192
+ if (typeof jsonData === "object") {
193
+ ans = callFunction(
194
+ null,
195
+ "JSONProvider",
196
+ structify(jsonData, 1),
197
+ "",
198
+ );
199
+ }
200
+ } catch (e) {}
201
+ }
202
+
203
+ return ans;
204
+ }),
205
+ );
206
+ return callFunction("body", "FormProvider", data);
207
+ }
208
+
209
+ let response = prepareDataProvider(
210
+ request.data,
211
+ "body",
212
+ ";",
213
+ 0,
214
+ !!request.isDataBinary,
215
+ !!request.isDataRaw,
216
+ );
217
+ if (!response) {
218
+ response = setVariableValue("body", repr(request.data));
219
+ }
220
+ return response;
221
+ }
222
+
223
+ function prepareRequestMessage(request: Request): string {
224
+ const method = request.urls[0].method.toLowerCase();
225
+ let reqMessage: string[] | string = [repr(method)];
226
+ if (request.headers.length) {
227
+ reqMessage.push("header");
228
+ }
229
+ if (containsBody(request)) {
230
+ if (reqMessage.length === 1) {
231
+ reqMessage.push("[]");
232
+ }
233
+ reqMessage.push("body");
234
+ }
235
+
236
+ if (
237
+ !request.headers.length &&
238
+ !containsBody(request) &&
239
+ method.toString() === "get"
240
+ ) {
241
+ reqMessage = ""; // TODO: just empty array?
242
+ }
243
+
244
+ const params = ["uri.EncodedURI"];
245
+ if (request.urls[0].auth || request.insecure) {
246
+ params.push("options");
247
+ }
248
+
249
+ const response = [
250
+ callFunction(
251
+ "response",
252
+ "RequestMessage",
253
+ reqMessage,
254
+ callFunction(null, ".send", params),
255
+ ),
256
+ ];
257
+
258
+ return response.join("\n");
259
+ }
260
+
261
+ export function toHTTPInterface(
262
+ request: Request,
263
+ warnings: Warnings,
264
+ ): [(string | string[] | null)[], Warnings] {
265
+ return [
266
+ [
267
+ "%% HTTP Interface",
268
+ "import matlab.net.*",
269
+ "import matlab.net.http.*",
270
+ containsBody(request) ? "import matlab.net.http.io.*" : null,
271
+ "",
272
+ prepareQueryString(request),
273
+ prepareCookies(request),
274
+ prepareHeaders(request),
275
+ prepareURI(request),
276
+ prepareAuth(request),
277
+ prepareMultipartUploads(request),
278
+ prepareData(request),
279
+ prepareRequestMessage(request),
280
+ "",
281
+ ],
282
+ warnings,
283
+ ];
284
+ }
@@ -0,0 +1,39 @@
1
+ import { parse, getFirst, COMMON_SUPPORTED_ARGS } from "../../parse.js";
2
+ import type { Request, Warnings } from "../../parse.js";
3
+
4
+ import { toWebServices } from "./webservices.js";
5
+ import { toHTTPInterface } from "./httpinterface.js";
6
+
7
+ export const supportedArgs = new Set([
8
+ ...COMMON_SUPPORTED_ARGS,
9
+ "insecure",
10
+ "no-insecure",
11
+ "form",
12
+ "form-string",
13
+ ]);
14
+
15
+ export function _toMATLAB(
16
+ requests: Request[],
17
+ warnings: Warnings = [],
18
+ ): string {
19
+ const request = getFirst(requests, warnings);
20
+
21
+ const [webServicesLines] = toWebServices(request, warnings);
22
+ const [httpInterfaceLines] = toHTTPInterface(request, warnings);
23
+ const lines = webServicesLines.concat("", httpInterfaceLines);
24
+ return lines
25
+ .flat()
26
+ .filter((line) => line !== null)
27
+ .join("\n");
28
+ }
29
+ export function toMATLABWarn(
30
+ curlCommand: string | string[],
31
+ warnings: Warnings = [],
32
+ ): [string, Warnings] {
33
+ const requests = parse(curlCommand, supportedArgs, warnings);
34
+ const matlab = _toMATLAB(requests, warnings);
35
+ return [matlab, warnings];
36
+ }
37
+ export function toMATLAB(curlCommand: string | string[]): string {
38
+ return toMATLABWarn(curlCommand)[0];
39
+ }