From cdf588ae28284f0d447d5f0a1ca525a603197fed Mon Sep 17 00:00:00 2001 From: Sunita Prajapati Date: Fri, 31 Jan 2025 15:45:03 +0530 Subject: [PATCH] fix: Unable to configure Custom Domain for React-Native --- packages/core/src/analytics.ts | 28 +++++++++++-------- packages/core/src/constants.ts | 4 +-- .../core/src/plugins/SegmentDestination.ts | 4 +-- packages/core/src/util.ts | 5 ++++ 4 files changed, 25 insertions(+), 16 deletions(-) diff --git a/packages/core/src/analytics.ts b/packages/core/src/analytics.ts index 374b4b9e..3fbe1c28 100644 --- a/packages/core/src/analytics.ts +++ b/packages/core/src/analytics.ts @@ -57,7 +57,12 @@ import { UserInfoState, UserTraits, } from './types'; -import { allSettled, getPluginsWithFlush, getPluginsWithReset } from './util'; +import { + allSettled, + getPluginsWithFlush, + getPluginsWithReset, + getURL, +} from './util'; import { getUUID } from './uuid'; import type { FlushPolicy } from './flushPolicies'; import { @@ -160,15 +165,13 @@ export class SegmentClient { if (ofType !== undefined) { return [...(plugins[ofType] ?? [])]; } - return ( - [ - ...this.getPlugins(PluginType.before), - ...this.getPlugins(PluginType.enrichment), - ...this.getPlugins(PluginType.utility), - ...this.getPlugins(PluginType.destination), - ...this.getPlugins(PluginType.after), - ] - ); + return [ + ...this.getPlugins(PluginType.before), + ...this.getPlugins(PluginType.enrichment), + ...this.getPlugins(PluginType.utility), + ...this.getPlugins(PluginType.destination), + ...this.getPlugins(PluginType.after), + ]; } /** @@ -320,10 +323,11 @@ export class SegmentClient { async fetchSettings() { const settingsPrefix: string = this.config.cdnProxy ?? settingsCDN; - const settingsEndpoint = `${settingsPrefix}/${this.config.writeKey}/settings`; + const settingsEndpoint = `/projects/${this.config.writeKey}/settings`; + const settingsURL = getURL(settingsPrefix, settingsEndpoint); try { - const res = await fetch(settingsEndpoint, { + const res = await fetch(settingsURL, { headers: { 'Cache-Control': 'no-cache', }, diff --git a/packages/core/src/constants.ts b/packages/core/src/constants.ts index a9dd5b87..fed45d0e 100644 --- a/packages/core/src/constants.ts +++ b/packages/core/src/constants.ts @@ -1,8 +1,8 @@ import type { Config } from './types'; -export const defaultApiHost = 'https://api.segment.io/v1/b'; +export const defaultApiHost = 'api.segment.io/v1'; -export const settingsCDN = 'https://cdn-settings.segment.com/v1/projects'; +export const settingsCDN = 'cdn-settings.segment.com/v1'; export const defaultConfig: Config = { writeKey: '', diff --git a/packages/core/src/plugins/SegmentDestination.ts b/packages/core/src/plugins/SegmentDestination.ts index b3717b8e..cc9dc007 100644 --- a/packages/core/src/plugins/SegmentDestination.ts +++ b/packages/core/src/plugins/SegmentDestination.ts @@ -6,7 +6,7 @@ import { SegmentEvent, UpdateType, } from '../types'; -import { chunk, createPromise } from '../util'; +import { chunk, createPromise, getURL } from '../util'; import { uploadEvents } from '../api'; import type { SegmentClient } from '../analytics'; import { DestinationMetadataEnrichment } from './DestinationMetadataEnrichment'; @@ -90,7 +90,7 @@ export class SegmentDestination extends DestinationPlugin { private getEndpoint(): string { const config = this.analytics?.getConfig(); - return config?.proxy ?? this.apiHost ?? defaultApiHost; + return getURL(config?.proxy ?? this.apiHost ?? defaultApiHost, '/b'); } configure(analytics: SegmentClient): void { diff --git a/packages/core/src/util.ts b/packages/core/src/util.ts index c2e2ccca..106c57f0 100644 --- a/packages/core/src/util.ts +++ b/packages/core/src/util.ts @@ -257,3 +257,8 @@ export const createPromise = ( resolve: resolver!, }; }; + +export function getURL(host: string, path: string) { + const s = `https://${host}${path}`; + return s; +}