@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.
- package/README.md +19 -9
- package/package.json +24 -27
- package/src/helpers/code-builder.js +85 -80
- package/src/helpers/form-data.js +28 -26
- package/src/helpers/headers.js +18 -8
- package/src/helpers/reducer.js +10 -14
- package/src/index.js +175 -151
- package/src/targets/c/index.js +3 -5
- package/src/targets/c/libcurl.js +20 -25
- package/src/targets/clojure/clj_http.js +126 -102
- package/src/targets/clojure/index.js +3 -5
- package/src/targets/csharp/httpclient.js +93 -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 +47 -54
- package/src/targets/http/http1.1.js +27 -37
- 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 +47 -42
- package/src/targets/javascript/fetch.js +66 -68
- package/src/targets/javascript/index.js +3 -5
- package/src/targets/javascript/jquery.js +50 -44
- package/src/targets/javascript/xhr.js +48 -44
- package/src/targets/kotlin/index.js +3 -5
- package/src/targets/kotlin/okhttp.js +31 -31
- package/src/targets/node/axios.js +37 -34
- package/src/targets/node/fetch.js +94 -87
- package/src/targets/node/index.js +4 -5
- package/src/targets/node/native.js +51 -45
- package/src/targets/node/request.js +69 -69
- package/src/targets/node/unirest.js +70 -68
- package/src/targets/objc/helpers.js +32 -25
- package/src/targets/objc/index.js +3 -5
- package/src/targets/objc/nsurlsession.js +108 -79
- package/src/targets/ocaml/cohttp.js +32 -30
- 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 +31 -29
- package/src/targets/php/http1.js +38 -49
- package/src/targets/php/http2.js +75 -75
- 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 +42 -37
- package/src/targets/python/index.js +4 -5
- package/src/targets/python/python3.js +33 -45
- package/src/targets/python/requests.js +48 -72
- package/src/targets/r/httr.js +60 -73
- package/src/targets/r/index.js +3 -5
- package/src/targets/ruby/index.js +3 -5
- package/src/targets/ruby/native.js +41 -35
- package/src/targets/shell/curl.js +108 -43
- package/src/{helpers/shell.js → 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 +44 -35
- package/src/targets/swift/index.js +3 -5
- package/src/targets/swift/nsurlsession.js +93 -79
- package/src/.DS_Store +0 -0
|
@@ -8,14 +8,12 @@
|
|
|
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
|
-
|
|
14
|
-
var shell = require('../../helpers/shell')
|
|
15
|
-
var CodeBuilder = require('../../helpers/code-builder')
|
|
11
|
+
const { format } = require('util');
|
|
12
|
+
const shell = require('./helpers');
|
|
13
|
+
const CodeBuilder = require('../../helpers/code-builder');
|
|
16
14
|
|
|
17
15
|
module.exports = function (source, options) {
|
|
18
|
-
|
|
16
|
+
const opts = {
|
|
19
17
|
body: false,
|
|
20
18
|
cert: false,
|
|
21
19
|
headers: false,
|
|
@@ -27,97 +25,105 @@ module.exports = function (source, options) {
|
|
|
27
25
|
style: false,
|
|
28
26
|
timeout: false,
|
|
29
27
|
verbose: false,
|
|
30
|
-
verify: false
|
|
31
|
-
|
|
28
|
+
verify: false,
|
|
29
|
+
...options,
|
|
30
|
+
};
|
|
32
31
|
|
|
33
|
-
|
|
32
|
+
const code = new CodeBuilder(opts.indent, opts.indent !== false ? ` \\\n${opts.indent}` : ' ');
|
|
34
33
|
|
|
35
|
-
|
|
36
|
-
|
|
34
|
+
let raw = false;
|
|
35
|
+
const flags = [];
|
|
37
36
|
|
|
38
37
|
if (opts.headers) {
|
|
39
|
-
flags.push(opts.short ? '-h' : '--headers')
|
|
38
|
+
flags.push(opts.short ? '-h' : '--headers');
|
|
40
39
|
}
|
|
41
40
|
|
|
42
41
|
if (opts.body) {
|
|
43
|
-
flags.push(opts.short ? '-b' : '--body')
|
|
42
|
+
flags.push(opts.short ? '-b' : '--body');
|
|
44
43
|
}
|
|
45
44
|
|
|
46
45
|
if (opts.verbose) {
|
|
47
|
-
flags.push(opts.short ? '-v' : '--verbose')
|
|
46
|
+
flags.push(opts.short ? '-v' : '--verbose');
|
|
48
47
|
}
|
|
49
48
|
|
|
50
49
|
if (opts.print) {
|
|
51
|
-
flags.push(
|
|
50
|
+
flags.push(format('%s=%s', opts.short ? '-p' : '--print', opts.print));
|
|
52
51
|
}
|
|
53
52
|
|
|
54
53
|
if (opts.verify) {
|
|
55
|
-
flags.push(
|
|
54
|
+
flags.push(format('--verify=%s', opts.verify));
|
|
56
55
|
}
|
|
57
56
|
|
|
58
57
|
if (opts.cert) {
|
|
59
|
-
flags.push(
|
|
58
|
+
flags.push(format('--cert=%s', opts.cert));
|
|
60
59
|
}
|
|
61
60
|
|
|
62
61
|
if (opts.pretty) {
|
|
63
|
-
flags.push(
|
|
62
|
+
flags.push(format('--pretty=%s', opts.pretty));
|
|
64
63
|
}
|
|
65
64
|
|
|
66
65
|
if (opts.style) {
|
|
67
|
-
flags.push(
|
|
66
|
+
flags.push(format('--style=%s', opts.pretty));
|
|
68
67
|
}
|
|
69
68
|
|
|
70
69
|
if (opts.timeout) {
|
|
71
|
-
flags.push(
|
|
70
|
+
flags.push(format('--timeout=%s', opts.timeout));
|
|
72
71
|
}
|
|
73
72
|
|
|
74
73
|
// construct query params
|
|
75
74
|
if (opts.queryParams) {
|
|
76
|
-
|
|
75
|
+
const queryStringKeys = Object.keys(source.queryObj);
|
|
77
76
|
|
|
78
77
|
queryStringKeys.forEach(function (name) {
|
|
79
|
-
|
|
78
|
+
const value = source.queryObj[name];
|
|
80
79
|
|
|
81
80
|
if (Array.isArray(value)) {
|
|
82
81
|
value.forEach(function (val) {
|
|
83
|
-
code.push('%s==%s', name, shell.quote(val))
|
|
84
|
-
})
|
|
82
|
+
code.push('%s==%s', name, shell.quote(val));
|
|
83
|
+
});
|
|
85
84
|
} else {
|
|
86
|
-
code.push('%s==%s', name, shell.quote(value))
|
|
85
|
+
code.push('%s==%s', name, shell.quote(value));
|
|
87
86
|
}
|
|
88
|
-
})
|
|
87
|
+
});
|
|
89
88
|
}
|
|
90
89
|
|
|
91
90
|
// construct headers
|
|
92
|
-
Object.keys(source.allHeaders)
|
|
93
|
-
|
|
94
|
-
|
|
91
|
+
Object.keys(source.allHeaders)
|
|
92
|
+
.sort()
|
|
93
|
+
.forEach(function (key) {
|
|
94
|
+
code.push('%s:%s', key, shell.quote(source.allHeaders[key]));
|
|
95
|
+
});
|
|
95
96
|
|
|
96
97
|
if (source.postData.mimeType === 'application/x-www-form-urlencoded') {
|
|
97
98
|
// construct post params
|
|
98
99
|
if (source.postData.params && source.postData.params.length) {
|
|
99
|
-
flags.push(opts.short ? '-f' : '--form')
|
|
100
|
+
flags.push(opts.short ? '-f' : '--form');
|
|
100
101
|
|
|
101
102
|
source.postData.params.forEach(function (param) {
|
|
102
|
-
code.push('%s=%s', param.name, shell.quote(param.value))
|
|
103
|
-
})
|
|
103
|
+
code.push('%s=%s', param.name, shell.quote(param.value));
|
|
104
|
+
});
|
|
104
105
|
}
|
|
105
106
|
} else {
|
|
106
|
-
raw = true
|
|
107
|
+
raw = true;
|
|
107
108
|
}
|
|
108
109
|
|
|
109
|
-
code.unshift(
|
|
110
|
+
code.unshift(
|
|
111
|
+
'http %s%s %s',
|
|
112
|
+
flags.length ? `${flags.join(' ')} ` : '',
|
|
113
|
+
source.method,
|
|
114
|
+
shell.quote(opts.queryParams ? source.url : source.fullUrl)
|
|
115
|
+
);
|
|
110
116
|
|
|
111
117
|
if (raw && source.postData.text) {
|
|
112
|
-
code.unshift('echo %s | ', shell.quote(source.postData.text))
|
|
118
|
+
code.unshift('echo %s | ', shell.quote(source.postData.text));
|
|
113
119
|
}
|
|
114
120
|
|
|
115
|
-
return code.join()
|
|
116
|
-
}
|
|
121
|
+
return code.join();
|
|
122
|
+
};
|
|
117
123
|
|
|
118
124
|
module.exports.info = {
|
|
119
125
|
key: 'httpie',
|
|
120
126
|
title: 'HTTPie',
|
|
121
127
|
link: 'http://httpie.org/',
|
|
122
|
-
description: 'a CLI, cURL-like tool for humans'
|
|
123
|
-
}
|
|
128
|
+
description: 'a CLI, cURL-like tool for humans',
|
|
129
|
+
};
|
|
@@ -1,14 +1,13 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
1
|
module.exports = {
|
|
4
2
|
info: {
|
|
5
3
|
key: 'shell',
|
|
6
4
|
title: 'Shell',
|
|
7
5
|
extname: '.sh',
|
|
8
|
-
default: 'curl'
|
|
6
|
+
default: 'curl',
|
|
7
|
+
cli: '%s',
|
|
9
8
|
},
|
|
10
9
|
|
|
11
10
|
curl: require('./curl'),
|
|
12
11
|
httpie: require('./httpie'),
|
|
13
|
-
wget: require('./wget')
|
|
14
|
-
}
|
|
12
|
+
wget: require('./wget'),
|
|
13
|
+
};
|
|
@@ -8,47 +8,45 @@
|
|
|
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
|
-
|
|
14
|
-
var helpers = require('../../helpers/shell')
|
|
15
|
-
var CodeBuilder = require('../../helpers/code-builder')
|
|
11
|
+
const { format } = require('util');
|
|
12
|
+
const helpers = require('./helpers');
|
|
13
|
+
const CodeBuilder = require('../../helpers/code-builder');
|
|
16
14
|
|
|
17
15
|
module.exports = function (source, options) {
|
|
18
|
-
|
|
16
|
+
const opts = {
|
|
19
17
|
indent: ' ',
|
|
20
18
|
short: false,
|
|
21
|
-
verbose: false
|
|
22
|
-
|
|
19
|
+
verbose: false,
|
|
20
|
+
...options,
|
|
21
|
+
};
|
|
23
22
|
|
|
24
|
-
|
|
23
|
+
const code = new CodeBuilder(opts.indent, opts.indent !== false ? ` \\\n${opts.indent}` : ' ');
|
|
25
24
|
|
|
26
25
|
if (opts.verbose) {
|
|
27
|
-
code.push('wget %s', opts.short ? '-v' : '--verbose')
|
|
26
|
+
code.push('wget %s', opts.short ? '-v' : '--verbose');
|
|
28
27
|
} else {
|
|
29
|
-
code.push('wget %s', opts.short ? '-q' : '--quiet')
|
|
28
|
+
code.push('wget %s', opts.short ? '-q' : '--quiet');
|
|
30
29
|
}
|
|
31
30
|
|
|
32
|
-
code.push('--method %s', helpers.quote(source.method))
|
|
31
|
+
code.push('--method %s', helpers.quote(source.method));
|
|
33
32
|
|
|
34
33
|
Object.keys(source.allHeaders).forEach(function (key) {
|
|
35
|
-
|
|
36
|
-
code.push('--header %s', helpers.quote(header))
|
|
37
|
-
})
|
|
34
|
+
const header = format('%s: %s', key, source.allHeaders[key]);
|
|
35
|
+
code.push('--header %s', helpers.quote(header));
|
|
36
|
+
});
|
|
38
37
|
|
|
39
38
|
if (source.postData.text) {
|
|
40
|
-
code.push(
|
|
39
|
+
code.push(`--body-data ${helpers.escape(helpers.quote(source.postData.text))}`);
|
|
41
40
|
}
|
|
42
41
|
|
|
43
|
-
code.push(opts.short ? '-O' : '--output-document')
|
|
44
|
-
.push('- %s', helpers.quote(source.fullUrl))
|
|
42
|
+
code.push(opts.short ? '-O' : '--output-document').push('- %s', helpers.quote(source.fullUrl));
|
|
45
43
|
|
|
46
|
-
return code.join()
|
|
47
|
-
}
|
|
44
|
+
return code.join();
|
|
45
|
+
};
|
|
48
46
|
|
|
49
47
|
module.exports.info = {
|
|
50
48
|
key: 'wget',
|
|
51
49
|
title: 'Wget',
|
|
52
50
|
link: 'https://www.gnu.org/software/wget/',
|
|
53
|
-
description: 'a free software package for retrieving files using HTTP, HTTPS'
|
|
54
|
-
}
|
|
51
|
+
description: 'a free software package for retrieving files using HTTP, HTTPS',
|
|
52
|
+
};
|
|
@@ -1,6 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
var util = require('util')
|
|
1
|
+
const { format } = require('util');
|
|
4
2
|
|
|
5
3
|
/**
|
|
6
4
|
* Create an string of given length filled with blank spaces
|
|
@@ -9,24 +7,25 @@ var util = require('util')
|
|
|
9
7
|
* @param {string} str String to pad out with
|
|
10
8
|
* @return {string}
|
|
11
9
|
*/
|
|
12
|
-
function buildString
|
|
13
|
-
|
|
10
|
+
function buildString(length, str) {
|
|
11
|
+
// eslint-disable-next-line prefer-spread
|
|
12
|
+
return Array.apply(null, new Array(length)).map(String.prototype.valueOf, str).join('');
|
|
14
13
|
}
|
|
15
14
|
|
|
16
15
|
/**
|
|
17
16
|
* Create a string corresponding to a Dictionary or Array literal representation with pretty option
|
|
18
17
|
* and indentation.
|
|
19
18
|
*/
|
|
20
|
-
function concatArray
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
19
|
+
function concatArray(arr, pretty, indentation, indentLevel) {
|
|
20
|
+
const currentIndent = buildString(indentLevel, indentation);
|
|
21
|
+
const closingBraceIndent = buildString(indentLevel - 1, indentation);
|
|
22
|
+
const join = pretty ? `,\n${currentIndent}` : ', ';
|
|
24
23
|
|
|
25
24
|
if (pretty) {
|
|
26
|
-
return
|
|
27
|
-
} else {
|
|
28
|
-
return '[' + arr.join(join) + ']'
|
|
25
|
+
return `[\n${currentIndent}${arr.join(join)}\n${closingBraceIndent}]`;
|
|
29
26
|
}
|
|
27
|
+
|
|
28
|
+
return `[${arr.join(join)}]`;
|
|
30
29
|
}
|
|
31
30
|
|
|
32
31
|
module.exports = {
|
|
@@ -39,7 +38,7 @@ module.exports = {
|
|
|
39
38
|
* @return {string}
|
|
40
39
|
*/
|
|
41
40
|
literalDeclaration: function (name, parameters, opts) {
|
|
42
|
-
return
|
|
41
|
+
return format('let %s = %s', name, this.literalRepresentation(parameters, opts));
|
|
43
42
|
},
|
|
44
43
|
|
|
45
44
|
/**
|
|
@@ -50,35 +49,45 @@ module.exports = {
|
|
|
50
49
|
* @return {string}
|
|
51
50
|
*/
|
|
52
51
|
literalRepresentation: function (value, opts, indentLevel) {
|
|
53
|
-
|
|
52
|
+
// eslint-disable-next-line no-param-reassign
|
|
53
|
+
indentLevel = indentLevel === undefined ? 1 : indentLevel + 1;
|
|
54
54
|
|
|
55
55
|
switch (Object.prototype.toString.call(value)) {
|
|
56
56
|
case '[object Number]':
|
|
57
|
-
return value
|
|
58
|
-
|
|
57
|
+
return value;
|
|
58
|
+
|
|
59
|
+
case '[object Array]': {
|
|
59
60
|
// Don't prettify arrays nto not take too much space
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
61
|
+
let pretty = false;
|
|
62
|
+
const valuesRepresentation = value.map(
|
|
63
|
+
function (v) {
|
|
64
|
+
// Switch to prettify if the value is a dictionary with multiple keys
|
|
65
|
+
if (Object.prototype.toString.call(v) === '[object Object]') {
|
|
66
|
+
pretty = Object.keys(v).length > 1;
|
|
67
|
+
}
|
|
68
|
+
return this.literalRepresentation(v, opts, indentLevel);
|
|
69
|
+
}.bind(this)
|
|
70
|
+
);
|
|
71
|
+
return concatArray(valuesRepresentation, pretty, opts.indent, indentLevel);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
case '[object Object]': {
|
|
75
|
+
const keyValuePairs = [];
|
|
76
|
+
// eslint-disable-next-line guard-for-in, no-restricted-syntax
|
|
77
|
+
for (const k in value) {
|
|
78
|
+
keyValuePairs.push(format('"%s": %s', k, this.literalRepresentation(value[k], opts, indentLevel)));
|
|
73
79
|
}
|
|
74
|
-
return concatArray(keyValuePairs, opts.pretty && keyValuePairs.length > 1, opts.indent, indentLevel)
|
|
80
|
+
return concatArray(keyValuePairs, opts.pretty && keyValuePairs.length > 1, opts.indent, indentLevel);
|
|
81
|
+
}
|
|
82
|
+
|
|
75
83
|
case '[object Boolean]':
|
|
76
|
-
return value.toString()
|
|
84
|
+
return value.toString();
|
|
85
|
+
|
|
77
86
|
default:
|
|
78
87
|
if (value === null || value === undefined) {
|
|
79
|
-
return ''
|
|
88
|
+
return '';
|
|
80
89
|
}
|
|
81
|
-
return
|
|
90
|
+
return `"${value.toString().replace(/"/g, '\\"')}"`;
|
|
82
91
|
}
|
|
83
|
-
}
|
|
84
|
-
}
|
|
92
|
+
},
|
|
93
|
+
};
|
|
@@ -1,12 +1,10 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
1
|
module.exports = {
|
|
4
2
|
info: {
|
|
5
3
|
key: 'swift',
|
|
6
4
|
title: 'Swift',
|
|
7
5
|
extname: '.swift',
|
|
8
|
-
default: 'nsurlsession'
|
|
6
|
+
default: 'nsurlsession',
|
|
9
7
|
},
|
|
10
8
|
|
|
11
|
-
nsurlsession: require('./nsurlsession')
|
|
12
|
-
}
|
|
9
|
+
nsurlsession: require('./nsurlsession'),
|
|
10
|
+
};
|
|
@@ -8,131 +8,145 @@
|
|
|
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
|
-
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
|
-
|
|
15
|
+
const opts = {
|
|
18
16
|
indent: ' ',
|
|
19
17
|
pretty: true,
|
|
20
|
-
timeout: '10'
|
|
21
|
-
|
|
18
|
+
timeout: '10',
|
|
19
|
+
...options,
|
|
20
|
+
};
|
|
22
21
|
|
|
23
|
-
|
|
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
|
-
|
|
50
|
-
|
|
51
|
-
|
|
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
|
|
55
|
+
for (let i = 1, len = source.postData.params.length; i < len; i++) {
|
|
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
|
|
58
|
-
|
|
59
|
-
|
|
66
|
+
code
|
|
67
|
+
.push(helpers.literalDeclaration('parameters', source.postData.jsonObj, opts), 'as [String : Any]')
|
|
68
|
+
.blank()
|
|
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
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
78
|
+
*/
|
|
79
|
+
code
|
|
80
|
+
.push(helpers.literalDeclaration('parameters', source.postData.params, opts))
|
|
81
|
+
.blank()
|
|
82
|
+
.push('let boundary = "%s"', source.postData.boundary)
|
|
83
|
+
.blank()
|
|
84
|
+
.push('var body = ""')
|
|
85
|
+
.push('var error: NSError? = nil')
|
|
86
|
+
.push('for param in parameters {')
|
|
87
|
+
.push(1, 'let paramName = param["name"]!')
|
|
88
|
+
.push(1, 'body += "--\\(boundary)\\r\\n"')
|
|
89
|
+
.push(1, 'body += "Content-Disposition:form-data; name=\\"\\(paramName)\\""')
|
|
90
|
+
.push(1, 'if let filename = param["fileName"] {')
|
|
91
|
+
.push(2, 'let contentType = param["content-type"]!')
|
|
92
|
+
.push(2, 'let fileContent = String(contentsOfFile: filename, encoding: String.Encoding.utf8)')
|
|
93
|
+
.push(2, 'if (error != nil) {')
|
|
94
|
+
.push(3, 'print(error)')
|
|
95
|
+
.push(2, '}')
|
|
96
|
+
.push(2, 'body += "; filename=\\"\\(filename)\\"\\r\\n"')
|
|
97
|
+
.push(2, 'body += "Content-Type: \\(contentType)\\r\\n\\r\\n"')
|
|
98
|
+
.push(2, 'body += fileContent')
|
|
99
|
+
.push(1, '} else if let paramValue = param["value"] {')
|
|
100
|
+
.push(2, 'body += "\\r\\n\\r\\n\\(paramValue)"')
|
|
101
|
+
.push(1, '}')
|
|
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
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
110
|
+
code
|
|
111
|
+
.blank()
|
|
112
|
+
// NSURLRequestUseProtocolCachePolicy is the default policy, let's just always set it to avoid confusion.
|
|
113
|
+
.push('let request = NSMutableURLRequest(url: NSURL(string: "%s")! as URL,', source.fullUrl)
|
|
114
|
+
.push(' cachePolicy: .useProtocolCachePolicy,')
|
|
115
|
+
.push(' timeoutInterval: %s)', parseInt(opts.timeout, 10).toFixed(1))
|
|
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
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
.
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
126
|
+
code
|
|
127
|
+
.blank()
|
|
128
|
+
// Retrieving the shared session will be less verbose than creating a new one.
|
|
129
|
+
.push('let session = URLSession.shared')
|
|
130
|
+
.push(
|
|
131
|
+
'let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in'
|
|
132
|
+
)
|
|
133
|
+
.push(1, 'if (error != nil) {')
|
|
134
|
+
.push(2, 'print(error)')
|
|
135
|
+
.push(1, '} else {')
|
|
136
|
+
// Casting the NSURLResponse to NSHTTPURLResponse so the user can see the status .
|
|
137
|
+
.push(2, 'let httpResponse = response as? HTTPURLResponse')
|
|
138
|
+
.push(2, 'print(httpResponse)')
|
|
139
|
+
.push(1, '}')
|
|
140
|
+
.push('})')
|
|
141
|
+
.blank()
|
|
142
|
+
.push('dataTask.resume()');
|
|
143
|
+
|
|
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
|