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

  1. Schema Definition - Define your API structure with TypeScript types
  2. Client Creation - Create a client instance with the Spoosh class
  3. Plugins - Extend functionality with caching, retry, invalidation, and more
  4. 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 createClient for simple use cases without plugins, but the Spoosh class is recommended for most applications.

Next Steps

On this page