Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
TimPchelintsev opened this issue Sep 13, 2021 · 2 comments

Comments

@TimPchelintsev
Copy link

TimPchelintsev commented Sep 13, 2021

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.

@github-actions
Copy link

Internal ticket number is OSS-917

@TimPchelintsev
Copy link
Author

TimPchelintsev commented Sep 15, 2021

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant