@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,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
|
-
|
|
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 =
|
|
19
|
-
indent: ' '
|
|
20
|
-
|
|
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.
|
|
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
|
-
|
|
47
|
+
if (source.postData.params) {
|
|
48
|
+
reqOpts.formData = {};
|
|
55
49
|
|
|
56
|
-
|
|
57
|
-
|
|
50
|
+
source.postData.params.forEach(function (param) {
|
|
51
|
+
const attachment = {};
|
|
58
52
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
53
|
+
if (!param.fileName && !param.contentType) {
|
|
54
|
+
reqOpts.formData[param.name] = param.value;
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
63
57
|
|
|
64
|
-
|
|
65
|
-
|
|
58
|
+
if (param.fileName) {
|
|
59
|
+
includeFS = true;
|
|
66
60
|
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
61
|
+
attachment.value = `fs.createReadStream("${param.fileName}")`;
|
|
62
|
+
} else if (param.value) {
|
|
63
|
+
attachment.value = param.value;
|
|
64
|
+
}
|
|
71
65
|
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
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
|
-
|
|
80
|
-
|
|
81
|
-
|
|
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.
|
|
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.
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
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
|
|
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
|
-
|
|
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 =
|
|
17
|
-
indent: ' '
|
|
18
|
-
|
|
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
|
|
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.
|
|
29
|
-
code.push('const CookieJar = unirest.jar();')
|
|
28
|
+
if (source.allHeaders.cookie) {
|
|
29
|
+
code.push('const CookieJar = unirest.jar();');
|
|
30
30
|
|
|
31
|
-
source.
|
|
32
|
-
|
|
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
|
|
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
|
-
|
|
66
|
+
if (source.postData.params) {
|
|
67
|
+
const multipart = [];
|
|
67
68
|
|
|
68
|
-
|
|
69
|
-
|
|
69
|
+
source.postData.params.forEach(function (param) {
|
|
70
|
+
const part = {};
|
|
70
71
|
|
|
71
|
-
|
|
72
|
-
|
|
72
|
+
if (param.fileName && !param.value) {
|
|
73
|
+
includeFS = true;
|
|
73
74
|
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
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
|
-
|
|
85
|
-
|
|
86
|
-
|
|
80
|
+
if (part.body) {
|
|
81
|
+
if (param.contentType) {
|
|
82
|
+
part['content-type'] = param.contentType;
|
|
83
|
+
}
|
|
87
84
|
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
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
|
|
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
|
-
|
|
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
|
-
|
|
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
|
|
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 ? ', ' :
|
|
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
|
|
51
|
+
return `@${value}`;
|
|
53
52
|
|
|
54
53
|
case '[object Array]': {
|
|
55
|
-
const valuesRepresentation = value.map(
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
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
|
-
|
|
64
|
-
keyValuePairs.push(
|
|
65
|
-
}
|
|
66
|
-
return
|
|
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
|
|
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
|
-
|
|
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
|
// 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
|
|
48
|
-
.
|
|
49
|
-
|
|
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(
|
|
53
|
-
|
|
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
|
|
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
|
|
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(
|
|
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(
|
|
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
|
|
94
|
-
.
|
|
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
|
|
99
|
-
.
|
|
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(
|
|
103
|
-
|
|
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
|
|
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(
|
|
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(
|
|
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:
|
|
136
|
-
}
|
|
163
|
+
description: "Foundation's NSURLSession request",
|
|
164
|
+
};
|