
Security News
Axios Maintainer Confirms Social Engineering Attack Behind npm Compromise
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.
snap-fetch
Advanced tools
snap-fetch is a light weight data fetching tool built for React that allows you to fetch data from an API, cache it, and store it in Redux using Redux Toolkit and Redux Snap. It provides intuitive hooks for performing *8queries** and mutations, as well as a hook for configuring **global api options**.
Web applications typically require data from a server in order to display it. They also typically need to update that data, communicate those modifications to the server, and maintain the cached data on the client in sync with the data on the server. This is made more hard by the requirement to include additional behaviors utilized in today's applications:
You can install snap-fetch using npm or yarn:
npm install snap-fetch
or
yarn add snap-fetch
import { name, reducer } from "snap-fetch";
export const rootReducer = combineReducers({
[name]: reducer,
});
/**
* Create the store with dynamic reducers
*/
import { configureStore } from "@reduxjs/toolkit";
import createSagaMiddleware from "redux-saga";
import { rootReducer } from "./reducers";
import { rootSnapFetchSaga } from "snap-fetch";
export function configureAppStore() {
const sagaMiddleware = createSagaMiddleware();
// Create the Redux store with middleware
const store = configureStore({
reducer: rootReducer,
middleware: (getDefaultMiddleware) =>
getDefaultMiddleware({
serializableCheck: false,
}).concat(sagaMiddleware),
});
// Run the root Snap
sagaMiddleware.run(rootSnapFetchSaga);
return { store };
}
The useSetBaseConfiguration hook is used to configure the base options for Snap.
useSetBaseConfiguration(options);
options (object): The configuration options.The options object accepts the following properties:
// To root of you project like App.tsx main.tsx
import { useSetBaseConfiguration } from "snap-fetch";
const baseUrl = "https://jsonplaceholder.typicode.com";
useSetBaseConfiguration({
baseUrl, // Required
disableCaching: boolean, // if true caching will be disabled, // this is global, can be overridden by individual disableCaching properties
// Below has no effect if you are using your own fetch function
headers: new Headers({
Authorization: `Bearer ${token}`,
}),
});
This hook allows you to fetch data from the server using the Snaps and store it in the redux store, it is configured to know if the same endpoint is called with the same queryParams, it would only refetch data if the cache is empty or mutated by mutation, or if queryParams are changed...
It uses the endpoint + queryParams to cache the state, which allow it to avoid unnecessary fetch requests.
it accepts two parameters
type RequestOptions = {
method?: Method,
disableCaching?: boolean, // will disable caching for the current endpoint request
fetchFunction?: (endpoint: string) => Promise<Response>, // custom fetch function if you don't like the built-in.
tags?: Tags, // Tags will be used to invalidate on mutation requests.
filter?: { [key: string]: number | boolean | string | undefined | null }, // your filters except for pagination.
pollingInterval?: number, // polling interval for polling requests
skip?: boolean, // skip on mount request for the current endpoint
single?: boolean, // to tell the snap-fetcher query you don't want to use pagination.
};
The useSnapQuery hook returns a query result object with the following properties:
useSnapQuery is a generic type function, the type is used to tell the type of the data returned from the api call.
data (T | undefined): The fetched data.isLoading (boolean): A flag indicating if the query is in progress.isError (boolean): A flag indicating if an error occurred during the query.error (Error | undefined): The error object, if any.paginationOptions (object): The pagination options for the query.refetch (function): A function to manually trigger a refetch of the query.Queries have built in pagination support the result of useSnapQuery will return a paginationOptions object with the following properties:
{
lastPage: number;
currentShowingItems: number | undefined;
totalItems: number;
changePageNo: (pageNo: number) => void;
changeSize: (size: number) => void;
pageNo: number;
size: number;
}
Import the necessary hooks from the snap-fetch package:
import { useSnapQuery } from "snap-fetch";
const MyComponent = () => {
const { data, isLoading, error } =
useSnapQuery <
Users >
("users",
{
tags: "getUsers",
});
if (isLoading) {
return <div>Loading...</div>;
}
if (error) {
return <div>Error: {error.message}</div>;
}
return (
<div>
{data.map((user) => (
<div key={user.id}>{user.name}</div>
))}
</div>
);
};
This hook allows you to manipulate the data and make mutation calls it will automatically revalidate the cache if queries with the same endpoint are available.
It accept two parameters:
type RequestOptions = {
method?: Method,
fetchFunction?: (endpoint: string) => Promise<Response>, // custom fetch function if you don't like the built-in.
invalidateTags?: Tags, // Tags will be used to invalidate on mutation requests.
body?: any, // Request body, will automatically remove the body if you accidentally use methods like "GET" or "HEAD"
};
data (T | undefined): The returned data.isLoading (boolean): A flag indicating if the query is in progress.isError (boolean): A flag indicating if an error occurred during the query.error (Error | undefined): The error object, if any.mutate (function): A function to trigger a fetch request.To perform a mutation and send data to the API, use the useSnapMutation hook. Here's an example:
const MyComponent = () => {
const { mutate, isLoading, error } = useSnapMutation("createUser", {
invalidateTags: ["getUsers"],
});
const handleSubmit = async (data) => {
try {
await mutate(data);
console.log("User created successfully!");
} catch (e) {
console.error("Error creating user:", e);
}
};
if (isLoading) {
return <div>Loading...</div>;
}
if (error) {
return <div>Error: {error.message}</div>;
}
return (
<div>
<form onSubmit={handleSubmit}>
{/* form fields */}
<button type="submit">Create User</button>
</form>
</div>
);
};
For further information please see the full documentation.
FAQs
## FEATURES
We found that snap-fetch demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 0 open source maintainers 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
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.

Security News
The Axios compromise shows how time-dependent dependency resolution makes exposure harder to detect and contain.