client/index.js

  1. 'use strict';
  2. /**
  3. * ### RTM API Client
  4. *
  5. * This Class is used to represent an RTM API Client. The Client contains
  6. * the API Key, API Secret and access permissions used to access the
  7. * RTM API endpoints.
  8. *
  9. * It also includes API wrapper functions for making a general RTM API request
  10. * as well as the auth-related functions.
  11. *
  12. * #### Usage
  13. *
  14. * The `RTMClient` Class is what is exported when the entire `rtm-api` module
  15. * is loaded via `require`.
  16. *
  17. * ```
  18. * const RTM = require('rtm-api');
  19. * let client = new RTM(API_KEY, API_SECRET, RTM.PERM_DELETE);
  20. * ```
  21. *
  22. * #### Auth Example
  23. *
  24. * This example gets an Auth URL to be opened by the RTM User
  25. *
  26. * ```
  27. * client.auth.getAuthUrl(function(err, authUrl, frob) {
  28. * // Have user authenticate and authorize the program with the authUrl
  29. * // Once authorized by the user, use the frob to get an authToken
  30. * )};
  31. * ```
  32. *
  33. * #### RTM API Example
  34. *
  35. * This example makes an RTM API request using the method `rtm.method` and the
  36. * parameter foo=bar.
  37. *
  38. * ```
  39. * client.get('rtm.method', {foo: "bar"}, function(err, resp) {
  40. * if ( err ) {
  41. * // handle error
  42. * }
  43. * // use the response
  44. * });
  45. * ```
  46. *
  47. * See {@link RTMUser#get|RTMUser.get} for making User-authenticated API requests.
  48. *
  49. * @class
  50. */
  51. class RTMClient {
  52. /**
  53. * Create a new RTM Client with the specified API access information
  54. * @param {string} key RTM API Client Key
  55. * @param {string} secret RTM API Client Secret
  56. * @param {string} [perms=RTMClient.PERM_READ] RTM API Client Access Permissions. This
  57. * should be one of {@link RTMClient.PERM_READ}, {@link RTMClient.PERM_WRITE} or
  58. * {@link RTMClient.PERM_DELETE}.
  59. * @constructor
  60. */
  61. constructor(key, secret, perms=RTMClient.PERM_READ) {
  62. this._apiKey = key;
  63. this._apiSecret = secret;
  64. this._perms = perms;
  65. }
  66. /**
  67. * RTM API Client Key
  68. * @type {string}
  69. */
  70. get key() {
  71. return this._apiKey;
  72. }
  73. /**
  74. * RTM API Client Secret
  75. * @type {string}
  76. */
  77. get secret() {
  78. return this._apiSecret;
  79. }
  80. /**
  81. * RTM API Client Access Permissions
  82. * @type {string}
  83. */
  84. get perms() {
  85. return this._perms;
  86. }
  87. // ===== USER FUNCTIONS ===== //
  88. /**
  89. * User export/import-related functions:
  90. * - {@link RTMClient~user/create|create}
  91. * - {@link RTMClient~user/export|export}
  92. * - {@link RTMClient~user/exportToString|exportToString}
  93. * - {@link RTMClient~user/import|import}
  94. * - {@link RTMClient~user/importFromString|importFromString}
  95. * @returns {{create: function, export: function, exportToString: function, import: function, importFromString: function}}
  96. */
  97. get user() {
  98. return require('./user.js')(this);
  99. }
  100. // ===== API HELPER FUNCTIONS ===== //
  101. /**
  102. * Make the specified RTM API call.
  103. *
  104. * The `method` should be the name of the RTM API method. Any necessary
  105. * parameters should be provided with `params` as an object with the properties
  106. * of the object as the parameters' key/value pairs.
  107. *
  108. * RTM API methods that require an AuthToken should set the `params` `auth_token`
  109. * property or provide a valid `RTMUser` with an AuthToken.
  110. * @param {string} method RTM API Method
  111. * @param {object} [params] RTM Method Parameters (as an object with key/value pairs)
  112. * @param {RTMUser} [user=undefined] The RTM User making the request
  113. * @param {function} callback Callback function(err, resp)
  114. * @param {RTMError} callback.err RTM Error Response, if encountered
  115. * @param {RTMSuccess} callback.resp The parsed RTM API Response, if successful
  116. */
  117. get(method, params, user, callback) {
  118. require('../utils/get.js')(method, params, user, this, callback);
  119. }
  120. /**
  121. * Auth-related functions:
  122. * - {@link RTMClient~auth/getAuthUrl|getAuthUrl}
  123. * - {@link RTMClient~auth/getAuthToken|getAuthToken}
  124. * - {@link RTMClient~auth/verifyAuthToken|verifyAuthToken}
  125. * @returns {{getAuthUrl: function, getAuthToken: function, verifyAuthToken: function}}
  126. */
  127. get auth() {
  128. return require('./auth.js')(this);
  129. }
  130. }
  131. // ==== RTM API PERMISSION LEVELS ==== //
  132. /**
  133. * RTM API Access: `read` -
  134. * gives the ability to read task, contact, group and list details and contents.
  135. * @type {string}
  136. * @default
  137. */
  138. RTMClient.PERM_READ = 'read';
  139. /**
  140. * RTM API Access: `write` -
  141. * gives the ability to add and modify task, contact, group and list details and
  142. * contents (also allows you to `read`).
  143. * @type {string}
  144. * @default
  145. */
  146. RTMClient.PERM_WRITE = 'write';
  147. /**
  148. * RTM API Access: `delete` -
  149. * gives the ability to delete task, contacts, groups and list (also allows
  150. * you to `read` and `write`).
  151. * @type {string}
  152. * @default
  153. */
  154. RTMClient.PERM_DELETE = 'delete';
  155. module.exports = RTMClient;