@readme/httpsnippet 2.5.0 → 3.0.2

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 -28
  3. package/src/helpers/code-builder.js +85 -80
  4. package/src/helpers/form-data.js +27 -25
  5. package/src/helpers/headers.js +10 -19
  6. package/src/helpers/reducer.js +10 -14
  7. package/src/index.js +167 -146
  8. package/src/targets/c/index.js +3 -5
  9. package/src/targets/c/libcurl.js +18 -23
  10. package/src/targets/clojure/clj_http.js +103 -87
  11. package/src/targets/clojure/index.js +3 -5
  12. package/src/targets/csharp/httpclient.js +90 -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 +46 -53
  17. package/src/targets/http/http1.1.js +26 -36
  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 +43 -38
  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 +46 -40
  29. package/src/targets/javascript/xhr.js +42 -38
  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 +33 -30
  33. package/src/targets/node/fetch.js +89 -83
  34. package/src/targets/node/index.js +4 -5
  35. package/src/targets/node/native.js +36 -30
  36. package/src/targets/node/request.js +64 -64
  37. package/src/targets/node/unirest.js +56 -55
  38. package/src/targets/objc/helpers.js +24 -23
  39. package/src/targets/objc/index.js +3 -5
  40. package/src/targets/objc/nsurlsession.js +73 -45
  41. package/src/targets/ocaml/cohttp.js +29 -27
  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 +29 -28
  46. package/src/targets/php/http1.js +33 -44
  47. package/src/targets/php/http2.js +70 -71
  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 +37 -34
  54. package/src/targets/python/index.js +4 -5
  55. package/src/targets/python/python3.js +33 -44
  56. package/src/targets/python/requests.js +47 -72
  57. package/src/targets/r/httr.js +59 -70
  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 +39 -33
  61. package/src/targets/shell/curl.js +78 -51
  62. package/src/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 +35 -32
  67. package/src/targets/swift/index.js +3 -5
  68. package/src/targets/swift/nsurlsession.js +53 -39
  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
- const util = require('util')
14
- const stringifyObject = require('stringify-object')
15
- const 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
- const opts = Object.assign({
19
- indent: ' '
20
- }, options)
15
+ const opts = {
16
+ indent: ' ',
17
+ ...options,
18
+ };
21
19
 
22
- let includeFS = false
23
- const 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
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
- const 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
- const 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()
109
-
110
- code.push(util.format('request(options, %s', 'function (error, response, body) {'))
105
+ code.push('const options = %s;', stringifyObject(reqOpts, { indent: ' ', inlineCharacterLimit: 80 })).blank();
111
106
 
107
+ code
108
+ .push('request(options, function (error, response, body) {')
112
109
  .push(1, 'if (error) throw new Error(error);')
113
110
  .blank()
114
111
  .push(1, 'console.log(body);')
115
112
  .push('});')
116
- .blank()
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,112 +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
- const CodeBuilder = require('../../helpers/code-builder')
11
+ const CodeBuilder = require('../../helpers/code-builder');
14
12
 
15
13
  module.exports = function (source, options) {
16
- const opts = Object.assign({
17
- indent: ' '
18
- }, options)
14
+ const opts = {
15
+ indent: ' ',
16
+ ...options,
17
+ };
19
18
 
20
- let includeFS = false
21
- const code = new CodeBuilder(opts.indent)
19
+ let includeFS = false;
20
+ const code = new CodeBuilder(opts.indent);
22
21
 
23
- code.push('const unirest = require("unirest");')
22
+ code
23
+ .push('const unirest = require("unirest");')
24
24
  .blank()
25
25
  .push('const req = unirest("%s", "%s");', source.method, source.url)
26
- .blank()
26
+ .blank();
27
27
 
28
- if (source.cookies.length) {
29
- code.push('const CookieJar = unirest.jar();')
28
+ if (source.allHeaders.cookie) {
29
+ code.push('const CookieJar = unirest.jar();');
30
30
 
31
- source.cookies.forEach(function (cookie) {
32
- code.push('CookieJar.add("%s=%s","%s");', encodeURIComponent(cookie.name), encodeURIComponent(cookie.value), source.url)
33
- })
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
+ });
34
37
 
35
- code.push('req.jar(CookieJar);')
36
- .blank()
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");')
58
+ code
59
+ .push('req.type("json");')
60
60
  .push('req.send(%s);', JSON.stringify(source.postData.jsonObj, null, opts.indent))
61
- .blank()
61
+ .blank();
62
62
  }
63
- break
63
+ break;
64
64
 
