@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,188 @@
1
+ 'use strict';
2
+
3
+ const RTMUser = require('../user/index.js');
4
+ const sign = require('./sign.js');
5
+
6
+ // API Configuration Properties
7
+ const config = require('../../config');
8
+ const scheme = config.api.scheme;
9
+ const base = config.api.url.auth;
10
+
11
+
12
+ /**
13
+ * Get Auth URL
14
+ * @param {RTMClient} client RTM Client making the request
15
+ * @param {function} callback Callback function(err, authUrl, frob)
16
+ * @private
17
+ */
18
+ function getAuthUrl(client, callback) {
19
+
20
+ // Get a frob from the API Server
21
+ _getFrob(client, function(err, frob) {
22
+ if ( err ) {
23
+ return callback(err);
24
+ }
25
+
26
+ // Build the AUTH URL
27
+ let authUrl = _buildAuthURL(frob, client);
28
+
29
+ // Return the Auth URL
30
+ return callback(null, authUrl, frob);
31
+
32
+ });
33
+
34
+ }
35
+
36
+
37
+ /**
38
+ * Get Auth Token
39
+ * @param {string} frob RTM Frob, from {@link getAuthUrl}
40
+ * @param {RTMClient} client RTM Client making the request
41
+ * @param {function} callback Callback function(err, user)
42
+ * @private
43
+ */
44
+ function getAuthToken(frob, client, callback) {
45
+
46
+ // Set request parameters
47
+ let params = {
48
+ frob: frob
49
+ };
50
+
51
+ // Get Auth Token
52
+ client.get('rtm.auth.getToken', params, function(err, resp) {
53
+ if ( err ) {
54
+ return callback(err);
55
+ }
56
+
57
+ // Create new RTMUser
58
+ let user = client.user.create(
59
+ resp.auth.user.id,
60
+ resp.auth.user.username,
61
+ resp.auth.user.fullname,
62
+ resp.auth.token
63
+ );
64
+
65
+ // Get a new Timeline for the User
66
+ user.get('rtm.timelines.create', function(err, resp) {
67
+ if ( err ) {
68
+ return callback(err);
69
+ }
70
+
71
+ // Set the User's Timeline
72
+ user.timeline = resp.timeline;
73
+
74
+ return callback(null, user);
75
+ });
76
+
77
+ });
78
+
79
+ }
80
+
81
+
82
+ /**
83
+ * Verify Auth Token
84
+ * @param {string|RTMUser} token RTM User Authentication Token or RTM User
85
+ * @param {RTMClient} client RTM Client making the request
86
+ * @param {function} callback Callback function(err, verified)
87
+ * @private
88
+ */
89
+ function verifyAuthToken(token, client, callback) {
90
+
91
+ // Get token from RTMUser
92
+ if ( typeof token === 'object' ) {
93
+ if ( token.constructor.name === 'RTMUser' ) {
94
+ token = token.authToken;
95
+ }
96
+ }
97
+
98
+ // Set request parameters
99
+ let params = {
100
+ auth_token: token
101
+ };
102
+
103
+ // Verify Token
104
+ client.get('rtm.auth.checkToken', params, function(err) {
105
+ if ( err) {
106
+ if ( err.code === 98 ) {
107
+ return callback(null, false);
108
+ }
109
+ else {
110
+ return callback(err);
111
+ }
112
+ }
113
+ else {
114
+ return callback(null, true);
115
+ }
116
+ });
117
+
118
+ }
119
+
120
+
121
+
122
+ // ==== HELPER FUNCTIONS ==== //
123
+
124
+
125
+ /**
126
+ * Get RTM Authentication Frob
127
+ * @param {RTMClient} client RTM Client making the request
128
+ * @param {function} callback Callback function(err, frob)
129
+ * @private
130
+ */
131
+ function _getFrob(client, callback) {
132
+
133
+ client.get('rtm.auth.getFrob', function(err, resp) {
134
+ if ( err ) {
135
+ return callback(err);
136
+ }
137
+ return callback(null, resp.frob);
138
+ });
139
+
140
+ }
141
+
142
+ /**
143
+ * Build the Authentication URL to send to the User
144
+ * @param {string} frob RTM Authentication Frob
145
+ * @param {RTMClient} client RTM Client making the request
146
+ * @returns {string} RTM Auth URL
147
+ * @private
148
+ */
149
+ function _buildAuthURL(frob, client) {
150
+
151
+ // Build Request Parameters
152
+ let params = {};
153
+ params.api_key = client.key;
154
+ params.perms = client.perms;
155
+ params.frob = frob;
156
+ params.api_sig = sign(params, client);
157
+
158
+ // Form Query
159
+ let query = _formQuery(params);
160
+
161
+ // Build Auth URL
162
+ return scheme + '://' + base + '?' + query;
163
+
164
+ }
165
+
166
+ /**
167
+ * Generate a URI Encoded query string from the parameters set of key/value pairs
168
+ * @param {object} params Object containing the key/value parameters
169
+ * @returns {string} URL Encoded query string
170
+ * @private
171
+ */
172
+ function _formQuery(params) {
173
+ let parts = [];
174
+ for ( let key in params ) {
175
+ if ( params.hasOwnProperty(key) ) {
176
+ parts.push(encodeURIComponent(key) + "=" + encodeURIComponent(params[key]));
177
+ }
178
+ }
179
+ return parts.join("&");
180
+ }
181
+
182
+
183
+
184
+ module.exports = {
185
+ getAuthUrl: getAuthUrl,
186
+ getAuthToken: getAuthToken,
187
+ verifyAuthToken: verifyAuthToken
188
+ };
@@ -0,0 +1,67 @@
1
+ const fetch = require('node-fetch');
2
+ const sign = require('../utils/sign')
3
+
4
+
5
+ // API Configuration Properties
6
+ const config = require('../../config');
7
+ const scheme = config.api.scheme;
8
+ const base = config.api.url.base;
9
+ const format = config.api.format;
10
+ const version = config.api.version;
11
+
12
+
13
+ function buildUrl(user,filter) {
14
+ let query = {
15
+ filter: filter ,
16
+ auth_token: user._authToken,
17
+ method: 'rtm.tasks.getList',
18
+ api_key: user._client._apiKey,
19
+ v: version,
20
+ format
21
+ }
22
+
23
+ let apiSig=sign(query,{secret:user._client._apiSecret})
24
+
25
+ let url = `${scheme}://${base}?${formQuery(query)}&api_sig=${apiSig}`
26
+ return url
27
+ }
28
+
29
+
30
+
31
+
32
+ /**
33
+ * Generate a URI Encoded query string from the parameters set of
34
+ * key/value pairs
35
+ * @param {object} params Object containing the key/value parameters
36
+ * @returns {string} URL Encoded query string
37
+ */
38
+ function formQuery(params) {
39
+ let parts = [];
40
+ for ( let key in params ) {
41
+ if ( params.hasOwnProperty(key) ) {
42
+ parts.push(encodeURIComponent(key) + "=" + encodeURIComponent(params[key]));
43
+ }
44
+ }
45
+ return parts.join("&");
46
+ }
47
+
48
+ /**
49
+ *
50
+ * @param {string} url fully formed url to call
51
+ * @returns JSON response
52
+ */
53
+ async function callAPI(url) {
54
+ try {
55
+ const response = await fetch(url);
56
+ if (await response.ok) {
57
+ return await response.json();
58
+ } else {
59
+ // TODO improve this error message
60
+ console.error('There was an error');
61
+ }
62
+ } catch (error) {
63
+ console.error(error)
64
+ }
65
+ }
66
+
67
+ module.exports = {callAPI,formQuery, buildUrl}
@@ -0,0 +1,247 @@
1
+ 'use strict';
2
+
3
+ const URL = require('url');
4
+
5
+ const parse = require('../response/parse.js');
6
+ const error = require('../response/error.js');
7
+ const RTMClient = require('../client/index.js');
8
+ const RTMUser = require('../user/index.js');
9
+ const sign = require('./sign.js');
10
+
11
+ // API Configuration Properties
12
+ const config = require('../../config');
13
+ const scheme = config.api.scheme;
14
+ const base = config.api.url.base;
15
+ const format = config.api.format;
16
+ const version = config.api.version;
17
+
18
+
19
+ /**
20
+ * Make the specified RTM API call.
21
+ * @param {string} method RTM API Method
22
+ * @param {object} [params={}] RTM Method Parameters (as an object with key/value pairs)
23
+ * @param {RTMUser} [user=undefined] The RTM User making the request
24
+ * @param {RTMClient} [client=undefined] The RTM Client making the request
25
+ * @param {function} callback Callback function(err, resp)
26
+ * @private
27
+ */
28
+ function get(method, params, user, client, callback) {
29
+
30
+ // Parse the given arguments
31
+ let args = _parseGetArgs(method, params, user, client, callback);
32
+
33
+ // Build the Request URL
34
+ let requestUrl = _buildRequestUrl(args.method, args.params, args.user, args.client);
35
+
36
+ // Parse the URL
37
+ let url = URL.parse(requestUrl);
38
+
39
+ // Build the request options
40
+ let options = {
41
+ host: url.host,
42
+ path: url.path,
43
+ method: 'GET'
44
+ };
45
+
46
+ // Determine timeout
47
+ let timeout = args.user ? args.user.requestTimeout : 0;
48
+
49
+ // Make the Request
50
+ setTimeout(function() {
51
+ _makeRequest(scheme, options, args.callback);
52
+ }, timeout);
53
+
54
+ }
55
+
56
+ /**
57
+ * Perform the API Request
58
+ * @param {string} scheme HTTP(s) Scheme (http or https)
59
+ * @param {object} options Request Options (host, path, method)
60
+ * @param {function} callback Final callback function(err, resp)
61
+ * @private
62
+ */
63
+ function _makeRequest(scheme, options, callback) {
64
+
65
+ // Require the http(s) module
66
+ let http = undefined;
67
+ if ( scheme === "https" ) {
68
+ http = require('https');
69
+ }
70
+ else {
71
+ http = require('http');
72
+ }
73
+
74
+ // Make the Request
75
+ let req = http.request(options, function(response) {
76
+ let resp = '';
77
+ response.on('data', function(chunk) {
78
+ resp += chunk;
79
+ });
80
+ response.on('end', function() {
81
+ // Server Errors
82
+ if ( response.statusCode === 503 ) {
83
+ return callback(error.rateLimitError());
84
+ }
85
+ else if ( response.statusCode >= 500 && response.statusCode <= 599 ) {
86
+ return callback(error.serverError());
87
+ }
88
+
89
+ // Parse the API Response
90
+ let parsed = parse(resp);
91
+
92
+ // Return parsed result as error or success
93
+ if ( !parsed.isOk ) {
94
+ return callback(parsed)
95
+ }
96
+ else {
97
+ return callback(null, parsed);
98
+ }
99
+ });
100
+ });
101
+ req.on('error', function() {
102
+ callback(error.networkError());
103
+ });
104
+ req.end();
105
+
106
+ }
107
+
108
+
109
+ /**
110
+ * Parse the arguments given to the get() function
111
+ * @returns {{method: *, params: *, user: *, client: *, callback: *}}
112
+ * @private
113
+ */
114
+ function _parseGetArgs() {
115
+
116
+ // Parsed arguments to return
117
+ let rtn = {};
118
+ rtn.params = {};
119
+
120
+ // Parse each of the arguments
121
+ for ( let key in arguments ) {
122
+ if ( arguments.hasOwnProperty(key) ) {
123
+ let arg = arguments[key];
124
+ if ( typeof arg === 'string' ) {
125
+ rtn.method = arg;
126
+ }
127
+ else if ( typeof arg === 'function' ) {
128
+ rtn.callback = arg;
129
+ }
130
+ else if ( typeof arg === 'object' ) {
131
+ if ( arg.constructor.name === 'RTMClient' ) {
132
+ rtn.client = arg;
133
+ }
134
+ else if ( arg.constructor.name === 'RTMUser' ) {
135
+ rtn.user = arg;
136
+ }
137
+ else {
138
+ rtn.params = arg;
139
+ }
140
+ }
141
+ }
142
+ }
143
+
144
+ // Return the parsed arguments
145
+ return rtn;
146
+
147
+ }
148
+
149
+
150
+
151
+ /**
152
+ * Build the API Request URL.
153
+ *
154
+ * This uses the properties in the `rtm.json` configuration file for the
155
+ * URL scheme, base URL, format and version. It will create a URL encoded
156
+ * query string from the passed parameters and add a signature to the request.
157
+ * @param {string} method RTM API Method
158
+ * @param {Object} [params={}] Request Parameters
159
+ * @param {RTMUser} [user=undefined] The RTM User making the request
160
+ * @param {RTMClient} [client=undefined] The RTM Client making the request
161
+ * @returns {string} Signed Request URL
162
+ * @private
163
+ */
164
+ function _buildRequestUrl(method, params, user, client) {
165
+
166
+ // Parse the given arguments
167
+ let args = _parseBuildRequestUrlArgs(method, params, user, client);
168
+
169
+ // Add User Auth Token, if provided
170
+ if ( args.user && args.user.authToken ) {
171
+ args.params.auth_token = args.user.authToken;
172
+ }
173
+
174
+ // Add method, api key, version and format to params
175
+ args.params.method = args.method;
176
+ args.params.api_key = args.client.key;
177
+ args.params.v = version;
178
+ args.params.format = format;
179
+ args.params.api_sig = sign(args.params, client);
180
+
181
+ // Generate query string from params
182
+ let query = _formQuery(args.params);
183
+
184
+ // Build the API request URL
185
+ return scheme + '://' + base + "?" + query;
186
+
187
+ }
188
+
189
+
190
+ /**
191
+ * Parse the arguments given to the _buildRequestUrl function
192
+ * @returns {{method: *, params: *, user: *, client: *}}
193
+ * @private
194
+ */
195
+ function _parseBuildRequestUrlArgs() {
196
+
197
+ // Parsed arguments to return
198
+ let rtn = {};
199
+ rtn.params = {};
200
+
201
+ // Parse each of the arguments
202
+ for ( let key in arguments ) {
203
+ if ( arguments.hasOwnProperty(key) ) {
204
+ let arg = arguments[key];
205
+ if ( typeof arg === 'string' ) {
206
+ rtn.method = arg;
207
+ }
208
+ else if ( typeof arg === 'object' ) {
209
+ if ( arg.constructor.name === 'RTMClient' ) {
210
+ rtn.client = arg;
211
+ }
212
+ else if ( arg.constructor.name === 'RTMUser' ) {
213
+ rtn.user = arg;
214
+ }
215
+ else {
216
+ rtn.params = arg;
217
+ }
218
+ }
219
+ }
220
+ }
221
+
222
+ // Return the parsed arguments
223
+ return rtn;
224
+
225
+ }
226
+
227
+
228
+ /**
229
+ * Generate a URI Encoded query string from the parameters set of
230
+ * key/value pairs
231
+ * @param {object} params Object containing the key/value parameters
232
+ * @returns {string} URL Encoded query string
233
+ * @private
234
+ */
235
+ function _formQuery(params) {
236
+ let parts = [];
237
+ for ( let key in params ) {
238
+ if ( params.hasOwnProperty(key) ) {
239
+ parts.push(encodeURIComponent(key) + "=" + encodeURIComponent(params[key]));
240
+ }
241
+ }
242
+ return parts.join("&");
243
+ }
244
+
245
+
246
+
247
+ module.exports = get;
@@ -0,0 +1,93 @@
1
+ 'use strict';
2
+
3
+ const crypto = require('crypto');
4
+
5
+
6
+ /**
7
+ * ### RTM API Signature
8
+ *
9
+ * This module provides the `sign()` function which can be used to sign the RTM
10
+ * API requests for a given set of parameters and `RTMClient`.
11
+ *
12
+ * The parameters should be given as a single object with a set of
13
+ * key/value pairs as the object's properties. For example, the
14
+ * parameters: `yxz=foo feg=bar abc=baz` should be given as on object
15
+ * as:
16
+ * ```
17
+ * {
18
+ * yxz: "foo",
19
+ * feg: "bar",
20
+ * abc: "baz"
21
+ * }
22
+ * ```
23
+ * @module utils/sign
24
+ * @private
25
+ */
26
+
27
+
28
+ /**
29
+ * Sign the RTM API Request
30
+ * @param {object} params Request parameters
31
+ * @param {RTMClient} client The RTM Client making the request
32
+ * @return {string} RTM API Signature
33
+ * @private
34
+ */
35
+ function sign(params, client) {
36
+
37
+ // Sort the properties of the parameters
38
+ params = _sort(params);
39
+
40
+ // Concatenate the parameter's key/value pairs
41
+ let cat = _concat(params);
42
+
43
+ // Add to Shared Secret
44
+ let toHash = client.secret + cat;
45
+
46
+ // Return the Hash
47
+ return crypto.createHash('md5').update(toHash).digest("hex");
48
+
49
+ }
50
+
51
+
52
+ /**
53
+ * Sort the properties of the Object alphabetically
54
+ * @param {object} object Object to sort
55
+ * @returns {object} Object with sorted properties
56
+ * @private
57
+ */
58
+ function _sort(object) {
59
+ let keys = [];
60
+ for ( let key in object ) {
61
+ if ( object.hasOwnProperty(key) ) {
62
+ keys.push(key);
63
+ }
64
+ }
65
+ keys.sort();
66
+
67
+ let rtn = {};
68
+ for ( let i = 0; i < keys.length; i++ ) {
69
+ let key = keys[i];
70
+ rtn[key] = object[key];
71
+ }
72
+ return rtn;
73
+ }
74
+
75
+
76
+ /**
77
+ * Concatenate the object's key/value pairs into a single string
78
+ * @param {object} object The object whose properties will be concatenated
79
+ * @returns {string} Concatenated key/value pairs
80
+ * @private
81
+ */
82
+ function _concat(object) {
83
+ let rtn = "";
84
+ for ( let key in object ) {
85
+ if ( object.hasOwnProperty(key) ) {
86
+ rtn += key + object[key];
87
+ }
88
+ }
89
+ return rtn;
90
+ }
91
+
92
+
93
+ module.exports = sign;