gd-sprest 9.0.2 → 9.0.4

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.
@@ -483,6 +483,19 @@ export interface IContextInformation {
483
483
  * @param spfxPageContext - The page context information variable from a SPFx project.
484
484
  */
485
485
  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;
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,27 @@ var _ContextInfo = /** @class */ (function () {
746
744
  url: url
747
745
  });
748
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;
752
+ };
753
+ // Method to validate the token
754
+ _ContextInfo.validateToken = function (digestValue) {
755
+ if (digestValue === void 0) { digestValue = this._contextInfo.formDigestValue; }
756
+ // See if no value exists
757
+ if (digestValue == null) {
758
+ return false;
759
+ }
760
+ // Get the current token expiration time
761
+ var dtToken = new Date(digestValue.split(',')[1]);
762
+ var timeout = this.formDigestTimeoutSeconds || 0;
763
+ // Return true if it's still valid
764
+ return Date.now() < dtToken.getTime() + timeout * 1000;
765
+ };
766
+ // The page context information from an spfx project
767
+ _ContextInfo._spfxPageContext = null;
749
768
  // Method to set the page context information from an SPFX project
750
769
  _ContextInfo.setPageContext = function (spfxPageContext) { exports.ContextInfo["_spfxPageContext"] = spfxPageContext; };
751
770
  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.04,
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
+ this.refreshToken(requestDigest, 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
+ this.refreshToken(requestDigest);
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
@@ -278,6 +301,46 @@ var XHRRequest = /** @class */ (function () {
278
301
  this.targetInfo.props.bufferFl || this.targetInfo.requestData == null ? this.xhr.send() : this.xhr.send(this.targetInfo.requestData);
279
302
  }
280
303
  };
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
+ };
281
344
  return XHRRequest;
282
345
  }());
283
346
  exports.XHRRequest = XHRRequest;
@@ -978,6 +978,19 @@ declare module 'gd-sprest/lib/contextInfo' {
978
978
  * @param spfxPageContext - The page context information variable from a SPFx project.
979
979
  */
980
980
  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;
981
994
  }
982
995
 
983
996
  export interface IThemeState {