box-node-sdk 1.35.0 → 1.37.2

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 (126) hide show
  1. package/CHANGELOG.md +37 -2
  2. package/README.md +1 -1
  3. package/lib/api-request-manager.d.ts +38 -0
  4. package/lib/api-request-manager.js +48 -55
  5. package/lib/api-request-manager.js.map +1 -0
  6. package/lib/api-request.d.ts +141 -0
  7. package/lib/api-request.js +202 -281
  8. package/lib/api-request.js.map +1 -0
  9. package/lib/box-client.d.ts +269 -0
  10. package/lib/box-client.js +551 -713
  11. package/lib/box-client.js.map +1 -0
  12. package/lib/box-node-sdk.d.ts +216 -0
  13. package/lib/box-node-sdk.js +317 -352
  14. package/lib/box-node-sdk.js.map +1 -0
  15. package/lib/chunked-uploader.d.ts +129 -0
  16. package/lib/chunked-uploader.js +287 -358
  17. package/lib/chunked-uploader.js.map +1 -0
  18. package/lib/enterprise-event-stream.d.ts +82 -0
  19. package/lib/enterprise-event-stream.js +189 -203
  20. package/lib/enterprise-event-stream.js.map +1 -0
  21. package/lib/event-stream.d.ts +92 -0
  22. package/lib/event-stream.js +274 -302
  23. package/lib/event-stream.js.map +1 -0
  24. package/lib/managers/collaboration-allowlist.d.ts +137 -0
  25. package/lib/managers/collaboration-allowlist.js +200 -0
  26. package/lib/managers/collaboration-allowlist.js.map +1 -0
  27. package/lib/managers/collaboration-whitelist.d.ts +3 -0
  28. package/lib/managers/collaboration-whitelist.js +8 -222
  29. package/lib/managers/collaboration-whitelist.js.map +1 -0
  30. package/lib/managers/collaborations.d.ts +166 -0
  31. package/lib/managers/collaborations.js +225 -258
  32. package/lib/managers/collaborations.js.map +1 -0
  33. package/lib/managers/collections.d.ts +42 -0
  34. package/lib/managers/collections.js +45 -50
  35. package/lib/managers/collections.js.map +1 -0
  36. package/lib/managers/comments.d.ts +103 -0
  37. package/lib/managers/comments.js +158 -173
  38. package/lib/managers/comments.js.map +1 -0
  39. package/lib/managers/device-pins.d.ts +52 -0
  40. package/lib/managers/device-pins.js +75 -88
  41. package/lib/managers/device-pins.js.map +1 -0
  42. package/lib/managers/enterprise.d.ts +162 -0
  43. package/lib/managers/enterprise.js +168 -199
  44. package/lib/managers/enterprise.js.map +1 -0
  45. package/lib/managers/events.d.ts +177 -0
  46. package/lib/managers/events.js +230 -254
  47. package/lib/managers/events.js.map +1 -0
  48. package/lib/managers/files.d.ts +772 -0
  49. package/lib/managers/files.js +1400 -1602
  50. package/lib/managers/files.js.map +1 -0
  51. package/lib/managers/folders.d.ts +347 -0
  52. package/lib/managers/folders.js +551 -567
  53. package/lib/managers/folders.js.map +1 -0
  54. package/lib/managers/groups.d.ts +202 -0
  55. package/lib/managers/groups.js +238 -287
  56. package/lib/managers/groups.js.map +1 -0
  57. package/lib/managers/legal-hold-policies.d.ts +190 -0
  58. package/lib/managers/legal-hold-policies.js +228 -272
  59. package/lib/managers/legal-hold-policies.js.map +1 -0
  60. package/lib/managers/metadata.d.ts +228 -0
  61. package/lib/managers/metadata.js +265 -328
  62. package/lib/managers/metadata.js.map +1 -0
  63. package/lib/managers/recent-items.d.ts +38 -0
  64. package/lib/managers/recent-items.js +32 -39
  65. package/lib/managers/recent-items.js.map +1 -0
  66. package/lib/managers/retention-policies.d.ts +213 -0
  67. package/lib/managers/retention-policies.js +235 -281
  68. package/lib/managers/retention-policies.js.map +1 -0
  69. package/lib/managers/search.d.ts +82 -0
  70. package/lib/managers/search.js +68 -88
  71. package/lib/managers/search.js.map +1 -0
  72. package/lib/managers/shared-items.d.ts +33 -0
  73. package/lib/managers/shared-items.js +54 -62
  74. package/lib/managers/shared-items.js.map +1 -0
  75. package/lib/managers/storage-policies.d.ts +86 -0
  76. package/lib/managers/storage-policies.js +108 -142
  77. package/lib/managers/storage-policies.js.map +1 -0
  78. package/lib/managers/tasks.d.ts +161 -0
  79. package/lib/managers/tasks.js +219 -260
  80. package/lib/managers/tasks.js.map +1 -0
  81. package/lib/managers/terms-of-service.d.ts +161 -0
  82. package/lib/managers/terms-of-service.js +250 -273
  83. package/lib/managers/terms-of-service.js.map +1 -0
  84. package/lib/managers/trash.d.ts +30 -0
  85. package/lib/managers/trash.js +30 -41
  86. package/lib/managers/trash.js.map +1 -0
  87. package/lib/managers/users.d.ts +131 -0
  88. package/lib/managers/users.js +160 -203
  89. package/lib/managers/users.js.map +1 -0
  90. package/lib/managers/web-links.d.ts +127 -0
  91. package/lib/managers/web-links.js +183 -209
  92. package/lib/managers/web-links.js.map +1 -0
  93. package/lib/managers/webhooks.d.ts +166 -0
  94. package/lib/managers/webhooks.js +312 -305
  95. package/lib/managers/webhooks.js.map +1 -0
  96. package/lib/sessions/anonymous-session.d.ts +69 -0
  97. package/lib/sessions/anonymous-session.js +88 -102
  98. package/lib/sessions/anonymous-session.js.map +1 -0
  99. package/lib/sessions/app-auth-session.d.ts +92 -0
  100. package/lib/sessions/app-auth-session.js +140 -160
  101. package/lib/sessions/app-auth-session.js.map +1 -0
  102. package/lib/sessions/basic-session.d.ts +56 -0
  103. package/lib/sessions/basic-session.js +40 -50
  104. package/lib/sessions/basic-session.js.map +1 -0
  105. package/lib/sessions/persistent-session.d.ts +96 -0
  106. package/lib/sessions/persistent-session.js +191 -211
  107. package/lib/sessions/persistent-session.js.map +1 -0
  108. package/lib/token-manager.d.ts +191 -0
  109. package/lib/token-manager.js +390 -465
  110. package/lib/token-manager.js.map +1 -0
  111. package/lib/util/config.d.ts +86 -0
  112. package/lib/util/config.js +124 -152
  113. package/lib/util/config.js.map +1 -0
  114. package/lib/util/errors.d.ts +50 -0
  115. package/lib/util/errors.js +134 -145
  116. package/lib/util/errors.js.map +1 -0
  117. package/lib/util/exponential-backoff.d.ts +11 -0
  118. package/lib/util/exponential-backoff.js +10 -22
  119. package/lib/util/exponential-backoff.js.map +1 -0
  120. package/lib/util/paging-iterator.d.ts +53 -0
  121. package/lib/util/paging-iterator.js +202 -218
  122. package/lib/util/paging-iterator.js.map +1 -0
  123. package/lib/util/url-path.d.ts +16 -0
  124. package/lib/util/url-path.js +20 -35
  125. package/lib/util/url-path.js.map +1 -0
  126. package/package.json +24 -9
