-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathjquery.js
More file actions
83 lines (67 loc) · 2.22 KB
/
jquery.js
File metadata and controls
83 lines (67 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/**
* @description
* HTTP code snippet generator for native XMLHttpRequest
*
* @author
* @AhmadNassri
*
* for any questions or issues regarding the generated code snippet, please open an issue mentioning the author.
*/
'use strict'
const CodeBuilder = require('../../helpers/code-builder')
const helpers = require('../../helpers/headers')
module.exports = function (source, options) {
const opts = Object.assign({
indent: ' '
}, options)
const code = new CodeBuilder(opts.indent)
const settings = {
async: true,
crossDomain: true,
url: source.fullUrl,
method: source.method,
headers: source.allHeaders
}
switch (source.postData.mimeType) {
case 'application/x-www-form-urlencoded':
settings.data = source.postData.paramsObj ? source.postData.paramsObj : source.postData.text
break
case 'application/json':
settings.processData = false
settings.data = source.postData.text
break
case 'multipart/form-data':
code.push('const form = new FormData();')
source.postData.params.forEach(function (param) {
code.push('form.append(%s, %s);', JSON.stringify(param.name), JSON.stringify(param.value || param.fileName || ''))
})
settings.processData = false
settings.contentType = false
settings.mimeType = 'multipart/form-data'
settings.data = '[form]'
// remove the contentType header
if (helpers.hasHeader(settings.headers, 'content-type')) {
if (helpers.getHeader(settings.headers, 'content-type').indexOf('boundary')) {
delete settings.headers[helpers.getHeaderName(settings.headers, 'content-type')]
}
}
code.blank()
break
default:
if (source.postData.text) {
settings.data = source.postData.text
}
}
code.push('const settings = ' + JSON.stringify(settings, null, opts.indent).replace('"[form]"', 'form') + ';')
.blank()
.push('$.ajax(settings).done(function (response) {')
.push(1, 'console.log(response);')
.push('});')
return code.join()
}
module.exports.info = {
key: 'jquery',
title: 'jQuery',
link: 'http://api.jquery.com/jquery.ajax/',
description: 'Perform an asynchronous HTTP (Ajax) requests with jQuery'
}