@readme/httpsnippet 5.1.1 → 6.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (93) hide show
  1. package/README.md +5 -3
  2. package/dist/helpers/escape.d.ts +46 -0
  3. package/dist/helpers/escape.js +101 -0
  4. package/dist/index.d.ts +11 -11
  5. package/dist/targets/c/libcurl/client.js +3 -1
  6. package/dist/targets/c/target.js +1 -0
  7. package/dist/targets/csharp/httpclient/client.js +2 -1
  8. package/dist/targets/csharp/restsharp/client.js +65 -11
  9. package/dist/targets/csharp/target.js +1 -0
  10. package/dist/targets/go/native/client.d.ts +1 -1
  11. package/dist/targets/go/native/client.js +22 -32
  12. package/dist/targets/go/target.js +1 -0
  13. package/dist/targets/java/asynchttp/client.js +2 -1
  14. package/dist/targets/java/nethttp/client.js +2 -1
  15. package/dist/targets/java/okhttp/client.js +2 -1
  16. package/dist/targets/java/unirest/client.js +2 -1
  17. package/dist/targets/javascript/axios/fixtures/application-json.js +1 -1
  18. package/dist/targets/javascript/axios/fixtures/headers.js +6 -1
  19. package/dist/targets/javascript/fetch/fixtures/application-form-encoded.d.ts +3 -3
  20. package/dist/targets/javascript/fetch/fixtures/application-json.d.ts +3 -3
  21. package/dist/targets/javascript/fetch/fixtures/application-json.js +1 -1
  22. package/dist/targets/javascript/fetch/fixtures/cookies.d.ts +2 -2
  23. package/dist/targets/javascript/fetch/fixtures/custom-method.d.ts +1 -1
  24. package/dist/targets/javascript/fetch/fixtures/full.d.ts +3 -3
  25. package/dist/targets/javascript/fetch/fixtures/headers.d.ts +3 -2
  26. package/dist/targets/javascript/fetch/fixtures/headers.js +6 -1
  27. package/dist/targets/javascript/fetch/fixtures/http-insecure.d.ts +1 -1
  28. package/dist/targets/javascript/fetch/fixtures/jsonObj-multiline.d.ts +3 -3
  29. package/dist/targets/javascript/fetch/fixtures/jsonObj-null-value.d.ts +3 -3
  30. package/dist/targets/javascript/fetch/fixtures/multipart-file.d.ts +1 -1
  31. package/dist/targets/javascript/fetch/fixtures/multipart-form-data-no-params.d.ts +2 -2
  32. package/dist/targets/javascript/fetch/fixtures/multipart-form-data.d.ts +1 -1
  33. package/dist/targets/javascript/fetch/fixtures/nested.d.ts +1 -1
  34. package/dist/targets/javascript/fetch/fixtures/postdata-malformed.d.ts +2 -2
  35. package/dist/targets/javascript/fetch/fixtures/query-encoded.d.ts +1 -1
  36. package/dist/targets/javascript/fetch/fixtures/query.d.ts +1 -1
  37. package/dist/targets/javascript/fetch/fixtures/short.d.ts +1 -1
  38. package/dist/targets/javascript/fetch/fixtures/text-plain.d.ts +3 -3
  39. package/dist/targets/javascript/jquery/client.js +1 -1
  40. package/dist/targets/javascript/jquery/fixtures/application-form-encoded.d.ts +7 -7
  41. package/dist/targets/javascript/jquery/fixtures/application-json.d.ts +7 -7
  42. package/dist/targets/javascript/jquery/fixtures/application-json.js +1 -1
  43. package/dist/targets/javascript/jquery/fixtures/cookies.d.ts +5 -5
  44. package/dist/targets/javascript/jquery/fixtures/custom-method.d.ts +5 -5
  45. package/dist/targets/javascript/jquery/fixtures/full.d.ts +6 -6
  46. package/dist/targets/javascript/jquery/fixtures/headers.d.ts +6 -5
  47. package/dist/targets/javascript/jquery/fixtures/headers.js +2 -1
  48. package/dist/targets/javascript/jquery/fixtures/http-insecure.d.ts +5 -5
  49. package/dist/targets/javascript/jquery/fixtures/jsonObj-multiline.d.ts +7 -7
  50. package/dist/targets/javascript/jquery/fixtures/jsonObj-null-value.d.ts +7 -7
  51. package/dist/targets/javascript/jquery/fixtures/multipart-data.d.ts +9 -9
  52. package/dist/targets/javascript/jquery/fixtures/multipart-file.d.ts +9 -9
  53. package/dist/targets/javascript/jquery/fixtures/multipart-form-data-no-params.d.ts +5 -5
  54. package/dist/targets/javascript/jquery/fixtures/multipart-form-data.d.ts +9 -9
  55. package/dist/targets/javascript/jquery/fixtures/nested.d.ts +5 -5
  56. package/dist/targets/javascript/jquery/fixtures/postdata-malformed.d.ts +5 -5
  57. package/dist/targets/javascript/jquery/fixtures/query-encoded.d.ts +5 -5
  58. package/dist/targets/javascript/jquery/fixtures/query.d.ts +5 -5
  59. package/dist/targets/javascript/jquery/fixtures/short.d.ts +5 -5
  60. package/dist/targets/javascript/jquery/fixtures/text-plain.d.ts +6 -6
  61. package/dist/targets/javascript/xhr/client.js +5 -3
  62. package/dist/targets/javascript/xhr/fixtures/application-json.js +1 -1
  63. package/dist/targets/javascript/xhr/fixtures/headers.js +1 -0
  64. package/dist/targets/kotlin/okhttp/client.js +2 -1
  65. package/dist/targets/node/axios/fixtures/application-json.js +1 -1
  66. package/dist/targets/node/axios/fixtures/headers.js +6 -1
  67. package/dist/targets/node/fetch/fixtures/application-json.js +1 -1
  68. package/dist/targets/node/fetch/fixtures/headers.js +6 -1
  69. package/dist/targets/node/native/client.js +5 -15
  70. package/dist/targets/node/native/fixtures/application-json.js +1 -1
  71. package/dist/targets/node/native/fixtures/headers.js +2 -1
  72. package/dist/targets/node/request/fixtures/application-json.js +1 -1
  73. package/dist/targets/node/request/fixtures/headers.js +6 -1
  74. package/dist/targets/node/unirest/fixtures/application-json.js +1 -1
  75. package/dist/targets/node/unirest/fixtures/headers.js +2 -1
  76. package/dist/targets/objc/nsurlsession/client.js +4 -3
  77. package/dist/targets/ocaml/cohttp/client.js +3 -2
  78. package/dist/targets/php/curl/client.d.ts +1 -1
  79. package/dist/targets/php/curl/client.js +20 -24
  80. package/dist/targets/php/guzzle/client.js +7 -5
  81. package/dist/targets/php/helpers.js +4 -1
  82. package/dist/targets/php/http1/client.js +15 -20
  83. package/dist/targets/php/http2/client.js +19 -23
  84. package/dist/targets/powershell/common.js +3 -2
  85. package/dist/targets/python/helpers.js +6 -12
  86. package/dist/targets/python/requests/client.js +13 -12
  87. package/dist/targets/r/httr/client.js +40 -26
  88. package/dist/targets/ruby/native/client.js +2 -4
  89. package/dist/targets/shell/curl/client.d.ts +2 -2
  90. package/dist/targets/shell/curl/client.js +12 -10
  91. package/dist/targets/swift/nsurlsession/client.js +4 -3
  92. package/dist/targets/targets.d.ts +7 -7
  93. package/package.json +12 -12
