@readme/httpsnippet 7.1.2 → 8.0.1
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 +1 -1
- package/README.md +24 -25
- package/dist/{chunk-DTZD7L6R.mjs → chunk-IAWYVBVW.js} +78 -61
- package/dist/chunk-IAWYVBVW.js.map +1 -0
- package/dist/{chunk-BCCGHMCJ.mjs → chunk-KT7MO6Z4.js} +1 -1
- package/dist/{chunk-UEOS42PC.mjs → chunk-Y7NI4MMY.js} +1 -1
- package/dist/helpers/code-builder.cjs +65 -0
- package/dist/helpers/code-builder.cjs.map +1 -0
- package/dist/helpers/code-builder.js +1 -63
- package/dist/helpers/code-builder.js.map +1 -1
- package/dist/helpers/reducer.cjs +20 -0
- package/dist/helpers/reducer.cjs.map +1 -0
- package/dist/helpers/reducer.js +1 -18
- package/dist/helpers/reducer.js.map +1 -1
- package/dist/{index-e612cd58.d.ts → index-babe3117.d.ts} +10 -6
- package/dist/{index-27be831e.d.ts → index-c8003aae.d.ts} +11 -7
- package/dist/index.cjs +3745 -0
- package/dist/index.cjs.map +1 -0
- package/dist/{index.d.mts → index.d.cts} +3 -3
- package/dist/index.d.ts +1 -1
- package/dist/index.js +179 -3747
- package/dist/index.js.map +1 -1
- package/dist/targets/index.cjs +3512 -0
- package/dist/targets/index.cjs.map +1 -0
- package/dist/targets/{index.d.mts → index.d.cts} +3 -3
- package/dist/targets/index.d.ts +1 -1
- package/dist/targets/index.js +2 -3493
- package/dist/targets/index.js.map +1 -1
- package/package.json +36 -40
- package/dist/chunk-DTZD7L6R.mjs.map +0 -1
- package/dist/helpers/code-builder.mjs +0 -3
- package/dist/helpers/code-builder.mjs.map +0 -1
- package/dist/helpers/reducer.mjs +0 -3
- package/dist/helpers/reducer.mjs.map +0 -1
- package/dist/index.mjs +0 -285
- package/dist/index.mjs.map +0 -1
- package/dist/targets/index.mjs +0 -4
- package/dist/targets/index.mjs.map +0 -1
- /package/dist/{chunk-BCCGHMCJ.mjs.map → chunk-KT7MO6Z4.js.map} +0 -0
- /package/dist/{chunk-UEOS42PC.mjs.map → chunk-Y7NI4MMY.js.map} +0 -0
- /package/dist/helpers/{code-builder.d.mts → code-builder.d.cts} +0 -0
- /package/dist/helpers/{reducer.d.mts → reducer.d.cts} +0 -0
package/dist/targets/index.js
CHANGED
|
@@ -1,3495 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
var stringifyObject9 = require('stringify-object');
|
|
4
|
-
|
|
5
|
-
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
6
|
-
|
|
7
|
-
var stringifyObject9__default = /*#__PURE__*/_interopDefault(stringifyObject9);
|
|
8
|
-
|
|
9
|
-
// src/helpers/code-builder.ts
|
|
10
|
-
var DEFAULT_INDENTATION_CHARACTER = "";
|
|
11
|
-
var DEFAULT_LINE_JOIN = "\n";
|
|
12
|
-
var CodeBuilder = class {
|
|
13
|
-
/**
|
|
14
|
-
* Helper object to format and aggragate lines of code.
|
|
15
|
-
* Lines are aggregated in a `code` array, and need to be joined to obtain a proper code snippet.
|
|
16
|
-
*/
|
|
17
|
-
constructor({ indent, join } = {}) {
|
|
18
|
-
this.postProcessors = [];
|
|
19
|
-
this.code = [];
|
|
20
|
-
this.indentationCharacter = DEFAULT_INDENTATION_CHARACTER;
|
|
21
|
-
this.lineJoin = DEFAULT_LINE_JOIN;
|
|
22
|
-
/**
|
|
23
|
-
* Add given indentation level to given line of code
|
|
24
|
-
*/
|
|
25
|
-
this.indentLine = (line, indentationLevel = 0) => {
|
|
26
|
-
const indent = this.indentationCharacter.repeat(indentationLevel);
|
|
27
|
-
return `${indent}${line}`;
|
|
28
|
-
};
|
|
29
|
-
/**
|
|
30
|
-
* Add the line at the beginning of the current lines
|
|
31
|
-
*/
|
|
32
|
-
this.unshift = (line, indentationLevel) => {
|
|
33
|
-
const newLine = this.indentLine(line, indentationLevel);
|
|
34
|
-
this.code.unshift(newLine);
|
|
35
|
-
};
|
|
36
|
-
/**
|
|
37
|
-
* Add the line at the end of the current lines
|
|
38
|
-
*/
|
|
39
|
-
this.push = (line, indentationLevel) => {
|
|
40
|
-
const newLine = this.indentLine(line, indentationLevel);
|
|
41
|
-
this.code.push(newLine);
|
|
42
|
-
};
|
|
43
|
-
/**
|
|
44
|
-
* Add an empty line at the end of current lines
|
|
45
|
-
*/
|
|
46
|
-
this.blank = () => {
|
|
47
|
-
this.code.push("");
|
|
48
|
-
};
|
|
49
|
-
/**
|
|
50
|
-
* Concatenate all current lines using the given lineJoin, then apply any replacers that may have been added
|
|
51
|
-
*/
|
|
52
|
-
this.join = () => {
|
|
53
|
-
const unreplacedCode = this.code.join(this.lineJoin);
|
|
54
|
-
const replacedOutput = this.postProcessors.reduce((accumulator, replacer) => replacer(accumulator), unreplacedCode);
|
|
55
|
-
return replacedOutput;
|
|
56
|
-
};
|
|
57
|
-
/**
|
|
58
|
-
* Often when writing modules you may wish to add a literal tag or bit of metadata that you wish to transform after other processing as a final step.
|
|
59
|
-
* To do so, you can provide a PostProcessor function and it will be run automatically for you when you call `join()` later on.
|
|
60
|
-
*/
|
|
61
|
-
this.addPostProcessor = (postProcessor) => {
|
|
62
|
-
this.postProcessors = [...this.postProcessors, postProcessor];
|
|
63
|
-
};
|
|
64
|
-
this.indentationCharacter = indent || DEFAULT_INDENTATION_CHARACTER;
|
|
65
|
-
this.lineJoin = join ?? DEFAULT_LINE_JOIN;
|
|
66
|
-
}
|
|
67
|
-
};
|
|
68
|
-
|
|
69
|
-
// src/helpers/escape.ts
|
|
70
|
-
function escapeString(rawValue, options = {}) {
|
|
71
|
-
const { delimiter = '"', escapeChar = "\\", escapeNewlines = true } = options;
|
|
72
|
-
const stringValue = rawValue.toString();
|
|
73
|
-
return [...stringValue].map((c2) => {
|
|
74
|
-
if (c2 === "\b") {
|
|
75
|
-
return `${escapeChar}b`;
|
|
76
|
-
} else if (c2 === " ") {
|
|
77
|
-
return `${escapeChar}t`;
|
|
78
|
-
} else if (c2 === "\n") {
|
|
79
|
-
if (escapeNewlines) {
|
|
80
|
-
return `${escapeChar}n`;
|
|
81
|
-
}
|
|
82
|
-
return c2;
|
|
83
|
-
} else if (c2 === "\f") {
|
|
84
|
-
return `${escapeChar}f`;
|
|
85
|
-
} else if (c2 === "\r") {
|
|
86
|
-
if (escapeNewlines) {
|
|
87
|
-
return `${escapeChar}r`;
|
|
88
|
-
}
|
|
89
|
-
return c2;
|
|
90
|
-
} else if (c2 === escapeChar) {
|
|
91
|
-
return escapeChar + escapeChar;
|
|
92
|
-
} else if (c2 === delimiter) {
|
|
93
|
-
return escapeChar + delimiter;
|
|
94
|
-
} else if (c2 < " " || c2 > "~") {
|
|
95
|
-
return JSON.stringify(c2).slice(1, -1);
|
|
96
|
-
}
|
|
97
|
-
return c2;
|
|
98
|
-
}).join("");
|
|
99
|
-
}
|
|
100
|
-
var escapeForSingleQuotes = (value) => escapeString(value, { delimiter: "'" });
|
|
101
|
-
var escapeForDoubleQuotes = (value) => escapeString(value, { delimiter: '"' });
|
|
102
|
-
|
|
103
|
-
// src/targets/c/libcurl/client.ts
|
|
104
|
-
var libcurl = {
|
|
105
|
-
info: {
|
|
106
|
-
key: "libcurl",
|
|
107
|
-
title: "Libcurl",
|
|
108
|
-
link: "http://curl.haxx.se/libcurl",
|
|
109
|
-
description: "Simple REST and HTTP API Client for C"
|
|
110
|
-
},
|
|
111
|
-
convert: ({ method, fullUrl, headersObj, allHeaders, postData }) => {
|
|
112
|
-
const { push, blank, join } = new CodeBuilder();
|
|
113
|
-
push("CURL *hnd = curl_easy_init();");
|
|
114
|
-
blank();
|
|
115
|
-
push(`curl_easy_setopt(hnd, CURLOPT_CUSTOMREQUEST, "${method.toUpperCase()}");`);
|
|
116
|
-
push("curl_easy_setopt(hnd, CURLOPT_WRITEDATA, stdout);");
|
|
117
|
-
push(`curl_easy_setopt(hnd, CURLOPT_URL, "${fullUrl}");`);
|
|
118
|
-
const headers = Object.keys(headersObj);
|
|
119
|
-
if (headers.length) {
|
|
120
|
-
blank();
|
|
121
|
-
push("struct curl_slist *headers = NULL;");
|
|
122
|
-
headers.forEach((header) => {
|
|
123
|
-
push(`headers = curl_slist_append(headers, "${header}: ${escapeForDoubleQuotes(headersObj[header])}");`);
|
|
124
|
-
});
|
|
125
|
-
push("curl_easy_setopt(hnd, CURLOPT_HTTPHEADER, headers);");
|
|
126
|
-
}
|
|
127
|
-
if (allHeaders.cookie) {
|
|
128
|
-
blank();
|
|
129
|
-
push(`curl_easy_setopt(hnd, CURLOPT_COOKIE, "${allHeaders.cookie}");`);
|
|
130
|
-
}
|
|
131
|
-
if (postData.text) {
|
|
132
|
-
blank();
|
|
133
|
-
push(`curl_easy_setopt(hnd, CURLOPT_POSTFIELDS, ${JSON.stringify(postData.text)});`);
|
|
134
|
-
}
|
|
135
|
-
blank();
|
|
136
|
-
push("CURLcode ret = curl_easy_perform(hnd);");
|
|
137
|
-
return join();
|
|
138
|
-
}
|
|
139
|
-
};
|
|
140
|
-
|
|
141
|
-
// src/targets/c/target.ts
|
|
142
|
-
var c = {
|
|
143
|
-
info: {
|
|
144
|
-
key: "c",
|
|
145
|
-
title: "C",
|
|
146
|
-
extname: ".c",
|
|
147
|
-
default: "libcurl",
|
|
148
|
-
cli: "c"
|
|
149
|
-
},
|
|
150
|
-
clientsById: {
|
|
151
|
-
libcurl
|
|
152
|
-
}
|
|
153
|
-
};
|
|
154
|
-
|
|
155
|
-
// src/helpers/headers.ts
|
|
156
|
-
var getHeaderName = (headers, name) => Object.keys(headers).find((header) => header.toLowerCase() === name.toLowerCase());
|
|
157
|
-
var getHeader = (headers, name) => {
|
|
158
|
-
const headerName = getHeaderName(headers, name);
|
|
159
|
-
if (!headerName) {
|
|
160
|
-
return void 0;
|
|
161
|
-
}
|
|
162
|
-
return headers[headerName];
|
|
163
|
-
};
|
|
164
|
-
var hasHeader = (headers, name) => Boolean(getHeaderName(headers, name));
|
|
165
|
-
var isMimeTypeJSON = (mimeType) => ["application/json", "application/x-json", "text/json", "text/x-json", "+json"].some(
|
|
166
|
-
(type) => mimeType.indexOf(type) > -1
|
|
167
|
-
);
|
|
168
|
-
|
|
169
|
-
// src/targets/clojure/clj_http/client.ts
|
|
170
|
-
var Keyword = class {
|
|
171
|
-
constructor(name) {
|
|
172
|
-
this.name = "";
|
|
173
|
-
this.toString = () => `:${this.name}`;
|
|
174
|
-
this.name = name;
|
|
175
|
-
}
|
|
176
|
-
};
|
|
177
|
-
var File = class {
|
|
178
|
-
constructor(path) {
|
|
179
|
-
this.path = "";
|
|
180
|
-
this.toString = () => `(clojure.java.io/file "${this.path}")`;
|
|
181
|
-
this.path = path;
|
|
182
|
-
}
|
|
183
|
-
};
|
|
184
|
-
var jsType = (input) => {
|
|
185
|
-
if (input === void 0) {
|
|
186
|
-
return null;
|
|
187
|
-
}
|
|
188
|
-
if (input === null) {
|
|
189
|
-
return "null";
|
|
190
|
-
}
|
|
191
|
-
return input.constructor.name.toLowerCase();
|
|
192
|
-
};
|
|
193
|
-
var objEmpty = (input) => {
|
|
194
|
-
if (input === void 0) {
|
|
195
|
-
return true;
|
|
196
|
-
} else if (jsType(input) === "object") {
|
|
197
|
-
return Object.keys(input).length === 0;
|
|
198
|
-
}
|
|
199
|
-
return false;
|
|
200
|
-
};
|
|
201
|
-
var filterEmpty = (input) => {
|
|
202
|
-
Object.keys(input).filter((x) => objEmpty(input[x])).forEach((x) => {
|
|
203
|
-
delete input[x];
|
|
204
|
-
});
|
|
205
|
-
return input;
|
|
206
|
-
};
|
|
207
|
-
var padBlock = (padSize, input) => {
|
|
208
|
-
const padding = " ".repeat(padSize);
|
|
209
|
-
return input.replace(/\n/g, `
|
|
210
|
-
${padding}`);
|
|
211
|
-
};
|
|
212
|
-
var jsToEdn = (js) => {
|
|
213
|
-
switch (jsType(js)) {
|
|
214
|
-
case "string":
|
|
215
|
-
return `"${js.replace(/"/g, '\\"')}"`;
|
|
216
|
-
case "file":
|
|
217
|
-
return js.toString();
|
|
218
|
-
case "keyword":
|
|
219
|
-
return js.toString();
|
|
220
|
-
case "null":
|
|
221
|
-
return "nil";
|
|
222
|
-
case "regexp":
|
|
223
|
-
return `#"${js.source}"`;
|
|
224
|
-
case "object": {
|
|
225
|
-
const obj = Object.keys(js).reduce((accumulator, key) => {
|
|
226
|
-
const val = padBlock(key.length + 2, jsToEdn(js[key]));
|
|
227
|
-
return `${accumulator}:${key} ${val}
|
|
228
|
-
`;
|
|
229
|
-
}, "").trim();
|
|
230
|
-
return `{${padBlock(1, obj)}}`;
|
|
231
|
-
}
|
|
232
|
-
case "array": {
|
|
233
|
-
const arr = js.reduce((accumulator, value) => `${accumulator} ${jsToEdn(value)}`, "").trim();
|
|
234
|
-
return `[${padBlock(1, arr)}]`;
|
|
235
|
-
}
|
|
236
|
-
default:
|
|
237
|
-
return js.toString();
|
|
238
|
-
}
|
|
239
|
-
};
|
|
240
|
-
var clj_http = {
|
|
241
|
-
info: {
|
|
242
|
-
key: "clj_http",
|
|
243
|
-
title: "clj-http",
|
|
244
|
-
link: "https://github.com/dakrone/clj-http",
|
|
245
|
-
description: "An idiomatic clojure http client wrapping the apache client."
|
|
246
|
-
},
|
|
247
|
-
convert: ({ queryObj, method, postData, url, allHeaders }, options) => {
|
|
248
|
-
const { push, join } = new CodeBuilder({ indent: options?.indent });
|
|
249
|
-
const methods = ["get", "post", "put", "delete", "patch", "head", "options"];
|
|
250
|
-
method = method.toLowerCase();
|
|
251
|
-
if (!methods.includes(method)) {
|
|
252
|
-
push("Method not supported");
|
|
253
|
-
return join();
|
|
254
|
-
}
|
|
255
|
-
const params2 = {
|
|
256
|
-
headers: allHeaders,
|
|
257
|
-
"query-params": queryObj
|
|
258
|
-
};
|
|
259
|
-
switch (postData.mimeType) {
|
|
260
|
-
case "application/json":
|
|
261
|
-
{
|
|
262
|
-
params2["content-type"] = new Keyword("json");
|
|
263
|
-
params2["form-params"] = postData.jsonObj;
|
|
264
|
-
const header = getHeaderName(params2.headers, "content-type");
|
|
265
|
-
if (header) {
|
|
266
|
-
delete params2.headers[header];
|
|
267
|
-
}
|
|
268
|
-
}
|
|
269
|
-
break;
|
|
270
|
-
case "application/x-www-form-urlencoded":
|
|
271
|
-
{
|
|
272
|
-
params2["form-params"] = postData.paramsObj;
|
|
273
|
-
const header = getHeaderName(params2.headers, "content-type");
|
|
274
|
-
if (header) {
|
|
275
|
-
delete params2.headers[header];
|
|
276
|
-
}
|
|
277
|
-
}
|
|
278
|
-
break;
|
|
279
|
-
case "text/plain":
|
|
280
|
-
{
|
|
281
|
-
params2.body = postData.text;
|
|
282
|
-
const header = getHeaderName(params2.headers, "content-type");
|
|
283
|
-
if (header) {
|
|
284
|
-
delete params2.headers[header];
|
|
285
|
-
}
|
|
286
|
-
}
|
|
287
|
-
break;
|
|
288
|
-
case "multipart/form-data": {
|
|
289
|
-
if (postData.params) {
|
|
290
|
-
params2.multipart = postData.params.map((param) => {
|
|
291
|
-
if (param.fileName && !param.value) {
|
|
292
|
-
return {
|
|
293
|
-
name: param.name,
|
|
294
|
-
content: new File(param.fileName)
|
|
295
|
-
};
|
|
296
|
-
}
|
|
297
|
-
return {
|
|
298
|
-
name: param.name,
|
|
299
|
-
content: param.value
|
|
300
|
-
};
|
|
301
|
-
});
|
|
302
|
-
const header = getHeaderName(params2.headers, "content-type");
|
|
303
|
-
if (header) {
|
|
304
|
-
delete params2.headers[header];
|
|
305
|
-
}
|
|
306
|
-
}
|
|
307
|
-
break;
|
|
308
|
-
}
|
|
309
|
-
}
|
|
310
|
-
switch (getHeader(params2.headers, "accept")) {
|
|
311
|
-
case "application/json":
|
|
312
|
-
{
|
|
313
|
-
params2.accept = new Keyword("json");
|
|
314
|
-
const header = getHeaderName(params2.headers, "accept");
|
|
315
|
-
if (header) {
|
|
316
|
-
delete params2.headers[header];
|
|
317
|
-
}
|
|
318
|
-
}
|
|
319
|
-
break;
|
|
320
|
-
}
|
|
321
|
-
push("(require '[clj-http.client :as client])\n");
|
|
322
|
-
if (objEmpty(filterEmpty(params2))) {
|
|
323
|
-
push(`(client/${method} "${url}")`);
|
|
324
|
-
} else {
|
|
325
|
-
const padding = 11 + method.length + url.length;
|
|
326
|
-
const formattedParams = padBlock(padding, jsToEdn(filterEmpty(params2)));
|
|
327
|
-
push(`(client/${method} "${url}" ${formattedParams})`);
|
|
328
|
-
}
|
|
329
|
-
return join();
|
|
330
|
-
}
|
|
331
|
-
};
|
|
332
|
-
|
|
333
|
-
// src/targets/clojure/target.ts
|
|
334
|
-
var clojure = {
|
|
335
|
-
info: {
|
|
336
|
-
key: "clojure",
|
|
337
|
-
title: "Clojure",
|
|
338
|
-
extname: ".clj",
|
|
339
|
-
default: "clj_http"
|
|
340
|
-
},
|
|
341
|
-
clientsById: {
|
|
342
|
-
clj_http
|
|
343
|
-
}
|
|
344
|
-
};
|
|
345
|
-
|
|
346
|
-
// src/targets/csharp/httpclient/client.ts
|
|
347
|
-
var getDecompressionMethods = (allHeaders) => {
|
|
348
|
-
let acceptEncodings = getHeader(allHeaders, "accept-encoding");
|
|
349
|
-
if (!acceptEncodings) {
|
|
350
|
-
return [];
|
|
351
|
-
}
|
|
352
|
-
const supportedMethods2 = {
|
|
353
|
-
gzip: "DecompressionMethods.GZip",
|
|
354
|
-
deflate: "DecompressionMethods.Deflate"
|
|
355
|
-
};
|
|
356
|
-
const methods = [];
|
|
357
|
-
if (typeof acceptEncodings === "string") {
|
|
358
|
-
acceptEncodings = [acceptEncodings];
|
|
359
|
-
}
|
|
360
|
-
acceptEncodings.forEach((acceptEncoding) => {
|
|
361
|
-
acceptEncoding.split(",").forEach((encoding) => {
|
|
362
|
-
const match = /\s*([^;\s]+)/.exec(encoding);
|
|
363
|
-
if (match) {
|
|
364
|
-
const method = supportedMethods2[match[1]];
|
|
365
|
-
if (method) {
|
|
366
|
-
methods.push(method);
|
|
367
|
-
}
|
|
368
|
-
}
|
|
369
|
-
});
|
|
370
|
-
});
|
|
371
|
-
return methods;
|
|
372
|
-
};
|
|
373
|
-
var httpclient = {
|
|
374
|
-
info: {
|
|
375
|
-
key: "httpclient",
|
|
376
|
-
title: "HttpClient",
|
|
377
|
-
link: "https://docs.microsoft.com/en-us/dotnet/api/system.net.http.httpclient",
|
|
378
|
-
description: ".NET Standard HTTP Client"
|
|
379
|
-
},
|
|
380
|
-
convert: ({ allHeaders, postData, method, fullUrl }, options) => {
|
|
381
|
-
const opts = {
|
|
382
|
-
indent: " ",
|
|
383
|
-
...options
|
|
384
|
-
};
|
|
385
|
-
const { push, join } = new CodeBuilder({ indent: opts.indent });
|
|
386
|
-
push("using System.Net.Http.Headers;");
|
|
387
|
-
let clienthandler = "";
|
|
388
|
-
const cookies = Boolean(allHeaders.cookie);
|
|
389
|
-
const decompressionMethods = getDecompressionMethods(allHeaders);
|
|
390
|
-
if (cookies || decompressionMethods.length) {
|
|
391
|
-
clienthandler = "clientHandler";
|
|
392
|
-
push("var clientHandler = new HttpClientHandler");
|
|
393
|
-
push("{");
|
|
394
|
-
if (cookies) {
|
|
395
|
-
push("UseCookies = false,", 1);
|
|
396
|
-
}
|
|
397
|
-
if (decompressionMethods.length) {
|
|
398
|
-
push(`AutomaticDecompression = ${decompressionMethods.join(" | ")},`, 1);
|
|
399
|
-
}
|
|
400
|
-
push("};");
|
|
401
|
-
}
|
|
402
|
-
push(`var client = new HttpClient(${clienthandler});`);
|
|
403
|
-
push("var request = new HttpRequestMessage");
|
|
404
|
-
push("{");
|
|
405
|
-
const methods = ["GET", "POST", "PUT", "DELETE", "PATCH", "HEAD", "OPTIONS", "TRACE"];
|
|
406
|
-
method = method.toUpperCase();
|
|
407
|
-
if (method && methods.includes(method)) {
|
|
408
|
-
method = `HttpMethod.${method[0]}${method.substring(1).toLowerCase()}`;
|
|
409
|
-
} else {
|
|
410
|
-
method = `new HttpMethod("${method}")`;
|
|
411
|
-
}
|
|
412
|
-
push(`Method = ${method},`, 1);
|
|
413
|
-
push(`RequestUri = new Uri("${fullUrl}"),`, 1);
|
|
414
|
-
const headers = Object.keys(allHeaders).filter((header) => {
|
|
415
|
-
switch (header.toLowerCase()) {
|
|
416
|
-
case "content-type":
|
|
417
|
-
case "content-length":
|
|
418
|
-
case "accept-encoding":
|
|
419
|
-
return false;
|
|
420
|
-
default:
|
|
421
|
-
return true;
|
|
422
|
-
}
|
|
423
|
-
});
|
|
424
|
-
if (headers.length) {
|
|
425
|
-
push("Headers =", 1);
|
|
426
|
-
push("{", 1);
|
|
427
|
-
headers.forEach((key) => {
|
|
428
|
-
push(`{ "${key}", "${escapeForDoubleQuotes(allHeaders[key])}" },`, 2);
|
|
429
|
-
});
|
|
430
|
-
push("},", 1);
|
|
431
|
-
}
|
|
432
|
-
if (postData.text) {
|
|
433
|
-
const contentType = postData.mimeType;
|
|
434
|
-
switch (contentType) {
|
|
435
|
-
case "application/x-www-form-urlencoded":
|
|
436
|
-
push("Content = new FormUrlEncodedContent(new Dictionary<string, string>", 1);
|
|
437
|
-
push("{", 1);
|
|
438
|
-
postData.params?.forEach((param) => {
|
|
439
|
-
push(`{ "${param.name}", "${param.value}" },`, 2);
|
|
440
|
-
});
|
|
441
|
-
push("}),", 1);
|
|
442
|
-
break;
|
|
443
|
-
case "multipart/form-data":
|
|
444
|
-
push("Content = new MultipartFormDataContent", 1);
|
|
445
|
-
push("{", 1);
|
|
446
|
-
postData.params?.forEach((param) => {
|
|
447
|
-
push(`new StringContent(${JSON.stringify(param.value || "")})`, 2);
|
|
448
|
-
push("{", 2);
|
|
449
|
-
push("Headers =", 3);
|
|
450
|
-
push("{", 3);
|
|
451
|
-
if (param.contentType) {
|
|
452
|
-
push(`ContentType = new MediaTypeHeaderValue("${param.contentType}"),`, 4);
|
|
453
|
-
}
|
|
454
|
-
push('ContentDisposition = new ContentDispositionHeaderValue("form-data")', 4);
|
|
455
|
-
push("{", 4);
|
|
456
|
-
push(`Name = "${param.name}",`, 5);
|
|
457
|
-
if (param.fileName) {
|
|
458
|
-
push(`FileName = "${param.fileName}",`, 5);
|
|
459
|
-
}
|
|
460
|
-
push("}", 4);
|
|
461
|
-
push("}", 3);
|
|
462
|
-
push("},", 2);
|
|
463
|
-
});
|
|
464
|
-
push("},", 1);
|
|
465
|
-
break;
|
|
466
|
-
default:
|
|
467
|
-
push(`Content = new StringContent(${JSON.stringify(postData.text || "")})`, 1);
|
|
468
|
-
push("{", 1);
|
|
469
|
-
push("Headers =", 2);
|
|
470
|
-
push("{", 2);
|
|
471
|
-
push(`ContentType = new MediaTypeHeaderValue("${contentType}")`, 3);
|
|
472
|
-
push("}", 2);
|
|
473
|
-
push("}", 1);
|
|
474
|
-
break;
|
|
475
|
-
}
|
|
476
|
-
}
|
|
477
|
-
push("};");
|
|
478
|
-
push("using (var response = await client.SendAsync(request))");
|
|
479
|
-
push("{");
|
|
480
|
-
push("response.EnsureSuccessStatusCode();", 1);
|
|
481
|
-
push("var body = await response.Content.ReadAsStringAsync();", 1);
|
|
482
|
-
push("Console.WriteLine(body);", 1);
|
|
483
|
-
push("}");
|
|
484
|
-
return join();
|
|
485
|
-
}
|
|
486
|
-
};
|
|
487
|
-
|
|
488
|
-
// src/targets/csharp/restsharp/client.ts
|
|
489
|
-
function title(s) {
|
|
490
|
-
return s[0].toUpperCase() + s.slice(1).toLowerCase();
|
|
491
|
-
}
|
|
492
|
-
var restsharp = {
|
|
493
|
-
info: {
|
|
494
|
-
key: "restsharp",
|
|
495
|
-
title: "RestSharp",
|
|
496
|
-
link: "http://restsharp.org/",
|
|
497
|
-
description: "Simple REST and HTTP API Client for .NET"
|
|
498
|
-
},
|
|
499
|
-
convert: ({ method, fullUrl, headersObj, cookies, postData, uriObj }) => {
|
|
500
|
-
const { push, join } = new CodeBuilder();
|
|
501
|
-
const isSupportedMethod = ["GET", "POST", "PUT", "DELETE", "PATCH", "HEAD", "OPTIONS"].includes(
|
|
502
|
-
method.toUpperCase()
|
|
503
|
-
);
|
|
504
|
-
if (!isSupportedMethod) {
|
|
505
|
-
return "Method not supported";
|
|
506
|
-
}
|
|
507
|
-
push("using RestSharp;\n\n");
|
|
508
|
-
push(`var options = new RestClientOptions("${fullUrl}");`);
|
|
509
|
-
push("var client = new RestClient(options);");
|
|
510
|
-
push('var request = new RestRequest("");');
|
|
511
|
-
const isMultipart = postData.mimeType && postData.mimeType === "multipart/form-data";
|
|
512
|
-
if (isMultipart) {
|
|
513
|
-
push("request.AlwaysMultipartFormData = true;");
|
|
514
|
-
}
|
|
515
|
-
Object.keys(headersObj).forEach((key) => {
|
|
516
|
-
if (postData.mimeType && key.toLowerCase() === "content-type" && postData.text) {
|
|
517
|
-
if (isMultipart && postData.boundary) {
|
|
518
|
-
push(`request.FormBoundary = "${postData.boundary}";`);
|
|
519
|
-
}
|
|
520
|
-
return;
|
|
521
|
-
}
|
|
522
|
-
push(`request.AddHeader("${key}", "${escapeForDoubleQuotes(headersObj[key])}");`);
|
|
523
|
-
});
|
|
524
|
-
cookies.forEach(({ name, value }) => {
|
|
525
|
-
push(`request.AddCookie("${name}", "${escapeForDoubleQuotes(value)}", "${uriObj.pathname}", "${uriObj.host}");`);
|
|
526
|
-
});
|
|
527
|
-
switch (postData.mimeType) {
|
|
528
|
-
case "multipart/form-data":
|
|
529
|
-
if (!postData.params)
|
|
530
|
-
break;
|
|
531
|
-
postData.params.forEach((param) => {
|
|
532
|
-
if (param.fileName) {
|
|
533
|
-
push(`request.AddFile("${param.name}", "${param.fileName}");`);
|
|
534
|
-
} else {
|
|
535
|
-
push(`request.AddParameter("${param.name}", "${param.value}");`);
|
|
536
|
-
}
|
|
537
|
-
});
|
|
538
|
-
break;
|
|
539
|
-
case "application/x-www-form-urlencoded":
|
|
540
|
-
if (!postData.params)
|
|
541
|
-
break;
|
|
542
|
-
postData.params.forEach((param) => {
|
|
543
|
-
push(`request.AddParameter("${param.name}", "${param.value}");`);
|
|
544
|
-
});
|
|
545
|
-
break;
|
|
546
|
-
case "application/json": {
|
|
547
|
-
if (!postData.text)
|
|
548
|
-
break;
|
|
549
|
-
const text = JSON.stringify(postData.text);
|
|
550
|
-
push(`request.AddJsonBody(${text}, false);`);
|
|
551
|
-
break;
|
|
552
|
-
}
|
|
553
|
-
default:
|
|
554
|
-
if (!postData.text)
|
|
555
|
-
break;
|
|
556
|
-
push(`request.AddStringBody("${postData.text}", "${postData.mimeType}");`);
|
|
557
|
-
}
|
|
558
|
-
push(`var response = await client.${title(method)}Async(request);
|
|
559
|
-
`);
|
|
560
|
-
push('Console.WriteLine("{0}", response.Content);\n');
|
|
561
|
-
return join();
|
|
562
|
-
}
|
|
563
|
-
};
|
|
564
|
-
|
|
565
|
-
// src/targets/csharp/target.ts
|
|
566
|
-
var csharp = {
|
|
567
|
-
info: {
|
|
568
|
-
key: "csharp",
|
|
569
|
-
title: "C#",
|
|
570
|
-
extname: ".cs",
|
|
571
|
-
default: "restsharp",
|
|
572
|
-
cli: "dotnet"
|
|
573
|
-
},
|
|
574
|
-
clientsById: {
|
|
575
|
-
httpclient,
|
|
576
|
-
restsharp
|
|
577
|
-
}
|
|
578
|
-
};
|
|
579
|
-
|
|
580
|
-
// src/targets/go/native/client.ts
|
|
581
|
-
var native = {
|
|
582
|
-
info: {
|
|
583
|
-
key: "native",
|
|
584
|
-
title: "NewRequest",
|
|
585
|
-
link: "http://golang.org/pkg/net/http/#NewRequest",
|
|
586
|
-
description: "Golang HTTP client request"
|
|
587
|
-
},
|
|
588
|
-
convert: ({ postData, method, allHeaders, fullUrl }, options = {}) => {
|
|
589
|
-
const { blank, push, join } = new CodeBuilder({ indent: " " });
|
|
590
|
-
const { showBoilerplate = true, checkErrors = false, printBody = true, timeout = -1 } = options;
|
|
591
|
-
const errorPlaceholder = checkErrors ? "err" : "_";
|
|
592
|
-
const indent = showBoilerplate ? 1 : 0;
|
|
593
|
-
const errorCheck = () => {
|
|
594
|
-
if (checkErrors) {
|
|
595
|
-
push("if err != nil {", indent);
|
|
596
|
-
push("panic(err)", indent + 1);
|
|
597
|
-
push("}", indent);
|
|
598
|
-
}
|
|
599
|
-
};
|
|
600
|
-
if (showBoilerplate) {
|
|
601
|
-
push("package main");
|
|
602
|
-
blank();
|
|
603
|
-
push("import (");
|
|
604
|
-
push('"fmt"', indent);
|
|
605
|
-
if (timeout > 0) {
|
|
606
|
-
push('"time"', indent);
|
|
607
|
-
}
|
|
608
|
-
if (postData.text) {
|
|
609
|
-
push('"strings"', indent);
|
|
610
|
-
}
|
|
611
|
-
push('"net/http"', indent);
|
|
612
|
-
if (printBody) {
|
|
613
|
-
push('"io"', indent);
|
|
614
|
-
}
|
|
615
|
-
push(")");
|
|
616
|
-
blank();
|
|
617
|
-
push("func main() {");
|
|
618
|
-
blank();
|
|
619
|
-
}
|
|
620
|
-
const hasTimeout = timeout > 0;
|
|
621
|
-
const hasClient = hasTimeout;
|
|
622
|
-
const client = hasClient ? "client" : "http.DefaultClient";
|
|
623
|
-
if (hasClient) {
|
|
624
|
-
push("client := http.Client{", indent);
|
|
625
|
-
if (hasTimeout) {
|
|
626
|
-
push(`Timeout: time.Duration(${timeout} * time.Second),`, indent + 1);
|
|
627
|
-
}
|
|
628
|
-
push("}", indent);
|
|
629
|
-
blank();
|
|
630
|
-
}
|
|
631
|
-
push(`url := "${fullUrl}"`, indent);
|
|
632
|
-
blank();
|
|
633
|
-
if (postData.text) {
|
|
634
|
-
push(`payload := strings.NewReader(${JSON.stringify(postData.text)})`, indent);
|
|
635
|
-
blank();
|
|
636
|
-
push(`req, ${errorPlaceholder} := http.NewRequest("${method}", url, payload)`, indent);
|
|
637
|
-
blank();
|
|
638
|
-
} else {
|
|
639
|
-
push(`req, ${errorPlaceholder} := http.NewRequest("${method}", url, nil)`, indent);
|
|
640
|
-
blank();
|
|
641
|
-
}
|
|
642
|
-
errorCheck();
|
|
643
|
-
if (Object.keys(allHeaders).length) {
|
|
644
|
-
Object.keys(allHeaders).forEach((key) => {
|
|
645
|
-
push(`req.Header.Add("${key}", "${escapeForDoubleQuotes(allHeaders[key])}")`, indent);
|
|
646
|
-
});
|
|
647
|
-
blank();
|
|
648
|
-
}
|
|
649
|
-
push(`res, ${errorPlaceholder} := ${client}.Do(req)`, indent);
|
|
650
|
-
errorCheck();
|
|
651
|
-
if (printBody) {
|
|
652
|
-
blank();
|
|
653
|
-
push("defer res.Body.Close()", indent);
|
|
654
|
-
push(`body, ${errorPlaceholder} := io.ReadAll(res.Body)`, indent);
|
|
655
|
-
errorCheck();
|
|
656
|
-
}
|
|
657
|
-
blank();
|
|
658
|
-
if (printBody) {
|
|
659
|
-
push("fmt.Println(string(body))", indent);
|
|
660
|
-
}
|
|
661
|
-
if (showBoilerplate) {
|
|
662
|
-
blank();
|
|
663
|
-
push("}");
|
|
664
|
-
}
|
|
665
|
-
return join();
|
|
666
|
-
}
|
|
667
|
-
};
|
|
668
|
-
|
|
669
|
-
// src/targets/go/target.ts
|
|
670
|
-
var go = {
|
|
671
|
-
info: {
|
|
672
|
-
key: "go",
|
|
673
|
-
title: "Go",
|
|
674
|
-
extname: ".go",
|
|
675
|
-
default: "native",
|
|
676
|
-
cli: "go"
|
|
677
|
-
},
|
|
678
|
-
clientsById: {
|
|
679
|
-
native
|
|
680
|
-
}
|
|
681
|
-
};
|
|
682
|
-
|
|
683
|
-
// src/targets/http/http1.1/client.ts
|
|
684
|
-
var CRLF = "\r\n";
|
|
685
|
-
var http11 = {
|
|
686
|
-
info: {
|
|
687
|
-
key: "http1.1",
|
|
688
|
-
title: "HTTP/1.1",
|
|
689
|
-
link: "https://tools.ietf.org/html/rfc7230",
|
|
690
|
-
description: "HTTP/1.1 request string in accordance with RFC 7230"
|
|
691
|
-
},
|
|
692
|
-
convert: ({ method, fullUrl, uriObj, httpVersion, allHeaders, postData }, options) => {
|
|
693
|
-
const opts = {
|
|
694
|
-
absoluteURI: false,
|
|
695
|
-
autoContentLength: true,
|
|
696
|
-
autoHost: true,
|
|
697
|
-
...options
|
|
698
|
-
};
|
|
699
|
-
const { blank, push, join } = new CodeBuilder({ indent: "", join: CRLF });
|
|
700
|
-
const requestUrl = opts.absoluteURI ? fullUrl : uriObj.path;
|
|
701
|
-
push(`${method} ${requestUrl} ${httpVersion}`);
|
|
702
|
-
const headerKeys = Object.keys(allHeaders);
|
|
703
|
-
headerKeys.forEach((key) => {
|
|
704
|
-
const keyCapitalized = key.toLowerCase().replace(/(^|-)(\w)/g, (input) => input.toUpperCase());
|
|
705
|
-
push(`${keyCapitalized}: ${allHeaders[key]}`);
|
|
706
|
-
});
|
|
707
|
-
if (opts.autoHost && !headerKeys.includes("host")) {
|
|
708
|
-
push(`Host: ${uriObj.host}`);
|
|
709
|
-
}
|
|
710
|
-
if (opts.autoContentLength && postData.text && !headerKeys.includes("content-length")) {
|
|
711
|
-
const length = Buffer.byteLength(postData.text, "ascii").toString();
|
|
712
|
-
push(`Content-Length: ${length}`);
|
|
713
|
-
}
|
|
714
|
-
blank();
|
|
715
|
-
const headerSection = join();
|
|
716
|
-
const messageBody = postData.text || "";
|
|
717
|
-
return `${headerSection}${CRLF}${messageBody}`;
|
|
718
|
-
}
|
|
719
|
-
};
|
|
720
|
-
|
|
721
|
-
// src/targets/http/target.ts
|
|
722
|
-
var http = {
|
|
723
|
-
info: {
|
|
724
|
-
key: "http",
|
|
725
|
-
title: "HTTP",
|
|
726
|
-
extname: null,
|
|
727
|
-
default: "1.1"
|
|
728
|
-
},
|
|
729
|
-
clientsById: {
|
|
730
|
-
"http1.1": http11
|
|
731
|
-
}
|
|
732
|
-
};
|
|
733
|
-
|
|
734
|
-
// src/targets/java/asynchttp/client.ts
|
|
735
|
-
var asynchttp = {
|
|
736
|
-
info: {
|
|
737
|
-
key: "asynchttp",
|
|
738
|
-
title: "AsyncHttp",
|
|
739
|
-
link: "https://github.com/AsyncHttpClient/async-http-client",
|
|
740
|
-
description: "Asynchronous Http and WebSocket Client library for Java"
|
|
741
|
-
},
|
|
742
|
-
convert: ({ method, allHeaders, postData, fullUrl }, options) => {
|
|
743
|
-
const opts = {
|
|
744
|
-
indent: " ",
|
|
745
|
-
...options
|
|
746
|
-
};
|
|
747
|
-
const { blank, push, join } = new CodeBuilder({ indent: opts.indent });
|
|
748
|
-
push("AsyncHttpClient client = new DefaultAsyncHttpClient();");
|
|
749
|
-
push(`client.prepare("${method.toUpperCase()}", "${fullUrl}")`);
|
|
750
|
-
Object.keys(allHeaders).forEach((key) => {
|
|
751
|
-
push(`.setHeader("${key}", "${escapeForDoubleQuotes(allHeaders[key])}")`, 1);
|
|
752
|
-
});
|
|
753
|
-
if (postData.text) {
|
|
754
|
-
push(`.setBody(${JSON.stringify(postData.text)})`, 1);
|
|
755
|
-
}
|
|
756
|
-
push(".execute()", 1);
|
|
757
|
-
push(".toCompletableFuture()", 1);
|
|
758
|
-
push(".thenAccept(System.out::println)", 1);
|
|
759
|
-
push(".join();", 1);
|
|
760
|
-
blank();
|
|
761
|
-
push("client.close();");
|
|
762
|
-
return join();
|
|
763
|
-
}
|
|
764
|
-
};
|
|
765
|
-
|
|
766
|
-
// src/targets/java/nethttp/client.ts
|
|
767
|
-
var nethttp = {
|
|
768
|
-
info: {
|
|
769
|
-
key: "nethttp",
|
|
770
|
-
title: "java.net.http",
|
|
771
|
-
link: "https://openjdk.java.net/groups/net/httpclient/intro.html",
|
|
772
|
-
description: "Java Standardized HTTP Client API"
|
|
773
|
-
},
|
|
774
|
-
convert: ({ allHeaders, fullUrl, method, postData }, options) => {
|
|
775
|
-
const opts = {
|
|
776
|
-
indent: " ",
|
|
777
|
-
...options
|
|
778
|
-
};
|
|
779
|
-
const { push, join } = new CodeBuilder({ indent: opts.indent });
|
|
780
|
-
push("HttpRequest request = HttpRequest.newBuilder()");
|
|
781
|
-
push(`.uri(URI.create("${fullUrl}"))`, 2);
|
|
782
|
-
Object.keys(allHeaders).forEach((key) => {
|
|
783
|
-
push(`.header("${key}", "${escapeForDoubleQuotes(allHeaders[key])}")`, 2);
|
|
784
|
-
});
|
|
785
|
-
if (postData.text) {
|
|
786
|
-
push(
|
|
787
|
-
`.method("${method.toUpperCase()}", HttpRequest.BodyPublishers.ofString(${JSON.stringify(postData.text)}))`,
|
|
788
|
-
2
|
|
789
|
-
);
|
|
790
|
-
} else {
|
|
791
|
-
push(`.method("${method.toUpperCase()}", HttpRequest.BodyPublishers.noBody())`, 2);
|
|
792
|
-
}
|
|
793
|
-
push(".build();", 2);
|
|
794
|
-
push(
|
|
795
|
-
"HttpResponse<String> response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString());"
|
|
796
|
-
);
|
|
797
|
-
push("System.out.println(response.body());");
|
|
798
|
-
return join();
|
|
799
|
-
}
|
|
800
|
-
};
|
|
801
|
-
|
|
802
|
-
// src/targets/java/okhttp/client.ts
|
|
803
|
-
var okhttp = {
|
|
804
|
-
info: {
|
|
805
|
-
key: "okhttp",
|
|
806
|
-
title: "OkHttp",
|
|
807
|
-
link: "http://square.github.io/okhttp/",
|
|
808
|
-
description: "An HTTP Request Client Library"
|
|
809
|
-
},
|
|
810
|
-
convert: ({ postData, method, fullUrl, allHeaders }, options) => {
|
|
811
|
-
const opts = {
|
|
812
|
-
indent: " ",
|
|
813
|
-
...options
|
|
814
|
-
};
|
|
815
|
-
const { push, blank, join } = new CodeBuilder({ indent: opts.indent });
|
|
816
|
-
const methods = ["GET", "POST", "PUT", "DELETE", "PATCH", "HEAD"];
|
|
817
|
-
const methodsWithBody = ["POST", "PUT", "DELETE", "PATCH"];
|
|
818
|
-
push("OkHttpClient client = new OkHttpClient();");
|
|
819
|
-
blank();
|
|
820
|
-
if (postData.text) {
|
|
821
|
-
if (postData.boundary) {
|
|
822
|
-
push(`MediaType mediaType = MediaType.parse("${postData.mimeType}; boundary=${postData.boundary}");`);
|
|
823
|
-
} else {
|
|
824
|
-
push(`MediaType mediaType = MediaType.parse("${postData.mimeType}");`);
|
|
825
|
-
}
|
|
826
|
-
push(`RequestBody body = RequestBody.create(mediaType, ${JSON.stringify(postData.text)});`);
|
|
827
|
-
}
|
|
828
|
-
push("Request request = new Request.Builder()");
|
|
829
|
-
push(`.url("${fullUrl}")`, 1);
|
|
830
|
-
if (!methods.includes(method.toUpperCase())) {
|
|
831
|
-
if (postData.text) {
|
|
832
|
-
push(`.method("${method.toUpperCase()}", body)`, 1);
|
|
833
|
-
} else {
|
|
834
|
-
push(`.method("${method.toUpperCase()}", null)`, 1);
|
|
835
|
-
}
|
|
836
|
-
} else if (methodsWithBody.includes(method.toUpperCase())) {
|
|
837
|
-
if (postData.text) {
|
|
838
|
-
push(`.${method.toLowerCase()}(body)`, 1);
|
|
839
|
-
} else {
|
|
840
|
-
push(`.${method.toLowerCase()}(null)`, 1);
|
|
841
|
-
}
|
|
842
|
-
} else {
|
|
843
|
-
push(`.${method.toLowerCase()}()`, 1);
|
|
844
|
-
}
|
|
845
|
-
Object.keys(allHeaders).forEach((key) => {
|
|
846
|
-
push(`.addHeader("${key}", "${escapeForDoubleQuotes(allHeaders[key])}")`, 1);
|
|
847
|
-
});
|
|
848
|
-
push(".build();", 1);
|
|
849
|
-
blank();
|
|
850
|
-
push("Response response = client.newCall(request).execute();");
|
|
851
|
-
return join();
|
|
852
|
-
}
|
|
853
|
-
};
|
|
854
|
-
|
|
855
|
-
// src/targets/java/unirest/client.ts
|
|
856
|
-
var unirest = {
|
|
857
|
-
info: {
|
|
858
|
-
key: "unirest",
|
|
859
|
-
title: "Unirest",
|
|
860
|
-
link: "http://unirest.io/java.html",
|
|
861
|
-
description: "Lightweight HTTP Request Client Library"
|
|
862
|
-
},
|
|
863
|
-
convert: ({ method, allHeaders, postData, fullUrl }, options) => {
|
|
864
|
-
const opts = {
|
|
865
|
-
indent: " ",
|
|
866
|
-
...options
|
|
867
|
-
};
|
|
868
|
-
const { join, push } = new CodeBuilder({ indent: opts.indent });
|
|
869
|
-
const methods = ["GET", "POST", "PUT", "DELETE", "PATCH", "HEAD", "OPTIONS"];
|
|
870
|
-
if (!methods.includes(method.toUpperCase())) {
|
|
871
|
-
push(`HttpResponse<String> response = Unirest.customMethod("${method.toUpperCase()}","${fullUrl}")`);
|
|
872
|
-
} else {
|
|
873
|
-
push(`HttpResponse<String> response = Unirest.${method.toLowerCase()}("${fullUrl}")`);
|
|
874
|
-
}
|
|
875
|
-
Object.keys(allHeaders).forEach((key) => {
|
|
876
|
-
push(`.header("${key}", "${escapeForDoubleQuotes(allHeaders[key])}")`, 1);
|
|
877
|
-
});
|
|
878
|
-
if (postData.text) {
|
|
879
|
-
push(`.body(${JSON.stringify(postData.text)})`, 1);
|
|
880
|
-
}
|
|
881
|
-
push(".asString();", 1);
|
|
882
|
-
return join();
|
|
883
|
-
}
|
|
884
|
-
};
|
|
885
|
-
|
|
886
|
-
// src/targets/java/target.ts
|
|
887
|
-
var java = {
|
|
888
|
-
info: {
|
|
889
|
-
key: "java",
|
|
890
|
-
title: "Java",
|
|
891
|
-
extname: ".java",
|
|
892
|
-
default: "unirest"
|
|
893
|
-
},
|
|
894
|
-
clientsById: {
|
|
895
|
-
asynchttp,
|
|
896
|
-
nethttp,
|
|
897
|
-
okhttp,
|
|
898
|
-
unirest
|
|
899
|
-
}
|
|
900
|
-
};
|
|
901
|
-
var axios = {
|
|
902
|
-
info: {
|
|
903
|
-
key: "axios",
|
|
904
|
-
title: "Axios",
|
|
905
|
-
link: "https://github.com/axios/axios",
|
|
906
|
-
description: "Promise based HTTP client for the browser and node.js"
|
|
907
|
-
},
|
|
908
|
-
convert: ({ allHeaders, method, url, queryObj, postData }, options) => {
|
|
909
|
-
const opts = {
|
|
910
|
-
indent: " ",
|
|
911
|
-
...options
|
|
912
|
-
};
|
|
913
|
-
const { blank, push, join, addPostProcessor } = new CodeBuilder({ indent: opts.indent });
|
|
914
|
-
push("import axios from 'axios';");
|
|
915
|
-
blank();
|
|
916
|
-
const requestOptions = {
|
|
917
|
-
method,
|
|
918
|
-
url
|
|
919
|
-
};
|
|
920
|
-
if (Object.keys(queryObj).length) {
|
|
921
|
-
requestOptions.params = queryObj;
|
|
922
|
-
}
|
|
923
|
-
if (Object.keys(allHeaders).length) {
|
|
924
|
-
requestOptions.headers = allHeaders;
|
|
925
|
-
}
|
|
926
|
-
switch (postData.mimeType) {
|
|
927
|
-
case "application/x-www-form-urlencoded":
|
|
928
|
-
if (postData.params) {
|
|
929
|
-
push("const encodedParams = new URLSearchParams();");
|
|
930
|
-
postData.params.forEach((param) => {
|
|
931
|
-
push(`encodedParams.set('${param.name}', '${param.value}');`);
|
|
932
|
-
});
|
|
933
|
-
blank();
|
|
934
|
-
requestOptions.data = "encodedParams,";
|
|
935
|
-
addPostProcessor((code) => code.replace(/'encodedParams,'/, "encodedParams,"));
|
|
936
|
-
}
|
|
937
|
-
break;
|
|
938
|
-
case "application/json":
|
|
939
|
-
if (postData.jsonObj) {
|
|
940
|
-
requestOptions.data = postData.jsonObj;
|
|
941
|
-
}
|
|
942
|
-
break;
|
|
943
|
-
case "multipart/form-data":
|
|
944
|
-
if (!postData.params) {
|
|
945
|
-
break;
|
|
946
|
-
}
|
|
947
|
-
push("const form = new FormData();");
|
|
948
|
-
postData.params.forEach((param) => {
|
|
949
|
-
push(`form.append('${param.name}', '${param.value || param.fileName || ""}');`);
|
|
950
|
-
});
|
|
951
|
-
blank();
|
|
952
|
-
requestOptions.data = "[form]";
|
|
953
|
-
break;
|
|
954
|
-
default:
|
|
955
|
-
if (postData.text) {
|
|
956
|
-
requestOptions.data = postData.text;
|
|
957
|
-
}
|
|
958
|
-
}
|
|
959
|
-
const optionString = stringifyObject9__default.default(requestOptions, {
|
|
960
|
-
indent: " ",
|
|
961
|
-
inlineCharacterLimit: 80
|
|
962
|
-
}).replace('"[form]"', "form");
|
|
963
|
-
push(`const options = ${optionString};`);
|
|
964
|
-
blank();
|
|
965
|
-
push("axios");
|
|
966
|
-
push(".request(options)", 1);
|
|
967
|
-
push(".then(function (response) {", 1);
|
|
968
|
-
push("console.log(response.data);", 2);
|
|
969
|
-
push("})", 1);
|
|
970
|
-
push(".catch(function (error) {", 1);
|
|
971
|
-
push("console.error(error);", 2);
|
|
972
|
-
push("});", 1);
|
|
973
|
-
return join();
|
|
974
|
-
}
|
|
975
|
-
};
|
|
976
|
-
var fetch = {
|
|
977
|
-
info: {
|
|
978
|
-
key: "fetch",
|
|
979
|
-
title: "fetch",
|
|
980
|
-
link: "https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch",
|
|
981
|
-
description: "Perform asynchronous HTTP requests with the Fetch API"
|
|
982
|
-
},
|
|
983
|
-
convert: ({ method, allHeaders, postData, fullUrl }, inputOpts) => {
|
|
984
|
-
const opts = {
|
|
985
|
-
indent: " ",
|
|
986
|
-
credentials: null,
|
|
987
|
-
...inputOpts
|
|
988
|
-
};
|
|
989
|
-
const { blank, join, push } = new CodeBuilder({ indent: opts.indent });
|
|
990
|
-
const options = {
|
|
991
|
-
method
|
|
992
|
-
};
|
|
993
|
-
if (Object.keys(allHeaders).length) {
|
|
994
|
-
options.headers = allHeaders;
|
|
995
|
-
}
|
|
996
|
-
if (opts.credentials !== null) {
|
|
997
|
-
options.credentials = opts.credentials;
|
|
998
|
-
}
|
|
999
|
-
switch (postData.mimeType) {
|
|
1000
|
-
case "application/x-www-form-urlencoded":
|
|
1001
|
-
options.body = postData.paramsObj ? postData.paramsObj : postData.text;
|
|
1002
|
-
break;
|
|
1003
|
-
case "application/json":
|
|
1004
|
-
if (postData.jsonObj) {
|
|
1005
|
-
options.body = postData.jsonObj;
|
|
1006
|
-
}
|
|
1007
|
-
break;
|
|
1008
|
-
case "multipart/form-data":
|
|
1009
|
-
if (!postData.params) {
|
|
1010
|
-
break;
|
|
1011
|
-
}
|
|
1012
|
-
const contentTypeHeader = getHeaderName(allHeaders, "content-type");
|
|
1013
|
-
if (contentTypeHeader) {
|
|
1014
|
-
delete allHeaders[contentTypeHeader];
|
|
1015
|
-
}
|
|
1016
|
-
push("const form = new FormData();");
|
|
1017
|
-
postData.params.forEach((param) => {
|
|
1018
|
-
push(`form.append('${param.name}', '${param.value || param.fileName || ""}');`);
|
|
1019
|
-
});
|
|
1020
|
-
blank();
|
|
1021
|
-
break;
|
|
1022
|
-
default:
|
|
1023
|
-
if (postData.text) {
|
|
1024
|
-
options.body = postData.text;
|
|
1025
|
-
}
|
|
1026
|
-
}
|
|
1027
|
-
if (options.headers && !Object.keys(options.headers).length) {
|
|
1028
|
-
delete options.headers;
|
|
1029
|
-
}
|
|
1030
|
-
push(
|
|
1031
|
-
`const options = ${stringifyObject9__default.default(options, {
|
|
1032
|
-
indent: opts.indent,
|
|
1033
|
-
inlineCharacterLimit: 80,
|
|
1034
|
-
// The Fetch API body only accepts string parameters, but stringified JSON can be difficult
|
|
1035
|
-
// to read, so we keep the object as a literal and use this transform function to wrap the
|
|
1036
|
-
// literal in a `JSON.stringify` call.
|
|
1037
|
-
transform: (object, property, originalResult) => {
|
|
1038
|
-
if (property === "body") {
|
|
1039
|
-
if (postData.mimeType === "application/x-www-form-urlencoded") {
|
|
1040
|
-
return `new URLSearchParams(${originalResult})`;
|
|
1041
|
-
} else if (postData.mimeType === "application/json") {
|
|
1042
|
-
return `JSON.stringify(${originalResult})`;
|
|
1043
|
-
}
|
|
1044
|
-
}
|
|
1045
|
-
return originalResult;
|
|
1046
|
-
}
|
|
1047
|
-
})};`
|
|
1048
|
-
);
|
|
1049
|
-
blank();
|
|
1050
|
-
if (postData.params && postData.mimeType === "multipart/form-data") {
|
|
1051
|
-
push("options.body = form;");
|
|
1052
|
-
blank();
|
|
1053
|
-
}
|
|
1054
|
-
push(`fetch('${fullUrl}', options)`);
|
|
1055
|
-
push(".then(response => response.json())", 1);
|
|
1056
|
-
push(".then(response => console.log(response))", 1);
|
|
1057
|
-
push(".catch(err => console.error(err));", 1);
|
|
1058
|
-
return join();
|
|
1059
|
-
}
|
|
1060
|
-
};
|
|
1061
|
-
var jquery = {
|
|
1062
|
-
info: {
|
|
1063
|
-
key: "jquery",
|
|
1064
|
-
title: "jQuery",
|
|
1065
|
-
link: "http://api.jquery.com/jquery.ajax/",
|
|
1066
|
-
description: "Perform an asynchronous HTTP (Ajax) requests with jQuery"
|
|
1067
|
-
},
|
|
1068
|
-
convert: ({ fullUrl, method, allHeaders, postData }, options) => {
|
|
1069
|
-
const opts = {
|
|
1070
|
-
indent: " ",
|
|
1071
|
-
...options
|
|
1072
|
-
};
|
|
1073
|
-
const { blank, push, join } = new CodeBuilder({ indent: opts.indent });
|
|
1074
|
-
const settings = {
|
|
1075
|
-
async: true,
|
|
1076
|
-
crossDomain: true,
|
|
1077
|
-
url: fullUrl,
|
|
1078
|
-
method,
|
|
1079
|
-
headers: allHeaders
|
|
1080
|
-
};
|
|
1081
|
-
switch (postData.mimeType) {
|
|
1082
|
-
case "application/x-www-form-urlencoded":
|
|
1083
|
-
settings.data = postData.paramsObj ? postData.paramsObj : postData.text;
|
|
1084
|
-
break;
|
|
1085
|
-
case "application/json":
|
|
1086
|
-
settings.processData = false;
|
|
1087
|
-
settings.data = postData.text;
|
|
1088
|
-
break;
|
|
1089
|
-
case "multipart/form-data":
|
|
1090
|
-
if (!postData.params) {
|
|
1091
|
-
break;
|
|
1092
|
-
}
|
|
1093
|
-
push("const form = new FormData();");
|
|
1094
|
-
postData.params.forEach((param) => {
|
|
1095
|
-
push(`form.append('${param.name}', '${param.value || param.fileName || ""}');`);
|
|
1096
|
-
});
|
|
1097
|
-
settings.processData = false;
|
|
1098
|
-
settings.contentType = false;
|
|
1099
|
-
settings.mimeType = "multipart/form-data";
|
|
1100
|
-
settings.data = "[form]";
|
|
1101
|
-
if (hasHeader(allHeaders, "content-type")) {
|
|
1102
|
-
if (getHeader(allHeaders, "content-type")?.includes("boundary")) {
|
|
1103
|
-
const headerName = getHeaderName(allHeaders, "content-type");
|
|
1104
|
-
if (headerName) {
|
|
1105
|
-
delete settings.headers[headerName];
|
|
1106
|
-
}
|
|
1107
|
-
}
|
|
1108
|
-
}
|
|
1109
|
-
blank();
|
|
1110
|
-
break;
|
|
1111
|
-
default:
|
|
1112
|
-
if (postData.text) {
|
|
1113
|
-
settings.data = postData.text;
|
|
1114
|
-
}
|
|
1115
|
-
}
|
|
1116
|
-
const stringifiedSettings = stringifyObject9__default.default(settings, { indent: opts.indent }).replace("'[form]'", "form");
|
|
1117
|
-
push(`const settings = ${stringifiedSettings};`);
|
|
1118
|
-
blank();
|
|
1119
|
-
push("$.ajax(settings).done(function (response) {");
|
|
1120
|
-
push("console.log(response);", 1);
|
|
1121
|
-
push("});");
|
|
1122
|
-
return join();
|
|
1123
|
-
}
|
|
1124
|
-
};
|
|
1125
|
-
var xhr = {
|
|
1126
|
-
info: {
|
|
1127
|
-
key: "xhr",
|
|
1128
|
-
title: "XMLHttpRequest",
|
|
1129
|
-
link: "https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest",
|
|
1130
|
-
description: "W3C Standard API that provides scripted client functionality"
|
|
1131
|
-
},
|
|
1132
|
-
convert: ({ postData, allHeaders, method, fullUrl }, options) => {
|
|
1133
|
-
const opts = {
|
|
1134
|
-
indent: " ",
|
|
1135
|
-
cors: true,
|
|
1136
|
-
...options
|
|
1137
|
-
};
|
|
1138
|
-
const { blank, push, join } = new CodeBuilder({ indent: opts.indent });
|
|
1139
|
-
switch (postData.mimeType) {
|
|
1140
|
-
case "application/json":
|
|
1141
|
-
push(
|
|
1142
|
-
`const data = JSON.stringify(${stringifyObject9__default.default(postData.jsonObj, {
|
|
1143
|
-
indent: opts.indent
|
|
1144
|
-
})});`
|
|
1145
|
-
);
|
|
1146
|
-
blank();
|
|
1147
|
-
break;
|
|
1148
|
-
case "multipart/form-data":
|
|
1149
|
-
if (!postData.params) {
|
|
1150
|
-
break;
|
|
1151
|
-
}
|
|
1152
|
-
push("const data = new FormData();");
|
|
1153
|
-
postData.params.forEach((param) => {
|
|
1154
|
-
push(`data.append('${param.name}', '${param.value || param.fileName || ""}');`);
|
|
1155
|
-
});
|
|
1156
|
-
if (hasHeader(allHeaders, "content-type")) {
|
|
1157
|
-
if (getHeader(allHeaders, "content-type")?.includes("boundary")) {
|
|
1158
|
-
const headerName = getHeaderName(allHeaders, "content-type");
|
|
1159
|
-
if (headerName) {
|
|
1160
|
-
delete allHeaders[headerName];
|
|
1161
|
-
}
|
|
1162
|
-
}
|
|
1163
|
-
}
|
|
1164
|
-
blank();
|
|
1165
|
-
break;
|
|
1166
|
-
default:
|
|
1167
|
-
push(`const data = ${postData.text ? `'${postData.text}'` : "null"};`);
|
|
1168
|
-
blank();
|
|
1169
|
-
}
|
|
1170
|
-
push("const xhr = new XMLHttpRequest();");
|
|
1171
|
-
if (opts.cors) {
|
|
1172
|
-
push("xhr.withCredentials = true;");
|
|
1173
|
-
}
|
|
1174
|
-
blank();
|
|
1175
|
-
push("xhr.addEventListener('readystatechange', function () {");
|
|
1176
|
-
push("if (this.readyState === this.DONE) {", 1);
|
|
1177
|
-
push("console.log(this.responseText);", 2);
|
|
1178
|
-
push("}", 1);
|
|
1179
|
-
push("});");
|
|
1180
|
-
blank();
|
|
1181
|
-
push(`xhr.open('${method}', '${fullUrl}');`);
|
|
1182
|
-
Object.keys(allHeaders).forEach((key) => {
|
|
1183
|
-
push(`xhr.setRequestHeader('${key}', '${escapeForSingleQuotes(allHeaders[key])}');`);
|
|
1184
|
-
});
|
|
1185
|
-
blank();
|
|
1186
|
-
push("xhr.send(data);");
|
|
1187
|
-
return join();
|
|
1188
|
-
}
|
|
1189
|
-
};
|
|
1190
|
-
|
|
1191
|
-
// src/targets/javascript/target.ts
|
|
1192
|
-
var javascript = {
|
|
1193
|
-
info: {
|
|
1194
|
-
key: "javascript",
|
|
1195
|
-
title: "JavaScript",
|
|
1196
|
-
extname: ".js",
|
|
1197
|
-
default: "xhr"
|
|
1198
|
-
},
|
|
1199
|
-
clientsById: {
|
|
1200
|
-
xhr,
|
|
1201
|
-
axios,
|
|
1202
|
-
fetch,
|
|
1203
|
-
jquery
|
|
1204
|
-
}
|
|
1205
|
-
};
|
|
1206
|
-
|
|
1207
|
-
// src/targets/json/native/client.ts
|
|
1208
|
-
var native2 = {
|
|
1209
|
-
info: {
|
|
1210
|
-
key: "native",
|
|
1211
|
-
title: "Native JSON",
|
|
1212
|
-
link: "https://www.json.org/json-en.html",
|
|
1213
|
-
description: "A JSON represetation of any HAR payload."
|
|
1214
|
-
},
|
|
1215
|
-
convert: ({ postData }, inputOpts) => {
|
|
1216
|
-
const opts = {
|
|
1217
|
-
indent: " ",
|
|
1218
|
-
...inputOpts
|
|
1219
|
-
};
|
|
1220
|
-
let payload = "";
|
|
1221
|
-
switch (postData.mimeType) {
|
|
1222
|
-
case "application/x-www-form-urlencoded":
|
|
1223
|
-
payload = postData.paramsObj ? postData.paramsObj : postData.text;
|
|
1224
|
-
break;
|
|
1225
|
-
case "application/json":
|
|
1226
|
-
if (postData.jsonObj) {
|
|
1227
|
-
payload = postData.jsonObj;
|
|
1228
|
-
}
|
|
1229
|
-
break;
|
|
1230
|
-
case "multipart/form-data":
|
|
1231
|
-
if (!postData.params) {
|
|
1232
|
-
break;
|
|
1233
|
-
}
|
|
1234
|
-
const multipartPayload = {};
|
|
1235
|
-
postData.params.forEach((param) => {
|
|
1236
|
-
multipartPayload[param.name] = param.value;
|
|
1237
|
-
});
|
|
1238
|
-
payload = multipartPayload;
|
|
1239
|
-
break;
|
|
1240
|
-
default:
|
|
1241
|
-
if (postData.text) {
|
|
1242
|
-
payload = postData.text;
|
|
1243
|
-
}
|
|
1244
|
-
}
|
|
1245
|
-
if (typeof payload === "undefined" || payload === "") {
|
|
1246
|
-
return "No JSON body";
|
|
1247
|
-
}
|
|
1248
|
-
return JSON.stringify(payload, null, opts.indent);
|
|
1249
|
-
}
|
|
1250
|
-
};
|
|
1251
|
-
|
|
1252
|
-
// src/targets/json/target.ts
|
|
1253
|
-
var json = {
|
|
1254
|
-
info: {
|
|
1255
|
-
key: "json",
|
|
1256
|
-
title: "JSON",
|
|
1257
|
-
extname: ".json",
|
|
1258
|
-
default: "native"
|
|
1259
|
-
},
|
|
1260
|
-
clientsById: {
|
|
1261
|
-
native: native2
|
|
1262
|
-
}
|
|
1263
|
-
};
|
|
1264
|
-
|
|
1265
|
-
// src/targets/kotlin/okhttp/client.ts
|
|
1266
|
-
var okhttp2 = {
|
|
1267
|
-
info: {
|
|
1268
|
-
key: "okhttp",
|
|
1269
|
-
title: "OkHttp",
|
|
1270
|
-
link: "http://square.github.io/okhttp/",
|
|
1271
|
-
description: "An HTTP Request Client Library"
|
|
1272
|
-
},
|
|
1273
|
-
convert: ({ postData, fullUrl, method, allHeaders }, options) => {
|
|
1274
|
-
const opts = {
|
|
1275
|
-
indent: " ",
|
|
1276
|
-
...options
|
|
1277
|
-
};
|
|
1278
|
-
const { blank, join, push } = new CodeBuilder({ indent: opts.indent });
|
|
1279
|
-
const methods = ["GET", "POST", "PUT", "DELETE", "PATCH", "HEAD"];
|
|
1280
|
-
const methodsWithBody = ["POST", "PUT", "DELETE", "PATCH"];
|
|
1281
|
-
push("val client = OkHttpClient()");
|
|
1282
|
-
blank();
|
|
1283
|
-
if (postData.text) {
|
|
1284
|
-
if (postData.boundary) {
|
|
1285
|
-
push(`val mediaType = MediaType.parse("${postData.mimeType}; boundary=${postData.boundary}")`);
|
|
1286
|
-
} else {
|
|
1287
|
-
push(`val mediaType = MediaType.parse("${postData.mimeType}")`);
|
|
1288
|
-
}
|
|
1289
|
-
push(`val body = RequestBody.create(mediaType, ${JSON.stringify(postData.text)})`);
|
|
1290
|
-
}
|
|
1291
|
-
push("val request = Request.Builder()");
|
|
1292
|
-
push(`.url("${fullUrl}")`, 1);
|
|
1293
|
-
if (!methods.includes(method.toUpperCase())) {
|
|
1294
|
-
if (postData.text) {
|
|
1295
|
-
push(`.method("${method.toUpperCase()}", body)`, 1);
|
|
1296
|
-
} else {
|
|
1297
|
-
push(`.method("${method.toUpperCase()}", null)`, 1);
|
|
1298
|
-
}
|
|
1299
|
-
} else if (methodsWithBody.includes(method.toUpperCase())) {
|
|
1300
|
-
if (postData.text) {
|
|
1301
|
-
push(`.${method.toLowerCase()}(body)`, 1);
|
|
1302
|
-
} else {
|
|
1303
|
-
push(`.${method.toLowerCase()}(null)`, 1);
|
|
1304
|
-
}
|
|
1305
|
-
} else {
|
|
1306
|
-
push(`.${method.toLowerCase()}()`, 1);
|
|
1307
|
-
}
|
|
1308
|
-
Object.keys(allHeaders).forEach((key) => {
|
|
1309
|
-
push(`.addHeader("${key}", "${escapeForDoubleQuotes(allHeaders[key])}")`, 1);
|
|
1310
|
-
});
|
|
1311
|
-
push(".build()", 1);
|
|
1312
|
-
blank();
|
|
1313
|
-
push("val response = client.newCall(request).execute()");
|
|
1314
|
-
return join();
|
|
1315
|
-
}
|
|
1316
|
-
};
|
|
1317
|
-
|
|
1318
|
-
// src/targets/kotlin/target.ts
|
|
1319
|
-
var kotlin = {
|
|
1320
|
-
info: {
|
|
1321
|
-
key: "kotlin",
|
|
1322
|
-
title: "Kotlin",
|
|
1323
|
-
extname: ".kt",
|
|
1324
|
-
default: "okhttp"
|
|
1325
|
-
},
|
|
1326
|
-
clientsById: {
|
|
1327
|
-
okhttp: okhttp2
|
|
1328
|
-
}
|
|
1329
|
-
};
|
|
1330
|
-
var axios2 = {
|
|
1331
|
-
info: {
|
|
1332
|
-
key: "axios",
|
|
1333
|
-
title: "Axios",
|
|
1334
|
-
link: "https://github.com/axios/axios",
|
|
1335
|
-
description: "Promise based HTTP client for the browser and node.js"
|
|
1336
|
-
},
|
|
1337
|
-
convert: ({ method, fullUrl, allHeaders, postData }, options) => {
|
|
1338
|
-
const opts = {
|
|
1339
|
-
indent: " ",
|
|
1340
|
-
...options
|
|
1341
|
-
};
|
|
1342
|
-
const { blank, join, push, addPostProcessor } = new CodeBuilder({ indent: opts.indent });
|
|
1343
|
-
push("const axios = require('axios');");
|
|
1344
|
-
const reqOpts = {
|
|
1345
|
-
method,
|
|
1346
|
-
url: fullUrl
|
|
1347
|
-
};
|
|
1348
|
-
if (Object.keys(allHeaders).length) {
|
|
1349
|
-
reqOpts.headers = allHeaders;
|
|
1350
|
-
}
|
|
1351
|
-
switch (postData.mimeType) {
|
|
1352
|
-
case "application/x-www-form-urlencoded":
|
|
1353
|
-
if (postData.params) {
|
|
1354
|
-
push("const { URLSearchParams } = require('url');");
|
|
1355
|
-
blank();
|
|
1356
|
-
push("const encodedParams = new URLSearchParams();");
|
|
1357
|
-
postData.params.forEach((param) => {
|
|
1358
|
-
push(`encodedParams.set('${param.name}', '${param.value}');`);
|
|
1359
|
-
});
|
|
1360
|
-
blank();
|
|
1361
|
-
reqOpts.data = "encodedParams,";
|
|
1362
|
-
addPostProcessor((code) => code.replace(/'encodedParams,'/, "encodedParams,"));
|
|
1363
|
-
}
|
|
1364
|
-
break;
|
|
1365
|
-
case "application/json":
|
|
1366
|
-
blank();
|
|
1367
|
-
if (postData.jsonObj) {
|
|
1368
|
-
reqOpts.data = postData.jsonObj;
|
|
1369
|
-
}
|
|
1370
|
-
break;
|
|
1371
|
-
default:
|
|
1372
|
-
blank();
|
|
1373
|
-
if (postData.text) {
|
|
1374
|
-
reqOpts.data = postData.text;
|
|
1375
|
-
}
|
|
1376
|
-
}
|
|
1377
|
-
const stringifiedOptions = stringifyObject9__default.default(reqOpts, { indent: " ", inlineCharacterLimit: 80 });
|
|
1378
|
-
push(`const options = ${stringifiedOptions};`);
|
|
1379
|
-
blank();
|
|
1380
|
-
push("axios");
|
|
1381
|
-
push(".request(options)", 1);
|
|
1382
|
-
push(".then(function (response) {", 1);
|
|
1383
|
-
push("console.log(response.data);", 2);
|
|
1384
|
-
push("})", 1);
|
|
1385
|
-
push(".catch(function (error) {", 1);
|
|
1386
|
-
push("console.error(error);", 2);
|
|
1387
|
-
push("});", 1);
|
|
1388
|
-
return join();
|
|
1389
|
-
}
|
|
1390
|
-
};
|
|
1391
|
-
var fetch2 = {
|
|
1392
|
-
info: {
|
|
1393
|
-
key: "fetch",
|
|
1394
|
-
title: "Fetch",
|
|
1395
|
-
link: "https://github.com/bitinn/node-fetch",
|
|
1396
|
-
description: "Simplified HTTP node-fetch client"
|
|
1397
|
-
},
|
|
1398
|
-
convert: ({ method, fullUrl, postData, headersObj, cookies }, options) => {
|
|
1399
|
-
const opts = {
|
|
1400
|
-
indent: " ",
|
|
1401
|
-
...options
|
|
1402
|
-
};
|
|
1403
|
-
let includeFS = false;
|
|
1404
|
-
const { blank, push, join, unshift } = new CodeBuilder({ indent: opts.indent });
|
|
1405
|
-
push("const fetch = require('node-fetch');");
|
|
1406
|
-
const url = fullUrl;
|
|
1407
|
-
const reqOpts = {
|
|
1408
|
-
method
|
|
1409
|
-
};
|
|
1410
|
-
if (Object.keys(headersObj).length) {
|
|
1411
|
-
reqOpts.headers = headersObj;
|
|
1412
|
-
}
|
|
1413
|
-
switch (postData.mimeType) {
|
|
1414
|
-
case "application/x-www-form-urlencoded":
|
|
1415
|
-
unshift("const { URLSearchParams } = require('url');");
|
|
1416
|
-
push("const encodedParams = new URLSearchParams();");
|
|
1417
|
-
blank();
|
|
1418
|
-
postData.params?.forEach((param) => {
|
|
1419
|
-
push(`encodedParams.set('${param.name}', '${param.value}');`);
|
|
1420
|
-
});
|
|
1421
|
-
reqOpts.body = "encodedParams";
|
|
1422
|
-
break;
|
|
1423
|
-
case "application/json":
|
|
1424
|
-
if (postData.jsonObj) {
|
|
1425
|
-
reqOpts.body = postData.jsonObj;
|
|
1426
|
-
}
|
|
1427
|
-
break;
|
|
1428
|
-
case "multipart/form-data":
|
|
1429
|
-
if (!postData.params) {
|
|
1430
|
-
break;
|
|
1431
|
-
}
|
|
1432
|
-
const contentTypeHeader = getHeaderName(headersObj, "content-type");
|
|
1433
|
-
if (contentTypeHeader) {
|
|
1434
|
-
delete headersObj[contentTypeHeader];
|
|
1435
|
-
}
|
|
1436
|
-
unshift("const FormData = require('form-data');");
|
|
1437
|
-
push("const formData = new FormData();");
|
|
1438
|
-
blank();
|
|
1439
|
-
postData.params.forEach((param) => {
|
|
1440
|
-
if (!param.fileName && !param.fileName && !param.contentType) {
|
|
1441
|
-
push(`formData.append('${param.name}', '${param.value}');`);
|
|
1442
|
-
return;
|
|
1443
|
-
}
|
|
1444
|
-
if (param.fileName) {
|
|
1445
|
-
includeFS = true;
|
|
1446
|
-
push(`formData.append('${param.name}', fs.createReadStream('${param.fileName}'));`);
|
|
1447
|
-
}
|
|
1448
|
-
});
|
|
1449
|
-
break;
|
|
1450
|
-
default:
|
|
1451
|
-
if (postData.text) {
|
|
1452
|
-
reqOpts.body = postData.text;
|
|
1453
|
-
}
|
|
1454
|
-
}
|
|
1455
|
-
if (cookies.length) {
|
|
1456
|
-
const cookiesString = cookies.map(({ name, value }) => `${encodeURIComponent(name)}=${encodeURIComponent(value)}`).join("; ");
|
|
1457
|
-
if (reqOpts.headers) {
|
|
1458
|
-
reqOpts.headers.cookie = cookiesString;
|
|
1459
|
-
} else {
|
|
1460
|
-
reqOpts.headers = {};
|
|
1461
|
-
reqOpts.headers.cookie = cookiesString;
|
|
1462
|
-
}
|
|
1463
|
-
}
|
|
1464
|
-
blank();
|
|
1465
|
-
push(`const url = '${url}';`);
|
|
1466
|
-
if (reqOpts.headers && !Object.keys(reqOpts.headers).length) {
|
|
1467
|
-
delete reqOpts.headers;
|
|
1468
|
-
}
|
|
1469
|
-
const stringifiedOptions = stringifyObject9__default.default(reqOpts, {
|
|
1470
|
-
indent: " ",
|
|
1471
|
-
inlineCharacterLimit: 80,
|
|
1472
|
-
// The Fetch API body only accepts string parameters, but stringified JSON can be difficult to
|
|
1473
|
-
// read, so we keep the object as a literal and use this transform function to wrap the literal
|
|
1474
|
-
// in a `JSON.stringify` call.
|
|
1475
|
-
transform: (object, property, originalResult) => {
|
|
1476
|
-
if (property === "body" && postData.mimeType === "application/json") {
|
|
1477
|
-
return `JSON.stringify(${originalResult})`;
|
|
1478
|
-
}
|
|
1479
|
-
return originalResult;
|
|
1480
|
-
}
|
|
1481
|
-
});
|
|
1482
|
-
push(`const options = ${stringifiedOptions};`);
|
|
1483
|
-
blank();
|
|
1484
|
-
if (includeFS) {
|
|
1485
|
-
unshift("const fs = require('fs');");
|
|
1486
|
-
}
|
|
1487
|
-
if (postData.params && postData.mimeType === "multipart/form-data") {
|
|
1488
|
-
push("options.body = formData;");
|
|
1489
|
-
blank();
|
|
1490
|
-
}
|
|
1491
|
-
push("fetch(url, options)");
|
|
1492
|
-
push(".then(res => res.json())", 1);
|
|
1493
|
-
push(".then(json => console.log(json))", 1);
|
|
1494
|
-
push(".catch(err => console.error('error:' + err));", 1);
|
|
1495
|
-
return join().replace(/'encodedParams'/, "encodedParams").replace(/"fs\.createReadStream\(\\"(.+)\\"\)"/, 'fs.createReadStream("$1")');
|
|
1496
|
-
}
|
|
1497
|
-
};
|
|
1498
|
-
var native3 = {
|
|
1499
|
-
info: {
|
|
1500
|
-
key: "native",
|
|
1501
|
-
title: "HTTP",
|
|
1502
|
-
link: "http://nodejs.org/api/http.html#http_http_request_options_callback",
|
|
1503
|
-
description: "Node.js native HTTP interface"
|
|
1504
|
-
},
|
|
1505
|
-
convert: ({ uriObj, method, allHeaders, postData }, options = {}) => {
|
|
1506
|
-
const { indent = " " } = options;
|
|
1507
|
-
const { blank, join, push, unshift } = new CodeBuilder({ indent });
|
|
1508
|
-
const reqOpts = {
|
|
1509
|
-
method,
|
|
1510
|
-
hostname: uriObj.hostname,
|
|
1511
|
-
port: uriObj.port,
|
|
1512
|
-
path: uriObj.path,
|
|
1513
|
-
headers: allHeaders
|
|
1514
|
-
};
|
|
1515
|
-
push(`const http = require('${uriObj.protocol?.replace(":", "")}');`);
|
|
1516
|
-
blank();
|
|
1517
|
-
push(`const options = ${stringifyObject9__default.default(reqOpts, { indent })};`);
|
|
1518
|
-
blank();
|
|
1519
|
-
push("const req = http.request(options, function (res) {");
|
|
1520
|
-
push("const chunks = [];", 1);
|
|
1521
|
-
blank();
|
|
1522
|
-
push("res.on('data', function (chunk) {", 1);
|
|
1523
|
-
push("chunks.push(chunk);", 2);
|
|
1524
|
-
push("});", 1);
|
|
1525
|
-
blank();
|
|
1526
|
-
push("res.on('end', function () {", 1);
|
|
1527
|
-
push("const body = Buffer.concat(chunks);", 2);
|
|
1528
|
-
push("console.log(body.toString());", 2);
|
|
1529
|
-
push("});", 1);
|
|
1530
|
-
push("});");
|
|
1531
|
-
blank();
|
|
1532
|
-
switch (postData.mimeType) {
|
|
1533
|
-
case "application/x-www-form-urlencoded":
|
|
1534
|
-
if (postData.paramsObj) {
|
|
1535
|
-
unshift("const qs = require('querystring');");
|
|
1536
|
-
push(
|
|
1537
|
-
`req.write(qs.stringify(${stringifyObject9__default.default(postData.paramsObj, {
|
|
1538
|
-
indent: " ",
|
|
1539
|
-
inlineCharacterLimit: 80
|
|
1540
|
-
})}));`
|
|
1541
|
-
);
|
|
1542
|
-
}
|
|
1543
|
-
break;
|
|
1544
|
-
case "application/json":
|
|
1545
|
-
if (postData.jsonObj) {
|
|
1546
|
-
push(
|
|
1547
|
-
`req.write(JSON.stringify(${stringifyObject9__default.default(postData.jsonObj, {
|
|
1548
|
-
indent: " ",
|
|
1549
|
-
inlineCharacterLimit: 80
|
|
1550
|
-
})}));`
|
|
1551
|
-
);
|
|
1552
|
-
}
|
|
1553
|
-
break;
|
|
1554
|
-
default:
|
|
1555
|
-
if (postData.text) {
|
|
1556
|
-
push(`req.write(${stringifyObject9__default.default(postData.text, { indent })});`);
|
|
1557
|
-
}
|
|
1558
|
-
}
|
|
1559
|
-
push("req.end();");
|
|
1560
|
-
return join();
|
|
1561
|
-
}
|
|
1562
|
-
};
|
|
1563
|
-
var request = {
|
|
1564
|
-
info: {
|
|
1565
|
-
key: "request",
|
|
1566
|
-
title: "Request",
|
|
1567
|
-
link: "https://github.com/request/request",
|
|
1568
|
-
description: "Simplified HTTP request client"
|
|
1569
|
-
},
|
|
1570
|
-
convert: ({ method, url, fullUrl, postData, headersObj, cookies }, options) => {
|
|
1571
|
-
const opts = {
|
|
1572
|
-
indent: " ",
|
|
1573
|
-
...options
|
|
1574
|
-
};
|
|
1575
|
-
let includeFS = false;
|
|
1576
|
-
const { push, blank, join, unshift, addPostProcessor } = new CodeBuilder({ indent: opts.indent });
|
|
1577
|
-
push("const request = require('request');");
|
|
1578
|
-
blank();
|
|
1579
|
-
const reqOpts = {
|
|
1580
|
-
method,
|
|
1581
|
-
url: fullUrl
|
|
1582
|
-
};
|
|
1583
|
-
if (Object.keys(headersObj).length) {
|
|
1584
|
-
reqOpts.headers = headersObj;
|
|
1585
|
-
}
|
|
1586
|
-
switch (postData.mimeType) {
|
|
1587
|
-
case "application/x-www-form-urlencoded":
|
|
1588
|
-
reqOpts.form = postData.paramsObj;
|
|
1589
|
-
break;
|
|
1590
|
-
case "application/json":
|
|
1591
|
-
if (postData.jsonObj) {
|
|
1592
|
-
reqOpts.body = postData.jsonObj;
|
|
1593
|
-
reqOpts.json = true;
|
|
1594
|
-
}
|
|
1595
|
-
break;
|
|
1596
|
-
case "multipart/form-data":
|
|
1597
|
-
if (!postData.params) {
|
|
1598
|
-
break;
|
|
1599
|
-
}
|
|
1600
|
-
reqOpts.formData = {};
|
|
1601
|
-
postData.params.forEach((param) => {
|
|
1602
|
-
if (!param.fileName && !param.fileName && !param.contentType) {
|
|
1603
|
-
reqOpts.formData[param.name] = param.value;
|
|
1604
|
-
return;
|
|
1605
|
-
}
|
|
1606
|
-
let attachment = {};
|
|
1607
|
-
if (param.fileName) {
|
|
1608
|
-
includeFS = true;
|
|
1609
|
-
attachment = {
|
|
1610
|
-
value: `fs.createReadStream(${param.fileName})`,
|
|
1611
|
-
options: {
|
|
1612
|
-
filename: param.fileName,
|
|
1613
|
-
contentType: param.contentType ? param.contentType : null
|
|
1614
|
-
}
|
|
1615
|
-
};
|
|
1616
|
-
} else if (param.value) {
|
|
1617
|
-
attachment.value = param.value;
|
|
1618
|
-
}
|
|
1619
|
-
reqOpts.formData[param.name] = attachment;
|
|
1620
|
-
});
|
|
1621
|
-
addPostProcessor((code) => code.replace(/'fs\.createReadStream\((.*)\)'/, "fs.createReadStream('$1')"));
|
|
1622
|
-
break;
|
|
1623
|
-
default:
|
|
1624
|
-
if (postData.text) {
|
|
1625
|
-
reqOpts.body = postData.text;
|
|
1626
|
-
}
|
|
1627
|
-
}
|
|
1628
|
-
if (cookies.length) {
|
|
1629
|
-
reqOpts.jar = "JAR";
|
|
1630
|
-
push("const jar = request.jar();");
|
|
1631
|
-
cookies.forEach(({ name, value }) => {
|
|
1632
|
-
push(`jar.setCookie(request.cookie('${encodeURIComponent(name)}=${encodeURIComponent(value)}'), '${url}');`);
|
|
1633
|
-
});
|
|
1634
|
-
blank();
|
|
1635
|
-
addPostProcessor((code) => code.replace(/'JAR'/, "jar"));
|
|
1636
|
-
}
|
|
1637
|
-
if (includeFS) {
|
|
1638
|
-
unshift("const fs = require('fs');");
|
|
1639
|
-
}
|
|
1640
|
-
push(`const options = ${stringifyObject9__default.default(reqOpts, { indent: " ", inlineCharacterLimit: 80 })};`);
|
|
1641
|
-
blank();
|
|
1642
|
-
push("request(options, function (error, response, body) {");
|
|
1643
|
-
push("if (error) throw new Error(error);", 1);
|
|
1644
|
-
blank();
|
|
1645
|
-
push("console.log(body);", 1);
|
|
1646
|
-
push("});");
|
|
1647
|
-
return join();
|
|
1648
|
-
}
|
|
1649
|
-
};
|
|
1650
|
-
var unirest2 = {
|
|
1651
|
-
info: {
|
|
1652
|
-
key: "unirest",
|
|
1653
|
-
title: "Unirest",
|
|
1654
|
-
link: "http://unirest.io/nodejs.html",
|
|
1655
|
-
description: "Lightweight HTTP Request Client Library"
|
|
1656
|
-
},
|
|
1657
|
-
convert: ({ method, url, cookies, queryObj, postData, headersObj }, options) => {
|
|
1658
|
-
const opts = {
|
|
1659
|
-
indent: " ",
|
|
1660
|
-
...options
|
|
1661
|
-
};
|
|
1662
|
-
let includeFS = false;
|
|
1663
|
-
const { addPostProcessor, blank, join, push, unshift } = new CodeBuilder({
|
|
1664
|
-
indent: opts.indent
|
|
1665
|
-
});
|
|
1666
|
-
push("const unirest = require('unirest');");
|
|
1667
|
-
blank();
|
|
1668
|
-
push(`const req = unirest('${method}', '${url}');`);
|
|
1669
|
-
blank();
|
|
1670
|
-
if (cookies.length) {
|
|
1671
|
-
push("const CookieJar = unirest.jar();");
|
|
1672
|
-
cookies.forEach((cookie) => {
|
|
1673
|
-
push(`CookieJar.add('${encodeURIComponent(cookie.name)}=${encodeURIComponent(cookie.value)}', '${url}');`);
|
|
1674
|
-
});
|
|
1675
|
-
push("req.jar(CookieJar);");
|
|
1676
|
-
blank();
|
|
1677
|
-
}
|
|
1678
|
-
if (Object.keys(queryObj).length) {
|
|
1679
|
-
push(`req.query(${stringifyObject9__default.default(queryObj, { indent: opts.indent })});`);
|
|
1680
|
-
blank();
|
|
1681
|
-
}
|
|
1682
|
-
if (Object.keys(headersObj).length) {
|
|
1683
|
-
push(`req.headers(${stringifyObject9__default.default(headersObj, { indent: opts.indent })});`);
|
|
1684
|
-
blank();
|
|
1685
|
-
}
|
|
1686
|
-
switch (postData.mimeType) {
|
|
1687
|
-
case "application/x-www-form-urlencoded":
|
|
1688
|
-
if (postData.paramsObj) {
|
|
1689
|
-
push(`req.form(${stringifyObject9__default.default(postData.paramsObj, { indent: opts.indent })});`);
|
|
1690
|
-
blank();
|
|
1691
|
-
}
|
|
1692
|
-
break;
|
|
1693
|
-
case "application/json":
|
|
1694
|
-
if (postData.jsonObj) {
|
|
1695
|
-
push("req.type('json');");
|
|
1696
|
-
push(`req.send(${stringifyObject9__default.default(postData.jsonObj, { indent: opts.indent })});`);
|
|
1697
|
-
blank();
|
|
1698
|
-
}
|
|
1699
|
-
break;
|
|
1700
|
-
case "multipart/form-data": {
|
|
1701
|
-
if (!postData.params) {
|
|
1702
|
-
break;
|
|
1703
|
-
}
|
|
1704
|
-
const multipart = [];
|
|
1705
|
-
postData.params.forEach((param) => {
|
|
1706
|
-
const part = {};
|
|
1707
|
-
if (param.fileName && !param.value) {
|
|
1708
|
-
includeFS = true;
|
|
1709
|
-
part.body = `fs.createReadStream('${param.fileName}')`;
|
|
1710
|
-
addPostProcessor((code) => code.replace(/'fs\.createReadStream\(\\'(.+)\\'\)'/, "fs.createReadStream('$1')"));
|
|
1711
|
-
} else if (param.value) {
|
|
1712
|
-
part.body = param.value;
|
|
1713
|
-
}
|
|
1714
|
-
if (part.body) {
|
|
1715
|
-
if (param.contentType) {
|
|
1716
|
-
part["content-type"] = param.contentType;
|
|
1717
|
-
}
|
|
1718
|
-
multipart.push(part);
|
|
1719
|
-
}
|
|
1720
|
-
});
|
|
1721
|
-
push(`req.multipart(${stringifyObject9__default.default(multipart, { indent: opts.indent })});`);
|
|
1722
|
-
blank();
|
|
1723
|
-
break;
|
|
1724
|
-
}
|
|
1725
|
-
default:
|
|
1726
|
-
if (postData.text) {
|
|
1727
|
-
push(`req.send(${stringifyObject9__default.default(postData.text, { indent: opts.indent })});`);
|
|
1728
|
-
blank();
|
|
1729
|
-
}
|
|
1730
|
-
}
|
|
1731
|
-
if (includeFS) {
|
|
1732
|
-
unshift("const fs = require('fs');");
|
|
1733
|
-
}
|
|
1734
|
-
push("req.end(function (res) {");
|
|
1735
|
-
push("if (res.error) throw new Error(res.error);", 1);
|
|
1736
|
-
blank();
|
|
1737
|
-
push("console.log(res.body);", 1);
|
|
1738
|
-
push("});");
|
|
1739
|
-
return join();
|
|
1740
|
-
}
|
|
1741
|
-
};
|
|
1742
|
-
|
|
1743
|
-
// src/targets/node/target.ts
|
|
1744
|
-
var node = {
|
|
1745
|
-
info: {
|
|
1746
|
-
key: "node",
|
|
1747
|
-
title: "Node.js",
|
|
1748
|
-
extname: ".js",
|
|
1749
|
-
default: "native",
|
|
1750
|
-
cli: "node %s"
|
|
1751
|
-
},
|
|
1752
|
-
clientsById: {
|
|
1753
|
-
native: native3,
|
|
1754
|
-
request,
|
|
1755
|
-
unirest: unirest2,
|
|
1756
|
-
axios: axios2,
|
|
1757
|
-
fetch: fetch2
|
|
1758
|
-
}
|
|
1759
|
-
};
|
|
1760
|
-
|
|
1761
|
-
// src/targets/objc/helpers.ts
|
|
1762
|
-
var nsDeclaration = (nsClass, name, parameters, indent) => {
|
|
1763
|
-
const opening = `${nsClass} *${name} = `;
|
|
1764
|
-
const literal = literalRepresentation(parameters, indent ? opening.length : void 0);
|
|
1765
|
-
return `${opening}${literal};`;
|
|
1766
|
-
};
|
|
1767
|
-
var literalRepresentation = (value, indentation) => {
|
|
1768
|
-
const join = indentation === void 0 ? ", " : `,
|
|
1769
|
-
${" ".repeat(indentation)}`;
|
|
1770
|
-
switch (Object.prototype.toString.call(value)) {
|
|
1771
|
-
case "[object Number]":
|
|
1772
|
-
return `@${value}`;
|
|
1773
|
-
case "[object Array]": {
|
|
1774
|
-
const valuesRepresentation = value.map((val) => literalRepresentation(val));
|
|
1775
|
-
return `@[ ${valuesRepresentation.join(join)} ]`;
|
|
1776
|
-
}
|
|
1777
|
-
case "[object Object]": {
|
|
1778
|
-
const keyValuePairs = [];
|
|
1779
|
-
Object.keys(value).forEach((key) => {
|
|
1780
|
-
keyValuePairs.push(`@"${key}": ${literalRepresentation(value[key])}`);
|
|
1781
|
-
});
|
|
1782
|
-
return `@{ ${keyValuePairs.join(join)} }`;
|
|
1783
|
-
}
|
|
1784
|
-
case "[object Boolean]":
|
|
1785
|
-
return value ? "@YES" : "@NO";
|
|
1786
|
-
default:
|
|
1787
|
-
if (value === null || value === void 0) {
|
|
1788
|
-
return "";
|
|
1789
|
-
}
|
|
1790
|
-
return `@"${value.toString().replace(/"/g, '\\"')}"`;
|
|
1791
|
-
}
|
|
1792
|
-
};
|
|
1793
|
-
|
|
1794
|
-
// src/targets/objc/nsurlsession/client.ts
|
|
1795
|
-
var nsurlsession = {
|
|
1796
|
-
info: {
|
|
1797
|
-
key: "nsurlsession",
|
|
1798
|
-
title: "NSURLSession",
|
|
1799
|
-
link: "https://developer.apple.com/library/mac/documentation/Foundation/Reference/NSURLSession_class/index.html",
|
|
1800
|
-
description: "Foundation's NSURLSession request"
|
|
1801
|
-
},
|
|
1802
|
-
convert: ({ allHeaders, postData, method, fullUrl }, options) => {
|
|
1803
|
-
const opts = {
|
|
1804
|
-
indent: " ",
|
|
1805
|
-
pretty: true,
|
|
1806
|
-
timeout: 10,
|
|
1807
|
-
...options
|
|
1808
|
-
};
|
|
1809
|
-
const { push, join, blank } = new CodeBuilder({ indent: opts.indent });
|
|
1810
|
-
const req = {
|
|
1811
|
-
hasHeaders: false,
|
|
1812
|
-
hasBody: false
|
|
1813
|
-
};
|
|
1814
|
-
push("#import <Foundation/Foundation.h>");
|
|
1815
|
-
if (Object.keys(allHeaders).length) {
|
|
1816
|
-
req.hasHeaders = true;
|
|
1817
|
-
blank();
|
|
1818
|
-
push(nsDeclaration("NSDictionary", "headers", allHeaders, opts.pretty));
|
|
1819
|
-
}
|
|
1820
|
-
if (postData.text || postData.jsonObj || postData.params) {
|
|
1821
|
-
req.hasBody = true;
|
|
1822
|
-
switch (postData.mimeType) {
|
|
1823
|
-
case "application/x-www-form-urlencoded":
|
|
1824
|
-
if (postData.params?.length) {
|
|
1825
|
-
blank();
|
|
1826
|
-
const [head, ...tail] = postData.params;
|
|
1827
|
-
push(
|
|
1828
|
-
`NSMutableData *postData = [[NSMutableData alloc] initWithData:[@"${head.name}=${head.value}" dataUsingEncoding:NSUTF8StringEncoding]];`
|
|
1829
|
-
);
|
|
1830
|
-
tail.forEach(({ name, value }) => {
|
|
1831
|
-
push(`[postData appendData:[@"&${name}=${value}" dataUsingEncoding:NSUTF8StringEncoding]];`);
|
|
1832
|
-
});
|
|
1833
|
-
} else {
|
|
1834
|
-
req.hasBody = false;
|
|
1835
|
-
}
|
|
1836
|
-
break;
|
|
1837
|
-
case "application/json":
|
|
1838
|
-
if (postData.jsonObj) {
|
|
1839
|
-
push(nsDeclaration("NSDictionary", "parameters", postData.jsonObj, opts.pretty));
|
|
1840
|
-
blank();
|
|
1841
|
-
push("NSData *postData = [NSJSONSerialization dataWithJSONObject:parameters options:0 error:nil];");
|
|
1842
|
-
}
|
|
1843
|
-
break;
|
|
1844
|
-
case "multipart/form-data":
|
|
1845
|
-
push(nsDeclaration("NSArray", "parameters", postData.params || [], opts.pretty));
|
|
1846
|
-
push(`NSString *boundary = @"${postData.boundary}";`);
|
|
1847
|
-
blank();
|
|
1848
|
-
push("NSError *error;");
|
|
1849
|
-
push("NSMutableString *body = [NSMutableString string];");
|
|
1850
|
-
push("for (NSDictionary *param in parameters) {");
|
|
1851
|
-
push('[body appendFormat:@"--%@\\r\\n", boundary];', 1);
|
|
1852
|
-
push('if (param[@"fileName"]) {', 1);
|
|
1853
|
-
push(
|
|
1854
|
-
'[body appendFormat:@"Content-Disposition:form-data; name=\\"%@\\"; filename=\\"%@\\"\\r\\n", param[@"name"], param[@"fileName"]];',
|
|
1855
|
-
2
|
|
1856
|
-
);
|
|
1857
|
-
push('[body appendFormat:@"Content-Type: %@\\r\\n\\r\\n", param[@"contentType"]];', 2);
|
|
1858
|
-
push(
|
|
1859
|
-
'[body appendFormat:@"%@", [NSString stringWithContentsOfFile:param[@"fileName"] encoding:NSUTF8StringEncoding error:&error]];',
|
|
1860
|
-
2
|
|
1861
|
-
);
|
|
1862
|
-
push("if (error) {", 2);
|
|
1863
|
-
push('NSLog(@"%@", error);', 3);
|
|
1864
|
-
push("}", 2);
|
|
1865
|
-
push("} else {", 1);
|
|
1866
|
-
push('[body appendFormat:@"Content-Disposition:form-data; name=\\"%@\\"\\r\\n\\r\\n", param[@"name"]];', 2);
|
|
1867
|
-
push('[body appendFormat:@"%@", param[@"value"]];', 2);
|
|
1868
|
-
push("}", 1);
|
|
1869
|
-
push("}");
|
|
1870
|
-
push('[body appendFormat:@"\\r\\n--%@--\\r\\n", boundary];');
|
|
1871
|
-
push("NSData *postData = [body dataUsingEncoding:NSUTF8StringEncoding];");
|
|
1872
|
-
break;
|
|
1873
|
-
default:
|
|
1874
|
-
blank();
|
|
1875
|
-
push(
|
|
1876
|
-
`NSData *postData = [[NSData alloc] initWithData:[@"${postData.text}" dataUsingEncoding:NSUTF8StringEncoding]];`
|
|
1877
|
-
);
|
|
1878
|
-
}
|
|
1879
|
-
}
|
|
1880
|
-
blank();
|
|
1881
|
-
push(`NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"${fullUrl}"]`);
|
|
1882
|
-
push(" cachePolicy:NSURLRequestUseProtocolCachePolicy");
|
|
1883
|
-
push(` timeoutInterval:${opts.timeout.toFixed(1)}];`);
|
|
1884
|
-
push(`[request setHTTPMethod:@"${method}"];`);
|
|
1885
|
-
if (req.hasHeaders) {
|
|
1886
|
-
push("[request setAllHTTPHeaderFields:headers];");
|
|
1887
|
-
}
|
|
1888
|
-
if (req.hasBody) {
|
|
1889
|
-
push("[request setHTTPBody:postData];");
|
|
1890
|
-
}
|
|
1891
|
-
blank();
|
|
1892
|
-
push("NSURLSession *session = [NSURLSession sharedSession];");
|
|
1893
|
-
push("NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request");
|
|
1894
|
-
push(
|
|
1895
|
-
" completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {"
|
|
1896
|
-
);
|
|
1897
|
-
push(" if (error) {", 1);
|
|
1898
|
-
push(' NSLog(@"%@", error);', 2);
|
|
1899
|
-
push(" } else {", 1);
|
|
1900
|
-
push(
|
|
1901
|
-
" NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;",
|
|
1902
|
-
2
|
|
1903
|
-
);
|
|
1904
|
-
push(' NSLog(@"%@", httpResponse);', 2);
|
|
1905
|
-
push(" }", 1);
|
|
1906
|
-
push(" }];");
|
|
1907
|
-
push("[dataTask resume];");
|
|
1908
|
-
return join();
|
|
1909
|
-
}
|
|
1910
|
-
};
|
|
1911
|
-
|
|
1912
|
-
// src/targets/objc/target.ts
|
|
1913
|
-
var objc = {
|
|
1914
|
-
info: {
|
|
1915
|
-
key: "objc",
|
|
1916
|
-
title: "Objective-C",
|
|
1917
|
-
extname: ".m",
|
|
1918
|
-
default: "nsurlsession"
|
|
1919
|
-
},
|
|
1920
|
-
clientsById: {
|
|
1921
|
-
nsurlsession
|
|
1922
|
-
}
|
|
1923
|
-
};
|
|
1924
|
-
|
|
1925
|
-
// src/targets/ocaml/cohttp/client.ts
|
|
1926
|
-
var cohttp = {
|
|
1927
|
-
info: {
|
|
1928
|
-
key: "cohttp",
|
|
1929
|
-
title: "CoHTTP",
|
|
1930
|
-
link: "https://github.com/mirage/ocaml-cohttp",
|
|
1931
|
-
description: "Cohttp is a very lightweight HTTP server using Lwt or Async for OCaml"
|
|
1932
|
-
},
|
|
1933
|
-
convert: ({ fullUrl, allHeaders, postData, method }, options) => {
|
|
1934
|
-
const opts = {
|
|
1935
|
-
indent: " ",
|
|
1936
|
-
...options
|
|
1937
|
-
};
|
|
1938
|
-
const methods = ["get", "post", "head", "delete", "patch", "put", "options"];
|
|
1939
|
-
const { push, blank, join } = new CodeBuilder({ indent: opts.indent });
|
|
1940
|
-
push("open Cohttp_lwt_unix");
|
|
1941
|
-
push("open Cohttp");
|
|
1942
|
-
push("open Lwt");
|
|
1943
|
-
blank();
|
|
1944
|
-
push(`let uri = Uri.of_string "${fullUrl}" in`);
|
|
1945
|
-
const headers = Object.keys(allHeaders);
|
|
1946
|
-
if (headers.length === 1) {
|
|
1947
|
-
push(
|
|
1948
|
-
`let headers = Header.add (Header.init ()) "${headers[0]}" "${escapeForDoubleQuotes(
|
|
1949
|
-
allHeaders[headers[0]]
|
|
1950
|
-
)}" in`
|
|
1951
|
-
);
|
|
1952
|
-
} else if (headers.length > 1) {
|
|
1953
|
-
push("let headers = Header.add_list (Header.init ()) [");
|
|
1954
|
-
headers.forEach((key) => {
|
|
1955
|
-
push(`("${key}", "${escapeForDoubleQuotes(allHeaders[key])}");`, 1);
|
|
1956
|
-
});
|
|
1957
|
-
push("] in");
|
|
1958
|
-
}
|
|
1959
|
-
if (postData.text) {
|
|
1960
|
-
push(`let body = Cohttp_lwt_body.of_string ${JSON.stringify(postData.text)} in`);
|
|
1961
|
-
}
|
|
1962
|
-
blank();
|
|
1963
|
-
const h = headers.length ? "~headers " : "";
|
|
1964
|
-
const b = postData.text ? "~body " : "";
|
|
1965
|
-
const m = methods.includes(method.toLowerCase()) ? `\`${method.toUpperCase()}` : `(Code.method_of_string "${method}")`;
|
|
1966
|
-
push(`Client.call ${h}${b}${m} uri`);
|
|
1967
|
-
push(">>= fun (res, body_stream) ->");
|
|
1968
|
-
push("(* Do stuff with the result *)", 1);
|
|
1969
|
-
return join();
|
|
1970
|
-
}
|
|
1971
|
-
};
|
|
1972
|
-
|
|
1973
|
-
// src/targets/ocaml/target.ts
|
|
1974
|
-
var ocaml = {
|
|
1975
|
-
info: {
|
|
1976
|
-
key: "ocaml",
|
|
1977
|
-
title: "OCaml",
|
|
1978
|
-
extname: ".ml",
|
|
1979
|
-
default: "cohttp"
|
|
1980
|
-
},
|
|
1981
|
-
clientsById: {
|
|
1982
|
-
cohttp
|
|
1983
|
-
}
|
|
1984
|
-
};
|
|
1985
|
-
|
|
1986
|
-
// src/targets/php/helpers.ts
|
|
1987
|
-
var convertType = (obj, indent, lastIndent) => {
|
|
1988
|
-
lastIndent = lastIndent || "";
|
|
1989
|
-
indent = indent || "";
|
|
1990
|
-
switch (Object.prototype.toString.call(obj)) {
|
|
1991
|
-
case "[object Boolean]":
|
|
1992
|
-
return obj;
|
|
1993
|
-
case "[object Null]":
|
|
1994
|
-
return "null";
|
|
1995
|
-
case "[object Undefined]":
|
|
1996
|
-
return "null";
|
|
1997
|
-
case "[object String]":
|
|
1998
|
-
return `'${escapeString(obj, { delimiter: "'", escapeNewlines: false })}'`;
|
|
1999
|
-
case "[object Number]":
|
|
2000
|
-
return obj.toString();
|
|
2001
|
-
case "[object Array]": {
|
|
2002
|
-
const contents = obj.map((item) => convertType(item, `${indent}${indent}`, indent)).join(`,
|
|
2003
|
-
${indent}`);
|
|
2004
|
-
return `[
|
|
2005
|
-
${indent}${contents}
|
|
2006
|
-
${lastIndent}]`;
|
|
2007
|
-
}
|
|
2008
|
-
case "[object Object]": {
|
|
2009
|
-
const result = [];
|
|
2010
|
-
for (const i in obj) {
|
|
2011
|
-
if (Object.prototype.hasOwnProperty.call(obj, i)) {
|
|
2012
|
-
result.push(`${convertType(i, indent)} => ${convertType(obj[i], `${indent}${indent}`, indent)}`);
|
|
2013
|
-
}
|
|
2014
|
-
}
|
|
2015
|
-
return `[
|
|
2016
|
-
${indent}${result.join(`,
|
|
2017
|
-
${indent}`)}
|
|
2018
|
-
${lastIndent}]`;
|
|
2019
|
-
}
|
|
2020
|
-
default:
|
|
2021
|
-
return "null";
|
|
2022
|
-
}
|
|
2023
|
-
};
|
|
2024
|
-
var supportedMethods = [
|
|
2025
|
-
"ACL",
|
|
2026
|
-
"BASELINE_CONTROL",
|
|
2027
|
-
"CHECKIN",
|
|
2028
|
-
"CHECKOUT",
|
|
2029
|
-
"CONNECT",
|
|
2030
|
-
"COPY",
|
|
2031
|
-
"DELETE",
|
|
2032
|
-
"GET",
|
|
2033
|
-
"HEAD",
|
|
2034
|
-
"LABEL",
|
|
2035
|
-
"LOCK",
|
|
2036
|
-
"MERGE",
|
|
2037
|
-
"MKACTIVITY",
|
|
2038
|
-
"MKCOL",
|
|
2039
|
-
"MKWORKSPACE",
|
|
2040
|
-
"MOVE",
|
|
2041
|
-
"OPTIONS",
|
|
2042
|
-
"POST",
|
|
2043
|
-
"PROPFIND",
|
|
2044
|
-
"PROPPATCH",
|
|
2045
|
-
"PUT",
|
|
2046
|
-
"REPORT",
|
|
2047
|
-
"TRACE",
|
|
2048
|
-
"UNCHECKOUT",
|
|
2049
|
-
"UNLOCK",
|
|
2050
|
-
"UPDATE",
|
|
2051
|
-
"VERSION_CONTROL"
|
|
2052
|
-
];
|
|
2053
|
-
|
|
2054
|
-
// src/targets/php/curl/client.ts
|
|
2055
|
-
var curl = {
|
|
2056
|
-
info: {
|
|
2057
|
-
key: "curl",
|
|
2058
|
-
title: "cURL",
|
|
2059
|
-
link: "http://php.net/manual/en/book.curl.php",
|
|
2060
|
-
description: "PHP with ext-curl"
|
|
2061
|
-
},
|
|
2062
|
-
convert: ({ uriObj, postData, fullUrl, method, httpVersion, cookies, headersObj }, options = {}) => {
|
|
2063
|
-
const {
|
|
2064
|
-
closingTag = false,
|
|
2065
|
-
indent = " ",
|
|
2066
|
-
maxRedirects = 10,
|
|
2067
|
-
namedErrors = false,
|
|
2068
|
-
noTags = false,
|
|
2069
|
-
shortTags = false,
|
|
2070
|
-
timeout = 30
|
|
2071
|
-
} = options;
|
|
2072
|
-
const { push, blank, join } = new CodeBuilder({ indent });
|
|
2073
|
-
if (!noTags) {
|
|
2074
|
-
push(shortTags ? "<?" : "<?php");
|
|
2075
|
-
blank();
|
|
2076
|
-
}
|
|
2077
|
-
push("$curl = curl_init();");
|
|
2078
|
-
blank();
|
|
2079
|
-
const curlOptions = [
|
|
2080
|
-
{
|
|
2081
|
-
escape: true,
|
|
2082
|
-
name: "CURLOPT_PORT",
|
|
2083
|
-
value: uriObj.port
|
|
2084
|
-
},
|
|
2085
|
-
{
|
|
2086
|
-
escape: true,
|
|
2087
|
-
name: "CURLOPT_URL",
|
|
2088
|
-
value: fullUrl
|
|
2089
|
-
},
|
|
2090
|
-
{
|
|
2091
|
-
escape: false,
|
|
2092
|
-
name: "CURLOPT_RETURNTRANSFER",
|
|
2093
|
-
value: "true"
|
|
2094
|
-
},
|
|
2095
|
-
{
|
|
2096
|
-
escape: true,
|
|
2097
|
-
name: "CURLOPT_ENCODING",
|
|
2098
|
-
value: ""
|
|
2099
|
-
},
|
|
2100
|
-
{
|
|
2101
|
-
escape: false,
|
|
2102
|
-
name: "CURLOPT_MAXREDIRS",
|
|
2103
|
-
value: maxRedirects
|
|
2104
|
-
},
|
|
2105
|
-
{
|
|
2106
|
-
escape: false,
|
|
2107
|
-
name: "CURLOPT_TIMEOUT",
|
|
2108
|
-
value: timeout
|
|
2109
|
-
},
|
|
2110
|
-
{
|
|
2111
|
-
escape: false,
|
|
2112
|
-
name: "CURLOPT_HTTP_VERSION",
|
|
2113
|
-
value: httpVersion === "HTTP/1.0" ? "CURL_HTTP_VERSION_1_0" : "CURL_HTTP_VERSION_1_1"
|
|
2114
|
-
},
|
|
2115
|
-
{
|
|
2116
|
-
escape: true,
|
|
2117
|
-
name: "CURLOPT_CUSTOMREQUEST",
|
|
2118
|
-
value: method
|
|
2119
|
-
},
|
|
2120
|
-
{
|
|
2121
|
-
escape: !postData.jsonObj,
|
|
2122
|
-
name: "CURLOPT_POSTFIELDS",
|
|
2123
|
-
value: postData ? postData.jsonObj ? `json_encode(${convertType(postData.jsonObj, indent.repeat(2), indent)})` : postData.text : void 0
|
|
2124
|
-
}
|
|
2125
|
-
];
|
|
2126
|
-
push("curl_setopt_array($curl, [");
|
|
2127
|
-
const curlopts = new CodeBuilder({ indent, join: `
|
|
2128
|
-
${indent}` });
|
|
2129
|
-
curlOptions.forEach(({ value, name, escape: escape2 }) => {
|
|
2130
|
-
if (value !== null && value !== void 0) {
|
|
2131
|
-
curlopts.push(`${name} => ${escape2 ? JSON.stringify(value) : value},`);
|
|
2132
|
-
}
|
|
2133
|
-
});
|
|
2134
|
-
const curlCookies = cookies.map((cookie) => `${encodeURIComponent(cookie.name)}=${encodeURIComponent(cookie.value)}`);
|
|
2135
|
-
if (curlCookies.length) {
|
|
2136
|
-
curlopts.push(`CURLOPT_COOKIE => "${curlCookies.join("; ")}",`);
|
|
2137
|
-
}
|
|
2138
|
-
const headers = Object.keys(headersObj).sort().map((key) => `"${key}: ${escapeForDoubleQuotes(headersObj[key])}"`);
|
|
2139
|
-
if (headers.length) {
|
|
2140
|
-
curlopts.push("CURLOPT_HTTPHEADER => [");
|
|
2141
|
-
curlopts.push(headers.join(`,
|
|
2142
|
-
${indent}${indent}`), 1);
|
|
2143
|
-
curlopts.push("],");
|
|
2144
|
-
}
|
|
2145
|
-
push(curlopts.join(), 1);
|
|
2146
|
-
push("]);");
|
|
2147
|
-
blank();
|
|
2148
|
-
push("$response = curl_exec($curl);");
|
|
2149
|
-
push("$err = curl_error($curl);");
|
|
2150
|
-
blank();
|
|
2151
|
-
push("curl_close($curl);");
|
|
2152
|
-
blank();
|
|
2153
|
-
push("if ($err) {");
|
|
2154
|
-
if (namedErrors) {
|
|
2155
|
-
push('echo array_flip(get_defined_constants(true)["curl"])[$err];', 1);
|
|
2156
|
-
} else {
|
|
2157
|
-
push('echo "cURL Error #:" . $err;', 1);
|
|
2158
|
-
}
|
|
2159
|
-
push("} else {");
|
|
2160
|
-
push("echo $response;", 1);
|
|
2161
|
-
push("}");
|
|
2162
|
-
if (!noTags && closingTag) {
|
|
2163
|
-
blank();
|
|
2164
|
-
push("?>");
|
|
2165
|
-
}
|
|
2166
|
-
return join();
|
|
2167
|
-
}
|
|
2168
|
-
};
|
|
2169
|
-
|
|
2170
|
-
// src/targets/php/guzzle/client.ts
|
|
2171
|
-
var guzzle = {
|
|
2172
|
-
info: {
|
|
2173
|
-
key: "guzzle",
|
|
2174
|
-
title: "Guzzle",
|
|
2175
|
-
link: "http://docs.guzzlephp.org/en/stable/",
|
|
2176
|
-
description: "PHP with Guzzle"
|
|
2177
|
-
},
|
|
2178
|
-
convert: ({ postData, fullUrl, method, cookies, headersObj }, options) => {
|
|
2179
|
-
const opts = {
|
|
2180
|
-
closingTag: false,
|
|
2181
|
-
indent: " ",
|
|
2182
|
-
noTags: false,
|
|
2183
|
-
shortTags: false,
|
|
2184
|
-
...options
|
|
2185
|
-
};
|
|
2186
|
-
const { push, blank, join } = new CodeBuilder({ indent: opts.indent });
|
|
2187
|
-
const { code: requestCode, push: requestPush, join: requestJoin } = new CodeBuilder({ indent: opts.indent });
|
|
2188
|
-
if (!opts.noTags) {
|
|
2189
|
-
push(opts.shortTags ? "<?" : "<?php");
|
|
2190
|
-
}
|
|
2191
|
-
push("require_once('vendor/autoload.php');");
|
|
2192
|
-
blank();
|
|
2193
|
-
switch (postData.mimeType) {
|
|
2194
|
-
case "application/x-www-form-urlencoded":
|
|
2195
|
-
requestPush(`'form_params' => ${convertType(postData.paramsObj, opts.indent + opts.indent, opts.indent)},`, 1);
|
|
2196
|
-
break;
|
|
2197
|
-
case "multipart/form-data": {
|
|
2198
|
-
const fields = [];
|
|
2199
|
-
if (postData.params) {
|
|
2200
|
-
postData.params.forEach((param) => {
|
|
2201
|
-
if (param.fileName) {
|
|
2202
|
-
const field = {
|
|
2203
|
-
name: param.name,
|
|
2204
|
-
filename: param.fileName,
|
|
2205
|
-
contents: param.value
|
|
2206
|
-
};
|
|
2207
|
-
if (param.contentType) {
|
|
2208
|
-
field.headers = { "Content-Type": param.contentType };
|
|
2209
|
-
}
|
|
2210
|
-
fields.push(field);
|
|
2211
|
-
} else if (param.value) {
|
|
2212
|
-
fields.push({
|
|
2213
|
-
name: param.name,
|
|
2214
|
-
contents: param.value
|
|
2215
|
-
});
|
|
2216
|
-
}
|
|
2217
|
-
});
|
|
2218
|
-
}
|
|
2219
|
-
if (fields.length) {
|
|
2220
|
-
requestPush(`'multipart' => ${convertType(fields, opts.indent + opts.indent, opts.indent)}`, 1);
|
|
2221
|
-
if (hasHeader(headersObj, "content-type")) {
|
|
2222
|
-
if (getHeader(headersObj, "content-type")?.indexOf("boundary")) {
|
|
2223
|
-
const headerName = getHeaderName(headersObj, "content-type");
|
|
2224
|
-
if (headerName) {
|
|
2225
|
-
delete headersObj[headerName];
|
|
2226
|
-
}
|
|
2227
|
-
}
|
|
2228
|
-
}
|
|
2229
|
-
}
|
|
2230
|
-
break;
|
|
2231
|
-
}
|
|
2232
|
-
default:
|
|
2233
|
-
if (postData.text) {
|
|
2234
|
-
requestPush(`'body' => ${convertType(postData.text)},`, 1);
|
|
2235
|
-
}
|
|
2236
|
-
}
|
|
2237
|
-
const headers = Object.keys(headersObj).sort().map(function(key) {
|
|
2238
|
-
return `${opts.indent}${opts.indent}'${key}' => '${escapeForSingleQuotes(headersObj[key])}',`;
|
|
2239
|
-
});
|
|
2240
|
-
const cookieString = cookies.map((cookie) => `${encodeURIComponent(cookie.name)}=${encodeURIComponent(cookie.value)}`).join("; ");
|
|
2241
|
-
if (cookieString.length) {
|
|
2242
|
-
headers.push(`${opts.indent}${opts.indent}'cookie' => '${escapeForSingleQuotes(cookieString)}',`);
|
|
2243
|
-
}
|
|
2244
|
-
if (headers.length) {
|
|
2245
|
-
requestPush("'headers' => [", 1);
|
|
2246
|
-
requestPush(headers.join("\n"));
|
|
2247
|
-
requestPush("],", 1);
|
|
2248
|
-
}
|
|
2249
|
-
push("$client = new \\GuzzleHttp\\Client();");
|
|
2250
|
-
blank();
|
|
2251
|
-
if (requestCode.length) {
|
|
2252
|
-
push(`$response = $client->request('${method}', '${fullUrl}', [`);
|
|
2253
|
-
push(requestJoin());
|
|
2254
|
-
push("]);");
|
|
2255
|
-
} else {
|
|
2256
|
-
push(`$response = $client->request('${method}', '${fullUrl}');`);
|
|
2257
|
-
}
|
|
2258
|
-
blank();
|
|
2259
|
-
push("echo $response->getBody();");
|
|
2260
|
-
if (!opts.noTags && opts.closingTag) {
|
|
2261
|
-
blank();
|
|
2262
|
-
push("?>");
|
|
2263
|
-
}
|
|
2264
|
-
return join();
|
|
2265
|
-
}
|
|
2266
|
-
};
|
|
2267
|
-
|
|
2268
|
-
// src/targets/php/http1/client.ts
|
|
2269
|
-
var http1 = {
|
|
2270
|
-
info: {
|
|
2271
|
-
key: "http1",
|
|
2272
|
-
title: "HTTP v1",
|
|
2273
|
-
link: "http://php.net/manual/en/book.http.php",
|
|
2274
|
-
description: "PHP with pecl/http v1"
|
|
2275
|
-
},
|
|
2276
|
-
convert: ({ method, url, postData, queryObj, headersObj, cookiesObj }, options = {}) => {
|
|
2277
|
-
const { closingTag = false, indent = " ", noTags = false, shortTags = false } = options;
|
|
2278
|
-
const { push, blank, join } = new CodeBuilder({ indent });
|
|
2279
|
-
if (!noTags) {
|
|
2280
|
-
push(shortTags ? "<?" : "<?php");
|
|
2281
|
-
blank();
|
|
2282
|
-
}
|
|
2283
|
-
if (!supportedMethods.includes(method.toUpperCase())) {
|
|
2284
|
-
push(`HttpRequest::methodRegister('${method}');`);
|
|
2285
|
-
}
|
|
2286
|
-
push("$request = new HttpRequest();");
|
|
2287
|
-
push(`$request->setUrl(${convertType(url)});`);
|
|
2288
|
-
if (supportedMethods.includes(method.toUpperCase())) {
|
|
2289
|
-
push(`$request->setMethod(HTTP_METH_${method.toUpperCase()});`);
|
|
2290
|
-
} else {
|
|
2291
|
-
push(`$request->setMethod(HttpRequest::HTTP_METH_${method.toUpperCase()});`);
|
|
2292
|
-
}
|
|
2293
|
-
blank();
|
|
2294
|
-
if (Object.keys(queryObj).length) {
|
|
2295
|
-
push(`$request->setQueryData(${convertType(queryObj, indent)});`);
|
|
2296
|
-
blank();
|
|
2297
|
-
}
|
|
2298
|
-
if (Object.keys(headersObj).length) {
|
|
2299
|
-
push(`$request->setHeaders(${convertType(headersObj, indent)});`);
|
|
2300
|
-
blank();
|
|
2301
|
-
}
|
|
2302
|
-
if (Object.keys(cookiesObj).length) {
|
|
2303
|
-
push(`$request->setCookies(${convertType(cookiesObj, indent)});`);
|
|
2304
|
-
blank();
|
|
2305
|
-
}
|
|
2306
|
-
switch (postData.mimeType) {
|
|
2307
|
-
case "application/x-www-form-urlencoded":
|
|
2308
|
-
push(`$request->setContentType(${convertType(postData.mimeType)});`);
|
|
2309
|
-
push(`$request->setPostFields(${convertType(postData.paramsObj, indent)});`);
|
|
2310
|
-
blank();
|
|
2311
|
-
break;
|
|
2312
|
-
case "application/json":
|
|
2313
|
-
push(`$request->setContentType(${convertType(postData.mimeType)});`);
|
|
2314
|
-
push(`$request->setBody(json_encode(${convertType(postData.jsonObj, indent)}));`);
|
|
2315
|
-
blank();
|
|
2316
|
-
break;
|
|
2317
|
-
default:
|
|
2318
|
-
if (postData.text) {
|
|
2319
|
-
push(`$request->setBody(${convertType(postData.text)});`);
|
|
2320
|
-
blank();
|
|
2321
|
-
}
|
|
2322
|
-
}
|
|
2323
|
-
push("try {");
|
|
2324
|
-
push("$response = $request->send();", 1);
|
|
2325
|
-
blank();
|
|
2326
|
-
push("echo $response->getBody();", 1);
|
|
2327
|
-
push("} catch (HttpException $ex) {");
|
|
2328
|
-
push("echo $ex;", 1);
|
|
2329
|
-
push("}");
|
|
2330
|
-
if (!noTags && closingTag) {
|
|
2331
|
-
blank();
|
|
2332
|
-
push("?>");
|
|
2333
|
-
}
|
|
2334
|
-
return join();
|
|
2335
|
-
}
|
|
2336
|
-
};
|
|
2337
|
-
|
|
2338
|
-
// src/targets/php/http2/client.ts
|
|
2339
|
-
var http2 = {
|
|
2340
|
-
info: {
|
|
2341
|
-
key: "http2",
|
|
2342
|
-
title: "HTTP v2",
|
|
2343
|
-
link: "http://devel-m6w6.rhcloud.com/mdref/http",
|
|
2344
|
-
description: "PHP with pecl/http v2"
|
|
2345
|
-
},
|
|
2346
|
-
convert: ({ postData, headersObj, method, queryObj, cookiesObj, url }, options = {}) => {
|
|
2347
|
-
const { closingTag = false, indent = " ", noTags = false, shortTags = false } = options;
|
|
2348
|
-
const { push, blank, join } = new CodeBuilder({ indent });
|
|
2349
|
-
let hasBody = false;
|
|
2350
|
-
if (!noTags) {
|
|
2351
|
-
push(shortTags ? "<?" : "<?php");
|
|
2352
|
-
blank();
|
|
2353
|
-
}
|
|
2354
|
-
push("$client = new http\\Client;");
|
|
2355
|
-
push("$request = new http\\Client\\Request;");
|
|
2356
|
-
blank();
|
|
2357
|
-
switch (postData.mimeType) {
|
|
2358
|
-
case "application/x-www-form-urlencoded":
|
|
2359
|
-
push("$body = new http\\Message\\Body;");
|
|
2360
|
-
push(`$body->append(new http\\QueryString(${convertType(postData.paramsObj, indent)}));`);
|
|
2361
|
-
blank();
|
|
2362
|
-
hasBody = true;
|
|
2363
|
-
break;
|
|
2364
|
-
case "multipart/form-data": {
|
|
2365
|
-
if (!postData.params) {
|
|
2366
|
-
break;
|
|
2367
|
-
}
|
|
2368
|
-
const files = [];
|
|
2369
|
-
const fields = {};
|
|
2370
|
-
postData.params.forEach(({ name, fileName, value, contentType }) => {
|
|
2371
|
-
if (fileName) {
|
|
2372
|
-
files.push({
|
|
2373
|
-
name,
|
|
2374
|
-
type: contentType,
|
|
2375
|
-
file: fileName,
|
|
2376
|
-
data: value
|
|
2377
|
-
});
|
|
2378
|
-
return;
|
|
2379
|
-
}
|
|
2380
|
-
if (value) {
|
|
2381
|
-
fields[name] = value;
|
|
2382
|
-
}
|
|
2383
|
-
});
|
|
2384
|
-
const field = Object.keys(fields).length ? convertType(fields, indent) : "null";
|
|
2385
|
-
const formValue = files.length ? convertType(files, indent) : "null";
|
|
2386
|
-
push("$body = new http\\Message\\Body;");
|
|
2387
|
-
push(`$body->addForm(${field}, ${formValue});`);
|
|
2388
|
-
if (hasHeader(headersObj, "content-type")) {
|
|
2389
|
-
if (getHeader(headersObj, "content-type")?.indexOf("boundary")) {
|
|
2390
|
-
const headerName = getHeaderName(headersObj, "content-type");
|
|
2391
|
-
if (headerName) {
|
|
2392
|
-
delete headersObj[headerName];
|
|
2393
|
-
}
|
|
2394
|
-
}
|
|
2395
|
-
}
|
|
2396
|
-
blank();
|
|
2397
|
-
hasBody = true;
|
|
2398
|
-
break;
|
|
2399
|
-
}
|
|
2400
|
-
case "application/json":
|
|
2401
|
-
push("$body = new http\\Message\\Body;");
|
|
2402
|
-
push(`$body->append(json_encode(${convertType(postData.jsonObj, indent)}));`);
|
|
2403
|
-
hasBody = true;
|
|
2404
|
-
break;
|
|
2405
|
-
default:
|
|
2406
|
-
if (postData.text) {
|
|
2407
|
-
push("$body = new http\\Message\\Body;");
|
|
2408
|
-
push(`$body->append(${convertType(postData.text)});`);
|
|
2409
|
-
blank();
|
|
2410
|
-
hasBody = true;
|
|
2411
|
-
}
|
|
2412
|
-
}
|
|
2413
|
-
push(`$request->setRequestUrl(${convertType(url)});`);
|
|
2414
|
-
push(`$request->setRequestMethod(${convertType(method)});`);
|
|
2415
|
-
if (hasBody) {
|
|
2416
|
-
push("$request->setBody($body);");
|
|
2417
|
-
blank();
|
|
2418
|
-
}
|
|
2419
|
-
if (Object.keys(queryObj).length) {
|
|
2420
|
-
push(`$request->setQuery(new http\\QueryString(${convertType(queryObj, indent)}));`);
|
|
2421
|
-
blank();
|
|
2422
|
-
}
|
|
2423
|
-
if (Object.keys(headersObj).length) {
|
|
2424
|
-
push(`$request->setHeaders(${convertType(headersObj, indent)});`);
|
|
2425
|
-
blank();
|
|
2426
|
-
}
|
|
2427
|
-
if (Object.keys(cookiesObj).length) {
|
|
2428
|
-
blank();
|
|
2429
|
-
push(`$client->setCookies(${convertType(cookiesObj, indent)});`);
|
|
2430
|
-
blank();
|
|
2431
|
-
}
|
|
2432
|
-
push("$client->enqueue($request)->send();");
|
|
2433
|
-
push("$response = $client->getResponse();");
|
|
2434
|
-
blank();
|
|
2435
|
-
push("echo $response->getBody();");
|
|
2436
|
-
if (!noTags && closingTag) {
|
|
2437
|
-
blank();
|
|
2438
|
-
push("?>");
|
|
2439
|
-
}
|
|
2440
|
-
return join();
|
|
2441
|
-
}
|
|
2442
|
-
};
|
|
2443
|
-
|
|
2444
|
-
// src/targets/php/target.ts
|
|
2445
|
-
var php = {
|
|
2446
|
-
info: {
|
|
2447
|
-
key: "php",
|
|
2448
|
-
title: "PHP",
|
|
2449
|
-
extname: ".php",
|
|
2450
|
-
default: "curl",
|
|
2451
|
-
cli: "php %s"
|
|
2452
|
-
},
|
|
2453
|
-
clientsById: {
|
|
2454
|
-
curl,
|
|
2455
|
-
guzzle,
|
|
2456
|
-
http1,
|
|
2457
|
-
http2
|
|
2458
|
-
}
|
|
2459
|
-
};
|
|
2460
|
-
|
|
2461
|
-
// src/targets/powershell/common.ts
|
|
2462
|
-
var generatePowershellConvert = (command) => {
|
|
2463
|
-
const convert = ({ method, headersObj, cookies, uriObj, fullUrl, postData, allHeaders }) => {
|
|
2464
|
-
const { push, join } = new CodeBuilder();
|
|
2465
|
-
const methods = ["GET", "POST", "PUT", "DELETE", "PATCH", "HEAD", "OPTIONS"];
|
|
2466
|
-
if (!methods.includes(method.toUpperCase())) {
|
|
2467
|
-
return "Method not supported";
|
|
2468
|
-
}
|
|
2469
|
-
const commandOptions = [];
|
|
2470
|
-
const headers = Object.keys(headersObj);
|
|
2471
|
-
if (headers.length) {
|
|
2472
|
-
push("$headers=@{}");
|
|
2473
|
-
headers.forEach((key) => {
|
|
2474
|
-
if (key !== "connection") {
|
|
2475
|
-
push(`$headers.Add("${key}", "${escapeString(headersObj[key], { escapeChar: "`" })}")`);
|
|
2476
|
-
}
|
|
2477
|
-
});
|
|
2478
|
-
commandOptions.push("-Headers $headers");
|
|
2479
|
-
}
|
|
2480
|
-
if (cookies.length) {
|
|
2481
|
-
push("$session = New-Object Microsoft.PowerShell.Commands.WebRequestSession");
|
|
2482
|
-
cookies.forEach((cookie) => {
|
|
2483
|
-
push("$cookie = New-Object System.Net.Cookie");
|
|
2484
|
-
push(`$cookie.Name = '${cookie.name}'`);
|
|
2485
|
-
push(`$cookie.Value = '${cookie.value}'`);
|
|
2486
|
-
push(`$cookie.Domain = '${uriObj.host}'`);
|
|
2487
|
-
push("$session.Cookies.Add($cookie)");
|
|
2488
|
-
});
|
|
2489
|
-
commandOptions.push("-WebSession $session");
|
|
2490
|
-
}
|
|
2491
|
-
if (postData.text) {
|
|
2492
|
-
commandOptions.push(
|
|
2493
|
-
`-ContentType '${escapeString(getHeader(allHeaders, "content-type"), { delimiter: "'", escapeChar: "`" })}'`
|
|
2494
|
-
);
|
|
2495
|
-
commandOptions.push(`-Body '${postData.text}'`);
|
|
2496
|
-
}
|
|
2497
|
-
push(`$response = ${command} -Uri '${fullUrl}' -Method ${method} ${commandOptions.join(" ")}`.trim());
|
|
2498
|
-
return join();
|
|
2499
|
-
};
|
|
2500
|
-
return convert;
|
|
2501
|
-
};
|
|
2502
|
-
|
|
2503
|
-
// src/targets/powershell/restmethod/client.ts
|
|
2504
|
-
var restmethod = {
|
|
2505
|
-
info: {
|
|
2506
|
-
key: "restmethod",
|
|
2507
|
-
title: "Invoke-RestMethod",
|
|
2508
|
-
link: "https://docs.microsoft.com/en-us/powershell/module/Microsoft.PowerShell.Utility/Invoke-RestMethod",
|
|
2509
|
-
description: "Powershell Invoke-RestMethod client"
|
|
2510
|
-
},
|
|
2511
|
-
convert: generatePowershellConvert("Invoke-RestMethod")
|
|
2512
|
-
};
|
|
2513
|
-
|
|
2514
|
-
// src/targets/powershell/webrequest/client.ts
|
|
2515
|
-
var webrequest = {
|
|
2516
|
-
info: {
|
|
2517
|
-
key: "webrequest",
|
|
2518
|
-
title: "Invoke-WebRequest",
|
|
2519
|
-
link: "https://docs.microsoft.com/en-us/powershell/module/Microsoft.PowerShell.Utility/Invoke-WebRequest",
|
|
2520
|
-
description: "Powershell Invoke-WebRequest client"
|
|
2521
|
-
},
|
|
2522
|
-
convert: generatePowershellConvert("Invoke-WebRequest")
|
|
2523
|
-
};
|
|
2524
|
-
|
|
2525
|
-
// src/targets/powershell/target.ts
|
|
2526
|
-
var powershell = {
|
|
2527
|
-
info: {
|
|
2528
|
-
key: "powershell",
|
|
2529
|
-
title: "Powershell",
|
|
2530
|
-
extname: ".ps1",
|
|
2531
|
-
default: "webrequest"
|
|
2532
|
-
},
|
|
2533
|
-
clientsById: {
|
|
2534
|
-
webrequest,
|
|
2535
|
-
restmethod
|
|
2536
|
-
}
|
|
2537
|
-
};
|
|
2538
|
-
|
|
2539
|
-
// src/targets/python/helpers.ts
|
|
2540
|
-
function concatValues(concatType, values, pretty, indentation, indentLevel) {
|
|
2541
|
-
const currentIndent = indentation.repeat(indentLevel);
|
|
2542
|
-
const closingBraceIndent = indentation.repeat(indentLevel - 1);
|
|
2543
|
-
const join = pretty ? `,
|
|
2544
|
-
${currentIndent}` : ", ";
|
|
2545
|
-
const openingBrace = concatType === "object" ? "{" : "[";
|
|
2546
|
-
const closingBrace = concatType === "object" ? "}" : "]";
|
|
2547
|
-
if (pretty) {
|
|
2548
|
-
return `${openingBrace}
|
|
2549
|
-
${currentIndent}${values.join(join)}
|
|
2550
|
-
${closingBraceIndent}${closingBrace}`;
|
|
2551
|
-
}
|
|
2552
|
-
if (concatType === "object" && values.length > 0) {
|
|
2553
|
-
return `${openingBrace} ${values.join(join)} ${closingBrace}`;
|
|
2554
|
-
}
|
|
2555
|
-
return `${openingBrace}${values.join(join)}${closingBrace}`;
|
|
2556
|
-
}
|
|
2557
|
-
var literalRepresentation2 = (value, opts, indentLevel) => {
|
|
2558
|
-
indentLevel = indentLevel === void 0 ? 1 : indentLevel + 1;
|
|
2559
|
-
switch (Object.prototype.toString.call(value)) {
|
|
2560
|
-
case "[object Number]":
|
|
2561
|
-
return value;
|
|
2562
|
-
case "[object Array]": {
|
|
2563
|
-
let pretty = false;
|
|
2564
|
-
const valuesRepresentation = value.map((v) => {
|
|
2565
|
-
if (Object.prototype.toString.call(v) === "[object Object]") {
|
|
2566
|
-
pretty = Object.keys(v).length > 1;
|
|
2567
|
-
}
|
|
2568
|
-
return literalRepresentation2(v, opts, indentLevel);
|
|
2569
|
-
});
|
|
2570
|
-
return concatValues("array", valuesRepresentation, pretty, opts.indent, indentLevel);
|
|
2571
|
-
}
|
|
2572
|
-
case "[object Object]": {
|
|
2573
|
-
const keyValuePairs = [];
|
|
2574
|
-
for (const key in value) {
|
|
2575
|
-
keyValuePairs.push(`"${key}": ${literalRepresentation2(value[key], opts, indentLevel)}`);
|
|
2576
|
-
}
|
|
2577
|
-
return concatValues("object", keyValuePairs, opts.pretty && keyValuePairs.length > 1, opts.indent, indentLevel);
|
|
2578
|
-
}
|
|
2579
|
-
case "[object Null]":
|
|
2580
|
-
return "None";
|
|
2581
|
-
case "[object Boolean]":
|
|
2582
|
-
return value ? "True" : "False";
|
|
2583
|
-
default:
|
|
2584
|
-
if (value === null || value === void 0) {
|
|
2585
|
-
return "";
|
|
2586
|
-
}
|
|
2587
|
-
return `"${value.toString().replace(/"/g, '\\"')}"`;
|
|
2588
|
-
}
|
|
2589
|
-
};
|
|
2590
|
-
|
|
2591
|
-
// src/targets/python/requests/client.ts
|
|
2592
|
-
var builtInMethods = ["HEAD", "GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"];
|
|
2593
|
-
var requests = {
|
|
2594
|
-
info: {
|
|
2595
|
-
key: "requests",
|
|
2596
|
-
title: "Requests",
|
|
2597
|
-
link: "http://docs.python-requests.org/en/latest/api/#requests.request",
|
|
2598
|
-
description: "Requests HTTP library"
|
|
2599
|
-
},
|
|
2600
|
-
convert: ({ fullUrl, postData, allHeaders, method }, options) => {
|
|
2601
|
-
const opts = {
|
|
2602
|
-
indent: " ",
|
|
2603
|
-
pretty: true,
|
|
2604
|
-
...options
|
|
2605
|
-
};
|
|
2606
|
-
const { push, blank, join, addPostProcessor } = new CodeBuilder({ indent: opts.indent });
|
|
2607
|
-
push("import requests");
|
|
2608
|
-
blank();
|
|
2609
|
-
push(`url = "${fullUrl}"`);
|
|
2610
|
-
blank();
|
|
2611
|
-
const headers = allHeaders;
|
|
2612
|
-
let payload = {};
|
|
2613
|
-
const files = {};
|
|
2614
|
-
let hasFiles = false;
|
|
2615
|
-
let hasPayload = false;
|
|
2616
|
-
let jsonPayload = false;
|
|
2617
|
-
switch (postData.mimeType) {
|
|
2618
|
-
case "application/json":
|
|
2619
|
-
if (postData.jsonObj) {
|
|
2620
|
-
push(`payload = ${literalRepresentation2(postData.jsonObj, opts)}`);
|
|
2621
|
-
jsonPayload = true;
|
|
2622
|
-
hasPayload = true;
|
|
2623
|
-
}
|
|
2624
|
-
break;
|
|
2625
|
-
case "multipart/form-data":
|
|
2626
|
-
if (!postData.params) {
|
|
2627
|
-
break;
|
|
2628
|
-
}
|
|
2629
|
-
payload = {};
|
|
2630
|
-
postData.params.forEach((p) => {
|
|
2631
|
-
if (p.fileName) {
|
|
2632
|
-
if (p.contentType) {
|
|
2633
|
-
files[p.name] = `('${p.fileName}', open('${p.fileName}', 'rb'), '${p.contentType}')`;
|
|
2634
|
-
} else {
|
|
2635
|
-
files[p.name] = `('${p.fileName}', open('${p.fileName}', 'rb'))`;
|
|
2636
|
-
}
|
|
2637
|
-
hasFiles = true;
|
|
2638
|
-
} else {
|
|
2639
|
-
payload[p.name] = p.value;
|
|
2640
|
-
hasPayload = true;
|
|
2641
|
-
}
|
|
2642
|
-
});
|
|
2643
|
-
if (hasFiles) {
|
|
2644
|
-
push(`files = ${literalRepresentation2(files, opts)}`);
|
|
2645
|
-
if (hasPayload) {
|
|
2646
|
-
push(`payload = ${literalRepresentation2(payload, opts)}`);
|
|
2647
|
-
}
|
|
2648
|
-
const headerName = getHeaderName(headers, "content-type");
|
|
2649
|
-
if (headerName) {
|
|
2650
|
-
delete headers[headerName];
|
|
2651
|
-
}
|
|
2652
|
-
} else {
|
|
2653
|
-
const nonFilePayload = JSON.stringify(postData.text);
|
|
2654
|
-
if (nonFilePayload) {
|
|
2655
|
-
push(`payload = ${nonFilePayload}`);
|
|
2656
|
-
hasPayload = true;
|
|
2657
|
-
}
|
|
2658
|
-
}
|
|
2659
|
-
addPostProcessor(
|
|
2660
|
-
(code) => code.replace(/"\('(.+)', open\('(.+)', 'rb'\)\)"/g, '("$1", open("$2", "rb"))').replace(/"\('(.+)', open\('(.+)', 'rb'\), '(.+)'\)"/g, '("$1", open("$2", "rb"), "$3")')
|
|
2661
|
-
);
|
|
2662
|
-
break;
|
|
2663
|
-
default: {
|
|
2664
|
-
if (postData.mimeType === "application/x-www-form-urlencoded" && postData.paramsObj) {
|
|
2665
|
-
push(`payload = ${literalRepresentation2(postData.paramsObj, opts)}`);
|
|
2666
|
-
hasPayload = true;
|
|
2667
|
-
break;
|
|
2668
|
-
}
|
|
2669
|
-
const stringPayload = JSON.stringify(postData.text);
|
|
2670
|
-
if (stringPayload) {
|
|
2671
|
-
push(`payload = ${stringPayload}`);
|
|
2672
|
-
hasPayload = true;
|
|
2673
|
-
}
|
|
2674
|
-
}
|
|
2675
|
-
}
|
|
2676
|
-
const headerCount = Object.keys(headers).length;
|
|
2677
|
-
if (headerCount === 0 && (hasPayload || hasFiles)) {
|
|
2678
|
-
blank();
|
|
2679
|
-
} else if (headerCount === 1) {
|
|
2680
|
-
Object.keys(headers).forEach((header) => {
|
|
2681
|
-
push(`headers = {"${header}": "${escapeForDoubleQuotes(headers[header])}"}`);
|
|
2682
|
-
blank();
|
|
2683
|
-
});
|
|
2684
|
-
} else if (headerCount > 1) {
|
|
2685
|
-
let count = 1;
|
|
2686
|
-
push("headers = {");
|
|
2687
|
-
Object.keys(headers).forEach((header) => {
|
|
2688
|
-
if (count !== headerCount) {
|
|
2689
|
-
push(`"${header}": "${escapeForDoubleQuotes(headers[header])}",`, 1);
|
|
2690
|
-
} else {
|
|
2691
|
-
push(`"${header}": "${escapeForDoubleQuotes(headers[header])}"`, 1);
|
|
2692
|
-
}
|
|
2693
|
-
count += 1;
|
|
2694
|
-
});
|
|
2695
|
-
push("}");
|
|
2696
|
-
blank();
|
|
2697
|
-
}
|
|
2698
|
-
let request2 = builtInMethods.includes(method) ? `response = requests.${method.toLowerCase()}(url` : `response = requests.request("${method}", url`;
|
|
2699
|
-
if (hasPayload) {
|
|
2700
|
-
if (jsonPayload) {
|
|
2701
|
-
request2 += ", json=payload";
|
|
2702
|
-
} else {
|
|
2703
|
-
request2 += ", data=payload";
|
|
2704
|
-
}
|
|
2705
|
-
}
|
|
2706
|
-
if (hasFiles) {
|
|
2707
|
-
request2 += ", files=files";
|
|
2708
|
-
}
|
|
2709
|
-
if (headerCount > 0) {
|
|
2710
|
-
request2 += ", headers=headers";
|
|
2711
|
-
}
|
|
2712
|
-
request2 += ")";
|
|
2713
|
-
push(request2);
|
|
2714
|
-
blank();
|
|
2715
|
-
push("print(response.text)");
|
|
2716
|
-
return join();
|
|
2717
|
-
}
|
|
2718
|
-
};
|
|
2719
|
-
|
|
2720
|
-
// src/targets/python/target.ts
|
|
2721
|
-
var python = {
|
|
2722
|
-
info: {
|
|
2723
|
-
key: "python",
|
|
2724
|
-
title: "Python",
|
|
2725
|
-
extname: ".py",
|
|
2726
|
-
default: "requests",
|
|
2727
|
-
cli: "python3 %s"
|
|
2728
|
-
},
|
|
2729
|
-
clientsById: {
|
|
2730
|
-
requests
|
|
2731
|
-
}
|
|
2732
|
-
};
|
|
2733
|
-
|
|
2734
|
-
// src/targets/r/httr/client.ts
|
|
2735
|
-
var httr = {
|
|
2736
|
-
info: {
|
|
2737
|
-
key: "httr",
|
|
2738
|
-
title: "httr",
|
|
2739
|
-
link: "https://cran.r-project.org/web/packages/httr/vignettes/quickstart.html",
|
|
2740
|
-
description: "httr: Tools for Working with URLs and HTTP"
|
|
2741
|
-
},
|
|
2742
|
-
convert: ({ url, queryObj, queryString, postData, allHeaders, method }) => {
|
|
2743
|
-
const { push, blank, join } = new CodeBuilder();
|
|
2744
|
-
push("library(httr)");
|
|
2745
|
-
blank();
|
|
2746
|
-
push(`url <- "${url}"`);
|
|
2747
|
-
blank();
|
|
2748
|
-
const qs = queryObj;
|
|
2749
|
-
delete queryObj.key;
|
|
2750
|
-
const queryCount = Object.keys(qs).length;
|
|
2751
|
-
if (queryString.length === 1) {
|
|
2752
|
-
push(`queryString <- list(${Object.keys(qs)} = "${Object.values(qs).toString()}")`);
|
|
2753
|
-
blank();
|
|
2754
|
-
} else if (queryString.length > 1) {
|
|
2755
|
-
push("queryString <- list(");
|
|
2756
|
-
Object.keys(qs).forEach((query, i) => {
|
|
2757
|
-
if (i !== queryCount - 1) {
|
|
2758
|
-
push(` ${query} = "${qs[query].toString()}",`);
|
|
2759
|
-
} else {
|
|
2760
|
-
push(` ${query} = "${qs[query].toString()}"`);
|
|
2761
|
-
}
|
|
2762
|
-
});
|
|
2763
|
-
push(")");
|
|
2764
|
-
blank();
|
|
2765
|
-
}
|
|
2766
|
-
const payload = JSON.stringify(postData.text);
|
|
2767
|
-
if (payload) {
|
|
2768
|
-
push(`payload <- ${payload}`);
|
|
2769
|
-
blank();
|
|
2770
|
-
}
|
|
2771
|
-
if (postData.text || postData.jsonObj || postData.params) {
|
|
2772
|
-
switch (postData.mimeType) {
|
|
2773
|
-
case "application/x-www-form-urlencoded":
|
|
2774
|
-
push('encode <- "form"');
|
|
2775
|
-
blank();
|
|
2776
|
-
break;
|
|
2777
|
-
case "application/json":
|
|
2778
|
-
push('encode <- "json"');
|
|
2779
|
-
blank();
|
|
2780
|
-
break;
|
|
2781
|
-
case "multipart/form-data":
|
|
2782
|
-
push('encode <- "multipart"');
|
|
2783
|
-
blank();
|
|
2784
|
-
break;
|
|
2785
|
-
default:
|
|
2786
|
-
push('encode <- "raw"');
|
|
2787
|
-
blank();
|
|
2788
|
-
break;
|
|
2789
|
-
}
|
|
2790
|
-
}
|
|
2791
|
-
const cookieHeader = getHeader(allHeaders, "cookie");
|
|
2792
|
-
const acceptHeader = getHeader(allHeaders, "accept");
|
|
2793
|
-
const setCookies = cookieHeader ? `set_cookies(\`${String(cookieHeader).replace(/;/g, '", `').replace(/` /g, "`").replace(/[=]/g, '` = "')}")` : void 0;
|
|
2794
|
-
const setAccept = acceptHeader ? `accept("${escapeForDoubleQuotes(acceptHeader)}")` : void 0;
|
|
2795
|
-
const setContentType = `content_type("${escapeForDoubleQuotes(postData.mimeType)}")`;
|
|
2796
|
-
const otherHeaders = Object.entries(allHeaders).filter(([key]) => !["cookie", "accept", "content-type"].includes(key.toLowerCase())).map(([key, value]) => `'${key}' = '${escapeForSingleQuotes(value)}'`).join(", ");
|
|
2797
|
-
const setHeaders = otherHeaders ? `add_headers(${otherHeaders})` : void 0;
|
|
2798
|
-
let request2 = `response <- VERB("${method}", url`;
|
|
2799
|
-
if (payload) {
|
|
2800
|
-
request2 += ", body = payload";
|
|
2801
|
-
}
|
|
2802
|
-
if (queryString.length) {
|
|
2803
|
-
request2 += ", query = queryString";
|
|
2804
|
-
}
|
|
2805
|
-
const headerAdditions = [setHeaders, setContentType, setAccept, setCookies].filter((x) => !!x).join(", ");
|
|
2806
|
-
if (headerAdditions) {
|
|
2807
|
-
request2 += `, ${headerAdditions}`;
|
|
2808
|
-
}
|
|
2809
|
-
if (postData.text || postData.jsonObj || postData.params) {
|
|
2810
|
-
request2 += ", encode = encode";
|
|
2811
|
-
}
|
|
2812
|
-
request2 += ")";
|
|
2813
|
-
push(request2);
|
|
2814
|
-
blank();
|
|
2815
|
-
push('content(response, "text")');
|
|
2816
|
-
return join();
|
|
2817
|
-
}
|
|
2818
|
-
};
|
|
2819
|
-
|
|
2820
|
-
// src/targets/r/target.ts
|
|
2821
|
-
var r = {
|
|
2822
|
-
info: {
|
|
2823
|
-
key: "r",
|
|
2824
|
-
title: "R",
|
|
2825
|
-
extname: ".r",
|
|
2826
|
-
default: "httr"
|
|
2827
|
-
},
|
|
2828
|
-
clientsById: {
|
|
2829
|
-
httr
|
|
2830
|
-
}
|
|
2831
|
-
};
|
|
2832
|
-
|
|
2833
|
-
// src/targets/ruby/native/client.ts
|
|
2834
|
-
var native4 = {
|
|
2835
|
-
info: {
|
|
2836
|
-
key: "native",
|
|
2837
|
-
title: "net::http",
|
|
2838
|
-
link: "http://ruby-doc.org/stdlib-2.2.1/libdoc/net/http/rdoc/Net/HTTP.html",
|
|
2839
|
-
description: "Ruby HTTP client"
|
|
2840
|
-
},
|
|
2841
|
-
convert: ({ uriObj, method: rawMethod, fullUrl, postData, allHeaders }) => {
|
|
2842
|
-
const { push, blank, join } = new CodeBuilder();
|
|
2843
|
-
push("require 'uri'");
|
|
2844
|
-
push("require 'net/http'");
|
|
2845
|
-
blank();
|
|
2846
|
-
const method = rawMethod.toUpperCase();
|
|
2847
|
-
const methods = [
|
|
2848
|
-
"GET",
|
|
2849
|
-
"POST",
|
|
2850
|
-
"HEAD",
|
|
2851
|
-
"DELETE",
|
|
2852
|
-
"PATCH",
|
|
2853
|
-
"PUT",
|
|
2854
|
-
"OPTIONS",
|
|
2855
|
-
"COPY",
|
|
2856
|
-
"LOCK",
|
|
2857
|
-
"UNLOCK",
|
|
2858
|
-
"MOVE",
|
|
2859
|
-
"TRACE"
|
|
2860
|
-
];
|
|
2861
|
-
const capMethod = method.charAt(0) + method.substring(1).toLowerCase();
|
|
2862
|
-
if (!methods.includes(method)) {
|
|
2863
|
-
push(`class Net::HTTP::${capMethod} < Net::HTTPRequest`);
|
|
2864
|
-
push(` METHOD = '${method.toUpperCase()}'`);
|
|
2865
|
-
push(` REQUEST_HAS_BODY = '${postData.text ? "true" : "false"}'`);
|
|
2866
|
-
push(" RESPONSE_HAS_BODY = true");
|
|
2867
|
-
push("end");
|
|
2868
|
-
blank();
|
|
2869
|
-
}
|
|
2870
|
-
push(`url = URI("${fullUrl}")`);
|
|
2871
|
-
blank();
|
|
2872
|
-
push("http = Net::HTTP.new(url.host, url.port)");
|
|
2873
|
-
if (uriObj.protocol === "https:") {
|
|
2874
|
-
push("http.use_ssl = true");
|
|
2875
|
-
}
|
|
2876
|
-
blank();
|
|
2877
|
-
push(`request = Net::HTTP::${capMethod}.new(url)`);
|
|
2878
|
-
const headers = Object.keys(allHeaders);
|
|
2879
|
-
if (headers.length) {
|
|
2880
|
-
headers.forEach((key) => {
|
|
2881
|
-
push(`request["${key}"] = '${escapeForSingleQuotes(allHeaders[key])}'`);
|
|
2882
|
-
});
|
|
2883
|
-
}
|
|
2884
|
-
if (postData.text) {
|
|
2885
|
-
push(`request.body = ${JSON.stringify(postData.text)}`);
|
|
2886
|
-
}
|
|
2887
|
-
blank();
|
|
2888
|
-
push("response = http.request(request)");
|
|
2889
|
-
push("puts response.read_body");
|
|
2890
|
-
return join();
|
|
2891
|
-
}
|
|
2892
|
-
};
|
|
2893
|
-
|
|
2894
|
-
// src/targets/ruby/target.ts
|
|
2895
|
-
var ruby = {
|
|
2896
|
-
info: {
|
|
2897
|
-
key: "ruby",
|
|
2898
|
-
title: "Ruby",
|
|
2899
|
-
extname: ".rb",
|
|
2900
|
-
default: "native"
|
|
2901
|
-
},
|
|
2902
|
-
clientsById: {
|
|
2903
|
-
native: native4
|
|
2904
|
-
}
|
|
2905
|
-
};
|
|
2906
|
-
|
|
2907
|
-
// src/helpers/shell.ts
|
|
2908
|
-
var quote = (value = "") => {
|
|
2909
|
-
const safe = /^[a-z0-9-_/.@%^=:]+$/i;
|
|
2910
|
-
const isShellSafe = safe.test(value);
|
|
2911
|
-
if (isShellSafe) {
|
|
2912
|
-
return value;
|
|
2913
|
-
}
|
|
2914
|
-
return `'${value.replace(/'/g, "'\\''")}'`;
|
|
2915
|
-
};
|
|
2916
|
-
var escape = (value) => value.replace(/\r/g, "\\r").replace(/\n/g, "\\n");
|
|
2917
|
-
|
|
2918
|
-
// src/targets/shell/curl/client.ts
|
|
2919
|
-
var params = {
|
|
2920
|
-
"http1.0": "0",
|
|
2921
|
-
"url ": "",
|
|
2922
|
-
cookie: "b",
|
|
2923
|
-
data: "d",
|
|
2924
|
-
form: "F",
|
|
2925
|
-
globoff: "g",
|
|
2926
|
-
header: "H",
|
|
2927
|
-
insecure: "k",
|
|
2928
|
-
request: "X"
|
|
2929
|
-
};
|
|
2930
|
-
var getArg = (short) => (longName) => {
|
|
2931
|
-
if (short) {
|
|
2932
|
-
const shortName = params[longName];
|
|
2933
|
-
if (!shortName) {
|
|
2934
|
-
return "";
|
|
2935
|
-
}
|
|
2936
|
-
return `-${shortName}`;
|
|
2937
|
-
}
|
|
2938
|
-
return `--${longName}`;
|
|
2939
|
-
};
|
|
2940
|
-
var curl2 = {
|
|
2941
|
-
info: {
|
|
2942
|
-
key: "curl",
|
|
2943
|
-
title: "cURL",
|
|
2944
|
-
link: "http://curl.haxx.se/",
|
|
2945
|
-
description: "cURL is a command line tool and library for transferring data with URL syntax"
|
|
2946
|
-
},
|
|
2947
|
-
convert: ({ fullUrl, method, httpVersion, headersObj, allHeaders, postData }, options = {}) => {
|
|
2948
|
-
const { indent = " ", short = false, binary = false, globOff = false } = options;
|
|
2949
|
-
const indentJSON = " ";
|
|
2950
|
-
const { push, join } = new CodeBuilder({
|
|
2951
|
-
...typeof indent === "string" ? { indent } : {},
|
|
2952
|
-
join: indent !== false ? ` \\
|
|
2953
|
-
${indent}` : " "
|
|
2954
|
-
});
|
|
2955
|
-
const arg = getArg(short);
|
|
2956
|
-
let formattedUrl = quote(fullUrl);
|
|
2957
|
-
push(`curl ${arg("request")} ${method}`);
|
|
2958
|
-
if (globOff) {
|
|
2959
|
-
formattedUrl = unescape(formattedUrl);
|
|
2960
|
-
push(arg("globoff"));
|
|
2961
|
-
}
|
|
2962
|
-
push(`${arg("url ")}${formattedUrl}`);
|
|
2963
|
-
if (httpVersion === "HTTP/1.0") {
|
|
2964
|
-
push(arg("http1.0"));
|
|
2965
|
-
}
|
|
2966
|
-
if (getHeader(allHeaders, "accept-encoding")) {
|
|
2967
|
-
push("--compressed");
|
|
2968
|
-
}
|
|
2969
|
-
if (postData.mimeType === "multipart/form-data") {
|
|
2970
|
-
const contentTypeHeaderName = getHeaderName(headersObj, "content-type");
|
|
2971
|
-
if (contentTypeHeaderName) {
|
|
2972
|
-
const contentTypeHeader = headersObj[contentTypeHeaderName];
|
|
2973
|
-
if (contentTypeHeaderName && contentTypeHeader) {
|
|
2974
|
-
const noBoundary = String(contentTypeHeader).replace(/; boundary.+?(?=(;|$))/, "");
|
|
2975
|
-
headersObj[contentTypeHeaderName] = noBoundary;
|
|
2976
|
-
allHeaders[contentTypeHeaderName] = noBoundary;
|
|
2977
|
-
}
|
|
2978
|
-
}
|
|
2979
|
-
}
|
|
2980
|
-
Object.keys(headersObj).sort().forEach((key) => {
|
|
2981
|
-
const header = `${key}: ${headersObj[key]}`;
|
|
2982
|
-
push(`${arg("header")} ${quote(header)}`);
|
|
2983
|
-
});
|
|
2984
|
-
if (allHeaders.cookie) {
|
|
2985
|
-
push(`${arg("cookie")} ${quote(allHeaders.cookie)}`);
|
|
2986
|
-
}
|
|
2987
|
-
switch (postData.mimeType) {
|
|
2988
|
-
case "multipart/form-data":
|
|
2989
|
-
postData.params?.forEach((param) => {
|
|
2990
|
-
let post = "";
|
|
2991
|
-
if (param.fileName) {
|
|
2992
|
-
post = `${param.name}=@${param.fileName}`;
|
|
2993
|
-
} else {
|
|
2994
|
-
post = `${param.name}=${param.value}`;
|
|
2995
|
-
}
|
|
2996
|
-
push(`${arg("form")} ${quote(post)}`);
|
|
2997
|
-
});
|
|
2998
|
-
break;
|
|
2999
|
-
case "application/x-www-form-urlencoded":
|
|
3000
|
-
if (postData.params) {
|
|
3001
|
-
postData.params.forEach((param) => {
|
|
3002
|
-
const unencoded = param.name;
|
|
3003
|
-
const encoded = encodeURIComponent(param.name);
|
|
3004
|
-
const needsEncoding = encoded !== unencoded;
|
|
3005
|
-
const name = needsEncoding ? encoded : unencoded;
|
|
3006
|
-
const flag = binary ? "--data-binary" : `--data${needsEncoding ? "-urlencode" : ""}`;
|
|
3007
|
-
push(`${flag} ${quote(`${name}=${param.value}`)}`);
|
|
3008
|
-
});
|
|
3009
|
-
} else {
|
|
3010
|
-
push(`${binary ? "--data-binary" : arg("data")} ${quote(postData.text)}`);
|
|
3011
|
-
}
|
|
3012
|
-
break;
|
|
3013
|
-
default:
|
|
3014
|
-
if (!postData.text) {
|
|
3015
|
-
break;
|
|
3016
|
-
}
|
|
3017
|
-
let builtPayload = false;
|
|
3018
|
-
if (isMimeTypeJSON(postData.mimeType)) {
|
|
3019
|
-
if (postData.text.length > 20) {
|
|
3020
|
-
try {
|
|
3021
|
-
const jsonPayload = JSON.parse(postData.text);
|
|
3022
|
-
builtPayload = true;
|
|
3023
|
-
if (postData.text.indexOf("'") > 0) {
|
|
3024
|
-
push(
|
|
3025
|
-
`${binary ? "--data-binary" : arg("data")} @- <<EOF
|
|
3026
|
-
${JSON.stringify(
|
|
3027
|
-
jsonPayload,
|
|
3028
|
-
null,
|
|
3029
|
-
indentJSON
|
|
3030
|
-
)}
|
|
3031
|
-
EOF`
|
|
3032
|
-
);
|
|
3033
|
-
} else {
|
|
3034
|
-
push(
|
|
3035
|
-
`${binary ? "--data-binary" : arg("data")} '
|
|
3036
|
-
${JSON.stringify(jsonPayload, null, indentJSON)}
|
|
3037
|
-
'`
|
|
3038
|
-
);
|
|
3039
|
-
}
|
|
3040
|
-
} catch (err) {
|
|
3041
|
-
}
|
|
3042
|
-
}
|
|
3043
|
-
}
|
|
3044
|
-
if (!builtPayload) {
|
|
3045
|
-
push(`${binary ? "--data-binary" : arg("data")} ${quote(postData.text)}`);
|
|
3046
|
-
}
|
|
3047
|
-
}
|
|
3048
|
-
return join();
|
|
3049
|
-
}
|
|
3050
|
-
};
|
|
3051
|
-
|
|
3052
|
-
// src/targets/shell/httpie/client.ts
|
|
3053
|
-
var httpie = {
|
|
3054
|
-
info: {
|
|
3055
|
-
key: "httpie",
|
|
3056
|
-
title: "HTTPie",
|
|
3057
|
-
link: "http://httpie.org/",
|
|
3058
|
-
description: "a CLI, cURL-like tool for humans"
|
|
3059
|
-
},
|
|
3060
|
-
convert: ({ allHeaders, postData, queryObj, fullUrl, method, url }, options) => {
|
|
3061
|
-
const opts = {
|
|
3062
|
-
body: false,
|
|
3063
|
-
cert: false,
|
|
3064
|
-
headers: false,
|
|
3065
|
-
indent: " ",
|
|
3066
|
-
pretty: false,
|
|
3067
|
-
print: false,
|
|
3068
|
-
queryParams: false,
|
|
3069
|
-
short: false,
|
|
3070
|
-
style: false,
|
|
3071
|
-
timeout: false,
|
|
3072
|
-
verbose: false,
|
|
3073
|
-
verify: false,
|
|
3074
|
-
...options
|
|
3075
|
-
};
|
|
3076
|
-
const { push, join, unshift } = new CodeBuilder({
|
|
3077
|
-
indent: opts.indent,
|
|
3078
|
-
// @ts-expect-error SEEMS LEGIT
|
|
3079
|
-
join: opts.indent !== false ? ` \\
|
|
3080
|
-
${opts.indent}` : " "
|
|
3081
|
-
});
|
|
3082
|
-
let raw = false;
|
|
3083
|
-
const flags = [];
|
|
3084
|
-
if (opts.headers) {
|
|
3085
|
-
flags.push(opts.short ? "-h" : "--headers");
|
|
3086
|
-
}
|
|
3087
|
-
if (opts.body) {
|
|
3088
|
-
flags.push(opts.short ? "-b" : "--body");
|
|
3089
|
-
}
|
|
3090
|
-
if (opts.verbose) {
|
|
3091
|
-
flags.push(opts.short ? "-v" : "--verbose");
|
|
3092
|
-
}
|
|
3093
|
-
if (opts.print) {
|
|
3094
|
-
flags.push(`${opts.short ? "-p" : "--print"}=${opts.print}`);
|
|
3095
|
-
}
|
|
3096
|
-
if (opts.verify) {
|
|
3097
|
-
flags.push(`--verify=${opts.verify}`);
|
|
3098
|
-
}
|
|
3099
|
-
if (opts.cert) {
|
|
3100
|
-
flags.push(`--cert=${opts.cert}`);
|
|
3101
|
-
}
|
|
3102
|
-
if (opts.pretty) {
|
|
3103
|
-
flags.push(`--pretty=${opts.pretty}`);
|
|
3104
|
-
}
|
|
3105
|
-
if (opts.style) {
|
|
3106
|
-
flags.push(`--style=${opts.style}`);
|
|
3107
|
-
}
|
|
3108
|
-
if (opts.timeout) {
|
|
3109
|
-
flags.push(`--timeout=${opts.timeout}`);
|
|
3110
|
-
}
|
|
3111
|
-
if (opts.queryParams) {
|
|
3112
|
-
Object.keys(queryObj).forEach((name) => {
|
|
3113
|
-
const value = queryObj[name];
|
|
3114
|
-
if (Array.isArray(value)) {
|
|
3115
|
-
value.forEach((val) => {
|
|
3116
|
-
push(`${name}==${quote(val)}`);
|
|
3117
|
-
});
|
|
3118
|
-
} else {
|
|
3119
|
-
push(`${name}==${quote(value)}`);
|
|
3120
|
-
}
|
|
3121
|
-
});
|
|
3122
|
-
}
|
|
3123
|
-
Object.keys(allHeaders).sort().forEach((key) => {
|
|
3124
|
-
push(`${key}:${quote(allHeaders[key])}`);
|
|
3125
|
-
});
|
|
3126
|
-
if (postData.mimeType === "application/x-www-form-urlencoded") {
|
|
3127
|
-
if (postData.params && postData.params.length) {
|
|
3128
|
-
flags.push(opts.short ? "-f" : "--form");
|
|
3129
|
-
postData.params.forEach((param) => {
|
|
3130
|
-
push(`${param.name}=${quote(param.value)}`);
|
|
3131
|
-
});
|
|
3132
|
-
}
|
|
3133
|
-
} else {
|
|
3134
|
-
raw = true;
|
|
3135
|
-
}
|
|
3136
|
-
const cliFlags = flags.length ? `${flags.join(" ")} ` : "";
|
|
3137
|
-
url = quote(opts.queryParams ? url : fullUrl);
|
|
3138
|
-
unshift(`http ${cliFlags}${method} ${url}`);
|
|
3139
|
-
if (raw && postData.text) {
|
|
3140
|
-
const postDataText = quote(postData.text);
|
|
3141
|
-
unshift(`echo ${postDataText} | `);
|
|
3142
|
-
}
|
|
3143
|
-
return join();
|
|
3144
|
-
}
|
|
3145
|
-
};
|
|
3146
|
-
|
|
3147
|
-
// src/targets/shell/wget/client.ts
|
|
3148
|
-
var wget = {
|
|
3149
|
-
info: {
|
|
3150
|
-
key: "wget",
|
|
3151
|
-
title: "Wget",
|
|
3152
|
-
link: "https://www.gnu.org/software/wget/",
|
|
3153
|
-
description: "a free software package for retrieving files using HTTP, HTTPS"
|
|
3154
|
-
},
|
|
3155
|
-
convert: ({ method, postData, allHeaders, fullUrl }, options) => {
|
|
3156
|
-
const opts = {
|
|
3157
|
-
indent: " ",
|
|
3158
|
-
short: false,
|
|
3159
|
-
verbose: false,
|
|
3160
|
-
...options
|
|
3161
|
-
};
|
|
3162
|
-
const { push, join } = new CodeBuilder({
|
|
3163
|
-
...typeof opts.indent === "string" ? { indent: opts.indent } : {},
|
|
3164
|
-
join: opts.indent !== false ? ` \\
|
|
3165
|
-
${opts.indent}` : " "
|
|
3166
|
-
});
|
|
3167
|
-
if (opts.verbose) {
|
|
3168
|
-
push(`wget ${opts.short ? "-v" : "--verbose"}`);
|
|
3169
|
-
} else {
|
|
3170
|
-
push(`wget ${opts.short ? "-q" : "--quiet"}`);
|
|
3171
|
-
}
|
|
3172
|
-
push(`--method ${quote(method)}`);
|
|
3173
|
-
Object.keys(allHeaders).forEach((key) => {
|
|
3174
|
-
const header = `${key}: ${allHeaders[key]}`;
|
|
3175
|
-
push(`--header ${quote(header)}`);
|
|
3176
|
-
});
|
|
3177
|
-
if (postData.text) {
|
|
3178
|
-
push(`--body-data ${escape(quote(postData.text))}`);
|
|
3179
|
-
}
|
|
3180
|
-
push(opts.short ? "-O" : "--output-document");
|
|
3181
|
-
push(`- ${quote(fullUrl)}`);
|
|
3182
|
-
return join();
|
|
3183
|
-
}
|
|
3184
|
-
};
|
|
3185
|
-
|
|
3186
|
-
// src/targets/shell/target.ts
|
|
3187
|
-
var shell = {
|
|
3188
|
-
info: {
|
|
3189
|
-
key: "shell",
|
|
3190
|
-
title: "Shell",
|
|
3191
|
-
extname: ".sh",
|
|
3192
|
-
default: "curl",
|
|
3193
|
-
cli: "%s"
|
|
3194
|
-
},
|
|
3195
|
-
clientsById: {
|
|
3196
|
-
curl: curl2,
|
|
3197
|
-
httpie,
|
|
3198
|
-
wget
|
|
3199
|
-
}
|
|
3200
|
-
};
|
|
3201
|
-
|
|
3202
|
-
// src/targets/swift/helpers.ts
|
|
3203
|
-
var buildString = (length, str) => str.repeat(length);
|
|
3204
|
-
var concatArray = (arr, pretty, indentation, indentLevel) => {
|
|
3205
|
-
const currentIndent = buildString(indentLevel, indentation);
|
|
3206
|
-
const closingBraceIndent = buildString(indentLevel - 1, indentation);
|
|
3207
|
-
const join = pretty ? `,
|
|
3208
|
-
${currentIndent}` : ", ";
|
|
3209
|
-
if (pretty) {
|
|
3210
|
-
return `[
|
|
3211
|
-
${currentIndent}${arr.join(join)}
|
|
3212
|
-
${closingBraceIndent}]`;
|
|
3213
|
-
}
|
|
3214
|
-
return `[${arr.join(join)}]`;
|
|
3215
|
-
};
|
|
3216
|
-
var literalDeclaration = (name, parameters, opts) => `let ${name} = ${literalRepresentation3(parameters, opts)}`;
|
|
3217
|
-
var literalRepresentation3 = (value, opts, indentLevel) => {
|
|
3218
|
-
indentLevel = indentLevel === void 0 ? 1 : indentLevel + 1;
|
|
3219
|
-
switch (Object.prototype.toString.call(value)) {
|
|
3220
|
-
case "[object Number]":
|
|
3221
|
-
return value;
|
|
3222
|
-
case "[object Array]": {
|
|
3223
|
-
let pretty = false;
|
|
3224
|
-
const valuesRepresentation = value.map((v) => {
|
|
3225
|
-
if (Object.prototype.toString.call(v) === "[object Object]") {
|
|
3226
|
-
pretty = Object.keys(v).length > 1;
|
|
3227
|
-
}
|
|
3228
|
-
return literalRepresentation3(v, opts, indentLevel);
|
|
3229
|
-
});
|
|
3230
|
-
return concatArray(valuesRepresentation, pretty, opts.indent, indentLevel);
|
|
3231
|
-
}
|
|
3232
|
-
case "[object Object]": {
|
|
3233
|
-
const keyValuePairs = [];
|
|
3234
|
-
for (const key in value) {
|
|
3235
|
-
keyValuePairs.push(`"${key}": ${literalRepresentation3(value[key], opts, indentLevel)}`);
|
|
3236
|
-
}
|
|
3237
|
-
return concatArray(
|
|
3238
|
-
keyValuePairs,
|
|
3239
|
-
// @ts-expect-error needs better types
|
|
3240
|
-
opts.pretty && keyValuePairs.length > 1,
|
|
3241
|
-
// @ts-expect-error needs better types
|
|
3242
|
-
opts.indent,
|
|
3243
|
-
indentLevel
|
|
3244
|
-
);
|
|
3245
|
-
}
|
|
3246
|
-
case "[object Boolean]":
|
|
3247
|
-
return value.toString();
|
|
3248
|
-
default:
|
|
3249
|
-
if (value === null || value === void 0) {
|
|
3250
|
-
return "";
|
|
3251
|
-
}
|
|
3252
|
-
return `"${value.toString().replace(/"/g, '\\"')}"`;
|
|
3253
|
-
}
|
|
3254
|
-
};
|
|
3255
|
-
|
|
3256
|
-
// src/targets/swift/nsurlsession/client.ts
|
|
3257
|
-
var nsurlsession2 = {
|
|
3258
|
-
info: {
|
|
3259
|
-
key: "nsurlsession",
|
|
3260
|
-
title: "NSURLSession",
|
|
3261
|
-
link: "https://developer.apple.com/library/mac/documentation/Foundation/Reference/NSURLSession_class/index.html",
|
|
3262
|
-
description: "Foundation's NSURLSession request"
|
|
3263
|
-
},
|
|
3264
|
-
convert: ({ allHeaders, postData, fullUrl, method }, options) => {
|
|
3265
|
-
const opts = {
|
|
3266
|
-
indent: " ",
|
|
3267
|
-
pretty: true,
|
|
3268
|
-
timeout: "10",
|
|
3269
|
-
...options
|
|
3270
|
-
};
|
|
3271
|
-
const { push, blank, join } = new CodeBuilder({ indent: opts.indent });
|
|
3272
|
-
const req = {
|
|
3273
|
-
hasHeaders: false,
|
|
3274
|
-
hasBody: false
|
|
3275
|
-
};
|
|
3276
|
-
push("import Foundation");
|
|
3277
|
-
if (Object.keys(allHeaders).length) {
|
|
3278
|
-
req.hasHeaders = true;
|
|
3279
|
-
blank();
|
|
3280
|
-
push(literalDeclaration("headers", allHeaders, opts));
|
|
3281
|
-
}
|
|
3282
|
-
if (postData.text || postData.jsonObj || postData.params) {
|
|
3283
|
-
req.hasBody = true;
|
|
3284
|
-
switch (postData.mimeType) {
|
|
3285
|
-
case "application/x-www-form-urlencoded":
|
|
3286
|
-
blank();
|
|
3287
|
-
if (postData.params?.length) {
|
|
3288
|
-
const [head, ...tail] = postData.params;
|
|
3289
|
-
push(`let postData = NSMutableData(data: "${head.name}=${head.value}".data(using: String.Encoding.utf8)!)`);
|
|
3290
|
-
tail.forEach(({ name, value }) => {
|
|
3291
|
-
push(`postData.append("&${name}=${value}".data(using: String.Encoding.utf8)!)`);
|
|
3292
|
-
});
|
|
3293
|
-
} else {
|
|
3294
|
-
req.hasBody = false;
|
|
3295
|
-
}
|
|
3296
|
-
break;
|
|
3297
|
-
case "application/json":
|
|
3298
|
-
if (postData.jsonObj) {
|
|
3299
|
-
push(`${literalDeclaration("parameters", postData.jsonObj, opts)} as [String : Any]`);
|
|
3300
|
-
blank();
|
|
3301
|
-
push("let postData = JSONSerialization.data(withJSONObject: parameters, options: [])");
|
|
3302
|
-
}
|
|
3303
|
-
break;
|
|
3304
|
-
case "multipart/form-data":
|
|
3305
|
-
push(literalDeclaration("parameters", postData.params, opts));
|
|
3306
|
-
blank();
|
|
3307
|
-
push(`let boundary = "${postData.boundary}"`);
|
|
3308
|
-
blank();
|
|
3309
|
-
push('var body = ""');
|
|
3310
|
-
push("var error: NSError? = nil");
|
|
3311
|
-
push("for param in parameters {");
|
|
3312
|
-
push('let paramName = param["name"]!', 1);
|
|
3313
|
-
push('body += "--\\(boundary)\\r\\n"', 1);
|
|
3314
|
-
push('body += "Content-Disposition:form-data; name=\\"\\(paramName)\\""', 1);
|
|
3315
|
-
push('if let filename = param["fileName"] {', 1);
|
|
3316
|
-
push('let contentType = param["content-type"]!', 2);
|
|
3317
|
-
push("let fileContent = String(contentsOfFile: filename, encoding: String.Encoding.utf8)", 2);
|
|
3318
|
-
push("if (error != nil) {", 2);
|
|
3319
|
-
push("print(error as Any)", 3);
|
|
3320
|
-
push("}", 2);
|
|
3321
|
-
push('body += "; filename=\\"\\(filename)\\"\\r\\n"', 2);
|
|
3322
|
-
push('body += "Content-Type: \\(contentType)\\r\\n\\r\\n"', 2);
|
|
3323
|
-
push("body += fileContent", 2);
|
|
3324
|
-
push('} else if let paramValue = param["value"] {', 1);
|
|
3325
|
-
push('body += "\\r\\n\\r\\n\\(paramValue)"', 2);
|
|
3326
|
-
push("}", 1);
|
|
3327
|
-
push("}");
|
|
3328
|
-
break;
|
|
3329
|
-
default:
|
|
3330
|
-
blank();
|
|
3331
|
-
push(`let postData = NSData(data: "${postData.text}".data(using: String.Encoding.utf8)!)`);
|
|
3332
|
-
}
|
|
3333
|
-
}
|
|
3334
|
-
blank();
|
|
3335
|
-
push(`let request = NSMutableURLRequest(url: NSURL(string: "${fullUrl}")! as URL,`);
|
|
3336
|
-
push(" cachePolicy: .useProtocolCachePolicy,");
|
|
3337
|
-
push(
|
|
3338
|
-
// @ts-expect-error needs better types
|
|
3339
|
-
` timeoutInterval: ${parseInt(opts.timeout, 10).toFixed(1)})`
|
|
3340
|
-
);
|
|
3341
|
-
push(`request.httpMethod = "${method}"`);
|
|
3342
|
-
if (req.hasHeaders) {
|
|
3343
|
-
push("request.allHTTPHeaderFields = headers");
|
|
3344
|
-
}
|
|
3345
|
-
if (req.hasBody) {
|
|
3346
|
-
push("request.httpBody = postData as Data");
|
|
3347
|
-
}
|
|
3348
|
-
blank();
|
|
3349
|
-
push("let session = URLSession.shared");
|
|
3350
|
-
push(
|
|
3351
|
-
"let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in"
|
|
3352
|
-
);
|
|
3353
|
-
push("if (error != nil) {", 1);
|
|
3354
|
-
push("print(error as Any)", 2);
|
|
3355
|
-
push("} else {", 1);
|
|
3356
|
-
push("let httpResponse = response as? HTTPURLResponse", 2);
|
|
3357
|
-
push("print(httpResponse)", 2);
|
|
3358
|
-
push("}", 1);
|
|
3359
|
-
push("})");
|
|
3360
|
-
blank();
|
|
3361
|
-
push("dataTask.resume()");
|
|
3362
|
-
return join();
|
|
3363
|
-
}
|
|
3364
|
-
};
|
|
3365
|
-
|
|
3366
|
-
// src/targets/swift/target.ts
|
|
3367
|
-
var swift = {
|
|
3368
|
-
info: {
|
|
3369
|
-
key: "swift",
|
|
3370
|
-
title: "Swift",
|
|
3371
|
-
extname: ".swift",
|
|
3372
|
-
default: "nsurlsession"
|
|
3373
|
-
},
|
|
3374
|
-
clientsById: {
|
|
3375
|
-
nsurlsession: nsurlsession2
|
|
3376
|
-
}
|
|
3377
|
-
};
|
|
3378
|
-
|
|
3379
|
-
// src/targets/index.ts
|
|
3380
|
-
var targets = {
|
|
3381
|
-
c,
|
|
3382
|
-
clojure,
|
|
3383
|
-
csharp,
|
|
3384
|
-
go,
|
|
3385
|
-
http,
|
|
3386
|
-
java,
|
|
3387
|
-
javascript,
|
|
3388
|
-
json,
|
|
3389
|
-
kotlin,
|
|
3390
|
-
node,
|
|
3391
|
-
objc,
|
|
3392
|
-
ocaml,
|
|
3393
|
-
php,
|
|
3394
|
-
powershell,
|
|
3395
|
-
python,
|
|
3396
|
-
r,
|
|
3397
|
-
ruby,
|
|
3398
|
-
shell,
|
|
3399
|
-
swift
|
|
3400
|
-
};
|
|
3401
|
-
var isTarget = (target) => {
|
|
3402
|
-
if (typeof target !== "object" || target === null || Array.isArray(target)) {
|
|
3403
|
-
const got = target === null ? "null" : Array.isArray(target) ? "array" : typeof target;
|
|
3404
|
-
throw new Error(`you tried to add a target which is not an object, got type: "${got}"`);
|
|
3405
|
-
}
|
|
3406
|
-
if (!Object.prototype.hasOwnProperty.call(target, "info")) {
|
|
3407
|
-
throw new Error("targets must contain an `info` object");
|
|
3408
|
-
}
|
|
3409
|
-
if (!Object.prototype.hasOwnProperty.call(target.info, "key")) {
|
|
3410
|
-
throw new Error("targets must have an `info` object with the property `key`");
|
|
3411
|
-
}
|
|
3412
|
-
if (!target.info.key) {
|
|
3413
|
-
throw new Error("target key must be a unique string");
|
|
3414
|
-
}
|
|
3415
|
-
if (Object.prototype.hasOwnProperty.call(targets, target.info.key)) {
|
|
3416
|
-
throw new Error(`a target already exists with this key, \`${target.info.key}\``);
|
|
3417
|
-
}
|
|
3418
|
-
if (!Object.prototype.hasOwnProperty.call(target.info, "title")) {
|
|
3419
|
-
throw new Error("targets must have an `info` object with the property `title`");
|
|
3420
|
-
}
|
|
3421
|
-
if (!target.info.title) {
|
|
3422
|
-
throw new Error("target title must be a non-zero-length string");
|
|
3423
|
-
}
|
|
3424
|
-
if (!Object.prototype.hasOwnProperty.call(target.info, "extname")) {
|
|
3425
|
-
throw new Error("targets must have an `info` object with the property `extname`");
|
|
3426
|
-
}
|
|
3427
|
-
if (!Object.prototype.hasOwnProperty.call(target, "clientsById") || !target.clientsById || Object.keys(target.clientsById).length === 0) {
|
|
3428
|
-
throw new Error(
|
|
3429
|
-
`No clients provided in target ${target.info.key}. You must provide the property \`clientsById\` containg your clients.`
|
|
3430
|
-
);
|
|
3431
|
-
}
|
|
3432
|
-
if (!Object.prototype.hasOwnProperty.call(target.info, "default")) {
|
|
3433
|
-
throw new Error("targets must have an `info` object with the property `default`");
|
|
3434
|
-
}
|
|
3435
|
-
if (!Object.prototype.hasOwnProperty.call(target.clientsById, target.info.default)) {
|
|
3436
|
-
throw new Error(
|
|
3437
|
-
`target ${target.info.key} is configured with a default client ${target.info.default}, but no such client was found in the property \`clientsById\` (found ${JSON.stringify(
|
|
3438
|
-
Object.keys(target.clientsById)
|
|
3439
|
-
)})`
|
|
3440
|
-
);
|
|
3441
|
-
}
|
|
3442
|
-
Object.values(target.clientsById).forEach(isClient);
|
|
3443
|
-
return true;
|
|
3444
|
-
};
|
|
3445
|
-
var addTarget = (target) => {
|
|
3446
|
-
if (!isTarget(target)) ;
|
|
3447
|
-
targets[target.info.key] = target;
|
|
3448
|
-
};
|
|
3449
|
-
var isClient = (client) => {
|
|
3450
|
-
if (!client) {
|
|
3451
|
-
throw new Error("clients must be objects");
|
|
3452
|
-
}
|
|
3453
|
-
if (!Object.prototype.hasOwnProperty.call(client, "info")) {
|
|
3454
|
-
throw new Error("targets client must contain an `info` object");
|
|
3455
|
-
}
|
|
3456
|
-
if (!Object.prototype.hasOwnProperty.call(client.info, "key")) {
|
|
3457
|
-
throw new Error("targets client must have an `info` object with property `key`");
|
|
3458
|
-
}
|
|
3459
|
-
if (!client.info.key) {
|
|
3460
|
-
throw new Error("client.info.key must contain an identifier unique to this target");
|
|
3461
|
-
}
|
|
3462
|
-
if (!Object.prototype.hasOwnProperty.call(client.info, "title")) {
|
|
3463
|
-
throw new Error("targets client must have an `info` object with property `title`");
|
|
3464
|
-
}
|
|
3465
|
-
if (!Object.prototype.hasOwnProperty.call(client.info, "description")) {
|
|
3466
|
-
throw new Error("targets client must have an `info` object with property `description`");
|
|
3467
|
-
}
|
|
3468
|
-
if (!Object.prototype.hasOwnProperty.call(client.info, "link")) {
|
|
3469
|
-
throw new Error("targets client must have an `info` object with property `link`");
|
|
3470
|
-
}
|
|
3471
|
-
if (!Object.prototype.hasOwnProperty.call(client, "convert") || typeof client.convert !== "function") {
|
|
3472
|
-
throw new Error("targets client must have a `convert` property containing a conversion function");
|
|
3473
|
-
}
|
|
3474
|
-
return true;
|
|
3475
|
-
};
|
|
3476
|
-
var addTargetClient = (targetId, client) => {
|
|
3477
|
-
if (!isClient(client)) ;
|
|
3478
|
-
if (!Object.prototype.hasOwnProperty.call(targets, targetId)) {
|
|
3479
|
-
throw new Error(`Sorry, but no ${targetId} target exists to add clients to`);
|
|
3480
|
-
}
|
|
3481
|
-
if (Object.prototype.hasOwnProperty.call(targets[targetId], client.info.key)) {
|
|
3482
|
-
throw new Error(
|
|
3483
|
-
`the target ${targetId} already has a client with the key ${client.info.key}, please use a different key`
|
|
3484
|
-
);
|
|
3485
|
-
}
|
|
3486
|
-
targets[targetId].clientsById[client.info.key] = client;
|
|
3487
|
-
};
|
|
3488
|
-
|
|
3489
|
-
exports.addTarget = addTarget;
|
|
3490
|
-
exports.addTargetClient = addTargetClient;
|
|
3491
|
-
exports.isClient = isClient;
|
|
3492
|
-
exports.isTarget = isTarget;
|
|
3493
|
-
exports.targets = targets;
|
|
1
|
+
export { addTarget, addTargetClient, isClient, isTarget, targets } from '../chunk-IAWYVBVW.js';
|
|
2
|
+
import '../chunk-Y7NI4MMY.js';
|
|
3494
3
|
//# sourceMappingURL=out.js.map
|
|
3495
4
|
//# sourceMappingURL=index.js.map
|