@readme/httpsnippet 6.0.0 → 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 (58) 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-json.js +1 -1
  20. package/dist/targets/javascript/fetch/fixtures/headers.d.ts +1 -0
  21. package/dist/targets/javascript/fetch/fixtures/headers.js +6 -1
  22. package/dist/targets/javascript/jquery/client.js +1 -1
  23. package/dist/targets/javascript/jquery/fixtures/application-json.js +1 -1
  24. package/dist/targets/javascript/jquery/fixtures/headers.d.ts +1 -0
  25. package/dist/targets/javascript/jquery/fixtures/headers.js +2 -1
  26. package/dist/targets/javascript/xhr/client.js +5 -3
  27. package/dist/targets/javascript/xhr/fixtures/application-json.js +1 -1
  28. package/dist/targets/javascript/xhr/fixtures/headers.js +1 -0
  29. package/dist/targets/kotlin/okhttp/client.js +2 -1
  30. package/dist/targets/node/axios/fixtures/application-json.js +1 -1
  31. package/dist/targets/node/axios/fixtures/headers.js +6 -1
  32. package/dist/targets/node/fetch/fixtures/application-json.js +1 -1
  33. package/dist/targets/node/fetch/fixtures/headers.js +6 -1
  34. package/dist/targets/node/native/client.js +5 -15
  35. package/dist/targets/node/native/fixtures/application-json.js +1 -1
  36. package/dist/targets/node/native/fixtures/headers.js +2 -1
  37. package/dist/targets/node/request/fixtures/application-json.js +1 -1
  38. package/dist/targets/node/request/fixtures/headers.js +6 -1
  39. package/dist/targets/node/unirest/fixtures/application-json.js +1 -1
  40. package/dist/targets/node/unirest/fixtures/headers.js +2 -1
  41. package/dist/targets/objc/nsurlsession/client.js +4 -3
  42. package/dist/targets/ocaml/cohttp/client.js +3 -2
  43. package/dist/targets/php/curl/client.d.ts +1 -1
  44. package/dist/targets/php/curl/client.js +20 -24
  45. package/dist/targets/php/guzzle/client.js +7 -5
  46. package/dist/targets/php/helpers.js +4 -1
  47. package/dist/targets/php/http1/client.js +15 -20
  48. package/dist/targets/php/http2/client.js +19 -23
  49. package/dist/targets/powershell/common.js +3 -2
  50. package/dist/targets/python/helpers.js +6 -12
  51. package/dist/targets/python/requests/client.js +13 -12
  52. package/dist/targets/r/httr/client.js +40 -26
  53. package/dist/targets/ruby/native/client.js +2 -4
  54. package/dist/targets/shell/curl/client.d.ts +2 -2
  55. package/dist/targets/shell/curl/client.js +12 -10
  56. package/dist/targets/swift/nsurlsession/client.js +4 -3
  57. package/dist/targets/targets.d.ts +7 -7
  58. package/package.json +7 -7
@@ -1,7 +1,12 @@
1
1
  "use strict";
2
2
  var options = {
3
3
  method: 'GET',
4
- headers: { accept: 'application/json', 'x-foo': 'Bar', 'x-bar': 'Foo' }
4
+ headers: {
5
+ accept: 'application/json',
6
+ 'x-foo': 'Bar',
7
+ 'x-bar': 'Foo',
8
+ 'quoted-value': '"quoted" \'string\''
9
+ }
5
10
  };
6
11
  fetch('https://httpbin.org/headers', options)
7
12
  .then(function (response) { return response.json(); })
@@ -59,7 +59,7 @@ exports.jquery = {
59
59
  settings.data = '[form]';
60
60
  // remove the contentType header
61
61
  if ((0, headers_1.hasHeader)(allHeaders, 'content-type')) {
62
- if ((_b = String((0, headers_1.getHeader)(allHeaders, 'content-type'))) === null || _b === void 0 ? void 0 : _b.includes('boundary')) {
62
+ if ((_b = (0, headers_1.getHeader)(allHeaders, 'content-type')) === null || _b === void 0 ? void 0 : _b.includes('boundary')) {
63
63
  var headerName = (0, headers_1.getHeaderName)(allHeaders, 'content-type');
64
64
  if (headerName) {
65
65
  delete settings.headers[headerName];
@@ -8,7 +8,7 @@ var settings = {
8
8
  'content-type': 'application/json'
9
9
  },
10
10
  processData: false,
11
- data: '{"number":1,"string":"f\"oo","arr":[1,2,3],"nested":{"a":"b"},"arr_mix":[1,"a",{"arr_mix_nested":{}}],"boolean":false}'
11
+ data: '{"number":1,"string":"f\"oo","arr":[1,2,3],"nested":{"a":"b"},"arr_mix":[1,"a",{"arr_mix_nested":[]}],"boolean":false}'
12
12
  };
13
13
  $.ajax(settings).done(function (response) {
14
14
  console.log(response);
@@ -7,5 +7,6 @@ declare namespace settings {
7
7
  accept: string;
8
8
  'x-foo': string;
9
9
  'x-bar': string;
10
+ 'quoted-value': string;
10
11
  };
11
12
  }
@@ -7,7 +7,8 @@ var settings = {
7
7
  headers: {
8
8
  accept: 'application/json',
9
9
  'x-foo': 'Bar',
10
- 'x-bar': 'Foo'
10
+ 'x-bar': 'Foo',
11
+ 'quoted-value': '"quoted" \'string\''
11
12
  }
12
13
  };
13
14
  $.ajax(settings).done(function (response) {
@@ -17,6 +17,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
17
17
  exports.xhr = void 0;
18
18
  var stringify_object_1 = __importDefault(require("stringify-object"));
19
19
  var code_builder_1 = require("../../../helpers/code-builder");
20
+ var escape_1 = require("../../../helpers/escape");
20
21
  var headers_1 = require("../../../helpers/headers");
21
22
  exports.xhr = {
22
23
  info: {
@@ -26,9 +27,10 @@ exports.xhr = {
26
27
  description: 'W3C Standard API that provides scripted client functionality',
27
28
  },
28
29
  convert: function (_a, options) {
30
+ var _b;
29
31
  var postData = _a.postData, allHeaders = _a.allHeaders, method = _a.method, fullUrl = _a.fullUrl;
30
32
  var opts = __assign({ indent: ' ', cors: true }, options);
31
- var _b = new code_builder_1.CodeBuilder({ indent: opts.indent }), blank = _b.blank, push = _b.push, join = _b.join;
33
+ var _c = new code_builder_1.CodeBuilder({ indent: opts.indent }), blank = _c.blank, push = _c.push, join = _c.join;
32
34
  switch (postData.mimeType) {
33
35
  case 'application/json':
34
36
  push("const data = JSON.stringify(".concat((0, stringify_object_1.default)(postData.jsonObj, {
@@ -46,7 +48,7 @@ exports.xhr = {
46
48
  });
47
49
  // remove the contentType header
48
50
  if ((0, headers_1.hasHeader)(allHeaders, 'content-type')) {
49
- if (String((0, headers_1.getHeader)(allHeaders, 'content-type')).includes('boundary')) {
51
+ if ((_b = (0, headers_1.getHeader)(allHeaders, 'content-type')) === null || _b === void 0 ? void 0 : _b.includes('boundary')) {
50
52
  var headerName = (0, headers_1.getHeaderName)(allHeaders, 'content-type');
51
53
  if (headerName) {
52
54
  delete allHeaders[headerName];
@@ -72,7 +74,7 @@ exports.xhr = {
72
74
  blank();
73
75
  push("xhr.open('".concat(method, "', '").concat(fullUrl, "');"));
74
76
  Object.keys(allHeaders).forEach(function (key) {
75
- push("xhr.setRequestHeader('".concat(key, "', '").concat(allHeaders[key], "');"));
77
+ push("xhr.setRequestHeader('".concat(key, "', '").concat((0, escape_1.escapeForSingleQuotes)(allHeaders[key]), "');"));
76
78
  });
77
79
  blank();
78
80
  push('xhr.send(data);');
@@ -14,7 +14,7 @@ var data = JSON.stringify({
14
14
  1,
15
15
  'a',
16
16
  {
17
- arr_mix_nested: {}
17
+ arr_mix_nested: []
18
18
  }
19
19
  ],
20
20
  boolean: false
@@ -11,4 +11,5 @@ xhr.open('GET', 'https://httpbin.org/headers');
11
11
  xhr.setRequestHeader('accept', 'application/json');
12
12
  xhr.setRequestHeader('x-foo', 'Bar');
13
13
  xhr.setRequestHeader('x-bar', 'Foo');
14
+ xhr.setRequestHeader('quoted-value', '"quoted" \'string\'');
14
15
  xhr.send(data);
@@ -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();
@@ -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 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
  }