package/README.md CHANGED
@@ -160,13 +160,15 @@ There are some major differences between this library and the [httpsnippet](http
160
160
  * Does not do any HAR schema validation. It's just assumed that the HAR you're supplying to the library is already valid.
161
161
  * The main `HTTPSnippet` export contains an `options` argument for an `harIsAlreadyEncoded` option for disabling [escaping](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent) of cookies and query strings in URLs.
162
162
  * We added this because all HARs that we interact with already have this data escaped and this option prevents them from being double encoded, thus corrupting the data.
163
+ * Does not support the `insecureSkipVerify` option on `go:native`, `node:native`, `ruby:native`, and `shell:curl` as we don't want snippets generated for our users to bypass SSL certificate verification.
163
164
  * Node
164
165
  * `fetch`
165
- * Body payloads are treated as an object literal and wrapped within `JSON.stringify()`. We do this to keep those targets looking nicer with those kinds of payloads. This also applies to the JS `fetch` target as wel.
166
+ * Body payloads are treated as an object literal and wrapped within `JSON.stringify()`. We do this to keep those targets looking nicer with those kinds of payloads. This also applies to the JS `fetch` target as well.
166
167
  * `request`
167
168
  * Does not provide query string parameters in a `params` argument due to complexities with query encoding.
168
- * PHP → `guzzle`
169
- * Snippets have `require_once('vendor/autoload.php');` prefixed at the top.
169
+ * PHP
170
+ * `guzzle`
171
+ * Snippets have `require_once('vendor/autoload.php');` prefixed at the top.
170
172
  * Python
171
173
  * `python3`
172
174
  * Does not ship this client due to its incompatibility with being able to support file uploads.
@@ -0,0 +1,46 @@
1
+ export interface EscapeOptions {
2
+ /**
3
+ * The delimiter that will be used to wrap the string (and so must be escaped
4
+ * when used within the string).
5
+ * Defaults to "
6
+ */
7
+ delimiter?: string;
8
+ /**
9
+ * The char to use to escape the delimiter and other special characters.
10
+ * Defaults to \
11
+ */
12
+ escapeChar?: string;
13
+ /**
14
+ * Whether newlines (\n and \r) should be escaped within the string.
15
+ * Defaults to true.
16
+ */
17
+ escapeNewlines?: boolean;
18
+ }
19
+ /**
20
+ * Escape characters within a value to make it safe to insert directly into a
21
+ * snippet. Takes options which define the escape requirements.
22
+ *
23
+ * This is closely based on the JSON-stringify string serialization algorithm,
24
+ * but generalized for other string delimiters (e.g. " or ') and different escape
25
+ * characters (e.g. Powershell uses `)
26
+ *
27
+ * See https://tc39.es/ecma262/multipage/structured-data.html#sec-quotejsonstring
28
+ * for the complete original algorithm.
29
+ */
30
+ export declare function escapeString(rawValue: any, options?: EscapeOptions): string;
31
+ /**
32
+ * Make a string value safe to insert literally into a snippet within single quotes,
33
+ * by escaping problematic characters, including single quotes inside the string,
34
+ * backslashes, newlines, and other special characters.
35
+ *
36
+ * If value is not a string, it will be stringified with .toString() first.
37
+ */
38
+ export declare const escapeForSingleQuotes: (value: any) => string;
39
+ /**
40
+ * Make a string value safe to insert literally into a snippet within double quotes,
41
+ * by escaping problematic characters, including double quotes inside the string,
42
+ * backslashes, newlines, and other special characters.
43
+ *
44
+ * If value is not a string, it will be stringified with .toString() first.
45
+ */
46
+ export declare const escapeForDoubleQuotes: (value: any) => string;
@@ -0,0 +1,101 @@
1
+ "use strict";
2
+ var __read = (this && this.__read) || function (o, n) {
3
+ var m = typeof Symbol === "function" && o[Symbol.iterator];
4
+ if (!m) return o;
5
+ var i = m.call(o), r, ar = [], e;
6
+ try {
7
+ while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
8
+ }
9
+ catch (error) { e = { error: error }; }
10
+ finally {
11
+ try {
12
+ if (r && !r.done && (m = i["return"])) m.call(i);
13
+ }
14
+ finally { if (e) throw e.error; }
15
+ }
16
+ return ar;
17
+ };
18
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
19
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
20
+ if (ar || !(i in from)) {
21
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
22
+ ar[i] = from[i];
23
+ }
24
+ }
25
+ return to.concat(ar || Array.prototype.slice.call(from));
26
+ };
27
+ Object.defineProperty(exports, "__esModule", { value: true });
28
+ exports.escapeForDoubleQuotes = exports.escapeForSingleQuotes = exports.escapeString = void 0;
29
+ /**
30
+ * Escape characters within a value to make it safe to insert directly into a
31
+ * snippet. Takes options which define the escape requirements.
32
+ *
33
+ * This is closely based on the JSON-stringify string serialization algorithm,
34
+ * but generalized for other string delimiters (e.g. " or ') and different escape
35
+ * characters (e.g. Powershell uses `)
36
+ *
37
+ * See https://tc39.es/ecma262/multipage/structured-data.html#sec-quotejsonstring
38
+ * for the complete original algorithm.
39
+ */
40
+ function escapeString(rawValue, options) {
41
+ if (options === void 0) { options = {}; }
42
+ var _a = options.delimiter, delimiter = _a === void 0 ? '"' : _a, _b = options.escapeChar, escapeChar = _b === void 0 ? '\\' : _b, _c = options.escapeNewlines, escapeNewlines = _c === void 0 ? true : _c;
43
+ var stringValue = rawValue.toString();
44
+ return __spreadArray([], __read(stringValue), false).map(function (c) {
45
+ if (c === '\b') {
46
+ return "".concat(escapeChar, "b");
47
+ }
48
+ else if (c === '\t') {
49
+ return "".concat(escapeChar, "t");
50
+ }
51
+ else if (c === '\n') {
52
+ if (escapeNewlines) {
53
+ return "".concat(escapeChar, "n");
54
+ }
55
+ return c; // Don't just continue, or this is caught by < \u0020
56
+ }
57
+ else if (c === '\f') {
58
+ return "".concat(escapeChar, "f");
59
+ }
60
+ else if (c === '\r') {
61
+ if (escapeNewlines) {
62
+ return "".concat(escapeChar, "r");
63
+ }
64
+ return c; // Don't just continue, or this is caught by < \u0020
65
+ }
66
+ else if (c === escapeChar) {
67
+ return escapeChar + escapeChar;
68
+ }
69
+ else if (c === delimiter) {
70
+ return escapeChar + delimiter;
71
+ }
72
+ else if (c < '\u0020' || c > '\u007E') {
73
+ // Delegate the trickier non-ASCII cases to the normal algorithm. Some of these
74
+ // are escaped as \uXXXX, whilst others are represented literally. Since we're
75
+ // using this primarily for header values that are generally (though not 100%
76
+ // strictly?) ASCII-only, this should almost never happen.
77
+ return JSON.stringify(c).slice(1, -1);
78
+ }
79
+ return c;
80
+ })
81
+ .join('');
82
+ }
83
+ exports.escapeString = escapeString;
84
+ /**
85
+ * Make a string value safe to insert literally into a snippet within single quotes,
86
+ * by escaping problematic characters, including single quotes inside the string,
87
+ * backslashes, newlines, and other special characters.
88
+ *
89
+ * If value is not a string, it will be stringified with .toString() first.
90
+ */
91
+ var escapeForSingleQuotes = function (value) { return escapeString(value, { delimiter: "'" }); };
92
+ exports.escapeForSingleQuotes = escapeForSingleQuotes;
93
+ /**
94
+ * Make a string value safe to insert literally into a snippet within double quotes,
95
+ * by escaping problematic characters, including double quotes inside the string,
96
+ * backslashes, newlines, and other special characters.
97
+ *
98
+ * If value is not a string, it will be stringified with .toString() first.
99
+ */
100
+ var escapeForDoubleQuotes = function (value) { return escapeString(value, { delimiter: '"' }); };
101
+ exports.escapeForDoubleQuotes = escapeForDoubleQuotes;
package/dist/index.d.ts CHANGED
@@ -12,24 +12,24 @@ export { addTarget, addTargetClient } from './targets/targets';
12
12
  * Then, in addition to that, it really adds to complexity with TypeScript (TypeScript takes this constraint very very seriously) in a way that's not actually super useful. So, we treat this object as though it could have both or either of `params` and/or `text`.
