@readme/httpsnippet 2.4.5 → 3.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (69) hide show
  1. package/README.md +19 -9
  2. package/package.json +24 -27
  3. package/src/helpers/code-builder.js +85 -80
  4. package/src/helpers/form-data.js +28 -26
  5. package/src/helpers/headers.js +18 -8
  6. package/src/helpers/reducer.js +10 -14
  7. package/src/index.js +175 -151
  8. package/src/targets/c/index.js +3 -5
  9. package/src/targets/c/libcurl.js +20 -25
  10. package/src/targets/clojure/clj_http.js +126 -102
  11. package/src/targets/clojure/index.js +3 -5
  12. package/src/targets/csharp/httpclient.js +93 -90
  13. package/src/targets/csharp/index.js +3 -5
  14. package/src/targets/csharp/restsharp.js +20 -22
  15. package/src/targets/go/index.js +3 -5
  16. package/src/targets/go/native.js +47 -54
  17. package/src/targets/http/http1.1.js +27 -37
  18. package/src/targets/http/index.js +3 -5
  19. package/src/targets/index.js +2 -4
  20. package/src/targets/java/asynchttp.js +22 -23
  21. package/src/targets/java/index.js +3 -5
  22. package/src/targets/java/nethttp.js +20 -24
  23. package/src/targets/java/okhttp.js +31 -31
  24. package/src/targets/java/unirest.js +22 -19
  25. package/src/targets/javascript/axios.js +47 -42
  26. package/src/targets/javascript/fetch.js +66 -68
  27. package/src/targets/javascript/index.js +3 -5
  28. package/src/targets/javascript/jquery.js +50 -44
  29. package/src/targets/javascript/xhr.js +48 -44
  30. package/src/targets/kotlin/index.js +3 -5
  31. package/src/targets/kotlin/okhttp.js +31 -31
  32. package/src/targets/node/axios.js +37 -34
  33. package/src/targets/node/fetch.js +94 -87
  34. package/src/targets/node/index.js +4 -5
  35. package/src/targets/node/native.js +51 -45
  36. package/src/targets/node/request.js +69 -69
  37. package/src/targets/node/unirest.js +70 -68
  38. package/src/targets/objc/helpers.js +32 -25
  39. package/src/targets/objc/index.js +3 -5
  40. package/src/targets/objc/nsurlsession.js +108 -79
  41. package/src/targets/ocaml/cohttp.js +32 -30
  42. package/src/targets/ocaml/index.js +3 -5
  43. package/src/targets/php/curl.js +86 -82
  44. package/src/targets/php/guzzle.js +135 -0
  45. package/src/targets/php/helpers.js +31 -29
  46. package/src/targets/php/http1.js +38 -49
  47. package/src/targets/php/http2.js +75 -75
  48. package/src/targets/php/index.js +5 -5
  49. package/src/targets/powershell/common.js +35 -30
  50. package/src/targets/powershell/index.js +3 -5
  51. package/src/targets/powershell/restmethod.js +3 -5
  52. package/src/targets/powershell/webrequest.js +3 -5
  53. package/src/targets/python/helpers.js +42 -37
  54. package/src/targets/python/index.js +4 -5
  55. package/src/targets/python/python3.js +33 -45
  56. package/src/targets/python/requests.js +48 -72
  57. package/src/targets/r/httr.js +60 -73
  58. package/src/targets/r/index.js +3 -5
  59. package/src/targets/ruby/index.js +3 -5
  60. package/src/targets/ruby/native.js +41 -35
  61. package/src/targets/shell/curl.js +108 -43
  62. package/src/{helpers/shell.js → targets/shell/helpers.js} +7 -9
  63. package/src/targets/shell/httpie.js +45 -39
  64. package/src/targets/shell/index.js +4 -5
  65. package/src/targets/shell/wget.js +20 -22
  66. package/src/targets/swift/helpers.js +44 -35
  67. package/src/targets/swift/index.js +3 -5
  68. package/src/targets/swift/nsurlsession.js +93 -79
  69. package/src/.DS_Store +0 -0
@@ -8,119 +8,119 @@
8
8
  * for any questions or issues regarding the generated code snippet, please open an issue mentioning the author.
9
9
  */
10
10
 
11
- 'use strict'
12
-
13
- var util = require('util')
14
- var stringifyObject = require('stringify-object')
15
- var CodeBuilder = require('../../helpers/code-builder')
11
+ const stringifyObject = require('stringify-object');
12
+ const CodeBuilder = require('../../helpers/code-builder');
16
13
 
