Spoosh
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-throttle

Usage

import { Spoosh } from "@spoosh/core";
import { throttlePlugin } from "@spoosh/plugin-throttle";

const client = 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

OptionTypeDescription
throttlenumberMax 1 request per X milliseconds. Extra requests return cached data.

Throttle vs Debounce

ThrottleDebounce
First requestExecutes immediatelyWaits for delay
Extra requestsReturns cached dataResets the delay timer
Use caseRate-limiting expensive endpointsWaiting for input to stop

Plugin Order

Register the throttle plugin at the end of your plugin list to ensure it blocks all requests, including force fetches:

import { Spoosh } from "@spoosh/core";

const client = new Spoosh<ApiSchema, Error>("/api").use([
  cachePlugin({ staleTime: 5000 }),
  deduplicationPlugin(),
  invalidationPlugin(),
  retryPlugin(),
  throttlePlugin(),
]);

On this page