forked from sebastianlzy/superagent-debugger
-
Notifications
You must be signed in to change notification settings - Fork 3
/
superdebug-browser.js
80 lines (72 loc) · 2.34 KB
/
superdebug-browser.js
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
'use strict'
var _debug = require('debug')
// IE :-(
if (!Object.keys) {
Object.keys = function (obj) {
var keys = []
for (var i in obj) {
if (obj.hasOwnProperty(i)) keys.push(i)
}
return keys
}
}
function formatToCurl (request, requestUrl) {
var headers = Object.keys(request.header).reduce(function (headers, key) {
return headers + ('-H \'' + key + ': ' + request.header[key] + '\' ')
}, '')
var data = ''
if (request._data) {
try {
data = `-d '${JSON.stringify(request._data)}' `
} catch (error) {
data = `-d '${request._data}' `
}
}
var formData = ''
if (request._formData) {
formData = '-F ' + Object.keys(request._formData).map(key => `'${key}=${request._formData[key]}' `).join('-F ')
}
var query = ''
if (request.qs) {
query = requestUrl.includes('?') ? '&' : '?'
query += Object.keys(request.qs).map(key => `${key}=${request.qs[key]}`).join('&')
}
return 'curl -v -X ' + request.method + ' ' + headers + formData + data + constructUrl(requestUrl, query)
};
function constructUrl (requestUrl, query) {
var urlparts = requestUrl.split('#')
var url = urlparts[0] + query
url += urlparts[1] ? '#' + urlparts[1] : ''
return url
}
function handleResponse (request, start, logger, debugLog, debugCurl) {
return function (response) {
var now = new Date().getTime()
var elapsed = now - start
var elapseTime = elapsed + 'ms'
var protocol = request.protocol.toUpperCase().replace(/[^\w]/g, '')
var requestMethod = request.method.toUpperCase()
var curl = formatToCurl(request, request.url)
debugCurl(curl)
logger(curl)
debugLog(
'%s %s %s %s %s',
protocol,
requestMethod,
response.status,
request.url,
'(' + elapseTime + ')'
)
logger(protocol + ' ' + requestMethod + ' ' + response.status + ' ' + request.url + ' (' + elapseTime + ')')
}
};
module.exports = function superdebug (logger, options = { logName: 'super-debug', curlName: 'super-curl' }) {
logger = logger || function () { }
options = options || { logName: 'super-debug', curlName: 'super-curl' }
return function (request) {
var debugLog = _debug(options.logName)
var debugCurl = _debug(options.curlName)
var start = new Date().getTime()
request.on('response', handleResponse(request, start, logger, debugLog, debugCurl))
}
}