@readme/httpsnippet 2.5.1 → 3.0.3

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 (68) 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 +74 -52
  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
@@ -8,65 +8,76 @@
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
 
25
24
  // Markers for headers to be created as litteral objects and later be set on the NSURLRequest if exist
26
25
  const req = {
27
26
  hasHeaders: false,
28
- hasBody: false
29
- }
27
+ hasBody: false,
28
+ };
30
29
 
31
30
  // We just want to make sure people understand that is the only dependency
32
- code.push('import Foundation')
31
+ code.push('import Foundation');
33
32
 
34
33
  if (Object.keys(source.allHeaders).length) {
35
- req.hasHeaders = true
36
- code.blank()
37
- .push(helpers.literalDeclaration('headers', source.allHeaders, opts))
34
+ req.hasHeaders = true;
35
+ code.blank().push(helpers.literalDeclaration('headers', source.allHeaders, opts));
38
36
  }
39
37
 
40
38
  if (source.postData.text || source.postData.jsonObj || source.postData.params) {
41
- req.hasBody = true
39
+ req.hasBody = true;
42
40
 
43
41
  switch (source.postData.mimeType) {
44
42
  case 'application/x-www-form-urlencoded':
45
43
  // By appending parameters one by one in the resulting snippet,
46
44
  // we make it easier for the user to edit it according to his or her needs after pasting.
47
45
  // The user can just add/remove lines adding/removing body parameters.
48
- code.blank()
49
- .push('let postData = NSMutableData(data: "%s=%s".data(using: String.Encoding.utf8)!)', source.postData.params[0].name, source.postData.params[0].value)
46
+ code
47
+ .blank()
48
+ .push(
49
+ 'let postData = NSMutableData(data: "%s=%s".data(using: String.Encoding.utf8)!)',
50
+ source.postData.params[0].name,
51
+ source.postData.params[0].value
52
+ );
53
+
54
+ // eslint-disable-next-line no-plusplus
50
55
  for (let i = 1, len = source.postData.params.length; i < len; i++) {
51
- code.push('postData.append("&%s=%s".data(using: String.Encoding.utf8)!)', source.postData.params[i].name, source.postData.params[i].value)
56
+ code.push(
57
+ 'postData.append("&%s=%s".data(using: String.Encoding.utf8)!)',
58
+ source.postData.params[i].name,
59
+ source.postData.params[i].value
60
+ );
52
61
  }
53
- break
62
+ break;
54
63
 
55
64
  case 'application/json':
56
65
  if (source.postData.jsonObj) {
57
- code.push(helpers.literalDeclaration('parameters', source.postData.jsonObj, opts), 'as [String : Any]')
66
+ code
67
+ .push(helpers.literalDeclaration('parameters', source.postData.jsonObj, opts), 'as [String : Any]')
58
68
  .blank()
59
- .push('let postData = JSONSerialization.data(withJSONObject: parameters, options: [])')
69
+ .push('let postData = JSONSerialization.data(withJSONObject: parameters, options: [])');
60
70
  }
61
- break
71
+ break;
62
72
 
63
73
  case 'multipart/form-data':
64
74
  /**
65
75
  * By appending multipart parameters one by one in the resulting snippet,
66
76
  * we make it easier for the user to edit it according to his or her needs after pasting.
67
77
  * The user can just edit the parameters NSDictionary or put this part of a snippet in a multipart builder method.
68
- */
69
- code.push(helpers.literalDeclaration('parameters', source.postData.params, opts))
78
+ */
79
+ code
80
+ .push(helpers.literalDeclaration('parameters', source.postData.params, opts))
70
81
  .blank()
71
82
  .push('let boundary = "%s"', source.postData.boundary)
72
83
  .blank()
@@ -88,34 +99,37 @@ module.exports = function (source, options) {
88
99
  .push(1, '} else if let paramValue = param["value"] {')
89
100
  .push(2, 'body += "\\r\\n\\r\\n\\(paramValue)"')
90
101
  .push(1, '}')
91
- .push('}')
92
- break
102
+ .push('}');
103
+ break;
93
104
 
94
105
  default:
95
- code.blank()
96
- .push('let postData = NSData(data: "%s".data(using: String.Encoding.utf8)!)', source.postData.text)
106
+ code.blank().push('let postData = NSData(data: "%s".data(using: String.Encoding.utf8)!)', source.postData.text);
97
107
  }
98
108
  }
99
109
 
100
- code.blank()
110
+ code
111
+ .blank()
101
112
  // NSURLRequestUseProtocolCachePolicy is the default policy, let's just always set it to avoid confusion.
102
113
  .push('let request = NSMutableURLRequest(url: NSURL(string: "%s")! as URL,', source.fullUrl)
103
114
  .push(' cachePolicy: .useProtocolCachePolicy,')
104
115
  .push(' timeoutInterval: %s)', parseInt(opts.timeout, 10).toFixed(1))
105
- .push('request.httpMethod = "%s"', source.method)
116
+ .push('request.httpMethod = "%s"', source.method);
106
117
 
107
118
  if (req.hasHeaders) {
108
- code.push('request.allHTTPHeaderFields = headers')
119
+ code.push('request.allHTTPHeaderFields = headers');
109
120
  }
110
121
 
111
122
  if (req.hasBody) {
112
- code.push('request.httpBody = postData as Data')
123
+ code.push('request.httpBody = postData as Data');
113
124
  }
114
125
 
115
- code.blank()
126
+ code
127
+ .blank()
116
128
  // Retrieving the shared session will be less verbose than creating a new one.
117
129
  .push('let session = URLSession.shared')
118
- .push('let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in')
130
+ .push(
131
+ 'let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in'
132
+ )
119
133
  .push(1, 'if (error != nil) {')
120
134
  .push(2, 'print(error)')
121
135
  .push(1, '} else {')
@@ -125,14 +139,14 @@ module.exports = function (source, options) {
125
139
  .push(1, '}')
126
140
  .push('})')
127
141
  .blank()
128
- .push('dataTask.resume()')
142
+ .push('dataTask.resume()');
129
143
 
130
- return code.join()
131
- }
144
+ return code.join();
145
+ };
132
146
 
133
147
  module.exports.info = {
134
148
  key: 'nsurlsession',
135
149
  title: 'NSURLSession',
136
150
  link: 'https://developer.apple.com/library/mac/documentation/Foundation/Reference/NSURLSession_class/index.html',
137
- description: 'Foundation\'s NSURLSession request'
138
- }
151
+ description: "Foundation's NSURLSession request",
152
+ };