Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add fetchParameters to configure fetch #1177

Merged
merged 3 commits into from
Dec 20, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions docs/config.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
36 changes: 24 additions & 12 deletions src/raven.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
3 changes: 3 additions & 0 deletions test/raven.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"}'
}
Expand Down
5 changes: 5 additions & 0 deletions typescript/raven.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down