@@ -1,23 +1,20 @@
1
+ "use strict";
1
2
  /**
2
3
  * @fileoverview A Persistent Box API Session.
3
4
  */
4
-
5
- 'use strict';
6
-
7
-
5
+ var __importDefault = (this && this.__importDefault) || function (mod) {
6
+ return (mod && mod.__esModule) ? mod : { "default": mod };
7
+ };
8
8
  // ------------------------------------------------------------------------------
9
9
  // Requirements
10
10
  // ------------------------------------------------------------------------------
11
- const assert = require('assert'),
12
- Promise = require('bluebird'),
13
- errors = require('../util/errors'),
14
- httpStatusCodes = require('http-status');
15
-
16
-
11
+ var assert_1 = __importDefault(require("assert"));
12
+ var bluebird_1 = require("bluebird");
13
+ var http_status_1 = __importDefault(require("http-status"));
14
+ var errors_1 = __importDefault(require("../util/errors"));
17
15
  // ------------------------------------------------------------------------------
18
16
  // Private
19
17
  // ------------------------------------------------------------------------------
20
-
21
18
  /**
22
19
  * Validate that an object is a valid TokenInfo object
23
20
  *
@@ -27,13 +24,12 @@ const assert = require('assert'),
27
24
  * @private
28
25
  */
