@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
@@ -3,7 +3,12 @@ var axios = require('axios');
3
3
  var options = {
4
4
  method: 'GET',
5
5
  url: 'https://httpbin.org/headers',
6
- headers: { accept: 'application/json', 'x-foo': 'Bar', 'x-bar': 'Foo' }
6
+ headers: {
7
+ accept: 'application/json',
8
+ 'x-foo': 'Bar',
9
+ 'x-bar': 'Foo',
10
+ 'quoted-value': '"quoted" \'string\''
11
+ }
7
12
  };
8
13
  axios
9
14
  .request(options)
@@ -9,7 +9,7 @@ var options = {
9
9
  string: 'f"oo',
10
10
  arr: [1, 2, 3],
11
11
  nested: { a: 'b' },
12
- arr_mix: [1, 'a', { arr_mix_nested: {} }],
12
+ arr_mix: [1, 'a', { arr_mix_nested: [] }],
13
13
  boolean: false
14
14
  })
15
15
  };
@@ -3,7 +3,12 @@ var fetch = require('node-fetch');
3
3
  var url = 'https://httpbin.org/headers';
4
4
  var options = {
5
5
  method: 'GET',
6
- headers: { accept: 'application/json', 'x-foo': 'Bar', 'x-bar': 'Foo' }
6
+ headers: {
7
+ accept: 'application/json',
8
+ 'x-foo': 'Bar',
9
+ 'x-bar': 'Foo',
10
+ 'quoted-value': '"quoted" \'string\''
11
+ }
7
12
  };
8
13
  fetch(url, options)
9
14
  .then(function (res) { return res.json(); })
@@ -1,15 +1,4 @@
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
  var __importDefault = (this && this.__importDefault) || function (mod) {
14
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
15
4
  };
