Skip to content

Commit

Permalink
added post support with data
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianlzy committed Jun 21, 2017
1 parent 25fa3ca commit c94b58e
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 72 deletions.
25 changes: 14 additions & 11 deletions example/index.js
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!");
});
7 changes: 7 additions & 0 deletions example/sdk.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,10 @@ superagent
.use(superdebug(console.info))
.send()
.end();

superagent
.post('http://localhost:3000/post')
.set({Accept: 'application/json'})
.use(superdebug(console.info))
.send({ name: 'superagent', type: 'debugger' })
.end();
154 changes: 93 additions & 61 deletions index.js
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)
);
};

0 comments on commit c94b58e

Please sign in to comment.