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

feat: Add HTTP header configurations to options #379

Merged
merged 9 commits into from
Apr 28, 2021
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
9 changes: 7 additions & 2 deletions src/amplitude-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,9 @@ var _parseConfig = function _parseConfig(options, config) {
return;
}

// Add exception in headers
const freeFormObjectKeys = new Set(['headers']);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit - this belongs better in the options.js?


// validates config value is defined, is the correct type, and some additional value sanity checks
var parseValidateAndLoad = function parseValidateAndLoad(key) {
if (!Object.prototype.hasOwnProperty.call(options, key)) {
Expand All @@ -342,7 +345,9 @@ var _parseConfig = function _parseConfig(options, config) {
};

for (var key in config) {
if (Object.prototype.hasOwnProperty.call(config, key)) {
if (freeFormObjectKeys.has(key)) {
options[key] = { ...options[key], ...config[key] };
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you might want to add a TODO to find a better soln here - this bypasses all checks, right?

There are still probably some checks to this - as in, header values can't be functions or nested objects, right?

} else if (Object.prototype.hasOwnProperty.call(config, key)) {
parseValidateAndLoad(key);
}
}
Expand Down Expand Up @@ -1520,7 +1525,7 @@ AmplitudeClient.prototype.sendEvents = function sendEvents() {
};

var scope = this;
new Request(url, data).send(function (status, response) {
new Request(url, data, this.options.headers).send(function (status, response) {
scope._sending = false;
try {
if (status === 200 && response === 'success') {
Expand Down
4 changes: 4 additions & 0 deletions src/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import language from './language';
* @property {string} [unsentKey=`amplitude_unsent`] - localStorage key that stores unsent events.
* @property {string} [unsentIdentifyKey=`amplitude_unsent_identify`] - localStorage key that stores unsent identifies.
* @property {number} [uploadBatchSize=`100`] - The maximum number of events to send to the server per request.
* @property {Object} [headers=`{ 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8' }`] - Headers attached to an event(s) upload network request. Custom header properties are merged with this object.
*/
export default {
apiEndpoint: 'api.amplitude.com',
Expand Down Expand Up @@ -89,4 +90,7 @@ export default {
unsentKey: 'amplitude_unsent',
unsentIdentifyKey: 'amplitude_unsent_identify',
uploadBatchSize: 100,
headers: {
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this option is a nested object, will a user passing in their own headers completely overwrite this (and therefore delete this Content-Type value)? Or will it keep Content-Type and append new headers from there?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, don't forget to put a comment for headers at the description of all the possible options at the top of this file

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added docs explaining this

};
11 changes: 9 additions & 2 deletions src/xhr.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,18 @@ import queryString from 'query-string';
/*
* Simple AJAX request object
*/
var Request = function (url, data) {
var Request = function (url, data, headers) {
this.url = url;
this.data = data || {};
this.headers = headers;
};

function setHeaders(xhr, headers) {
for (const header in headers) {
xhr.setRequestHeader(header, headers[header]);
}
}

Request.prototype.send = function (callback) {
var isIE = window.XDomainRequest ? true : false;
if (isIE) {
Expand Down Expand Up @@ -35,7 +42,7 @@ Request.prototype.send = function (callback) {
callback(xhr.status, xhr.responseText);
}
};
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
setHeaders(xhr, this.headers);
xhr.send(queryString.stringify(this.data));
}
//log('sent request to ' + this.url + ' with data ' + decodeURIComponent(queryString(this.data)));
Expand Down
26 changes: 26 additions & 0 deletions test/amplitude-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -1482,6 +1482,32 @@ describe('AmplitudeClient', function () {
assert.equal(server.requests[0].url, 'http://api.amplitude.com');
assert.equal(server.requests[0].method, 'POST');
assert.equal(server.requests[0].async, true);
assert.equal(
server.requests[0].requestHeaders['Content-Type'],
'application/x-www-form-urlencoded;charset=utf-8',
);
});

it('should send request with merged custom header values', function () {
amplitude.init(apiKey, null, {
headers: { Authorization: 'Bearer NOT_A_REAL_BEARER_TOKEN' },
});
amplitude.logEvent('Event Type 1');
assert.lengthOf(server.requests, 1);
assert.equal(
server.requests[0].requestHeaders['Content-Type'],
'application/x-www-form-urlencoded;charset=utf-8',
);
assert.equal(server.requests[0].requestHeaders['Authorization'], 'Bearer NOT_A_REAL_BEARER_TOKEN');
});

it('should send request with overwritten custom header values', function () {
amplitude.init(apiKey, null, {
headers: { 'Content-Type': 'application/json;charset=utf-8' },
});
amplitude.logEvent('Event Type 1');
assert.lengthOf(server.requests, 1);
assert.equal(server.requests[0].requestHeaders['Content-Type'], 'application/json;charset=utf-8');
});

it('should send https request', function () {
Expand Down