gd-sprest 9.0.3 → 9.0.5

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.
@@ -478,24 +483,11 @@ export interface IContextInformation {
478
483
  */
479
484
  getWeb(url: string): IBaseExecution<{ GetContextWebInformation: ContextWebInformation }>;
480
485
 
481
- /**
482
- * Refreshes the form digest value used for the requests.
483
- * @param url Optional url to target, otherwise the current web is used.
484
- * @param onComplete Method to call after the token is refreshed. If not supplied, then the token is refreshed synchronously.
485
- */
486
- refreshToken(url?: string, onComplete?: () => void);
487
-
488
486
  /**
489
487
  * Sets the page context information for modern pages.
490
488
  * @param spfxPageContext - The page context information variable from a SPFx project.
491
489
  */
492
490
  setPageContext(spfxPageContext: any);
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.
497
- */
498
- validateToken(digestValue: string): boolean;
499
491
  }
500
492
 
501
493
  // Theme State
@@ -744,44 +744,65 @@ var _ContextInfo = /** @class */ (function () {
744
744
  url: url
745
745
  });
746
746
  };
747
- // Refresh the token
748
- _ContextInfo.refreshToken = function (url, onComplete) {
747
+ _ContextInfo.enableRefreshToken = function () {
749
748
  var _this = this;
750
- // Create the request
751
- var request = this.getWeb(url);
752
- // See if we are doing an async request
753
- if (onComplete) {
754
- // Execute the request
755
- request.execute(function (context) {
756
- // Update the token
757
- _this._contextInfo.formDigestTimeoutSeconds = context.GetContextWebInformation.FormDigestTimeoutSeconds;
758
- _this._contextInfo.formDigestValue = context.GetContextWebInformation.FormDigestValue;
759
- // Resolve the request
760
- onComplete();
761
- }, onComplete);
749
+ // See if the request digest exists
750
+ if (this.formDigestValue == null) {
751
+ return;
762
752
  }
763
- else {
764
- // Execute the request
765
- var context = request.executeAndWait();
766
- // Update the token
767
- this._contextInfo.formDigestTimeoutSeconds = context.GetContextWebInformation.FormDigestTimeoutSeconds;
768
- this._contextInfo.formDigestValue = context.GetContextWebInformation.FormDigestValue;
753
+ // See if we already have a loop method
754
+ if (this._loopId) {
755
+ return;
769
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);
770
780
  };
771
781
  // Method to validate the token
772
782
  _ContextInfo.validateToken = function (digestValue) {
783
+ if (digestValue === void 0) { digestValue = this._contextInfo.formDigestValue; }
773
784
  // See if no value exists
774
785
  if (digestValue == null) {
775
786
  return false;
776
787
  }
777
- // Get the current token and return true if it's still valid
788
+ // Get the current token expiration time
778
789
  var dtToken = new Date(digestValue.split(',')[1]);
779
- return Date.now() < dtToken.getTime();
790
+ var timeout = this.formDigestTimeoutSeconds || 0;
791
+ // Return true if it's still valid
792
+ return Date.now() < dtToken.getTime() + timeout * 1000;
780
793
  };
794
+ var _a;
795
+ _a = _ContextInfo;
781
796
  // The page context information from an spfx project
782
797
  _ContextInfo._spfxPageContext = null;
783
798
  // Method to set the page context information from an SPFX project
784
- _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;
785
806
  return _ContextInfo;
786
807
  }());
787
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.03,
12
+ __ver: 9.05,
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
- lib_1.ContextInfo.refreshToken(this.targetInfo.props.url, 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
- lib_1.ContextInfo.refreshToken(this.targetInfo.props.url);
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
@@ -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.
@@ -973,24 +978,11 @@ declare module 'gd-sprest/lib/contextInfo' {
973
978
  */
974
979
  getWeb(url: string): IBaseExecution<{ GetContextWebInformation: ContextWebInformation }>;
975
980
 
976
- /**
977
- * Refreshes the form digest value used for the requests.
978
- * @param url Optional url to target, otherwise the current web is used.
979
- * @param onComplete Method to call after the token is refreshed. If not supplied, then the token is refreshed synchronously.
980
- */
981
- refreshToken(url?: string, onComplete?: () => void);
982
-
983
981
  /**
984
982
  * Sets the page context information for modern pages.
985
983
  * @param spfxPageContext - The page context information variable from a SPFx project.
986
984
  */
987
985
  setPageContext(spfxPageContext: any);
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.
992
- */
993
- validateToken(digestValue: string): boolean;
994
986
  }
995
987
 
996
988
  export interface IThemeState {