所以我想使用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
样板的几十个实例。
我已经google了一下,发现,令人惊讶的是,基本上没有解决方案。有办法吗?或者我想我可以在我的webpack配置中设置别名吗?
答案 0 :(得分:-1)
您可以在项目中创建子包并链接到该子包。您需要实现一些文件:
首先为该子包创建一个文件夹,在我的例子中: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"
}
}
这个名称对于模仿superagent非常重要,无法更改
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
实现/版本的变化;你必须手动完成这个。
正如我读到的那样,你可能想要删除一些方法。我的主要目标是如何将这个包装器注入/加载到实际项目中,而不是superagent本身。