@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,128 +8,157 @@
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 helpers = require('./helpers')
14
- var 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
- var 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
- var code = new CodeBuilder(opts.indent)
24
- // Markers for headers to be created as litteral objects and later be set on the NSURLRequest if exist
25
- var req = {
22
+ const code = new CodeBuilder(opts.indent);
23
+ // Markers for headers to be created as literal objects and later be set on the NSURLRequest if exist
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)
50
- for (var i = 1, len = source.postData.params.length; i < len; i++) {
51
- code.push('[postData appendData:[@"&%s=%s" dataUsingEncoding:NSUTF8StringEncoding]];',
52
- source.postData.params[i].name, source.postData.params[i].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
+ );
52
+
53
+ // eslint-disable-next-line no-plusplus
54
+ for (let i = 1, len = source.postData.params.length; i < len; i++) {
55
+ code.push(
56
+ '[postData appendData:[@"&%s=%s" dataUsingEncoding:NSUTF8StringEncoding]];',
57
+ source.postData.params[i].name,
58
+ source.postData.params[i].value
59
+ );
53
60
  }
54
- break
61
+ break;
55
62
 
56
63
  case 'application/json':
57
64
  if (source.postData.jsonObj) {
58
- code.push(helpers.nsDeclaration('NSDictionary', 'parameters', source.postData.jsonObj, opts.pretty))
59
- .blank()
60
- .push('NSData *postData = [NSJSONSerialization dataWithJSONObject:parameters options:0 error:nil];')
65
+ code
66
+ .push(helpers.nsDeclaration('NSDictionary', 'parameters', source.postData.jsonObj, opts.pretty))
67
+ .blank()
68
+ .push('NSData *postData = [NSJSONSerialization dataWithJSONObject:parameters options:0 error:nil];');
61
69
  }
62
- break
70
+ break;
63
71
 
64
72
  case 'multipart/form-data':
65
73
  // By appending multipart parameters one by one in the resulting snippet,
66
74
  // we make it easier for the user to edit it according to his or her needs after pasting.
67
75
  // The user can just edit the parameters NSDictionary or put this part of a snippet in a multipart builder method.
68
- code.push(helpers.nsDeclaration('NSArray', 'parameters', source.postData.params, opts.pretty))
69
- .push('NSString *boundary = @"%s";', source.postData.boundary)
70
- .blank()
71
- .push('NSError *error;')
72
- .push('NSMutableString *body = [NSMutableString string];')
73
- .push('for (NSDictionary *param in parameters) {')
74
- .push(1, '[body appendFormat:@"--%@\\r\\n", boundary];')
75
- .push(1, 'if (param[@"fileName"]) {')
76
- .push(2, '[body appendFormat:@"Content-Disposition:form-data; name=\\"%@\\"; filename=\\"%@\\"\\r\\n", param[@"name"], param[@"fileName"]];')
77
- .push(2, '[body appendFormat:@"Content-Type: %@\\r\\n\\r\\n", param[@"contentType"]];')
78
- .push(2, '[body appendFormat:@"%@", [NSString stringWithContentsOfFile:param[@"fileName"] encoding:NSUTF8StringEncoding error:&error]];')
79
- .push(2, 'if (error) {')
80
- .push(3, 'NSLog(@"%@", error);')
81
- .push(2, '}')
82
- .push(1, '} else {')
83
- .push(2, '[body appendFormat:@"Content-Disposition:form-data; name=\\"%@\\"\\r\\n\\r\\n", param[@"name"]];')
84
- .push(2, '[body appendFormat:@"%@", param[@"value"]];')
85
- .push(1, '}')
86
- .push('}')
87
- .push('[body appendFormat:@"\\r\\n--%@--\\r\\n", boundary];')
88
- .push('NSData *postData = [body dataUsingEncoding:NSUTF8StringEncoding];')
89
- break
76
+ code
77
+ .push(helpers.nsDeclaration('NSArray', 'parameters', source.postData.params, opts.pretty))
78
+ .push('NSString *boundary = @"%s";', source.postData.boundary)
79
+ .blank()
80
+ .push('NSError *error;')
81
+ .push('NSMutableString *body = [NSMutableString string];')
82
+ .push('for (NSDictionary *param in parameters) {')
83
+ .push(1, '[body appendFormat:@"--%@\\r\\n", boundary];')
84
+ .push(1, 'if (param[@"fileName"]) {')
85
+ .push(
86
+ 2,
87
+ '[body appendFormat:@"Content-Disposition:form-data; name=\\"%@\\"; filename=\\"%@\\"\\r\\n", param[@"name"], param[@"fileName"]];'
88
+ )
89
+ .push(2, '[body appendFormat:@"Content-Type: %@\\r\\n\\r\\n", param[@"contentType"]];')
90
+ .push(
91
+ 2,
92
+ '[body appendFormat:@"%@", [NSString stringWithContentsOfFile:param[@"fileName"] encoding:NSUTF8StringEncoding error:&error]];'
93
+ )
94
+ .push(2, 'if (error) {')
95
+ .push(3, 'NSLog(@"%@", error);')
96
+ .push(2, '}')
97
+ .push(1, '} else {')
98
+ .push(2, '[body appendFormat:@"Content-Disposition:form-data; name=\\"%@\\"\\r\\n\\r\\n", param[@"name"]];')
99
+ .push(2, '[body appendFormat:@"%@", param[@"value"]];')
100
+ .push(1, '}')
101
+ .push('}')
102
+ .push('[body appendFormat:@"\\r\\n--%@--\\r\\n", boundary];')
103
+ .push('NSData *postData = [body dataUsingEncoding:NSUTF8StringEncoding];');
104
+ break;
90
105
 
91
106
  default:
92
- code.blank()
93
- .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
+ );
94
112
  }
