gd-sprest 9.0.2 → 9.0.3

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.
@@ -478,11 +478,24 @@ export interface IContextInformation {
478
478
  */
479
479
  getWeb(url: string): IBaseExecution<{ GetContextWebInformation: ContextWebInformation }>;
480
480
 
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
+
481
488
  /**
482
489
  * Sets the page context information for modern pages.
483
490
  * @param spfxPageContext - The page context information variable from a SPFx project.
484
491
  */
485
492
  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;
486
499
  }
487
500
 
488
501
  // Theme State
@@ -735,8 +735,6 @@ var _ContextInfo = /** @class */ (function () {
735
735
  return v.toString(16);
736
736
  });
737
737
  };
738
- // The page context information from an spfx project
739
- _ContextInfo._spfxPageContext = null;
740
738
  // Method to get the context information for a web
741
739
  _ContextInfo.getWeb = function (url) {
742
740
  // Create a new base object
@@ -746,6 +744,42 @@ var _ContextInfo = /** @class */ (function () {
746
744
  url: url
747
745
  });
748
746
  };
747
+ // Refresh the token
748
+ _ContextInfo.refreshToken = function (url, onComplete) {
749
+ 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);
762
+ }
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;
769
+ }
770
+ };
771
+ // Method to validate the token
772
+ _ContextInfo.validateToken = function (digestValue) {
773
+ // See if no value exists
774
+ if (digestValue == null) {
775
+ return false;
776
+ }
777
+ // Get the current token and return true if it's still valid
778
+ var dtToken = new Date(digestValue.split(',')[1]);
779
+ return Date.now() < dtToken.getTime();
780
+ };
781
+ // The page context information from an spfx project
782
+ _ContextInfo._spfxPageContext = null;
749
783
  // Method to set the page context information from an SPFX project
750
784
  _ContextInfo.setPageContext = function (spfxPageContext) { exports.ContextInfo["_spfxPageContext"] = spfxPageContext; };
751
785
  return _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.02,
12
+ __ver: 9.03,
13
13
  AppContext: function (siteUrl) { return Lib.Site.getAppContext(siteUrl); },
14
14
  Apps: Lib.Apps,
15
15
  ContextInfo: Lib.ContextInfo,
@@ -200,6 +200,33 @@ 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
+ };
203
230
  // Set the request digest
204
231
  var requestDigest = this.targetInfo.props.requestDigest || "";
205
232
  if (requestDigest == "") {
@@ -207,29 +234,25 @@ var XHRRequest = /** @class */ (function () {
207
234
  requestDigest = lib_1.ContextInfo.document ? lib_1.ContextInfo.document.querySelector("#__REQUESTDIGEST") : "";
208
235
  requestDigest = requestDigest ? requestDigest.value : lib_1.ContextInfo.formDigestValue;
209
236
  }
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.");
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);
221
245
  }
222
246
  else {
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
- });
247
+ // Refresh the request digest value
248
+ lib_1.ContextInfo.refreshToken(this.targetInfo.props.url);
249
+ // Process the request
250
+ processRequest();
228
251
  }
229
252
  }
230
253
  else {
231
- // Execute the request
232
- this.executeRequest(requestDigest);
254
+ // Process the request
255
+ processRequest();
233
256
  }
234
257
  };
235
258
  // Method to execute the xml http request
@@ -973,11 +973,24 @@ declare module 'gd-sprest/lib/contextInfo' {
973
973
  */
974
974
  getWeb(url: string): IBaseExecution<{ GetContextWebInformation: ContextWebInformation }>;
975
975
 
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
+
976
983
  /**
977
984
  * Sets the page context information for modern pages.
978
985
  * @param spfxPageContext - The page context information variable from a SPFx project.
979
986
  */
980
987
  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;
981
994
  }
982
995
 
983
996
  export interface IThemeState {