-
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
c94b58e
commit 7c33a48
Showing
1 changed file
with
70 additions
and
56 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,86 +1,100 @@ | ||
'use strict'; | ||
"use strict"; | ||
|
||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
value: true | ||
}); | ||
var reduce = require('lodash/reduce'); | ||
var isEmpty = require('lodash/isEmpty'); | ||
var queryString = require('query-string'); | ||
var chalk = require('chalk'); | ||
var url = require('url'); | ||
var _debug = require('debug'); | ||
var moment = require('moment'); | ||
var reduce = require("lodash/reduce"); | ||
var isEmpty = require("lodash/isEmpty"); | ||
var queryString = require("query-string"); | ||
var get = require("lodash/get"); | ||
var chalk = require("chalk"); | ||
var url = require("url"); | ||
var _debug = require("debug"); | ||
var moment = require("moment"); | ||
|
||
var appendQuery = function appendQuery(qs) { | ||
if (!isEmpty(qs)) { | ||
return '?' + queryString.stringify(qs); | ||
} | ||
return ''; | ||
if (!isEmpty(qs)) { | ||
return "?" + queryString.stringify(qs); | ||
} | ||
return ""; | ||
}; | ||
|
||
var constructUrl = exports.constructUrl = function constructUrl(url) { | ||
var qs = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
return url + appendQuery(qs); | ||
var qs = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
return url + appendQuery(qs); | ||
}; | ||
|
||
var formatRequestData = function formatRequestData(requestData) { | ||
if (requestData) { | ||
try { | ||
return "-d '" + JSON.stringify(requestData) + "' "; | ||
} catch (e) { | ||
return ""; | ||
} | ||
} | ||
return ""; | ||
}; | ||
|
||
var mapRequestToCurl = exports.mapRequestToCurl = function mapRequestToCurl(request, requestUrl) { | ||
var headers = reduce(request.header, function (headers, value, header) { | ||
return headers + ('-H \'' + header + ': ' + value + '\' '); | ||
}, ' '); | ||
return 'curl -v -X ' + request.method + headers + requestUrl; | ||
var headers = reduce(request.header, function (headers, value, header) { | ||
return headers + ("-H '" + header + ": " + value + "' "); | ||
}, " "); | ||
var requestData = formatRequestData(get(request, "_data", false)); | ||
|
||
return "curl -v -X " + request.method + headers + requestData + requestUrl; | ||
}; | ||
|
||
var getColorByResponseStatus = exports.getColorByResponseStatus = function getColorByResponseStatus(status) { | ||
if (status < 300) { | ||
return 'green'; | ||
} else if (status < 400) { | ||
return 'yellow'; | ||
} | ||
return 'red'; | ||
if (status < 300) { | ||
return "green"; | ||
} else if (status < 400) { | ||
return "yellow"; | ||
} | ||
return "red"; | ||
}; | ||
|
||
var getColorByResponseTime = exports.getColorByResponseTime = function getColorByResponseTime(ms) { | ||
if (ms < 200) { | ||
return 'green'; | ||
} else if (ms < 1000) { | ||
return 'magenta'; | ||
} else if (ms < 5000) { | ||
return 'yellow'; | ||
} | ||
return 'red'; | ||
if (ms < 200) { | ||
return "green"; | ||
} else if (ms < 1000) { | ||
return "magenta"; | ||
} else if (ms < 5000) { | ||
return "yellow"; | ||
} | ||
return "red"; | ||
}; | ||
|
||
var handleResponse = function handleResponse(request, start, logger, debugLog, debugCurl) { | ||
return function (response) { | ||
var now = new Date().getTime(); | ||
var elapsed = now - start; | ||
var elapseTime = elapsed + 'ms'; | ||
return function (response) { | ||
var now = new Date().getTime(); | ||
var elapsed = now - start; | ||
var elapseTime = elapsed + "ms"; | ||
|
||
var uri = url.parse(request.url); | ||
var protocol = uri.protocol.toUpperCase().replace(/[^\w]/g, ''); | ||
var requestMethod = request.method.toUpperCase(); | ||
var responseStatus = response.status; | ||
var uri = url.parse(request.url); | ||
var protocol = uri.protocol.toUpperCase().replace(/[^\w]/g, ""); | ||
var requestMethod = request.method.toUpperCase(); | ||
var responseStatus = response.status; | ||
|
||
var requestUrl = constructUrl(request.url, request.qs); | ||
var curl = mapRequestToCurl(request, requestUrl); | ||
var requestUrl = constructUrl(request.url, request.qs); | ||
var 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 + ")"); | ||
}; | ||
}; | ||
|
||
exports.default = function () { | ||
var logger = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null; | ||
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : { logName: 'super-debug', curlName: 'super-curl' }; | ||
return function (request) { | ||
var debugLog = _debug(options.logName); | ||
var debugCurl = _debug(options.curlName); | ||
var logger = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null; | ||
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : { logName: "super-debug", curlName: "super-curl" }; | ||
return function (request) { | ||
var debugLog = _debug(options.logName); | ||
var debugCurl = _debug(options.curlName); | ||
|
||
var start = new Date().getTime(); | ||
var start = new Date().getTime(); | ||
|
||
request.on('response', handleResponse(request, start, logger, debugLog, debugCurl)); | ||
}; | ||
request.on("response", handleResponse(request, start, logger, debugLog, debugCurl)); | ||
}; | ||
}; |