
Security News
Crates.io Implements Trusted Publishing Support
Crates.io adds Trusted Publishing support, enabling secure GitHub Actions-based crate releases without long-lived API tokens.
react-network-hijack
Advanced tools
A small higher order component for hijacking all network requests while a component is mounted
The aim for this library is to provide an easy way to mock network requests in a browser while using React components. This was originally developed with the intent of being used as part of your stories in Storybook but there is no reason it shouldn't work with a framework which runs in the browser.
To use this in your stories or other browser tests you can specify a set of urls, their methods to mock and what data to return as options to the HoC and then pass your own component in.
import withNetworkMock from "react-network-hijack";
const NetworkMocked = withNetworkMock({
GET: {
"https://jsonplaceholder.typicode.com/todos/1": {
body: {
...
},
status: 200,
delay: 200
}
},
POST: {
...
}
})(MyComponent);
The top level object contains the methods to mock. For each of these methods you can mock individual urls with a set of options.
body:
status:
delay:
If a request is not mocked an event will be emitted that you can capture in some other component and display a warning to the user. You can import this with:
import { UNMOCKED_EVENT } from "react-network-hijack";
You can also pass a whitelist to the withNetworkMock
function to stop emits happening on certain URLs. The whitelist can contain either regular expressions for fuzzy matching or strings for exact matches.
import withNetworkMock from "react-network-hijack";
const whitelist = ["example.com", /www.example.com/];
const NetworkMocked = withNetworkMock(config, whitelist)(MyComponent);
This project is licensed under the MIT License - see the LICENSE.md file for details
FAQs
A small higher order component for hijacking all network requests while a component is mounted
We found that react-network-hijack demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Crates.io adds Trusted Publishing support, enabling secure GitHub Actions-based crate releases without long-lived API tokens.
Research
/Security News
Undocumented protestware found in 28 npm packages disrupts UI for Russian-language users visiting Russian and Belarusian domains.
Research
/Security News
North Korean threat actors deploy 67 malicious npm packages using the newly discovered XORIndex malware loader.