95
113
  }
96
114
 
97
- code.blank()
98
- .push('NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"' + source.fullUrl + '"]')
99
- // NSURLRequestUseProtocolCachePolicy is the default policy, let's just always set it to avoid confusion.
100
- .push(' cachePolicy:NSURLRequestUseProtocolCachePolicy')
101
- .push(' timeoutInterval:' + parseInt(opts.timeout, 10).toFixed(1) + '];')
102
- .push('[request setHTTPMethod:@"' + source.method + '"];')
115
+ code
116
+ .blank()
117
+ .push(
118
+ `NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"${source.fullUrl}"]`
119
+ )
120
+ // NSURLRequestUseProtocolCachePolicy is the default policy, let's just always set it to avoid confusion.
121
+ .push(' cachePolicy:NSURLRequestUseProtocolCachePolicy')
122
+ .push(
123
+ ` timeoutInterval:${parseInt(opts.timeout, 10).toFixed(1)}];`
124
+ )
125
+ .push(`[request setHTTPMethod:@"${source.method}"];`);
103
126
 
104
127
  if (req.hasHeaders) {
105
- code.push('[request setAllHTTPHeaderFields:headers];')
128
+ code.push('[request setAllHTTPHeaderFields:headers];');
106
129
  }
107
130
 
108
131
  if (req.hasBody) {
109
- code.push('[request setHTTPBody:postData];')
132
+ code.push('[request setHTTPBody:postData];');
110
133
  }
111
134
 
112
- code.blank()
113
- // Retrieving the shared session will be less verbose than creating a new one.
114
- .push('NSURLSession *session = [NSURLSession sharedSession];')
115
- .push('NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request')
116
- .push(' completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {')
117
- .push(1, ' if (error) {')
118
- .push(2, ' NSLog(@"%@", error);')
119
- .push(1, ' } else {')
120
- // Casting the NSURLResponse to NSHTTPURLResponse so the user can see the status .
121
- .push(2, ' NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;')
122
- .push(2, ' NSLog(@"%@", httpResponse);')
123
- .push(1, ' }')
124
- .push(' }];')
125
- .push('[dataTask resume];')
126
-
127
- return code.join()
128
- }
135
+ code
136
+ .blank()
137
+ // Retrieving the shared session will be less verbose than creating a new one.
138
+ .push('NSURLSession *session = [NSURLSession sharedSession];')
139
+ .push('NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request')
140
+ .push(
141
+ ' completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {'
142
+ )
143
+ .push(1, ' if (error) {')
144
+ .push(2, ' NSLog(@"%@", error);')
145
+ .push(1, ' } else {')
146
+ // Casting the NSURLResponse to NSHTTPURLResponse so the user can see the status .
147
+ .push(
148
+ 2,
149
+ ' NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;'
150
+ )
151
+ .push(2, ' NSLog(@"%@", httpResponse);')
152
+ .push(1, ' }')
153
+ .push(' }];')
154
+ .push('[dataTask resume];');
155
+
156
+ return code.join();
157
+ };
129
158
 