65
65
  case 'multipart/form-data': {
66
- const multipart = []
66
+ if (source.postData.params) {
67
+ const multipart = [];
67
68
 
68
- source.postData.params.forEach(function (param) {
69
- const part = {}
69
+ source.postData.params.forEach(function (param) {
70
+ const part = {};
70
71
 
71
- if (param.fileName && !param.value) {
72
- includeFS = true
72
+ if (param.fileName && !param.value) {
73
+ includeFS = true;
73
74
 
74
- part.body = 'fs.createReadStream("' + param.fileName + '")'
75
- } else if (param.value) {
76
- part.body = param.value
77
- }
78
-
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;
91
92
  }
92
93
 
93
94
  default:
94
95
  if (source.postData.text) {
95
- code.push('req.send(%s);', JSON.stringify(source.postData.text, null, opts.indent))
96
- .blank()
96
+ code.push('req.send(%s);', JSON.stringify(source.postData.text, null, opts.indent)).blank();
97
97
  }
98
98
  }
99
99
 
100
100
  if (includeFS) {
101
- code.unshift('const fs = require("fs");')
101
+ code.unshift('const fs = require("fs");');
102
102
  }
103
103
 
104
- code.push('req.end(function (res) {')
104
+ code
105
+ .push('req.end(function (res) {')
105
106
  .push(1, 'if (res.error) throw new Error(res.error);')
106
107
  .blank()
107
108
  .push(1, 'console.log(res.body);')
108
109
  .push('});')
109
- .blank()
110
+ .blank();
110
111
 
111
- return code.join().replace(/"fs\.createReadStream\(\\"(.+)\\"\)"/, 'fs.createReadStream("$1")')
112
- }
112
+ return code.join().replace(/"fs\.createReadStream\(\\"(.+)\\"\)"/, 'fs.createReadStream("$1")');
113
+ };
113
114
 
114
115
  module.exports.info = {
115
116
  key: 'unirest',
116
117
  title: 'Unirest',
117
118
  link: 'http://unirest.io/nodejs.html',
118
- description: 'Lightweight HTTP Request Client Library'
119
- }
119
+ description: 'Lightweight HTTP Request Client Library',
120
+ };
@@ -1,6 +1,4 @@
1
- 'use strict'
2
-
3
- const 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
- const opening = nsClass + ' *' + name + ' = '
37
- const 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,35 +44,37 @@ module.exports = {
45
44
  * @return {string}
46
45
  */
47
46
  literalRepresentation: function (value, indentation) {
48
- const 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
51
+ return `@${value}`;
53
52
 
54
53
  case '[object Array]': {
55
- const valuesRepresentation = value.map(function (v) {
56
- return this.literalRepresentation(v)
57
- }.bind(this))
58
- return '@[ ' + valuesRepresentation.join(join) + ' ]'
54
+ const valuesRepresentation = value.map(
55
+ function (v) {
56
+ return this.literalRepresentation(v);
57
+ }.bind(this)
58
+ );
59
+ return `@[ ${valuesRepresentation.join(join)} ]`;
59
60
  }
60
61
 
61
62
  case '[object Object]': {
62
- const keyValuePairs = []
63
- for (const k in value) {
64
- keyValuePairs.push(util.format('@"%s": %s', k, this.literalRepresentation(value[k])))
65
- }
66
- return '@{ ' + keyValuePairs.join(join) + ' }'
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)} }`;
67
68
  }
68
69
 
69
70
  case '[object Boolean]':
70
- return value ? '@YES' : '@NO'
71
+ return value ? '@YES' : '@NO';
71
72
 
72
73
  default:
73
74
  if (value === null || value === undefined) {
74
- return ''
75
+ return '';
75
76
  }
76
- return '@"' + value.toString().replace(/"/g, '\\"') + '"'
77
+ return `@"${value.toString().replace(/"/g, '\\"')}"`;
77
78
  }
78
- }
79
- }
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
+ };
@@ -8,65 +8,73 @@
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
- const helpers = require('./helpers')
14
- const CodeBuilder = require('../../helpers/code-builder')
11
+ const helpers = require('./helpers');
12
+ const CodeBuilder = require('../../helpers/code-builder');
15
13
 
16
14
  module.exports = function (source, options) {
17
- const opts = Object.assign({
15
+ const opts = {
18
16
  indent: ' ',
19
17
  pretty: true,
20
- timeout: '10'
21
- }, options)
18
+ timeout: '10',
19
+ ...options,
20
+ };
22
21
 
23
- const code = new CodeBuilder(opts.indent)
22
+ const code = new CodeBuilder(opts.indent);
24
23
  // Markers for headers to be created as literal objects and later be set on the NSURLRequest if exist
25
24
  const req = {
26
25
  hasHeaders: false,
27
- hasBody: false
28
- }
26
+ hasBody: false,
27
+ };
29
28
 
