@segment/analytics-next
Version:
Analytics Next (aka Analytics 2.0) is the latest version of Segment’s JavaScript SDK - enabling you to send your data to any tool without having to learn, test, or use a new API every time.
47 lines (43 loc) • 1.24 kB
text/typescript
import { JSONObject, Options } from '../core/events/interfaces'
import { CDNSettings } from '../browser'
/**
* Merge legacy settings and initialized Integration option overrides.
*
* This will merge any options that were passed from initialization into
* overrides for settings that are returned by the Segment CDN.
*
* i.e. this allows for passing options directly into destinations from
* the Analytics constructor.
*/
export function mergedOptions(
cdnSettings: CDNSettings,
options: Options
): Record<string, JSONObject> {
const optionOverrides = Object.entries(options.integrations ?? {}).reduce(
(overrides, [integration, options]) => {
if (typeof options === 'object') {
return {
...overrides,
[integration]: options,
}
}
return {
...overrides,
[integration]: {},
}
},
{} as Record<string, JSONObject>
)
return Object.entries(cdnSettings.integrations).reduce(
(integrationSettings, [integration, settings]) => {
return {
...integrationSettings,
[integration]: {
...settings,
...optionOverrides[integration],
},
}
},
{} as Record<string, JSONObject>
)
}