130
159
  module.exports.info = {
131
160
  key: 'nsurlsession',
132
161
  title: 'NSURLSession',
133
162
  link: 'https://developer.apple.com/library/mac/documentation/Foundation/Reference/NSURLSession_class/index.html',
134
- description: 'Foundation\'s NSURLSession request'
135
- }
163
+ description: "Foundation's NSURLSession request",
164
+ };
@@ -8,64 +8,66 @@
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)
14
+ const opts = {
15
+ indent: ' ',
16
+ ...options,
17
+ };
19
18
 
20
- var methods = ['get', 'post', 'head', 'delete', 'patch', 'put', 'options']
21
- var code = new CodeBuilder(opts.indent)
19
+ const methods = ['get', 'post', 'head', 'delete', 'patch', 'put', 'options'];
20
+ const code = new CodeBuilder(opts.indent);
22
21
 
23
- code.push('open Cohttp_lwt_unix')
24
- .push('open Cohttp')
25
- .push('open Lwt')
26
- .blank()
27
- .push('let uri = Uri.of_string "%s" in', source.fullUrl)
22
+ code
23
+ .push('open Cohttp_lwt_unix')
24
+ .push('open Cohttp')
25
+ .push('open Lwt')
26
+ .blank()
27
+ .push('let uri = Uri.of_string "%s" in', source.fullUrl);
28
28
 
29
29
  // Add headers, including the cookies
30
- var headers = Object.keys(source.allHeaders)
30
+ const headers = Object.keys(source.allHeaders);
31
31
 
32
32
  if (headers.length === 1) {
33
- code.push('let headers = Header.add (Header.init ()) "%s" "%s" in', headers[0], source.allHeaders[headers[0]])
33
+ code.push('let headers = Header.add (Header.init ()) "%s" "%s" in', headers[0], source.allHeaders[headers[0]]);
34
34
  } else if (headers.length > 1) {
35
- code.push('let headers = Header.add_list (Header.init ()) [')
35
+ code.push('let headers = Header.add_list (Header.init ()) [');
36
36
 
37
37
  headers.forEach(function (key) {
38
- code.push(1, '("%s", "%s");', key, source.allHeaders[key])
39
- })
38
+ code.push(1, '("%s", "%s");', key, source.allHeaders[key]);
39
+ });
40
40
 
41
- code.push('] in')
41
+ code.push('] in');
42
42
  }
43
43
 
44
44
  // Add body
45
45
  if (source.postData.text) {
46
46
  // Just text
47
- code.push('let body = Cohttp_lwt_body.of_string %s in', JSON.stringify(source.postData.text))
47
+ code.push('let body = Cohttp_lwt_body.of_string %s in', JSON.stringify(source.postData.text));
48
48
  }
49
49
 
50
50
  // Do the request
51
- code.blank()
51
+ code.blank();
52
52
 
53
- code.push('Client.call %s%s%s uri',
53
+ code.push(
54
+ 'Client.call %s%s%s uri',
54
55
  headers.length ? '~headers ' : '',
55
56
  source.postData.text ? '~body ' : '',
56
- (methods.indexOf(source.method.toLowerCase()) >= 0 ? ('`' + source.method.toUpperCase()) : '(Code.method_of_string "' + source.method + '")')
57
- )
57
+ methods.indexOf(source.method.toLowerCase()) >= 0
58
+ ? `\`${source.method.toUpperCase()}`
59
+ : `(Code.method_of_string "${source.method}")`
60
+ );
58
61
 
59
62
  // Catch result
60
- code.push('>>= fun (res, body_stream) ->')
61
- .push(1, '(* Do stuff with the result *)')
63
+ code.push('>>= fun (res, body_stream) ->').push(1, '(* Do stuff with the result *)');
62
64
 
63
- return code.join()
64
- }
65
+ return code.join();
66
+ };
65
67
 
66
68
  module.exports.info = {
67
69
  key: 'cohttp',
68
70
  title: 'CoHTTP',
69
71
  link: 'https://github.com/mirage/ocaml-cohttp',
70
- description: 'Cohttp is a very lightweight HTTP server using Lwt or Async for OCaml'
71
- }
72
+ description: 'Cohttp is a very lightweight HTTP server using Lwt or Async for OCaml',
73
+ };
@@ -1,12 +1,10 @@
1
- 'use strict'
2
-
3
1
  module.exports = {
4
2
  info: {
5
3
  key: 'ocaml',
6
4
  title: 'OCaml',
7
5
  extname: '.ml',
8
- default: 'cohttp'
6
+ default: 'cohttp',
9
7
  },
10
8
 
11
- cohttp: require('./cohttp')
12
- }
9
+ cohttp: require('./cohttp'),
10
+ };
@@ -8,101 +8,108 @@
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 CodeBuilder = require('../../helpers/code-builder')
11
+ const { format } = require('util');
12
+ const CodeBuilder = require('../../helpers/code-builder');
15
13
 
16
14
  module.exports = function (source, options) {
17
- var opts = Object.assign({
15
+ const opts = {
18
16
  closingTag: false,
19
17
  indent: ' ',
20
18
  maxRedirects: 10,
21
19
  namedErrors: false,
22
20
  noTags: false,
23
21
  shortTags: false,
24
- timeout: 30
25
- }, options)
22
+ timeout: 30,
23
+ ...options,
24
+ };
26
25
 
27
- var code = new CodeBuilder(opts.indent)
26
+ const code = new CodeBuilder(opts.indent);
28
27
 
29
28
  if (!opts.noTags) {
30
- code.push(opts.shortTags ? '<?' : '<?php')
31
- .blank()
29
+ code.push(opts.shortTags ? '<?' : '<?php').blank();
32
30
  }
33
31
 
34
- code.push('$curl = curl_init();')
35
- .blank()
36
-
37
- var curlOptions = [{
38
- escape: true,
39
- name: 'CURLOPT_PORT',
40
- value: source.uriObj.port
41
- }, {
42
- escape: true,
43
- name: 'CURLOPT_URL',
44
- value: source.fullUrl
45
- }, {
46
- escape: false,
47
- name: 'CURLOPT_RETURNTRANSFER',
48
- value: 'true'
49
- }, {
50
- escape: true,
51
- name: 'CURLOPT_ENCODING',
52
- value: ''
53
- }, {
54
- escape: false,
55
- name: 'CURLOPT_MAXREDIRS',
56
- value: opts.maxRedirects
57
- }, {
58
- escape: false,
59
- name: 'CURLOPT_TIMEOUT',
60
- value: opts.timeout
61
- }, {
62
- escape: false,
63
- name: 'CURLOPT_HTTP_VERSION',
64
- value: source.httpVersion === 'HTTP/1.0' ? 'CURL_HTTP_VERSION_1_0' : 'CURL_HTTP_VERSION_1_1'
65
- }, {
66
- escape: true,
67
- name: 'CURLOPT_CUSTOMREQUEST',
68
- value: source.method
69
- }, {
70
- escape: true,
71
- name: 'CURLOPT_POSTFIELDS',
72
- value: source.postData ? source.postData.text : undefined
73
- }]
74
-
75
- code.push('curl_setopt_array($curl, [')
76
-
77
- var curlopts = new CodeBuilder(opts.indent, '\n' + opts.indent)
32
+ code.push('$curl = curl_init();').blank();
33
+
34
+ const curlOptions = [
35
+ {
36
+ escape: true,
37
+ name: 'CURLOPT_PORT',
38
+ value: source.uriObj.port,
39
+ },
40
+ {
41
+ escape: true,
42
+ name: 'CURLOPT_URL',
43
+ value: source.fullUrl,
44
+ },
45
+ {
46
+ escape: false,
47
+ name: 'CURLOPT_RETURNTRANSFER',
48
+ value: 'true',
49
+ },
50
+ {
51
+ escape: true,
52
+ name: 'CURLOPT_ENCODING',
53
+ value: '',
54
+ },
55
+ {
56
+ escape: false,
57
+ name: 'CURLOPT_MAXREDIRS',
58
+ value: opts.maxRedirects,
59
+ },
60
+ {
61
+ escape: false,
62
+ name: 'CURLOPT_TIMEOUT',
63
+ value: opts.timeout,
64
+ },
65
+ {
66
+ escape: false,
67
+ name: 'CURLOPT_HTTP_VERSION',
68
+ value: source.httpVersion === 'HTTP/1.0' ? 'CURL_HTTP_VERSION_1_0' : 'CURL_HTTP_VERSION_1_1',
69
+ },
70
+ {
71
+ escape: true,
72
+ name: 'CURLOPT_CUSTOMREQUEST',
73
+ value: source.method,
74
+ },
75
+ {
76
+ escape: true,
77
+ name: 'CURLOPT_POSTFIELDS',
78
+ value: source.postData ? source.postData.text : undefined,
79
+ },
80
+ ];
81
+
82
+ code.push('curl_setopt_array($curl, [');
83
+
84
+ const curlopts = new CodeBuilder(opts.indent, `\n${opts.indent}`);
78
85
 
79
86
  curlOptions.forEach(function (option) {
80
- if (!~[null, undefined].indexOf(option.value)) {
81
- curlopts.push(util.format('%s => %s,', option.name, option.escape ? JSON.stringify(option.value) : option.value))
87
+ if (![null, undefined].includes(option.value)) {
88
+ curlopts.push(format('%s => %s,', option.name, option.escape ? JSON.stringify(option.value) : option.value));
82
89
  }
83
- })
90
+ });
84
91
 
