@datadog/mobile-react-native
Version:
A client-side React Native module to interact with Datadog
58 lines (55 loc) • 2.5 kB
JavaScript
/*
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0.
* This product includes software developed at Datadog (https://www.datadoghq.com/).
* Copyright 2016-Present Datadog, Inc.
*/
import { InternalLog } from '../../InternalLog';
import { SdkVerbosity } from '../../SdkVerbosity';
import { DdSdk } from '../../sdk/DdSdk';
import { AttributesSingleton } from '../AttributesSingleton/AttributesSingleton';
import { UserInfoSingleton } from '../UserInfoSingleton/UserInfoSingleton';
import { deepClone } from './utils/deepClone';
/**
* Generic class for applying event mappers.
*
* Calls params in this order: formatRawEventForMapper, eventMapper, formatMapperEventForNative.
*
* @param eventMapper the user-registered event mapper
* @param formatRawEventForMapper formatter that gets the raw event (from javascript call) and returns the input given to the mapper
* @param formatMapperEventForNative formatter that gets the output of the mapper, and returns the input given to the native SDKs
* @param formatRawEventForNative called when no event mapper is registered
*/
export class EventMapper {
constructor(eventMapper, formatRawEventForMapper, formatMapperEventForNative, formatRawEventForNative) {
this.eventMapper = eventMapper;
this.formatRawEventForMapper = formatRawEventForMapper;
this.formatMapperEventForNative = formatMapperEventForNative;
this.formatRawEventForNative = formatRawEventForNative;
}
applyEventMapper = rawEvent => {
if (!this.eventMapper) {
return this.formatRawEventForNative(rawEvent);
}
// formatting
const userInfo = UserInfoSingleton.getInstance().getUserInfo();
const attributes = AttributesSingleton.getInstance().getAttributes();
const initialEvent = this.formatRawEventForMapper(rawEvent, {
userInfo,
attributes
});
// mapper
const backupEvent = deepClone(initialEvent);
try {
const mappedEvent = this.eventMapper(initialEvent);
if (!mappedEvent) {
return null;
}
return this.formatMapperEventForNative(mappedEvent, backupEvent);
} catch (error) {
InternalLog.log(`The event mapper crashed when mapping ${JSON.stringify(backupEvent)}: ${error}`, SdkVerbosity.WARN);
DdSdk.telemetryDebug('Error while running the event mapper');
return this.formatMapperEventForNative(backupEvent, backupEvent);
}
};
}
//# sourceMappingURL=EventMapper.js.map