From 47974224760773d9d41a05fbc5e578f080e2f361 Mon Sep 17 00:00:00 2001 From: Ruslan Zavacky Date: Mon, 18 Dec 2017 11:46:50 +0000 Subject: [PATCH 1/3] Add `fetchParameters` to configure `fetch` --- docs/config.rst | 6 ++++++ src/raven.js | 35 +++++++++++++++++++++++------------ test/raven.test.js | 2 ++ 3 files changed, 31 insertions(+), 12 deletions(-) diff --git a/docs/config.rst b/docs/config.rst index ec230662bfb5..323ae01b2cfc 100644 --- a/docs/config.rst +++ b/docs/config.rst @@ -303,6 +303,12 @@ Those configuration options are documented below: } } +.. describe:: fetchParameters + + ``fetch()`` init parameters. + + Reference: https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch#Parameters + .. describe:: allowDuplicates diff --git a/src/raven.js b/src/raven.js index bc5481353d4a..6149ed058128 100644 --- a/src/raven.js +++ b/src/raven.js @@ -85,6 +85,11 @@ function Raven() { instrument: true, sampleRate: 1 }; + this._fetchDefaults = { + method: 'POST', + credentials: 'include', + keepalive: true + }; this._ignoreOnError = 0; this._isRavenInstalled = false; this._originalErrorStackTraceLimit = Error.stackTraceLimit; @@ -1899,15 +1904,21 @@ Raven.prototype = { var url = opts.url + '?' + urlencode(opts.auth); var evaluatedHeaders = null; + var evaluatedFetchParameters = {}; + if (opts.options.headers) { - evaluatedHeaders = this._evaluateHeaders(opts.options.headers); + evaluatedHeaders = this._evaluateHash(opts.options.headers); + } + + if (opts.options.fetchParameters) { + evaluatedFetchParameters = this._evaluateHash(opts.options.fetchParameters); } if (supportsFetch()) { - var fetchOptions = { - method: 'POST', - body: stringify(opts.data) - }; + evaluatedFetchParameters.body = stringify(opts.data); + + var defaultFetchOptions = objectMerge({}, this._fetchDefaults); + var fetchOptions = objectMerge(defaultFetchOptions, evaluatedFetchParameters); if (evaluatedHeaders) { fetchOptions.headers = evaluatedHeaders; @@ -1982,17 +1993,17 @@ Raven.prototype = { request.send(stringify(opts.data)); }, - _evaluateHeaders: function(headers) { - var evaluatedHeaders = {}; + _evaluateHash: function(hash) { + var evaluated = {}; - for (var key in headers) { - if (headers.hasOwnProperty(key)) { - var value = headers[key]; - evaluatedHeaders[key] = typeof value === 'function' ? value() : value; + for (var key in hash) { + if (hash.hasOwnProperty(key)) { + var value = hash[key]; + evaluated[key] = typeof value === 'function' ? value() : value; } } - return evaluatedHeaders; + return evaluated; }, _logDebug: function(level) { diff --git a/test/raven.test.js b/test/raven.test.js index 105ce594e989..222f0d8899d9 100644 --- a/test/raven.test.js +++ b/test/raven.test.js @@ -1775,6 +1775,8 @@ describe('globals', function() { assert.deepEqual(window.fetch.lastCall.args, [ 'http://localhost/?a=1&b=2', { + credentials: 'include', + keepalive: true, method: 'POST', body: '{"foo":"bar"}' } From 2fd8924c02d48aa763deed71d5c5b409da16efff Mon Sep 17 00:00:00 2001 From: Ruslan Zavacky Date: Mon, 18 Dec 2017 14:18:00 +0000 Subject: [PATCH 2/3] Add referrerPolicy: 'origin' --- docs/config.rst | 12 ++++++++++-- src/raven.js | 3 ++- test/raven.test.js | 1 + 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/docs/config.rst b/docs/config.rst index 323ae01b2cfc..e1ffb4b322e7 100644 --- a/docs/config.rst +++ b/docs/config.rst @@ -305,9 +305,17 @@ Those configuration options are documented below: .. describe:: fetchParameters - ``fetch()`` init parameters. + ``fetch()`` init parameters (https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch#Parameters). - Reference: https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch#Parameters + Defaults: + + .. code-block:: javascript + { + method: 'POST', + credentials: 'include', + keepalive: true, + referrerPolicy: 'origin' + } .. describe:: allowDuplicates diff --git a/src/raven.js b/src/raven.js index 6149ed058128..07616c71175a 100644 --- a/src/raven.js +++ b/src/raven.js @@ -88,7 +88,8 @@ function Raven() { this._fetchDefaults = { method: 'POST', credentials: 'include', - keepalive: true + keepalive: true, + referrerPolicy: 'origin' }; this._ignoreOnError = 0; this._isRavenInstalled = false; diff --git a/test/raven.test.js b/test/raven.test.js index 222f0d8899d9..5736ab2b901c 100644 --- a/test/raven.test.js +++ b/test/raven.test.js @@ -1777,6 +1777,7 @@ describe('globals', function() { { credentials: 'include', keepalive: true, + referrerPolicy: 'origin', method: 'POST', body: '{"foo":"bar"}' } From b39755c4bbea91355a8d5c53f2f5d0c048bb5854 Mon Sep 17 00:00:00 2001 From: Ruslan Zavacky Date: Tue, 19 Dec 2017 10:28:38 +0000 Subject: [PATCH 3/3] Add typescript definition for fetchParameters --- typescript/raven.d.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/typescript/raven.d.ts b/typescript/raven.d.ts index 222a873a5186..7ac0b30671fe 100644 --- a/typescript/raven.d.ts +++ b/typescript/raven.d.ts @@ -68,6 +68,11 @@ declare module Raven { [key: string]: (string | Function); }; + /** `fetch` init parameters */ + fetchParameters?: { + [key: string]: (string | Function); + }; + /** Allow use of private/secretKey. */ allowSecretKey?: boolean;