17
14
  module.exports = function (source, options) {
18
- var opts = Object.assign({
19
- indent: ' '
20
- }, options)
15
+ const opts = {
16
+ indent: ' ',
17
+ ...options,
18
+ };
21
19
 
22
- var includeFS = false
23
- var code = new CodeBuilder(opts.indent)
20
+ let includeFS = false;
21
+ const code = new CodeBuilder(opts.indent);
24
22
 
25
- code.push("const request = require('request');")
26
- .blank()
23
+ code.push("const request = require('request');").blank();
27
24
 
28
- var reqOpts = {
25
+ const reqOpts = {
29
26
  method: source.method,
30
- url: source.url
31
- }
32
-
33
- if (Object.keys(source.queryObj).length) {
34
- reqOpts.qs = source.queryObj
35
- }
27
+ url: source.fullUrl,
28
+ };
36
29
 
37
30
  if (Object.keys(source.headersObj).length) {
38
- reqOpts.headers = source.headersObj
31
+ reqOpts.headers = source.headersObj;
39
32
  }
40
33
 
41
34
  switch (source.postData.mimeType) {
42
35
  case 'application/x-www-form-urlencoded':
43
- reqOpts.form = source.postData.paramsObj
44
- break
36
+ reqOpts.form = source.postData.paramsObj;
37
+ break;
45
38
 
46
39
  case 'application/json':
47
40
  if (source.postData.jsonObj) {
48
- reqOpts.body = source.postData.jsonObj
49
- reqOpts.json = true
41
+ reqOpts.body = source.postData.jsonObj;
42
+ reqOpts.json = true;
50
43
  }
51
- break
44
+ break;
52
45
 
53
46
  case 'multipart/form-data':
54
- reqOpts.formData = {}
47
+ if (source.postData.params) {
48
+ reqOpts.formData = {};
55
49
 
56
- source.postData.params.forEach(function (param) {
57
- var attachment = {}
50
+ source.postData.params.forEach(function (param) {
51
+ const attachment = {};
58
52
 
59
- if (!param.fileName && !param.fileName && !param.contentType) {
60
- reqOpts.formData[param.name] = param.value
61
- return
62
- }
53
+ if (!param.fileName && !param.contentType) {
54
+ reqOpts.formData[param.name] = param.value;
55
+ return;
56
+ }
63
57
 
64
- if (param.fileName) {
65
- includeFS = true
58
+ if (param.fileName) {
59
+ includeFS = true;
66
60
 
67
- attachment.value = 'fs.createReadStream("' + param.fileName + '")'
68
- } else if (param.value) {
69
- attachment.value = param.value
70
- }
61
+ attachment.value = `fs.createReadStream("${param.fileName}")`;
62
+ } else if (param.value) {
63
+ attachment.value = param.value;
64
+ }
71
65
 
72
- if (param.fileName) {
73
- attachment.options = {
74
- filename: param.fileName,
75
- contentType: param.contentType ? param.contentType : null
66
+ if (param.fileName) {
67
+ attachment.options = {
68
+ filename: param.fileName,
69
+ contentType: param.contentType ? param.contentType : null,
70
+ };
76
71
  }
77
- }
78
72
 
79
- reqOpts.formData[param.name] = attachment
80
- })
81
- break
73
+ reqOpts.formData[param.name] = attachment;
74
+ });
75
+ }
76
+ break;
82
77
 
83
78
  default:
84
79
  if (source.postData.text) {
85
- reqOpts.body = source.postData.text
80
+ reqOpts.body = source.postData.text;
86
81
  }
87
82
  }
88
83
 
89
84
  // construct cookies argument
90
- if (source.cookies.length) {
91
- reqOpts.jar = 'JAR'
85
+ if (source.allHeaders.cookie) {
86
+ reqOpts.jar = 'JAR';
92
87
 
93
- code.push('const jar = request.jar();')
88
+ code.push('const jar = request.jar();');
94
89
 
95
- var url = source.url
90
+ const url = source.url;
96
91
 
97
- source.cookies.forEach(function (cookie) {
98
- code.push("jar.setCookie(request.cookie('%s=%s'), '%s');", encodeURIComponent(cookie.name), encodeURIComponent(cookie.value), url)
99
- })
100
- code.blank()
92
+ // Cookies are already encoded within `source.allHeaders` so we can pull them out of that instead of doing our
93
+ // own encoding work.
94
+ source.allHeaders.cookie.split('; ').forEach(function (cookie) {
95
+ const [name, value] = cookie.split('=');
96
+ code.push("jar.setCookie(request.cookie('%s=%s'), '%s');", name, value, url);
97
+ });
98
+ code.blank();
101
99
  }
102
100
 
103
101
  if (includeFS) {
104
- code.unshift("const fs = require('fs');")
102
+ code.unshift("const fs = require('fs');");
105
103
  }
106
104
 
107
- code.push('const options = %s;', stringifyObject(reqOpts, { indent: ' ', inlineCharacterLimit: 80 }))
108
- .blank()
105
+ code.push('const options = %s;', stringifyObject(reqOpts, { indent: ' ', inlineCharacterLimit: 80 })).blank();
109
106
 
110
- code.push(util.format('request(options, %s', 'function (error, response, body) {'))
111
-
112
- .push(1, 'if (error) throw new Error(error);')
113
- .blank()
114
- .push(1, 'console.log(body);')
115
- .push('});')
116
- .blank()
107
+ code
108
+ .push('request(options, function (error, response, body) {')
109
+ .push(1, 'if (error) throw new Error(error);')
110
+ .blank()
111
+ .push(1, 'console.log(body);')
112
+ .push('});')
113
+ .blank();
117
114
 
118
- return code.join().replace('"JAR"', 'jar').replace(/'fs\.createReadStream\("(.+)"\)'/g, "fs.createReadStream('$1')")
119
- }
115
+ return code
116
+ .join()
117
+ .replace("'JAR'", 'jar')
118
+ .replace(/'fs\.createReadStream\("(.+)"\)'/g, "fs.createReadStream('$1')");
119
+ };
120
120
 
121
121
  module.exports.info = {
122
122
  key: 'request',
123
123
  title: 'Request',
124
124
  link: 'https://github.com/request/request',
125
- description: 'Simplified HTTP request client'
126
- }
125
+ description: 'Simplified HTTP request client',
126
+ };
@@ -8,111 +8,113 @@
8
8
  * for any questions or issues regarding the generated code snippet, please open an issue mentioning the author.
