Skip to content

Instantly share code, notes, and snippets.

@hharzer
Forked from n400/expose-response-headers.md
Created August 6, 2020 02:24
Show Gist options
  • Save hharzer/d73d6cc3073e62f71b7564eea8364fea to your computer and use it in GitHub Desktop.
Save hharzer/d73d6cc3073e62f71b7564eea8364fea to your computer and use it in GitHub Desktop.
Example observers to expose response headers from FaunaDB JS driver

When the FaunaDB JavaScript driver returns query results, it's exposing the the response payload, but not the response headers. However, the response headers contain query metrics you might want (e.g., read ops, write ops, transaction retires, query time, bytes in/out, storage bytes read/written, read ops).

The web shell/REPL at dashboard.fauna.com also presents this information to you through the GUI (click the "i" on left).

Here are some examples to help you get the repsonse headers from the driver:

var faunadb = require('./index')
var q = faunadb.query
function account (res) {
  if (!res || !res.responseHeaders) return
  h = res.responseHeaders
  console.log(h)
}
var client = new faunadb.Client({
  secret: 'secret',
  domain: 'localhost',
  port: 8443,
  scheme: 'http',
  observer: account
})

The FaunaDB dashboard does it by hooking it into API.selectDatabase:

type ApiConfig = {
  urlString: string
  secret: string
  opts?: ClientConfig
}
export const selectDatabase = (path: string | null | undefined, opts: ClientConfig): void => {
  const secret = path ? createDatabaseSecret(path) : rootSecret()
  connect({
    ...apiConfig,
    secret,
    opts
  })
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment