@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.
- package/README.md +19 -9
- package/package.json +24 -28
- package/src/helpers/code-builder.js +85 -80
- package/src/helpers/form-data.js +27 -25
- package/src/helpers/headers.js +10 -19
- package/src/helpers/reducer.js +10 -14
- package/src/index.js +167 -146
- package/src/targets/c/index.js +3 -5
- package/src/targets/c/libcurl.js +18 -23
- package/src/targets/clojure/clj_http.js +103 -87
- package/src/targets/clojure/index.js +3 -5
- package/src/targets/csharp/httpclient.js +90 -90
- package/src/targets/csharp/index.js +3 -5
- package/src/targets/csharp/restsharp.js +20 -22
- package/src/targets/go/index.js +3 -5
- package/src/targets/go/native.js +46 -53
- package/src/targets/http/http1.1.js +26 -36
- package/src/targets/http/index.js +3 -5
- package/src/targets/index.js +2 -4
- package/src/targets/java/asynchttp.js +22 -23
- package/src/targets/java/index.js +3 -5
- package/src/targets/java/nethttp.js +20 -24
- package/src/targets/java/okhttp.js +31 -31
- package/src/targets/java/unirest.js +22 -19
- package/src/targets/javascript/axios.js +43 -38
- package/src/targets/javascript/fetch.js +66 -68
- package/src/targets/javascript/index.js +3 -5
- package/src/targets/javascript/jquery.js +46 -40
- package/src/targets/javascript/xhr.js +42 -38
- package/src/targets/kotlin/index.js +3 -5
- package/src/targets/kotlin/okhttp.js +31 -31
- package/src/targets/node/axios.js +33 -30
- package/src/targets/node/fetch.js +89 -83
- package/src/targets/node/index.js +4 -5
- package/src/targets/node/native.js +36 -30
- package/src/targets/node/request.js +64 -64
- package/src/targets/node/unirest.js +56 -55
- package/src/targets/objc/helpers.js +24 -23
- package/src/targets/objc/index.js +3 -5
- package/src/targets/objc/nsurlsession.js +73 -45
- package/src/targets/ocaml/cohttp.js +29 -27
- package/src/targets/ocaml/index.js +3 -5
- package/src/targets/php/curl.js +86 -82
- package/src/targets/php/guzzle.js +135 -0
- package/src/targets/php/helpers.js +29 -28
- package/src/targets/php/http1.js +33 -44
- package/src/targets/php/http2.js +70 -71
- package/src/targets/php/index.js +5 -5
- package/src/targets/powershell/common.js +35 -30
- package/src/targets/powershell/index.js +3 -5
- package/src/targets/powershell/restmethod.js +3 -5
- package/src/targets/powershell/webrequest.js +3 -5
- package/src/targets/python/helpers.js +37 -34
- package/src/targets/python/index.js +4 -5
- package/src/targets/python/python3.js +33 -44
- package/src/targets/python/requests.js +47 -72
- package/src/targets/r/httr.js +59 -70
- package/src/targets/r/index.js +3 -5
- package/src/targets/ruby/index.js +3 -5
- package/src/targets/ruby/native.js +39 -33
- package/src/targets/shell/curl.js +78 -51
- package/src/targets/shell/helpers.js +7 -9
- package/src/targets/shell/httpie.js +45 -39
- package/src/targets/shell/index.js +4 -5
- package/src/targets/shell/wget.js +20 -22
- package/src/targets/swift/helpers.js +35 -32
- package/src/targets/swift/index.js +3 -5
- package/src/targets/swift/nsurlsession.js +53 -39
- package/src/.DS_Store +0 -0
|
@@ -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
|
-
|
|
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 =
|
|
15
|
+
const opts = {
|
|
18
16
|
indent: ' ',
|
|
19
17
|
pretty: true,
|
|
20
|
-
timeout: '10'
|
|
21
|
-
|
|
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
|
|
49
|
-
.
|
|
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(
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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(
|
|
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:
|
|
138
|
-
}
|
|
151
|
+
description: "Foundation's NSURLSession request",
|
|
152
|
+
};
|
package/src/.DS_Store
DELETED
|
Binary file
|