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

added support for fetch #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
76 changes: 71 additions & 5 deletions dist/mixpanel.amd.js
Original file line number Diff line number Diff line change
Expand Up @@ -4122,12 +4122,13 @@ define(function () { 'use strict';
*/
// http://hacks.mozilla.org/2009/07/cross-site-xmlhttprequest-with-cors/
// https://developer.mozilla.org/en-US/docs/DOM/XMLHttpRequest#withCredentials
var USE_FETCH = !_.isUndefined(fetch) && typeof(fetch) === 'function';
var USE_XHR = (window$1.XMLHttpRequest && 'withCredentials' in new XMLHttpRequest());

// IE<10 does not support cross-origin XHR's but script tags
// with defer won't block window.onload; ENQUEUE_REQUESTS
// should only be true for Opera<12
var ENQUEUE_REQUESTS = !USE_XHR && (userAgent.indexOf('MSIE') === -1) && (userAgent.indexOf('Mozilla') === -1);
var ENQUEUE_REQUESTS = !USE_XHR && !USE_FETCH && (userAgent.indexOf('MSIE') === -1) && (userAgent.indexOf('Mozilla') === -1);

// save reference to navigator.sendBeacon so it can be minified
var sendBeacon = null;
Expand Down Expand Up @@ -4345,7 +4346,7 @@ define(function () { 'use strict';
this.request_batchers = {};
this._batch_requests = this.get_config('batch_requests');
if (this._batch_requests) {
if (!_.localStorage.is_supported(true) || !USE_XHR) {
if (!_.localStorage.is_supported(true) || (!USE_XHR && !USE_FETCH)) {
this._batch_requests = false;
console.log('Turning off Mixpanel request-queueing; needs XHR and localStorage support');
_.each(this.get_batcher_configs(), function(batcher_config) {
Expand Down Expand Up @@ -4467,7 +4468,7 @@ define(function () { 'use strict';
return null;
}

if (USE_XHR) {
if (USE_XHR || USE_FETCH) {
var callback_function = function(response) {
callback(response, data);
};
Expand Down Expand Up @@ -4507,7 +4508,7 @@ define(function () { 'use strict';
options = null;
}
options = _.extend(DEFAULT_OPTIONS, options || {});
if (!USE_XHR) {
if (!USE_XHR && !USE_FETCH) {
options.method = 'GET';
}
var use_post = options.method === 'POST';
Expand All @@ -4520,7 +4521,7 @@ define(function () { 'use strict';
if (this.get_config('test')) { data['test'] = 1; }
if (verbose_mode) { data['verbose'] = 1; }
if (this.get_config('img')) { data['img'] = 1; }
if (!USE_XHR) {
if (!USE_XHR && !USE_FETCH) {
if (callback) {
data['callback'] = callback;
} else if (verbose_mode || this.get_config('test')) {
Expand Down Expand Up @@ -4630,6 +4631,71 @@ define(function () { 'use strict';
lib.report_error(e);
succeeded = false;
}
} else if (USE_FETCH) {
try {
var xhr_headers = this.get_config('xhr_headers');
if (use_post) {
xhr_headers['Content-Type'] = 'application/x-www-form-urlencoded';
}

var fetchOpts = {
method: options.method,
mode: 'cors',
credentials: 'include',
headers: xhr_headers,
body: body_data
};

fetch(url, fetchOpts)
.then(function (response) {
return response.text().then(function (body) {
return {
status: response.status,
statusText: response.statusText,
headers: response.headers,
body: body
};
});
})
.then(function (res) {
if (res.status === 200) {
if (callback) {
var body = res.body;
if (verbose_mode) {
var response;
try {
response = _.JSONDecode(body);
} catch (e) {
lib.report_error(e);
if (options.ignore_json_errors) {
response = body;
} else {
return;
}
}
callback(response);
} else {
callback(Number(body));
}
}
} else {
var error = 'Bad HTTP status: ' + res.status + ' ' + res.statusText;
lib.report_error(error);

if (callback) {
if (verbose_mode) {
var xhr_req = { status: res.status, responseHeaders: res.headers };
callback({ status: 0, error: error, xhr_req: xhr_req });
} else {
callback(0);
}
}
}
});
} catch (e) {
lib.report_error(e);
succeeded = false;
}
} else {
var script = document$1.createElement('script');
script.type = 'text/javascript';
Expand Down
76 changes: 71 additions & 5 deletions dist/mixpanel.cjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -4122,12 +4122,13 @@ var NOOP_FUNC = function() {};
*/
// http://hacks.mozilla.org/2009/07/cross-site-xmlhttprequest-with-cors/
// https://developer.mozilla.org/en-US/docs/DOM/XMLHttpRequest#withCredentials
var USE_FETCH = !_.isUndefined(fetch) && typeof(fetch) === 'function';
var USE_XHR = (window$1.XMLHttpRequest && 'withCredentials' in new XMLHttpRequest());

// IE<10 does not support cross-origin XHR's but script tags
// with defer won't block window.onload; ENQUEUE_REQUESTS
// should only be true for Opera<12
var ENQUEUE_REQUESTS = !USE_XHR && (userAgent.indexOf('MSIE') === -1) && (userAgent.indexOf('Mozilla') === -1);
var ENQUEUE_REQUESTS = !USE_XHR && !USE_FETCH && (userAgent.indexOf('MSIE') === -1) && (userAgent.indexOf('Mozilla') === -1);

// save reference to navigator.sendBeacon so it can be minified
var sendBeacon = null;
Expand Down Expand Up @@ -4345,7 +4346,7 @@ MixpanelLib.prototype._init = function(token, config, name) {
this.request_batchers = {};
this._batch_requests = this.get_config('batch_requests');
if (this._batch_requests) {
if (!_.localStorage.is_supported(true) || !USE_XHR) {
if (!_.localStorage.is_supported(true) || (!USE_XHR && !USE_FETCH)) {
this._batch_requests = false;
console.log('Turning off Mixpanel request-queueing; needs XHR and localStorage support');
_.each(this.get_batcher_configs(), function(batcher_config) {
Expand Down Expand Up @@ -4467,7 +4468,7 @@ MixpanelLib.prototype._prepare_callback = function(callback, data) {
return null;
}

if (USE_XHR) {
if (USE_XHR || USE_FETCH) {
var callback_function = function(response) {
callback(response, data);
};
Expand Down Expand Up @@ -4507,7 +4508,7 @@ MixpanelLib.prototype._send_request = function(url, data, options, callback) {
options = null;
}
options = _.extend(DEFAULT_OPTIONS, options || {});
if (!USE_XHR) {
if (!USE_XHR && !USE_FETCH) {
options.method = 'GET';
}
var use_post = options.method === 'POST';
Expand All @@ -4520,7 +4521,7 @@ MixpanelLib.prototype._send_request = function(url, data, options, callback) {
if (this.get_config('test')) { data['test'] = 1; }
if (verbose_mode) { data['verbose'] = 1; }
if (this.get_config('img')) { data['img'] = 1; }
if (!USE_XHR) {
if (!USE_XHR && !USE_FETCH) {
if (callback) {
data['callback'] = callback;
} else if (verbose_mode || this.get_config('test')) {
Expand Down Expand Up @@ -4630,6 +4631,71 @@ MixpanelLib.prototype._send_request = function(url, data, options, callback) {
lib.report_error(e);
succeeded = false;
}
} else if (USE_FETCH) {
try {
var xhr_headers = this.get_config('xhr_headers');
if (use_post) {
xhr_headers['Content-Type'] = 'application/x-www-form-urlencoded';
}

var fetchOpts = {
method: options.method,
mode: 'cors',
credentials: 'include',
headers: xhr_headers,
body: body_data
};

fetch(url, fetchOpts)
.then(function (response) {
return response.text().then(function (body) {
return {
status: response.status,
statusText: response.statusText,
headers: response.headers,
body: body
};
});
})
.then(function (res) {
if (res.status === 200) {
if (callback) {
var body = res.body;
if (verbose_mode) {
var response;
try {
response = _.JSONDecode(body);
} catch (e) {
lib.report_error(e);
if (options.ignore_json_errors) {
response = body;
} else {
return;
}
}
callback(response);
} else {
callback(Number(body));
}
}
} else {
var error = 'Bad HTTP status: ' + res.status + ' ' + res.statusText;
lib.report_error(error);

if (callback) {
if (verbose_mode) {
var xhr_req = { status: res.status, responseHeaders: res.headers };
callback({ status: 0, error: error, xhr_req: xhr_req });
} else {
callback(0);
}
}
}
});
} catch (e) {
lib.report_error(e);
succeeded = false;
}
} else {
var script = document$1.createElement('script');
script.type = 'text/javascript';
Expand Down
76 changes: 71 additions & 5 deletions dist/mixpanel.globals.js
Original file line number Diff line number Diff line change
Expand Up @@ -4123,12 +4123,13 @@
*/
// http://hacks.mozilla.org/2009/07/cross-site-xmlhttprequest-with-cors/
// https://developer.mozilla.org/en-US/docs/DOM/XMLHttpRequest#withCredentials
var USE_FETCH = !_.isUndefined(fetch) && typeof(fetch) === 'function';
var USE_XHR = (window$1.XMLHttpRequest && 'withCredentials' in new XMLHttpRequest());

// IE<10 does not support cross-origin XHR's but script tags
// with defer won't block window.onload; ENQUEUE_REQUESTS
// should only be true for Opera<12
var ENQUEUE_REQUESTS = !USE_XHR && (userAgent.indexOf('MSIE') === -1) && (userAgent.indexOf('Mozilla') === -1);
var ENQUEUE_REQUESTS = !USE_XHR && !USE_FETCH && (userAgent.indexOf('MSIE') === -1) && (userAgent.indexOf('Mozilla') === -1);

// save reference to navigator.sendBeacon so it can be minified
var sendBeacon = null;
Expand Down Expand Up @@ -4346,7 +4347,7 @@
this.request_batchers = {};
this._batch_requests = this.get_config('batch_requests');
if (this._batch_requests) {
if (!_.localStorage.is_supported(true) || !USE_XHR) {
if (!_.localStorage.is_supported(true) || (!USE_XHR && !USE_FETCH)) {
this._batch_requests = false;
console.log('Turning off Mixpanel request-queueing; needs XHR and localStorage support');
_.each(this.get_batcher_configs(), function(batcher_config) {
Expand Down Expand Up @@ -4468,7 +4469,7 @@
return null;
}

if (USE_XHR) {
if (USE_XHR || USE_FETCH) {
var callback_function = function(response) {
callback(response, data);
};
Expand Down Expand Up @@ -4508,7 +4509,7 @@
options = null;
}
options = _.extend(DEFAULT_OPTIONS, options || {});
if (!USE_XHR) {
if (!USE_XHR && !USE_FETCH) {
options.method = 'GET';
}
var use_post = options.method === 'POST';
Expand All @@ -4521,7 +4522,7 @@
if (this.get_config('test')) { data['test'] = 1; }
if (verbose_mode) { data['verbose'] = 1; }
if (this.get_config('img')) { data['img'] = 1; }
if (!USE_XHR) {
if (!USE_XHR && !USE_FETCH) {
if (callback) {
data['callback'] = callback;
} else if (verbose_mode || this.get_config('test')) {
Expand Down Expand Up @@ -4631,6 +4632,71 @@
lib.report_error(e);
succeeded = false;
}
} else if (USE_FETCH) {
try {
var xhr_headers = this.get_config('xhr_headers');
if (use_post) {
xhr_headers['Content-Type'] = 'application/x-www-form-urlencoded';
}

var fetchOpts = {
method: options.method,
mode: 'cors',
credentials: 'include',
headers: xhr_headers,
body: body_data
};

fetch(url, fetchOpts)
.then(function (response) {
return response.text().then(function (body) {
return {
status: response.status,
statusText: response.statusText,
headers: response.headers,
body: body
};
});
})
.then(function (res) {
if (res.status === 200) {
if (callback) {
var body = res.body;
if (verbose_mode) {
var response;
try {
response = _.JSONDecode(body);
} catch (e) {
lib.report_error(e);
if (options.ignore_json_errors) {
response = body;
} else {
return;
}
}
callback(response);
} else {
callback(Number(body));
}
}
} else {
var error = 'Bad HTTP status: ' + res.status + ' ' + res.statusText;
lib.report_error(error);

if (callback) {
if (verbose_mode) {
var xhr_req = { status: res.status, responseHeaders: res.headers };
callback({ status: 0, error: error, xhr_req: xhr_req });
} else {
callback(0);
}
}
}
});
} catch (e) {
lib.report_error(e);
succeeded = false;
}
} else {
var script = document$1.createElement('script');
script.type = 'text/javascript';
Expand Down
1 change: 0 additions & 1 deletion dist/mixpanel.js

This file was deleted.

Loading