diff --git a/docs/config.rst b/docs/config.rst index ec230662bfb5..e1ffb4b322e7 100644 --- a/docs/config.rst +++ b/docs/config.rst @@ -303,6 +303,20 @@ Those configuration options are documented below: } } +.. describe:: fetchParameters + + ``fetch()`` init parameters (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 bc5481353d4a..07616c71175a 100644 --- a/src/raven.js +++ b/src/raven.js @@ -85,6 +85,12 @@ function Raven() { instrument: true, sampleRate: 1 }; + this._fetchDefaults = { + method: 'POST', + credentials: 'include', + keepalive: true, + referrerPolicy: 'origin' + }; this._ignoreOnError = 0; this._isRavenInstalled = false; this._originalErrorStackTraceLimit = Error.stackTraceLimit; @@ -1899,15 +1905,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 +1994,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..5736ab2b901c 100644 --- a/test/raven.test.js +++ b/test/raven.test.js @@ -1775,6 +1775,9 @@ describe('globals', function() { assert.deepEqual(window.fetch.lastCall.args, [ 'http://localhost/?a=1&b=2', { + credentials: 'include', + keepalive: true, + referrerPolicy: 'origin', method: 'POST', body: '{"foo":"bar"}' } 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;