faunadb-js icon indicating copy to clipboard operation
faunadb-js copied to clipboard

Please make it possible to get response headers for query directly🙏

Open TimPchelintsev opened this issue 3 years ago • 2 comments

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.

TimPchelintsev avatar Sep 13 '21 16:09 TimPchelintsev

Internal ticket number is OSS-917

github-actions[bot] avatar Sep 13 '21 16:09 github-actions[bot]

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
  }
}

TimPchelintsev avatar Sep 15 '21 10:09 TimPchelintsev