29
26
  function isObjectValidTokenInfo(obj) {
30
- return Boolean(obj &&
31
- obj.accessToken &&
32
- obj.refreshToken &&
33
- obj.accessTokenTTLMS &&
34
- obj.acquiredAtMS);
27
+ return Boolean(obj &&
28
+ obj.accessToken &&
29
+ obj.refreshToken &&
30
+ obj.accessTokenTTLMS &&
31
+ obj.acquiredAtMS);
35
32
  }
36
-
37
33
  /**
38
34
  * Validate that an object is a valid TokenStore object
39
35
  *
@@ -43,14 +39,11 @@ function isObjectValidTokenInfo(obj) {
43
39
  * @private
44
40
  */
45
41
  function isObjectValidTokenStore(obj) {
46
- return Boolean(obj && obj.read && obj.write && obj.clear);
42
+ return Boolean(obj && obj.read && obj.write && obj.clear);
47
43
  }
48
-
49
-
50
44
  // ------------------------------------------------------------------------------
51
45
  // Public
52
46
  // ------------------------------------------------------------------------------
53
-
54
47
  /**
55
48
  * A Persistent API Session has the ability to refresh its access token once it becomes expired.
56
49
  * It takes in a full tokenInfo object for authentication. It can detect when its tokens have
@@ -66,194 +59,181 @@ function isObjectValidTokenStore(obj) {
66
59
  * @param {TokenManager} tokenManager The token manager
67
60
  * @constructor
68
61
  */
