@beauraines/rtm-api 1.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (52) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +285 -0
  3. package/config.js +22 -0
  4. package/docs/RTMClient.html +2798 -0
  5. package/docs/RTMError.html +1029 -0
  6. package/docs/RTMList.html +966 -0
  7. package/docs/RTMResponse.html +868 -0
  8. package/docs/RTMSuccess.html +337 -0
  9. package/docs/RTMTask.html +2461 -0
  10. package/docs/RTMUser.html +6761 -0
  11. package/docs/client_auth.js.html +123 -0
  12. package/docs/client_index.js.html +241 -0
  13. package/docs/client_user.js.html +170 -0
  14. package/docs/global.html +386 -0
  15. package/docs/index.html +305 -0
  16. package/docs/list_index.js.html +159 -0
  17. package/docs/response_error.js.html +172 -0
  18. package/docs/response_response.js.html +160 -0
  19. package/docs/response_success.js.html +104 -0
  20. package/docs/scripts/linenumber.js +25 -0
  21. package/docs/scripts/prettify/Apache-License-2.0.txt +202 -0
  22. package/docs/scripts/prettify/lang-css.js +2 -0
  23. package/docs/scripts/prettify/prettify.js +28 -0
  24. package/docs/styles/jsdoc.css +664 -0
  25. package/docs/styles/prettify.css +79 -0
  26. package/docs/task_helper.js.html +531 -0
  27. package/docs/task_index.js.html +304 -0
  28. package/docs/user_index.js.html +347 -0
  29. package/docs/user_lists.js.html +208 -0
  30. package/docs/user_tasks.js.html +703 -0
  31. package/jsdoc.json +13 -0
  32. package/package.json +33 -0
  33. package/src/client/auth.js +65 -0
  34. package/src/client/index.js +182 -0
  35. package/src/client/user.js +112 -0
  36. package/src/list/helper.js +131 -0
  37. package/src/list/index.js +101 -0
  38. package/src/response/error.js +114 -0
  39. package/src/response/index.js +8 -0
  40. package/src/response/parse.js +51 -0
  41. package/src/response/response.js +102 -0
  42. package/src/response/success.js +46 -0
  43. package/src/task/helper.js +469 -0
  44. package/src/task/index.js +264 -0
  45. package/src/user/index.js +288 -0
  46. package/src/user/lists.js +150 -0
  47. package/src/user/tasks.js +708 -0
  48. package/src/utils/auth.js +188 -0
  49. package/src/utils/fetch.js +67 -0
  50. package/src/utils/get.js +247 -0
  51. package/src/utils/sign.js +93 -0
  52. package/src/utils/taskIds.js +188 -0
