function listen(fn){
//this line simply adds functions to call in an array based on setup below
if(listen.prototype.arguments){listen.prototype.arguments.push(fn)}
//below is setup(functions hijacked so services that use them report to this too)
listen.prototype.arguments=[] //array of functions to call
let original1=XMLHttpRequest.prototype.send, original2=fetch
function replace1(){
let toReturn=original1.bind(this)(...arguments)
this.addEventListener("load",res=>fn(this.responseText))
return toReturn
}
function replace2(){
let toReturn=original2.bind(this)(...arguments)
toReturn.then(res=>res.text().then(fn))
return toReturn
}
Object.defineProperty(XMLHttpRequest.prototype,"send",{value:replace1})
Object.defineProperty(window,"fetch",{value:replace2})
}
//example usage
let socket=some_Web_Socket_Your_Backend_To_Handle_This_Data
//perhaps this is not the most elaborate example but I hope the point is understood
listen(socket.send.bind(socket))
2条答案
按热度按时间emeijp431#
您可以劫持
XMLHttpRequest
和fetch
函数。非传统的,但它会工作基于我放置的注解,客户端可以使用一个小函数来完成这些工作
wvyml7n52#
您可以在NodeJS/ExpressJS中创建一个端点,如下所示
fetchGraphQLData看起来类似于这样:
node-fetch
或axios
**库来执行HTTP请求,否则部署中会遇到问题。