gd-sprest 9.0.4 → 9.0.6

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.
@@ -464,6 +464,11 @@ export interface IContextInformation {
464
464
  * Methods
465
465
  */
466
466
 
467
+ /**
468
+ * Runs a loop to ensure the digest value doesn't expire.
469
+ */
470
+ enableRefreshToken();
471
+
467
472
  /**
468
473
  * Generates a guid.
469
474
  * @returns A GUID as a string value.
@@ -483,19 +488,6 @@ export interface IContextInformation {
483
488
  * @param spfxPageContext - The page context information variable from a SPFx project.
484
489
  */
485
490
  setPageContext(spfxPageContext: any);
486
-
487
- /**
488
- * Updates the form digest context information.
489
- * @param digestValue The form digest value.
490
- * @param timeout The timeout value in seconds.
491
- */
492
- updateToken(digestValue: string, timeout: number);
493
-
494
- /**
495
- * Validates the date/time of a form digest value to ensure it's still valid.
496
- * @param digestValue The request digest form value. The default context info for the page is used, if a value isn't given.
497
- */
498
- validateToken(digestValue?: string): boolean;
499
491
  }
500
492
 
501
493
  // Theme State
@@ -744,11 +744,39 @@ var _ContextInfo = /** @class */ (function () {
744
744
  url: url
745
745
  });
746
746
  };
