Plugins
Throttle
Limit request frequency
The throttle plugin limits how often a request can be made. Extra requests immediately return cached data instead of hitting the network.
Installation
npm install @spoosh/plugin-throttleUsage
import { Spoosh } from "@spoosh/core";
import { throttlePlugin } from "@spoosh/plugin-throttle";
const spoosh = new Spoosh<ApiSchema, Error>("/api").use([throttlePlugin()]);
// Max 1 request per second - extras return cached data
data = injectRead((api) => api("expensive").GET(), { throttle: 1000 });Options
Per-Request Options
| Option | Type | Description |
|---|---|---|
throttle | number | Max 1 request per X milliseconds. Extra requests return cached data. |
Throttle vs Debounce
| Throttle | Debounce | |
|---|---|---|
| First request | Executes immediately | Waits for delay |
| Extra requests | Returns cached data | Resets the delay timer |
| Use case | Rate-limiting expensive endpoints | Waiting for input to stop |
How It Works
The throttle plugin runs with priority 100, meaning it executes last in the middleware chain. This ensures it blocks all requests (including force fetches and retries) that exceed the throttle limit.
You can register plugins in any order - the priority system handles execution order automatically:
import { Spoosh } from "@spoosh/core";
const spoosh = new Spoosh<ApiSchema, Error>("/api").use([
cachePlugin({ staleTime: 5000 }),
throttlePlugin(), // Executes last due to priority: 100
retryPlugin(),
invalidationPlugin(),
]);