|
1 |
| -import XHRInterceptor from "react-native/Libraries/Network/XHRInterceptor" |
2 | 1 | import type { ReactotronCore, Plugin } from "reactotron-core-client"
|
3 | 2 |
|
| 3 | +// Attempt to require XHRInterceptor using static paths |
| 4 | +let XHRInterceptorModule |
| 5 | +try { |
| 6 | + // Try the new path first (for RN >= 0.79) |
| 7 | + // Yay breaking changes :( https://github.com/facebook/react-native/releases/tag/v0.79.0#:~:text=APIs%3A%20Move-,XHRInterceptor,-API%20to%20src |
| 8 | + XHRInterceptorModule = require("react-native/src/private/inspector/XHRInterceptor") |
| 9 | +} catch (e) { |
| 10 | + try { |
| 11 | + // Fallback to the old path (for RN < 0.79) |
| 12 | + XHRInterceptorModule = require("react-native/Libraries/Network/XHRInterceptor") |
| 13 | + } catch (e2) { |
| 14 | + console.error("Reactotron: Failed to require XHRInterceptor from both known paths.", e, e2) |
| 15 | + console.warn( |
| 16 | + "Reactotron: XHRInterceptor could not be loaded. Network monitoring will be disabled." |
| 17 | + ) |
| 18 | + // Assign a dummy object later if checks fail |
| 19 | + XHRInterceptorModule = null // Indicate failure to require |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +let XHRInterceptor |
| 24 | +if (XHRInterceptorModule) { |
| 25 | + // Check if methods are directly on the module |
| 26 | + if ( |
| 27 | + typeof XHRInterceptorModule.setSendCallback === "function" && |
| 28 | + typeof XHRInterceptorModule.setResponseCallback === "function" && |
| 29 | + typeof XHRInterceptorModule.enableInterception === "function" |
| 30 | + ) { |
| 31 | + XHRInterceptor = XHRInterceptorModule |
| 32 | + } |
| 33 | + // Check if methods are on the default export |
| 34 | + else if ( |
| 35 | + XHRInterceptorModule.default && |
| 36 | + typeof XHRInterceptorModule.default.setSendCallback === "function" && |
| 37 | + typeof XHRInterceptorModule.default.setResponseCallback === "function" && |
| 38 | + typeof XHRInterceptorModule.default.enableInterception === "function" |
| 39 | + ) { |
| 40 | + XHRInterceptor = XHRInterceptorModule.default |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +// If still no valid XHRInterceptor after checking module and module.default, assign the dummy |
| 45 | +if (!XHRInterceptor) { |
| 46 | + // Log error only if we initially managed to require *something* |
| 47 | + if (XHRInterceptorModule) { |
| 48 | + console.error("Reactotron: Required XHRInterceptor module does not have expected methods.") |
| 49 | + console.warn("Reactotron: Network monitoring will be disabled.") |
| 50 | + } |
| 51 | + // Assign a dummy object to prevent crashes later when calling its methods |
| 52 | + XHRInterceptor = { |
| 53 | + setSendCallback: () => {}, |
| 54 | + setResponseCallback: () => {}, |
| 55 | + enableInterception: () => {}, |
| 56 | + } |
| 57 | +} |
| 58 | + |
4 | 59 | /**
|
5 | 60 | * Don't include the response bodies for images by default.
|
6 | 61 | */
|
|
0 commit comments