-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
25fa3ca
commit c94b58e
Showing
3 changed files
with
114 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,18 @@ | ||
let express = require('express') | ||
let app = express() | ||
let express = require("express"); | ||
let app = express(); | ||
|
||
app.get('/debug', function (req, res) { | ||
res.json({message: 'What an awesome debugger'}) | ||
}) | ||
app.get("/debug", function(req, res) { | ||
res.json({ message: "What an awesome debugger" }); | ||
}); | ||
|
||
app.get('/404', function (req, res) { | ||
res.status(404).end(); | ||
}) | ||
app.get("/404", function(req, res) { | ||
res.status(404).end(); | ||
}); | ||
|
||
app.listen(3000, function () { | ||
console.log('Superagent-debugger app listening on port 3000!') | ||
}) | ||
app.post("/post", function(req, res) { | ||
res.json({ message: "i am a post request" }).end(); | ||
}); | ||
|
||
app.listen(3000, function() { | ||
console.log("Superagent-debugger app listening on port 3000!"); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,79 +1,111 @@ | ||
let reduce = require('lodash/reduce'); | ||
let isEmpty = require('lodash/isEmpty'); | ||
let queryString = require('query-string'); | ||
let chalk = require('chalk'); | ||
let url = require('url'); | ||
let _debug = require('debug'); | ||
let moment = require('moment'); | ||
let reduce = require("lodash/reduce"); | ||
let isEmpty = require("lodash/isEmpty"); | ||
let queryString = require("query-string"); | ||
let get = require("lodash/get"); | ||
let chalk = require("chalk"); | ||
let url = require("url"); | ||
let _debug = require("debug"); | ||
let moment = require("moment"); | ||
|
||
const appendQuery = (qs) => { | ||
if (!isEmpty(qs)) { | ||
return `?${queryString.stringify(qs)}`; | ||
} | ||
return ''; | ||
const appendQuery = qs => { | ||
if (!isEmpty(qs)) { | ||
return `?${queryString.stringify(qs)}`; | ||
} | ||
return ""; | ||
}; | ||
|
||
export const constructUrl = (url, qs = {}) => ( | ||
url + appendQuery(qs) | ||
); | ||
export const constructUrl = (url, qs = {}) => url + appendQuery(qs); | ||
|
||
const formatRequestData = requestData => { | ||
if (requestData) { | ||
try { | ||
return `-d '${JSON.stringify(requestData)}' `; | ||
} catch (e) { | ||
return ""; | ||
} | ||
} | ||
return ""; | ||
}; | ||
|
||
export const mapRequestToCurl = (request, requestUrl) => { | ||
const headers = reduce(request.header, (headers, value, header) => headers + `-H '${header}: ${value}' `, ' '); | ||
return `curl -v -X ${request.method}${headers}${requestUrl}`; | ||
const headers = reduce( | ||
request.header, | ||
(headers, value, header) => headers + `-H '${header}: ${value}' `, | ||
" " | ||
); | ||
let requestData = formatRequestData(get(request, "_data", false)); | ||
|
||
return `curl -v -X ${request.method}${headers}${requestData}${requestUrl}`; | ||
}; | ||
|
||
export const getColorByResponseStatus = (status) => { | ||
if (status < 300) { | ||
return 'green'; | ||
} else if (status < 400) { | ||
return 'yellow'; | ||
} | ||
return 'red'; | ||
export const getColorByResponseStatus = status => { | ||
if (status < 300) { | ||
return "green"; | ||
} else if (status < 400) { | ||
return "yellow"; | ||
} | ||
return "red"; | ||
}; | ||
|
||
export const getColorByResponseTime = (ms) => { | ||
if (ms < 200) { | ||
return 'green'; | ||
} else if (ms < 1000) { | ||
return 'magenta'; | ||
} else if (ms < 5000) { | ||
return 'yellow'; | ||
} | ||
return 'red'; | ||
export const getColorByResponseTime = ms => { | ||
if (ms < 200) { | ||
return "green"; | ||
} else if (ms < 1000) { | ||
return "magenta"; | ||
} else if (ms < 5000) { | ||
return "yellow"; | ||
} | ||
return "red"; | ||
}; | ||
|
||
const handleResponse = (request, start, logger, debugLog, debugCurl) => (response) => { | ||
const now = new Date().getTime(); | ||
const elapsed = now - start; | ||
const elapseTime = elapsed + 'ms'; | ||
const handleResponse = ( | ||
request, | ||
start, | ||
logger, | ||
debugLog, | ||
debugCurl | ||
) => response => { | ||
const now = new Date().getTime(); | ||
const elapsed = now - start; | ||
const elapseTime = elapsed + "ms"; | ||
|
||
const uri = url.parse(request.url); | ||
const protocol = uri.protocol.toUpperCase().replace(/[^\w]/g, ''); | ||
const requestMethod = request.method.toUpperCase(); | ||
const responseStatus = response.status; | ||
const uri = url.parse(request.url); | ||
const protocol = uri.protocol.toUpperCase().replace(/[^\w]/g, ""); | ||
const requestMethod = request.method.toUpperCase(); | ||
const responseStatus = response.status; | ||
|
||
const requestUrl = constructUrl(request.url, request.qs); | ||
const curl = mapRequestToCurl(request, requestUrl); | ||
const requestUrl = constructUrl(request.url, request.qs); | ||
const curl = mapRequestToCurl(request, requestUrl); | ||
|
||
debugCurl.apply(null, [chalk.gray(curl)]); | ||
logger(curl) | ||
debugCurl.apply(null, [chalk.gray(curl)]); | ||
logger(curl); | ||
|
||
debugLog.apply(null, [ | ||
'%s %s %s %s %s', | ||
chalk.magenta(protocol), | ||
chalk.cyan(requestMethod), | ||
chalk[getColorByResponseStatus(responseStatus)](responseStatus), | ||
chalk.gray(requestUrl), | ||
chalk.gray('(') + chalk[getColorByResponseTime(elapsed)](elapseTime) + chalk.gray(')') | ||
]) | ||
logger(`${protocol} ${requestMethod} ${responseStatus} ${requestUrl} (${elapseTime})`) | ||
} | ||
debugLog.apply(null, [ | ||
"%s %s %s %s %s", | ||
chalk.magenta(protocol), | ||
chalk.cyan(requestMethod), | ||
chalk[getColorByResponseStatus(responseStatus)](responseStatus), | ||
chalk.gray(requestUrl), | ||
chalk.gray("(") + | ||
chalk[getColorByResponseTime(elapsed)](elapseTime) + | ||
chalk.gray(")") | ||
]); | ||
logger( | ||
`${protocol} ${requestMethod} ${responseStatus} ${requestUrl} (${elapseTime})` | ||
); | ||
}; | ||
|
||
export default (logger = null, options = {logName: 'super-debug', curlName: 'super-curl'}) => (request) => { | ||
const debugLog = _debug(options.logName); | ||
const debugCurl = _debug(options.curlName); | ||
export default ( | ||
logger = null, | ||
options = { logName: "super-debug", curlName: "super-curl" } | ||
) => request => { | ||
const debugLog = _debug(options.logName); | ||
const debugCurl = _debug(options.curlName); | ||
|
||
const start = new Date().getTime(); | ||
const start = new Date().getTime(); | ||
|
||
request.on('response', handleResponse(request, start, logger, debugLog, debugCurl)); | ||
} | ||
request.on( | ||
"response", | ||
handleResponse(request, start, logger, debugLog, debugCurl) | ||
); | ||
}; |