@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
|
@@ -7,83 +7,88 @@
|
|
|
7
7
|
*
|
|
8
8
|
* for any questions or issues regarding the generated code snippet, please open an issue mentioning the author.
|
|
9
9
|
*/
|
|
10
|
-
'use strict'
|
|
11
10
|
|
|
12
|
-
const
|
|
13
|
-
const
|
|
14
|
-
const CodeBuilder = require('../../helpers/code-builder')
|
|
11
|
+
const stringifyObject = require('stringify-object');
|
|
12
|
+
const CodeBuilder = require('../../helpers/code-builder');
|
|
15
13
|
|
|
16
14
|
module.exports = function (source, options) {
|
|
17
|
-
const opts =
|
|
18
|
-
indent: ' '
|
|
19
|
-
|
|
15
|
+
const opts = {
|
|
16
|
+
indent: ' ',
|
|
17
|
+
...options,
|
|
18
|
+
};
|
|
20
19
|
|
|
21
|
-
const code = new CodeBuilder(opts.indent)
|
|
20
|
+
const code = new CodeBuilder(opts.indent);
|
|
22
21
|
|
|
23
|
-
code.push('import axios from "axios";')
|
|
24
|
-
.blank()
|
|
22
|
+
code.push('import axios from "axios";').blank();
|
|
25
23
|
|
|
26
24
|
const reqOpts = {
|
|
27
25
|
method: source.method,
|
|
28
|
-
url: source.url
|
|
29
|
-
}
|
|
26
|
+
url: source.url,
|
|
27
|
+
};
|
|
30
28
|
|
|
31
29
|
if (Object.keys(source.queryObj).length) {
|
|
32
|
-
reqOpts.params = source.queryObj
|
|
30
|
+
reqOpts.params = source.queryObj;
|
|
33
31
|
}
|
|
34
32
|
|
|
35
33
|
if (Object.keys(source.allHeaders).length) {
|
|
36
|
-
reqOpts.headers = source.allHeaders
|
|
34
|
+
reqOpts.headers = source.allHeaders;
|
|
37
35
|
}
|
|
38
36
|
|
|
39
37
|
switch (source.postData.mimeType) {
|
|
40
38
|
case 'application/x-www-form-urlencoded':
|
|
41
|
-
reqOpts.data = source.postData.paramsObj
|
|
42
|
-
break
|
|
39
|
+
reqOpts.data = source.postData.paramsObj;
|
|
40
|
+
break;
|
|
43
41
|
|
|
44
42
|
case 'application/json':
|
|
45
43
|
if (source.postData.jsonObj) {
|
|
46
|
-
reqOpts.data = source.postData.jsonObj
|
|
44
|
+
reqOpts.data = source.postData.jsonObj;
|
|
47
45
|
}
|
|
48
|
-
break
|
|
46
|
+
break;
|
|
49
47
|
|
|
50
48
|
case 'multipart/form-data':
|
|
51
|
-
|
|
49
|
+
if (source.postData.params) {
|
|
50
|
+
code.push('const form = new FormData();');
|
|
52
51
|
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
52
|
+
source.postData.params.forEach(function (param) {
|
|
53
|
+
code.push(
|
|
54
|
+
'form.append(%s, %s);',
|
|
55
|
+
JSON.stringify(param.name),
|
|
56
|
+
JSON.stringify(param.value || param.fileName || '')
|
|
57
|
+
);
|
|
58
|
+
});
|
|
60
59
|
|
|
61
|
-
|
|
60
|
+
code.blank();
|
|
62
61
|
|
|
63
|
-
|
|
64
|
-
|
|
62
|
+
reqOpts.data = '[form]';
|
|
63
|
+
}
|
|
64
|
+
break;
|
|
65
65
|
|
|
66
66
|
default:
|
|
67
67
|
if (source.postData.text) {
|
|
68
|
-
reqOpts.data = source.postData.text
|
|
68
|
+
reqOpts.data = source.postData.text;
|
|
69
69
|
}
|
|
70
70
|
}
|
|
71
71
|
|
|
72
|
-
code
|
|
73
|
-
.
|
|
72
|
+
code
|
|
73
|
+
.push(
|
|
74
|
+
'const options = %s;',
|
|
75
|
+
stringifyObject(reqOpts, { indent: ' ', inlineCharacterLimit: 80 }).replace('"[form]"', 'form')
|
|
76
|
+
)
|
|
77
|
+
.blank();
|
|
74
78
|
|
|
75
|
-
code
|
|
79
|
+
code
|
|
80
|
+
.push('axios.request(options).then(function (response) {')
|
|
76
81
|
.push(1, 'console.log(response.data);')
|
|
77
82
|
.push('}).catch(%s', 'function (error) {')
|
|
78
83
|
.push(1, 'console.error(error);')
|
|
79
|
-
.push('});')
|
|
84
|
+
.push('});');
|
|
80
85
|
|
|
81
|
-
return code.join()
|
|
82
|
-
}
|
|
86
|
+
return code.join();
|
|
87
|
+
};
|
|
83
88
|
|
|
84
89
|
module.exports.info = {
|
|
85
90
|
key: 'axios',
|
|
86
91
|
title: 'Axios',
|
|
87
92
|
link: 'https://github.com/axios/axios',
|
|
88
|
-
description: 'Promise based HTTP client for the browser and node.js'
|
|
89
|
-
}
|
|
93
|
+
description: 'Promise based HTTP client for the browser and node.js',
|
|
94
|
+
};
|
|
@@ -8,108 +8,106 @@
|
|
|
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 stringifyObject = require('stringify-object')
|
|
14
|
-
const CodeBuilder = require('../../helpers/code-builder')
|
|
11
|
+
const stringifyObject = require('stringify-object');
|
|
12
|
+
const CodeBuilder = require('../../helpers/code-builder');
|
|
15
13
|
|
|
16
14
|
module.exports = function (source, options) {
|
|
17
|
-
const opts =
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
)
|
|
24
|
-
|
|
25
|
-
const
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
method: source.method
|
|
29
|
-
}
|
|
15
|
+
const opts = {
|
|
16
|
+
indent: ' ',
|
|
17
|
+
credentials: null,
|
|
18
|
+
...options,
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
const code = new CodeBuilder(opts.indent);
|
|
22
|
+
|
|
23
|
+
const reqOptions = {
|
|
24
|
+
method: source.method,
|
|
25
|
+
};
|
|
30
26
|
|
|
31
27
|
if (Object.keys(source.allHeaders).length) {
|
|
32
|
-
|
|
28
|
+
reqOptions.headers = source.allHeaders;
|
|
33
29
|
}
|
|
34
30
|
|
|
35
31
|
if (opts.credentials !== null) {
|
|
36
|
-
|
|
32
|
+
reqOptions.credentials = opts.credentials;
|
|
37
33
|
}
|
|
38
34
|
|
|
39
35
|
switch (source.postData.mimeType) {
|
|
40
36
|
case 'application/x-www-form-urlencoded':
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
: source.postData.text
|
|
44
|
-
break
|
|
37
|
+
reqOptions.body = source.postData.paramsObj ? source.postData.paramsObj : source.postData.text;
|
|
38
|
+
break;
|
|
45
39
|
|
|
46
40
|
case 'application/json':
|
|
47
41
|
if (source.postData.jsonObj) {
|
|
48
|
-
|
|
42
|
+
reqOptions.body = source.postData.jsonObj;
|
|
49
43
|
}
|
|
50
|
-
break
|
|
44
|
+
break;
|
|
51
45
|
|
|
52
46
|
case 'multipart/form-data':
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
47
|
+
if (source.postData.params) {
|
|
48
|
+
code.push('const form = new FormData();');
|
|
49
|
+
|
|
50
|
+
source.postData.params.forEach(function (param) {
|
|
51
|
+
code.push(
|
|
52
|
+
'form.append(%s, %s);',
|
|
53
|
+
JSON.stringify(param.name),
|
|
54
|
+
JSON.stringify(param.value || param.fileName || '')
|
|
55
|
+
);
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
code.blank();
|
|
59
|
+
}
|
|
60
|
+
break;
|
|
65
61
|
|
|
66
62
|
default:
|
|
67
63
|
if (source.postData.text) {
|
|
68
|
-
|
|
64
|
+
reqOptions.body = source.postData.text;
|
|
69
65
|
}
|
|
70
66
|
}
|
|
71
67
|
|
|
72
|
-
code
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
68
|
+
code
|
|
69
|
+
.push(
|
|
70
|
+
'const options = %s;',
|
|
71
|
+
stringifyObject(reqOptions, {
|
|
72
|
+
indent: opts.indent,
|
|
73
|
+
inlineCharacterLimit: 80,
|
|
74
|
+
|
|
75
|
+
// The Fetch API body only accepts string parameters, but stringified JSON can be difficult to
|
|
76
|
+
// read, so we keep the object as a literal and use this transform function to wrap the literal
|
|
77
|
+
// in a `JSON.stringify` call.
|
|
78
|
+
transform: (object, property, originalResult) => {
|
|
79
|
+
if (property === 'body') {
|
|
80
|
+
if (source.postData.mimeType === 'application/x-www-form-urlencoded') {
|
|
81
|
+
return `new URLSearchParams(${originalResult})`;
|
|
82
|
+
} else if (source.postData.mimeType === 'application/json') {
|
|
83
|
+
return `JSON.stringify(${originalResult})`;
|
|
84
|
+
}
|
|
88
85
|
}
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
86
|
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
87
|
+
return originalResult;
|
|
88
|
+
},
|
|
89
|
+
})
|
|
90
|
+
)
|
|
91
|
+
.blank();
|
|
96
92
|
|
|
97
93
|
if (source.postData.mimeType === 'multipart/form-data') {
|
|
98
|
-
|
|
99
|
-
.blank()
|
|
94
|
+
if (source.postData.params) {
|
|
95
|
+
code.push('options.body = form;').blank();
|
|
96
|
+
}
|
|
100
97
|
}
|
|
101
98
|
|
|
102
|
-
code
|
|
99
|
+
code
|
|
100
|
+
.push("fetch('%s', options)", source.fullUrl)
|
|
103
101
|
.push(1, '.then(response => response.json())')
|
|
104
102
|
.push(1, '.then(response => console.log(response))')
|
|
105
|
-
.push(1, '.catch(err => console.error(err));')
|
|
103
|
+
.push(1, '.catch(err => console.error(err));');
|
|
106
104
|
|
|
107
|
-
return code.join()
|
|
108
|
-
}
|
|
105
|
+
return code.join();
|
|
106
|
+
};
|
|
109
107
|
|
|
110
108
|
module.exports.info = {
|
|
111
109
|
key: 'fetch',
|
|
112
110
|
title: 'fetch',
|
|
113
111
|
link: 'https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch',
|
|
114
|
-
description: 'Perform asynchronous HTTP requests with the Fetch API'
|
|
115
|
-
}
|
|
112
|
+
description: 'Perform asynchronous HTTP requests with the Fetch API',
|
|
113
|
+
};
|
|
@@ -1,15 +1,13 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
1
|
module.exports = {
|
|
4
2
|
info: {
|
|
5
3
|
key: 'javascript',
|
|
6
4
|
title: 'JavaScript',
|
|
7
5
|
extname: '.js',
|
|
8
|
-
default: 'xhr'
|
|
6
|
+
default: 'xhr',
|
|
9
7
|
},
|
|
10
8
|
|
|
11
9
|
jquery: require('./jquery'),
|
|
12
10
|
fetch: require('./fetch'),
|
|
13
11
|
xhr: require('./xhr'),
|
|
14
|
-
axios: require('./axios')
|
|
15
|
-
}
|
|
12
|
+
axios: require('./axios'),
|
|
13
|
+
};
|
|
@@ -8,76 +8,82 @@
|
|
|
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')
|
|
14
|
-
const helpers = require('../../helpers/headers')
|
|
11
|
+
const CodeBuilder = require('../../helpers/code-builder');
|
|
12
|
+
const helpers = require('../../helpers/headers');
|
|
15
13
|
|
|
16
14
|
module.exports = function (source, options) {
|
|
17
|
-
const opts =
|
|
18
|
-
indent: ' '
|
|
19
|
-
|
|
15
|
+
const opts = {
|
|
16
|
+
indent: ' ',
|
|
17
|
+
...options,
|
|
18
|
+
};
|
|
20
19
|
|
|
21
|
-
const code = new CodeBuilder(opts.indent)
|
|
20
|
+
const code = new CodeBuilder(opts.indent);
|
|
22
21
|
|
|
23
22
|
const settings = {
|
|
24
23
|
async: true,
|
|
25
24
|
crossDomain: true,
|
|
26
25
|
url: source.fullUrl,
|
|
27
26
|
method: source.method,
|
|
28
|
-
headers: source.allHeaders
|
|
29
|
-
}
|
|
27
|
+
headers: source.allHeaders,
|
|
28
|
+
};
|
|
30
29
|
|
|
31
30
|
switch (source.postData.mimeType) {
|
|
32
31
|
case 'application/x-www-form-urlencoded':
|
|
33
|
-
settings.data = source.postData.paramsObj ? source.postData.paramsObj : source.postData.text
|
|
34
|
-
break
|
|
32
|
+
settings.data = source.postData.paramsObj ? source.postData.paramsObj : source.postData.text;
|
|
33
|
+
break;
|
|
35
34
|
|
|
36
35
|
case 'application/json':
|
|
37
|
-
settings.processData = false
|
|
38
|
-
settings.data = source.postData.text
|
|
39
|
-
break
|
|
36
|
+
settings.processData = false;
|
|
37
|
+
settings.data = source.postData.text;
|
|
38
|
+
break;
|
|
40
39
|
|
|
41
40
|
case 'multipart/form-data':
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
41
|
+
if (source.postData.params) {
|
|
42
|
+
code.push('const form = new FormData();');
|
|
43
|
+
|
|
44
|
+
source.postData.params.forEach(function (param) {
|
|
45
|
+
code.push(
|
|
46
|
+
'form.append(%s, %s);',
|
|
47
|
+
JSON.stringify(param.name),
|
|
48
|
+
JSON.stringify(param.value || param.fileName || '')
|
|
49
|
+
);
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
settings.processData = false;
|
|
53
|
+
settings.contentType = false;
|
|
54
|
+
settings.mimeType = 'multipart/form-data';
|
|
55
|
+
settings.data = '[form]';
|
|
56
|
+
|
|
57
|
+
// remove the contentType header
|
|
58
|
+
if (helpers.hasHeader(settings.headers, 'content-type')) {
|
|
59
|
+
if (helpers.getHeader(settings.headers, 'content-type').indexOf('boundary')) {
|
|
60
|
+
delete settings.headers[helpers.getHeaderName(settings.headers, 'content-type')];
|
|
61
|
+
}
|
|
57
62
|
}
|
|
58
|
-
}
|
|
59
63
|
|
|
60
|
-
|
|
61
|
-
|
|
64
|
+
code.blank();
|
|
65
|
+
}
|
|
66
|
+
break;
|
|
62
67
|
|
|
63
68
|
default:
|
|
64
69
|
if (source.postData.text) {
|
|
65
|
-
settings.data = source.postData.text
|
|
70
|
+
settings.data = source.postData.text;
|
|
66
71
|
}
|
|
67
72
|
}
|
|
68
73
|
|
|
69
|
-
code
|
|
74
|
+
code
|
|
75
|
+
.push(`const settings = ${JSON.stringify(settings, null, opts.indent).replace('"[form]"', 'form')};`)
|
|
70
76
|
.blank()
|
|
71
77
|
.push('$.ajax(settings).done(function (response) {')
|
|
72
78
|
.push(1, 'console.log(response);')
|
|
73
|
-
.push('});')
|
|
79
|
+
.push('});');
|
|
74
80
|
|
|
75
|
-
return code.join()
|
|
76
|
-
}
|
|
81
|
+
return code.join();
|
|
82
|
+
};
|
|
77
83
|
|
|
78
84
|
module.exports.info = {
|
|
79
85
|
key: 'jquery',
|
|
80
86
|
title: 'jQuery',
|
|
81
87
|
link: 'http://api.jquery.com/jquery.ajax/',
|
|
82
|
-
description: 'Perform an asynchronous HTTP (Ajax) requests with jQuery'
|
|
83
|
-
}
|
|
88
|
+
description: 'Perform an asynchronous HTTP (Ajax) requests with jQuery',
|
|
89
|
+
};
|
|
@@ -8,75 +8,79 @@
|
|
|
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')
|
|
14
|
-
const helpers = require('../../helpers/headers')
|
|
11
|
+
const CodeBuilder = require('../../helpers/code-builder');
|
|
12
|
+
const helpers = require('../../helpers/headers');
|
|
15
13
|
|
|
16
14
|
module.exports = function (source, options) {
|
|
17
|
-
const opts =
|
|
15
|
+
const opts = {
|
|
18
16
|
indent: ' ',
|
|
19
|
-
cors: true
|
|
20
|
-
|
|
17
|
+
cors: true,
|
|
18
|
+
...options,
|
|
19
|
+
};
|
|
21
20
|
|
|
22
|
-
const code = new CodeBuilder(opts.indent)
|
|
21
|
+
const code = new CodeBuilder(opts.indent);
|
|
23
22
|
|
|
24
23
|
switch (source.postData.mimeType) {
|
|
25
24
|
case 'application/json':
|
|
26
|
-
code.push('const data = JSON.stringify(%s);', JSON.stringify(source.postData.jsonObj, null, opts.indent))
|
|
27
|
-
|
|
28
|
-
break
|
|
25
|
+
code.push('const data = JSON.stringify(%s);', JSON.stringify(source.postData.jsonObj, null, opts.indent)).blank();
|
|
26
|
+
break;
|
|
29
27
|
|
|
30
28
|
case 'multipart/form-data':
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
29
|
+
if (source.postData.params) {
|
|
30
|
+
code.push('const data = new FormData();');
|
|
31
|
+
|
|
32
|
+
source.postData.params.forEach(function (param) {
|
|
33
|
+
code.push(
|
|
34
|
+
'data.append(%s, %s);',
|
|
35
|
+
JSON.stringify(param.name),
|
|
36
|
+
JSON.stringify(param.value || param.fileName || '')
|
|
37
|
+
);
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
// remove the contentType header
|
|
41
|
+
if (helpers.hasHeader(source.allHeaders, 'content-type')) {
|
|
42
|
+
if (helpers.getHeader(source.allHeaders, 'content-type').indexOf('boundary')) {
|
|
43
|
+
// eslint-disable-next-line no-param-reassign
|
|
44
|
+
delete source.allHeaders[helpers.getHeaderName(source.allHeaders, 'content-type')];
|
|
45
|
+
}
|
|
41
46
|
}
|
|
42
|
-
}
|
|
43
47
|
|
|
44
|
-
|
|
45
|
-
|
|
48
|
+
code.blank();
|
|
49
|
+
}
|
|
50
|
+
break;
|
|
46
51
|
|
|
47
52
|
default:
|
|
48
|
-
code.push('const data = %s;', JSON.stringify(source.postData.text || null))
|
|
49
|
-
.blank()
|
|
53
|
+
code.push('const data = %s;', JSON.stringify(source.postData.text || null)).blank();
|
|
50
54
|
}
|
|
51
55
|
|
|
52
|
-
code.push('const xhr = new XMLHttpRequest();')
|
|
56
|
+
code.push('const xhr = new XMLHttpRequest();');
|
|
53
57
|
|
|
54
58
|
if (opts.cors) {
|
|
55
|
-
code.push('xhr.withCredentials = true;')
|
|
59
|
+
code.push('xhr.withCredentials = true;');
|
|
56
60
|
}
|
|
57
61
|
|
|
58
|
-
code
|
|
62
|
+
code
|
|
63
|
+
.blank()
|
|
59
64
|
.push('xhr.addEventListener("readystatechange", function () {')
|
|
60
65
|
.push(1, 'if (this.readyState === this.DONE) {')
|
|
61
66
|
.push(2, 'console.log(this.responseText);')
|
|
62
67
|
.push(1, '}')
|
|
63
68
|
.push('});')
|
|
64
69
|
.blank()
|
|
65
|
-
.push('xhr.open(%s, %s);', JSON.stringify(source.method), JSON.stringify(source.fullUrl))
|
|
70
|
+
.push('xhr.open(%s, %s);', JSON.stringify(source.method), JSON.stringify(source.fullUrl));
|
|
66
71
|
|
|
67
72
|
Object.keys(source.allHeaders).forEach(function (key) {
|
|
68
|
-
code.push('xhr.setRequestHeader(%s, %s);', JSON.stringify(key), JSON.stringify(source.allHeaders[key]))
|
|
69
|
-
})
|
|
73
|
+
code.push('xhr.setRequestHeader(%s, %s);', JSON.stringify(key), JSON.stringify(source.allHeaders[key]));
|
|
74
|
+
});
|
|
70
75
|
|
|
71
|
-
code.blank()
|
|
72
|
-
.push('xhr.send(data);')
|
|
76
|
+
code.blank().push('xhr.send(data);');
|
|
73
77
|
|
|
74
|
-
return code.join()
|
|
75
|
-
}
|
|
78
|
+
return code.join();
|
|
79
|
+
};
|
|
76
80
|
|
|
77
81
|
module.exports.info = {
|
|
78
82
|
key: 'xhr',
|
|
79
83
|
title: 'XMLHttpRequest',
|
|
80
84
|
link: 'https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest',
|
|
81
|
-
description: 'W3C Standard API that provides scripted client functionality'
|
|
82
|
-
}
|
|
85
|
+
description: 'W3C Standard API that provides scripted client functionality',
|
|
86
|
+
};
|