69
- function PersistentSession(tokenInfo, tokenStore, config, tokenManager) {
70
- this._config = config;
71
- this._tokenManager = tokenManager;
72
-
73
- // Keeps track of if tokens are currently being refreshed
74
- this._refreshPromise = null;
75
-
76
- // Set valid PersistentSession credentials. Throw if expected credentials are invalid or not given.
77
- assert(isObjectValidTokenInfo(tokenInfo), 'tokenInfo is improperly formatted. Properties required: accessToken, refreshToken, accessTokenTTLMS and acquiredAtMS.');
78
- this._setTokenInfo(tokenInfo);
79
-
80
- // If tokenStore was provided, set the persistent data & current store operations
81
- if (tokenStore) {
82
- assert(isObjectValidTokenStore(tokenStore), 'Token store provided but is improperly formatted. Methods required: read(), write(), clear().');
83
- this._tokenStore = Promise.promisifyAll(tokenStore);
84
- }
85
- }
86
-
87
- /**
88
- * Sets all relevant token info for this client.
89
- *
90
- * @param {TokenInfo} tokenInfo A valid TokenInfo object.
91
- * @returns {void}
92
- * @private
93
- */
94
- PersistentSession.prototype._setTokenInfo = function(tokenInfo) {
95
- this._tokenInfo = {
96
- accessToken: tokenInfo.accessToken,
97
- refreshToken: tokenInfo.refreshToken,
98
- accessTokenTTLMS: tokenInfo.accessTokenTTLMS,
99
- acquiredAtMS: tokenInfo.acquiredAtMS
100
- };
101
- };
102
-
103
- /**
104
- * Attempts to refresh tokens for the client.
105
- * Will use the Box refresh token grant to complete the refresh. On refresh failure, we'll
106
- * check the token store for more recently updated tokens and load them if found. Otherwise
107
- * an error will be propagated.
108
- *
109
- * @param {TokenRequestOptions} [options] - Sets optional behavior for the token grant
110
- * @returns {Promise<string>} Promise resolving to the access token
111
- * @private
112
- */
113
- PersistentSession.prototype._refreshTokens = function(options) {
114
- // If not already refreshing, kick off a token refresh request and set a lock so that additional
115
- // client requests don't try as well
116
- if (!this._refreshPromise) {
117
-
118
- this._refreshPromise = this._tokenManager.getTokensRefreshGrant(this._tokenInfo.refreshToken, options)
119
- .catch(err => {
120
- // If we got an error response from Box API, but it was 400 invalid_grant, it indicates we may have just
121
- // made the request with an invalidated refresh token. Since only a max of 2 refresh tokens can be valid
122
- // at any point in time, and a horizontally scaled app could have multiple Node instances running in parallel,
123
- // it is possible to hit cases where too many servers all refresh a user's tokens at once
124
- // and cause this server's token to become invalidated. However, the user should still be alive, but
125
- // we'll need to check the central data store for the latest valid tokens that some other server in the app
126
- // cluster would have received. So, instead pull tokens from the central store and attempt to use them.
127
- if (err.statusCode === httpStatusCodes.BAD_REQUEST && this._tokenStore) {
128
- var invalidGrantError = err;
129
-
130
- // Check the tokenStore to see if tokens have been updated recently. If they have, then another
131
- // instance of the session may have already refreshed the user tokens, which would explain why
132
- // we couldn't refresh.
133
- return this._tokenStore.readAsync()
134
- .catch(e => errors.unwrapAndThrow(e))
135
- .then(storeTokenInfo => {
136
- // if the tokens we got from the central store are the same as the tokens we made the failed request with
137
- // already, then we can be sure that no other servers have valid tokens for this server either.
138
- // Thus, this user truly has an expired refresh token. So, propagate an "Expired Tokens" error.
139
- if (!storeTokenInfo || storeTokenInfo.refreshToken === this._tokenInfo.refreshToken) {
140
- throw errors.buildAuthError(invalidGrantError.response);
141
- }
142
-
143
- // Propagate the fresh tokens that we found in the session
144
- return storeTokenInfo;
145
- });
146
- }
147
-
148
- // Box API returned a permanent error that is not retryable and we can't recover.
149
- // We have no usable tokens for the user and no way to refresh them - propagate a permanent error.
150
- throw err;
151
- })
152
- .then(tokenInfo => {
153
- // Success! We got back a TokenInfo object from the API.
154
- // If we have a token store, we'll write it there now before finishing up the request.
155
- if (this._tokenStore) {
156
- return this._tokenStore.writeAsync(tokenInfo)
157
- .catch(e => errors.unwrapAndThrow(e))
158
- .then(() => tokenInfo);
159
- }
160
-
161
- // If no token store, Set and propagate the access token immediately
162
- return tokenInfo;
163
- })
164
- .then(tokenInfo => {
165
- // Set and propagate the new access token
166
- this._setTokenInfo(tokenInfo);
167
- return tokenInfo.accessToken;
168
- })
169
- .catch(err => this.handleExpiredTokensError(err))
170
- .finally(() => {
171
- // Refresh complete, clear promise
172
- this._refreshPromise = null;
173
- });
174
- }
175
-
176
- return this._refreshPromise;
177
- };
178
-
179
-
180
- // ------------------------------------------------------------------------------
181
- // Public Instance
182
- // ------------------------------------------------------------------------------
183
-
184
- /**
185
- * Returns the clients access token.
186
- *
187
- * If tokens don't yet exist, first attempt to retrieve them.
188
- * If tokens are expired, first attempt to refresh them.
189
- *
190
- * @param {TokenRequestOptions} [options] - Sets optional behavior for the token grant
191
- * @returns {Promise<string>} Promise resolving to the access token
192
- */
193
- PersistentSession.prototype.getAccessToken = function(options) {
194
- // If our tokens are not fresh, we need to refresh them
195
- var expirationBuffer = Math.max(this._config.expiredBufferMS, this._config.staleBufferMS);
196
- if (!this._tokenManager.isAccessTokenValid(this._tokenInfo, expirationBuffer)) {
197
- return this._refreshTokens(options);
198
- }
199
-
200
- // Current access token is still valid. Return it.
201
- return Promise.resolve(this._tokenInfo.accessToken);
202
- };
203
-
204
- /**
205
- * Revokes the session's tokens. If the session has a refresh token we'll use that,
206
- * since it is more likely to be up to date. Otherwise, we'll revoke the accessToken.
207
- * Revoking either one will disable the other as well.
208
- *
209
- * @param {TokenRequestOptions} [options] - Sets optional behavior for the token grant
210
- * @returns {Promise} Promise that resolves when the revoke succeeds
211
- */
212
- PersistentSession.prototype.revokeTokens = function(options) {
213
- return this._tokenManager.revokeTokens(this._tokenInfo.refreshToken, options);
214
- };
215
-
216
- /**
217
- * Exchange the client access token for one with lower scope
218
- * @param {string|string[]} scopes The scope(s) requested for the new token
219
- * @param {string} [resource] The absolute URL of an API resource to scope the new token to
220
- * @param {Object} [options] - Optional parameters
221
- * @param {TokenRequestOptions} [options.tokenRequestOptions] - Sets optional behavior for the token grant
222
- * @returns {void}
223
- */
224
- PersistentSession.prototype.exchangeToken = function(scopes, resource, options) {
225
-
226
- return this.getAccessToken(options)
227
- .then(accessToken => this._tokenManager.exchangeToken(accessToken, scopes, resource, options));
228
- };
229
-
230
- /**
231
- * Handle an an "Expired Tokens" Error. If our tokens are expired, we need to clear the token
232
- * store (if present) before continuing.
233
- *
234
- * @param {Errors~ExpiredTokensError} err An "expired tokens" error including information
235
- * about the request/response.
236
- * @returns {Promise<Error>} Promise resolving to an error. This will
237
- * usually be the original response error, but could an error from trying to access the
238
- * token store as well.
239
- */
240
- PersistentSession.prototype.handleExpiredTokensError = function(err) {
241
-
242
- if (!this._tokenStore) {
243
- return Promise.resolve(err);
244
- }
245
-
246
- // If a token store is available, clear the store and throw either error
247
- // eslint-disable-next-line promise/no-promise-in-callback
248
- return this._tokenStore.clearAsync()
249
- .catch(e => errors.unwrapAndThrow(e))
250
- .then(() => {
251
- throw err;
252
- });
253
- };
254
-
255
- /**
256
- * @module box-node-sdk/lib/sessions/persistent-session
257
- * @see {@Link PersistentSession}
258
- */
62
+ var PersistentSession = /** @class */ (function () {
63
+ function PersistentSession(tokenInfo, tokenStore, config, tokenManager) {
64
+ this._config = config;
65
+ this._tokenManager = tokenManager;
66
+ // Keeps track of if tokens are currently being refreshed
67
+ this._refreshPromise = null;
68
+ // Set valid PersistentSession credentials. Throw if expected credentials are invalid or not given.
69
+ assert_1.default(isObjectValidTokenInfo(tokenInfo), 'tokenInfo is improperly formatted. Properties required: accessToken, refreshToken, accessTokenTTLMS and acquiredAtMS.');
70
+ this._setTokenInfo(tokenInfo);
71
+ // If tokenStore was provided, set the persistent data & current store operations
72
+ if (tokenStore) {
73
+ assert_1.default(isObjectValidTokenStore(tokenStore), 'Token store provided but is improperly formatted. Methods required: read(), write(), clear().');
74
+ this._tokenStore = bluebird_1.Promise.promisifyAll(tokenStore);
75
+ }
76
+ }
77
+ /**
78
+ * Sets all relevant token info for this client.
79
+ *
80
+ * @param {TokenInfo} tokenInfo A valid TokenInfo object.
81
+ * @returns {void}
82
+ * @private
83
+ */
84
+ PersistentSession.prototype._setTokenInfo = function (tokenInfo) {
85
+ this._tokenInfo = {
86
+ accessToken: tokenInfo.accessToken,
87
+ refreshToken: tokenInfo.refreshToken,
88
+ accessTokenTTLMS: tokenInfo.accessTokenTTLMS,
89
+ acquiredAtMS: tokenInfo.acquiredAtMS,
90
+ };
91
+ };
92
+ /**
93
+ * Attempts to refresh tokens for the client.
94
+ * Will use the Box refresh token grant to complete the refresh. On refresh failure, we'll
95
+ * check the token store for more recently updated tokens and load them if found. Otherwise
96
+ * an error will be propagated.
97
+ *
98
+ * @param {TokenRequestOptions} [options] - Sets optional behavior for the token grant
99
+ * @returns {Promise<string>} Promise resolving to the access token
100
+ * @private
101
+ */
102
+ PersistentSession.prototype._refreshTokens = function (options) {
103
+ var _this = this;
104
+ // If not already refreshing, kick off a token refresh request and set a lock so that additional
105
+ // client requests don't try as well
106
+ if (!this._refreshPromise) {
107
+ this._refreshPromise = this._tokenManager
108
+ .getTokensRefreshGrant(this._tokenInfo.refreshToken, options)
109
+ .catch(function (err) {
110
+ // If we got an error response from Box API, but it was 400 invalid_grant, it indicates we may have just
111
+ // made the request with an invalidated refresh token. Since only a max of 2 refresh tokens can be valid
112
+ // at any point in time, and a horizontally scaled app could have multiple Node instances running in parallel,
113
+ // it is possible to hit cases where too many servers all refresh a user's tokens at once
114
+ // and cause this server's token to become invalidated. However, the user should still be alive, but
115
+ // we'll need to check the central data store for the latest valid tokens that some other server in the app
116
+ // cluster would have received. So, instead pull tokens from the central store and attempt to use them.
117
+ if (err.statusCode === http_status_1.default.BAD_REQUEST &&
118
+ _this._tokenStore) {
119
+ var invalidGrantError = err;
120
+ // Check the tokenStore to see if tokens have been updated recently. If they have, then another
121
+ // instance of the session may have already refreshed the user tokens, which would explain why
122
+ // we couldn't refresh.
123
+ return _this._tokenStore
124
+ .readAsync()
125
+ .catch(function (e) { return errors_1.default.unwrapAndThrow(e); })
126
+ .then(function (storeTokenInfo) {
127
+ // if the tokens we got from the central store are the same as the tokens we made the failed request with
128
+ // already, then we can be sure that no other servers have valid tokens for this server either.
129
+ // Thus, this user truly has an expired refresh token. So, propagate an "Expired Tokens" error.
130
+ if (!storeTokenInfo ||
131
+ storeTokenInfo.refreshToken === _this._tokenInfo.refreshToken) {
132
+ throw errors_1.default.buildAuthError(invalidGrantError.response);
133
+ }
134
+ // Propagate the fresh tokens that we found in the session
135
+ return storeTokenInfo;
136
+ });
137
+ }
138
+ // Box API returned a permanent error that is not retryable and we can't recover.
139
+ // We have no usable tokens for the user and no way to refresh them - propagate a permanent error.
140
+ throw err;
141
+ })
142
+ .then(function (tokenInfo) {
143
+ // Success! We got back a TokenInfo object from the API.
144
+ // If we have a token store, we'll write it there now before finishing up the request.
145
+ if (_this._tokenStore) {
146
+ return _this._tokenStore
147
+ .writeAsync(tokenInfo)
148
+ .catch(function (e) { return errors_1.default.unwrapAndThrow(e); })
149
+ .then(function () { return tokenInfo; });
150
+ }
151
+ // If no token store, Set and propagate the access token immediately
152
+ return tokenInfo;
153
+ })
154
+ .then(function (tokenInfo) {
155
+ // Set and propagate the new access token
156
+ _this._setTokenInfo(tokenInfo);
157
+ return tokenInfo.accessToken;
158
+ })
159
+ .catch(function (err) { return _this.handleExpiredTokensError(err); })
160
+ .finally(function () {
161
+ // Refresh complete, clear promise
162
+ _this._refreshPromise = null;
163
+ });
164
+ }
165
+ return this._refreshPromise;
166
+ };
167
+ // ------------------------------------------------------------------------------
168
+ // Public Instance
169
+ // ------------------------------------------------------------------------------
170
+ /**
171
+ * Returns the clients access token.
172
+ *
173
+ * If tokens don't yet exist, first attempt to retrieve them.
174
+ * If tokens are expired, first attempt to refresh them.
175
+ *
176
+ * @param {TokenRequestOptions} [options] - Sets optional behavior for the token grant
177
+ * @returns {Promise<string>} Promise resolving to the access token
178
+ */
179
+ PersistentSession.prototype.getAccessToken = function (options) {
180
+ // If our tokens are not fresh, we need to refresh them
181
+ var expirationBuffer = Math.max(this._config.expiredBufferMS, this._config.staleBufferMS);
182
+ if (!this._tokenManager.isAccessTokenValid(this._tokenInfo, expirationBuffer)) {
183
+ return this._refreshTokens(options);
184
+ }
185
+ // Current access token is still valid. Return it.
186
+ return bluebird_1.Promise.resolve(this._tokenInfo.accessToken);
187
+ };
188
+ /**
189
+ * Revokes the session's tokens. If the session has a refresh token we'll use that,
190
+ * since it is more likely to be up to date. Otherwise, we'll revoke the accessToken.
191
+ * Revoking either one will disable the other as well.
192
+ *
193
+ * @param {TokenRequestOptions} [options] - Sets optional behavior for the token grant
194
+ * @returns {Promise} Promise that resolves when the revoke succeeds
195
+ */
196
+ PersistentSession.prototype.revokeTokens = function (options) {
197
+ return this._tokenManager.revokeTokens(this._tokenInfo.refreshToken, options);
198
+ };
199
+ /**
200
+ * Exchange the client access token for one with lower scope
201
+ * @param {string|string[]} scopes The scope(s) requested for the new token
202
+ * @param {string} [resource] The absolute URL of an API resource to scope the new token to
203
+ * @param {Object} [options] - Optional parameters
204
+ * @param {TokenRequestOptions} [options.tokenRequestOptions] - Sets optional behavior for the token grant
205
+ * @returns {void}
206
+ */
207
+ PersistentSession.prototype.exchangeToken = function (scopes, resource, options) {
208
+ var _this = this;
209
+ return this.getAccessToken(options).then(function (accessToken) {
210
+ return _this._tokenManager.exchangeToken(accessToken, scopes, resource, options);
211
+ });
212
+ };
213
+ /**
214
+ * Handle an an "Expired Tokens" Error. If our tokens are expired, we need to clear the token
215
+ * store (if present) before continuing.
216
+ *
217
+ * @param {Errors~ExpiredTokensError} err An "expired tokens" error including information
218
+ * about the request/response.
219
+ * @returns {Promise<Error>} Promise resolving to an error. This will
220
+ * usually be the original response error, but could an error from trying to access the
221
+ * token store as well.
222
+ */
223
+ PersistentSession.prototype.handleExpiredTokensError = function (err /* FIXME */) {
224
+ if (!this._tokenStore) {
225
+ return bluebird_1.Promise.resolve(err);
226
+ }
227
+ // If a token store is available, clear the store and throw either error
228
+ // eslint-disable-next-line promise/no-promise-in-callback
229
+ return this._tokenStore
230
+ .clearAsync()
231
+ .catch(function (e) { return errors_1.default.unwrapAndThrow(e); })
232
+ .then(function () {
233
+ throw err;
234
+ });
235
+ };
236
+ return PersistentSession;
237
+ }());
259
238
  module.exports = PersistentSession;