13
13
  */
14
14
  type PostDataBase = PostDataCommon & {
15
- text?: string;
16
15
  params?: Param[];
16
+ text?: string;
17
17
  };
18
18
  export type HarRequest = Omit<NpmHarRequest, 'postData'> & {
19
19
  postData: PostDataBase;
20
20
  };
21
21
  export interface RequestExtras {
22
+ allHeaders: ReducedHelperObject;
23
+ cookiesObj: ReducedHelperObject;
24
+ fullUrl: string;
25
+ headersObj: ReducedHelperObject;
22
26
  postData: PostDataBase & {
27
+ boundary?: string;
23
28
  jsonObj?: ReducedHelperObject;
24
29
  paramsObj?: ReducedHelperObject;
25
- boundary?: string;
26
30
  };
27
- fullUrl: string;
28
31
  queryObj: ReducedHelperObject;
29
- headersObj: ReducedHelperObject;
30
32
  uriObj: UrlWithParsedQuery;
31
- cookiesObj: ReducedHelperObject;
32
- allHeaders: ReducedHelperObject;
33
33
  }
34
34
  export type Request = HarRequest & RequestExtras;
35
35
  interface Entry {
@@ -37,12 +37,12 @@ interface Entry {
37
37
  }
38
38
  interface HarEntry {
39
39
  log: {
40
- version: string;
41
40
  creator: {
42
41
  name: string;
43
42
  version: string;
44
43
  };
45
44
  entries: Entry[];
45
+ version: string;
46
46
  };
47
47
  }
48
48
  export interface HTTPSnippetOptions {
@@ -80,16 +80,16 @@ export declare class HTTPSnippet {
80
80
  bodySize: number;
81
81
  comment?: string | undefined;
82
82
  postData: PostDataCommon & {
83
- text?: string | undefined;
84
83
  params?: Param[] | undefined;
84
+ text?: string | undefined;
85
85
  } & {
86
+ boundary?: string | undefined;
86
87
  jsonObj?: ReducedHelperObject | undefined;
87
88
  paramsObj?: ReducedHelperObject | undefined;
88
- boundary?: string | undefined;
89
89
  };
90
- queryObj: ReducedHelperObject;
91
- headersObj: ReducedHelperObject;
92
90
  cookiesObj: ReducedHelperObject;
91
+ headersObj: ReducedHelperObject;
92
+ queryObj: ReducedHelperObject;
93
93
  };
94
94
  convert: (targetId: TargetId, clientId?: ClientId, options?: any) => string | false | string[];
95
95
  }
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.libcurl = void 0;
4
4
  var code_builder_1 = require("../../../helpers/code-builder");
5
+ var escape_1 = require("../../../helpers/escape");
5
6
  exports.libcurl = {
6
7
  info: {
7
8
  key: 'libcurl',
@@ -15,6 +16,7 @@ exports.libcurl = {
15
16
  push('CURL *hnd = curl_easy_init();');
16
17
  blank();
17
18
  push("curl_easy_setopt(hnd, CURLOPT_CUSTOMREQUEST, \"".concat(method.toUpperCase(), "\");"));
19
+ push('curl_easy_setopt(hnd, CURLOPT_WRITEDATA, stdout);');
18
20
  push("curl_easy_setopt(hnd, CURLOPT_URL, \"".concat(fullUrl, "\");"));
19
21
  // Add headers, including the cookies
20
22
  var headers = Object.keys(headersObj);
@@ -23,7 +25,7 @@ exports.libcurl = {
23
25
  blank();
24
26
  push('struct curl_slist *headers = NULL;');
25
27
  headers.forEach(function (header) {
26
- push("headers = curl_slist_append(headers, \"".concat(header, ": ").concat(headersObj[header], "\");"));
28
+ push("headers = curl_slist_append(headers, \"".concat(header, ": ").concat((0, escape_1.escapeForDoubleQuotes)(headersObj[header]), "\");"));
27
29
  });
28
30
  push('curl_easy_setopt(hnd, CURLOPT_HTTPHEADER, headers);');
29
31
  }
@@ -8,6 +8,7 @@ exports.c = {
8
8
  title: 'C',
9
9
  extname: '.c',
10
10
  default: 'libcurl',
11
+ cli: 'c',
11
12
  },
12
13
  clientsById: {
13
14
  libcurl: client_1.libcurl,
@@ -13,6 +13,7 @@ var __assign = (this && this.__assign) || function () {
13
13
  Object.defineProperty(exports, "__esModule", { value: true });
14
14
  exports.httpclient = void 0;
15
15
  var code_builder_1 = require("../../../helpers/code-builder");
16
+ var escape_1 = require("../../../helpers/escape");
16
17
  var headers_1 = require("../../../helpers/headers");
17
18
  var getDecompressionMethods = function (allHeaders) {
18
19
  var acceptEncodings = (0, headers_1.getHeader)(allHeaders, 'accept-encoding');
@@ -100,7 +101,7 @@ exports.httpclient = {
100
101
  push('Headers =', 1);
101
102
  push('{', 1);
102
103
  headers.forEach(function (key) {
103
- push("{ \"".concat(String(key), "\", \"").concat(allHeaders[key], "\" },"), 2);
104
+ push("{ \"".concat(key, "\", \"").concat((0, escape_1.escapeForDoubleQuotes)(allHeaders[key]), "\" },"), 2);
104
105
  });
105
106
  push('},', 1);
106
107
  }
@@ -2,7 +2,10 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.restsharp = void 0;
4
4
  var code_builder_1 = require("../../../helpers/code-builder");
5
- var headers_1 = require("../../../helpers/headers");
5
+ var escape_1 = require("../../../helpers/escape");
6
+ function title(s) {
7
+ return s[0].toUpperCase() + s.slice(1).toLowerCase();
8
+ }
6
9
  exports.restsharp = {
7
10
  info: {
8
11
  key: 'restsharp',
@@ -11,28 +14,79 @@ exports.restsharp = {
11
14
  description: 'Simple REST and HTTP API Client for .NET',
12
15
  },
13
16
  convert: function (_a) {
14
- var allHeaders = _a.allHeaders, method = _a.method, fullUrl = _a.fullUrl, headersObj = _a.headersObj, cookies = _a.cookies, postData = _a.postData;
17
+ var method = _a.method, fullUrl = _a.fullUrl, headersObj = _a.headersObj, cookies = _a.cookies, postData = _a.postData, uriObj = _a.uriObj;
15
18
  var _b = new code_builder_1.CodeBuilder(), push = _b.push, join = _b.join;
16
19
  var isSupportedMethod = ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'HEAD', 'OPTIONS'].includes(method.toUpperCase());
17
20
  if (!isSupportedMethod) {
18
21
  return 'Method not supported';
19
22
  }
20
- push("var client = new RestClient(\"".concat(fullUrl, "\");"));
21
- push("var request = new RestRequest(Method.".concat(method.toUpperCase(), ");"));
23
+ push('using RestSharp;\n\n');
24
+ push("var options = new RestClientOptions(\"".concat(fullUrl, "\");"));
25
+ push('var client = new RestClient(options);');
26
+ // The first argument is the sub-path to the base URL, given as the
27
+ // constructor to RestClient; for our purposes we're just giving the entire
28
+ // URL as the base path so it can be an empty string
29
+ push('var request = new RestRequest("");');
30
+ // If we have multipart form data, set this value. Setting the content-type header manually and then trying to add a mutlipart file parameter
31
+ var isMultipart = postData.mimeType && postData.mimeType === 'multipart/form-data';
32
+ if (isMultipart) {
33
+ push('request.AlwaysMultipartFormData = true;');
34
+ }
22
35
  // Add headers, including the cookies
23
36
  Object.keys(headersObj).forEach(function (key) {
24
- push("request.AddHeader(\"".concat(key, "\", \"").concat(headersObj[key], "\");"));
37
+ // if we have post data, restsharp really wants to set the contentType
38
+ // itself; do not add a content-type header or you end up with failures
39
+ // which manifest as unhandled exceptions.
40
+ //
41
+ // The only case where we _do_ want to add it is if there's no postData
42
+ // text, in which case there will be no `AddJsonBody` call, and restsharp
43
+ // won't know to set the content type
44
+ if (postData.mimeType && key.toLowerCase() === 'content-type' && postData.text) {
45
+ if (isMultipart && postData.boundary) {
46
+ push("request.FormBoundary = \"".concat(postData.boundary, "\";"));
47
+ }
48
+ return;
49
+ }
50
+ push("request.AddHeader(\"".concat(key, "\", \"").concat((0, escape_1.escapeForDoubleQuotes)(headersObj[key]), "\");"));
25
51
  });
26
52
  cookies.forEach(function (_a) {
27
53
  var name = _a.name, value = _a.value;
28
- push("request.AddCookie(\"".concat(name, "\", \"").concat(value, "\");"));
54
+ push("request.AddCookie(\"".concat(name, "\", \"").concat((0, escape_1.escapeForDoubleQuotes)(value), "\", \"").concat(uriObj.pathname, "\", \"").concat(uriObj.host, "\");"));
29
55
  });
30
- if (postData.text) {
31
- var header = (0, headers_1.getHeader)(allHeaders, 'content-type');
32
- var text = JSON.stringify(postData.text);
33
- push("request.AddParameter(\"".concat(header, "\", ").concat(text, ", ParameterType.RequestBody);"));
56
+ switch (postData.mimeType) {
57
+ case 'multipart/form-data':
58
+ if (!postData.params)
59
+ break;
60
+ postData.params.forEach(function (param) {
61
+ if (param.fileName) {
62
+ push("request.AddFile(\"".concat(param.name, "\", \"").concat(param.fileName, "\");"));
63
+ }
64
+ else {
65
+ push("request.AddParameter(\"".concat(param.name, "\", \"").concat(param.value, "\");"));
66
+ }
67
+ });
68
+ break;
69
+ case 'application/x-www-form-urlencoded':
70
+ if (!postData.params)
71
+ break;
72
+ postData.params.forEach(function (param) {
73
+ push("request.AddParameter(\"".concat(param.name, "\", \"").concat(param.value, "\");"));
74
+ });
75
+ break;
76
+ case 'application/json': {
77
+ if (!postData.text)
78
+ break;
79
+ var text = JSON.stringify(postData.text);
80
+ push("request.AddJsonBody(".concat(text, ", false);"));
81
+ break;
82
+ }
83
+ default:
84
+ if (!postData.text)
85
+ break;
86
+ push("request.AddStringBody(\"".concat(postData.text, "\", \"").concat(postData.mimeType, "\");"));
34
87
  }
35
- push('IRestResponse response = client.Execute(request);');
88
+ push("var response = await client.".concat(title(method), "Async(request);\n"));
89
+ push('Console.WriteLine("{0}", response.Content);\n');
36
90
  return join();
37
91
  },
38
92
  };
@@ -9,6 +9,7 @@ exports.csharp = {
9
9
  title: 'C#',
10
10
  extname: '.cs',
11
11
  default: 'restsharp',
12
+ cli: 'dotnet',
12
13
  },
13
14
  clientsById: {
14
15
  httpclient: client_1.httpclient,
@@ -9,9 +9,9 @@
9
9
  */
10
10
  import type { Client } from '../../targets';
11
11
  export interface GoNativeOptions {
12
- showBoilerplate?: boolean;
13
12
  checkErrors?: boolean;
14
13
  printBody?: boolean;
14
+ showBoilerplate?: boolean;
15
15
  timeout?: number;
16
16
  }
17
17
  export declare const native: Client<GoNativeOptions>;
@@ -1,18 +1,8 @@
1
1
  "use strict";
2
- var __assign = (this && this.__assign) || function () {
3
- __assign = Object.assign || function(t) {
4
- for (var s, i = 1, n = arguments.length; i < n; i++) {
5
- s = arguments[i];
6
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
- t[p] = s[p];
8
- }
9
- return t;
10
- };
11
- return __assign.apply(this, arguments);
12
- };
13
2
  Object.defineProperty(exports, "__esModule", { value: true });
14
3
  exports.native = void 0;
15
4
  var code_builder_1 = require("../../../helpers/code-builder");
5
+ var escape_1 = require("../../../helpers/escape");
16
6
  exports.native = {
17
7
  info: {
18
8
  key: 'native',
@@ -22,32 +12,33 @@ exports.native = {
22
12
  },
23
13
  convert: function (_a, options) {
24
14
  var postData = _a.postData, method = _a.method, allHeaders = _a.allHeaders, fullUrl = _a.fullUrl;
15
+ if (options === void 0) { options = {}; }
25
16
  var _b = new code_builder_1.CodeBuilder({ indent: '\t' }), blank = _b.blank, push = _b.push, join = _b.join;
26
- var opts = __assign({ showBoilerplate: true, checkErrors: false, printBody: true, timeout: -1 }, options);
27
- var errorPlaceholder = opts.checkErrors ? 'err' : '_';
28
- var indent = opts.showBoilerplate ? 1 : 0;
17
+ var _c = options.showBoilerplate, showBoilerplate = _c === void 0 ? true : _c, _d = options.checkErrors, checkErrors = _d === void 0 ? false : _d, _e = options.printBody, printBody = _e === void 0 ? true : _e, _f = options.timeout, timeout = _f === void 0 ? -1 : _f;
18
+ var errorPlaceholder = checkErrors ? 'err' : '_';
19
+ var indent = showBoilerplate ? 1 : 0;
29
20
  var errorCheck = function () {
30
- if (opts.checkErrors) {
21
+ if (checkErrors) {
31
22
  push('if err != nil {', indent);
32
23
  push('panic(err)', indent + 1);
33
24
  push('}', indent);
34
25
  }
35
26
  };
36
27
  // Create boilerplate
37
- if (opts.showBoilerplate) {
28
+ if (showBoilerplate) {
38
29
  push('package main');
39
30
  blank();
40
31
  push('import (');
41
32
  push('"fmt"', indent);
42
- if (opts.timeout > 0) {
33
+ if (timeout > 0) {
43
34
  push('"time"', indent);
44
35
  }
45
36
  if (postData.text) {
46
37
  push('"strings"', indent);
47
38
  }
48
39
  push('"net/http"', indent);
49
- if (opts.printBody) {
50
- push('"io/ioutil"', indent);
40
+ if (printBody) {
41
+ push('"io"', indent);
51
42
  }
52
43
  push(')');
53
44
  blank();
@@ -55,17 +46,17 @@ exports.native = {
55
46
  blank();
56
47
  }
57
48
  // Create client
58
- var client;
59
- if (opts.timeout > 0) {
60
- client = 'client';
49
+ var hasTimeout = timeout > 0;
50
+ var hasClient = hasTimeout;
51
+ var client = hasClient ? 'client' : 'http.DefaultClient';
52
+ if (hasClient) {
61
53
  push('client := http.Client{', indent);
62
- push("Timeout: time.Duration(".concat(opts.timeout, " * time.Second),"), indent + 1);
54
+ if (hasTimeout) {
55
+ push("Timeout: time.Duration(".concat(timeout, " * time.Second),"), indent + 1);
56
+ }
63
57
  push('}', indent);
64
58
  blank();
65
59
  }
66
- else {
67
- client = 'http.DefaultClient';
68
- }
69
60
  push("url := \"".concat(fullUrl, "\""), indent);
70
61
  blank();
71
62
  // If we have body content or not create the var and reader or nil
@@ -83,7 +74,7 @@ exports.native = {
83
74
  // Add headers
84
75
  if (Object.keys(allHeaders).length) {
85
76
  Object.keys(allHeaders).forEach(function (key) {
86
- push("req.Header.Add(\"".concat(key, "\", \"").concat(allHeaders[key], "\")"), indent);
77
+ push("req.Header.Add(\"".concat(key, "\", \"").concat((0, escape_1.escapeForDoubleQuotes)(allHeaders[key]), "\")"), indent);
87
78
  });
88
79
  blank();
89
80
  }
@@ -91,20 +82,19 @@ exports.native = {
91
82
  push("res, ".concat(errorPlaceholder, " := ").concat(client, ".Do(req)"), indent);
92
83
  errorCheck();
93
84
  // Get Body
94
- if (opts.printBody) {
85
+ if (printBody) {
95
86
  blank();
96
87
  push('defer res.Body.Close()', indent);
97
- push("body, ".concat(errorPlaceholder, " := ioutil.ReadAll(res.Body)"), indent);
88
+ push("body, ".concat(errorPlaceholder, " := io.ReadAll(res.Body)"), indent);
98
89
  errorCheck();
99
90
  }
100
91
  // Print it
101
92
  blank();
102
- push('fmt.Println(res)', indent);
103
- if (opts.printBody) {
93
+ if (printBody) {
104
94
  push('fmt.Println(string(body))', indent);
105
95
  }
106
96
  // End main block
107
- if (opts.showBoilerplate) {
97
+ if (showBoilerplate) {
108
98
  blank();
109
99
  push('}');
110
100
  }
@@ -8,6 +8,7 @@ exports.go = {
8
8
  title: 'Go',
9
9
  extname: '.go',
10
10
  default: 'native',
11
+ cli: 'go',
11
12
  },
12
13
  clientsById: {
13
14
  native: client_1.native,
@@ -13,6 +13,7 @@ var __assign = (this && this.__assign) || function () {
13
13
  Object.defineProperty(exports, "__esModule", { value: true });
14
14
  exports.asynchttp = void 0;
15
15
  var code_builder_1 = require("../../../helpers/code-builder");
16
+ var escape_1 = require("../../../helpers/escape");
16
17
  exports.asynchttp = {
17
18
  info: {
18
19
  key: 'asynchttp',
@@ -28,7 +29,7 @@ exports.asynchttp = {
28
29
  push("client.prepare(\"".concat(method.toUpperCase(), "\", \"").concat(fullUrl, "\")"));
29
30
  // Add headers, including the cookies
30
31
  Object.keys(allHeaders).forEach(function (key) {
31
- push(".setHeader(\"".concat(key, "\", \"").concat(allHeaders[key], "\")"), 1);
32
+ push(".setHeader(\"".concat(key, "\", \"").concat((0, escape_1.escapeForDoubleQuotes)(allHeaders[key]), "\")"), 1);
32
33
  });
33
34
  if (postData.text) {
34
35
  push(".setBody(".concat(JSON.stringify(postData.text), ")"), 1);
@@ -13,6 +13,7 @@ var __assign = (this && this.__assign) || function () {
13
13
  Object.defineProperty(exports, "__esModule", { value: true });
14
14
  exports.nethttp = void 0;
15
15
  var code_builder_1 = require("../../../helpers/code-builder");
16
+ var escape_1 = require("../../../helpers/escape");
16
17
  exports.nethttp = {
17
18
  info: {
18
19
  key: 'nethttp',
@@ -27,7 +28,7 @@ exports.nethttp = {
27
28
  push('HttpRequest request = HttpRequest.newBuilder()');
28
29
  push(".uri(URI.create(\"".concat(fullUrl, "\"))"), 2);
29
30
  Object.keys(allHeaders).forEach(function (key) {
30
- push(".header(\"".concat(key, "\", \"").concat(allHeaders[key], "\")"), 2);
31
+ push(".header(\"".concat(key, "\", \"").concat((0, escape_1.escapeForDoubleQuotes)(allHeaders[key]), "\")"), 2);
31
32
  });
32
33
  if (postData.text) {
33
34
  push(".method(\"".concat(method.toUpperCase(), "\", HttpRequest.BodyPublishers.ofString(").concat(JSON.stringify(postData.text), "))"), 2);
@@ -13,6 +13,7 @@ var __assign = (this && this.__assign) || function () {
13
13
  Object.defineProperty(exports, "__esModule", { value: true });
14
14
  exports.okhttp = void 0;
15
15
  var code_builder_1 = require("../../../helpers/code-builder");
16
+ var escape_1 = require("../../../helpers/escape");
16
17
  exports.okhttp = {
17
18
  info: {
18
19
  key: 'okhttp',
@@ -60,7 +61,7 @@ exports.okhttp = {
60
61
  }
61
62
  // Add headers, including the cookies
62
63
  Object.keys(allHeaders).forEach(function (key) {
63
- push(".addHeader(\"".concat(key, "\", \"").concat(allHeaders[key], "\")"), 1);
64
+ push(".addHeader(\"".concat(key, "\", \"").concat((0, escape_1.escapeForDoubleQuotes)(allHeaders[key]), "\")"), 1);
64
65
  });
65
66
  push('.build();', 1);
66
67
  blank();
@@ -13,6 +13,7 @@ var __assign = (this && this.__assign) || function () {
13
13
  Object.defineProperty(exports, "__esModule", { value: true });
14
14
  exports.unirest = void 0;
15
15
  var code_builder_1 = require("../../../helpers/code-builder");
16
+ var escape_1 = require("../../../helpers/escape");
16
17
  exports.unirest = {
17
18
  info: {
18
19
  key: 'unirest',
@@ -33,7 +34,7 @@ exports.unirest = {
33
34
  }
34
35
  // Add headers, including the cookies
35
36
  Object.keys(allHeaders).forEach(function (key) {
36
- push(".header(\"".concat(key, "\", \"").concat(allHeaders[key], "\")"), 1);
37
+ push(".header(\"".concat(key, "\", \"").concat((0, escape_1.escapeForDoubleQuotes)(allHeaders[key]), "\")"), 1);
37
38
  });
38
39
  if (postData.text) {
39
40
  push(".body(".concat(JSON.stringify(postData.text), ")"), 1);
@@ -13,7 +13,7 @@ var options = {
13
13
  string: 'f"oo',
14
14
  arr: [1, 2, 3],
15
15
  nested: { a: 'b' },
16
- arr_mix: [1, 'a', { arr_mix_nested: {} }],
16
+ arr_mix: [1, 'a', { arr_mix_nested: [] }],
17
17
  boolean: false
18
18
  }
19
19
  };
@@ -7,7 +7,12 @@ var axios_1 = __importDefault(require("axios"));
7
7
  var options = {
8
8
  method: 'GET',
9
9
  url: 'https://httpbin.org/headers',
10
- headers: { accept: 'application/json', 'x-foo': 'Bar', 'x-bar': 'Foo' }
10
+ headers: {
11
+ accept: 'application/json',
12
+ 'x-foo': 'Bar',
13
+ 'x-bar': 'Foo',
14
+ 'quoted-value': '"quoted" \'string\''
15
+ }
11
16
  };
12
17
  axios_1.default
13
18
  .request(options)
@@ -1,8 +1,8 @@
1
1
  /// <reference types="node" />
2
2
  declare namespace options {
3
- const method: string;
4
- const headers: {
3
+ let method: string;
4
+ let headers: {
5
5
  'content-type': string;
6
6
  };
7
- const body: import("url").URLSearchParams;
7
+ let body: import("url").URLSearchParams;
8
8
  }