Skip to content

Commit

Permalink
add cdnSettings field to AnalyticsBrowserSettings (#389)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisradek authored Mar 15, 2022
1 parent 05def30 commit b844d2e
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
35 changes: 35 additions & 0 deletions src/__tests__/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,21 @@ import * as SegmentPlugin from '../plugins/segmentio'
import jar from 'js-cookie'
import { AMPLITUDE_WRITEKEY, TEST_WRITEKEY } from './test-writekeys'

// eslint-disable-next-line @typescript-eslint/no-explicit-any
let fetchCalls: Array<any>[] = []
// Mock unfetch so we can record any http requests made
jest.mock('unfetch', () => {
const originalModule = jest.requireActual('unfetch')
return {
__esModule: true,
...originalModule,
default: (...args: unknown[]) => {
fetchCalls.push(args)
return originalModule.apply(originalModule, args)
},
}
})

const sleep = (time: number): Promise<void> =>
new Promise((resolve) => {
setTimeout(resolve, time)
Expand Down Expand Up @@ -69,6 +84,8 @@ const writeKey = TEST_WRITEKEY
describe('Initialization', () => {
beforeEach(async () => {
jest.resetAllMocks()
jest.resetModules()
fetchCalls = []
})

it('loads plugins', async () => {
Expand Down Expand Up @@ -177,6 +194,24 @@ describe('Initialization', () => {
expect(mockPage).not.toHaveBeenCalled()
})

it('fetch remote source settings by default', async () => {
await AnalyticsBrowser.load({
writeKey,
})

expect(fetchCalls.length).toBeGreaterThan(0)
expect(fetchCalls[0][0]).toMatch(/\/settings$/)
})

it('does not fetch source settings if cdnSettings is set', async () => {
await AnalyticsBrowser.load({
writeKey,
cdnSettings: { integrations: {} },
})

expect(fetchCalls.length).toBe(0)
})

describe('options.integrations permutations', () => {
const settings = { writeKey }

Expand Down
14 changes: 12 additions & 2 deletions src/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,15 @@ export interface LegacySettings {
remotePlugins?: RemotePlugin[]
}

export interface AnalyticsBrowserSettings extends AnalyticsSettings {
/**
* The settings for the Segment Source.
* If provided, `AnalyticsBrowser` will not fetch remote settings
* for the source.
*/
cdnSettings?: LegacySettings & Record<string, unknown>
}

export function loadLegacySettings(writeKey: string): Promise<LegacySettings> {
const cdn = window.analytics?._cdn ?? getCDN()
return fetch(`${cdn}/v1/projects/${writeKey}/settings`)
Expand Down Expand Up @@ -219,10 +228,11 @@ async function registerPlugins(

export class AnalyticsBrowser {
static async load(
settings: AnalyticsSettings,
settings: AnalyticsBrowserSettings,
options: InitOptions = {}
): Promise<[Analytics, Context]> {
const legacySettings = await loadLegacySettings(settings.writeKey)
const legacySettings =
settings.cdnSettings ?? (await loadLegacySettings(settings.writeKey))

const retryQueue: boolean =
legacySettings.integrations['Segment.io']?.retryQueue ?? true
Expand Down

0 comments on commit b844d2e

Please sign in to comment.