UNPKG

@embrace-io/react-native

Version:
85 lines (84 loc) 4.19 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.applyAxiosNetworkInterceptor = void 0; const react_native_1 = require("react-native"); const gzip_js_1 = require("gzip-js"); const EmbraceManagerModule_1 = require("../../EmbraceManagerModule"); const UNKNONW_ERROR_MESSAGE = "[Embrace] Embrace was unable to capture the errored request due to limitations in the Axios API. Please raise a bug on the Axios repo if this affects you."; const logErrorWithoutResponse = (error) => { console.warn(UNKNONW_ERROR_MESSAGE, error.message, error.stack); }; const logErrorWithResponse = (method, url, embraceMetadata, dataRequest, dataReceived, status, errorMessage, errorName) => { const endInMillis = new Date().getTime(); const gzippedRequestData = dataRequest ? (0, gzip_js_1.zip)(dataRequest).length : 0; const gzippedResponseData = dataReceived ? (0, gzip_js_1.zip)(dataReceived).length : 0; const { startInMillis } = embraceMetadata; const logIOS = () => EmbraceManagerModule_1.EmbraceManagerModule.logNetworkRequest(url, method, startInMillis, endInMillis, gzippedRequestData, gzippedResponseData, status, errorMessage); const logAndroid = () => EmbraceManagerModule_1.EmbraceManagerModule.logNetworkClientError(url, method, startInMillis, endInMillis, errorName, errorMessage); return react_native_1.Platform.OS === "ios" ? logIOS() : logAndroid(); }; const applyRequestInterceptors = (request) => { const requestConfig = (config) => { if (config) { config.embraceMetadata = { startInMillis: new Date().getTime() }; } return config; }; const requestOnReject = (error) => { logErrorWithoutResponse(error); return Promise.reject(error); }; request.use(requestConfig, requestOnReject); }; const applyResponseInterceptors = (response) => { const responseConfig = (response) => { const { config: { method, url, embraceMetadata, data: dataRequest }, status, data: dataReceived, } = response; const endInMillis = new Date().getTime(); const gzippedRequestData = dataRequest ? (0, gzip_js_1.zip)(dataRequest).length : 0; const gzippedResponseData = dataReceived ? (0, gzip_js_1.zip)(dataReceived).length : 0; if (!url || !method || !embraceMetadata) { console.warn(UNKNONW_ERROR_MESSAGE); return response; } const { startInMillis } = embraceMetadata; try { EmbraceManagerModule_1.EmbraceManagerModule.logNetworkRequest(url, method, startInMillis, endInMillis, gzippedRequestData, gzippedResponseData, status, null); } catch (e) { console.warn(`[Embrace] Could not send the network request to Embrace's server.`, e); } return response; }; const responseOnRejected = (error) => { if (!error.response) { logErrorWithoutResponse(error); return Promise.reject(error); } const { config: { method, url, embraceMetadata, data: dataRequest }, status, data: dataReceived, } = error.response; if (url && method && embraceMetadata) { try { logErrorWithResponse(method, url, embraceMetadata, dataRequest, dataReceived, status, error.message, error.name); } catch (e) { console.warn(`[Embrace] Could not send the network error to Embrace's server.`, e); } } else { logErrorWithoutResponse(error); } return Promise.reject(error); }; response.use(responseConfig, responseOnRejected); }; /** * This method injects the Axios interceptors to the instance passed through params * We use those interceptors to track the request and response of your API calls. * The tracked data will be displayed on the dashboard * @param networkSDKInstance */ const applyAxiosNetworkInterceptor = (networkSDKInstance) => { const { interceptors: { request, response }, } = networkSDKInstance; applyRequestInterceptors(request); applyResponseInterceptors(response); }; exports.applyAxiosNetworkInterceptor = applyAxiosNetworkInterceptor;