gpt4 book ai didi

javascript - 将 superagent 包装在高阶函数中以进行全局错误日志记录?

转载 作者:行者123 更新时间:2023-12-03 09:01:56 25 4
gpt4 key购买 nike

所以我想用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 的变化实现/版本;您必须手动执行此操作。

在我阅读本文时,您可能想要删除一些方法。我的主要目标是如何将这个包装器注入(inject)/加载到实际项目中,而不是在 superagent 本身上。

关于javascript - 将 superagent 包装在高阶函数中以进行全局错误日志记录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42601541/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com