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

fix: for Unable to configure Custom Domain for React-Native #1043

Open
wants to merge 2 commits 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
12 changes: 8 additions & 4 deletions packages/core/src/__tests__/internal/fetchSettings.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import { settingsCDN } from '../../constants';
import { SEGMENT_DESTINATION_KEY } from '../../plugins/SegmentDestination';
import { getMockLogger, MockSegmentStore } from '../../test-helpers';
import { getURL } from '../../util';

describe('internal #getSettings', () => {
const defaultIntegrationSettings = {
Expand Down Expand Up @@ -50,7 +51,7 @@
await client.fetchSettings();

expect(fetch).toHaveBeenCalledWith(
`${settingsCDN}/${clientArgs.config.writeKey}/settings`,
getURL(settingsCDN, `/projects/${clientArgs.config.writeKey}/settings`),
{
headers: {
'Cache-Control': 'no-cache',
Expand All @@ -71,7 +72,8 @@
await client.fetchSettings();

expect(fetch).toHaveBeenCalledWith(
`${settingsCDN}/${clientArgs.config.writeKey}/settings`,
getURL(settingsCDN, `/projects/${clientArgs.config.writeKey}/settings`),
//`${settingsCDN}/projects/${clientArgs.config.writeKey}/settings`,
{
headers: {
'Cache-Control': 'no-cache',
Expand Down Expand Up @@ -102,7 +104,8 @@
await anotherClient.fetchSettings();

expect(fetch).toHaveBeenCalledWith(
`${settingsCDN}/${clientArgs.config.writeKey}/settings`,
getURL(settingsCDN,`/projects/${clientArgs.config.writeKey}/settings`),

Check warning on line 107 in packages/core/src/__tests__/internal/fetchSettings.test.ts

View workflow job for this annotation

GitHub Actions / build-and-test

Insert `·`
//`${settingsCDN}//${clientArgs.config.writeKey}/settings`,
{
headers: {
'Cache-Control': 'no-cache',
Expand All @@ -128,7 +131,8 @@
await anotherClient.fetchSettings();

expect(fetch).toHaveBeenCalledWith(
`${settingsCDN}/${clientArgs.config.writeKey}/settings`,
getURL(settingsCDN, `/projects/${clientArgs.config.writeKey}/settings`),
//`${settingsCDN}/projects/${clientArgs.config.writeKey}/settings`,
{
headers: {
'Cache-Control': 'no-cache',
Expand Down
41 changes: 40 additions & 1 deletion packages/core/src/__tests__/util.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { UserTraits } from '../types';
import { chunk, allSettled, deepCompare } from '../util';
import { chunk, allSettled, deepCompare, getURL } from '../util';

describe('#chunk', () => {
it('handles empty array', () => {
Expand Down Expand Up @@ -159,3 +159,42 @@ describe('deepCompare', () => {
expect(deepCompare(a, b)).toBe(false);
});
});

describe('getURL function', () => {
// Positive Test Cases
it('should return correct URL for valid host and path', () => {
expect(getURL('www.example.com', '/home')).toBe(
'https://www.example.com/home'
);
expect(getURL('blog.example.com', '/posts')).toBe(
'https://blog.example.com/posts'
);
});

it('should return the root URL when the path is empty', () => {
expect(getURL('www.example.com', '')).toBe('https://www.example.com/');
});

it('should handle query parameters correctly in the URL path', () => {
expect(getURL('www.example.com', '/search?q=test')).toBe(
'https://www.example.com/search?q=test'
);
});

it('should handle special characters in the URL path', () => {
expect(getURL('www.example.com', '/about#section1')).toBe(
'https://www.example.com/about#section1'
);
});

// Negative Test Cases
it('should handle empty host gracefully', () => {
expect(getURL('', '/home')).toBe('https:///home');
});

it('should handle invalid characters in the host', () => {
expect(getURL('invalid host.com', '/path')).toBe(
'https://invalid host.com/path'
);
});
});
28 changes: 16 additions & 12 deletions packages/core/src/analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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),
];
}

/**
Expand Down Expand Up @@ -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',
},
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/constants.ts
Original file line number Diff line number Diff line change
@@ -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: '',
Expand Down
7 changes: 4 additions & 3 deletions packages/core/src/plugins/SegmentDestination.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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 {
Expand All @@ -116,7 +116,8 @@ export class SegmentDestination extends DestinationPlugin {
segmentSettings?.apiHost !== undefined &&
segmentSettings?.apiHost !== null
) {
this.apiHost = `https://${segmentSettings.apiHost}/b`;
//assign the api host from segment settings (domain/v1)
this.apiHost = segmentSettings.apiHost;
}
this.settingsResolve();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
SEGMENT_DESTINATION_KEY,
SegmentDestination,
} from '../SegmentDestination';
import { getURL } from '../../util';

jest.mock('uuid');

Expand Down Expand Up @@ -319,14 +320,14 @@ describe('SegmentDestination', () => {

expect(sendEventsSpy).toHaveBeenCalledTimes(2);
expect(sendEventsSpy).toHaveBeenCalledWith({
url: defaultApiHost,
url: getURL(defaultApiHost, '/b'),
writeKey: '123-456',
events: events.slice(0, 2).map((e) => ({
...e,
})),
});
expect(sendEventsSpy).toHaveBeenCalledWith({
url: defaultApiHost,
url: getURL(defaultApiHost, '/b'),
writeKey: '123-456',
events: events.slice(2, 4).map((e) => ({
...e,
Expand All @@ -353,7 +354,7 @@ describe('SegmentDestination', () => {

expect(sendEventsSpy).toHaveBeenCalledTimes(1);
expect(sendEventsSpy).toHaveBeenCalledWith({
url: `https://${customEndpoint}/b`,
url: getURL(customEndpoint, '/b'),
writeKey: '123-456',
events: events.slice(0, 2).map((e) => ({
...e,
Expand Down Expand Up @@ -384,7 +385,7 @@ describe('SegmentDestination', () => {

expect(sendEventsSpy).toHaveBeenCalledTimes(1);
expect(sendEventsSpy).toHaveBeenCalledWith({
url: customEndpoint,
url: getURL(customEndpoint, '/b'),
writeKey: '123-456',
events: events.slice(0, 2).map((e) => ({
...e,
Expand Down
11 changes: 11 additions & 0 deletions packages/core/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,3 +257,14 @@ export const createPromise = <T>(
resolve: resolver!,
};
};

export function getURL(host: string, path: string) {
if (path === '') {
path = '/'; // Ensure a trailing slash if path is empty
}
if (!host.startsWith('https://')) {
host = 'https://' + host;
}
const s = `${host}${path}`;
return s;
}
Loading