Gathering detailed insights and metrics for react-native-netwatch
Gathering detailed insights and metrics for react-native-netwatch
Gathering detailed insights and metrics for react-native-netwatch
Gathering detailed insights and metrics for react-native-netwatch
npm install react-native-netwatch
Typescript
Module System
Node Version
NPM Version
TypeScript (83.76%)
Objective-C (5.99%)
Java (5.87%)
JavaScript (1.9%)
Kotlin (1.04%)
Swift (0.73%)
Ruby (0.5%)
Starlark (0.22%)
Total Downloads
153,558
Last Day
53
Last Week
436
Last Month
2,100
Last Year
48,150
19 Stars
343 Commits
6 Forks
7 Watching
104 Branches
7 Contributors
Latest Version
1.3.16
Package Id
react-native-netwatch@1.3.16
Unpacked Size
795.25 kB
Size
228.81 kB
File Count
142
NPM Version
8.19.4
Node Version
16.20.2
Publised On
24 Jan 2024
Cumulative downloads
Total Downloads
Last day
-23.2%
53
Compared to previous day
Last week
-12.8%
436
Compared to previous week
Last month
-25.7%
2,100
Compared to previous month
Last year
4.5%
48,150
Compared to previous year
10
39
Network traffic logger for React Native.
Includes an interface to see http traffic from RN and native side
To avoid to have too much dependencies and conflict versions, before install, you must have these dependancies in your react-native project.
Netwatch has react-native-vector-icons as dependency. Be sure that you have these fonts installed in your project:
Please refer to this page for more details: Install fonts react-native-vector-icons
1yarn add react-native-netwatch
or
1npm install react-native-netwatch
Inside your project, go to ios directory and execute pod install
1cd ios && pod install && ..
OR simply
1npx pod-install
If you want add Network traffic in your project, just import 'react-native-netwatch'
and add the Netwatch component in the most higher position in the tree of components.
For example, just after your store provider or your root component
Now, when you will launch your application and shake the device, it will display automatically Netwatch.
You have two possibilities to activate Netwatch in your project. With a button from you app or by shaking your phone. If you want activate Netwatch with a button from your app, you must disable shake and instead pass the props onPressClose and visible.
1 2import { Netwatch } from 'react-native-netwatch'; 3 4const App = () => { 5 const [netwatchVisible, setNetwatchVisible] = useState(false); 6 7 return ( 8 <Provider store={store}> 9 <Netwatch 10 enabled={true} 11 interceptIOS={true} 12 /> 13 <AppNavigator /> 14 </Provider> 15 ); 16}; 17 18export default App;
1 2import { Netwatch } from 'react-native-netwatch'; 3 4const App = () => { 5 const [netwatchVisible, setNetwatchVisible] = useState(false); 6 7 return ( 8 <Provider store={store}> 9 <Netwatch 10 enabled={true} 11 interceptIOS={true} 12 visible={netwatchVisible} 13 onPressClose={() => setNetwatchVisible(false)} 14 disableShake 15 /> 16 <TouchableHighlight 17 style={styles.openButton} 18 onPress={() => setNetwatchVisible(true)} 19 testID="buttonDisplayNetwatch" 20 > 21 <Text style={styles.textStyle}>Display Netwatch</Text> 22 </TouchableHighlight> 23 <AppNavigator /> 24 </Provider> 25 ); 26}; 27 28export default App;
You can add 'react-native-netwatch' as a middleware to catch Redux actions
To do that, just import reduxLogger from 'react-native-netwatch'
1import { applyMiddleware, combineReducers, compose, createStore } from 'redux'; 2import appActionsReducer from './reducers/appActionsReducer'; 3import { reduxLogger } from 'react-native-netwatch'; 4 5const createReducer = () => (state, action) => 6 combineReducers({ 7 app: appActionsReducer, 8 })(state, action); 9 10const store = createStore( 11 createReducer(), 12 compose(applyMiddleware(reduxLogger)), 13); 14 15export default store; 16
Example in our demo application here
To be able to intercept requests from Android side and display them into Netwatch
You have to add to your OkHttp client Netwatch interceptor
1okHttpClient.addInterceptor(new NetwatchInterceptor(context));
Example in our demo application here
Nothing to do on native side for the iOS.
You have just to set interceptIOS
to true and it will intercept requests which use URLProtocol
on native side and display them into Netwatch
1'Bridging-Header.h'
2
3#import <NetwatchInterceptor.h>
1let configuration = URLSessionConfiguration.default
2configuration.protocolClasses?.insert(NetwatchInterceptor.self, at: 0)
3let sessionManager = Alamofire.SessionManager(configuration: configuration)
4sessionManager.request(...)
You can have statistics and see how many requests are succeeded or failed. By default, the indicator is closed. If you want the percentage, just press the indactor to opened it. Press again to close.
If you have applied a filter, stats are updated for current filtered view. If you have filtered to see Redux Action, the indicator is not interactive and just show a purple indicator.
If you want, you can add extra datas in the redux items to have more visual information. Instead of just see 'redux action' as label, your own text will be displayed. To do that, you must passed to Netwatch a props called reduxConfig. This is an object where each key correspond to a redux action in your project. All values must be string.
1 2import { Netwatch } from 'react-native-netwatch'; 3 4const reduxConfigExample = { 5 DOWNLOAD_APP_TRANSLATIONS_SUCCESS: "👨 - Extra info", 6} 7 8const App = () => { 9 const [netwatchVisible, setNetwatchVisible] = useState(false); 10 11 return ( 12 <Provider store={store}> 13 <Netwatch 14 enabled={true} 15 interceptIOS={true} 16 reduxConfig={reduxConfigExample} 17 /> 18 <AppNavigator /> 19 </Provider> 20 ); 21}; 22 23export default App;
You will see something like that:
There is a known incompatibility between Netwatch and Reactotron. If you want to redirect the requests into Reactotron, you should set the props useReactotron
to true (have to reload the app if you edit its value). To go back to netwatch revert the props to false and reload again.
At this moment, it is not possible to display requests into Netwatch and Reactotron at the same time. You must choose between these tools.
Params | Type | Default | Mandatory ? | Description |
---|---|---|---|---|
enabled | Boolean | true | yes | Enabled/Disabled logger to intercept request and actions |
visible | Boolean | false | no | Show the main screen to display intercepted requests/actions |
onPressClose | Function | undefined | no | Called when Close button is pressed in the Main screen |
interceptIOS | Boolean | false | no | Intercept native iOS requests |
disableShake | Boolean | false | no | Set to true to disable shake feature to display Netwatch |
maxRequests | Number | 100 | no | Maximum requests displayed |
showStats | Boolean | true | no | Show stats indicator |
reduxConfig | Object | {} | no | Extra infos for Redux Action. Accept only string as vaulues |
useReactotron | Boolean | false | no | Redirect requests to Reactotron instead of Netwatch |
theme | String | 'dark' | no | Possible values are 'dark' or 'light' |
Netwatch also provides a way to mock responses which is useful during testing and development.
Netwatch UI provides a user-friendly way to create, export, and import mocks directly from the mobile application.
To create a mock:
To export a mock:
The mock will be copied to the clipboard in a JSON format.
To import a mock:
The mock from the clipboard will be parsed and added to the list of mocked requests.
You can easily control which mock responses are active at any time using the Netwatch UI.
To enable a mock:
To disable a mock:
Netwatch provides three ways to mock responses with presets using props:
loadMockPresetFromClipboard
)loadMockPresetFromInputParameters
)mockPresets
prop.mockPresets
mockPresets
is an array of MockResponse
objects defining the mock HTTP request/response behavior.
1const mockResponses = [ 2 { 3 method: 'GET', 4 url: '/api/v1/users', 5 status: 200, 6 body: { message: 'Success' }, 7 }, 8]; 9 10<Netwatch mockPresets={mockResponses} enabled={true} />;
loadMockPresetFromClipboard
Copy your mock response data to your clipboard in the correct format and set loadMockPresetFromClipboard
to true
.
loadMockPresetFromInputParameters
Set loadMockPresetFromInputParameters
to true
and pass the mock data as the mockPresets
prop.
Note: If both loadMockPresetFromClipboard
and loadMockPresetFromInputParameters
are true
, the data from the clipboard will be used.
Simulate request timeouts with the timeout
field in MockResponse
.
1const mockResponses = [ 2 { 3 method: 'GET', 4 url: '/api/v1/users', 5 status: 200, 6 body: { message: 'Success' }, 7 timeout: 5, // Simulates a delay of 5 seconds 8 }, 9]; 10 11<Netwatch mockPresets={mockResponses} enabled={true} />;
Mock HTTP headers with the headers
field in MockResponse
.
1const mockResponses = [ 2 { 3 method: 'GET', 4 url: '/api/v1/users', 5 status: 200, 6 body: { message: 'Success' }, 7 headers: { 8 'Content-Type': 'application/json', 9 'X-Custom-Header': 'CustomHeaderValue', 10 }, 11 }, 12]; 13 14<Netwatch mockPresets={mockResponses} enabled={true} />;
Simulate HTTP error statuses such as 401, 500, and 400.
1const mockResponses = [ 2 { 3 method: 'GET', 4 url: '/api/v1/secure', 5 status: 401, 6 body: { message: 'Unauthorized' }, 7 }, 8 { 9 method: 'POST', 10 url: '/api/v1/users', 11 status: 500, 12 body: { message: 'Internal Server Error' }, 13 }, 14 { 15 method: 'POST', 16 url: '/api/v1/users', 17 status: 400, 18 body: { message: 'Bad Request' }, 19 }, 20]; 21 22<Netwatch mockPresets={mockResponses} enabled={true} />;
You can integrate Netwatch with Appium to facilitate your automation testing.
Use Appium's mobile: launchApp
method to start your app with specified parameters:
1const mockResponses = [ 2 { 3 method: 'GET', 4 url: '/api/v1/users', 5 status: 200, 6 body: { message: 'Success' } 7 } 8]; 9 10driver.execute('mobile: launchApp', { 11 sessionId: driver.sessionId, 12 bundleId: driver.capabilities.bundleId, 13 arguments: ['-netwatchMocks', JSON.stringify(mockResponses)], 14});
Then in your React Native application, set loadMockPresetFromInputParameters
to true
:
1<Netwatch enabled={true} loadMockPresetFromInputParameters={true} />
For Android, use Appium's startActivity
method:
1const mockResponses = [ 2 { 3 method: 'GET', 4 url: '/api/v1/users', 5 status: 200, 6 body: { message: 'Success' } 7 } 8]; 9 10driver.startActivity( 11 driver.capabilities.appPackage, 12 driver.capabilities.appActivity, 13 undefined, 14 undefined, 15 undefined, 16 undefined, 17 undefined, 18 `--es 'netwatchMocks' '${JSON.stringify(mockResponses)}'` 19);
Then in your React Native application, set loadMockPresetFromInputParameters
to true
:
1<Netwatch enabled={true} loadMockPresetFromInputParameters={true} />
No vulnerabilities found.
No security vulnerabilities found.