@@ -27,8 +16,9 @@ exports.native = {
27
16
  convert: function (_a, options) {
28
17
  var _b;
29
18
  var uriObj = _a.uriObj, method = _a.method, allHeaders = _a.allHeaders, postData = _a.postData;
30
- var opts = __assign({ indent: ' ' }, options);
31
- var _c = new code_builder_1.CodeBuilder({ indent: opts.indent }), blank = _c.blank, join = _c.join, push = _c.push, unshift = _c.unshift;
19
+ if (options === void 0) { options = {}; }
20
+ var _c = options.indent, indent = _c === void 0 ? ' ' : _c;
21
+ var _d = new code_builder_1.CodeBuilder({ indent: indent }), blank = _d.blank, join = _d.join, push = _d.push, unshift = _d.unshift;
32
22
  var reqOpts = {
33
23
  method: method,
34
24
  hostname: uriObj.hostname,
@@ -38,7 +28,7 @@ exports.native = {
38
28
  };
39
29
  push("const http = require('".concat((_b = uriObj.protocol) === null || _b === void 0 ? void 0 : _b.replace(':', ''), "');"));
40
30
  blank();
41
- push("const options = ".concat((0, stringify_object_1.default)(reqOpts, { indent: opts.indent }), ";"));
31
+ push("const options = ".concat((0, stringify_object_1.default)(reqOpts, { indent: indent }), ";"));
42
32
  blank();
43
33
  push('const req = http.request(options, function (res) {');
44
34
  push('const chunks = [];', 1);
@@ -73,7 +63,7 @@ exports.native = {
73
63
  break;
74
64
  default:
75
65
  if (postData.text) {
76
- push("req.write(".concat((0, stringify_object_1.default)(postData.text, { indent: opts.indent }), ");"));
66
+ push("req.write(".concat((0, stringify_object_1.default)(postData.text, { indent: indent }), ");"));
77
67
  }
78
68
  }
79
69
  push('req.end();');
@@ -24,7 +24,7 @@ req.write(JSON.stringify({
24
24
  string: 'f"oo',
25
25
  arr: [1, 2, 3],
26
26
  nested: { a: 'b' },
27
- arr_mix: [1, 'a', { arr_mix_nested: {} }],
27
+ arr_mix: [1, 'a', { arr_mix_nested: [] }],
28
28
  boolean: false
29
29
  }));
30
30
  req.end();
@@ -8,7 +8,8 @@ var options = {
8
8
  headers: {
9
9
  accept: 'application/json',
10
10
  'x-foo': 'Bar',
11
- 'x-bar': 'Foo'
11
+ 'x-bar': 'Foo',
12
+ 'quoted-value': '"quoted" \'string\''
12
13
  }
13
14
  };
14
15
  var req = http.request(options, function (res) {
@@ -9,7 +9,7 @@ var options = {
9
9
  string: 'f"oo',
10
10
  arr: [1, 2, 3],
11
11
  nested: { a: 'b' },
12
- arr_mix: [1, 'a', { arr_mix_nested: {} }],
12
+ arr_mix: [1, 'a', { arr_mix_nested: [] }],
13
13
  boolean: false
14
14
  },
15
15
  json: true
@@ -3,7 +3,12 @@ var request = require('request');
3
3
  var options = {
4
4
  method: 'GET',
5
5
  url: 'https://httpbin.org/headers',
6
- headers: { accept: 'application/json', 'x-foo': 'Bar', 'x-bar': 'Foo' }
6
+ headers: {
7
+ accept: 'application/json',
8
+ 'x-foo': 'Bar',
9
+ 'x-bar': 'Foo',
10
+ 'quoted-value': '"quoted" \'string\''
11
+ }
7
12
  };
8
13
  request(options, function (error, response, body) {
9
14
  if (error)
@@ -20,7 +20,7 @@ req.send({
20
20
  1,
21
21
  'a',
22
22
  {
23
- arr_mix_nested: {}
23
+ arr_mix_nested: []
24
24
  }
25
25
  ],
26
26
  boolean: false
@@ -4,7 +4,8 @@ var req = unirest('GET', 'https://httpbin.org/headers');
4
4
  req.headers({
5
5
  accept: 'application/json',
6
6
  'x-foo': 'Bar',
7
- 'x-bar': 'Foo'
7
+ 'x-bar': 'Foo',
8
+ 'quoted-value': '"quoted" \'string\''
8
9
  });
9
10
  req.end(function (res) {
10
11
  if (res.error)
@@ -38,9 +38,10 @@ exports.nsurlsession = {
38
38
  description: "Foundation's NSURLSession request",
39
39
  },
40
40
  convert: function (_a, options) {
41
+ var _b;
41
42
  var allHeaders = _a.allHeaders, postData = _a.postData, method = _a.method, fullUrl = _a.fullUrl;
42
43
  var opts = __assign({ indent: ' ', pretty: true, timeout: 10 }, options);
43
- var _b = new code_builder_1.CodeBuilder({ indent: opts.indent }), push = _b.push, join = _b.join, blank = _b.blank;
44
+ var _c = new code_builder_1.CodeBuilder({ indent: opts.indent }), push = _c.push, join = _c.join, blank = _c.blank;
44
45
  // Markers for headers to be created as literal objects and later be set on the NSURLRequest if exist
45
46
  var req = {
46
47
  hasHeaders: false,
@@ -57,12 +58,12 @@ exports.nsurlsession = {
57
58
  req.hasBody = true;
58
59
  switch (postData.mimeType) {
59
60
  case 'application/x-www-form-urlencoded':
60
- if (postData.params) {
61
+ if ((_b = postData.params) === null || _b === void 0 ? void 0 : _b.length) {
61
62
  // By appending parameters one by one in the resulting snippet,
62
63
  // we make it easier for the user to edit it according to his or her needs after pasting.
63
64
  // The user can just add/remove lines adding/removing body parameters.
64
65
  blank();
65
- var _c = __read(postData.params), head = _c[0], tail = _c.slice(1);
66
+ var _d = __read(postData.params), head = _d[0], tail = _d.slice(1);
66
67
  push("NSMutableData *postData = [[NSMutableData alloc] initWithData:[@\"".concat(head.name, "=").concat(head.value, "\" dataUsingEncoding:NSUTF8StringEncoding]];"));
67
68
  tail.forEach(function (_a) {
68
69
  var name = _a.name, value = _a.value;
@@ -13,6 +13,7 @@ var __assign = (this && this.__assign) || function () {
13
13
  Object.defineProperty(exports, "__esModule", { value: true });
14
14
  exports.cohttp = void 0;
15
15
  var code_builder_1 = require("../../../helpers/code-builder");
16
+ var escape_1 = require("../../../helpers/escape");
16
17
  exports.cohttp = {
17
18
  info: {
18
19
  key: 'cohttp',
@@ -33,12 +34,12 @@ exports.cohttp = {
33
34
  // Add headers, including the cookies
34
35
  var headers = Object.keys(allHeaders);
35
36
  if (headers.length === 1) {
36
- push("let headers = Header.add (Header.init ()) \"".concat(headers[0], "\" \"").concat(allHeaders[headers[0]], "\" in"));
37
+ push("let headers = Header.add (Header.init ()) \"".concat(headers[0], "\" \"").concat((0, escape_1.escapeForDoubleQuotes)(allHeaders[headers[0]]), "\" in"));
37
38
  }
38
39
  else if (headers.length > 1) {
39
40
  push('let headers = Header.add_list (Header.init ()) [');
40
41
  headers.forEach(function (key) {
41
- push("(\"".concat(key, "\", \"").concat(allHeaders[key], "\");"), 1);
42
+ push("(\"".concat(key, "\", \"").concat((0, escape_1.escapeForDoubleQuotes)(allHeaders[key]), "\");"), 1);
42
43
  });
43
44
  push('] in');
44
45
  }
@@ -11,7 +11,7 @@ import type { Client } from '../../targets';
11
11
  export interface CurlOptions {
12
12
  closingTag?: boolean;
13
13
  maxRedirects?: number;
14
- nameErrors?: boolean;
14
+ namedErrors?: boolean;
15
15
  noTags?: boolean;
16
16
  shortTags?: boolean;
17
17
  timeout?: number;
@@ -1,18 +1,9 @@
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.curl = void 0;
15
4
  var code_builder_1 = require("../../../helpers/code-builder");
5
+ var escape_1 = require("../../../helpers/escape");
6
+ var helpers_1 = require("../helpers");
16
7
  exports.curl = {
17
8
  info: {
18
9
  key: 'curl',
@@ -22,10 +13,11 @@ exports.curl = {
22
13
  },
23
14
  convert: function (_a, options) {
24
15
  var uriObj = _a.uriObj, postData = _a.postData, fullUrl = _a.fullUrl, method = _a.method, httpVersion = _a.httpVersion, cookies = _a.cookies, headersObj = _a.headersObj;
25
- var opts = __assign({ closingTag: false, indent: ' ', maxRedirects: 10, namedErrors: false, noTags: false, shortTags: false, timeout: 30 }, options);
26
- var _b = new code_builder_1.CodeBuilder({ indent: opts.indent }), push = _b.push, blank = _b.blank, join = _b.join;
27
- if (!opts.noTags) {
28
- push(opts.shortTags ? '<?' : '<?php');
16
+ if (options === void 0) { options = {}; }
17
+ var _b = options.closingTag, closingTag = _b === void 0 ? false : _b, _c = options.indent, indent = _c === void 0 ? ' ' : _c, _d = options.maxRedirects, maxRedirects = _d === void 0 ? 10 : _d, _e = options.namedErrors, namedErrors = _e === void 0 ? false : _e, _f = options.noTags, noTags = _f === void 0 ? false : _f, _g = options.shortTags, shortTags = _g === void 0 ? false : _g, _h = options.timeout, timeout = _h === void 0 ? 30 : _h;
18
+ var _j = new code_builder_1.CodeBuilder({ indent: indent }), push = _j.push, blank = _j.blank, join = _j.join;
19
+ if (!noTags) {
20
+ push(shortTags ? '<?' : '<?php');
29
21
  blank();
30
22
  }
31
23
  push('$curl = curl_init();');
@@ -54,12 +46,12 @@ exports.curl = {
54
46
  {
55
47
  escape: false,
56
48
  name: 'CURLOPT_MAXREDIRS',
57
- value: opts.maxRedirects,
49
+ value: maxRedirects,
58
50
  },
59
51
  {
60
52
  escape: false,
61
53
  name: 'CURLOPT_TIMEOUT',
62
- value: opts.timeout,
54
+ value: timeout,
63
55
  },
64
56
  {
65
57
  escape: false,
@@ -72,13 +64,17 @@ exports.curl = {
72
64
  value: method,
73
65
  },
74
66
  {
75
- escape: true,
67
+ escape: !postData.jsonObj,
76
68
  name: 'CURLOPT_POSTFIELDS',
77
- value: postData ? postData.text : undefined,
69
+ value: postData
70
+ ? postData.jsonObj
71
+ ? "json_encode(".concat((0, helpers_1.convertType)(postData.jsonObj, indent.repeat(2), indent), ")")
72
+ : postData.text
73
+ : undefined,
78
74
  },
79
75
  ];
80
76
  push('curl_setopt_array($curl, [');
81
- var curlopts = new code_builder_1.CodeBuilder({ indent: opts.indent, join: "\n".concat(opts.indent) });
77
+ var curlopts = new code_builder_1.CodeBuilder({ indent: indent, join: "\n".concat(indent) });
82
78
  curlOptions.forEach(function (_a) {
83
79
  var value = _a.value, name = _a.name, escape = _a.escape;
84
80
  if (value !== null && value !== undefined) {
@@ -93,10 +89,10 @@ exports.curl = {
93
89
  // construct cookies
94
90
  var headers = Object.keys(headersObj)
95
91
  .sort()
96
- .map(function (key) { return "\"".concat(key, ": ").concat(headersObj[key], "\""); });
92
+ .map(function (key) { return "\"".concat(key, ": ").concat((0, escape_1.escapeForDoubleQuotes)(headersObj[key]), "\""); });
97
93
  if (headers.length) {
98
94
  curlopts.push('CURLOPT_HTTPHEADER => [');
99
- curlopts.push(headers.join(",\n".concat(opts.indent).concat(opts.indent)), 1);
95
+ curlopts.push(headers.join(",\n".concat(indent).concat(indent)), 1);
100
96
  curlopts.push('],');
101
97
  }
102
98
  push(curlopts.join(), 1);
@@ -108,7 +104,7 @@ exports.curl = {
108
104
  push('curl_close($curl);');
109
105
  blank();
110
106
  push('if ($err) {');
111
- if (opts.namedErrors) {
107
+ if (namedErrors) {
112
108
  push('echo array_flip(get_defined_constants(true)["curl"])[$err];', 1);
113
109
  }
114
110
  else {
@@ -117,7 +113,7 @@ exports.curl = {
117
113
  push('} else {');
118
114
  push('echo $response;', 1);
119
115
  push('}');
120
- if (!opts.noTags && opts.closingTag) {
116
+ if (!noTags && closingTag) {
121
117
  blank();
122
118
  push('?>');
123
119
  }
@@ -13,6 +13,7 @@ var __assign = (this && this.__assign) || function () {
13
13
  Object.defineProperty(exports, "__esModule", { value: true });
14
14
  exports.guzzle = 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 helpers_1 = require("../helpers");
18
19
  exports.guzzle = {
@@ -23,10 +24,11 @@ exports.guzzle = {
23
24
  description: 'PHP with Guzzle',
24
25
  },
25
26
  convert: function (_a, options) {
27
+ var _b;
26
28
  var postData = _a.postData, fullUrl = _a.fullUrl, method = _a.method, cookies = _a.cookies, headersObj = _a.headersObj;
27
29
  var opts = __assign({ closingTag: false, indent: ' ', noTags: false, shortTags: false }, options);
28
- var _b = new code_builder_1.CodeBuilder({ indent: opts.indent }), push = _b.push, blank = _b.blank, join = _b.join;
29
- var _c = new code_builder_1.CodeBuilder({ indent: opts.indent }), requestCode = _c.code, requestPush = _c.push, requestJoin = _c.join;
30
+ var _c = new code_builder_1.CodeBuilder({ indent: opts.indent }), push = _c.push, blank = _c.blank, join = _c.join;
31
+ var _d = new code_builder_1.CodeBuilder({ indent: opts.indent }), requestCode = _d.code, requestPush = _d.push, requestJoin = _d.join;
30
32
  if (!opts.noTags) {
31
33
  push(opts.shortTags ? '<?' : '<?php');
32
34
  }
@@ -63,7 +65,7 @@ exports.guzzle = {
63
65
  requestPush("'multipart' => ".concat((0, helpers_1.convertType)(fields_1, opts.indent + opts.indent, opts.indent)), 1);
64
66
  // Guzzle adds its own boundary for multipart requests.
65
67
  if ((0, headers_1.hasHeader)(headersObj, 'content-type')) {
66
- if (String((0, headers_1.getHeader)(headersObj, 'content-type')).indexOf('boundary')) {
68
+ if ((_b = (0, headers_1.getHeader)(headersObj, 'content-type')) === null || _b === void 0 ? void 0 : _b.indexOf('boundary')) {
67
69
  var headerName = (0, headers_1.getHeaderName)(headersObj, 'content-type');
68
70
  if (headerName) {
69
71
  delete headersObj[headerName];
@@ -82,14 +84,14 @@ exports.guzzle = {
82
84
  var headers = Object.keys(headersObj)
83
85
  .sort()
84
86
  .map(function (key) {
85
- return "".concat(opts.indent).concat(opts.indent, "'").concat(key, "' => '").concat(headersObj[key], "',");
87
+ return "".concat(opts.indent).concat(opts.indent, "'").concat(key, "' => '").concat((0, escape_1.escapeForSingleQuotes)(headersObj[key]), "',");
86
88
  });
87
89
  // construct cookies
88
90
  var cookieString = cookies
89
91
  .map(function (cookie) { return "".concat(encodeURIComponent(cookie.name), "=").concat(encodeURIComponent(cookie.value)); })
90
92
  .join('; ');
91
93
  if (cookieString.length) {
92
- headers.push("".concat(opts.indent).concat(opts.indent, "'cookie' => '").concat(cookieString, "',"));
94
+ headers.push("".concat(opts.indent).concat(opts.indent, "'cookie' => '").concat((0, escape_1.escapeForSingleQuotes)(cookieString), "',"));
93
95
  }
94
96
  if (headers.length) {
95
97
  requestPush("'headers' => [", 1);
@@ -1,16 +1,19 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.supportedMethods = exports.convertType = void 0;
4
+ var escape_1 = require("../../helpers/escape");
4
5
  var convertType = function (obj, indent, lastIndent) {
5
6
  lastIndent = lastIndent || '';
6
7
  indent = indent || '';
7
8
  switch (Object.prototype.toString.call(obj)) {
9
+ case '[object Boolean]':
10
+ return obj;
8
11
  case '[object Null]':
9
12
  return 'null';
10
13
  case '[object Undefined]':
11
14
  return 'null';
12
15
  case '[object String]':
13
- return "'".concat(obj.replace(/\\/g, '\\\\').replace(/'/g, "'"), "'");
16
+ return "'".concat((0, escape_1.escapeString)(obj, { delimiter: "'", escapeNewlines: false }), "'");
14
17
  case '[object Number]':
15
18
  return obj.toString();
16
19
  case '[object Array]': {
@@ -1,15 +1,4 @@
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.http1 = void 0;
15
4
  var code_builder_1 = require("../../../helpers/code-builder");
@@ -23,10 +12,11 @@ exports.http1 = {
23
12
  },
24
13
  convert: function (_a, options) {
25
14
  var method = _a.method, url = _a.url, postData = _a.postData, queryObj = _a.queryObj, headersObj = _a.headersObj, cookiesObj = _a.cookiesObj;
26
- var opts = __assign({ closingTag: false, indent: ' ', noTags: false, shortTags: false }, options);
27
- var _b = new code_builder_1.CodeBuilder({ indent: opts.indent }), push = _b.push, blank = _b.blank, join = _b.join;
28
- if (!opts.noTags) {
29
- push(opts.shortTags ? '<?' : '<?php');
15
+ if (options === void 0) { options = {}; }
16
+ var _b = options.closingTag, closingTag = _b === void 0 ? false : _b, _c = options.indent, indent = _c === void 0 ? ' ' : _c, _d = options.noTags, noTags = _d === void 0 ? false : _d, _e = options.shortTags, shortTags = _e === void 0 ? false : _e;
17
+ var _f = new code_builder_1.CodeBuilder({ indent: indent }), push = _f.push, blank = _f.blank, join = _f.join;
18
+ if (!noTags) {
19
+ push(shortTags ? '<?' : '<?php');
30
20
  blank();
31
21
  }
32
22
  if (!helpers_1.supportedMethods.includes(method.toUpperCase())) {
@@ -42,21 +32,26 @@ exports.http1 = {
42
32
  }
43
33
  blank();
44
34
  if (Object.keys(queryObj).length) {
45
- push("$request->setQueryData(".concat((0, helpers_1.convertType)(queryObj, opts.indent), ");"));
35
+ push("$request->setQueryData(".concat((0, helpers_1.convertType)(queryObj, indent), ");"));
46
36
  blank();
47
37
  }
48
38
  if (Object.keys(headersObj).length) {
49
- push("$request->setHeaders(".concat((0, helpers_1.convertType)(headersObj, opts.indent), ");"));
39
+ push("$request->setHeaders(".concat((0, helpers_1.convertType)(headersObj, indent), ");"));
50
40
  blank();
51
41
  }
52
42
  if (Object.keys(cookiesObj).length) {
53
- push("$request->setCookies(".concat((0, helpers_1.convertType)(cookiesObj, opts.indent), ");"));
43
+ push("$request->setCookies(".concat((0, helpers_1.convertType)(cookiesObj, indent), ");"));
54
44
  blank();
55
45
  }
56
46
  switch (postData.mimeType) {
57
47
  case 'application/x-www-form-urlencoded':
58
48
  push("$request->setContentType(".concat((0, helpers_1.convertType)(postData.mimeType), ");"));
59
- push("$request->setPostFields(".concat((0, helpers_1.convertType)(postData.paramsObj, opts.indent), ");"));
49
+ push("$request->setPostFields(".concat((0, helpers_1.convertType)(postData.paramsObj, indent), ");"));
50
+ blank();
51
+ break;
52
+ case 'application/json':
53
+ push("$request->setContentType(".concat((0, helpers_1.convertType)(postData.mimeType), ");"));
54
+ push("$request->setBody(json_encode(".concat((0, helpers_1.convertType)(postData.jsonObj, indent), "));"));
60
55
  blank();
61
56
  break;
62
57
  default:
@@ -72,7 +67,7 @@ exports.http1 = {
72
67
  push('} catch (HttpException $ex) {');
73
68
  push('echo $ex;', 1);
74
69
  push('}');
75
- if (!opts.noTags && opts.closingTag) {
70
+ if (!noTags && closingTag) {
76
71
  blank();
77
72
  push('?>');
78
73
  }
@@ -1,15 +1,4 @@
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.http2 = void 0;
15
4
  var code_builder_1 = require("../../../helpers/code-builder");
@@ -23,12 +12,14 @@ exports.http2 = {
23
12
  description: 'PHP with pecl/http v2',
24
13
  },
25
14
  convert: function (_a, options) {
15
+ var _b;
26
16
  var postData = _a.postData, headersObj = _a.headersObj, method = _a.method, queryObj = _a.queryObj, cookiesObj = _a.cookiesObj, url = _a.url;
27
- var opts = __assign({ closingTag: false, indent: ' ', noTags: false, shortTags: false }, options);
28
- var _b = new code_builder_1.CodeBuilder({ indent: opts.indent }), push = _b.push, blank = _b.blank, join = _b.join;
17
+ if (options === void 0) { options = {}; }
18
+ var _c = options.closingTag, closingTag = _c === void 0 ? false : _c, _d = options.indent, indent = _d === void 0 ? ' ' : _d, _e = options.noTags, noTags = _e === void 0 ? false : _e, _f = options.shortTags, shortTags = _f === void 0 ? false : _f;
19
+ var _g = new code_builder_1.CodeBuilder({ indent: indent }), push = _g.push, blank = _g.blank, join = _g.join;
29
20
  var hasBody = false;
30
- if (!opts.noTags) {
31
- push(opts.shortTags ? '<?' : '<?php');
21
+ if (!noTags) {
22
+ push(shortTags ? '<?' : '<?php');
32
23
  blank();
33
24
  }
34
25
  push('$client = new http\\Client;');
@@ -37,7 +28,7 @@ exports.http2 = {
37
28
  switch (postData.mimeType) {
38
29
  case 'application/x-www-form-urlencoded':
39
30
  push('$body = new http\\Message\\Body;');
40
- push("$body->append(new http\\QueryString(".concat((0, helpers_1.convertType)(postData.paramsObj, opts.indent), "));"));
31
+ push("$body->append(new http\\QueryString(".concat((0, helpers_1.convertType)(postData.paramsObj, indent), "));"));
41
32
  blank();
42
33
  hasBody = true;
43
34
  break;
@@ -62,13 +53,13 @@ exports.http2 = {
62
53
  fields_1[name] = value;
63
54
  }
64
55
  });
65
- var field = Object.keys(fields_1).length ? (0, helpers_1.convertType)(fields_1, opts.indent) : 'null';
66
- var formValue = files_1.length ? (0, helpers_1.convertType)(files_1, opts.indent) : 'null';
56
+ var field = Object.keys(fields_1).length ? (0, helpers_1.convertType)(fields_1, indent) : 'null';
57
+ var formValue = files_1.length ? (0, helpers_1.convertType)(files_1, indent) : 'null';
67
58
  push('$body = new http\\Message\\Body;');
68
59
  push("$body->addForm(".concat(field, ", ").concat(formValue, ");"));
69
60
  // remove the contentType header
70
61
  if ((0, headers_1.hasHeader)(headersObj, 'content-type')) {
71
- if (String((0, headers_1.getHeader)(headersObj, 'content-type')).indexOf('boundary')) {
62
+ if ((_b = (0, headers_1.getHeader)(headersObj, 'content-type')) === null || _b === void 0 ? void 0 : _b.indexOf('boundary')) {
72
63
  var headerName = (0, headers_1.getHeaderName)(headersObj, 'content-type');
73
64
  if (headerName) {
74
65
  delete headersObj[headerName];
@@ -79,6 +70,11 @@ exports.http2 = {
79
70
  hasBody = true;
80
71
  break;
81
72
  }
73
+ case 'application/json':
74
+ push('$body = new http\\Message\\Body;');
75
+ push("$body->append(json_encode(".concat((0, helpers_1.convertType)(postData.jsonObj, indent), "));"));
76
+ hasBody = true;
77
+ break;
82
78
  default:
83
79
  if (postData.text) {
84
80
  push('$body = new http\\Message\\Body;');
@@ -94,23 +90,23 @@ exports.http2 = {
94
90
  blank();
95
91
  }
96
92
  if (Object.keys(queryObj).length) {
97
- push("$request->setQuery(new http\\QueryString(".concat((0, helpers_1.convertType)(queryObj, opts.indent), "));"));
93
+ push("$request->setQuery(new http\\QueryString(".concat((0, helpers_1.convertType)(queryObj, indent), "));"));
98
94
  blank();
99
95
  }
100
96
  if (Object.keys(headersObj).length) {
101
- push("$request->setHeaders(".concat((0, helpers_1.convertType)(headersObj, opts.indent), ");"));
97
+ push("$request->setHeaders(".concat((0, helpers_1.convertType)(headersObj, indent), ");"));
102
98
  blank();
103
99
  }
104
100
  if (Object.keys(cookiesObj).length) {
105
101
  blank();
106
- push("$client->setCookies(".concat((0, helpers_1.convertType)(cookiesObj, opts.indent), ");"));
102
+ push("$client->setCookies(".concat((0, helpers_1.convertType)(cookiesObj, indent), ");"));
107
103
  blank();
108
104
  }
109
105
  push('$client->enqueue($request)->send();');
110
106
  push('$response = $client->getResponse();');
111
107
  blank();
112
108
  push('echo $response->getBody();');
113
- if (!opts.noTags && opts.closingTag) {
109
+ if (!noTags && closingTag) {
114
110
  blank();
115
111
  push('?>');
116
112
  }
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.generatePowershellConvert = void 0;
4
4
  var code_builder_1 = require("../../helpers/code-builder");
5
+ var escape_1 = require("../../helpers/escape");
5
6
  var headers_1 = require("../../helpers/headers");
6
7
  var generatePowershellConvert = function (command) {
7
8
  var convert = function (_a) {
@@ -20,7 +21,7 @@ var generatePowershellConvert = function (command) {
20
21
  headers.forEach(function (key) {
21
22
  if (key !== 'connection') {
22
23
  // Not allowed
23
- push("$headers.Add(\"".concat(key, "\", \"").concat(headersObj[key], "\")"));
24
+ push("$headers.Add(\"".concat(key, "\", \"").concat((0, escape_1.escapeString)(headersObj[key], { escapeChar: '`' }), "\")"));
24
25
  }
25
26
  });
26
27
  commandOptions.push('-Headers $headers');
@@ -38,7 +39,7 @@ var generatePowershellConvert = function (command) {
38
39
  commandOptions.push('-WebSession $session');
39
40
  }
40
41
  if (postData.text) {
41
- commandOptions.push("-ContentType '".concat((0, headers_1.getHeader)(allHeaders, 'content-type'), "'"));
42
+ commandOptions.push("-ContentType '".concat((0, escape_1.escapeString)((0, headers_1.getHeader)(allHeaders, 'content-type'), { delimiter: "'", escapeChar: '`' }), "'"));
42
43
  commandOptions.push("-Body '".concat(postData.text, "'"));
43
44
  }
44
45
  push("$response = ".concat(command, " -Uri '").concat(fullUrl, "' -Method ").concat(method, " ").concat(commandOptions.join(' ')).trim());
@@ -1,29 +1,23 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.literalRepresentation = void 0;
4
- /**
5
- * Create an string of given length filled with blank spaces
6
- *
7
- * @param length Length of the array to return
8
- * @param str String to pad out with
9
- */
10
- function buildString(length, str) {
11
- return str.repeat(length);
12
- }
13
4
  /**
14
5
  * Create a string corresponding to a Dictionary or Array literal representation with pretty option
15
6
  * and indentation.
16
7
  */
17
8
  function concatValues(concatType, values, pretty, indentation, indentLevel) {
18
- var currentIndent = buildString(indentLevel, indentation);
19
- var closingBraceIndent = buildString(indentLevel - 1, indentation);
9
+ var currentIndent = indentation.repeat(indentLevel);
10
+ var closingBraceIndent = indentation.repeat(indentLevel - 1);
20
11
  var join = pretty ? ",\n".concat(currentIndent) : ', ';
21
12
  var openingBrace = concatType === 'object' ? '{' : '[';
22
13
  var closingBrace = concatType === 'object' ? '}' : ']';
23
14
  if (pretty) {
24
15
  return "".concat(openingBrace, "\n").concat(currentIndent).concat(values.join(join), "\n").concat(closingBraceIndent).concat(closingBrace);
25
16
  }
26
- return openingBrace + values.join(join) + closingBrace;
17
+ if (concatType === 'object' && values.length > 0) {
18
+ return "".concat(openingBrace, " ").concat(values.join(join), " ").concat(closingBrace);
19
+ }
20
+ return "".concat(openingBrace).concat(values.join(join)).concat(closingBrace);
27
21
  }
28
22
  /**
29
23
  * Create a valid Python string of a literal value according to its type.