@@ -0,0 +1,288 @@
1
+ 'use strict';
2
+
3
+ const config = require('../../config');
4
+
5
+ /**
6
+ * ### RTM User
7
+ *
8
+ * This Class is used to represent an authorized RTM User. An `RTMUser` instance
9
+ * contains the user's ID, username and fullname as well as an auth token that can
10
+ * be used to make user-authenticated API requests.
11
+ *
12
+ * #### Usage
13
+ *
14
+ * An `RTMUser` instance can created manually with an {@link RTMClient}:
15
+ *
16
+ * ```
17
+ * let user = client.user.create(1234, 'username', 'full name', 'auth_token');
18
+ * ```
19
+ *
20
+ * or can be returned through the RTM API auth process (specifically the callback
21
+ * function from {@link RTMClient~auth/getAuthToken|RTMClient.auth.getAuthToken()}).
22
+ *
23
+ * ```
24
+ * // Get an Auth URL
25
+ * client.auth.getAuthUrl(function(err, authUrl, frob) {
26
+ *
27
+ * // Have the User open this URL in their browser
28
+ * console.log(authUrl);
29
+ *
30
+ * // Get an authenticated RTMUser
31
+ * client.auth.getAuthToken(frob, function(err, user) {
32
+ *
33
+ * // user is an instance of RTMUser
34
+ *
35
+ * });
36
+ *
37
+ * });
38
+ * ```
39
+ *
40
+ * #### API Wrappers
41
+ *
42
+ * The `RTMUser` also includes a number of wrapper functions for commonly
43
+ * used RTM API methods dealing with Lists and Tasks.
44
+ *
45
+ * **Tasks:**
46
+ *
47
+ * For example, to get the User's RTM Tasks:
48
+ *
49
+ * ```
50
+ * user.tasks.get(function(err, tasks) {
51
+ * console.log(tasks);
52
+ * });
53
+ * ```
54
+ *
55
+ * The `tasks.get()` function will also fetch the User's RTM Lists and add
56
+ * the List (as an `RTMList` instance) that contains the Task to the `list`
57
+ * property of the `RTMTask`.
58
+ *
59
+ * @class
60
+ */
61
+ class RTMUser {
62
+
63
+ /**
64
+ * Create a new RTM User.
65
+ *
66
+ * An `RTMUser` can be used to make user-authenticated RTM API calls and also
67
+ * includes wrapper methods around some common RTM API methods.
68
+ * @param {number} id The RTM User's ID
69
+ * @param {string} username The RTM User's username
70
+ * @param {string} fullname The RTM User's full name
71
+ * @param {string} authToken The RTM User's Auth Token
72
+ * @constructor
73
+ */
74
+ constructor(id, username, fullname, authToken) {
75
+ this._id = parseFloat(id);
76
+ this._username = username;
77
+ this._fullname = fullname;
78
+ this._authToken = authToken;
79
+ this._client = undefined;
80
+ this._timeline = undefined;
81
+ this._burstsRemaining = config.api.rate.bursts;
82
+ this._lastBurst = undefined;
83
+ this._nextRequest = new Date().getTime();
84
+ }
85
+
86
+
87
+ // ==== RTMUser PROPERTY GETTERS & SETTERS ==== //
88
+
89
+ /**
90
+ * RTM User ID
91
+ * @type {number}
92
+ */
93
+ get id() {
94
+ return this._id;
95
+ }
96
+
97
+ /**
98
+ * RTM User Username
99
+ * @type {string}
100
+ */
101
+ get username() {
102
+ return this._username;
103
+ }
104
+
105
+ /**
106
+ * RTM User fullname
107
+ * @type {string}
108
+ */
109
+ get fullname() {
110
+ return this._fullname;
111
+ }
112
+
113
+ /**
114
+ * RTM User Auth Token
115
+ * @type {string}
116
+ */
117
+ get authToken() {
118
+ return this._authToken;
119
+ }
120
+
121
+ /**
122
+ * Set the RTM User Auth Token
123
+ * @param {string} token
124
+ * @private
125
+ */
126
+ set authToken(token) {
127
+ this._authToken = token;
128
+ }
129
+
130
+ /**
131
+ * The {@link RTMClient} that authorized this User
132
+ * @type {RTMClient}
133
+ */
134
+ get client() {
135
+ if ( !this._client ) {
136
+ throw "User does not have Client specified";
137
+ }
138
+ return this._client;
139
+ }
140
+
141
+ /**
142
+ * Set the Client that authorized this User
143
+ * @param {RTMClient} client
144
+ * @private
145
+ */
146
+ set client(client) {
147
+ this._client = client;
148
+ }
149
+
150
+ /**
151
+ * The RTM Timeline for this User
152
+ * @type {number}
153
+ */
154
+ get timeline() {
155
+ if ( !this._timeline ) {
156
+ throw "User does not have a valid timeline set";
157
+ }
158
+ return this._timeline;
159
+ }
160
+
161
+ /**
162
+ * Set the RTM Timeline for this User
163
+ * @param {number} timeline
164
+ * @private
165
+ */
166
+ set timeline(timeline) {
167
+ this._timeline = parseFloat(timeline);
168
+ }
169
+
170
+
171
+ // ==== REQUEST RATE FUNCTIONS ==== //
172
+
173
+ /**
174
+ * Time (ms) to wait to make an API Request
175
+ * @returns {number}
176
+ */
177
+ get requestTimeout() {
178
+ let now = new Date().getTime();
179
+ let next = this._nextRequest;
180
+ let wait = next - now < 0 ? 0 : next - now;
181
+
182
+ // Default Timeout
183
+ let timeout = config.api.rate.timeout;
184
+
185
+ // Can we start bursting again?
186
+ if ( this._lastBurst !== undefined ) {
187
+ let burstDelta = now - this._lastBurst;
188
+ if ( burstDelta > config.api.rate.burstWait ) {
189
+ this._burstsRemaining = config.api.rate.bursts;
190
+ }
191
+ }
192
+
193
+ // Do we have bursts remaining?
194
+ if ( this._burstsRemaining > 0 ) {
195
+ this._burstsRemaining--;
196
+ timeout = config.api.rate.burstTimeout;
197
+ if ( this._burstsRemaining === 0 ) {
198
+ this._lastBurst = now;
199
+ }
200
+ }
201
+
202
+ // Set Next Request Time
203
+ this._nextRequest = now + wait + timeout;
204
+
205
+ // Return wait time
206
+ return wait;
207
+ }
208
+
209
+
210
+ // ==== API HELPER FUNCTIONS ==== //
211
+
212
+ /**
213
+ * Make the specified RTM API call.
214
+ *
215
+ * The `method` should be the name of the RTM API method. Any necessary
216
+ * parameters should be provided with `params` as an object with the properties
217
+ * of the object as the parameters' key/value pairs.
218
+ *
219
+ * This function will automatically add the User's auth token to the request.
220
+ * @param {string} method RTM API Method
221
+ * @param {object} [params] RTM API Method Parameters
222
+ * @param {function} callback Callback function(err, resp)
223
+ * @param {RTMError} callback.err RTM Error Response, if encountered
224
+ * @param {RTMSuccess} callback.resp The parsed RTM API Response, if successful
225
+ */
226
+ get(method, params, callback) {
227
+ require('../utils/get.js')(method, params, this, this.client, callback);
228
+ }
229
+
230
+ /**
231
+ * Verify the Auth Token of this RTM User
232
+ * @param {function} callback Callback function(err, verified)
233
+ * @param {RTMError} callback.err RTM Error, if encountered (excluding a `Login failed / Invalid auth token` error)
234
+ * @param {boolean} callback.verified `true` if the User's auth token was successfully verified or `false` if
235
+ * a `Login failed / Invalid auth token` error was encountered
236
+ */
237
+ verifyAuthToken(callback) {
238
+ require('../utils/auth.js').verifyAuthToken(this.authToken, this.client, callback);
239
+ }
240
+
241
+ /**
242
+ * Clear the Task Index Cache for this RTM User
243
+ */
244
+ clearTaskIndexCache() {
245
+ require('../utils/taskIds.js').clear(this.id);
246
+ }
247
+
248
+
249
+ /**
250
+ * RTM List related functions:
251
+ * - {@link RTMUser~lists/get|get}
252
+ * - {@link RTMUser~lists/add|add}
253
+ * - {@link RTMUser~lists/rename|rename}
254
+ * - {@link RTMUser~lists/remove|remove}
255
+ * @returns {{get: function, add: function, remove: function, rename: function}}
256
+ */
257
+ get lists() {
258
+ return require('./lists.js')(this);
259
+ }
260
+
261
+
262
+ /**
263
+ * RTM Task related functions:
264
+ * - {@link RTMUser~tasks/get|get}
265
+ * - {@link RTMUser~tasks/add|add}
266
+ * - {@link RTMUser~tasks/remove|remove}
267
+ * - {@link RTMUser~tasks/complete|complete}
268
+ * - {@link RTMUser~tasks/uncomplete|uncomplete}
269
+ * - {@link RTMUser~tasks/addNotes|addNotes}
270
+ * - {@link RTMUser~tasks/addTags|addTags}
271
+ * - {@link RTMUser~tasks/removeTags|removeTags}
272
+ * - {@link RTMUser~tasks/priority|priority}
273
+ * - {@link RTMUser~tasks/decreasePriority|decreasePriority}
274
+ * - {@link RTMUser~tasks/increasePriority|increasePriority}
275
+ * - {@link RTMUser~tasks/move|move}
276
+ * - {@link RTMUser~tasks/setDueDate|setDueDate}
277
+ * - {@link RTMUser~tasks/postpone|postpone}
278
+ * - {@link RTMUser~tasks/setName|setName}
279
+ * @returns {{get: function, add:function, remove: function, complete: function, uncomplete: function, addNotes: function, addTags: function, removeTags: function, priority: function, decreasePriority: function, increasePriority: function, move: function, setDueDate: function, postpone: function, setName: function}}
280
+ */
281
+ get tasks() {
282
+ return require('./tasks.js')(this);
283
+ }
284
+
285
+ }
286
+
287
+
288
+ module.exports = RTMUser;
@@ -0,0 +1,150 @@
1
+ 'use strict';
2
+
3
+ const errors = require('../response/error.js');
4
+ const _lists = require('../list/helper.js');
5
+
6
+
7
+ /**
8
+ * This module returns the RTM list-related functions for the RTMUser
9
+ * @param {RTMUser} user RTM User instance
10
+ * @returns {{get: function, update: function, add: function, remove: function, rename: function, archive: function}}
11
+ * @private
12
+ */
13
+ module.exports = function(user) {
14
+ let rtn = {};
15
+
16
+ /**
17
+ * Get the list of RTM Lists for this User from the API Server
18
+ * @param {function} callback Callback function(err, lists)
19
+ * @param {RTMError} callback.err RTM API Error Response, if encountered
20
+ * @param {RTMList[]} callback.lists List of User's RTM Lists
21
+ * @function RTMUser~lists/get
22
+ */
23
+ rtn.get = function(callback) {
24
+ _lists.get(user, callback);
25
+ };
26
+
27
+ /**
28
+ * Add a new RTM List for this User
29
+ * @param {string} name Name of the new RTM List
30
+ * @param {string} [filter] Smart List Filter
31
+ * @param {function} callback Callback function(err, lists)
32
+ * @param {RTMError} callback.err RTM API Error Response, if encountered
33
+ * @function RTMUser~lists/add
34
+ */
35
+ rtn.add = function(name, filter, callback) {
36
+ if ( callback === undefined && typeof filter === 'function' ) {
37
+ callback = filter;
38
+ filter = undefined;
39
+ }
40
+ _lists.add(name, filter, user, callback);
41
+ };
42
+
43
+ /**
44
+ * Remove the specified RTM List for this User
45
+ * @param {string} name RTM List Name
46
+ * @param {function} callback Callback function(err)
47
+ * @param {RTMError} callback.err RTM API Error Response, if encountered
48
+ * @function RTMUser~lists/remove
49
+ */
50
+ rtn.remove = function(name, callback) {
51
+
52
+ // Get the User's Lists
53
+ _lists.get(user, function(err, lists) {
54
+ if ( err ) {
55
+ return callback(err);
56
+ }
57
+
58
+ // Find the matching list IDs
59
+ let ids = [];
60
+ for ( let i = 0; i < lists.length; i++ ) {
61
+ if ( lists[i].name.toLowerCase() === name.toLowerCase() ) {
62
+ ids.push(lists[i].id);
63
+ }
64
+ }
65
+
66
+ // Remove the matching List
67
+ if ( ids.length === 1 ) {
68
+ return _lists.remove(ids[0], user, callback);
69
+ }
70
+ else {
71
+ return callback(errors.referenceError());
72
+ }
73
+
74
+ });
75
+
76
+ };
77
+
78
+ /**
79
+ * Rename the specified RTM List for this User
80
+ * @param {string} oldName Old RTM List Name
81
+ * @param {string} newName New RTM List name
82
+ * @param {function} callback Callback function(err)
83
+ * @param {RTMError} callback.err RTM API Error Response, if encountered
84
+ * @function RTMUser~lists/rename
85
+ */
86
+ rtn.rename = function(oldName, newName, callback) {
87
+
88
+ // Get the User's Lists
89
+ _lists.get(user, function(err, lists) {
90
+ if ( err ) {
91
+ return callback(err);
92
+ }
93
+
94
+ // Find the matching list IDs
95
+ let ids = [];
96
+ for ( let i = 0; i < lists.length; i++ ) {
97
+ if ( lists[i].name.toLowerCase() === oldName.toLowerCase() ) {
98
+ ids.push(lists[i].id);
99
+ }
100
+ }
101
+
102
+ // Rename the matching List
103
+ if ( ids.length === 1 ) {
104
+ return _lists.rename(ids[0], newName, user, callback);
105
+ }
106
+ else {
107
+ return callback(errors.referenceError());
108
+ }
109
+
110
+ });
111
+
112
+ };
113
+
114
+ /**
115
+ * Archive the specified RTM List for this User
116
+ * @param {string} name RTM List Name
117
+ * @param {function} callback Callback function(err)
118
+ * @param {RTMError} callback.err RTM API Error Response, if encountered
119
+ * @function RTMUser~lists/archive
120
+ */
121
+ rtn.archive = function(name, callback) {
122
+
123
+ // Get the User's Lists
124
+ _lists.get(user, function(err, lists) {
125
+ if ( err ) {
126
+ return callback(err);
127
+ }
128
+
129
+ // Find the matching list IDs
130
+ let ids = [];
131
+ for ( let i = 0; i < lists.length; i++ ) {
132
+ if ( lists[i].name.toLowerCase() === name.toLowerCase() ) {
133
+ ids.push(lists[i].id);
134
+ }
135
+ }
136
+
137
+ // Remove the matching List
138
+ if ( ids.length === 1 ) {
139
+ return _lists.archive(ids[0], user, callback);
140
+ }
141
+ else {
142
+ return callback(errors.referenceError());
143
+ }
144
+
145
+ });
146
+
147
+ };
148
+
149
+ return rtn;
150
+ };