Skip to content

Commit 270e52a

Browse files
chrisknuClaude
and
Claude
committed
Refactor API host resolution to use a common _getApiHost method for DRY implementation
- Added _getApiHost method to MixpanelLib prototype - Updated all endpoint URL building to use this common method - Maintains backward compatibility with existing api_host config - Consistently handles per-endpoint host overrides 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent 8a5b5c8 commit 270e52a

File tree

4 files changed

+17
-10
lines changed

4 files changed

+17
-10
lines changed

src/mixpanel-core.js

+12-3
Original file line numberDiff line numberDiff line change
@@ -1207,9 +1207,7 @@ MixpanelLib.prototype.track = addOptOutCheckMixpanelLib(function (
12071207
properties: properties,
12081208
};
12091209

1210-
// Use api_hosts[events] if available, fall back to api_host
1211-
var api_host =
1212-
this.get_config("api_hosts")["events"] || this.get_config("api_host");
1210+
var api_host = this._getApiHost("events");
12131211

12141212
var ret = this._track_or_batch(
12151213
{
@@ -2066,6 +2064,17 @@ MixpanelLib.prototype.get_config = function (prop_name) {
20662064
return this["config"][prop_name];
20672065
};
20682066

2067+
/**
2068+
* Get the API host for a specific endpoint type, falling back to the default api_host if not specified
2069+
*
2070+
* @param {String} endpoint_type The type of endpoint (e.g., "events", "people", "groups")
2071+
* @returns {String} The API host to use for this endpoint
2072+
* @private
2073+
*/
2074+
MixpanelLib.prototype._getApiHost = function (endpoint_type) {
2075+
return this.get_config("api_hosts")[endpoint_type] || this.get_config("api_host");
2076+
};
2077+
20692078
/**
20702079
* Fetch a hook function from config, with safe default, and run it
20712080
* against the given arguments

src/mixpanel-group.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -169,9 +169,8 @@ MixpanelGroup.prototype._send_request = function (data, callback) {
169169

170170
var date_encoded_data = _.encodeDates(data);
171171

172-
// Get api_host for groups endpoint if available, otherwise use default api_host
173-
var api_host =
174-
this._get_config("api_hosts")["groups"] || this._get_config("api_host");
172+
// Get api_host for groups endpoint
173+
var api_host = this._mixpanel._getApiHost("groups");
175174

176175
return this._mixpanel._track_or_batch(
177176
{

src/mixpanel-people.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -390,9 +390,8 @@ MixpanelPeople.prototype._send_request = function (data, callback) {
390390
return _.truncate(date_encoded_data, 255);
391391
}
392392

393-
// Get api_host for people endpoint if available, otherwise use default api_host
394-
var api_host =
395-
this._get_config("api_hosts")["people"] || this._get_config("api_host");
393+
// Get api_host for people endpoint
394+
var api_host = this._mixpanel._getApiHost("people");
396395

397396
return this._mixpanel._track_or_batch(
398397
{

src/recorder/session-recording.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ SessionRecording.prototype._sendRequest = function(currentReplayId, reqParams, r
349349
retryAfter: response.headers.get('Retry-After')
350350
});
351351
}.bind(this);
352-
const apiHost = this.getConfig('session_recording_use_proxy') ? this.getConfig('api_host') : 'https://api.mixpanel.com';
352+
const apiHost = this.getConfig('session_recording_use_proxy') ? this._mixpanel._getApiHost("record") : 'https://api.mixpanel.com';
353353

354354
window['fetch'](apiHost + '/' + this.getConfig('api_routes')['record'] + '?' + new URLSearchParams(reqParams), {
355355
'method': 'POST',

0 commit comments

Comments
 (0)