gpt4 book ai didi

javascript - 如何在 NestJS 中记录所有 Axios 外部 http 请求

转载 作者:行者123 更新时间:2023-12-03 00:22:50 24 4
gpt4 key购买 nike

我希望能够使用完整的 url、 header 等记录每个 axios 请求,但目前没有找到方法。

到目前为止我所做的就是基于此 answer 编写一个 Http 拦截器

export class HttpLoggerInterceptor implements NestInterceptor {
intercept(
context: ExecutionContext,
call$: Observable<any>,
): Observable<any> {
return call$.pipe(
map(data => {
// pipe call to add / modify header(s) after remote method
const req = context.switchToHttp().getRequest();
return data;
}),
);
}
}

现在我在调试时浏览对象 reqcontext 属性,但看不到 Asios 请求 url 等。除非我错过了。

我的 Controller 路由(在这种情况下是api/data)发生了N个http外部调用,但拦截器仅拦截 Controller Controller 调用而不是Axios调用。

有什么想法吗?

这是context对象:

args:Array(2) [IncomingMessage, ServerResponse]
constructorRef:class AppController { … }
getRequest:() => …
getResponse:() => …
handler:data() { … }
__proto__:Object {constructor: , getClass: , getHandler: , …}

req:

_dumped:false
_events:Object {}
_eventsCount:0
_maxListeners:undefined
_parsedOriginalUrl:Url {protocol: null, slashes: null, auth: null, …}
_parsedUrl:Url {protocol: null, slashes: null, auth: null, …}
_readableState:ReadableState {objectMode: false, highWaterMark: 16384, buffer: BufferList, …}
baseUrl:""
body:Object {}
client:Socket {connecting: false, _hadError: false, _handle: TCP, …}
complete:true
connection:Socket {connecting: false, _hadError: false, _handle: TCP, …}
destroyed:false
fresh:false
headers:Object {accept: "application/json, text/plain, */*", user-agent: "axios/0.18.0", host: "localhost:3000", …}
host:"localhost"
hostname:"localhost"
httpVersion:"1.1"
httpVersionMajor:1
httpVersionMinor:1
ip:"::ffff:127.0.0.1"
ips:Array(0)
method:"GET"
next:function next(err) { … }
originalUrl:"/api/data"
params:Object {}
__proto__:Object {constructor: , __defineGetter__: , __defineSetter__: , …}
path:"/api/data"
protocol:"http"
query:Object {}
rawHeaders:Array(8) ["Accept", "application/json, text/plain, */*", "User-Agent", …]
rawTrailers:Array(0) []
readable:true
readableBuffer:BufferList
readableFlowing:null
readableHighWaterMark:16384
readableLength:0
res:ServerResponse {_events: Object, _eventsCount: 1, _maxListeners: undefined, …}
route:Route {path: "/api/data", stack: Array(1), methods: Object}
secure:false
socket:Socket {connecting: false, _hadError: false, _handle: TCP, …}
stale:true
statusCode:null
statusMessage:null
subdomains:Array(0)
trailers:Object {}
upgrade:false
url:"/api/data"
xhr:false

最佳答案

Nest.js-Interceptors 仅处理 Controller 处理的请求和发出的响应。如果您在处理 Controller 请求时使用 Axios 发出 http 请求,则拦截器不会处理这些请求。

<小时/>

axios拦截器

HttpService 直接通过 get axiosRef() 公开其 axios 实例。有了它,您可以添加 axios interceptor :

this.httpService.axiosRef.interceptors.request.use(config => { /*...*/ return config })

例如,您可以在 onModuleInit() 中执行此操作您的AppModule

<小时/>

委托(delegate)给外观

作为替代方案,您可以创建一个 HttpService 外观,用于记录请求并将所有调用委托(delegate)给内置 HttpService:

@Injectable()
export class MyHttpService {
private logger: Logger = new Logger(MyHttpService.name);

constructor (private httpService: HttpService) {}

public get<T = any>(url: string, config?: AxiosRequestConfig): Observable<AxiosResponse<T>> {
this.logger.log({url, config});
return this.httpService.get(url, config)
.pipe(tap(response => this.logger.log(response)));
}

// ... all the other methods you need.

}

您可以创建自己的 LoggingHttpModule,导入内置 HttpModule 并导出 MyHttpService

关于javascript - 如何在 NestJS 中记录所有 Axios 外部 http 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54229131/

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