239
+ //# sourceMappingURL=persistent-session.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"persistent-session.js","sourceRoot":"","sources":["../../src/sessions/persistent-session.ts"],"names":[],"mappings":";AAAA;;GAEG;;;;AAEH,iFAAiF;AACjF,eAAe;AACf,iFAAiF;AAEjF,kDAA4B;AAC5B,qCAAmC;AACnC,4DAA0C;AAC1C,0DAAoC;AAYpC,iFAAiF;AACjF,UAAU;AACV,iFAAiF;AAEjF;;;;;;;GAOG;AACH,SAAS,sBAAsB,CAAC,GAAwB;IACvD,OAAO,OAAO,CACb,GAAG;QACF,GAAG,CAAC,WAAW;QACf,GAAG,CAAC,YAAY;QAChB,GAAG,CAAC,gBAAgB;QACpB,GAAG,CAAC,YAAY,CACjB,CAAC;AACH,CAAC;AAED;;;;;;;GAOG;AACH,SAAS,uBAAuB,CAAC,GAAwB;IACxD,OAAO,OAAO,CAAC,GAAG,IAAI,GAAG,CAAC,IAAI,IAAI,GAAG,CAAC,KAAK,IAAI,GAAG,CAAC,KAAK,CAAC,CAAC;AAC3D,CAAC;AAED,iFAAiF;AACjF,SAAS;AACT,iFAAiF;AAEjF;;;;;;;;;;;;;;GAcG;AACH;IAOC,2BACC,SAAoB,EACpB,UAAsB,EACtB,MAAc,EACd,YAA0B;QAE1B,IAAI,CAAC,OAAO,GAAG,MAAM,CAAC;QACtB,IAAI,CAAC,aAAa,GAAG,YAAY,CAAC;QAElC,yDAAyD;QACzD,IAAI,CAAC,eAAe,GAAG,IAAI,CAAC;QAE5B,mGAAmG;QACnG,gBAAM,CACL,sBAAsB,CAAC,SAAS,CAAC,EACjC,uHAAuH,CACvH,CAAC;QACF,IAAI,CAAC,aAAa,CAAC,SAAS,CAAC,CAAC;QAE9B,iFAAiF;QACjF,IAAI,UAAU,EAAE;YACf,gBAAM,CACL,uBAAu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