9
9
  */
10
10
 
11
- 'use strict'
12
-
13
- var CodeBuilder = require('../../helpers/code-builder')
11
+ const CodeBuilder = require('../../helpers/code-builder');
14
12
 
15
13
  module.exports = function (source, options) {
16
- var opts = Object.assign({
17
- indent: ' '
18
- }, options)
19
-
20
- var includeFS = false
21
- var code = new CodeBuilder(opts.indent)
22
-
23
- code.push('const unirest = require("unirest");')
24
- .blank()
25
- .push('const req = unirest("%s", "%s");', source.method, source.url)
26
- .blank()
27
-
28
- if (source.cookies.length) {
29
- code.push('const CookieJar = unirest.jar();')
30
-
31
- source.cookies.forEach(function (cookie) {
32
- code.push('CookieJar.add("%s=%s","%s");', encodeURIComponent(cookie.name), encodeURIComponent(cookie.value), source.url)
33
- })
34
-
35
- code.push('req.jar(CookieJar);')
36
- .blank()
14
+ const opts = {
15
+ indent: ' ',
16
+ ...options,
17
+ };
18
+
19
+ let includeFS = false;
20
+ const code = new CodeBuilder(opts.indent);
21
+
22
+ code
23
+ .push('const unirest = require("unirest");')
24
+ .blank()
25
+ .push('const req = unirest("%s", "%s");', source.method, source.url)
26
+ .blank();
27
+
28
+ if (source.allHeaders.cookie) {
29
+ code.push('const CookieJar = unirest.jar();');
30
+
31
+ // Cookies are already encoded within `source.allHeaders` so we can pull them out of that instead of doing our
32
+ // own encoding work.
33
+ source.allHeaders.cookie.split('; ').forEach(function (cookie) {
34
+ const [name, value] = cookie.split('=');
35
+ code.push('CookieJar.add("%s=%s","%s");', name, value, source.url);
36
+ });
37
+
38
+ code.push('req.jar(CookieJar);').blank();
37
39
  }
38
40
 
39
41
  if (Object.keys(source.queryObj).length) {
40
- code.push('req.query(%s);', JSON.stringify(source.queryObj, null, opts.indent))
41
- .blank()
42
+ code.push('req.query(%s);', JSON.stringify(source.queryObj, null, opts.indent)).blank();
42
43
  }
43
44
 
44
45
  if (Object.keys(source.headersObj).length) {
45
- code.push('req.headers(%s);', JSON.stringify(source.headersObj, null, opts.indent))
46
- .blank()
46
+ code.push('req.headers(%s);', JSON.stringify(source.headersObj, null, opts.indent)).blank();
47
47
  }
48
48
 
49
49
  switch (source.postData.mimeType) {
50
50
  case 'application/x-www-form-urlencoded':
51
51
  if (source.postData.paramsObj) {
52
- code.push('req.form(%s);', JSON.stringify(source.postData.paramsObj, null, opts.indent))
53
- .blank()
52
+ code.push('req.form(%s);', JSON.stringify(source.postData.paramsObj, null, opts.indent)).blank();
54
53
  }
55
- break
54
+ break;
56
55
 
57
56
  case 'application/json':
58
57
  if (source.postData.jsonObj) {
59
- code.push('req.type("json");')
60
- .push('req.send(%s);', JSON.stringify(source.postData.jsonObj, null, opts.indent))
61
- .blank()
58
+ code
59
+ .push('req.type("json");')
60
+ .push('req.send(%s);', JSON.stringify(source.postData.jsonObj, null, opts.indent))
61
+ .blank();
62
62
  }
63
- break
64
-
65
- case 'multipart/form-data':
66
- var multipart = []
63
+ break;
67
64
 
68
- source.postData.params.forEach(function (param) {
69
- var part = {}
65
+ case 'multipart/form-data': {
66
+ if (source.postData.params) {
67
+ const multipart = [];
70
68
 
71
- if (param.fileName && !param.value) {
72
- includeFS = true
69
+ source.postData.params.forEach(function (param) {
70
+ const part = {};
73
71
 
74
- part.body = 'fs.createReadStream("' + param.fileName + '")'
75
- } else if (param.value) {
76
- part.body = param.value
77
- }
72
+ if (param.fileName && !param.value) {
73
+ includeFS = true;
78
74
 
79
- if (part.body) {
80
- if (param.contentType) {
81
- part['content-type'] = param.contentType
75
+ part.body = `fs.createReadStream("${param.fileName}")`;
76
+ } else if (param.value) {
77
+ part.body = param.value;
82
78
  }
83
79
 
84
- multipart.push(part)
85
- }
86
- })
80
+ if (part.body) {
81
+ if (param.contentType) {
82
+ part['content-type'] = param.contentType;
83
+ }
87
84
 
88
- code.push('req.multipart(%s);', JSON.stringify(multipart, null, opts.indent))
89
- .blank()
90
- break
85
+ multipart.push(part);
86
+ }
87
+ });
88
+
89
+ code.push('req.multipart(%s);', JSON.stringify(multipart, null, opts.indent)).blank();
90
+ }
91
+ break;
92
+ }
91
93
 
92
94
  default:
93
95
  if (source.postData.text) {
94
- code.push('req.send(%s);', JSON.stringify(source.postData.text, null, opts.indent))
95
- .blank()
96
+ code.push('req.send(%s);', JSON.stringify(source.postData.text, null, opts.indent)).blank();
96
97
  }
97
98
  }
98
99
 
99
100
  if (includeFS) {
100
- code.unshift('const fs = require("fs");')
101
+ code.unshift('const fs = require("fs");');
101
102
  }
102
103
 
103
- code.push('req.end(function (res) {')
104
- .push(1, 'if (res.error) throw new Error(res.error);')
105
- .blank()
106
- .push(1, 'console.log(res.body);')
107
- .push('});')
108
- .blank()
104
+ code
105
+ .push('req.end(function (res) {')
106
+ .push(1, 'if (res.error) throw new Error(res.error);')
107
+ .blank()
108
+ .push(1, 'console.log(res.body);')
109
+ .push('});')
110
+ .blank();
109
111
 
110
- return code.join().replace(/"fs\.createReadStream\(\\"(.+)\\"\)"/, 'fs.createReadStream("$1")')
111
- }
112
+ return code.join().replace(/"fs\.createReadStream\(\\"(.+)\\"\)"/, 'fs.createReadStream("$1")');
113
+ };
112
114
 
113
115
  module.exports.info = {
114
116
  key: 'unirest',
115
117
  title: 'Unirest',
116
118
  link: 'http://unirest.io/nodejs.html',
117
- description: 'Lightweight HTTP Request Client Library'
118
- }
119
+ description: 'Lightweight HTTP Request Client Library',
120
+ };
@@ -1,6 +1,4 @@
1
- 'use strict'
2
-
3
- var util = require('util')
1
+ const { format } = require('util');
4
2
 