85
92
  // construct cookies
86
- var cookies = source.cookies.map(function (cookie) {
87
- return encodeURIComponent(cookie.name) + '=' + encodeURIComponent(cookie.value)
88
- })
89
-
90
- if (cookies.length) {
91
- curlopts.push(util.format('CURLOPT_COOKIE => "%s",', cookies.join('; ')))
93
+ if (source.allHeaders.cookie) {
94
+ curlopts.push(format('CURLOPT_COOKIE => "%s",', source.allHeaders.cookie));
92
95
  }
93
96
 
94
97
  // construct cookies
95
- var headers = Object.keys(source.headersObj).sort().map(function (key) {
96
- return util.format('"%s: %s"', key, source.headersObj[key])
97
- })
98
+ const headers = Object.keys(source.headersObj)
99
+ .sort()
100
+ .map(function (key) {
101
+ return format('"%s: %s"', key, source.headersObj[key]);
102
+ });
98
103
 
99
104
  if (headers.length) {
100
- curlopts.push('CURLOPT_HTTPHEADER => [')
101
- .push(1, headers.join(',\n' + opts.indent + opts.indent))
102
- .push('],')
105
+ curlopts
106
+ .push('CURLOPT_HTTPHEADER => [')
107
+ .push(1, headers.join(`,\n${opts.indent}${opts.indent}`))
108
+ .push('],');
103
109
  }
104
110
 
105
- code.push(1, curlopts.join())
111
+ code
112
+ .push(1, curlopts.join())
106
113
  .push(']);')
107
114
  .blank()
108
115
  .push('$response = curl_exec($curl);')
@@ -110,29 +117,26 @@ module.exports = function (source, options) {
110
117
  .blank()
111
118
  .push('curl_close($curl);')
112
119
  .blank()
113
- .push('if ($err) {')
120
+ .push('if ($err) {');
114
121
 
115
122
  if (opts.namedErrors) {
116
- code.push(1, 'echo array_flip(get_defined_constants(true)["curl"])[$err];')
123
+ code.push(1, 'echo array_flip(get_defined_constants(true)["curl"])[$err];');
117
124
  } else {
118
- code.push(1, 'echo "cURL Error #:" . $err;')
125
+ code.push(1, 'echo "cURL Error #:" . $err;');
119
126
  }
120
127
 
121
- code.push('} else {')
122
- .push(1, 'echo $response;')
123
- .push('}')
128
+ code.push('} else {').push(1, 'echo $response;').push('}');
124
129
 
125
130
  if (!opts.noTags && opts.closingTag) {
126
- code.blank()
127
- .push('?>')
131
+ code.blank().push('?>');
128
132
  }
129
133
 
130
- return code.join()
131
- }
134
+ return code.join();
135
+ };
132
136
 
133
137
  module.exports.info = {
134
138
  key: 'curl',
135
139
  title: 'cURL',
136
140
  link: 'http://php.net/manual/en/book.curl.php',
137
- description: 'PHP with ext-curl'
138
- }
141
+ description: 'PHP with ext-curl',
142
+ };