Core
Core Concepts
Understanding the fundamentals of Spoosh
The @spoosh/core package provides the foundation for the Spoosh type-safe API toolkit. This section covers the core concepts you need to understand.
Overview
Spoosh is built around a few key concepts:
- Schema Definition - Define your API structure with TypeScript types
- Client Creation - Create a client instance with the
Spooshclass - Plugins - Extend functionality with caching, retry, invalidation, and more
- Response Format - Consistent response structure across all requests
Architecture
┌─────────────────────────────────────────────────────────┐
│ Your App │
├─────────────────────────────────────────────────────────┤
│ useRead / useWrite / useInfiniteRead (@spoosh/react) │
├─────────────────────────────────────────────────────────┤
│ Plugins (cache, invalidation, retry, etc.) │
├─────────────────────────────────────────────────────────┤
│ Spoosh (@spoosh/core) │
├─────────────────────────────────────────────────────────┤
│ fetch API │
└─────────────────────────────────────────────────────────┘Creating a Client
Use the Spoosh class to create your API client with plugins:
import { Spoosh } from "@spoosh/core";
import { cachePlugin } from "@spoosh/plugin-cache";
import { deduplicationPlugin } from "@spoosh/plugin-deduplication";
import { invalidationPlugin } from "@spoosh/plugin-invalidation";
const spoosh = new Spoosh<ApiSchema, Error>("/api").use([
cachePlugin({ staleTime: 5000 }),
deduplicationPlugin(), // Prevent duplicate requests during invalidation
invalidationPlugin(),
]);Note: There's also
createClientfor simple use cases without plugins, but theSpooshclass is recommended for most applications.