import type {ADError} from '../utils/instrumentation-helpers'; import InstrumentationModule from '../../native/InstrumentationModule'; type RequestTrackerHeaders = {[key: string]: string}; /** * Create and configure a RequestTracker object before sending an HTTP request to track it manually. * * If the SDK does not automatically discover your HTTP requests, use this class to explicitly * report them. Note that most users will not need to use this class; check the documentation * for the list of HTTP APIs that are automatically discovered. * * Warning: `Instrumentation.start({ appKey: })` must've been called. * * Usage: * * ```ts * import {RequestTracker} from '@appdynamics/react-native-agent'; * * function sendRequest() { * const tracker = await RequestTracker("https://www.appdynamics.com"); * * const correlationHeaders = await RequestTracker.getServerCorrelationHeaders(); * * const headers = { * 'Custom-Header': 'Test-Header', * ...correlationHeaders * }; * await tracker.setRequestHeaders(headers); * * try { * const response = await fetch(url); * * const responseHeaders = {}; * response.headers.forEach((value: string, key: string) => { * responseHeaders[key] = value; * }); * * await tracker.setResponseStatusCode(response.status); * await tracker.setResponseHeaders(responseHeaders); * await tracker.setUserDataDate('timestamp', new Date()); * } catch (error: any) { * await tracker.setError(error); * } finally { * await tracker.reportDone(); * } * } * ``` * * @param {string} url The URL being requested. * @returns {object} Tracker object. */ async function RequestTracker(url: string) { const id = await InstrumentationModule.getRequestTrackerWithUrl(url); /** * An error describing the failure to receive a response, if one occurred. * * If the request was successful, this should not be called. * * @param {Error} error The response error. */ async function setError(error: Error) { const adError: ADError = { message: error.message, stack: error.stack ?? 'N/A', }; await InstrumentationModule.setRequestTrackerErrorInfo(id, adError); } /** * The status code of response, if one was received. * * If a response was received, this should be an an number. If an error occurred and a * response was not received, this should not be called. * * @param {number} statusCode The response status code. */ async function setResponseStatusCode(statusCode: number) { await InstrumentationModule.setRequestTrackerStatusCode(id, statusCode); } /** * A dictionary representing the keys and values from the client's request header. * * @param {Headers} requestHeaders The request headers. */ async function setRequestHeaders(requestHeaders: RequestTrackerHeaders) { await InstrumentationModule.setRequestTrackerRequestHeaders( id, requestHeaders, ); } /** * A dictionary representing the keys and values from the server’s response header. * * If an error occurred and a response was not received, this should not be called. * * @param {Headers} responseHeaders The received response headers. */ async function setResponseHeaders(responseHeaders: RequestTrackerHeaders) { await InstrumentationModule.setRequestTrackerResponseHeaders( id, responseHeaders, ); } /** * Stops tracking an HTTP request. * * Immediately after receiving a response or an error, set the appropriate properties and call this method * to report the outcome of the HTTP request. You should not continue to use this object after calling * this method -- if you need to track another request, create another RequestTracker. */ async function reportDone() { await InstrumentationModule.requestTrackerReport(id); } /** * Sets a key-value pair identifier that will be included with this tracker. * This identifier can be used to add `String` types. * * The `key` must be unique across your application. * The `key` namespace is distinct for each user data type. * Re-using the same `key` overwrites the previous `value`. * The `key` is limited to `Instrumentation.maxUserDataStringLength` * characters. * * The `value` is also limited to `Instrumentation.maxUserDataStringLength` * characters. * * @param {string} key A key name for the value. * @param {string} value A string value. */ async function setUserData(key: string, value: string) { await InstrumentationModule.setRequestTrackerUserData(id, key, value); } /** * Sets a key-value pair identifier that will be included with this tracker. * This identifier can be used to add the `int` types. * * The `key` must be unique across your application. * The `key` namespace is distinct for each user data type. * Re-using the same `key` overwrites the previous `value`. * The `key` is limited to `Instrumentation.maxUserDataStringLength` * characters. * * The `value` is also limited to `Instrumentation.maxUserDataStringLength` * characters. * * @param {string} key A key name for the value. * @param {number} value A number value. */ async function setUserDataNumber(key: string, value: number) { await InstrumentationModule.setRequestTrackerUserDataNumber(id, key, value); } /** * Sets a key-value pair identifier that will be included with this tracker. * This identifier can be used to add the `bool` types. * * The `key` must be unique across your application. * The `key` namespace is distinct for each user data type. * Re-using the same `key` overwrites the previous `value`. * The `key` is limited to `Instrumentation.maxUserDataStringLength` * characters. * * The `value` is also limited to `Instrumentation.maxUserDataStringLength` * characters. * * @param {string} key A key name for the value. * @param {boolean} value A boolean value. */ async function setUserDataBoolean(key: string, value: boolean) { await InstrumentationModule.setRequestTrackerUserDataBoolean( id, key, value, ); } /** * Sets a key-value pair identifier that will be included with this tracker. * This identifier can be used to add the `DateTime` types. * * The `key` must be unique across your application. * The `key` namespace is distinct for each user data type. * Re-using the same `key` overwrites the previous `value`. * The `key` is limited to `Instrumentation.maxUserDataStringLength` * characters. * * The `value` is also limited to `Instrumentation.maxUserDataStringLength` * characters. * * @param {string} key A key name for the value. * @param {Date} value A date value. */ async function setUserDataDate(key: string, value: Date) { await InstrumentationModule.setRequestTrackerUserDataDate( id, key, value.valueOf(), ); } return { setError, setRequestHeaders, setResponseHeaders, setUserData, setUserDataBoolean, setUserDataNumber, setUserDataDate, setResponseStatusCode, reportDone, }; } export {RequestTracker, type RequestTrackerHeaders};