starling.js

  1. import Customer from './customer';
  2. import Account from './account';
  3. import Address from './address';
  4. import Transaction from './transaction';
  5. import Card from './card';
  6. import Contact from './contact';
  7. /**
  8. * Facade to dispatch operations to services
  9. */
  10. class Starling {
  11. /**
  12. * Create an instance of the starling client
  13. * @param {Object=} options - configuration parameters
  14. */
  15. constructor (options) {
  16. const defaults = {
  17. apiUrl: 'https://api.starlingbank.com'
  18. };
  19. this.config = Object.assign({}, defaults, options);
  20. this.customer = new Customer(this.config);
  21. this.account = new Account(this.config);
  22. this.address = new Address(this.config);
  23. this.transaction = new Transaction(this.config);
  24. this.contact = new Contact(this.config);
  25. this.card = new Card(this.config);
  26. }
  27. /**
  28. * Gets the customer's details
  29. * @param {string=} accessToken - the oauth bearer token. If not
  30. * specified, the accessToken on the options object is used.
  31. * @return {Promise} - the http request promise
  32. */
  33. getCustomer (accessToken = this.config.accessToken) {
  34. return this.customer.getCustomer(accessToken);
  35. }
  36. /**
  37. * Gets the customer's account details
  38. * @param {string=} accessToken - the oauth bearer token. If not
  39. * specified, the accessToken on the options object is used.
  40. * @return {Promise} - the http request promise
  41. */
  42. getAccount (accessToken = this.config.accessToken) {
  43. return this.account.getAccount(accessToken);
  44. }
  45. /**
  46. * Gets the customer's balance
  47. * @param {string=} accessToken - the oauth bearer token. If not
  48. * specified, the accessToken on the options object is used.
  49. * @return {Promise} - the http request promise
  50. */
  51. getBalance (accessToken = this.config.accessToken) {
  52. return this.account.getBalance(accessToken);
  53. }
  54. /**
  55. * Gets the customer's addresses (current and previous)
  56. * @param {string=} accessToken - the oauth bearer token. If not
  57. * specified, the accessToken on the options object is used.
  58. * @return {Promise} - the http request promise
  59. */
  60. getAddresses (accessToken = this.config.accessToken) {
  61. return this.address.getAddresses(accessToken);
  62. }
  63. /**
  64. * Gets the customer's transaction history
  65. * @param {string} fromDate - filter transactions after this date. Format: YYYY-MM-DD (optional,
  66. * defaults to one month in past if not provided)
  67. * @param {string} toDate - filter transactions before this date. Format: YYYY-MM-DD (optional,
  68. * defaults to current date if not provided)
  69. * @param {string=} accessToken - the oauth bearer token. If not
  70. * specified, the accessToken on the options object is used.
  71. * @return {Promise} - the http request promise
  72. */
  73. getTransactions (fromDate, toDate, accessToken = this.config.accessToken) {
  74. return this.transaction.getTransactions(fromDate, toDate, accessToken);
  75. }
  76. /**
  77. * Gets the full details of a single transaction
  78. * @param {string} transactionID - the unique transaction ID
  79. * @param {string=} accessToken - the oauth bearer token. If not
  80. * specified, the accessToken on the options object is used.
  81. * @return {Promise} - the http request promise
  82. */
  83. getTransaction (transactionID, accessToken = this.config.accessToken) {
  84. return this.transaction.getTransaction(transactionID, accessToken);
  85. }
  86. /**
  87. * Gets the customer's balance
  88. * @param {string=} accessToken - the oauth bearer token. If not
  89. * specified, the accessToken on the options object is used.
  90. * @return {Promise} - the http request promise
  91. */
  92. getContacts (accessToken = this.config.accessToken) {
  93. return this.contact.getContacts(accessToken);
  94. }
  95. /**
  96. * Gets the customer's card
  97. * @param {string=} accessToken - the oauth bearer token. If not
  98. * specified, the accessToken on the options object is used.
  99. * @return {Promise} - the http request promise
  100. */
  101. getCard (accessToken = this.config.accessToken) {
  102. return this.card.getCard(accessToken);
  103. }
  104. }
  105. module.exports = Starling;