30
29
  // We just want to make sure people understand that is the only dependency
31
- code.push('#import <Foundation/Foundation.h>')
30
+ code.push('#import <Foundation/Foundation.h>');
32
31
 
33
32
  if (Object.keys(source.allHeaders).length) {
34
- req.hasHeaders = true
35
- code.blank()
36
- .push(helpers.nsDeclaration('NSDictionary', 'headers', source.allHeaders, opts.pretty))
33
+ req.hasHeaders = true;
34
+ code.blank().push(helpers.nsDeclaration('NSDictionary', 'headers', source.allHeaders, opts.pretty));
37
35
  }
38
36
 
39
37
  if (source.postData.text || source.postData.jsonObj || source.postData.params) {
40
- req.hasBody = true
38
+ req.hasBody = true;
41
39
 
42
40
  switch (source.postData.mimeType) {
43
41
  case 'application/x-www-form-urlencoded':
44
42
  // By appending parameters one by one in the resulting snippet,
45
43
  // we make it easier for the user to edit it according to his or her needs after pasting.
46
44
  // The user can just add/remove lines adding/removing body parameters.
47
- code.blank()
48
- .push('NSMutableData *postData = [[NSMutableData alloc] initWithData:[@"%s=%s" dataUsingEncoding:NSUTF8StringEncoding]];',
49
- source.postData.params[0].name, source.postData.params[0].value)
45
+ code
46
+ .blank()
47
+ .push(
48
+ 'NSMutableData *postData = [[NSMutableData alloc] initWithData:[@"%s=%s" dataUsingEncoding:NSUTF8StringEncoding]];',
49
+ source.postData.params[0].name,
50
+ source.postData.params[0].value
51
+ );
50
52
 
53
+ // eslint-disable-next-line no-plusplus
51
54
  for (let i = 1, len = source.postData.params.length; i < len; i++) {
52
- code.push('[postData appendData:[@"&%s=%s" dataUsingEncoding:NSUTF8StringEncoding]];',
53
- source.postData.params[i].name, source.postData.params[i].value)
55
+ code.push(
56
+ '[postData appendData:[@"&%s=%s" dataUsingEncoding:NSUTF8StringEncoding]];',
57
+ source.postData.params[i].name,
58
+ source.postData.params[i].value
59
+ );
54
60
  }
55
- break
61
+ break;
56
62
 
57
63
  case 'application/json':
58
64
  if (source.postData.jsonObj) {
59
- code.push(helpers.nsDeclaration('NSDictionary', 'parameters', source.postData.jsonObj, opts.pretty))
65
+ code
66
+ .push(helpers.nsDeclaration('NSDictionary', 'parameters', source.postData.jsonObj, opts.pretty))
60
67
  .blank()
61
- .push('NSData *postData = [NSJSONSerialization dataWithJSONObject:parameters options:0 error:nil];')
68
+ .push('NSData *postData = [NSJSONSerialization dataWithJSONObject:parameters options:0 error:nil];');
62
69
  }
63
- break
70
+ break;
64
71
 
65
72
  case 'multipart/form-data':
66
73
  // By appending multipart parameters one by one in the resulting snippet,
67
74
  // we make it easier for the user to edit it according to his or her needs after pasting.
68
75
  // The user can just edit the parameters NSDictionary or put this part of a snippet in a multipart builder method.
69
- code.push(helpers.nsDeclaration('NSArray', 'parameters', source.postData.params, opts.pretty))
76
+ code
77
+ .push(helpers.nsDeclaration('NSArray', 'parameters', source.postData.params, opts.pretty))
70
78
  .push('NSString *boundary = @"%s";', source.postData.boundary)
71
79
  .blank()
72
80
  .push('NSError *error;')
@@ -74,9 +82,15 @@ module.exports = function (source, options) {
74
82
  .push('for (NSDictionary *param in parameters) {')
75
83
  .push(1, '[body appendFormat:@"--%@\\r\\n", boundary];')
76
84
  .push(1, 'if (param[@"fileName"]) {')
77
- .push(2, '[body appendFormat:@"Content-Disposition:form-data; name=\\"%@\\"; filename=\\"%@\\"\\r\\n", param[@"name"], param[@"fileName"]];')
85
+ .push(
86
+ 2,
87
+ '[body appendFormat:@"Content-Disposition:form-data; name=\\"%@\\"; filename=\\"%@\\"\\r\\n", param[@"name"], param[@"fileName"]];'
88
+ )
78
89
  .push(2, '[body appendFormat:@"Content-Type: %@\\r\\n\\r\\n", param[@"contentType"]];')
79
- .push(2, '[body appendFormat:@"%@", [NSString stringWithContentsOfFile:param[@"fileName"] encoding:NSUTF8StringEncoding error:&error]];')
90
+ .push(
91
+ 2,
92
+ '[body appendFormat:@"%@", [NSString stringWithContentsOfFile:param[@"fileName"] encoding:NSUTF8StringEncoding error:&error]];'
93
+ )
80
94
  .push(2, 'if (error) {')
81
95
  .push(3, 'NSLog(@"%@", error);')
82
96
  .push(2, '}')
@@ -86,51 +100,65 @@ module.exports = function (source, options) {
86
100
  .push(1, '}')
87
101
  .push('}')
88
102
  .push('[body appendFormat:@"\\r\\n--%@--\\r\\n", boundary];')
89
- .push('NSData *postData = [body dataUsingEncoding:NSUTF8StringEncoding];')
90
- break
103
+ .push('NSData *postData = [body dataUsingEncoding:NSUTF8StringEncoding];');
104
+ break;
91
105
 
92
106
  default:
93
- code.blank()
94
- .push('NSData *postData = [[NSData alloc] initWithData:[@"' + source.postData.text + '" dataUsingEncoding:NSUTF8StringEncoding]];')
107
+ code
108
+ .blank()
109
+ .push(
110
+ `NSData *postData = [[NSData alloc] initWithData:[@"${source.postData.text}" dataUsingEncoding:NSUTF8StringEncoding]];`
111
+ );
95
112
  }
96
113
  }
97
114
 
98
- code.blank()
99
- .push('NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"' + source.fullUrl + '"]')
115
+ code
116
+ .blank()
117
+ .push(
118
+ `NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"${source.fullUrl}"]`
119
+ )
100
120
  // NSURLRequestUseProtocolCachePolicy is the default policy, let's just always set it to avoid confusion.
101
121
  .push(' cachePolicy:NSURLRequestUseProtocolCachePolicy')
102
- .push(' timeoutInterval:' + parseInt(opts.timeout, 10).toFixed(1) + '];')
103
- .push('[request setHTTPMethod:@"' + source.method + '"];')
122
+ .push(
123
+ ` timeoutInterval:${parseInt(opts.timeout, 10).toFixed(1)}];`
124
+ )
125
+ .push(`[request setHTTPMethod:@"${source.method}"];`);
104
126
 
105
127
  if (req.hasHeaders) {
106
- code.push('[request setAllHTTPHeaderFields:headers];')
128
+ code.push('[request setAllHTTPHeaderFields:headers];');
107
129
  }
108
130
 
109
131
  if (req.hasBody) {
110
- code.push('[request setHTTPBody:postData];')
132
+ code.push('[request setHTTPBody:postData];');
111
133
  }
112
134
 
113
- code.blank()
135
+ code
136
+ .blank()
114
137
  // Retrieving the shared session will be less verbose than creating a new one.
115
138
  .push('NSURLSession *session = [NSURLSession sharedSession];')
116
139
  .push('NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request')
117
- .push(' completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {')
140
+ .push(
141
+ ' completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {'
142
+ )
118
143
  .push(1, ' if (error) {')
119
144
  .push(2, ' NSLog(@"%@", error);')
120
145
  .push(1, ' } else {')
121
146
  // Casting the NSURLResponse to NSHTTPURLResponse so the user can see the status .
122
- .push(2, ' NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;')
147
+ .push(
148
+ 2,
149
+ ' NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;'
150
+ )
123
151
  .push(2, ' NSLog(@"%@", httpResponse);')
124
152
  .push(1, ' }')
125
153
  .push(' }];')
126
- .push('[dataTask resume];')
154
+ .push('[dataTask resume];');
127
155
 
128
- return code.join()
129
- }
156
+ return code.join();
157
+ };
130
158
 
131
159
  module.exports.info = {
132
160
  key: 'nsurlsession',
133
161
  title: 'NSURLSession',
134
162
  link: 'https://developer.apple.com/library/mac/documentation/Foundation/Reference/NSURLSession_class/index.html',
135
- description: 'Foundation\'s NSURLSession request'
136
- }
163
+ description: "Foundation's NSURLSession request",
164
+ };