UNPKG

@datadog/mobile-react-native

Version:

A client-side React Native module to interact with Datadog

185 lines (183 loc) 7.37 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getJSONConfiguration = exports.formatPropagatorType = exports.FileBasedConfiguration = void 0; var _DdSdkReactNativeConfiguration = require("../../DdSdkReactNativeConfiguration"); var _SdkVerbosity = require("../../SdkVerbosity"); var _TrackingConsent = require("../../TrackingConsent"); var _types = require("../../rum/types"); /* * 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. */ class FileBasedConfiguration extends _DdSdkReactNativeConfiguration.DatadogProviderConfiguration { constructor(params) { const configuration = getJSONConfiguration(params?.configuration); super(configuration.clientToken, configuration.env, configuration.applicationId, configuration.trackInteractions, configuration.trackResources, configuration.trackErrors, configuration.trackingConsent, configuration.useAccessibilityLabel); this.longTaskThresholdMs = configuration.longTaskThresholdMs || _DdSdkReactNativeConfiguration.DEFAULTS.longTaskThresholdMs; this.verbosity = configuration.verbosity; this.actionNameAttribute = configuration.actionNameAttribute; this.site = configuration.site || _DdSdkReactNativeConfiguration.DEFAULTS.site; this.resourceTracingSamplingRate = configuration.resourceTracingSamplingRate || _DdSdkReactNativeConfiguration.DEFAULTS.resourceTracingSamplingRate; this.firstPartyHosts = configuration.firstPartyHosts || _DdSdkReactNativeConfiguration.DEFAULTS.getFirstPartyHosts(); this.errorEventMapper = params?.errorEventMapper || _DdSdkReactNativeConfiguration.DEFAULTS.errorEventMapper; this.resourceEventMapper = params?.resourceEventMapper || _DdSdkReactNativeConfiguration.DEFAULTS.resourceEventMapper; this.actionEventMapper = params?.actionEventMapper || _DdSdkReactNativeConfiguration.DEFAULTS.actionEventMapper; } } exports.FileBasedConfiguration = FileBasedConfiguration; const resolveJSONConfiguration = userSpecifiedConfiguration => { if (userSpecifiedConfiguration === undefined || userSpecifiedConfiguration === null) { try { // This corresponds to a file located at the root of a RN project. // /!\ We have to write the require this way as dynamic requires are not supported by Hermes. // eslint-disable-next-line global-require, @typescript-eslint/no-var-requires const jsonContent = require('../../../../../../datadog-configuration.json'); if (typeof jsonContent !== 'object' || !jsonContent['configuration']) { console.error(`Failed to parse the Datadog configuration file located at the root of the project. Your configuration must validate the node_modules/@datadog/mobile-react-native/datadog-configuration.schema.json JSON schema. You can use VSCode to check your configuration by adding the following line to your JSON file: { "$schema": "./node_modules/@datadog/mobile-react-native/datadog-configuration.schema.json", }`); return {}; } return jsonContent.configuration; } catch (error) { console.error(`Failed to read Datadog configuration file at the root of the project. If you don't have a datadog-configuration.json file at the same level as your node_modules directory,\ please use the following syntax:\n new FileBasedConfiguration({configuration: require('./file/to/configuration-file.json')}) `); return {}; } } if (typeof userSpecifiedConfiguration !== 'object' || !userSpecifiedConfiguration['configuration']) { console.error(`Failed to parse the Datadog configuration file you provided. Your configuration must validate the node_modules/@datadog/mobile-react-native/datadog-configuration.schema.json JSON schema. You can use VSCode to check your configuration by adding the following line to your JSON file: { "$schema": "./node_modules/@datadog/mobile-react-native/datadog-configuration.schema.json", }`); return {}; } return userSpecifiedConfiguration['configuration']; }; const getJSONConfiguration = userSpecifiedConfiguration => { const configuration = resolveJSONConfiguration(userSpecifiedConfiguration); return { clientToken: configuration.clientToken, env: configuration.env, applicationId: configuration.applicationId, trackInteractions: configuration.trackInteractions, trackResources: configuration.trackResources, trackErrors: configuration.trackErrors, trackingConsent: buildTrackingConsent(configuration.trackingConsent), longTaskThresholdMs: configuration.longTaskThresholdMs, verbosity: buildSdkVerbosity(configuration.verbosity), actionNameAttribute: configuration.actionNameAttribute, resourceTracingSamplingRate: configuration.resourceTracingSamplingRate, firstPartyHosts: buildFirstPartyHosts(configuration.firstPartyHosts), site: configuration.site, useAccessibilityLabel: configuration.useAccessibilityLabel }; }; exports.getJSONConfiguration = getJSONConfiguration; const buildFirstPartyHosts = firstPartyHosts => { if (!firstPartyHosts) { return undefined; } try { return firstPartyHosts.map(({ match, propagatorTypes }) => ({ match, propagatorTypes: propagatorTypes.map(formatPropagatorType) })); } catch (error) { console.error(`Failed to parse the first party hosts from the Datadog configuration file you provided: ${error.message} The first party hosts will not be set for this session. `); return undefined; } }; const formatPropagatorType = propagatorType => { switch (propagatorType.toLowerCase()) { case 'b3': { return _types.PropagatorType.B3; } case 'b3multi': { return _types.PropagatorType.B3MULTI; } case 'datadog': { return _types.PropagatorType.DATADOG; } case 'tracecontext': { return _types.PropagatorType.TRACECONTEXT; } default: { throw new Error(`Failed to parse propagator type ${propagatorType}.`); } } }; exports.formatPropagatorType = formatPropagatorType; const buildTrackingConsent = trackingConsent => { if (trackingConsent === undefined) { return undefined; } switch (trackingConsent.toLowerCase()) { case 'granted': { return _TrackingConsent.TrackingConsent.GRANTED; } case 'pending': { return _TrackingConsent.TrackingConsent.PENDING; } case 'not_granted': { return _TrackingConsent.TrackingConsent.NOT_GRANTED; } default: { return _DdSdkReactNativeConfiguration.DEFAULTS.trackingConsent; } } }; const buildSdkVerbosity = verbosity => { if (verbosity === undefined) { return undefined; } switch (verbosity.toLowerCase()) { case 'debug': { return _SdkVerbosity.SdkVerbosity.DEBUG; } case 'info': { return _SdkVerbosity.SdkVerbosity.INFO; } case 'warn': { return _SdkVerbosity.SdkVerbosity.WARN; } case 'error': { return _SdkVerbosity.SdkVerbosity.ERROR; } default: { return undefined; } } }; //# sourceMappingURL=FileBasedConfiguration.js.map