因此,我想使用winston
记录应用程序发出的所有请求。我通过superagent
发出所有API请求,并且我基本上想设置某种中间件,例如:
// Global error middleware
superagent.on('request', req => winston.log(req))
然后在另一个文件中,正常导入
superagent
并使其工作:import superagent from 'superagent'
const request = superagent.get('url' ..etc)
我不想导入自己的包装函数,因为恐怕团队中的其他开发人员会忘记这种做法,直接从superagent导入,从而避免了错误记录。我显然也不想写日志到
superagent
样板的数十个实例中。我四处搜寻,令人惊讶的是,基本上没有解决方案。有办法吗?另外,我想我可以在我的webpack配置中设置别名?
最佳答案
您可以在项目中创建一个子包并链接到该子包。您需要实现一些文件:
首先,为该子程序包创建一个文件夹,以我的情况为例:lib/superagent+logger
(相对于主项目文件夹)
可以根据需要更改名称,以下文件中的name
除外。lib/superagent+logger/package.json
{
"name": "superagent",
"version": "0.1.0",
"description": "superagent wrapper that adds a global logger",
"main": "index.js",
"dependencies": {
"superagent": "latest"
}
}
该名称对于模拟 super 代理很重要,不能更改。
lib/superagent+logger/index.js
var superagent = require("superagent");
function logRequest(req){
//that's the part you'd want to implement
console.log('tracing signal to ', JSON.stringify(req.url));
}
function Smith(){ return superagent.apply(null, arguments).on('request', logRequest); }
Object.setPrototypeOf(Smith, superagent);
module.exports = Object.assign(
Smith,
{
Request(method, url){
return superagent.Request(method, url).on('request', logRequest)
},
agent(options){
return superagent.agent(options).on('request', logRequest)
},
Response(req){
return superagent.Response(req).on('request', logRequest)
},
acl(url, data, fn){
return superagent.acl(url, data, fn).on('request', logRequest)
},
bind(url, data, fn){
return superagent.bind(url, data, fn).on('request', logRequest)
},
checkout(url, data, fn){
return superagent.checkout(url, data, fn).on('request', logRequest)
},
connect(url, data, fn){
return superagent.connect(url, data, fn).on('request', logRequest)
},
copy(url, data, fn){
return superagent.copy(url, data, fn).on('request', logRequest)
},
delete(url, data, fn){
return superagent.delete(url, data, fn).on('request', logRequest)
},
get(url, data, fn){
return superagent.get(url, data, fn).on('request', logRequest)
},
head(url, data, fn){
return superagent.head(url, data, fn).on('request', logRequest)
},
link(url, data, fn){
return superagent.link(url, data, fn).on('request', logRequest)
},
lock(url, data, fn){
return superagent.lock(url, data, fn).on('request', logRequest)
},
"m-search": function(url, data, fn){
return superagent["m-search"](url, data, fn).on('request', logRequest)
},
merge(url, data, fn){
return superagent.merge(url, data, fn).on('request', logRequest)
},
mkactivity(url, data, fn){
return superagent.mkactivity(url, data, fn).on('request', logRequest)
},
mkcalendar(url, data, fn){
return superagent.mkcalendar(url, data, fn).on('request', logRequest)
},
mkcol(url, data, fn){
return superagent.mkcol(url, data, fn).on('request', logRequest)
},
move(url, data, fn){
return superagent.move(url, data, fn).on('request', logRequest)
},
notify(url, data, fn){
return superagent.notify(url, data, fn).on('request', logRequest)
},
options(url, data, fn){
return superagent.options(url, data, fn).on('request', logRequest)
},
patch(url, data, fn){
return superagent.patch(url, data, fn).on('request', logRequest)
},
post(url, data, fn){
return superagent.post(url, data, fn).on('request', logRequest)
},
propfind(url, data, fn){
return superagent.propfind(url, data, fn).on('request', logRequest)
},
proppatch(url, data, fn){
return superagent.proppatch(url, data, fn).on('request', logRequest)
},
purge(url, data, fn){
return superagent.purge(url, data, fn).on('request', logRequest)
},
put(url, data, fn){
return superagent.put(url, data, fn).on('request', logRequest)
},
rebind(url, data, fn){
return superagent.rebind(url, data, fn).on('request', logRequest)
},
report(url, data, fn){
return superagent.report(url, data, fn).on('request', logRequest)
},
search(url, data, fn){
return superagent.search(url, data, fn).on('request', logRequest)
},
subscribe(url, data, fn){
return superagent.subscribe(url, data, fn).on('request', logRequest)
},
trace(url, data, fn){
return superagent.trace(url, data, fn).on('request', logRequest)
},
unbind(url, data, fn){
return superagent.unbind(url, data, fn).on('request', logRequest)
},
unlink(url, data, fn){
return superagent.unlink(url, data, fn).on('request', logRequest)
},
unlock(url, data, fn){
return superagent.unlock(url, data, fn).on('request', logRequest)
},
unsubscribe(url, data, fn){
return superagent.unsubscribe(url, data, fn).on('request', logRequest)
},
del(url, data, fn){
return superagent.del(url, data, fn).on('request', logRequest)
}
}
);
//wrapper built and ready to use
console.log("Hello Mr Anderson\n");
在您的实际项目中:
/package.json
{
...
"dependencies": {
"superagent": "file:lib/superagent+logger"
}
}
和一个简单的测试:
var request = require("superagent");
void request.get("http://google.de").end();
编辑:
我在
lib/superagent+logger/index.js
中写出了以前动态创建的部分并使您感到困惑。+ 没有动态构建的方法
-没有动态构建的方法
该模块没有什么让人惊讶的地方,但是它也不适应内部
superagent
实现/版本中的更改;您将必须手动执行此操作。在阅读本文时,可能要删除一些方法。我的主要目标是如何将此包装器加载/加载到实际项目中,而不是在 super 代理本身上。