getToken.js 657 Bytes
const createError = require('http-errors')
const su = require('superagent')
const { path, head } = require('ramda')

const dbhost = process.env.COUCHDB_URL
const dbtokens = process.env.DB_TOKENS

const headers = {
  'Accept': 'application/json',
  'Content-Type': 'application/json',
}

const getAccessToken = () =>
  su.get(`${dbhost}/${dbtokens}/_all_docs`)
  .set(headers)
  .query({ limit: 1, include_docs:true })
  .then(path(['body', 'rows']))
  .then(head)
  .then(path(['doc', 'accessToken']))

module.exports = (req, res, next) => {
  getAccessToken()
  .then((accessToken) => res.json({ accessToken }))
  .catch(error => next(createError(500)))
}