@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.
- package/LICENSE +21 -0
- package/README.md +153 -0
- package/dist/src/Headers.d.ts +23 -0
- package/dist/src/Headers.js +287 -0
- package/dist/src/Headers.js.map +1 -0
- package/dist/src/Query.d.ts +8 -0
- package/dist/src/Query.js +136 -0
- package/dist/src/Query.js.map +1 -0
- package/dist/src/Request.d.ts +200 -0
- package/dist/src/Request.js +1191 -0
- package/dist/src/Request.js.map +1 -0
- package/dist/src/Warnings.d.ts +15 -0
- package/dist/src/Warnings.js +111 -0
- package/dist/src/Warnings.js.map +1 -0
- package/dist/src/cli.d.ts +2 -0
- package/dist/src/cli.js +333 -0
- package/dist/src/cli.js.map +1 -0
- package/dist/src/curl/auth.d.ts +12 -0
- package/dist/src/curl/auth.js +40 -0
- package/dist/src/curl/auth.js.map +1 -0
- package/dist/src/curl/form.d.ts +28 -0
- package/dist/src/curl/form.js +218 -0
- package/dist/src/curl/form.js.map +1 -0
- package/dist/src/curl/opts.d.ts +2052 -0
- package/dist/src/curl/opts.js +1000 -0
- package/dist/src/curl/opts.js.map +1 -0
- package/dist/src/curl/url.d.ts +14 -0
- package/dist/src/curl/url.js +218 -0
- package/dist/src/curl/url.js.map +1 -0
- package/dist/src/generators/ansible.d.ts +5 -0
- package/dist/src/generators/ansible.js +282 -0
- package/dist/src/generators/ansible.js.map +1 -0
- package/dist/src/generators/c.d.ts +12 -0
- package/dist/src/generators/c.js +1230 -0
- package/dist/src/generators/c.js.map +1 -0
- package/dist/src/generators/cfml.d.ts +5 -0
- package/dist/src/generators/cfml.js +134 -0
- package/dist/src/generators/cfml.js.map +1 -0
- package/dist/src/generators/clojure.d.ts +7 -0
- package/dist/src/generators/clojure.js +436 -0
- package/dist/src/generators/clojure.js.map +1 -0
- package/dist/src/generators/csharp.d.ts +8 -0
- package/dist/src/generators/csharp.js +356 -0
- package/dist/src/generators/csharp.js.map +1 -0
- package/dist/src/generators/dart.d.ts +5 -0
- package/dist/src/generators/dart.js +273 -0
- package/dist/src/generators/dart.js.map +1 -0
- package/dist/src/generators/elixir.d.ts +8 -0
- package/dist/src/generators/elixir.js +296 -0
- package/dist/src/generators/elixir.js.map +1 -0
- package/dist/src/generators/go.d.ts +5 -0
- package/dist/src/generators/go.js +228 -0
- package/dist/src/generators/go.js.map +1 -0
- package/dist/src/generators/har.d.ts +7 -0
- package/dist/src/generators/har.js +115 -0
- package/dist/src/generators/har.js.map +1 -0
- package/dist/src/generators/http.d.ts +7 -0
- package/dist/src/generators/http.js +165 -0
- package/dist/src/generators/http.js.map +1 -0
- package/dist/src/generators/httpie.d.ts +5 -0
- package/dist/src/generators/httpie.js +446 -0
- package/dist/src/generators/httpie.js.map +1 -0
- package/dist/src/generators/java/httpurlconnection.d.ts +5 -0
- package/dist/src/generators/java/httpurlconnection.js +123 -0
- package/dist/src/generators/java/httpurlconnection.js.map +1 -0
- package/dist/src/generators/java/java.d.ts +8 -0
- package/dist/src/generators/java/java.js +247 -0
- package/dist/src/generators/java/java.js.map +1 -0
- package/dist/src/generators/java/jsoup.d.ts +5 -0
- package/dist/src/generators/java/jsoup.js +152 -0
- package/dist/src/generators/java/jsoup.js.map +1 -0
- package/dist/src/generators/java/okhttp.d.ts +5 -0
- package/dist/src/generators/java/okhttp.js +232 -0
- package/dist/src/generators/java/okhttp.js.map +1 -0
- package/dist/src/generators/javascript/axios.d.ts +5 -0
- package/dist/src/generators/javascript/axios.js +332 -0
- package/dist/src/generators/javascript/axios.js.map +1 -0
- package/dist/src/generators/javascript/got.d.ts +5 -0
- package/dist/src/generators/javascript/got.js +277 -0
- package/dist/src/generators/javascript/got.js.map +1 -0
- package/dist/src/generators/javascript/http.d.ts +8 -0
- package/dist/src/generators/javascript/http.js +183 -0
- package/dist/src/generators/javascript/http.js.map +1 -0
- package/dist/src/generators/javascript/javascript.d.ts +37 -0
- package/dist/src/generators/javascript/javascript.js +676 -0
- package/dist/src/generators/javascript/javascript.js.map +1 -0
- package/dist/src/generators/javascript/jquery.d.ts +15 -0
- package/dist/src/generators/javascript/jquery.js +297 -0
- package/dist/src/generators/javascript/jquery.js.map +1 -0
- package/dist/src/generators/javascript/ky.d.ts +7 -0
- package/dist/src/generators/javascript/ky.js +245 -0
- package/dist/src/generators/javascript/ky.js.map +1 -0
- package/dist/src/generators/javascript/request.d.ts +5 -0
- package/dist/src/generators/javascript/request.js +113 -0
- package/dist/src/generators/javascript/request.js.map +1 -0
- package/dist/src/generators/javascript/superagent.d.ts +10 -0
- package/dist/src/generators/javascript/superagent.js +301 -0
- package/dist/src/generators/javascript/superagent.js.map +1 -0
- package/dist/src/generators/javascript/xhr.d.ts +8 -0
- package/dist/src/generators/javascript/xhr.js +147 -0
- package/dist/src/generators/javascript/xhr.js.map +1 -0
- package/dist/src/generators/json.d.ts +45 -0
- package/dist/src/generators/json.js +197 -0
- package/dist/src/generators/json.js.map +1 -0
- package/dist/src/generators/julia.d.ts +8 -0
- package/dist/src/generators/julia.js +365 -0
- package/dist/src/generators/julia.js.map +1 -0
- package/dist/src/generators/kotlin.d.ts +8 -0
- package/dist/src/generators/kotlin.js +325 -0
- package/dist/src/generators/kotlin.js.map +1 -0
- package/dist/src/generators/lua.d.ts +5 -0
- package/dist/src/generators/lua.js +192 -0
- package/dist/src/generators/lua.js.map +1 -0
- package/dist/src/generators/matlab/common.d.ts +16 -0
- package/dist/src/generators/matlab/common.js +238 -0
- package/dist/src/generators/matlab/common.js.map +1 -0
- package/dist/src/generators/matlab/httpinterface.d.ts +2 -0
- package/dist/src/generators/matlab/httpinterface.js +211 -0
- package/dist/src/generators/matlab/httpinterface.js.map +1 -0
- package/dist/src/generators/matlab/matlab.d.ts +5 -0
- package/dist/src/generators/matlab/matlab.js +29 -0
- package/dist/src/generators/matlab/matlab.js.map +1 -0
- package/dist/src/generators/matlab/webservices.d.ts +2 -0
- package/dist/src/generators/matlab/webservices.js +191 -0
- package/dist/src/generators/matlab/webservices.js.map +1 -0
- package/dist/src/generators/objectivec.d.ts +8 -0
- package/dist/src/generators/objectivec.js +269 -0
- package/dist/src/generators/objectivec.js.map +1 -0
- package/dist/src/generators/ocaml.d.ts +8 -0
- package/dist/src/generators/ocaml.js +150 -0
- package/dist/src/generators/ocaml.js.map +1 -0
- package/dist/src/generators/perl.d.ts +9 -0
- package/dist/src/generators/perl.js +209 -0
- package/dist/src/generators/perl.js.map +1 -0
- package/dist/src/generators/php/guzzle.d.ts +5 -0
- package/dist/src/generators/php/guzzle.js +328 -0
- package/dist/src/generators/php/guzzle.js.map +1 -0
- package/dist/src/generators/php/php.d.ts +8 -0
- package/dist/src/generators/php/php.js +217 -0
- package/dist/src/generators/php/php.js.map +1 -0
- package/dist/src/generators/php/requests.d.ts +5 -0
- package/dist/src/generators/php/requests.js +89 -0
- package/dist/src/generators/php/requests.js.map +1 -0
- package/dist/src/generators/powershell.d.ts +11 -0
- package/dist/src/generators/powershell.js +377 -0
- package/dist/src/generators/powershell.js.map +1 -0
- package/dist/src/generators/python/http.d.ts +5 -0
- package/dist/src/generators/python/http.js +128 -0
- package/dist/src/generators/python/http.js.map +1 -0
- package/dist/src/generators/python/python.d.ts +22 -0
- package/dist/src/generators/python/python.js +1693 -0
- package/dist/src/generators/python/python.js.map +1 -0
- package/dist/src/generators/r/httr.d.ts +10 -0
- package/dist/src/generators/r/httr.js +298 -0
- package/dist/src/generators/r/httr.js.map +1 -0
- package/dist/src/generators/r/httr2.d.ts +5 -0
- package/dist/src/generators/r/httr2.js +214 -0
- package/dist/src/generators/r/httr2.js.map +1 -0
- package/dist/src/generators/ruby/httparty.d.ts +6 -0
- package/dist/src/generators/ruby/httparty.js +308 -0
- package/dist/src/generators/ruby/httparty.js.map +1 -0
- package/dist/src/generators/ruby/ruby.d.ts +14 -0
- package/dist/src/generators/ruby/ruby.js +515 -0
- package/dist/src/generators/ruby/ruby.js.map +1 -0
- package/dist/src/generators/rust.d.ts +8 -0
- package/dist/src/generators/rust.js +177 -0
- package/dist/src/generators/rust.js.map +1 -0
- package/dist/src/generators/swift.d.ts +8 -0
- package/dist/src/generators/swift.js +219 -0
- package/dist/src/generators/swift.js.map +1 -0
- package/dist/src/generators/wget.d.ts +8 -0
- package/dist/src/generators/wget.js +472 -0
- package/dist/src/generators/wget.js.map +1 -0
- package/dist/src/index.d.ts +53 -0
- package/dist/src/index.js +54 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/parse.d.ts +13 -0
- package/dist/src/parse.js +43 -0
- package/dist/src/parse.js.map +1 -0
- package/dist/src/shell/Parser.d.ts +4 -0
- package/dist/src/shell/Parser.js +6 -0
- package/dist/src/shell/Parser.js.map +1 -0
- package/dist/src/shell/Word.d.ts +48 -0
- package/dist/src/shell/Word.js +486 -0
- package/dist/src/shell/Word.js.map +1 -0
- package/dist/src/shell/tokenizer.d.ts +3 -0
- package/dist/src/shell/tokenizer.js +610 -0
- package/dist/src/shell/tokenizer.js.map +1 -0
- package/dist/src/shell/webParser.d.ts +4 -0
- package/dist/src/shell/webParser.js +15 -0
- package/dist/src/shell/webParser.js.map +1 -0
- package/dist/src/utils.d.ts +6 -0
- package/dist/src/utils.js +21 -0
- package/dist/src/utils.js.map +1 -0
- package/dist/tools/compare-requests.d.ts +2 -0
- package/dist/tools/compare-requests.js +448 -0
- package/dist/tools/compare-requests.js.map +1 -0
- package/dist/tools/gen-test.d.ts +2 -0
- package/dist/tools/gen-test.js +114 -0
- package/dist/tools/gen-test.js.map +1 -0
- package/dist/tree-sitter-bash.wasm +0 -0
- package/index.d.ts +3 -0
- package/package.json +97 -0
- package/src/Headers.ts +334 -0
- package/src/Query.ts +147 -0
- package/src/Request.ts +1649 -0
- package/src/Warnings.ts +154 -0
- package/src/cli.ts +561 -0
- package/src/curl/auth.ts +45 -0
- package/src/curl/form.ts +297 -0
- package/src/curl/opts.ts +1417 -0
- package/src/curl/url.ts +241 -0
- package/src/generators/ansible.ts +364 -0
- package/src/generators/c.ts +1371 -0
- package/src/generators/cfml.ts +155 -0
- package/src/generators/clojure.ts +521 -0
- package/src/generators/csharp.ts +383 -0
- package/src/generators/dart.ts +293 -0
- package/src/generators/elixir.ts +349 -0
- package/src/generators/go.ts +269 -0
- package/src/generators/har.ts +147 -0
- package/src/generators/http.ts +194 -0
- package/src/generators/httpie.ts +506 -0
- package/src/generators/java/httpurlconnection.ts +151 -0
- package/src/generators/java/java.ts +280 -0
- package/src/generators/java/jsoup.ts +172 -0
- package/src/generators/java/okhttp.ts +257 -0
- package/src/generators/javascript/axios.ts +395 -0
- package/src/generators/javascript/got.ts +362 -0
- package/src/generators/javascript/http.ts +232 -0
- package/src/generators/javascript/javascript.ts +825 -0
- package/src/generators/javascript/jquery.ts +360 -0
- package/src/generators/javascript/ky.ts +315 -0
- package/src/generators/javascript/request.ts +152 -0
- package/src/generators/javascript/superagent.ts +373 -0
- package/src/generators/javascript/xhr.ts +192 -0
- package/src/generators/json.ts +293 -0
- package/src/generators/julia.ts +398 -0
- package/src/generators/kotlin.ts +355 -0
- package/src/generators/lua.ts +209 -0
- package/src/generators/matlab/common.ts +281 -0
- package/src/generators/matlab/httpinterface.ts +284 -0
- package/src/generators/matlab/matlab.ts +39 -0
- package/src/generators/matlab/webservices.ts +263 -0
- package/src/generators/objectivec.ts +297 -0
- package/src/generators/ocaml.ts +168 -0
- package/src/generators/perl.ts +237 -0
- package/src/generators/php/guzzle.ts +368 -0
- package/src/generators/php/php.ts +239 -0
- package/src/generators/php/requests.ts +103 -0
- package/src/generators/powershell.ts +430 -0
- package/src/generators/python/http.ts +167 -0
- package/src/generators/python/python.ts +1934 -0
- package/src/generators/r/httr.ts +328 -0
- package/src/generators/r/httr2.ts +268 -0
- package/src/generators/ruby/httparty.ts +351 -0
- package/src/generators/ruby/ruby.ts +564 -0
- package/src/generators/rust.ts +243 -0
- package/src/generators/swift.ts +235 -0
- package/src/generators/wget.ts +533 -0
- package/src/index.ts +89 -0
- package/src/parse.ts +72 -0
- package/src/shell/Parser.ts +8 -0
- package/src/shell/Word.ts +531 -0
- package/src/shell/tokenizer.ts +736 -0
- package/src/shell/webParser.ts +18 -0
- package/src/utils.ts +25 -0
- package/tools/compare-requests.ts +571 -0
- package/tools/extract_curl_args.py +511 -0
- package/tools/gen-test.ts +129 -0
|
@@ -0,0 +1,355 @@
|
|
|
1
|
+
import { Word, eq } from "../shell/Word.js";
|
|
2
|
+
import { parse, getFirst, COMMON_SUPPORTED_ARGS } from "../parse.js";
|
|
3
|
+
import type { Request, Warnings } from "../parse.js";
|
|
4
|
+
import { parseQueryString } from "../Query.js";
|
|
5
|
+
|
|
6
|
+
export const supportedArgs = new Set([
|
|
7
|
+
...COMMON_SUPPORTED_ARGS,
|
|
8
|
+
"max-time",
|
|
9
|
+
"connect-timeout",
|
|
10
|
+
"location",
|
|
11
|
+
"location-trusted",
|
|
12
|
+
"upload-file",
|
|
13
|
+
"form",
|
|
14
|
+
"form-string",
|
|
15
|
+
]);
|
|
16
|
+
|
|
17
|
+
// The only difference from Java is that in Kotlin the $ needs to be escaped
|
|
18
|
+
// https://kotlinlang.org/docs/java-to-kotlin-idioms-strings.html#concatenate-strings
|
|
19
|
+
// https://docs.oracle.com/javase/specs/jls/se7/html/jls-3.html#jls-3.10.6
|
|
20
|
+
// https://docs.oracle.com/javase/specs/jls/se7/html/jls-3.html#jls-3.3
|
|
21
|
+
const regexEscape = /\$|"|\\|\p{C}|[^ \P{Z}]/gu;
|
|
22
|
+
const regexDigit = /[0-9]/; // it's 0-7 actually but that would generate confusing code
|
|
23
|
+
export function reprStr(s: string): string {
|
|
24
|
+
return (
|
|
25
|
+
'"' +
|
|
26
|
+
s.replace(regexEscape, (c: string, index: number, string: string) => {
|
|
27
|
+
switch (c) {
|
|
28
|
+
case "$":
|
|
29
|
+
return "\\$";
|
|
30
|
+
case "\\":
|
|
31
|
+
return "\\\\";
|
|
32
|
+
case "\b":
|
|
33
|
+
return "\\b";
|
|
34
|
+
case "\f":
|
|
35
|
+
return "\\f";
|
|
36
|
+
case "\n":
|
|
37
|
+
return "\\n";
|
|
38
|
+
case "\r":
|
|
39
|
+
return "\\r";
|
|
40
|
+
case "\t":
|
|
41
|
+
return "\\t";
|
|
42
|
+
case '"':
|
|
43
|
+
return '\\"';
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
if (c.length === 2) {
|
|
47
|
+
const first = c.charCodeAt(0);
|
|
48
|
+
const second = c.charCodeAt(1);
|
|
49
|
+
return (
|
|
50
|
+
"\\u" +
|
|
51
|
+
first.toString(16).padStart(4, "0") +
|
|
52
|
+
"\\u" +
|
|
53
|
+
second.toString(16).padStart(4, "0")
|
|
54
|
+
);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
if (c === "\0" && !regexDigit.test(string.charAt(index + 1))) {
|
|
58
|
+
return "\\0";
|
|
59
|
+
}
|
|
60
|
+
return "\\u" + c.charCodeAt(0).toString(16).padStart(4, "0");
|
|
61
|
+
}) +
|
|
62
|
+
'"'
|
|
63
|
+
);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// TODO: anything different here?
|
|
67
|
+
export function repr(w: Word, imports: Set<string>): string {
|
|
68
|
+
const args: string[] = [];
|
|
69
|
+
for (const t of w.tokens) {
|
|
70
|
+
if (typeof t === "string") {
|
|
71
|
+
args.push(reprStr(t));
|
|
72
|
+
} else if (t.type === "variable") {
|
|
73
|
+
args.push("System.getenv(" + reprStr(t.value) + ') ?: ""');
|
|
74
|
+
imports.add("java.lang.System");
|
|
75
|
+
} else {
|
|
76
|
+
args.push("exec(" + reprStr(t.value) + ")");
|
|
77
|
+
imports.add("java.lang.Runtime");
|
|
78
|
+
imports.add("java.util.Scanner");
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
return args.join(" + ");
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export function _toKotlin(
|
|
85
|
+
requests: Request[],
|
|
86
|
+
warnings: Warnings = [],
|
|
87
|
+
): string {
|
|
88
|
+
const request = getFirst(requests, warnings);
|
|
89
|
+
const url = request.urls[0];
|
|
90
|
+
|
|
91
|
+
const imports = new Set<string>([
|
|
92
|
+
"java.io.IOException",
|
|
93
|
+
"okhttp3.OkHttpClient",
|
|
94
|
+
"okhttp3.Request",
|
|
95
|
+
]);
|
|
96
|
+
|
|
97
|
+
let kotlinCode = "";
|
|
98
|
+
|
|
99
|
+
kotlinCode += "val client = OkHttpClient()";
|
|
100
|
+
const clientLines = [];
|
|
101
|
+
if (request.timeout) {
|
|
102
|
+
// TODO: floats don't work here
|
|
103
|
+
clientLines.push(
|
|
104
|
+
" .callTimeout(" + request.timeout.toString() + ", TimeUnit.SECONDS)\n",
|
|
105
|
+
);
|
|
106
|
+
imports.add("java.util.concurrent.TimeUnit");
|
|
107
|
+
}
|
|
108
|
+
if (request.connectTimeout) {
|
|
109
|
+
clientLines.push(
|
|
110
|
+
" .connectTimeout(" +
|
|
111
|
+
request.connectTimeout.toString() +
|
|
112
|
+
", TimeUnit.SECONDS)\n",
|
|
113
|
+
);
|
|
114
|
+
imports.add("java.util.concurrent.TimeUnit");
|
|
115
|
+
}
|
|
116
|
+
if (request.followRedirects === false) {
|
|
117
|
+
clientLines.push(" .followRedirects(false)\n");
|
|
118
|
+
clientLines.push(" .followSslRedirects(false)\n");
|
|
119
|
+
}
|
|
120
|
+
// TODO: Proxy
|
|
121
|
+
if (clientLines.length) {
|
|
122
|
+
kotlinCode += ".newBuilder()\n";
|
|
123
|
+
for (const line of clientLines) {
|
|
124
|
+
kotlinCode += line;
|
|
125
|
+
}
|
|
126
|
+
kotlinCode += " .build()";
|
|
127
|
+
}
|
|
128
|
+
kotlinCode += "\n\n";
|
|
129
|
+
|
|
130
|
+
if (url.auth) {
|
|
131
|
+
const [name, password] = url.auth;
|
|
132
|
+
kotlinCode +=
|
|
133
|
+
"val credential = Credentials.basic(" +
|
|
134
|
+
repr(name, imports) +
|
|
135
|
+
", " +
|
|
136
|
+
repr(password, imports) +
|
|
137
|
+
");\n\n";
|
|
138
|
+
imports.add("okhttp3.Credentials");
|
|
139
|
+
|
|
140
|
+
if (request.authType !== "basic") {
|
|
141
|
+
warnings.push([
|
|
142
|
+
"okhttp-unsupported-auth-type",
|
|
143
|
+
"OkHttp doesn't support auth type " + request.authType,
|
|
144
|
+
]);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
const methodCallArgs = [];
|
|
149
|
+
const contentType = request.headers.getContentType();
|
|
150
|
+
const exactContentType = request.headers.get("content-type");
|
|
151
|
+
if (url.uploadFile) {
|
|
152
|
+
if (eq(url.uploadFile, "-") || eq(url.uploadFile, ".")) {
|
|
153
|
+
warnings.push(["upload-stdin", "uploading from stdin isn't supported"]);
|
|
154
|
+
}
|
|
155
|
+
if (exactContentType) {
|
|
156
|
+
kotlinCode +=
|
|
157
|
+
"val MEDIA_TYPE = " +
|
|
158
|
+
repr(exactContentType, imports) +
|
|
159
|
+
".toMediaType()\n\n";
|
|
160
|
+
imports.add("okhttp3.MediaType.Companion.toMediaType");
|
|
161
|
+
}
|
|
162
|
+
methodCallArgs.push("file.asRequestBody(MEDIA_TYPE)");
|
|
163
|
+
kotlinCode += "val file = File(" + repr(url.uploadFile, imports) + ")\n\n";
|
|
164
|
+
imports.add("java.io.File");
|
|
165
|
+
imports.add("okhttp3.RequestBody.Companion.asRequestBody");
|
|
166
|
+
} else if (request.multipartUploads) {
|
|
167
|
+
methodCallArgs.push("requestBody");
|
|
168
|
+
kotlinCode += "val requestBody = MultipartBody.Builder()\n";
|
|
169
|
+
kotlinCode += " .setType(MultipartBody.FORM)\n";
|
|
170
|
+
for (const m of request.multipartUploads) {
|
|
171
|
+
const args = [repr(m.name, imports)];
|
|
172
|
+
if ("content" in m) {
|
|
173
|
+
args.push(repr(m.content, imports));
|
|
174
|
+
} else {
|
|
175
|
+
if ("filename" in m && m.filename) {
|
|
176
|
+
args.push(repr(m.filename, imports));
|
|
177
|
+
args.push(
|
|
178
|
+
"File(" + repr(m.contentFile, imports) + ").asRequestBody()", // TODO: content type here
|
|
179
|
+
);
|
|
180
|
+
imports.add("java.io.File");
|
|
181
|
+
imports.add("okhttp3.RequestBody.Companion.asRequestBody");
|
|
182
|
+
} else {
|
|
183
|
+
// TODO: import
|
|
184
|
+
// TODO: probably doesn't work
|
|
185
|
+
args.push("Files.readAllBytes(" + repr(m.contentFile, imports) + ")");
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
kotlinCode += " .addFormDataPart(" + args.join(", ") + ")\n";
|
|
189
|
+
}
|
|
190
|
+
kotlinCode += " .build()\n\n";
|
|
191
|
+
imports.add("okhttp3.MultipartBody");
|
|
192
|
+
} else if (
|
|
193
|
+
request.dataArray &&
|
|
194
|
+
request.dataArray.length === 1 &&
|
|
195
|
+
!(request.dataArray[0] instanceof Word) &&
|
|
196
|
+
!request.dataArray[0].name
|
|
197
|
+
) {
|
|
198
|
+
// TODO: surely --upload-file and this can't be identical,
|
|
199
|
+
// doesn't this ignore url encoding?
|
|
200
|
+
if (exactContentType) {
|
|
201
|
+
kotlinCode +=
|
|
202
|
+
"val MEDIA_TYPE = " +
|
|
203
|
+
repr(exactContentType, imports) +
|
|
204
|
+
".toMediaType()\n\n";
|
|
205
|
+
imports.add("okhttp3.MediaType.Companion.toMediaType");
|
|
206
|
+
}
|
|
207
|
+
methodCallArgs.push("file.asRequestBody(MEDIA_TYPE)");
|
|
208
|
+
imports.add("okhttp3.RequestBody.Companion.asRequestBody");
|
|
209
|
+
kotlinCode +=
|
|
210
|
+
"val file = File(" +
|
|
211
|
+
repr(request.dataArray[0].filename, imports) +
|
|
212
|
+
")\n\n";
|
|
213
|
+
imports.add("java.io.File");
|
|
214
|
+
} else if (request.data) {
|
|
215
|
+
if (contentType === "application/x-www-form-urlencoded") {
|
|
216
|
+
const [queryList] = parseQueryString(request.data);
|
|
217
|
+
if (!queryList) {
|
|
218
|
+
if (exactContentType) {
|
|
219
|
+
kotlinCode +=
|
|
220
|
+
"val MEDIA_TYPE = " +
|
|
221
|
+
repr(exactContentType, imports) +
|
|
222
|
+
".toMediaType()\n\n";
|
|
223
|
+
imports.add("okhttp3.MediaType.Companion.toMediaType");
|
|
224
|
+
}
|
|
225
|
+
methodCallArgs.push("requestBody.toRequestBody(MEDIA_TYPE)");
|
|
226
|
+
imports.add("okhttp3.RequestBody.Companion.toRequestBody");
|
|
227
|
+
kotlinCode +=
|
|
228
|
+
"val requestBody = " + repr(request.data, imports) + "\n\n";
|
|
229
|
+
} else {
|
|
230
|
+
methodCallArgs.push("formBody");
|
|
231
|
+
kotlinCode += "val formBody = FormBody.Builder()\n";
|
|
232
|
+
for (const [name, value] of queryList) {
|
|
233
|
+
kotlinCode +=
|
|
234
|
+
" .add(" +
|
|
235
|
+
repr(name, imports) +
|
|
236
|
+
", " +
|
|
237
|
+
repr(value, imports) +
|
|
238
|
+
")\n";
|
|
239
|
+
}
|
|
240
|
+
kotlinCode += " .build()\n\n";
|
|
241
|
+
imports.add("okhttp3.FormBody");
|
|
242
|
+
}
|
|
243
|
+
} else {
|
|
244
|
+
if (exactContentType) {
|
|
245
|
+
kotlinCode +=
|
|
246
|
+
"val MEDIA_TYPE = " +
|
|
247
|
+
repr(exactContentType, imports) +
|
|
248
|
+
".toMediaType()\n\n";
|
|
249
|
+
imports.add("okhttp3.MediaType.Companion.toMediaType");
|
|
250
|
+
}
|
|
251
|
+
methodCallArgs.push("requestBody.toRequestBody(MEDIA_TYPE)");
|
|
252
|
+
kotlinCode += "val requestBody = " + repr(request.data, imports) + "\n\n";
|
|
253
|
+
imports.add("okhttp3.RequestBody.Companion.toRequestBody");
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
kotlinCode += "val request = Request.Builder()\n";
|
|
258
|
+
kotlinCode += " .url(" + repr(url.url, imports) + ")\n";
|
|
259
|
+
const methods = ["DELETE", "GET", "HEAD", "PATCH", "POST", "PUT"];
|
|
260
|
+
const dataMethods = ["DELETE", "PATCH", "POST", "PUT"];
|
|
261
|
+
const requiredDataMethods = ["PATCH", "POST", "PUT"];
|
|
262
|
+
const method = url.method;
|
|
263
|
+
const methodStr = url.method.toString();
|
|
264
|
+
let methodCall = "method";
|
|
265
|
+
if (
|
|
266
|
+
!method.isString() ||
|
|
267
|
+
!methods.includes(methodStr) ||
|
|
268
|
+
// If we appended something to methodCallArgs it means we need to send data
|
|
269
|
+
(methodCallArgs.length && !dataMethods.includes(methodStr)) ||
|
|
270
|
+
(!methodCallArgs.length && requiredDataMethods.includes(methodStr))
|
|
271
|
+
) {
|
|
272
|
+
if (!methodCallArgs.length) {
|
|
273
|
+
methodCallArgs.push('"".toRequestBody()');
|
|
274
|
+
imports.add("okhttp3.RequestBody.Companion.toRequestBody");
|
|
275
|
+
}
|
|
276
|
+
methodCallArgs.unshift(repr(method, imports));
|
|
277
|
+
} else {
|
|
278
|
+
if (methodStr === "DELETE" && !methodCallArgs.length) {
|
|
279
|
+
methodCallArgs.push('"".toRequestBody()');
|
|
280
|
+
imports.add("okhttp3.RequestBody.Companion.toRequestBody");
|
|
281
|
+
}
|
|
282
|
+
methodCall = method.toString().toLowerCase();
|
|
283
|
+
}
|
|
284
|
+
if (methodCall !== "get") {
|
|
285
|
+
kotlinCode += " ." + methodCall + "(" + methodCallArgs.join(", ") + ")\n";
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
if (request.headers.length) {
|
|
289
|
+
for (const [headerName, headerValue] of request.headers) {
|
|
290
|
+
if (headerValue === null) {
|
|
291
|
+
continue;
|
|
292
|
+
}
|
|
293
|
+
kotlinCode +=
|
|
294
|
+
" .header(" +
|
|
295
|
+
repr(headerName, imports) +
|
|
296
|
+
", " +
|
|
297
|
+
repr(headerValue, imports) +
|
|
298
|
+
")\n";
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
if (url.auth) {
|
|
302
|
+
const authHeader = request.headers.lowercase
|
|
303
|
+
? "authorization"
|
|
304
|
+
: "Authorization";
|
|
305
|
+
kotlinCode += ' .header("' + authHeader + '", credential)\n';
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
kotlinCode += " .build()\n";
|
|
309
|
+
|
|
310
|
+
kotlinCode += "\n";
|
|
311
|
+
kotlinCode += "client.newCall(request).execute().use { response ->\n";
|
|
312
|
+
kotlinCode +=
|
|
313
|
+
' if (!response.isSuccessful) throw IOException("Unexpected code $response")\n';
|
|
314
|
+
kotlinCode += " response.body!!.string()\n";
|
|
315
|
+
kotlinCode += "}\n";
|
|
316
|
+
|
|
317
|
+
let preambleCode = "";
|
|
318
|
+
for (const imp of Array.from(imports).sort()) {
|
|
319
|
+
preambleCode += "import " + imp + "\n";
|
|
320
|
+
}
|
|
321
|
+
if (imports.size) {
|
|
322
|
+
preambleCode += "\n";
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
// TODO
|
|
326
|
+
if (imports.has("java.lang.Runtime")) {
|
|
327
|
+
// Helper function that runs a bash command and always returns a string
|
|
328
|
+
preambleCode += "fun exec(cmd: String): String {\n";
|
|
329
|
+
preambleCode += " try {\n";
|
|
330
|
+
preambleCode += " val p = Runtime.getRuntime().exec(cmd)\n";
|
|
331
|
+
preambleCode += " p.waitFor()\n";
|
|
332
|
+
preambleCode +=
|
|
333
|
+
' val s = Scanner(p.getInputStream()).useDelimiter("\\\\A")\n';
|
|
334
|
+
preambleCode += ' return s.hasNext() ? s.next() : ""\n';
|
|
335
|
+
preambleCode += " } catch (Exception e) {\n";
|
|
336
|
+
preambleCode += ' return ""\n';
|
|
337
|
+
preambleCode += " }\n";
|
|
338
|
+
preambleCode += "}\n";
|
|
339
|
+
preambleCode += "\n";
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
return preambleCode + kotlinCode;
|
|
343
|
+
}
|
|
344
|
+
export function toKotlinWarn(
|
|
345
|
+
curlCommand: string | string[],
|
|
346
|
+
warnings: Warnings = [],
|
|
347
|
+
): [string, Warnings] {
|
|
348
|
+
const requests = parse(curlCommand, supportedArgs, warnings);
|
|
349
|
+
const kotlin = _toKotlin(requests, warnings);
|
|
350
|
+
return [kotlin, warnings];
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
export function toKotlin(curlCommand: string | string[]): string {
|
|
354
|
+
return toKotlinWarn(curlCommand)[0];
|
|
355
|
+
}
|
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
import { Word, eq, joinWords } from "../shell/Word.js";
|
|
2
|
+
import { parse, getFirst, COMMON_SUPPORTED_ARGS } from "../parse.js";
|
|
3
|
+
import type { Request, Warnings } from "../parse.js";
|
|
4
|
+
|
|
5
|
+
import { reprStr as pyreprStr } from "./python/python.js";
|
|
6
|
+
|
|
7
|
+
export const supportedArgs = new Set([
|
|
8
|
+
...COMMON_SUPPORTED_ARGS,
|
|
9
|
+
// "form",
|
|
10
|
+
// "form-string",
|
|
11
|
+
]);
|
|
12
|
+
|
|
13
|
+
function reprStr(s: string): string {
|
|
14
|
+
if (s.includes('"') && !s.includes("'")) {
|
|
15
|
+
return pyreprStr(s, "'");
|
|
16
|
+
}
|
|
17
|
+
return pyreprStr(s, '"');
|
|
18
|
+
}
|
|
19
|
+
function repr(w: Word): string {
|
|
20
|
+
const args: string[] = [];
|
|
21
|
+
for (const t of w.tokens) {
|
|
22
|
+
if (typeof t === "string") {
|
|
23
|
+
args.push(reprStr(t));
|
|
24
|
+
} else if (t.type === "variable") {
|
|
25
|
+
args.push("os.getenv(" + reprStr(t.value) + ")");
|
|
26
|
+
} else {
|
|
27
|
+
args.push("io.popen(" + reprStr(t.value) + '):read("*a")');
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
return args.join(" .. ");
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// http://lua-users.org/wiki/TablesTutorial
|
|
34
|
+
const VALID_KEY = /^[a-zA-Z_][a-zA-Z0-9_]*$/;
|
|
35
|
+
function reprKey(key: Word | string): string {
|
|
36
|
+
if (key instanceof Word) {
|
|
37
|
+
if (key.isString() && VALID_KEY.test(key.toString())) {
|
|
38
|
+
return key.toString();
|
|
39
|
+
}
|
|
40
|
+
return "[" + repr(key) + "]";
|
|
41
|
+
}
|
|
42
|
+
if (VALID_KEY.test(key)) {
|
|
43
|
+
return key;
|
|
44
|
+
}
|
|
45
|
+
return "[" + reprStr(key) + "]";
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function jsonToLua(data: any, indent = 0): string {
|
|
49
|
+
if (typeof data === "string") {
|
|
50
|
+
return reprStr(data);
|
|
51
|
+
}
|
|
52
|
+
if (typeof data === "number") {
|
|
53
|
+
return data.toString();
|
|
54
|
+
}
|
|
55
|
+
if (typeof data === "boolean") {
|
|
56
|
+
return data ? "true" : "false";
|
|
57
|
+
}
|
|
58
|
+
if (data === null) {
|
|
59
|
+
return "nil";
|
|
60
|
+
}
|
|
61
|
+
if (Array.isArray(data)) {
|
|
62
|
+
if (data.length === 0) {
|
|
63
|
+
return "{}";
|
|
64
|
+
}
|
|
65
|
+
let code = "{\n";
|
|
66
|
+
for (const item of data) {
|
|
67
|
+
code += "\t".repeat(indent + 1) + jsonToLua(item, indent + 1) + ",\n";
|
|
68
|
+
}
|
|
69
|
+
if (code.endsWith(",\n")) {
|
|
70
|
+
code = code.slice(0, -2) + "\n";
|
|
71
|
+
}
|
|
72
|
+
code += "\t".repeat(indent) + "}";
|
|
73
|
+
return code;
|
|
74
|
+
}
|
|
75
|
+
if (typeof data === "object") {
|
|
76
|
+
const entries = Object.entries(data);
|
|
77
|
+
if (entries.length === 0) {
|
|
78
|
+
// TODO: not correct https://github.com/rxi/json.lua/issues/23
|
|
79
|
+
return "{}";
|
|
80
|
+
}
|
|
81
|
+
let code = "{\n";
|
|
82
|
+
for (const [key, value] of entries) {
|
|
83
|
+
code +=
|
|
84
|
+
"\t".repeat(indent + 1) +
|
|
85
|
+
reprKey(key) +
|
|
86
|
+
" = " +
|
|
87
|
+
jsonToLua(value, indent + 1) +
|
|
88
|
+
",\n";
|
|
89
|
+
}
|
|
90
|
+
if (code.endsWith(",\n")) {
|
|
91
|
+
code = code.slice(0, -2) + "\n";
|
|
92
|
+
}
|
|
93
|
+
code += "\t".repeat(indent) + "}";
|
|
94
|
+
return code;
|
|
95
|
+
} else {
|
|
96
|
+
throw new Error("Unsupported JSON type: " + typeof data);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export function _toLua(requests: Request[], warnings: Warnings = []): string {
|
|
101
|
+
const request = getFirst(requests, warnings);
|
|
102
|
+
|
|
103
|
+
const imports: Set<string> = new Set(["http"]);
|
|
104
|
+
|
|
105
|
+
let code = "";
|
|
106
|
+
code += "local body, code, headers, status = http.request";
|
|
107
|
+
|
|
108
|
+
if (
|
|
109
|
+
eq(request.urls[0].method, "GET") &&
|
|
110
|
+
!request.data &&
|
|
111
|
+
!request.headers.length &&
|
|
112
|
+
!request.urls[0].auth
|
|
113
|
+
) {
|
|
114
|
+
code += "(" + repr(request.urls[0].originalUrl) + ")\n";
|
|
115
|
+
} else if (
|
|
116
|
+
eq(request.urls[0].method, "POST") &&
|
|
117
|
+
request.data &&
|
|
118
|
+
!request.urls[0].auth &&
|
|
119
|
+
request.headers.length === 1 &&
|
|
120
|
+
request.headers.has("content-type") &&
|
|
121
|
+
eq(request.headers.get("content-type"), "application/x-www-form-urlencoded")
|
|
122
|
+
) {
|
|
123
|
+
code += "(\n";
|
|
124
|
+
code += "\t" + repr(request.urls[0].originalUrl) + "\n";
|
|
125
|
+
code += "\t" + repr(request.data) + "\n";
|
|
126
|
+
code += ")\n";
|
|
127
|
+
} else {
|
|
128
|
+
code += "{\n";
|
|
129
|
+
if (!eq(request.urls[0].method, "GET")) {
|
|
130
|
+
code += "\tmethod = " + repr(request.urls[0].method) + ",\n";
|
|
131
|
+
}
|
|
132
|
+
code += "\turl = " + repr(request.urls[0].originalUrl) + ",\n";
|
|
133
|
+
|
|
134
|
+
if (request.data) {
|
|
135
|
+
code += "\tsource = ltn12.source.string(";
|
|
136
|
+
if (
|
|
137
|
+
request.headers.getContentType() === "application/json" &&
|
|
138
|
+
request.data.isString()
|
|
139
|
+
) {
|
|
140
|
+
try {
|
|
141
|
+
code +=
|
|
142
|
+
"json.encode(" +
|
|
143
|
+
jsonToLua(JSON.parse(request.data.toString()), 1) +
|
|
144
|
+
")";
|
|
145
|
+
imports.add("json");
|
|
146
|
+
} catch (e) {
|
|
147
|
+
code += repr(request.data);
|
|
148
|
+
}
|
|
149
|
+
} else {
|
|
150
|
+
code += repr(request.data);
|
|
151
|
+
}
|
|
152
|
+
code += "),\n";
|
|
153
|
+
imports.add("ltn12");
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
if (request.headers.length > 0 || request.urls[0].auth) {
|
|
157
|
+
code += "\theaders = {\n";
|
|
158
|
+
for (const [name, value] of request.headers) {
|
|
159
|
+
if (value === null) {
|
|
160
|
+
continue;
|
|
161
|
+
}
|
|
162
|
+
code += "\t\t" + reprKey(name) + " = " + repr(value) + ",\n";
|
|
163
|
+
}
|
|
164
|
+
if (request.urls[0].auth) {
|
|
165
|
+
code +=
|
|
166
|
+
'\t\tauthentication = "Basic " .. (mime.b64(' +
|
|
167
|
+
repr(joinWords(request.urls[0].auth, ":")) +
|
|
168
|
+
")),\n";
|
|
169
|
+
imports.add("mime");
|
|
170
|
+
}
|
|
171
|
+
if (code.endsWith(",\n")) {
|
|
172
|
+
code = code.slice(0, -2) + "\n";
|
|
173
|
+
}
|
|
174
|
+
code += "\t},\n";
|
|
175
|
+
}
|
|
176
|
+
if (code.endsWith(",\n")) {
|
|
177
|
+
code = code.slice(0, -2) + "\n";
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
// code += "\tsink = ltn12.sink.table(respbody)\n";
|
|
181
|
+
|
|
182
|
+
code += "}\n";
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
let importCode = "";
|
|
186
|
+
if (imports.size) {
|
|
187
|
+
for (const imp of Array.from(imports).sort()) {
|
|
188
|
+
if (imp === "http") {
|
|
189
|
+
importCode += 'local http = require("socket.http")\n';
|
|
190
|
+
} else {
|
|
191
|
+
importCode += "local " + imp + ' = require("' + imp + '")\n';
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
return importCode + "\n" + code;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
export function toLuaWarn(
|
|
200
|
+
curlCommand: string | string[],
|
|
201
|
+
warnings: Warnings = [],
|
|
202
|
+
): [string, Warnings] {
|
|
203
|
+
const requests = parse(curlCommand, supportedArgs, warnings);
|
|
204
|
+
const lua = _toLua(requests, warnings);
|
|
205
|
+
return [lua, warnings];
|
|
206
|
+
}
|
|
207
|
+
export function toLua(curlCommand: string | string[]): string {
|
|
208
|
+
return toLuaWarn(curlCommand)[0];
|
|
209
|
+
}
|