All files / lib/client index.js

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98                                                                                                                                                                                                   
/**
 * Client Module
 * @description The client module is the entry point for our SDK.
 *              It holds a Promise-based authentication method
 *              ([connect]{@link client#connect}) as well as
 *              allowing for raw use of the resources (without authentication).
 * @module client
 */
 
import {
  assocPath,
  curry,
  merge,
  map,
  ifElse,
  is,
} from 'ramda'
 
import strategies from './strategies'
import resources from '../resources'
 
const bindOptions = options => func => func.bind(null, options)
 
const bindRecursive = options => ifElse(
  is(Function),
  bindOptions(options),
  resource => map(bindRecursive(options), resource)
)

/**
 * Binds the `options` and `version` received as param
 * to the client's resources.
 *
 * @param {Object} options
 * @param {Object} version
 * @returns A version of resources with its methods' first param binded to `options`
 * and sets the header 'X-PagarMe-Version' to the version passed as param
 */
const withVersion = curry((options, version) => {
  const optionsWithVersion = assocPath(
    ['headers', 'X-PagarMe-Version'],
    version,
    options
  )
 
  const boundClientWithVersion = map(bindRecursive(optionsWithVersion), resources)
  return boundClientWithVersion
})
 
/**
 * Binds the `options` received as param
 * to the client's resources.
 * @private
 *
 * @param {Object} options
 * @returns A version of resources with its methods' first param binded to `options`
 */
function bindClientOptions ({ options, authentication }) {
  const boundClient = map(bindRecursive(options), resources)
 
  return merge(
    boundClient,
    {
      authentication,
      withVersion: withVersion(options),
    }
  )
}
 
/**
 * Returns a version of client with
 * authentication data binded to the
 * resource requests.
 *
 * @example
 * // API Key Authentication
 * pagarme.client.connect({ api_key: 'ak_test_y7jk294ynbzf93' })
 *
 * // Encryption Key Authentication
 * pagarme.client.connect({ encryption_key: 'ek_test_y7jk294ynbzf93' })
 *
 * // Login Authentication
 * pagarme.client.connect({ email: 'user@email.com', password: '123456' })
 *
 * @param {Object} authentication
 * @returns {Promise} A Promise that resolves to a client with authentication data binded
 */
function connect (authentication) {
  return strategies
    .find(authentication)
    .then(s => s.execute())
    .then(bindClientOptions)
}
 
const client = merge({ connect }, resources)
 
export default client