5
3
  module.exports = {
6
4
  /**
@@ -10,7 +8,8 @@ module.exports = {
10
8
  * @return {string}
11
9
  */
12
10
  blankString: function (length) {
13
- return Array.apply(null, new Array(length)).map(String.prototype.valueOf, ' ').join('')
11
+ // eslint-disable-next-line prefer-spread
12
+ return Array.apply(null, new Array(length)).map(String.prototype.valueOf, ' ').join('');
14
13
  },
15
14
 
16
15
  /**
@@ -33,9 +32,9 @@ module.exports = {
33
32
  * NSDictionary *params = @{ @"a": @"b", @"c": @"d" };
34
33
  */
35
34
  nsDeclaration: function (nsClass, name, parameters, indent) {
36
- var opening = nsClass + ' *' + name + ' = '
37
- var literal = this.literalRepresentation(parameters, indent ? opening.length : undefined)
38
- return opening + literal + ';'
35
+ const opening = `${nsClass} *${name} = `;
36
+ const literal = this.literalRepresentation(parameters, indent ? opening.length : undefined);
37
+ return `${opening + literal};`;
39
38
  },
40
39
 
41
40
  /**
@@ -45,29 +44,37 @@ module.exports = {
45
44
  * @return {string}
46
45
  */
47
46
  literalRepresentation: function (value, indentation) {
48
- var join = indentation === undefined ? ', ' : ',\n ' + this.blankString(indentation)
47
+ const join = indentation === undefined ? ', ' : `,\n ${this.blankString(indentation)}`;
49
48
 
50
49
  switch (Object.prototype.toString.call(value)) {
51
50
  case '[object Number]':
52
- return '@' + value
53
- case '[object Array]':
54
- var valuesRepresentation = value.map(function (v) {
55
- return this.literalRepresentation(v)
56
- }.bind(this))
57
- return '@[ ' + valuesRepresentation.join(join) + ' ]'
58
- case '[object Object]':
59
- var keyValuePairs = []
60
- for (var k in value) {
61
- keyValuePairs.push(util.format('@"%s": %s', k, this.literalRepresentation(value[k])))
62
- }
63
- return '@{ ' + keyValuePairs.join(join) + ' }'
51
+ return `@${value}`;
52
+
53
+ case '[object Array]': {
54
+ const valuesRepresentation = value.map(
55
+ function (v) {
56
+ return this.literalRepresentation(v);
57
+ }.bind(this)
58
+ );
59
+ return `@[ ${valuesRepresentation.join(join)} ]`;
60
+ }
61
+
62
+ case '[object Object]': {
63
+ const keyValuePairs = [];
64
+ Object.keys(value).forEach(k => {
65
+ keyValuePairs.push(format('@"%s": %s', k, this.literalRepresentation(value[k])));
66
+ });
67
+ return `@{ ${keyValuePairs.join(join)} }`;
68
+ }
69
+
64
70
  case '[object Boolean]':
65
- return value ? '@YES' : '@NO'
71
+ return value ? '@YES' : '@NO';
72
+
66
73
  default:
67
74
  if (value === null || value === undefined) {
68
- return ''
75
+ return '';
69
76
  }
70
- return '@"' + value.toString().replace(/"/g, '\\"') + '"'
77
+ return `@"${value.toString().replace(/"/g, '\\"')}"`;
71
78
  }
72
- }
73
- }
79
+ },
80
+ };
@@ -1,12 +1,10 @@
1
- 'use strict'
2
-
3
1
  module.exports = {
4
2
  info: {
5
3
  key: 'objc',
6
4
  title: 'Objective-C',
7
5
  extname: '.m',
8
- default: 'nsurlsession'
6
+ default: 'nsurlsession',
9
7
  },
10
8
 
11
- nsurlsession: require('./nsurlsession')
12
- }
9
+ nsurlsession: require('./nsurlsession'),
10
+ };