747
- // Method to update the token
748
- _ContextInfo.updateToken = function (digestValue, timeout) {
749
- // Update the context information
750
- this._contextInfo.formDigestTimeoutSeconds = timeout;
751
- this._contextInfo.formDigestValue = digestValue;
747
+ _ContextInfo.enableRefreshToken = function () {
748
+ var _this = this;
749
+ // See if the request digest exists
750
+ if (this.formDigestValue == null) {
751
+ return;
752
+ }
753
+ // See if we already have a loop method
754
+ if (this._loopId) {
755
+ return;
756
+ }
757
+ // Create a loop
758
+ this._loopId = setInterval(function () {
759
+ // See if the digest is valid
760
+ if (_this.validateToken()) {
761
+ return;
762
+ }
763
+ // Log
764
+ console.info("[gd-sprest] Token has expired. Trying to refresh the token.");
765
+ // Get the context
766
+ _this.getWeb().execute(function (context) {
767
+ // Log
768
+ console.info("[gd-sprest] Successfully updated the token information.");
769
+ // Update the context info
770
+ _this._contextInfo.formDigestTimeoutSeconds = context.GetContextWebInformation.FormDigestTimeoutSeconds;
771
+ _this._contextInfo.formDigestValue = context.GetContextWebInformation.FormDigestValue;
772
+ }, function () {
773
+ // Log
774
+ console.info("[gd-sprest] Unable to get the context information to refresh the token.");
775
+ // Stop the loop
776
+ clearInterval(_this._loopId);
777
+ _this._loopId = null;
778
+ });
779
+ }, 500);
752
780
  };
753
781
  // Method to validate the token
754
782
  _ContextInfo.validateToken = function (digestValue) {
@@ -763,10 +791,18 @@ var _ContextInfo = /** @class */ (function () {
763
791
  // Return true if it's still valid
764
792
  return Date.now() < dtToken.getTime() + timeout * 1000;
765
793
  };
794
+ var _a;
795
+ _a = _ContextInfo;
766
796
  // The page context information from an spfx project
767
797
  _ContextInfo._spfxPageContext = null;
768
798
  // Method to set the page context information from an SPFX project
769
- _ContextInfo.setPageContext = function (spfxPageContext) { exports.ContextInfo["_spfxPageContext"] = spfxPageContext; };
799
+ _ContextInfo.setPageContext = function (spfxPageContext) {
800
+ // Set the page context information
801
+ exports.ContextInfo["_spfxPageContext"] = spfxPageContext;
802
+ // Enable the refresh token
803
+ _a.enableRefreshToken();
804
+ };
805
+ _ContextInfo._loopId = null;
770
806
  return _ContextInfo;
771
807
  }());
772
808
  exports.ContextInfo = _ContextInfo;
package/build/rest.js CHANGED
@@ -9,7 +9,7 @@ var sptypes_1 = require("./sptypes");
9
9
  * SharePoint REST Library
10
10
  */
11
11
  exports.$REST = {
12
- __ver: 9.04,
12
+ __ver: 9.06,
13
13
  AppContext: function (siteUrl) { return Lib.Site.getAppContext(siteUrl); },
14
14
  Apps: Lib.Apps,
15
15
  ContextInfo: Lib.ContextInfo,
@@ -55,6 +55,8 @@ var global = Lib.ContextInfo.window.$REST;
55
55
  if (global == null || global.__ver == null || global.__ver < exports.$REST.__ver) {
56
56
  // Set the global variable
57
57
  Lib.ContextInfo.window.$REST = exports.$REST;
58
+ // Enable the refresh token
59
+ Lib.ContextInfo.enableRefreshToken();
58
60
  // Ensure the SP lib exists
59
61
  if (Lib.ContextInfo.window.SP) {
60
62
  // If MDS is turned on in a SP2013 environment, it may throw an error
@@ -200,33 +200,6 @@ var XHRRequest = /** @class */ (function () {
200
200
  // Method to execute the xml http request
201
201
  XHRRequest.prototype.execute = function () {
202
202
  var _this = this;
203
- // Executes the request
204
- var processRequest = function () {
205
- // See if we are targeting the context endpoint
206
- if (_this.targetInfo.props.endpoint == "contextinfo") {
207
- // Execute the request
208
- _this.executeRequest(requestDigest);
209
- }
210
- // See if this is a post request and the request digest does not exist
211
- else if (_this.targetInfo.requestMethod != "GET" && requestDigest == "") {
212
- // See if this is a synchronous request
213
- if (!_this.asyncFl) {
214
- // Log
215
- console.info("[gd-sprest] POST requests must include the request digest information for synchronous requests. This is due to the modern page not including this information on the page.");
216
- }
217
- else {
218
- // Get the context information
219
- lib_1.ContextInfo.getWeb(_this.targetInfo.props.url || document.location.pathname.substr(0, document.location.pathname.lastIndexOf('/'))).execute(function (contextInfo) {
220
- // Execute the request
221
- _this.executeRequest(contextInfo.GetContextWebInformation.FormDigestValue);
222
- });
223
- }
224
- }
225
- else {
226
- // Execute the request
227
- _this.executeRequest(requestDigest);
228
- }
229
- };
230
203
  // Set the request digest
231
204
  var requestDigest = this.targetInfo.props.requestDigest || "";
232
205
  if (requestDigest == "") {
@@ -234,25 +207,29 @@ var XHRRequest = /** @class */ (function () {
234
207
  requestDigest = lib_1.ContextInfo.document ? lib_1.ContextInfo.document.querySelector("#__REQUESTDIGEST") : "";
235
208
  requestDigest = requestDigest ? requestDigest.value : lib_1.ContextInfo.formDigestValue;
236
209
  }
237
- // See if we need to update the request digest
238
- if (requestDigest && lib_1.ContextInfo.validateToken(requestDigest) == false) {
239
- // Log
240
- console.info("[gd-sprest] Token has expired. Trying to refresh the token.");
241
- // See if this is an async request
242
- if (this.asyncFl) {
243
- // Refresh the request digest value and then process the request
244
- this.refreshToken(requestDigest, processRequest);
210
+ // See if we are targeting the context endpoint
211
+ if (this.targetInfo.props.endpoint == "contextinfo") {
212
+ // Execute the request
213
+ this.executeRequest(requestDigest);
214
+ }
215
+ // See if this is a post request and the request digest does not exist
216
+ else if (this.targetInfo.requestMethod != "GET" && requestDigest == "") {
217
+ // See if this is a synchronous request
218
+ if (!this.asyncFl) {
219
+ // Log
220
+ console.info("[gd-sprest] POST requests must include the request digest information for synchronous requests. This is due to the modern page not including this information on the page.");
245
221
  }
246
222
  else {
247
- // Refresh the request digest value
248
- this.refreshToken(requestDigest);
249
- // Process the request
250
- processRequest();
223
+ // Get the context information
224
+ lib_1.ContextInfo.getWeb(this.targetInfo.props.url || document.location.pathname.substr(0, document.location.pathname.lastIndexOf('/'))).execute(function (contextInfo) {
225
+ // Execute the request
226
+ _this.executeRequest(contextInfo.GetContextWebInformation.FormDigestValue);
227
+ });
251
228
  }
252
229
  }
253
230
  else {
254
- // Process the request
255
- processRequest();
231
+ // Execute the request
232
+ this.executeRequest(requestDigest);
256
233
  }
257
234
  };
258
235
  // Method to execute the xml http request
@@ -301,46 +278,6 @@ var XHRRequest = /** @class */ (function () {
301
278
  this.targetInfo.props.bufferFl || this.targetInfo.requestData == null ? this.xhr.send() : this.xhr.send(this.targetInfo.requestData);
302
279
  }
303
280
  };
304
- // Method to refresh the token
305
- XHRRequest.prototype.refreshToken = function (requestDigest, onComplete) {
306
- // Process the response
307
- var processResponse = function () {
308
- // Get the response
309
- var context = JSON.parse(xhr.response);
310
- // Update the context information
311
- lib_1.ContextInfo.updateToken(context.GetContextWebInformation.FormDigestValue, context.GetContextWebInformation.FormDigestTimeoutSeconds);
312
- };
313
- // Create the request
314
- var xhr = this.createXHR();
315
- var url = (this.targetInfo.props.url || lib_1.ContextInfo.webAbsoluteUrl) + "/_api/context";
316
- xhr.open("POST", url, onComplete ? true : false);
317
- // Set the headers
318
- xhr.setRequestHeader("Accept", "application/json");
319
- xhr.setRequestHeader("Content-Type", "application/json");
320
- xhr.setRequestHeader("X-RequestDigest", requestDigest);
321
- xhr.setRequestHeader("X-HTTP-Method", "POST");
322
- // See if we are making an asynchronous request
323
- if (onComplete) {
324
- // Set the state change event
325
- xhr.onreadystatechange = function () {
326
- // See if the request has finished
327
- if (xhr.readyState == 4) {
328
- // Update the context information
329
- processResponse();
330
- // Call the complete method
331
- onComplete();
332
- }
333
- };
334
- // Execute the request
335
- xhr.send();
336
- }
337
- else {
338
- // Execute the request
339
- xhr.send();
340
- // Process the response
341
- processResponse();
342
- }
343
- };
344
281
  return XHRRequest;
345
282
  }());
346
283
  exports.XHRRequest = XHRRequest;
@@ -959,6 +959,11 @@ declare module 'gd-sprest/lib/contextInfo' {
959
959
  };
960
960
 
961
961
 
962
+ /**
963
+ * Runs a loop to ensure the digest value doesn't expire.
964
+ */
965
+ enableRefreshToken();
966
+
962
967
  /**
963
968
  * Generates a guid.
964
969
  * @returns A GUID as a string value.
@@ -978,19 +983,6 @@ declare module 'gd-sprest/lib/contextInfo' {
978
983
  * @param spfxPageContext - The page context information variable from a SPFx project.
979
984
  */
980
985
  setPageContext(spfxPageContext: any);
981
-
982
- /**
983
- * Updates the form digest context information.
984
- * @param digestValue The form digest value.
985
- * @param timeout The timeout value in seconds.
986
- */
987
- updateToken(digestValue: string, timeout: number);
988
-
989
- /**
990
- * Validates the date/time of a form digest value to ensure it's still valid.
991
- * @param digestValue The request digest form value. The default context info for the page is used, if a value isn't given.
992
- */
993
- validateToken(digestValue?: string): boolean;
994
986
  }
995
987
 
996
988
  export interface IThemeState {