Skip to content

Commit eac1c97

Browse files
authored
Merge pull request #417 from zhaoz/master
Allow setting withCredentials in spf xhr options
2 parents 0a4d35a + fd69163 commit eac1c97

File tree

5 files changed

+27
-4
lines changed

5 files changed

+27
-4
lines changed

src/api.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,13 @@ spf.RequestOptions.prototype.onDone;
325325
spf.RequestOptions.prototype.postData;
326326

327327

328+
/**
329+
* Optional flag to configure the XHR to send withCredentials or not.
330+
* @type {boolean|undefined}
331+
*/
332+
spf.RequestOptions.prototype.withCredentials;
333+
334+
328335
/**
329336
* Definition of CustomEvents dispatched by SPF.
330337
* @interface

src/client/base.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,7 @@ spf.MultipartResponse;
285285
* - onRequest: optional callback when a request will be made.
286286
* - postData: optional data to send with a request. Only used if the method
287287
* is set to "POST".
288+
* - withCredentials: optional flag to send credentials if true.
288289
*
289290
* @typedef {{
290291
* headers: (Object.<string>|undefined),
@@ -295,7 +296,8 @@ spf.MultipartResponse;
295296
* onPartProcess: (function(spf.EventDetail)|undefined),
296297
* onProcess: (function(spf.EventDetail)|undefined),
297298
* onRequest: (function(spf.EventDetail)|undefined),
298-
* postData: (ArrayBuffer|Blob|Document|FormData|null|string|undefined)
299+
* postData: (ArrayBuffer|Blob|Document|FormData|null|string|undefined),
300+
* withCredentials: (boolean|undefined)
299301
* }}
300302
*/
301303
spf.RequestOptions;

src/client/nav/nav.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -983,7 +983,8 @@ spf.nav.load_ = function(url, options, info) {
983983
onError: handleError,
984984
onSuccess: handleSuccess,
985985
postData: options['postData'],
986-
type: info.type
986+
type: info.type,
987+
withCredentials: options['withCredentials']
987988
});
988989
};
989990

src/client/nav/request.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ goog.require('spf.url');
5252
* alter the URL identifier and XHR header and used to determine whether
5353
* the global "navigation received" callback is executed; defaults to
5454
* "request".
55+
* - withCredentials: optional flag to send credentials if true.
5556
*
5657
* @typedef {{
5758
* method: (string|undefined),
@@ -65,7 +66,8 @@ goog.require('spf.url');
6566
* postData: spf.net.xhr.PostData,
6667
* current: (string|null|undefined),
6768
* referer: (string|null|undefined),
68-
* type: (string|undefined)
69+
* type: (string|undefined),
70+
* withCredentials: (boolean|undefined)
6971
* }}
7072
*/
7173
spf.nav.request.Options;
@@ -194,6 +196,11 @@ spf.nav.request.send = function(url, opt_options) {
194196
onDone: handleComplete,
195197
onTimeout: handleComplete
196198
};
199+
200+
if (options.withCredentials) {
201+
xhrOpts.withCredentials = options.withCredentials;
202+
}
203+
197204
// As an advanced option, allow XHR requests to enforce JSON responses.
198205
// This can make response parsing more efficient by reducing contention on
199206
// the main thread (especially for very large responses), but as a

src/client/net/xhr.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ goog.require('spf');
3030
* - responseType: type to create from the XHR response.
3131
* - timeoutMs: number of milliseconds after which the request will be timed
3232
* out by the client. Default is to allow the browser to handle timeouts.
33+
* - withCredentials: optional flag to send credentials if true.
3334
*
3435
* @typedef {{
3536
* headers: (Object.<string>|undefined),
@@ -38,7 +39,8 @@ goog.require('spf');
3839
* onHeaders: (function(XMLHttpRequest)|undefined),
3940
* onTimeout: (function(XMLHttpRequest)|undefined),
4041
* responseType: (string|undefined),
41-
* timeoutMs: (number|undefined)
42+
* timeoutMs: (number|undefined),
43+
* withCredentials: (boolean|undefined)
4244
* }}
4345
*/
4446
spf.net.xhr.Options;
@@ -151,6 +153,10 @@ spf.net.xhr.send = function(method, url, data, opt_options) {
151153
xhr.responseType = 'json';
152154
}
153155

156+
if (options.withCredentials) {
157+
xhr.withCredentials = options.withCredentials;
158+
}
159+
154160
// For POST, default to `Content-Type: application/x-www-form-urlencoded`
155161
// unless a custom header was given.
156162
var isFormData = ('FormData' in window && data instanceof FormData);

0 commit comments

Comments
 (0)