faunadb-js
faunadb-js copied to clipboard
Please make it possible to get response headers for query directly🙏
Now it is possible to observe headers with observer function, but it will be much more convenient to simply pass some per-query option like returnResponse
so that I can get billing headers in my tests and write a test suite to calculate projected cost of my app.
Currently, client.query(...)
returns query result only. Desired feature will work like this:
const { result, response } = client.query(..., { returnResponse: true })
I firmly believe this feature will benefit all Faunadb community.
Internal ticket number is OSS-917
Based on idea from this Fauna forum issue, I managed to incorporate this solution by overriding faunadb.Client class:
class ObserverClient extends faunadb.Client {
constructor(opts) {
super({
...opts,
// overrides observer to save last response
observer: (res) => {
this._lastResponse = res
opts.observer && opts.observer(res) // in case we have custom observer passed.
},
})
this._lastResponse = null
}
async query(expr, options = {}) {
const { returnResponse, ...rest } = options
const result = await super.query(expr, rest)
return returnResponse ? { result, response: this._lastResponse } : result
}
}